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