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