ad1f37
From d09bb1ab8ef9bb91457c0ead09589e8807489260 Mon Sep 17 00:00:00 2001
ad1f37
From: Ondrej Dubaj <odubaj@redhat.com>
ad1f37
Date: Thu, 6 Aug 2020 08:09:53 +0200
ad1f37
Subject: [PATCH] Permit a deflateParams() parameter change.
ad1f37
ad1f37
This change allows a parameter change even if the input data has
ad1f37
not all been compressed and copied to the application output
ad1f37
buffer, so long as all of the input data has been compressed to
ad1f37
the internal pending output buffer. This also allows an immediate
ad1f37
deflateParams change so long as there have been no deflate calls
ad1f37
since initialization or reset.
ad1f37
---
ad1f37
 deflate.c |  6 +++---
ad1f37
 zlib.h    | 11 ++++++-----
ad1f37
 2 files changed, 9 insertions(+), 8 deletions(-)
ad1f37
ad1f37
diff --git a/deflate.c b/deflate.c
ad1f37
index 9705c1c..f3c9924 100644
ad1f37
--- a/deflate.c
ad1f37
+++ b/deflate.c
ad1f37
@@ -509,7 +509,7 @@ int ZEXPORT deflateResetKeep (strm)
ad1f37
         s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
ad1f37
 #endif
ad1f37
         adler32(0L, Z_NULL, 0);
ad1f37
-    s->last_flush = Z_NO_FLUSH;
ad1f37
+    s->last_flush = -2;
ad1f37
 
ad1f37
     _tr_init(s);
ad1f37
 
ad1f37
@@ -606,13 +606,13 @@ int ZEXPORT deflateParams(strm, level, strategy)
ad1f37
     func = configuration_table[s->level].func;
ad1f37
 
ad1f37
     if ((strategy != s->strategy || func != configuration_table[level].func ||
ad1f37
-        hook_flush != Z_NO_FLUSH) && s->high_water) {
ad1f37
+        hook_flush != Z_NO_FLUSH) && s->last_flush != -2) {
ad1f37
         /* Flush the last buffer: */
ad1f37
         int err = deflate(strm, RANK(hook_flush) > RANK(Z_BLOCK) ?
ad1f37
                           hook_flush : Z_BLOCK); 
ad1f37
         if (err == Z_STREAM_ERROR)
ad1f37
             return err;
ad1f37
-        if (strm->avail_out == 0)
ad1f37
+        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
ad1f37
             return Z_BUF_ERROR;
ad1f37
     }
ad1f37
     if (s->level != level) {
ad1f37
diff --git a/zlib.h b/zlib.h
ad1f37
index f09cdaf..001624e 100644
ad1f37
--- a/zlib.h
ad1f37
+++ b/zlib.h
ad1f37
@@ -712,11 +712,12 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
ad1f37
    used to switch between compression and straight copy of the input data, or
ad1f37
    to switch to a different kind of input data requiring a different strategy.
ad1f37
    If the compression approach (which is a function of the level) or the
ad1f37
-   strategy is changed, and if any input has been consumed in a previous
ad1f37
-   deflate() call, then the input available so far is compressed with the old
ad1f37
-   level and strategy using deflate(strm, Z_BLOCK).  There are three approaches
ad1f37
-   for the compression levels 0, 1..3, and 4..9 respectively.  The new level
ad1f37
-   and strategy will take effect at the next call of deflate().
ad1f37
+   strategy is changed, and if there have been any deflate() calls since the
ad1f37
+   state was initialized or reset, then the input available so far is
ad1f37
+   compressed with the old level and strategy using deflate(strm, Z_BLOCK).
ad1f37
+   There are three approaches for the compression levels 0, 1..3, and 4..9
ad1f37
+   respectively.  The new level and strategy will take effect at the next call
ad1f37
+   of deflate().
ad1f37
 
ad1f37
      If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does
ad1f37
    not have enough output space to complete, then the parameter change will not
ad1f37
-- 
ad1f37
2.26.0
ad1f37