Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:10

0001 /* compress.c -- compress a memory buffer
0002  * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 /* @(#) $Id$ */
0007 
0008 #define ZLIB_INTERNAL
0009 #include "zlib.h"
0010 
0011 /* ===========================================================================
0012      Compresses the source buffer into the destination buffer. The level
0013    parameter has the same meaning as in deflateInit.  sourceLen is the byte
0014    length of the source buffer. Upon entry, destLen is the total size of the
0015    destination buffer, which must be at least 0.1% larger than sourceLen plus
0016    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
0017 
0018      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
0019    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
0020    Z_STREAM_ERROR if the level parameter is invalid.
0021 */
0022 int ZEXPORT compress2(dest, destLen, source, sourceLen, level)
0023     Bytef *dest;
0024     uLongf *destLen;
0025     const Bytef *source;
0026     uLong sourceLen;
0027     int level;
0028 {
0029     z_stream stream;
0030     int err;
0031     const uInt max = (uInt)-1;
0032     uLong left;
0033 
0034     left = *destLen;
0035     *destLen = 0;
0036 
0037     stream.zalloc = (alloc_func)0;
0038     stream.zfree = (free_func)0;
0039     stream.opaque = (voidpf)0;
0040 
0041     err = deflateInit(&stream, level);
0042     if (err != Z_OK) return err;
0043 
0044     stream.next_out = dest;
0045     stream.avail_out = 0;
0046     stream.next_in = (z_const Bytef *)source;
0047     stream.avail_in = 0;
0048 
0049     do {
0050         if (stream.avail_out == 0) {
0051             stream.avail_out = left > (uLong)max ? max : (uInt)left;
0052             left -= stream.avail_out;
0053         }
0054         if (stream.avail_in == 0) {
0055             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
0056             sourceLen -= stream.avail_in;
0057         }
0058         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
0059     } while (err == Z_OK);
0060 
0061     *destLen = stream.total_out;
0062     deflateEnd(&stream);
0063     return err == Z_STREAM_END ? Z_OK : err;
0064 }
0065 
0066 /* ===========================================================================
0067  */
0068 int ZEXPORT compress(dest, destLen, source, sourceLen)
0069     Bytef *dest;
0070     uLongf *destLen;
0071     const Bytef *source;
0072     uLong sourceLen;
0073 {
0074     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
0075 }
0076 
0077 /* ===========================================================================
0078      If the default memLevel or windowBits for deflateInit() is changed, then
0079    this function needs to be updated.
0080  */
0081 uLong ZEXPORT compressBound(sourceLen)
0082     uLong sourceLen;
0083 {
0084     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
0085            (sourceLen >> 25) + 13;
0086 }