730648
Subject: [PATCH] Fixed DFLTCC compression level switching issues
730648
730648
---
b75b57
 configure                     |  4 +--
730648
 contrib/s390/dfltcc.c         | 52 ++++++++++++++++++++++++++++++-----
730648
 contrib/s390/dfltcc_deflate.h |  2 ++
730648
 deflate.c                     | 12 ++++----
730648
 test/infcover.c               |  2 +-
b75b57
 5 files changed, 57 insertions(+), 15 deletions(-)
730648
730648
diff --git a/configure b/configure
b75b57
index bfe4386..70ed86b 100755
730648
--- a/configure
730648
+++ b/configure
b75b57
@@ -139,7 +139,7 @@ case "$1" in
b75b57
     -w* | --warn) warn=1; shift ;;
b75b57
     -d* | --debug) debug=1; shift ;;
b75b57
     --dfltcc)
b75b57
-	    CFLAGS="$CFLAGS -DDFLTCC"
b75b57
+	    CFLAGS="$CFLAGS -DDFLTCC -DDFLTCC_LEVEL_MASK=0x7e"
b75b57
       OBJC="$OBJC dfltcc.o"
b75b57
 	    PIC_OBJC="$PIC_OBJC dfltcc.lo"
b75b57
       shift
730648
@@ -838,7 +838,7 @@ cat > $test.c << EOF
730648
 #include <sys/sdt.h>
730648
 int main() { return 0; }
730648
 EOF
730648
-if try ${CC} ${CFLAGS} $test.c; then
730648
+ if try $CC -c $CFLAGS $test.c; then
730648
     echo "Checking for sys/sdt.h ... Yes." | tee -a configure.log
730648
     CFLAGS="$CFLAGS -DHAVE_SYS_SDT_H"
730648
     SFLAGS="$SFLAGS -DHAVE_SYS_SDT_H"
730648
diff --git a/contrib/s390/dfltcc.c b/contrib/s390/dfltcc.c
730648
index d88a0d6..94a196f 100644
730648
--- a/contrib/s390/dfltcc.c
730648
+++ b/contrib/s390/dfltcc.c
730648
@@ -350,8 +350,12 @@ int ZLIB_INTERNAL dfltcc_deflate(strm, flush, result)
730648
     int soft_bcc;
730648
     int no_flush;
730648
 
730648
-    if (!dfltcc_can_deflate(strm))
730648
+    if (!dfltcc_can_deflate(strm)) {
730648
+        /* Clear history. */
730648
+        if (flush == Z_FULL_FLUSH)
730648
+            param->hl = 0;
730648
         return 0;
730648
+    }
730648
 
730648
 again:
730648
     masked_avail_in = 0;
730648
@@ -376,7 +380,8 @@ again:
730648
         /* Clear history. */
730648
         if (flush == Z_FULL_FLUSH)
730648
             param->hl = 0; 
730648
-        *result = need_more;
730648
+        /* Trigger block post-processing if necessary. */
730648
+        *result = no_flush ? need_more : block_done;
730648
         return 1;
730648
     }
730648
 
730648
@@ -403,13 +408,18 @@ again:
730648
             param->bcf = 0;
730648
             dfltcc_state->block_threshold =
730648
                 strm->total_in + dfltcc_state->block_size;
730648
-            if (strm->avail_out == 0) {
730648
-                *result = need_more;
730648
-                return 1;
730648
-            }
730648
         }
730648
     }
730648
 
730648
+    /* No space for compressed data. If we proceed, dfltcc_cmpr() will return
730648
+     * DFLTCC_CC_OP1_TOO_SHORT without buffering header bits, but we will still
730648
+     * set BCF=1, which is wrong. Avoid complications and return early.
730648
+     */
730648
+    if (strm->avail_out == 0) {
730648
+        *result = need_more;
730648
+        return 1;
730648
+    }
730648
+
730648
     /* The caller gave us too much data. Pass only one block worth of
730648
      * uncompressed data to DFLTCC and mask the rest, so that on the next
730648
      * iteration we start a new block.
730648
@@ -737,10 +747,15 @@ __attribute__((constructor)) local void init_globals(void)
730648
      * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
730648
      * is 64-bit, it's always z/Architecture mode at runtime.
730648
      */
730648
-    __asm__ volatile(".machinemode push\n"
730648
+     __asm__ volatile(
730648
+#ifndef __clang__
730648
+                     ".machinemode push\n"
730648
                      ".machinemode zarch\n"
730648
+#endif
730648
                      "stfle %[facilities]\n"
730648
+#ifndef __clang__
730648
                      ".machinemode pop\n"
730648
+#endif
730648
                      : [facilities] "=Q" (cpu_facilities)
730648
                      , [r0] "+r" (r0)
730648
                      :
730648
@@ -872,6 +887,28 @@ int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy, flush)
730648
     return Z_OK; 
730648
 }
730648
 
