os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/compress.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* compress.c -- compress a memory buffer
sl@0
     2
 * Copyright (C) 1995-1998 Jean-loup Gailly.
sl@0
     3
 * For conditions of distribution and use, see copyright notice in zlib.h 
sl@0
     4
 */
sl@0
     5
sl@0
     6
/* @(#) $Id$ */
sl@0
     7
sl@0
     8
#include "zlib.h"
sl@0
     9
sl@0
    10
/* ===========================================================================
sl@0
    11
     Compresses the source buffer into the destination buffer. The level
sl@0
    12
   parameter has the same meaning as in deflateInit.  sourceLen is the byte
sl@0
    13
   length of the source buffer. Upon entry, destLen is the total size of the
sl@0
    14
   destination buffer, which must be at least 0.1% larger than sourceLen plus
sl@0
    15
   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
sl@0
    16
sl@0
    17
     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
sl@0
    18
   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
sl@0
    19
   Z_STREAM_ERROR if the level parameter is invalid.
sl@0
    20
*/
sl@0
    21
int ZEXPORT compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
sl@0
    22
{
sl@0
    23
    z_stream stream;
sl@0
    24
    int err;
sl@0
    25
sl@0
    26
    stream.next_in = (Bytef*)source;
sl@0
    27
    stream.avail_in = (uInt)sourceLen;
sl@0
    28
#ifdef MAXSEG_64K
sl@0
    29
    /* Check for source > 64K on 16-bit machine: */
sl@0
    30
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
sl@0
    31
#endif
sl@0
    32
    stream.next_out = dest;
sl@0
    33
    stream.avail_out = (uInt)*destLen;
sl@0
    34
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
sl@0
    35
sl@0
    36
    stream.zalloc = (alloc_func)0;
sl@0
    37
    stream.zfree = (free_func)0;
sl@0
    38
    stream.opaque = (voidpf)0;
sl@0
    39
sl@0
    40
    err = deflateInit(&stream, level);
sl@0
    41
    if (err != Z_OK) return err;
sl@0
    42
sl@0
    43
    err = deflate(&stream, Z_FINISH);
sl@0
    44
    if (err != Z_STREAM_END) {
sl@0
    45
        deflateEnd(&stream);
sl@0
    46
        return err == Z_OK ? Z_BUF_ERROR : err;
sl@0
    47
    }
sl@0
    48
    *destLen = stream.total_out;
sl@0
    49
sl@0
    50
    err = deflateEnd(&stream);
sl@0
    51
    return err;
sl@0
    52
}
sl@0
    53
sl@0
    54
/* ===========================================================================
sl@0
    55
 */
sl@0
    56
int ZEXPORT compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
sl@0
    57
{
sl@0
    58
    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
sl@0
    59
}