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