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