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