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