aboutsummaryrefslogtreecommitdiff
path: root/uncompr.c
diff options
context:
space:
mode:
authorMark Adler <git@madler.net>2026-01-12 09:29:40 -0800
committerMark Adler <git@madler.net>2026-01-12 10:13:28 -0800
commit4edb00de5aac7e4aa9110374bd1991c4d070eddb (patch)
tree5a28fee50f6af03c57e0389a85be69b31727b340 /uncompr.c
parent1a40058a92d525aa49a6eac698cfde500fc9b92f (diff)
downloadzlib-4edb00de5aac7e4aa9110374bd1991c4d070eddb.tar.gz
zlib-4edb00de5aac7e4aa9110374bd1991c4d070eddb.tar.bz2
zlib-4edb00de5aac7e4aa9110374bd1991c4d070eddb.zip
Add _z versions of the compress and uncompress functions.
Provide size_t arguments for Windows, on which a long is 32 bits.
Diffstat (limited to 'uncompr.c')
-rw-r--r--uncompr.c56
1 files changed, 34 insertions, 22 deletions
diff --git a/uncompr.c b/uncompr.c
index 5e256663..8f7438ee 100644
--- a/uncompr.c
+++ b/uncompr.c
@@ -23,24 +23,20 @@
23 memory, Z_BUF_ERROR if there was not enough room in the output buffer, or 23 memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
24 Z_DATA_ERROR if the input data was corrupted, including if the input data is 24 Z_DATA_ERROR if the input data was corrupted, including if the input data is
25 an incomplete zlib stream. 25 an incomplete zlib stream.
26
27 The _z versions of the functions take size_t length arguments.
26*/ 28*/
27int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source, 29int ZEXPORT uncompress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
28 uLong *sourceLen) { 30 z_size_t *sourceLen) {
29 z_stream stream; 31 z_stream stream;
30 int err; 32 int err;
31 const uInt max = (uInt)-1; 33 const uInt max = (uInt)-1;
32 uLong len, left; 34 z_size_t len, left;
33 Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
34 35
35 len = *sourceLen; 36 len = *sourceLen;
36 if (*destLen) { 37 left = *destLen;
37 left = *destLen; 38 if (left == 0 && dest == Z_NULL)
38 *destLen = 0; 39 dest = (Bytef *)&stream.reserved; /* next_out cannot be NULL */
39 }
40 else {
41 left = 1;
42 dest = buf;
43 }
44 40
45 stream.next_in = (z_const Bytef *)source; 41 stream.next_in = (z_const Bytef *)source;
46 stream.avail_in = 0; 42 stream.avail_in = 0;
@@ -56,30 +52,46 @@ int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
56 52
57 do { 53 do {
58 if (stream.avail_out == 0) { 54 if (stream.avail_out == 0) {
59 stream.avail_out = left > (uLong)max ? max : (uInt)left; 55 stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
60 left -= stream.avail_out; 56 left -= stream.avail_out;
61 } 57 }
62 if (stream.avail_in == 0) { 58 if (stream.avail_in == 0) {
63 stream.avail_in = len > (uLong)max ? max : (uInt)len; 59 stream.avail_in = len > (z_size_t)max ? max : (uInt)len;
64 len -= stream.avail_in; 60 len -= stream.avail_in;
65 } 61 }
66 err = inflate(&stream, Z_NO_FLUSH); 62 err = inflate(&stream, Z_NO_FLUSH);
67 } while (err == Z_OK); 63 } while (err == Z_OK);
68 64
69 *sourceLen -= len + stream.avail_in; 65 /* Set len and left to the unused input data and unused output space. Set
70 if (dest != buf) 66 *sourceLen to the amount of input consumed. Set *destLen to the amount
71 *destLen = stream.total_out; 67 of data produced. */
72 else if (stream.total_out && err == Z_BUF_ERROR) 68 len += stream.avail_in;
73 left = 1; 69 left += stream.avail_out;
70 *sourceLen -= len;
71 *destLen -= left;
74 72
75 inflateEnd(&stream); 73 inflateEnd(&stream);
76 return err == Z_STREAM_END ? Z_OK : 74 return err == Z_STREAM_END ? Z_OK :
77 err == Z_NEED_DICT ? Z_DATA_ERROR : 75 err == Z_NEED_DICT ? Z_DATA_ERROR :
78 err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : 76 err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR :
79 err; 77 err;
80} 78}
81 79int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
80 uLong *sourceLen) {
81 int ret;
82 z_size_t got = *destLen, used = *sourceLen;
83 ret = uncompress2_z(dest, &got, source, &used);
84 *sourceLen = (uLong)used;
85 *destLen = (uLong)got;
86 return ret;
87}
88int ZEXPORT uncompress_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
89 z_size_t sourceLen) {
90 z_size_t used = sourceLen;
91 return uncompress2_z(dest, destLen, source, &used);
92}
82int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, 93int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
83 uLong sourceLen) { 94 uLong sourceLen) {
84 return uncompress2(dest, destLen, source, &sourceLen); 95 uLong used = sourceLen;
96 return uncompress2(dest, destLen, source, &used);
85} 97}