]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Fix compress() to work on lengths more than a maximum unsigned.
authorMark Adler <madler@alumni.caltech.edu>
Sat, 26 Apr 2014 04:52:19 +0000 (21:52 -0700)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 3 Nov 2015 17:50:35 +0000 (18:50 +0100)
 Conflicts:
compress.c

compress.c

index 0bf1b0fc28cd6e7d7c36a1988cfa1e096655dcab..abae9b3ae229235720ead667a309690584575cc2 100644 (file)
@@ -1,5 +1,5 @@
 /* compress.c -- compress a memory buffer
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2005, 2014 Jean-loup Gailly, Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -23,13 +23,11 @@ int ZEXPORT compress2(unsigned char *dest, uLong *destLen, const unsigned char *
                         uLong sourceLen, int level) {
     z_stream stream;
     int err;
+    const uInt max = -1;
+    uLong left;
 
-    stream.next_in = (const unsigned char *)source;
-    stream.avail_in = (uInt)sourceLen;
-    stream.next_out = dest;
-    stream.avail_out = (uInt)*destLen;
-    if ((uLong)stream.avail_out != *destLen)
-        return Z_BUF_ERROR;
+    left = *destLen;
+    *destLen = 0;
 
     stream.zalloc = (alloc_func)0;
     stream.zfree = (free_func)0;
@@ -39,15 +37,26 @@ int ZEXPORT compress2(unsigned char *dest, uLong *destLen, const unsigned char *
     if (err != Z_OK)
         return err;
 
-    err = deflate(&stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-        deflateEnd(&stream);
-        return err == Z_OK ? Z_BUF_ERROR : err;
-    }
-    *destLen = stream.total_out;
+    stream.next_out = dest;
+    stream.avail_out = 0;
+    stream.next_in = (z_const Bytef *)source;
+    stream.avail_in = 0;
 
-    err = deflateEnd(&stream);
-    return err;
+    do {
+        if (stream.avail_out == 0) {
+            stream.avail_out = left > (uLong)max ? max : (uInt)left;
+            left -= stream.avail_out;
+        }
+        if (stream.avail_in == 0) {
+            stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
+            sourceLen -= stream.avail_in;
+        }
+        err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
+    } while (err == Z_OK);
+
+    *destLen = stream.total_out;
+    deflateEnd(&stream);
+    return err == Z_STREAM_END ? Z_OK : err;
 }
 
 /* ===========================================================================