- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# compress.c

    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.c,v 1.1 2003/11/09 01:37:56 gabest Exp $ */
    00007
    00008 #include "zlib.h"
    00009
    00010 /* ===========================================================================
    00011      Compresses the source buffer into the destination buffer. The level
    00012    parameter has the same meaning as in deflateInit.  sourceLen is the byte
    00013    length of the source buffer. Upon entry, destLen is the total size of the
    00014    destination buffer, which must be at least 0.1% larger than sourceLen plus
    00015    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
    00016
    00017      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
    00018    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
    00019    Z_STREAM_ERROR if the level parameter is invalid.
    00020 */
    00021 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
    00022     Bytef *dest;
    00023     uLongf *destLen;
    00024     const Bytef *source;
    00025     uLong sourceLen;
    00026     int level;
    00027 {
    00028     z_stream stream;
    00029     int err;
    00030
    00031     stream.next_in = (Bytef*)source;
    00032     stream.avail_in = (uInt)sourceLen;
    00033 #ifdef MAXSEG_64K
    00034     /* Check for source > 64K on 16-bit machine: */
    00035     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
    00036 #endif
    00037     stream.next_out = dest;
    00038     stream.avail_out = (uInt)*destLen;
    00039     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
    00040
    00041     stream.zalloc = (alloc_func)0;
    00042     stream.zfree = (free_func)0;
    00043     stream.opaque = (voidpf)0;
    00044
    00045     err = deflateInit(&stream, level);
    00046     if (err != Z_OK) return err;
    00047
    00048     err = deflate(&stream, Z_FINISH);
    00049     if (err != Z_STREAM_END) {
    00050         deflateEnd(&stream);
    00051         return err == Z_OK ? Z_BUF_ERROR : err;
    00052     }
    00053     *destLen = stream.total_out;
    00054
    00055     err = deflateEnd(&stream);
    00056     return err;
    00057 }
    00058
    00059 /* ===========================================================================
    00060  */
    00061 int ZEXPORT compress (dest, destLen, source, sourceLen)
    00062     Bytef *dest;
    00063     uLongf *destLen;
    00064     const Bytef *source;
    00065     uLong sourceLen;
    00066 {
    00067     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
    00068 }