Line | Count | Source (jump to first uncovered line) |
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 | | */ |
25 | 21.6k | int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t *sourceLen) { |
26 | 21.6k | PREFIX3(stream) stream; |
27 | 21.6k | int err; |
28 | 21.6k | const unsigned int max = (unsigned int)-1; |
29 | 21.6k | z_uintmax_t len, left; |
30 | 21.6k | unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */ |
31 | | |
32 | 21.6k | len = *sourceLen; |
33 | 21.6k | if (*destLen) { |
34 | 21.6k | left = *destLen; |
35 | 21.6k | *destLen = 0; |
36 | 21.6k | } else { |
37 | 0 | left = 1; |
38 | 0 | dest = buf; |
39 | 0 | } |
40 | | |
41 | 21.6k | stream.next_in = (z_const unsigned char *)source; |
42 | 21.6k | stream.avail_in = 0; |
43 | 21.6k | stream.zalloc = NULL; |
44 | 21.6k | stream.zfree = NULL; |
45 | 21.6k | stream.opaque = NULL; |
46 | | |
47 | 21.6k | err = PREFIX(inflateInit)(&stream); |
48 | 21.6k | if (err != Z_OK) return err; |
49 | | |
50 | 21.6k | stream.next_out = dest; |
51 | 21.6k | stream.avail_out = 0; |
52 | | |
53 | 22.7k | do { |
54 | 22.7k | if (stream.avail_out == 0) { |
55 | 21.8k | stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left; |
56 | 21.8k | left -= stream.avail_out; |
57 | 21.8k | } |
58 | 22.7k | if (stream.avail_in == 0) { |
59 | 22.5k | stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len; |
60 | 22.5k | len -= stream.avail_in; |
61 | 22.5k | } |
62 | 22.7k | err = PREFIX(inflate)(&stream, Z_NO_FLUSH); |
63 | 22.7k | } while (err == Z_OK); |
64 | | |
65 | 21.6k | *sourceLen -= len + stream.avail_in; |
66 | 21.6k | if (dest != buf) |
67 | 21.6k | *destLen = (z_size_t)stream.total_out; |
68 | 0 | else if (stream.total_out && err == Z_BUF_ERROR) |
69 | 0 | left = 1; |
70 | | |
71 | 21.6k | PREFIX(inflateEnd)(&stream); |
72 | 21.6k | return err == Z_STREAM_END ? Z_OK : |
73 | 21.6k | err == Z_NEED_DICT ? Z_DATA_ERROR : |
74 | 4.32k | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : |
75 | 4.32k | err; |
76 | 21.6k | } |
77 | | |
78 | 21.6k | int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) { |
79 | 21.6k | return PREFIX(uncompress2)(dest, destLen, source, &sourceLen); |
80 | 21.6k | } |