ad1f37
From 608b71008c16ce6fbf2305145c5ffb69cd88ef59 Mon Sep 17 00:00:00 2001
ad1f37
From: Ondrej Dubaj <odubaj@redhat.com>
ad1f37
Date: Fri, 7 Aug 2020 07:12:50 +0200
ad1f37
Subject: [PATCH] Fix for Z hardware-accelerated deflate for s390x
ad1f37
ad1f37
---
ad1f37
 configure                     |   7 +
ad1f37
 contrib/s390/dfltcc.c         | 244 +++++++++++++++++++++-------------
ad1f37
 contrib/s390/dfltcc_deflate.h |  10 +-
ad1f37
 deflate.c                     |  21 +--
ad1f37
 4 files changed, 177 insertions(+), 105 deletions(-)
ad1f37
ad1f37
diff --git a/configure b/configure
ad1f37
index 66caece..bfe4386 100755
ad1f37
--- a/configure
ad1f37
+++ b/configure
ad1f37
@@ -114,6 +114,7 @@ case "$1" in
ad1f37
       echo '  configure [--const] [--zprefix] [--prefix=PREFIX]  [--eprefix=EXPREFIX]' | tee -a configure.log
ad1f37
       echo '    [--static] [--64] [--libdir=LIBDIR] [--sharedlibdir=LIBDIR]' | tee -a configure.log
ad1f37
       echo '    [--includedir=INCLUDEDIR] [--archs="-arch i386 -arch x86_64"]' | tee -a configure.log
ad1f37
+      echo '    [--dfltcc]' | tee -a configure.log
ad1f37
         exit 0 ;;
ad1f37
     -p*=* | --prefix=*) prefix=`echo $1 | sed 's/.*=//'`; shift ;;
ad1f37
     -e*=* | --eprefix=*) exec_prefix=`echo $1 | sed 's/.*=//'`; shift ;;
ad1f37
@@ -137,6 +138,12 @@ case "$1" in
ad1f37
     -c* | --const) zconst=1; shift ;;
ad1f37
     -w* | --warn) warn=1; shift ;;
ad1f37
     -d* | --debug) debug=1; shift ;;
ad1f37
+    --dfltcc)
ad1f37
+	    CFLAGS="$CFLAGS -DDFLTCC"
ad1f37
+      OBJC="$OBJC dfltcc.o"
ad1f37
+	    PIC_OBJC="$PIC_OBJC dfltcc.lo"
ad1f37
+      shift
ad1f37
+      ;; 
ad1f37
     *)
ad1f37
       echo "unknown option: $1" | tee -a configure.log
ad1f37
       echo "$0 --help for help" | tee -a configure.log
ad1f37
diff --git a/contrib/s390/dfltcc.c b/contrib/s390/dfltcc.c
ad1f37
index d187796..d88a0d6 100644
ad1f37
--- a/contrib/s390/dfltcc.c
ad1f37
+++ b/contrib/s390/dfltcc.c
ad1f37
@@ -2,12 +2,13 @@
ad1f37
 
ad1f37
 /*
ad1f37
    Use the following commands to build zlib with DFLTCC support:
ad1f37
-        $ CFLAGS=-DDFLTCC ./configure
ad1f37
-        $ make OBJA=dfltcc.o PIC_OBJA=dfltcc.lo
ad1f37
+        $ ./configure --dfltcc
ad1f37
+        $ make 
ad1f37
 */
ad1f37
 
ad1f37
 #define _GNU_SOURCE
ad1f37
 #include <ctype.h>
ad1f37
+#include <errno.h>
ad1f37
 #include <inttypes.h>
ad1f37
 #include <stddef.h>
ad1f37
 #include <stdio.h>
