Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:11

0001 /* uncompr.c -- decompress a memory buffer
0002  * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
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 size
0014    of the destination buffer, which must be large enough to hold the entire
0015    uncompressed data. (The size of the uncompressed data must have been saved
0016    previously by the compressor and transmitted to the decompressor by some
0017    mechanism outside the scope of this compression library.) Upon exit,
0018    *destLen is the size of the decompressed data and *sourceLen is the number
0019    of source bytes consumed. Upon return, source + *sourceLen points to the
0020    first unused input byte.
0021 
0022      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
0023    memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
0024    Z_DATA_ERROR if the input data was corrupted, including if the input data is
0025    an incomplete zlib stream.
0026 */
0027 int ZEXPORT uncompress2(dest, destLen, source, sourceLen)
0028     Bytef *dest;
0029     uLongf *destLen;
0030     const Bytef *source;
0031     uLong *sourceLen;
0032 {
0033     z_stream stream;
0034     int err;
0035     const uInt max = (uInt)-1;
0036     uLong len, left;
0037     Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
0038 
0039     len = *sourceLen;
0040     if (*destLen) {
0041         left = *destLen;
0042         *destLen = 0;
0043     }
0044     else {
0045         left = 1;
0046         dest = buf;
0047     }
0048 
0049     stream.next_in = (z_const Bytef *)source;
0050     stream.avail_in = 0;
0051     stream.zalloc = (alloc_func)0;
0052     stream.zfree = (free_func)0;
0053     stream.opaque = (voidpf)0;
0054 
0055     err = inflateInit(&stream);
0056     if (err != Z_OK) return err;
0057 
0058     stream.next_out = dest;
0059     stream.avail_out = 0;
0060 
0061     do {
0062         if (stream.avail_out == 0) {
0063             stream.avail_out = left > (uLong)max ? max : (uInt)left;
0064             left -= stream.avail_out;
0065         }
0066         if (stream.avail_in == 0) {
0067             stream.avail_in = len > (uLong)max ? max : (uInt)len;
0068             len -= stream.avail_in;
0069         }
0070         err = inflate(&stream, Z_NO_FLUSH);
0071     } while (err == Z_OK);
0072 
0073     *sourceLen -= len + stream.avail_in;
0074     if (dest != buf)
0075         *destLen = stream.total_out;
0076     else if (stream.total_out && err == Z_BUF_ERROR)
0077         left = 1;
0078 
0079     inflateEnd(&stream);
0080     return err == Z_STREAM_END ? Z_OK :
0081            err == Z_NEED_DICT ? Z_DATA_ERROR  :
0082            err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
0083            err;
0084 }
0085 
0086 int ZEXPORT uncompress(dest, destLen, source, sourceLen)
0087     Bytef *dest;
0088     uLongf *destLen;
0089     const Bytef *source;
0090     uLong sourceLen;
0091 {
0092     return uncompress2(dest, destLen, source, &sourceLen);
0093 }