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