ad1f37
@@ -230,31 +231,28 @@ struct dfltcc_state {
ad1f37
 /*
ad1f37
    Compress.
ad1f37
  */
ad1f37
-local inline int dfltcc_are_params_ok(int level,
ad1f37
-                                      uInt window_bits,
ad1f37
-                                      int strategy,
ad1f37
-                                      uLong level_mask);
ad1f37
-local inline int dfltcc_are_params_ok(level, window_bits, strategy, level_mask)
ad1f37
+local inline int dfltcc_can_deflate_with_params(z_streamp strm,
ad1f37
+                                                 int level,
ad1f37
+                                                 uInt window_bits,
ad1f37
+                                                 int strategy);
ad1f37
+local inline int dfltcc_can_deflate_with_params(strm,
ad1f37
+                                                 level,
ad1f37
+                                                 window_bits,
ad1f37
+                                                 strategy)
ad1f37
+    z_streamp strm; 
ad1f37
     int level;
ad1f37
     uInt window_bits;
ad1f37
     int strategy;
ad1f37
-    uLong level_mask;
ad1f37
-{
ad1f37
-    return (level_mask & (1 << level)) != 0 &&
ad1f37
-        (window_bits == HB_BITS) &&
ad1f37
-        (strategy == Z_FIXED || strategy == Z_DEFAULT_STRATEGY);
ad1f37
-}
ad1f37
-
ad1f37
-
ad1f37
-int ZLIB_INTERNAL dfltcc_can_deflate(strm)
ad1f37
-    z_streamp strm;
ad1f37
 {
ad1f37
     deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
     struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
 
ad1f37
     /* Unsupported compression settings */
ad1f37
-    if (!dfltcc_are_params_ok(state->level, state->w_bits, state->strategy,
ad1f37
-                              dfltcc_state->level_mask))
ad1f37
+    if ((dfltcc_state->level_mask & (1 << level)) == 0)
ad1f37
+        return 0;
ad1f37
+    if (window_bits != HB_BITS)
ad1f37
+        return 0;
ad1f37
+    if (strategy != Z_FIXED && strategy != Z_DEFAULT_STRATEGY) 
ad1f37
         return 0;
ad1f37
 
ad1f37
     /* Unsupported hardware */
ad1f37
@@ -266,6 +264,17 @@ int ZLIB_INTERNAL dfltcc_can_deflate(strm)
ad1f37
     return 1;
ad1f37
 }
ad1f37
 
ad1f37
+int ZLIB_INTERNAL dfltcc_can_deflate(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+
ad1f37
+    return dfltcc_can_deflate_with_params(strm,
ad1f37
+                                          state->level,
ad1f37
+                                          state->w_bits,
ad1f37
+                                          state->strategy);
ad1f37
+} 
ad1f37
+
ad1f37
 local void dfltcc_gdht OF((z_streamp strm));
ad1f37
 local void dfltcc_gdht(strm)
ad1f37
     z_streamp strm;
ad1f37
@@ -349,22 +358,24 @@ again:
ad1f37
     soft_bcc = 0;
ad1f37
     no_flush = flush == Z_NO_FLUSH;
ad1f37
 
ad1f37
-    /* Trailing empty block. Switch to software, except when Continuation Flag
ad1f37
-     * is set, which means that DFLTCC has buffered some output in the
ad1f37
-     * parameter block and needs to be called again in order to flush it.
ad1f37
+    /* No input data. Return, except when Continuation Flag is set, which means
ad1f37
+     * that DFLTCC has buffered some output in the parameter block and needs to
ad1f37
+     * be called again in order to flush it. 
ad1f37
      */
ad1f37
-    if (flush == Z_FINISH && strm->avail_in == 0 && !param->cf) {
ad1f37
-        if (param->bcf) {
ad1f37
-            /* A block is still open, and the hardware does not support closing
ad1f37
-             * blocks without adding data. Thus, close it manually.
ad1f37
-             */
ad1f37
+    if (strm->avail_in == 0 && !param->cf) {
ad1f37
+        /* A block is still open, and the hardware does not support closing
ad1f37
+         * blocks without adding data. Thus, close it manually.
ad1f37
+         */
ad1f37
+        if (!no_flush && param->bcf) { 
ad1f37
             send_eobs(strm, param);
ad1f37
             param->bcf = 0;
ad1f37
         }
ad1f37
-        return 0;
ad1f37
-    }
ad1f37
-
ad1f37
-    if (strm->avail_in == 0 && !param->cf) {
ad1f37
+        /* Let one of deflate_* functions write a trailing empty block. */
ad1f37
+        if (flush == Z_FINISH)
ad1f37
+            return 0;
ad1f37
+        /* Clear history. */
ad1f37
+        if (flush == Z_FULL_FLUSH)
ad1f37
+            param->hl = 0; 
ad1f37
         *result = need_more;
ad1f37
         return 1;
ad1f37
     }
ad1f37
@@ -418,7 +429,7 @@ again:
ad1f37
     param->cvt = state->wrap == 2 ? CVT_CRC32 : CVT_ADLER32;
ad1f37
     if (!no_flush)
ad1f37
         /* We need to close a block. Always do this in software - when there is
ad1f37
-         * no input data, the hardware will not nohor BCC. */
ad1f37
+         * no input data, the hardware will not honor BCC. */
ad1f37
         soft_bcc = 1;
ad1f37
     if (flush == Z_FINISH && !param->bcf)
ad1f37
         /* We are about to open a BFINAL block, set Block Header Final bit
ad1f37
@@ -433,8 +444,8 @@ again:
ad1f37
     param->sbb = (unsigned int)state->bi_valid;
ad1f37
     if (param->sbb > 0)
ad1f37
         *strm->next_out = (Bytef)state->bi_buf;
ad1f37
-    if (param->hl)
ad1f37
-        param->nt = 0; /* Honor history */
ad1f37
+    /* Honor history and check value */
ad1f37
+    param->nt = 0; 
ad1f37
     param->cv = state->wrap == 2 ? ZSWAP32(strm->adler) : strm->adler;
ad1f37
 
ad1f37
     /* When opening a block, choose a Huffman-Table Type */
ad1f37
@@ -642,27 +653,86 @@ int ZLIB_INTERNAL dfltcc_inflate_disable(strm)
ad1f37
     return 0;
ad1f37
 }
ad1f37
 
ad1f37
-/*
ad1f37
-   Memory management.
ad1f37
-   DFLTCC requires parameter blocks and window to be aligned. zlib allows
ad1f37
-   users to specify their own allocation functions, so using e.g.
ad1f37
-   `posix_memalign' is not an option. Thus, we overallocate and take the
ad1f37
-   aligned portion of the buffer.
ad1f37
-*/
ad1f37
+local int env_dfltcc_disabled;
ad1f37
+local int env_source_date_epoch;
ad1f37
+local unsigned long env_level_mask;
ad1f37
+local unsigned long env_block_size;
ad1f37
+local unsigned long env_block_threshold;
ad1f37
+local unsigned long env_dht_threshold;
ad1f37
+local unsigned long env_ribm;
ad1f37
+local uint64_t cpu_facilities[(DFLTCC_FACILITY / 64) + 1];
ad1f37
+local struct dfltcc_qaf_param cpu_af __attribute__((aligned(8))); 
ad1f37
+
ad1f37
 local inline int is_dfltcc_enabled OF((void));
ad1f37
 local inline int is_dfltcc_enabled(void)
ad1f37
+{
ad1f37
+    if (env_dfltcc_disabled)
ad1f37
+      /* User has explicitly disabled DFLTCC. */
ad1f37
+      return 0;
ad1f37
+
ad1f37
+    return is_bit_set((const char *)cpu_facilities, DFLTCC_FACILITY);
ad1f37
+}
ad1f37
+
ad1f37
+local unsigned long xstrtoul OF((const char *s, unsigned long _default));
ad1f37
+local unsigned long xstrtoul(s, _default)
ad1f37
+    const char *s;
ad1f37
+    unsigned long _default;
ad1f37
+{
ad1f37
+    char *endptr;
ad1f37
+    unsigned long result;
ad1f37
+
ad1f37
+    if (!(s && *s))
ad1f37
+        return _default;
ad1f37
+    errno = 0;
ad1f37
+    result = strtoul(s, &endptr, 0);
ad1f37
+    return (errno || *endptr) ? _default : result;
ad1f37
+}
ad1f37
+
ad1f37
+__attribute__((constructor)) local void init_globals OF((void));
ad1f37
+__attribute__((constructor)) local void init_globals(void)
ad1f37
 {
ad1f37
     const char *env;
ad1f37
-    uint64_t facilities[(DFLTCC_FACILITY / 64) + 1];
ad1f37
     register char r0 __asm__("r0");
ad1f37
 
ad1f37
     env = secure_getenv("DFLTCC");
ad1f37
-    if (env && !strcmp(env, "0"))
ad1f37
-      /* User has explicitly disabled DFLTCC. */
ad1f37
-      return 0;
ad1f37
+    
ad1f37
+
ad1f37
+    env_dfltcc_disabled = env && !strcmp(env, "0");
ad1f37
+
ad1f37
+    env = secure_getenv("SOURCE_DATE_EPOCH");
ad1f37
+    env_source_date_epoch = !!env;
ad1f37
+
ad1f37
+#ifndef DFLTCC_LEVEL_MASK
ad1f37
+#define DFLTCC_LEVEL_MASK 0x2
ad1f37
+#endif
ad1f37
+    env_level_mask = xstrtoul(secure_getenv("DFLTCC_LEVEL_MASK"),
ad1f37
+                              DFLTCC_LEVEL_MASK);
ad1f37
+
ad1f37
+#ifndef DFLTCC_BLOCK_SIZE
ad1f37
+#define DFLTCC_BLOCK_SIZE 1048576
ad1f37
+#endif
ad1f37
+    env_block_size = xstrtoul(secure_getenv("DFLTCC_BLOCK_SIZE"),
ad1f37
+                              DFLTCC_BLOCK_SIZE);
ad1f37
 
ad1f37
-    memset(facilities, 0, sizeof(facilities));
ad1f37
-    r0 = sizeof(facilities) / sizeof(facilities[0]) - 1;
ad1f37
+#ifndef DFLTCC_FIRST_FHT_BLOCK_SIZE
ad1f37
+#define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
ad1f37
+#endif
ad1f37
+    env_block_threshold = xstrtoul(secure_getenv("DFLTCC_FIRST_FHT_BLOCK_SIZE"),
ad1f37
+                                   DFLTCC_FIRST_FHT_BLOCK_SIZE);
ad1f37
+
ad1f37
+#ifndef DFLTCC_DHT_MIN_SAMPLE_SIZE
ad1f37
+#define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
ad1f37
+#endif
ad1f37
+    env_dht_threshold = xstrtoul(secure_getenv("DFLTCC_DHT_MIN_SAMPLE_SIZE"),
ad1f37
+                                  DFLTCC_DHT_MIN_SAMPLE_SIZE);
ad1f37
+
ad1f37
+#ifndef DFLTCC_RIBM
ad1f37
+#define DFLTCC_RIBM 0
ad1f37
+#endif
ad1f37
+    env_ribm = xstrtoul(secure_getenv("DFLTCC_RIBM"), DFLTCC_RIBM);
ad1f37
+
ad1f37
+    memset(cpu_facilities, 0, sizeof(cpu_facilities));
ad1f37
+    r0 = sizeof(cpu_facilities) / sizeof(cpu_facilities[0]) - 1;
ad1f37
     /* STFLE is supported since z9-109 and only in z/Architecture mode. When
ad1f37
      * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
ad1f37
      * is 64-bit, it's always z/Architecture mode at runtime.
ad1f37
@@ -671,31 +741,35 @@ local inline int is_dfltcc_enabled(void)
ad1f37
                      ".machinemode zarch\n"
ad1f37
                      "stfle %[facilities]\n"
ad1f37
                      ".machinemode pop\n"
ad1f37
-                     : [facilities] "=Q" (facilities)
ad1f37
+                     : [facilities] "=Q" (cpu_facilities)
ad1f37
                      , [r0] "+r" (r0)
ad1f37
                      :
ad1f37
                      : "cc");
ad1f37
-    return is_bit_set((const char *)facilities, DFLTCC_FACILITY);
ad1f37
+    /* Initialize available functions */
ad1f37
+    if (is_dfltcc_enabled())
ad1f37
+        dfltcc(DFLTCC_QAF, &cpu_af, NULL, NULL, NULL, NULL, NULL);
ad1f37
+    else
ad1f37
+        memset(&cpu_af, 0, sizeof(cpu_af)); 
ad1f37
 }
ad1f37
 
ad1f37
+/*
ad1f37
+   Memory management.
ad1f37
+
ad1f37
+   DFLTCC requires parameter blocks and window to be aligned. zlib allows
ad1f37
+   users to specify their own allocation functions, so using e.g.
ad1f37
+   `posix_memalign' is not an option. Thus, we overallocate and take the
ad1f37
+   aligned portion of the buffer.
ad1f37
+*/ 
ad1f37
 void ZLIB_INTERNAL dfltcc_reset(strm, size)
ad1f37
     z_streamp strm;
ad1f37
     uInt size;
ad1f37
 {
ad1f37
     struct dfltcc_state *dfltcc_state =
ad1f37
         (struct dfltcc_state *)((char FAR *)strm->state + ALIGN_UP(size, 8));
ad1f37
-    struct dfltcc_qaf_param *param =
ad1f37
-        (struct dfltcc_qaf_param *)&dfltcc_state->param;
ad1f37
-    const char *s;
ad1f37
 
ad1f37
-    /* Initialize available functions */
ad1f37
-    if (is_dfltcc_enabled()) {
ad1f37
-        dfltcc(DFLTCC_QAF, param, NULL, NULL, NULL, NULL, NULL);
ad1f37
-        memmove(&dfltcc_state->af, param, sizeof(dfltcc_state->af));
ad1f37
-    } else
ad1f37
-        memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
ad1f37
+    memcpy(&dfltcc_state->af, &cpu_af, sizeof(dfltcc_state->af));
ad1f37
 
ad1f37
-    if (secure_getenv("SOURCE_DATE_EPOCH"))
ad1f37
+    if (env_source_date_epoch)
ad1f37
         /* User needs reproducible results, but the output of DFLTCC_CMPR
ad1f37
          * depends on buffers' page offsets.
ad1f37
          */
ad1f37
@@ -706,36 +780,11 @@ void ZLIB_INTERNAL dfltcc_reset(strm, size)
ad1f37
     dfltcc_state->param.nt = 1;
ad1f37
 
ad1f37
     /* Initialize tuning parameters */
ad1f37
-#ifndef DFLTCC_LEVEL_MASK
ad1f37
-#define DFLTCC_LEVEL_MASK 0x2
ad1f37
-#endif
ad1f37
-    s = secure_getenv("DFLTCC_LEVEL_MASK");
ad1f37
-    dfltcc_state->level_mask = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
-                                           DFLTCC_LEVEL_MASK;
ad1f37
-#ifndef DFLTCC_BLOCK_SIZE
ad1f37
-#define DFLTCC_BLOCK_SIZE 1048576
ad1f37
-#endif
ad1f37
-    s = secure_getenv("DFLTCC_BLOCK_SIZE");
ad1f37
-    dfltcc_state->block_size = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
-                                           DFLTCC_BLOCK_SIZE;
ad1f37
-#ifndef DFLTCC_FIRST_FHT_BLOCK_SIZE
ad1f37
-#define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
ad1f37
-#endif
ad1f37
-    s = secure_getenv("DFLTCC_FIRST_FHT_BLOCK_SIZE");
ad1f37
-    dfltcc_state->block_threshold = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
-                                                DFLTCC_FIRST_FHT_BLOCK_SIZE;
ad1f37
-#ifndef DFLTCC_DHT_MIN_SAMPLE_SIZE
ad1f37
-#define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
ad1f37
-#endif
ad1f37
-    s = secure_getenv("DFLTCC_DHT_MIN_SAMPLE_SIZE");
ad1f37
-    dfltcc_state->dht_threshold = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
-                                              DFLTCC_DHT_MIN_SAMPLE_SIZE;
ad1f37
-#ifndef DFLTCC_RIBM
ad1f37
-#define DFLTCC_RIBM 0
ad1f37
-#endif
ad1f37
-    s = secure_getenv("DFLTCC_RIBM");
ad1f37
-    dfltcc_state->param.ribm = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
-                                           DFLTCC_RIBM;
ad1f37
+    dfltcc_state->level_mask = env_level_mask;
ad1f37
+    dfltcc_state->block_size = env_block_size;
ad1f37
+    dfltcc_state->block_threshold = env_block_threshold;
ad1f37
+    dfltcc_state->dht_threshold = env_dht_threshold;
ad1f37
+    dfltcc_state->param.ribm = env_ribm;
ad1f37
 }
ad1f37
 
ad1f37
 voidpf ZLIB_INTERNAL dfltcc_alloc_state(strm, items, size)
ad1f37
@@ -787,22 +836,26 @@ void ZLIB_INTERNAL dfltcc_free_window(strm, w)
ad1f37
 
ad1f37
 /*
ad1f37
    Switching between hardware and software compression.
ad1f37
+
ad1f37
    DFLTCC does not support all zlib settings, e.g. generation of non-compressed
ad1f37
    blocks or alternative window sizes. When such settings are applied on the
ad1f37
    fly with deflateParams, we need to convert between hardware and software
ad1f37
    window formats.
ad1f37
 */
ad1f37
-int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy)
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy, flush)
ad1f37
     z_streamp strm;
