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