]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Fix bug in deflateBound() for level 0 and memLevel 9.
authorMark Adler <madler@alumni.caltech.edu>
Thu, 15 Dec 2022 17:07:13 +0000 (09:07 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 3 Feb 2023 14:49:02 +0000 (15:49 +0100)
memLevel 9 would cause deflateBound() to assume the use of fixed
blocks, even if the compression level was 0, which forces stored
blocks. That could result in a bound less than the size of the
compressed data. Now level 0 always uses the stored blocks bound.

deflate.c

index 554aa7083bdb5ec10a228121763152ffd44a3017..c94e065e641ef53077b062525bca34d5d36fb168 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -662,8 +662,15 @@ unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long
 
     /* if not default parameters, return conservative bound */
     if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) ||  /* hook for IBM Z DFLTCC */
-            s->w_bits != 15 || HASH_BITS < 15)
+            s->w_bits != 15 || HASH_BITS < 15) {
+        if (s->level == 0) {
+            /* upper bound for stored blocks with length 127 (memLevel == 1) --
+               ~4% overhead plus a small constant */
+            complen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + (sourceLen >> 11) + 7;
+        }
+
         return complen + wraplen;
+    }
 
 #ifndef NO_QUICK_STRATEGY
     return sourceLen                       /* The source size itself */