compress.cc

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: compress.cc,v 1.1 2005-05-12 21:04:53 duns Exp $ */
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 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
00023 {
00024     z_stream stream;
00025     int err;
00026 
00027     stream.next_in = (Bytef*)source;
00028     stream.avail_in = (uInt)sourceLen;
00029 #ifdef MAXSEG_64K
00030     /* Check for source > 64K on 16-bit machine: */
00031     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
00032 #endif
00033     stream.next_out = dest;
00034     stream.avail_out = (uInt)*destLen;
00035     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
00036 
00037     stream.zalloc = (alloc_func)0;
00038     stream.zfree = (free_func)0;
00039     stream.opaque = (voidpf)0;
00040 
00041     err = deflateInit(&stream, level);
00042     if (err != Z_OK) return err;
00043 
00044     err = deflate(&stream, Z_FINISH);
00045     if (err != Z_STREAM_END) {
00046         deflateEnd(&stream);
00047         return err == Z_OK ? Z_BUF_ERROR : err;
00048     }
00049     *destLen = stream.total_out;
00050 
00051     err = deflateEnd(&stream);
00052     return err;
00053 }
00054 
00055 /* ===========================================================================
00056  */
00057 int ZEXPORT compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
00058 {
00059     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
00060 }
00061 
00062 /* ===========================================================================
00063      If the default memLevel or windowBits for deflateInit() is changed, then
00064    this function needs to be updated.
00065  */
00066 uLong ZEXPORT compressBound (uLong sourceLen)
00067 {
00068     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
00069 }

Generated on Mon May 27 17:47:34 2013 for Geant4 by  doxygen 1.4.7