adenilson / rpms / zlib

Forked from rpms/zlib 8 months ago
Clone

Blame SOURCES/zlib-1.2.11-IBM-DFLTCC-compression-level-switching-issues.patch

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