Back to index

tetex-bin  3.0
compress.c
Go to the documentation of this file.
00001 /* compress.c -- compress a memory buffer
00002  * Copyright (C) 1995-2002 Jean-loup Gailly.
00003  * For conditions of distribution and use, see copyright notice in zlib.h
00004  */
00005 
00006 /* @(#) $Id$ */
00007 
00008 #define ZLIB_INTERNAL
00009 #include "zlib.h"
00010 
00011 /* ===========================================================================
00012      Compresses the source buffer into the destination buffer. The level
00013    parameter has the same meaning as in deflateInit.  sourceLen is the byte
00014    length of the source buffer. Upon entry, destLen is the total size of the
00015    destination buffer, which must be at least 0.1% larger than sourceLen plus
00016    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
00017 
00018      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00019    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
00020    Z_STREAM_ERROR if the level parameter is invalid.
00021 */
00022 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
00023     Bytef *dest;
00024     uLongf *destLen;
00025     const Bytef *source;
00026     uLong sourceLen;
00027     int level;
00028 {
00029     z_stream stream;
00030     int err;
00031 
00032     stream.next_in = (Bytef*)source;
00033     stream.avail_in = (uInt)sourceLen;
00034 #ifdef MAXSEG_64K
00035     /* Check for source > 64K on 16-bit machine: */
00036     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
00037 #endif
00038     stream.next_out = dest;
00039     stream.avail_out = (uInt)*destLen;
00040     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
00041 
00042     stream.zalloc = (alloc_func)0;
00043     stream.zfree = (free_func)0;
00044     stream.opaque = (voidpf)0;
00045 
00046     err = deflateInit(&stream, level);
00047     if (err != Z_OK) return err;
00048 
00049     err = deflate(&stream, Z_FINISH);
00050     if (err != Z_STREAM_END) {
00051         deflateEnd(&stream);
00052         return err == Z_OK ? Z_BUF_ERROR : err;
00053     }
00054     *destLen = stream.total_out;
00055 
00056     err = deflateEnd(&stream);
00057     return err;
00058 }
00059 
00060 /* ===========================================================================
00061  */
00062 int ZEXPORT compress (dest, destLen, source, sourceLen)
00063     Bytef *dest;
00064     uLongf *destLen;
00065     const Bytef *source;
00066     uLong sourceLen;
00067 {
00068     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
00069 }
00070 
00071 /* ===========================================================================
00072      If the default memLevel or windowBits for deflateInit() is changed, then
00073    this function needs to be updated.
00074  */
00075 uLong ZEXPORT compressBound (sourceLen)
00076     uLong sourceLen;
00077 {
00078     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
00079 }