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