zlib123/compress.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 11 Apr 2006 14:33:48 +0000
changeset 774 916c2e81e77f
parent 718 2e809894a316
permissions -rw-r--r--
7zip support, written by Dennis Schridde, and heavily Ryanified by me.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
718
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/* compress.c -- compress a memory buffer
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Copyright (C) 1995-2003 Jean-loup Gailly.
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 */
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
/* @(#) $Id$ */
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#define ZLIB_INTERNAL
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include "zlib.h"
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
/* ===========================================================================
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
     Compresses the source buffer into the destination buffer. The level
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
   parameter has the same meaning as in deflateInit.  sourceLen is the byte
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
   length of the source buffer. Upon entry, destLen is the total size of the
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
   destination buffer, which must be at least 0.1% larger than sourceLen plus
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
   Z_STREAM_ERROR if the level parameter is invalid.
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
*/
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    Bytef *dest;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    uLongf *destLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    const Bytef *source;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    uLong sourceLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    int level;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
{
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    z_stream stream;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    int err;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    stream.next_in = (Bytef*)source;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    stream.avail_in = (uInt)sourceLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#ifdef MAXSEG_64K
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    /* Check for source > 64K on 16-bit machine: */
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#endif
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    stream.next_out = dest;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    stream.avail_out = (uInt)*destLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    stream.zalloc = (alloc_func)0;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    stream.zfree = (free_func)0;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    stream.opaque = (voidpf)0;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    err = deflateInit(&stream, level);
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    if (err != Z_OK) return err;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    err = deflate(&stream, Z_FINISH);
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    if (err != Z_STREAM_END) {
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        deflateEnd(&stream);
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        return err == Z_OK ? Z_BUF_ERROR : err;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    }
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    *destLen = stream.total_out;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    err = deflateEnd(&stream);
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    return err;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
}
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
/* ===========================================================================
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
 */
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
int ZEXPORT compress (dest, destLen, source, sourceLen)
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    Bytef *dest;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    uLongf *destLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    const Bytef *source;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    uLong sourceLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
{
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
}
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
/* ===========================================================================
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
     If the default memLevel or windowBits for deflateInit() is changed, then
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
   this function needs to be updated.
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
 */
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
uLong ZEXPORT compressBound (sourceLen)
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    uLong sourceLen;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
{
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
2e809894a316 Moved to actual zlib 1.2.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
}