xref: /freebsd/sys/contrib/zlib/uncompr.c (revision c9083b850aed19fff781fd82084e8308c9ae6df6)
1*c9083b85SXin LI /* uncompr.c -- decompress a memory buffer
2*c9083b85SXin LI  * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
3*c9083b85SXin LI  * For conditions of distribution and use, see copyright notice in zlib.h
4*c9083b85SXin LI  */
5*c9083b85SXin LI 
6*c9083b85SXin LI /* @(#) $Id$ */
7*c9083b85SXin LI 
8*c9083b85SXin LI #define ZLIB_INTERNAL
9*c9083b85SXin LI #include "zlib.h"
10*c9083b85SXin LI 
11*c9083b85SXin LI /* ===========================================================================
12*c9083b85SXin LI      Decompresses the source buffer into the destination buffer.  *sourceLen is
13*c9083b85SXin LI    the byte length of the source buffer. Upon entry, *destLen is the total size
14*c9083b85SXin LI    of the destination buffer, which must be large enough to hold the entire
15*c9083b85SXin LI    uncompressed data. (The size of the uncompressed data must have been saved
16*c9083b85SXin LI    previously by the compressor and transmitted to the decompressor by some
17*c9083b85SXin LI    mechanism outside the scope of this compression library.) Upon exit,
18*c9083b85SXin LI    *destLen is the size of the decompressed data and *sourceLen is the number
19*c9083b85SXin LI    of source bytes consumed. Upon return, source + *sourceLen points to the
20*c9083b85SXin LI    first unused input byte.
21*c9083b85SXin LI 
22*c9083b85SXin LI      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
23*c9083b85SXin LI    memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
24*c9083b85SXin LI    Z_DATA_ERROR if the input data was corrupted, including if the input data is
25*c9083b85SXin LI    an incomplete zlib stream.
26*c9083b85SXin LI */
27*c9083b85SXin LI int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
28*c9083b85SXin LI     Bytef *dest;
29*c9083b85SXin LI     uLongf *destLen;
30*c9083b85SXin LI     const Bytef *source;
31*c9083b85SXin LI     uLong *sourceLen;
32*c9083b85SXin LI {
33*c9083b85SXin LI     z_stream stream;
34*c9083b85SXin LI     int err;
35*c9083b85SXin LI     const uInt max = (uInt)-1;
36*c9083b85SXin LI     uLong len, left;
37*c9083b85SXin LI     Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
38*c9083b85SXin LI 
39*c9083b85SXin LI     len = *sourceLen;
40*c9083b85SXin LI     if (*destLen) {
41*c9083b85SXin LI         left = *destLen;
42*c9083b85SXin LI         *destLen = 0;
43*c9083b85SXin LI     }
44*c9083b85SXin LI     else {
45*c9083b85SXin LI         left = 1;
46*c9083b85SXin LI         dest = buf;
47*c9083b85SXin LI     }
48*c9083b85SXin LI 
49*c9083b85SXin LI     stream.next_in = (z_const Bytef *)source;
50*c9083b85SXin LI     stream.avail_in = 0;
51*c9083b85SXin LI     stream.zalloc = (alloc_func)0;
52*c9083b85SXin LI     stream.zfree = (free_func)0;
53*c9083b85SXin LI     stream.opaque = (voidpf)0;
54*c9083b85SXin LI 
55*c9083b85SXin LI     err = inflateInit(&stream);
56*c9083b85SXin LI     if (err != Z_OK) return err;
57*c9083b85SXin LI 
58*c9083b85SXin LI     stream.next_out = dest;
59*c9083b85SXin LI     stream.avail_out = 0;
60*c9083b85SXin LI 
61*c9083b85SXin LI     do {
62*c9083b85SXin LI         if (stream.avail_out == 0) {
63*c9083b85SXin LI             stream.avail_out = left > (uLong)max ? max : (uInt)left;
64*c9083b85SXin LI             left -= stream.avail_out;
65*c9083b85SXin LI         }
66*c9083b85SXin LI         if (stream.avail_in == 0) {
67*c9083b85SXin LI             stream.avail_in = len > (uLong)max ? max : (uInt)len;
68*c9083b85SXin LI             len -= stream.avail_in;
69*c9083b85SXin LI         }
70*c9083b85SXin LI         err = inflate(&stream, Z_NO_FLUSH);
71*c9083b85SXin LI     } while (err == Z_OK);
72*c9083b85SXin LI 
73*c9083b85SXin LI     *sourceLen -= len + stream.avail_in;
74*c9083b85SXin LI     if (dest != buf)
75*c9083b85SXin LI         *destLen = stream.total_out;
76*c9083b85SXin LI     else if (stream.total_out && err == Z_BUF_ERROR)
77*c9083b85SXin LI         left = 1;
78*c9083b85SXin LI 
79*c9083b85SXin LI     inflateEnd(&stream);
80*c9083b85SXin LI     return err == Z_STREAM_END ? Z_OK :
81*c9083b85SXin LI            err == Z_NEED_DICT ? Z_DATA_ERROR  :
82*c9083b85SXin LI            err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
83*c9083b85SXin LI            err;
84*c9083b85SXin LI }
85*c9083b85SXin LI 
86*c9083b85SXin LI int ZEXPORT uncompress (dest, destLen, source, sourceLen)
87*c9083b85SXin LI     Bytef *dest;
88*c9083b85SXin LI     uLongf *destLen;
89*c9083b85SXin LI     const Bytef *source;
90*c9083b85SXin LI     uLong sourceLen;
91*c9083b85SXin LI {
92*c9083b85SXin LI     return uncompress2(dest, destLen, source, &sourceLen);
93*c9083b85SXin LI }
94