stdlibs/libz/zlib/compress.cpp
changeset 65 c4aad78f92f5
parent 50 79045913e4e9
child 66 38bdaa106551
equal deleted inserted replaced
50:79045913e4e9 65:c4aad78f92f5
     1 /* compress.cpp -- compress a memory buffer
       
     2  * Copyright (C) 1995-2003 Jean-loup Gailly.
       
     3  * For conditions of distribution and use, see copyright notice in zlib.h
       
     4  */
       
     5 
       
     6 /* @(#) $Id: compress.cpp,v 1.1.2.1 2008/08/14 15:26:57 e0222316 Exp $ */
       
     7 
       
     8 #define ZLIB_INTERNAL
       
     9 #include "libzcore.h"
       
    10 
       
    11 /* ===========================================================================
       
    12      Compresses the source buffer into the destination buffer. The level
       
    13    parameter has the same meaning as in deflateInit.  sourceLen is the byte
       
    14    length of the source buffer. Upon entry, destLen is the total size of the
       
    15    destination buffer, which must be at least 0.1% larger than sourceLen plus
       
    16    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
       
    17 
       
    18      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
       
    19    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
       
    20    Z_STREAM_ERROR if the level parameter is invalid.
       
    21 */
       
    22 
       
    23 #ifdef __SYMBIAN32__
       
    24 EXPORT_C int  compress2_r (Bytef * dest, uLongf * destLen,const Bytef *  source,uLong  sourceLen,int level)
       
    25 #else
       
    26 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
       
    27     Bytef *dest;
       
    28     uLongf *destLen;
       
    29     const Bytef *source;
       
    30     uLong sourceLen;
       
    31     int level;
       
    32 #endif //__SYMBIAN32__
       
    33 {
       
    34     z_stream stream;
       
    35     int err;
       
    36 
       
    37     stream.next_in = (Bytef*)source;
       
    38     stream.avail_in = (uInt)sourceLen;
       
    39 #ifdef MAXSEG_64K
       
    40     /* Check for source > 64K on 16-bit machine: */
       
    41     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
       
    42 #endif
       
    43     stream.next_out = dest;
       
    44     stream.avail_out = (uInt)*destLen;
       
    45     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
       
    46 
       
    47     stream.zalloc = (alloc_func)0;
       
    48     stream.zfree = (free_func)0;
       
    49     stream.opaque = (voidpf)0;
       
    50 
       
    51     err = deflateInit_r(&stream, level);
       
    52     if (err != Z_OK) return err;
       
    53 
       
    54     err = deflate_r(&stream, Z_FINISH);
       
    55     if (err != Z_STREAM_END) {
       
    56         deflateEnd_r(&stream);
       
    57         return err == Z_OK ? Z_BUF_ERROR : err;
       
    58     }
       
    59     *destLen = stream.total_out;
       
    60 
       
    61     err = deflateEnd_r(&stream);
       
    62     return err;
       
    63 }
       
    64 
       
    65 /* ===========================================================================
       
    66  */
       
    67 
       
    68 #ifdef __SYMBIAN32__
       
    69 EXPORT_C  int compress_r (Bytef * dest,    uLongf *  destLen,   const Bytef * source,  uLong  sourceLen)
       
    70 #else
       
    71 int ZEXPORT compress (dest, destLen, source, sourceLen)
       
    72     Bytef *dest;
       
    73     uLongf *destLen;
       
    74     const Bytef *source;
       
    75     uLong sourceLen;
       
    76 #endif //__SYMBIAN32__
       
    77 {
       
    78     return compress2_r(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
       
    79 }
       
    80 
       
    81 
       
    82 /* ===========================================================================
       
    83      If the default memLevel or windowBits for deflateInit() is changed, then
       
    84    this function needs to be updated.
       
    85  */
       
    86  
       
    87 
       
    88 #ifdef __SYMBIAN32__
       
    89 EXPORT_C  uLong  compressBound_r (uLong sourceLen)
       
    90 #else  
       
    91 uLong ZEXPORT compressBound (sourceLen)
       
    92     uLong sourceLen;
       
    93 #endif //__SYMBIAN32__
       
    94 {
       
    95     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
       
    96 }
       
    97 
       
    98 
       
    99