adenilson / rpms / zlib

Forked from rpms/zlib 8 months ago
Clone

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

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