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