ad1f37
     int level;
ad1f37
     int strategy;
ad1f37
+    int *flush;
ad1f37
 {
ad1f37
     deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
     struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
     struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
     int could_deflate = dfltcc_can_deflate(strm);
ad1f37
-    int can_deflate = dfltcc_are_params_ok(level, state->w_bits, strategy,
ad1f37
-                                           dfltcc_state->level_mask);
ad1f37
+    int can_deflate = dfltcc_can_deflate_with_params(strm,
ad1f37
+                                                     level,
ad1f37
+                                                     state->w_bits,
ad1f37
+                                                     strategy); 
ad1f37
 
ad1f37
     if (can_deflate == could_deflate)
ad1f37
         /* We continue to work in the same mode - no changes needed */
ad1f37
@@ -812,8 +865,11 @@ int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy)
ad1f37
         /* DFLTCC was not used yet - no changes needed */
ad1f37
         return Z_OK;
ad1f37
 
ad1f37
-    /* Switching between hardware and software is not implemented */
ad1f37
-    return Z_STREAM_ERROR;
ad1f37
+    /* For now, do not convert between window formats - simply get rid of the
ad1f37
+     * old data instead.
ad1f37
+     */
ad1f37
+    *flush = Z_FULL_FLUSH;
ad1f37
+    return Z_OK; 
ad1f37
 }
