Blame SOURCES/zlib-1.2.11-IBM-Z-hw-accelrated-inflate-small-window.patch

8cf299
--- a/contrib/s390/dfltcc.c
8cf299
+++ b/contrib/s390/dfltcc.c
8cf299
@@ -539,10 +539,6 @@ int ZLIB_INTERNAL dfltcc_can_inflate(strm)
8cf299
     struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
8cf299
     struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
8cf299
 
8cf299
-    /* Unsupported compression settings */
8cf299
-    if (state->wbits != HB_BITS)
8cf299
-        return 0;
8cf299
-
8cf299
     /* Unsupported hardware */
8cf299
     return is_bit_set(dfltcc_state->af.fns, DFLTCC_XPND) &&
8cf299
                is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0);
8cf299
@@ -606,8 +602,6 @@ dfltcc_inflate_action ZLIB_INTERNAL dfltcc_inflate(strm, flush, ret)
8cf299
     /* Translate stream to parameter block */
8cf299
     param->cvt = state->flags ? CVT_CRC32 : CVT_ADLER32;
8cf299
     param->sbb = state->bits;
8cf299
-    param->hl = state->whave; /* Software and hardware history formats match */
8cf299
-    param->ho = (state->wnext - state->whave) & ((1 << HB_BITS) - 1);
8cf299
     if (param->hl)
8cf299
         param->nt = 0; /* Honor history for the first block */
8cf299
     param->cv = state->flags ? ZSWAP32(state->check) : state->check;
8cf299
@@ -621,8 +615,6 @@ dfltcc_inflate_action ZLIB_INTERNAL dfltcc_inflate(strm, flush, ret)
8cf299
     strm->msg = oesc_msg(dfltcc_state->msg, param->oesc);
8cf299
     state->last = cc == DFLTCC_CC_OK;
8cf299
     state->bits = param->sbb;
8cf299
-    state->whave = param->hl;
8cf299
-    state->wnext = (param->ho + param->hl) & ((1 << HB_BITS) - 1);
8cf299
     strm->adler = state->check = state->flags ? ZSWAP32(param->cv) : param->cv;
8cf299
     if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0) {
8cf299
         /* Report an error if stream is corrupted */
8cf299
@@ -644,11 +636,52 @@ int ZLIB_INTERNAL dfltcc_was_inflate_used(strm)
8cf299
     return !param->nt;
8cf299
 }
8cf299
 
8cf299
+/*
8cf299
+   Rotates a circular buffer.
8cf299
+   The implementation is based on https://cplusplus.com/reference/algorithm/rotate/
8cf299
+ */
8cf299
+local void rotate OF((Bytef *start, Bytef *pivot, Bytef *end));
8cf299
+local void rotate(start, pivot, end)
8cf299
+    Bytef *start;
8cf299
+    Bytef *pivot;
8cf299
+    Bytef *end;
8cf299
+{
8cf299
+    Bytef *p = pivot;
8cf299
+    Bytef tmp;
8cf299
+
8cf299
+    while (p != start) {
8cf299
+        tmp = *start;
8cf299
+        *start = *p;
8cf299
+        *p = tmp;
8cf299
+
8cf299
+        start++;
8cf299
+        p++;
8cf299
+
8cf299
+        if (p == end)
8cf299
+            p = pivot;
8cf299
+        else if (start == pivot)
8cf299
+            pivot = p;
8cf299
+    }
8cf299
+}
8cf299
+
8cf299
+#define MIN(x, y) ({    \
8cf299
+    typeof(x) _x = (x); \
8cf299
+    typeof(y) _y = (y); \
8cf299
+    _x < _y ? _x : _y;  \
8cf299
+})
8cf299
+
8cf299
+#define MAX(x, y) ({    \
8cf299
+    typeof(x) _x = (x); \
8cf299
+    typeof(y) _y = (y); \
8cf299
+    _x > _y ? _x : _y;  \
8cf299
+})
8cf299
+
8cf299
 int ZLIB_INTERNAL dfltcc_inflate_disable(strm)
8cf299
     z_streamp strm;
