1*44bedb31SLionel Sambuc /* $NetBSD: uncompr.c,v 1.2 2006/01/27 00:45:27 christos Exp $ */
2*44bedb31SLionel Sambuc
3*44bedb31SLionel Sambuc /* uncompr.c -- decompress a memory buffer
4*44bedb31SLionel Sambuc * Copyright (C) 1995-2003 Jean-loup Gailly.
5*44bedb31SLionel Sambuc * For conditions of distribution and use, see copyright notice in zlib.h
6*44bedb31SLionel Sambuc */
7*44bedb31SLionel Sambuc
8*44bedb31SLionel Sambuc /* @(#) Id */
9*44bedb31SLionel Sambuc
10*44bedb31SLionel Sambuc #define ZLIB_INTERNAL
11*44bedb31SLionel Sambuc #include "zlib.h"
12*44bedb31SLionel Sambuc
13*44bedb31SLionel Sambuc /* ===========================================================================
14*44bedb31SLionel Sambuc Decompresses the source buffer into the destination buffer. sourceLen is
15*44bedb31SLionel Sambuc the byte length of the source buffer. Upon entry, destLen is the total
16*44bedb31SLionel Sambuc size of the destination buffer, which must be large enough to hold the
17*44bedb31SLionel Sambuc entire uncompressed data. (The size of the uncompressed data must have
18*44bedb31SLionel Sambuc been saved previously by the compressor and transmitted to the decompressor
19*44bedb31SLionel Sambuc by some mechanism outside the scope of this compression library.)
20*44bedb31SLionel Sambuc Upon exit, destLen is the actual size of the compressed buffer.
21*44bedb31SLionel Sambuc This function can be used to decompress a whole file at once if the
22*44bedb31SLionel Sambuc input file is mmap'ed.
23*44bedb31SLionel Sambuc
24*44bedb31SLionel Sambuc uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
25*44bedb31SLionel Sambuc enough memory, Z_BUF_ERROR if there was not enough room in the output
26*44bedb31SLionel Sambuc buffer, or Z_DATA_ERROR if the input data was corrupted.
27*44bedb31SLionel Sambuc */
uncompress(dest,destLen,source,sourceLen)28*44bedb31SLionel Sambuc int ZEXPORT uncompress (dest, destLen, source, sourceLen)
29*44bedb31SLionel Sambuc Bytef *dest;
30*44bedb31SLionel Sambuc uLongf *destLen;
31*44bedb31SLionel Sambuc const Bytef *source;
32*44bedb31SLionel Sambuc uLong sourceLen;
33*44bedb31SLionel Sambuc {
34*44bedb31SLionel Sambuc z_stream stream;
35*44bedb31SLionel Sambuc int err;
36*44bedb31SLionel Sambuc
37*44bedb31SLionel Sambuc stream.next_in = (Bytef*)__UNCONST(source);
38*44bedb31SLionel Sambuc stream.avail_in = (uInt)sourceLen;
39*44bedb31SLionel Sambuc /* Check for source > 64K on 16-bit machine: */
40*44bedb31SLionel Sambuc if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
41*44bedb31SLionel Sambuc
42*44bedb31SLionel Sambuc stream.next_out = dest;
43*44bedb31SLionel Sambuc stream.avail_out = (uInt)*destLen;
44*44bedb31SLionel Sambuc if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
45*44bedb31SLionel Sambuc
46*44bedb31SLionel Sambuc stream.zalloc = (alloc_func)0;
47*44bedb31SLionel Sambuc stream.zfree = (free_func)0;
48*44bedb31SLionel Sambuc
49*44bedb31SLionel Sambuc err = inflateInit(&stream);
50*44bedb31SLionel Sambuc if (err != Z_OK) return err;
51*44bedb31SLionel Sambuc
52*44bedb31SLionel Sambuc err = inflate(&stream, Z_FINISH);
53*44bedb31SLionel Sambuc if (err != Z_STREAM_END) {
54*44bedb31SLionel Sambuc inflateEnd(&stream);
55*44bedb31SLionel Sambuc if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
56*44bedb31SLionel Sambuc return Z_DATA_ERROR;
57*44bedb31SLionel Sambuc return err;
58*44bedb31SLionel Sambuc }
59*44bedb31SLionel Sambuc *destLen = stream.total_out;
60*44bedb31SLionel Sambuc
61*44bedb31SLionel Sambuc err = inflateEnd(&stream);
62*44bedb31SLionel Sambuc return err;
63*44bedb31SLionel Sambuc }
64