aboutsummaryrefslogtreecommitdiff
path: root/compress.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 /compress.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 'compress.c')
-rw-r--r--compress.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/compress.c b/compress.c
index 0c709d06..a27223ca 100644
--- a/compress.c
+++ b/compress.c
@@ -18,13 +18,15 @@
18 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 18 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19 memory, Z_BUF_ERROR if there was not enough room in the output buffer, 19 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20 Z_STREAM_ERROR if the level parameter is invalid. 20 Z_STREAM_ERROR if the level parameter is invalid.
21
22 The _z versions of the functions take size_t length arguments.
21*/ 23*/
22int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source, 24int ZEXPORT compress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
23 uLong sourceLen, int level) { 25 z_size_t sourceLen, int level) {
24 z_stream stream; 26 z_stream stream;
25 int err; 27 int err;
26 const uInt max = (uInt)-1; 28 const uInt max = (uInt)-1;
27 uLong left; 29 z_size_t left;
28 30
29 left = *destLen; 31 left = *destLen;
30 *destLen = 0; 32 *destLen = 0;
@@ -43,23 +45,36 @@ int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
43 45
44 do { 46 do {
45 if (stream.avail_out == 0) { 47 if (stream.avail_out == 0) {
46 stream.avail_out = left > (uLong)max ? max : (uInt)left; 48 stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
47 left -= stream.avail_out; 49 left -= stream.avail_out;
48 } 50 }
49 if (stream.avail_in == 0) { 51 if (stream.avail_in == 0) {
50 stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; 52 stream.avail_in = sourceLen > (z_size_t)max ? max :
53 (uInt)sourceLen;
51 sourceLen -= stream.avail_in; 54 sourceLen -= stream.avail_in;
52 } 55 }
53 err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); 56 err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
54 } while (err == Z_OK); 57 } while (err == Z_OK);
55 58
56 *destLen = stream.total_out; 59 *destLen = stream.next_out - dest;
57 deflateEnd(&stream); 60 deflateEnd(&stream);
58 return err == Z_STREAM_END ? Z_OK : err; 61 return err == Z_STREAM_END ? Z_OK : err;
59} 62}
60 63int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
64 uLong sourceLen, int level) {
65 int ret;
66 z_size_t got = *destLen;
67 ret = compress2_z(dest, &got, source, sourceLen, level);
68 *destLen = (uLong)got;
69 return ret;
70}
61/* =========================================================================== 71/* ===========================================================================
62 */ 72 */
73int ZEXPORT compress_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
74 z_size_t sourceLen) {
75 return compress2_z(dest, destLen, source, sourceLen,
76 Z_DEFAULT_COMPRESSION);
77}
63int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, 78int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
64 uLong sourceLen) { 79 uLong sourceLen) {
65 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 80 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);