8cf299
 {
8cf299
     struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
8cf299
     struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
8cf299
+    struct dfltcc_param_v0 *param = &dfltcc_state->param;
8cf299
 
8cf299
     if (!dfltcc_can_inflate(strm))
8cf299
         return 0;
8cf299
@@ -660,6 +693,9 @@ int ZLIB_INTERNAL dfltcc_inflate_disable(strm)
8cf299
         return 1;
8cf299
     /* DFLTCC was not used yet - decompress in software */
8cf299
     memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
8cf299
+    /* Convert the window from the hardware to the software format */
8cf299
+    rotate(state->window, state->window + param->ho, state->window + HB_SIZE);
8cf299
+    state->whave = state->wnext = MIN(param->hl, state->wsize);
8cf299
     return 0;
8cf299
 }
8cf299
 
8cf299
@@ -830,9 +866,9 @@ voidpf ZLIB_INTERNAL dfltcc_alloc_window(strm, items, size)
8cf299
     voidpf p, w;
8cf299
 
8cf299
     /* To simplify freeing, we store the pointer to the allocated buffer right
8cf299
-     * before the window.
8cf299
+     * before the window. Note that DFLTCC always uses HB_SIZE bytes.
8cf299
      */
8cf299
-    p = ZALLOC(strm, sizeof(voidpf) + items * size + PAGE_ALIGN,
8cf299
+    p = ZALLOC(strm, sizeof(voidpf) + MAX(items * size, HB_SIZE) + PAGE_ALIGN,
8cf299
                sizeof(unsigned char));
8cf299
     if (p == NULL)
8cf299
         return NULL;
8cf299
@@ -841,6 +877,14 @@ voidpf ZLIB_INTERNAL dfltcc_alloc_window(strm, items, size)
8cf299
     return w;
8cf299
 }
8cf299
 
8cf299
+void ZLIB_INTERNAL dfltcc_copy_window(dest, src, n)
8cf299
+    void *dest;
8cf299
+    const void *src;
8cf299
+    size_t n;
8cf299
+{
8cf299
+    memcpy(dest, src, MAX(n, HB_SIZE));
8cf299
+}
8cf299
+
8cf299
 void ZLIB_INTERNAL dfltcc_free_window(strm, w)
8cf299
     z_streamp strm;
8cf299
     voidpf w;
8cf299
@@ -951,6 +995,24 @@ local void append_history(param, history, buf, count)
8cf299
     }
8cf299
 }
8cf299
 
8cf299
+local void get_history OF((struct dfltcc_param_v0 FAR *param,
8cf299
+                           const Bytef *history,
8cf299
+                           Bytef *buf));
8cf299
+local void get_history(param, history, buf)
8cf299
+    struct dfltcc_param_v0 FAR *param;
8cf299
+    const Bytef *history;
8cf299
+    Bytef *buf;
8cf299
+{
8cf299
+    if (param->ho + param->hl <= HB_SIZE)
8cf299
+        /* Circular history buffer does not wrap - copy one chunk */
8cf299
+        memcpy(buf, history + param->ho, param->hl);
8cf299
+    else {
8cf299
+        /* Circular history buffer wraps - copy two chunks */
8cf299
+        memcpy(buf, history + param->ho, HB_SIZE - param->ho);
8cf299
+        memcpy(buf + HB_SIZE - param->ho, history, param->ho + param->hl - HB_SIZE);
8cf299
+    }
8cf299
+}
8cf299
+
8cf299
 int ZLIB_INTERNAL dfltcc_deflate_set_dictionary(strm, dictionary, dict_length)
8cf299
     z_streamp strm;
8cf299
     const Bytef *dictionary;
8cf299
@@ -975,20 +1037,43 @@ int ZLIB_INTERNAL dfltcc_deflate_get_dictionary(strm, dictionary, dict_length)
8cf299
     struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
8cf299
     struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
8cf299
 
