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