/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/uncompr.c
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 | | #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 | | */ |
30 | 4.92k | int Z_EXPORT PREFIX(uncompress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t *sourceLen) { |
31 | 4.92k | PREFIX3(stream) stream; |
32 | 4.92k | int err; |
33 | 4.92k | const unsigned int max = (unsigned int)-1; |
34 | 4.92k | z_size_t len, left; |
35 | 4.92k | unsigned char buf[1]; /* for detection of incomplete stream when *destLen == 0 */ |
36 | | |
37 | 4.92k | len = *sourceLen; |
38 | 4.92k | if (*destLen) { |
39 | 4.92k | left = *destLen; |
40 | 4.92k | *destLen = 0; |
41 | 4.92k | } else { |
42 | 0 | left = 1; |
43 | 0 | dest = buf; |
44 | 0 | } |
45 | | |
46 | 4.92k | stream.next_in = (z_const unsigned char *)source; |
47 | 4.92k | stream.avail_in = 0; |
48 | 4.92k | stream.zalloc = NULL; |
49 | 4.92k | stream.zfree = NULL; |
50 | 4.92k | stream.opaque = NULL; |
51 | | |
52 | 4.92k | err = PREFIX(inflateInit)(&stream); |
53 | 4.92k | if (err != Z_OK) return err; |
54 | | |
55 | 4.92k | stream.next_out = dest; |
56 | 4.92k | stream.avail_out = 0; |
57 | | |
58 | 4.92k | do { |
59 | 4.92k | if (stream.avail_out == 0) { |
60 | 4.92k | stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left; |
61 | 4.92k | left -= stream.avail_out; |
62 | 4.92k | } |
63 | 4.92k | if (stream.avail_in == 0) { |
64 | 4.92k | stream.avail_in = len > (unsigned long)max ? max : (unsigned int)len; |
65 | 4.92k | len -= stream.avail_in; |
66 | 4.92k | } |
67 | 4.92k | err = PREFIX(inflate)(&stream, Z_NO_FLUSH); |
68 | 4.92k | } while (err == Z_OK); |
69 | | |
70 | 4.92k | *sourceLen -= len + stream.avail_in; |
71 | 4.92k | if (dest != buf) |
72 | 4.92k | *destLen = (z_size_t)stream.total_out; |
73 | 0 | else if (stream.total_out && err == Z_BUF_ERROR) |
74 | 0 | left = 1; |
75 | | |
76 | 4.92k | PREFIX(inflateEnd)(&stream); |
77 | 4.92k | return err == Z_STREAM_END ? Z_OK : |
78 | 4.92k | err == Z_NEED_DICT ? Z_DATA_ERROR : |
79 | 0 | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : |
80 | 0 | err; |
81 | 4.92k | } |
82 | | |
83 | 4.92k | int Z_EXPORT PREFIX(uncompress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) { |
84 | 4.92k | return PREFIX(uncompress2)(dest, destLen, source, &sourceLen); |
85 | 4.92k | } |