8cf299
-    if (dictionary) {
8cf299
-        if (param->ho + param->hl <= HB_SIZE)
8cf299
-            /* Circular history buffer does not wrap - copy one chunk */
8cf299
-            zmemcpy(dictionary, state->window + param->ho, param->hl);
8cf299
-        else {
8cf299
-            /* Circular history buffer wraps - copy two chunks */
8cf299
-            zmemcpy(dictionary,
8cf299
-                    state->window + param->ho,
8cf299
-                    HB_SIZE - param->ho);
8cf299
-            zmemcpy(dictionary + HB_SIZE - param->ho,
8cf299
-                    state->window,
8cf299
-                    param->ho + param->hl - HB_SIZE);
8cf299
-        }
8cf299
+    if (dictionary)
8cf299
+        get_history(param, state->window, dictionary);
8cf299
+    if (dict_length)
8cf299
+        *dict_length = param->hl;
8cf299
+    return Z_OK;
8cf299
+}
8cf299
+
8cf299
+int ZLIB_INTERNAL dfltcc_inflate_set_dictionary(strm, dictionary, dict_length)
8cf299
+    z_streamp strm;
8cf299
+    const Bytef *dictionary;
8cf299
+    uInt dict_length;
8cf299
+{
8cf299
+    struct inflate_state *state = (struct inflate_state *)strm->state;
8cf299
+    struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
8cf299
+    struct dfltcc_param_v0 *param = &dfltcc_state->param;
8cf299
+
8cf299
+    if (inflate_ensure_window(state)) {
8cf299
+        state->mode = MEM;
8cf299
+        return Z_MEM_ERROR;
8cf299
     }
8cf299
+
8cf299
+    append_history(param, state->window, dictionary, dict_length);
8cf299
+    state->havedict = 1;
8cf299
+    return Z_OK;
8cf299
+}
8cf299
+
8cf299
+int ZLIB_INTERNAL dfltcc_inflate_get_dictionary(strm, dictionary, dict_length)
8cf299
+    z_streamp strm;
8cf299
+    Bytef *dictionary;
8cf299
+    uInt *dict_length;
8cf299
+{
8cf299
+    struct inflate_state *state = (struct inflate_state *)strm->state;
8cf299
+    struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
8cf299
+    struct dfltcc_param_v0 *param = &dfltcc_state->param;
8cf299
+
8cf299
+    if (dictionary && state->window)
8cf299
+        get_history(param, state->window, dictionary);
8cf299
     if (dict_length)
8cf299
         *dict_length = param->hl;
8cf299
     return Z_OK;
8cf299
--- a/contrib/s390/dfltcc.h
8cf299
+++ b/contrib/s390/dfltcc.h
8cf299
@@ -11,6 +11,8 @@ void ZLIB_INTERNAL dfltcc_copy_state OF((voidpf dst, const voidpf src,
8cf299
 void ZLIB_INTERNAL dfltcc_reset OF((z_streamp strm, uInt size));
8cf299
 voidpf ZLIB_INTERNAL dfltcc_alloc_window OF((z_streamp strm, uInt items,
8cf299
                                              uInt size));
8cf299
+void ZLIB_INTERNAL dfltcc_copy_window OF((void *dest, const void *src,
8cf299
+                                          size_t n));
8cf299
 void ZLIB_INTERNAL dfltcc_free_window OF((z_streamp strm, voidpf w));
8cf299
 #define DFLTCC_BLOCK_HEADER_BITS 3
8cf299
 #define DFLTCC_HLITS_COUNT_BITS 5
8cf299
@@ -44,11 +46,18 @@ dfltcc_inflate_action ZLIB_INTERNAL dfltcc_inflate OF((z_streamp strm,
8cf299
                                                        int flush, int *ret));
8cf299
 int ZLIB_INTERNAL dfltcc_was_inflate_used OF((z_streamp strm));
8cf299
 int ZLIB_INTERNAL dfltcc_inflate_disable OF((z_streamp strm));
8cf299
+int ZLIB_INTERNAL dfltcc_inflate_set_dictionary OF((z_streamp strm,
8cf299
+                                                    const Bytef *dictionary,
8cf299
+                                                    uInt dict_length));
8cf299
+int ZLIB_INTERNAL dfltcc_inflate_get_dictionary OF((z_streamp strm,
8cf299
+                                                    Bytef *dictionary,
8cf299
+                                                    uInt* dict_length));
8cf299
 
8cf299
 #define ZALLOC_STATE dfltcc_alloc_state
8cf299
 #define ZFREE_STATE ZFREE
8cf299
 #define ZCOPY_STATE dfltcc_copy_state
8cf299
 #define ZALLOC_WINDOW dfltcc_alloc_window
8cf299
+#define ZCOPY_WINDOW dfltcc_copy_window
8cf299
 #define ZFREE_WINDOW dfltcc_free_window
8cf299
 #define TRY_FREE_WINDOW dfltcc_free_window
8cf299
 #define INFLATE_RESET_KEEP_HOOK(strm) \
8cf299
@@ -77,5 +86,15 @@ int ZLIB_INTERNAL dfltcc_inflate_disable OF((z_streamp strm));
8cf299
     do { \
8cf299
         if (dfltcc_was_inflate_used((strm))) return Z_STREAM_ERROR; \
8cf299
     } while (0)
8cf299
+#define INFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) \
8cf299
+    do { \
8cf299
+        if (dfltcc_can_inflate(strm)) \
8cf299
+            return dfltcc_inflate_set_dictionary(strm, dict, dict_len); \
8cf299
+    } while (0)
8cf299
+#define INFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) \
8cf299
+    do { \
8cf299
+        if (dfltcc_can_inflate(strm)) \
8cf299
+            return dfltcc_inflate_get_dictionary(strm, dict, dict_len); \
8cf299
+    } while (0)
8cf299
 
8cf299
 #endif
8cf299
\ No newline at end of file
8cf299
diff --git a/inflate.c b/inflate.c
8cf299
index 3750152..a0e2169 100644
8cf299
--- a/inflate.c
8cf299
+++ b/inflate.c
8cf299
@@ -93,6 +93,7 @@
8cf299
 #define ZFREE_STATE ZFREE
8cf299
 #define ZCOPY_STATE zmemcpy
8cf299
 #define ZALLOC_WINDOW ZALLOC
8cf299
+#define ZCOPY_WINDOW zmemcpy
8cf299
 #define ZFREE_WINDOW ZFREE
8cf299
 #define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
8cf299
 #define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0)
8cf299
@@ -101,6 +102,8 @@
8cf299
 #define INFLATE_NEED_UPDATEWINDOW(strm) 1
8cf299
 #define INFLATE_MARK_HOOK(strm) do {} while (0)
8cf299
 #define INFLATE_SYNC_POINT_HOOK(strm) do {} while (0)
8cf299
+#define INFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
8cf299
+#define INFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
8cf299
 #endif
8cf299
 
8cf299
 #ifdef MAKEFIXED
8cf299
@@ -1330,6 +1333,8 @@ uInt *dictLength;
8cf299
     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
8cf299
     state = (struct inflate_state FAR *)strm->state;
8cf299
 
8cf299
+    INFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);
8cf299
+
8cf299
     /* copy dictionary */
8cf299
     if (state->whave && dictionary != Z_NULL) {
8cf299
         zmemcpy(dictionary, state->window + state->wnext,
8cf299
@@ -1365,6 +1370,8 @@ uInt dictLength;
8cf299
             return Z_DATA_ERROR;
8cf299
     }
8cf299
 
8cf299
+    INFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);
8cf299
+
8cf299
     /* copy dictionary to window using updatewindow(), which will amend the
8cf299
        existing dictionary if appropriate */
8cf299
     ret = updatewindow(strm, dictionary + dictLength, dictLength);
8cf299
@@ -1529,8 +1536,7 @@ z_streamp source;
8cf299
     }
8cf299
     copy->next = copy->codes + (state->next - state->codes);
8cf299
     if (window != Z_NULL) {
8cf299
-        wsize = 1U << state->wbits;
8cf299
-        zmemcpy(window, state->window, wsize);
8cf299
+        ZCOPY_WINDOW(window, state->window, 1U << state->wbits);
8cf299
     }
8cf299
     copy->window = window;
8cf299
     dest->state = (struct internal_state FAR *)copy;