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