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