ad1f37
 
ad1f37
 /*
ad1f37
diff --git a/contrib/s390/dfltcc_deflate.h b/contrib/s390/dfltcc_deflate.h
ad1f37
index a129a91..de36784 100644
ad1f37
--- a/contrib/s390/dfltcc_deflate.h
ad1f37
+++ b/contrib/s390/dfltcc_deflate.h
ad1f37
@@ -9,7 +9,8 @@ int ZLIB_INTERNAL dfltcc_deflate OF((z_streamp strm,
ad1f37
                                      block_state *result));
ad1f37
 int ZLIB_INTERNAL dfltcc_deflate_params OF((z_streamp strm,
ad1f37
                                             int level,
ad1f37
-                                            int strategy));
ad1f37
+                                            int strategy,
ad1f37
+                                            int *flush)); 
ad1f37
 int ZLIB_INTERNAL dfltcc_deflate_set_dictionary OF((z_streamp strm,
ad1f37
                                                     const Bytef *dictionary,
ad1f37
                                                     uInt dict_length));
ad1f37
@@ -29,11 +30,14 @@ int ZLIB_INTERNAL dfltcc_deflate_get_dictionary OF((z_streamp strm,
ad1f37
     } while (0)
ad1f37
 #define DEFLATE_RESET_KEEP_HOOK(strm) \
ad1f37
     dfltcc_reset((strm), sizeof(deflate_state))
ad1f37
-#define DEFLATE_PARAMS_HOOK(strm, level, strategy) \
ad1f37
+#define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) \
ad1f37
     do { \
ad1f37
         int err; \
ad1f37
 \
ad1f37
-        err = dfltcc_deflate_params((strm), (level), (strategy)); \
ad1f37
+        err = dfltcc_deflate_params((strm), \
ad1f37
+                                    (level), \
ad1f37
+                                    (strategy), \
ad1f37
+                                    (hook_flush)); \
ad1f37
         if (err == Z_STREAM_ERROR) \
ad1f37
             return err; \
ad1f37
     } while (0)
ad1f37
diff --git a/deflate.c b/deflate.c
ad1f37
index b17a7dd..a80bd3e 100644
ad1f37
--- a/deflate.c
ad1f37
+++ b/deflate.c
ad1f37
@@ -74,7 +74,7 @@ const char deflate_copyright[] =
ad1f37
 #define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
ad1f37
 #define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
ad1f37
 #define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
ad1f37
-#define DEFLATE_PARAMS_HOOK(strm, level, strategy) do {} while (0)
ad1f37
+#define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) do {} while (0)
ad1f37
 #define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
ad1f37
 #define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
ad1f37
 #define DEFLATE_HOOK(strm, flush, bstate) 0
ad1f37
@@ -589,6 +589,7 @@ int ZEXPORT deflateParams(strm, level, strategy)
ad1f37
 {
ad1f37
     deflate_state *s;
ad1f37
     compress_func func;
ad1f37
+    int hook_flush = Z_NO_FLUSH;
ad1f37
 
ad1f37
     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
ad1f37
     s = strm->state;
ad1f37
@@ -601,13 +602,14 @@ int ZEXPORT deflateParams(strm, level, strategy)
ad1f37
     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
ad1f37
         return Z_STREAM_ERROR;
ad1f37
     }
ad1f37
-    DEFLATE_PARAMS_HOOK(strm, level, strategy);
ad1f37
+    DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush);
ad1f37
     func = configuration_table[s->level].func;
ad1f37
 
ad1f37
-    if ((strategy != s->strategy || func != configuration_table[level].func) &&
ad1f37
-        s->high_water) {
ad1f37
+    if ((strategy != s->strategy || func != configuration_table[level].func ||
ad1f37
+        hook_flush != Z_NO_FLUSH) && s->high_water) {
ad1f37
         /* Flush the last buffer: */
