/* uncompr.c -- decompress a memory buffer1* Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler2* For conditions of distribution and use, see copyright notice in zlib.h3*/45/* @(#) $Id$ */67#define ZLIB_INTERNAL8#include "zlib.h"910/* ===========================================================================11Decompresses the source buffer into the destination buffer. *sourceLen is12the byte length of the source buffer. Upon entry, *destLen is the total size13of the destination buffer, which must be large enough to hold the entire14uncompressed data. (The size of the uncompressed data must have been saved15previously by the compressor and transmitted to the decompressor by some16mechanism outside the scope of this compression library.) Upon exit,17*destLen is the size of the decompressed data and *sourceLen is the number18of source bytes consumed. Upon return, source + *sourceLen points to the19first unused input byte.2021uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough22memory, Z_BUF_ERROR if there was not enough room in the output buffer, or23Z_DATA_ERROR if the input data was corrupted, including if the input data is24an incomplete zlib stream.25*/26int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)27Bytef *dest;28uLongf *destLen;29const Bytef *source;30uLong *sourceLen;31{32z_stream stream;33int err;34const uInt max = (uInt)-1;35uLong len, left;36Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */3738len = *sourceLen;39if (*destLen) {40left = *destLen;41*destLen = 0;42}43else {44left = 1;45dest = buf;46}4748stream.next_in = (z_const Bytef *)source;49stream.avail_in = 0;50stream.zalloc = (alloc_func)0;51stream.zfree = (free_func)0;52stream.opaque = (voidpf)0;5354err = inflateInit(&stream);55if (err != Z_OK) return err;5657stream.next_out = dest;58stream.avail_out = 0;5960do {61if (stream.avail_out == 0) {62stream.avail_out = left > (uLong)max ? max : (uInt)left;63left -= stream.avail_out;64}65if (stream.avail_in == 0) {66stream.avail_in = len > (uLong)max ? max : (uInt)len;67len -= stream.avail_in;68}69err = inflate(&stream, Z_NO_FLUSH);70} while (err == Z_OK);7172*sourceLen -= len + stream.avail_in;73if (dest != buf)74*destLen = stream.total_out;75else if (stream.total_out && err == Z_BUF_ERROR)76left = 1;7778inflateEnd(&stream);79return err == Z_STREAM_END ? Z_OK :80err == Z_NEED_DICT ? Z_DATA_ERROR :81err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :82err;83}8485int ZEXPORT uncompress (dest, destLen, source, sourceLen)86Bytef *dest;87uLongf *destLen;88const Bytef *source;89uLong sourceLen;90{91return uncompress2(dest, destLen, source, &sourceLen);92}939495