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