adenilson / rpms / zlib

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