File indexing completed on 2024-04-21 11:22:12

0001 /* uncompr.c -- decompress a memory buffer
0002  * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
0003  * For conditions of distribution and use, see copyright notice in zlib.h
0004  */
0005 
0006 /* @(#) $Id$ */
0007 
0008 #define ZLIB_INTERNAL
0009 #include "zlib.h"
0010 
0011 /* ===========================================================================
0012      Decompresses the source buffer into the destination buffer.  sourceLen is
0013    the byte length of the source buffer. Upon entry, destLen is the total
0014    size of the destination buffer, which must be large enough to hold the
0015    entire uncompressed data. (The size of the uncompressed data must have
0016    been saved previously by the compressor and transmitted to the decompressor
0017    by some mechanism outside the scope of this compression library.)
0018    Upon exit, destLen is the actual size of the compressed buffer.
0019 
0020      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
0021    enough memory, Z_BUF_ERROR if there was not enough room in the output
0022    buffer, or Z_DATA_ERROR if the input data was corrupted.
0023 */
0024 int ZEXPORT uncompress (dest, destLen, source, sourceLen)
0025     Bytef *dest;
0026     uLongf *destLen;
0027     const Bytef *source;
0028     uLong sourceLen;
0029 {
0030     z_stream stream;
0031     int err;
0032 
0033     stream.next_in = (z_const Bytef *)source;
0034     stream.avail_in = (uInt)sourceLen;
0035     /* Check for source > 64K on 16-bit machine: */
0036     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
0037 
0038     stream.next_out = dest;
0039     stream.avail_out = (uInt)*destLen;
0040     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
0041 
0042     stream.zalloc = (alloc_func)0;
0043     stream.zfree = (free_func)0;
0044 
0045     err = inflateInit(&stream);
0046     if (err != Z_OK) return err;
0047 
0048     err = inflate(&stream, Z_FINISH);
0049     if (err != Z_STREAM_END) {
0050         inflateEnd(&stream);
0051         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
0052             return Z_DATA_ERROR;
0053         return err;
0054     }
0055     *destLen = stream.total_out;
0056 
0057     err = inflateEnd(&stream);
0058     return err;
0059 }