annotate src/zlib-1.2.8/compress.c @ 43:5ea0608b923f

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