ad1f37
-        int err = deflate(strm, Z_BLOCK);
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
@@ -1065,7 +1067,6 @@ int ZEXPORT deflate (strm, flush)
ad1f37
     }
ad1f37
 
ad1f37
     if (flush != Z_FINISH) return Z_OK;
ad1f37
-    if (s->wrap <= 0) return Z_STREAM_END;
ad1f37
 
ad1f37
     /* Write the trailer */
ad1f37
 #ifdef GZIP
ad1f37
@@ -1081,7 +1082,7 @@ int ZEXPORT deflate (strm, flush)
ad1f37
     }
ad1f37
     else
ad1f37
 #endif
ad1f37
-    {
ad1f37
+    if (s->wrap == 1) {
ad1f37
         putShortMSB(s, (uInt)(strm->adler >> 16));
ad1f37
         putShortMSB(s, (uInt)(strm->adler & 0xffff));
ad1f37
     }
ad1f37
@@ -1090,7 +1091,11 @@ int ZEXPORT deflate (strm, flush)
ad1f37
      * to flush the rest.
ad1f37
      */
ad1f37
     if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
ad1f37
-    return s->pending != 0 ? Z_OK : Z_STREAM_END;
ad1f37
+    if (s->pending == 0) {
ad1f37
+        Assert(s->bi_valid == 0, "bi_buf not flushed");
ad1f37
+        return Z_STREAM_END;
ad1f37
+    }
ad1f37
+    return Z_OK; 
ad1f37
 }
ad1f37
 
ad1f37
 /* ========================================================================= */
ad1f37
-- 
ad1f37
2.26.0
ad1f37