zlib121/uncompr.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 19 Dec 2003 01:49:12 +0000
changeset 612 03b07cbd1bc7
parent 602 691c1eadb8b7
permissions -rw-r--r--
Added zlib_license_change.txt to RPM spec (thanks, Edward!)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/* uncompr.c -- decompress a memory buffer
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Copyright (C) 1995-2003 Jean-loup Gailly.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
/* @(#) $Id: uncompr.c,v 1.1 2003/12/07 05:29:20 icculus Exp $ */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#define ZLIB_INTERNAL
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include "zlib.h"
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
/* ===========================================================================
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
     Decompresses the source buffer into the destination buffer.  sourceLen is
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
   the byte length of the source buffer. Upon entry, destLen is the total
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
   size of the destination buffer, which must be large enough to hold the
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
   entire uncompressed data. (The size of the uncompressed data must have
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
   been saved previously by the compressor and transmitted to the decompressor
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
   by some mechanism outside the scope of this compression library.)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
   Upon exit, destLen is the actual size of the compressed buffer.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
     This function can be used to decompress a whole file at once if the
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
   input file is mmap'ed.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
   enough memory, Z_BUF_ERROR if there was not enough room in the output
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
   buffer, or Z_DATA_ERROR if the input data was corrupted.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
*/
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
int ZEXPORT uncompress (dest, destLen, source, sourceLen)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    Bytef *dest;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
    uLongf *destLen;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    const Bytef *source;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    uLong sourceLen;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    z_stream stream;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    int err;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    stream.next_in = (Bytef*)source;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    stream.avail_in = (uInt)sourceLen;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    /* Check for source > 64K on 16-bit machine: */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    stream.next_out = dest;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    stream.avail_out = (uInt)*destLen;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    stream.zalloc = (alloc_func)0;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    stream.zfree = (free_func)0;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    err = inflateInit(&stream);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    if (err != Z_OK) return err;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    err = inflate(&stream, Z_FINISH);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    if (err != Z_STREAM_END) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        inflateEnd(&stream);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
            return Z_DATA_ERROR;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        return err;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    *destLen = stream.total_out;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    err = inflateEnd(&stream);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    return err;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
}