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