michael@0: /* uncompr.c -- decompress a memory buffer michael@0: * Copyright (C) 1995-2003, 2010 Jean-loup Gailly. michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: /* @(#) $Id$ */ michael@0: michael@0: #define ZLIB_INTERNAL michael@0: #include "zlib.h" michael@0: michael@0: /* =========================================================================== michael@0: Decompresses the source buffer into the destination buffer. sourceLen is michael@0: the byte length of the source buffer. Upon entry, destLen is the total michael@0: size of the destination buffer, which must be large enough to hold the michael@0: entire uncompressed data. (The size of the uncompressed data must have michael@0: been saved previously by the compressor and transmitted to the decompressor michael@0: by some mechanism outside the scope of this compression library.) michael@0: Upon exit, destLen is the actual size of the compressed buffer. michael@0: michael@0: uncompress returns Z_OK if success, Z_MEM_ERROR if there was not michael@0: enough memory, Z_BUF_ERROR if there was not enough room in the output michael@0: buffer, or Z_DATA_ERROR if the input data was corrupted. michael@0: */ michael@0: int ZEXPORT uncompress (dest, destLen, source, sourceLen) michael@0: Bytef *dest; michael@0: uLongf *destLen; michael@0: const Bytef *source; michael@0: uLong sourceLen; michael@0: { michael@0: z_stream stream; michael@0: int err; michael@0: michael@0: stream.next_in = (z_const Bytef *)source; michael@0: stream.avail_in = (uInt)sourceLen; michael@0: /* Check for source > 64K on 16-bit machine: */ michael@0: if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; michael@0: michael@0: stream.next_out = dest; michael@0: stream.avail_out = (uInt)*destLen; michael@0: if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; michael@0: michael@0: stream.zalloc = (alloc_func)0; michael@0: stream.zfree = (free_func)0; michael@0: michael@0: err = inflateInit(&stream); michael@0: if (err != Z_OK) return err; michael@0: michael@0: err = inflate(&stream, Z_FINISH); michael@0: if (err != Z_STREAM_END) { michael@0: inflateEnd(&stream); michael@0: if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) michael@0: return Z_DATA_ERROR; michael@0: return err; michael@0: } michael@0: *destLen = stream.total_out; michael@0: michael@0: err = inflateEnd(&stream); michael@0: return err; michael@0: }