annotate src/zlib-1.2.8/compress.c @ 128:5b4145a0d408

Current zlib source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 14:33:52 +0100
parents
children
rev   line source
cannam@128 1 /* compress.c -- compress a memory buffer
cannam@128 2 * Copyright (C) 1995-2005 Jean-loup Gailly.
cannam@128 3 * For conditions of distribution and use, see copyright notice in zlib.h
cannam@128 4 */
cannam@128 5
cannam@128 6 /* @(#) $Id$ */
cannam@128 7
cannam@128 8 #define ZLIB_INTERNAL
cannam@128 9 #include "zlib.h"
cannam@128 10
cannam@128 11 /* ===========================================================================
cannam@128 12 Compresses the source buffer into the destination buffer. The level
cannam@128 13 parameter has the same meaning as in deflateInit. sourceLen is the byte
cannam@128 14 length of the source buffer. Upon entry, destLen is the total size of the
cannam@128 15 destination buffer, which must be at least 0.1% larger than sourceLen plus
cannam@128 16 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
cannam@128 17
cannam@128 18 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
cannam@128 19 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
cannam@128 20 Z_STREAM_ERROR if the level parameter is invalid.
cannam@128 21 */
cannam@128 22 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
cannam@128 23 Bytef *dest;
cannam@128 24 uLongf *destLen;
cannam@128 25 const Bytef *source;
cannam@128 26 uLong sourceLen;
cannam@128 27 int level;
cannam@128 28 {
cannam@128 29 z_stream stream;
cannam@128 30 int err;
cannam@128 31
cannam@128 32 stream.next_in = (z_const Bytef *)source;
cannam@128 33 stream.avail_in = (uInt)sourceLen;
cannam@128 34 #ifdef MAXSEG_64K
cannam@128 35 /* Check for source > 64K on 16-bit machine: */
cannam@128 36 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
cannam@128 37 #endif
cannam@128 38 stream.next_out = dest;
cannam@128 39 stream.avail_out = (uInt)*destLen;
cannam@128 40 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
cannam@128 41
cannam@128 42 stream.zalloc = (alloc_func)0;
cannam@128 43 stream.zfree = (free_func)0;
cannam@128 44 stream.opaque = (voidpf)0;
cannam@128 45
cannam@128 46 err = deflateInit(&stream, level);
cannam@128 47 if (err != Z_OK) return err;
cannam@128 48
cannam@128 49 err = deflate(&stream, Z_FINISH);
cannam@128 50 if (err != Z_STREAM_END) {
cannam@128 51 deflateEnd(&stream);
cannam@128 52 return err == Z_OK ? Z_BUF_ERROR : err;
cannam@128 53 }
cannam@128 54 *destLen = stream.total_out;
cannam@128 55
cannam@128 56 err = deflateEnd(&stream);
cannam@128 57 return err;
cannam@128 58 }
cannam@128 59
cannam@128 60 /* ===========================================================================
cannam@128 61 */
cannam@128 62 int ZEXPORT compress (dest, destLen, source, sourceLen)
cannam@128 63 Bytef *dest;
cannam@128 64 uLongf *destLen;
cannam@128 65 const Bytef *source;
cannam@128 66 uLong sourceLen;
cannam@128 67 {
cannam@128 68 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
cannam@128 69 }
cannam@128 70
cannam@128 71 /* ===========================================================================
cannam@128 72 If the default memLevel or windowBits for deflateInit() is changed, then
cannam@128 73 this function needs to be updated.
cannam@128 74 */
cannam@128 75 uLong ZEXPORT compressBound (sourceLen)
cannam@128 76 uLong sourceLen;
cannam@128 77 {
cannam@128 78 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
cannam@128 79 (sourceLen >> 25) + 13;
cannam@128 80 }