/* 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(Bytef *dest, uLongf *destLen, const Bytef *source,27uLong *sourceLen) {28z_stream stream;29int err;30const uInt max = (uInt)-1;31uLong len, left;32Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */3334len = *sourceLen;35if (*destLen) {36left = *destLen;37*destLen = 0;38}39else {40left = 1;41dest = buf;42}4344stream.next_in = (z_const Bytef *)source;45stream.avail_in = 0;46stream.zalloc = (alloc_func)0;47stream.zfree = (free_func)0;48stream.opaque = (voidpf)0;4950err = inflateInit(&stream);51if (err != Z_OK) return err;5253stream.next_out = dest;54stream.avail_out = 0;5556do {57if (stream.avail_out == 0) {58stream.avail_out = left > (uLong)max ? max : (uInt)left;59left -= stream.avail_out;60}61if (stream.avail_in == 0) {62stream.avail_in = len > (uLong)max ? max : (uInt)len;63len -= stream.avail_in;64}65err = inflate(&stream, Z_NO_FLUSH);66} while (err == Z_OK);6768*sourceLen -= len + stream.avail_in;69if (dest != buf)70*destLen = stream.total_out;71else if (stream.total_out && err == Z_BUF_ERROR)72left = 1;7374inflateEnd(&stream);75return err == Z_STREAM_END ? Z_OK :76err == Z_NEED_DICT ? Z_DATA_ERROR :77err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :78err;79}8081int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,82uLong sourceLen) {83return uncompress2(dest, destLen, source, &sourceLen);84}858687