730648
+int ZLIB_INTERNAL dfltcc_deflate_done(strm, flush)
730648
+    z_streamp strm;
730648
+    int flush;
730648
+{
730648
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
730648
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
730648
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
730648
+
730648
+    /* When deflate(Z_FULL_FLUSH) is called with small avail_out, it might
730648
+     * close the block without resetting the compression state. Detect this
730648
+     * situation and return that deflation is not done.
730648
+     */
730648
+    if (flush == Z_FULL_FLUSH && strm->avail_out == 0)
730648
+        return 0;
730648
+
730648
+    /* Return that deflation is not done if DFLTCC is used and either it
730648
+     * buffered some data (Continuation Flag is set), or has not written EOBS
730648
+     * yet (Block-Continuation Flag is set).
730648
+     */
730648
+    return !dfltcc_can_deflate(strm) || (!param->cf && !param->bcf);
730648
+}
730648
+
730648
 /*
730648
    Preloading history.
730648
 */
730648
@@ -925,6 +962,7 @@ int ZLIB_INTERNAL dfltcc_deflate_set_dictionary(strm, dictionary, dict_length)
730648
 
730648
     append_history(param, state->window, dictionary, dict_length);
730648
     state->strstart = 1; /* Add FDICT to zlib header */
730648
+    state->block_start = state->strstart; /* Make deflate_stored happy */
730648
     return Z_OK;
730648
 }
730648
 
730648
diff --git a/contrib/s390/dfltcc_deflate.h b/contrib/s390/dfltcc_deflate.h
730648
index de36784..914daa4 100644
730648
--- a/contrib/s390/dfltcc_deflate.h
730648
+++ b/contrib/s390/dfltcc_deflate.h
730648
@@ -11,6 +11,7 @@ int ZLIB_INTERNAL dfltcc_deflate_params OF((z_streamp strm,
730648
                                             int level,
730648
                                             int strategy,
730648
                                             int *flush)); 
730648
+int ZLIB_INTERNAL dfltcc_deflate_done OF((z_streamp strm, int flush));
730648
 int ZLIB_INTERNAL dfltcc_deflate_set_dictionary OF((z_streamp strm,
730648
                                                     const Bytef *dictionary,
730648
                                                     uInt dict_length));
730648
@@ -41,6 +42,7 @@ int ZLIB_INTERNAL dfltcc_deflate_get_dictionary OF((z_streamp strm,
730648
         if (err == Z_STREAM_ERROR) \
730648
             return err; \
730648
     } while (0)
730648
+#define DEFLATE_DONE dfltcc_deflate_done
730648
 #define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, source_len) \
730648
     do { \
730648
         if (dfltcc_can_deflate((strm))) \
730648
diff --git a/deflate.c b/deflate.c
730648
index d907a1b..085abbe 100644
730648
--- a/deflate.c
730648
+++ b/deflate.c
730648
@@ -75,6 +75,7 @@ const char deflate_copyright[] =
730648
 #define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
730648
 #define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
730648
 #define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) do {} while (0)
730648
+#define DEFLATE_DONE(strm, flush) 1
730648
 #define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
730648
 #define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
730648
 #define DEFLATE_HOOK(strm, flush, bstate) 0
730648
@@ -605,14 +606,15 @@ int ZEXPORT deflateParams(strm, level, strategy)
730648
     DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush);
730648
     func = configuration_table[s->level].func;
730648
 
730648
-    if ((strategy != s->strategy || func != configuration_table[level].func ||
730648
-        hook_flush != Z_NO_FLUSH) && s->last_flush != -2) {
730648
+    if (((strategy != s->strategy || func != configuration_table[level].func) &&
730648
+        s->last_flush != -2) || hook_flush != Z_NO_FLUSH) {
730648
         /* Flush the last buffer: */
730648
-        int err = deflate(strm, RANK(hook_flush) > RANK(Z_BLOCK) ?
730648
-                          hook_flush : Z_BLOCK); 
730648
+        int flush = RANK(hook_flush) > RANK(Z_BLOCK) ? hook_flush : Z_BLOCK;
730648
+        int err = deflate(strm, flush);
730648
         if (err == Z_STREAM_ERROR)
730648
             return err;
730648
-        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
730648
+        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead ||
730648
+            !DEFLATE_DONE(strm, flush))
730648
             return Z_BUF_ERROR;
730648
     }
730648
     if (s->level != level) {
730648
diff --git a/test/infcover.c b/test/infcover.c
730648
index a34cd17..a208219 100644
730648
--- a/test/infcover.c
730648
+++ b/test/infcover.c
730648
@@ -373,7 +373,7 @@ local void cover_support(void)
730648
     mem_setup(&strm;;
730648
     strm.avail_in = 0;
730648
     strm.next_in = Z_NULL;
730648
-    ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream));
730648
+    ret = inflateInit_(&strm, &ZLIB_VERSION[1], (int)sizeof(z_stream));
730648
                                                 assert(ret == Z_VERSION_ERROR);
730648
     mem_done(&strm, "wrong version");
730648
 
730648
-- 
730648
2.26.0
730648