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