adenilson / rpms / zlib

Forked from rpms/zlib 8 months ago
Clone
ad1f37
From 4e65ca20fc242e4a03471558a357d7809adeb9c4 Mon Sep 17 00:00:00 2001
ad1f37
From: IBM developers
ad1f37
Date: Thu, 1 Aug 2019 09:02:01 +0200
ad1f37
Subject: [PATCH] Add support for IBM Z hardware-accelerated deflate
ad1f37
ad1f37
Future versions of IBM Z mainframes will provide DFLTCC instruction,
ad1f37
which implements deflate algorithm in hardware with estimated
ad1f37
compression and decompression performance orders of magnitude faster
ad1f37
than the current zlib and ratio comparable with that of level 1.
ad1f37
ad1f37
This patch adds DFLTCC support to zlib. In order to enable it, the
ad1f37
following build commands should be used:
ad1f37
ad1f37
    $ CFLAGS=-DDFLTCC ./configure
ad1f37
    $ make OBJA=dfltcc.o PIC_OBJA=dfltcc.lo
ad1f37
ad1f37
When built like this, zlib would compress in hardware on level 1, and in
ad1f37
software on all other levels. Decompression will always happen in
ad1f37
hardware. In order to enable DFLTCC compression for levels 1-6 (i.e. to
ad1f37
make it used by default) one could either add -DDFLTCC_LEVEL_MASK=0x7e
ad1f37
at compile time, or set the environment variable DFLTCC_LEVEL_MASK to
ad1f37
0x7e at run time.
ad1f37
ad1f37
Two DFLTCC compression calls produce the same results only when they
ad1f37
both are made on machines of the same generation, and when the
ad1f37
respective buffers have the same offset relative to the start of the
ad1f37
page. Therefore care should be taken when using hardware compression
ad1f37
when reproducible results are desired. One such use case - reproducible
ad1f37
software builds - is handled explicitly: when SOURCE_DATE_EPOCH
ad1f37
environment variable is set, the hardware compression is disabled.
ad1f37
ad1f37
DFLTCC does not support every single zlib feature, in particular:
ad1f37
ad1f37
    * inflate(Z_BLOCK) and inflate(Z_TREES)
ad1f37
    * inflateMark()
ad1f37
    * inflatePrime()
ad1f37
    * deflateParams() after the first deflate() call
ad1f37
ad1f37
When used, these functions will either switch to software, or, in case
ad1f37
this is not possible, gracefully fail.
ad1f37
ad1f37
This patch tries to add DFLTCC support in a least intrusive way.
ad1f37
All SystemZ-specific code was placed into a separate file, but
ad1f37
unfortunately there is still a noticeable amount of changes in the
ad1f37
main zlib code. Below is the summary of those changes.
ad1f37
ad1f37
DFLTCC takes as arguments a parameter block, an input buffer, an output
ad1f37
buffer and a window. Since DFLTCC requires parameter block to be
ad1f37
doubleword-aligned, and it's reasonable to allocate it alongside
ad1f37
deflate and inflate states, ZALLOC_STATE, ZFREE_STATE and ZCOPY_STATE
ad1f37
macros were introduced in order to encapsulate the allocation details.
ad1f37
The same is true for window, for which ZALLOC_WINDOW and
ad1f37
TRY_FREE_WINDOW macros were introduced.
ad1f37
ad1f37
While for inflate software and hardware window formats match, this is
ad1f37
not the case for deflate. Therefore, deflateSetDictionary and
ad1f37
deflateGetDictionary need special handling, which is triggered using the
ad1f37
new DEFLATE_SET_DICTIONARY_HOOK and DEFLATE_GET_DICTIONARY_HOOK macros.
ad1f37
ad1f37
deflateResetKeep() and inflateResetKeep() now update the DFLTCC
ad1f37
parameter block, which is allocated alongside zlib state, using
ad1f37
the new DEFLATE_RESET_KEEP_HOOK and INFLATE_RESET_KEEP_HOOK macros.
ad1f37
ad1f37
In order to make unsupported deflateParams(), inflatePrime() and
ad1f37
inflateMark() calls to fail gracefully, the new DEFLATE_PARAMS_HOOK,
ad1f37
INFLATE_PRIME_HOOK and INFLATE_MARK_HOOK macros were introduced.
ad1f37
ad1f37
The algorithm implemented in hardware has different compression ratio
ad1f37
than the one implemented in software. In order for deflateBound() to
ad1f37
return the correct results for the hardware implementation, the new
ad1f37
DEFLATE_BOUND_ADJUST_COMPLEN and DEFLATE_NEED_CONSERVATIVE_BOUND macros
ad1f37
were introduced.
ad1f37
ad1f37
Actual compression and decompression are handled by the new DEFLATE_HOOK
ad1f37
and INFLATE_TYPEDO_HOOK macros. Since inflation with DFLTCC manages the
ad1f37
window on its own, calling updatewindow() is suppressed using the new
ad1f37
INFLATE_NEED_UPDATEWINDOW() macro.
ad1f37
ad1f37
In addition to compression, DFLTCC computes CRC-32 and Adler-32
ad1f37
checksums, therefore, whenever it's used, software checksumming needs to
ad1f37
be suppressed using the new DEFLATE_NEED_CHECKSUM and
ad1f37
INFLATE_NEED_CHECKSUM macros.
ad1f37
ad1f37
DFLTCC will refuse to write an End-of-block Symbol if there is no input
ad1f37
data, thus in some cases it is necessary to do this manually. In order
ad1f37
to achieve this, send_bits, bi_reverse, bi_windup and flush_pending
ad1f37
were promoted from local to ZLIB_INTERNAL. Furthermore, since block and
ad1f37
stream termination must be handled in software as well, block_state enum
ad1f37
was moved to deflate.h.
ad1f37
ad1f37
Since the first call to dfltcc_inflate already needs the window, and it
ad1f37
might be not allocated yet, inflate_ensure_window was factored out of
ad1f37
updatewindow and made ZLIB_INTERNAL.
ad1f37
---
ad1f37
 Makefile.in                   |   8 +
ad1f37
 configure                     |  13 +
ad1f37
 contrib/README.contrib        |   4 +
ad1f37
 contrib/s390/dfltcc.c         | 901 ++++++++++++++++++++++++++++++++++
ad1f37
 contrib/s390/dfltcc.h         |  55 +++
ad1f37
 contrib/s390/dfltcc_deflate.h |  50 ++
ad1f37
 deflate.c                     |  60 ++-
ad1f37
 deflate.h                     |  12 +
ad1f37
 gzguts.h                      |   4 +
ad1f37
 inflate.c                     |  84 +++-
ad1f37
 inflate.h                     |   2 +
ad1f37
 test/infcover.c               |   2 +-
ad1f37
 test/minigzip.c               |   4 +
ad1f37
 trees.c                       |  13 +-
ad1f37
 14 files changed, 1161 insertions(+), 51 deletions(-)
ad1f37
 create mode 100644 contrib/s390/dfltcc.c
ad1f37
 create mode 100644 contrib/s390/dfltcc.h
ad1f37
 create mode 100644 contrib/s390/dfltcc_deflate.h
ad1f37
ad1f37
diff --git a/Makefile.in b/Makefile.in
ad1f37
index 5a77949..e756e2f 100644
ad1f37
--- a/Makefile.in
ad1f37
+++ b/Makefile.in
ad1f37
@@ -143,6 +143,14 @@ match.lo: match.S
ad1f37
 	mv _match.o match.lo
ad1f37
 	rm -f _match.s
ad1f37
 
ad1f37
+dfltcc.o: $(SRCDIR)contrib/s390/dfltcc.c $(SRCDIR)zlib.h zconf.h
ad1f37
+	$(CC) $(CFLAGS) $(ZINC) -c -o $@ $(SRCDIR)contrib/s390/dfltcc.c
ad1f37
+
ad1f37
+dfltcc.lo: $(SRCDIR)contrib/s390/dfltcc.c $(SRCDIR)zlib.h zconf.h
ad1f37
+	-@mkdir objs 2>/dev/null || test -d objs
ad1f37
+	$(CC) $(SFLAGS) $(ZINC) -DPIC -c -o objs/dfltcc.o $(SRCDIR)contrib/s390/dfltcc.c
ad1f37
+	-@mv objs/dfltcc.o $@
ad1f37
+
ad1f37
 example.o: $(SRCDIR)test/example.c $(SRCDIR)zlib.h zconf.h
ad1f37
 	$(CC) $(CFLAGS) $(ZINCOUT) -c -o $@ $(SRCDIR)test/example.c
ad1f37
 
ad1f37
diff --git a/configure b/configure
ad1f37
index e974d1f..8fab355 100755
ad1f37
--- a/configure
ad1f37
+++ b/configure
ad1f37
@@ -826,6 +826,19 @@ EOF
ad1f37
   fi
ad1f37
 fi
ad1f37
 
ad1f37
+# Check whether sys/sdt.h is available
ad1f37
+cat > $test.c << EOF
ad1f37
+#include <sys/sdt.h>
ad1f37
+int main() { return 0; }
ad1f37
+EOF
ad1f37
+if try ${CC} ${CFLAGS} $test.c; then
ad1f37
+    echo "Checking for sys/sdt.h ... Yes." | tee -a configure.log
ad1f37
+    CFLAGS="$CFLAGS -DHAVE_SYS_SDT_H"
ad1f37
+    SFLAGS="$SFLAGS -DHAVE_SYS_SDT_H"
ad1f37
+else
ad1f37
+    echo "Checking for sys/sdt.h ... No." | tee -a configure.log
ad1f37
+fi
ad1f37
+
ad1f37
 # show the results in the log
ad1f37
 echo >> configure.log
ad1f37
 echo ALL = $ALL >> configure.log
ad1f37
diff --git a/contrib/README.contrib b/contrib/README.contrib
ad1f37
index a411d5c..b4d3b18 100644
ad1f37
--- a/contrib/README.contrib
ad1f37
+++ b/contrib/README.contrib
ad1f37
@@ -67,6 +67,10 @@ puff/       by Mark Adler <madler@alumni.caltech.edu>
ad1f37
         Small, low memory usage inflate.  Also serves to provide an
ad1f37
         unambiguous description of the deflate format.
ad1f37
 
ad1f37
+s390/       by Ilya Leoshkevich <iii@linux.ibm.com>
ad1f37
+        Hardware-accelerated deflate on IBM Z with DEFLATE CONVERSION CALL
ad1f37
+        instruction.
ad1f37
+
ad1f37
 testzlib/   by Gilles Vollant <info@winimage.com>
ad1f37
         Example of the use of zlib
ad1f37
 
ad1f37
diff --git a/contrib/s390/dfltcc.c b/contrib/s390/dfltcc.c
ad1f37
new file mode 100644
ad1f37
index 0000000..d187796
ad1f37
--- /dev/null
ad1f37
+++ b/contrib/s390/dfltcc.c
ad1f37
@@ -0,0 +1,901 @@
ad1f37
+/* dfltcc.c - SystemZ DEFLATE CONVERSION CALL support. */
ad1f37
+
ad1f37
+/*
ad1f37
+   Use the following commands to build zlib with DFLTCC support:
ad1f37
+        $ CFLAGS=-DDFLTCC ./configure
ad1f37
+        $ make OBJA=dfltcc.o PIC_OBJA=dfltcc.lo
ad1f37
+*/
ad1f37
+
ad1f37
+#define _GNU_SOURCE
ad1f37
+#include <ctype.h>
ad1f37
+#include <inttypes.h>
ad1f37
+#include <stddef.h>
ad1f37
+#include <stdio.h>
ad1f37
+#include <stdint.h>
ad1f37
+#include <stdlib.h>
ad1f37
+#include "../../zutil.h"
ad1f37
+#include "../../deflate.h"
ad1f37
+#include "../../inftrees.h"
ad1f37
+#include "../../inflate.h"
ad1f37
+#include "dfltcc.h"
ad1f37
+#include "dfltcc_deflate.h"
ad1f37
+#ifdef HAVE_SYS_SDT_H
ad1f37
+#include <sys/sdt.h>
ad1f37
+#endif
ad1f37
+
ad1f37
+/*
ad1f37
+   C wrapper for the DEFLATE CONVERSION CALL instruction.
ad1f37
+ */
ad1f37
+typedef enum {
ad1f37
+    DFLTCC_CC_OK = 0,
ad1f37
+    DFLTCC_CC_OP1_TOO_SHORT = 1,
ad1f37
+    DFLTCC_CC_OP2_TOO_SHORT = 2,
ad1f37
+    DFLTCC_CC_OP2_CORRUPT = 2,
ad1f37
+    DFLTCC_CC_AGAIN = 3,
ad1f37
+} dfltcc_cc;
ad1f37
+
ad1f37
+#define DFLTCC_QAF 0
ad1f37
+#define DFLTCC_GDHT 1
ad1f37
+#define DFLTCC_CMPR 2
ad1f37
+#define DFLTCC_XPND 4
ad1f37
+#define HBT_CIRCULAR (1 << 7)
ad1f37
+#define HB_BITS 15
ad1f37
+#define HB_SIZE (1 << HB_BITS)
ad1f37
+#define DFLTCC_FACILITY 151
ad1f37
+
ad1f37
+local inline dfltcc_cc dfltcc OF((int fn, void *param,
ad1f37
+                                  Bytef **op1, size_t *len1,
ad1f37
+                                  z_const Bytef **op2, size_t *len2,
ad1f37
+                                  void *hist));
ad1f37
+local inline dfltcc_cc dfltcc(fn, param, op1, len1, op2, len2, hist)
ad1f37
+    int fn;
ad1f37
+    void *param;
ad1f37
+    Bytef **op1;
ad1f37
+    size_t *len1;
ad1f37
+    z_const Bytef **op2;
ad1f37
+    size_t *len2;
ad1f37
+    void *hist;
ad1f37
+{
ad1f37
+    Bytef *t2 = op1 ? *op1 : NULL;
ad1f37
+    size_t t3 = len1 ? *len1 : 0;
ad1f37
+    z_const Bytef *t4 = op2 ? *op2 : NULL;
ad1f37
+    size_t t5 = len2 ? *len2 : 0;
ad1f37
+    register int r0 __asm__("r0") = fn;
ad1f37
+    register void *r1 __asm__("r1") = param;
ad1f37
+    register Bytef *r2 __asm__("r2") = t2;
ad1f37
+    register size_t r3 __asm__("r3") = t3;
ad1f37
+    register z_const Bytef *r4 __asm__("r4") = t4;
ad1f37
+    register size_t r5 __asm__("r5") = t5;
ad1f37
+    int cc;
ad1f37
+
ad1f37
+    __asm__ volatile(
ad1f37
+#ifdef HAVE_SYS_SDT_H
ad1f37
+                     STAP_PROBE_ASM(zlib, dfltcc_entry,
ad1f37
+                                    STAP_PROBE_ASM_TEMPLATE(5))
ad1f37
+#endif
ad1f37
+                     ".insn rrf,0xb9390000,%[r2],%[r4],%[hist],0\n"
ad1f37
+#ifdef HAVE_SYS_SDT_H
ad1f37
+                     STAP_PROBE_ASM(zlib, dfltcc_exit,
ad1f37
+                                    STAP_PROBE_ASM_TEMPLATE(5))
ad1f37
+#endif
ad1f37
+                     "ipm %[cc]\n"
ad1f37
+                     : [r2] "+r" (r2)
ad1f37
+                     , [r3] "+r" (r3)
ad1f37
+                     , [r4] "+r" (r4)
ad1f37
+                     , [r5] "+r" (r5)
ad1f37
+                     , [cc] "=r" (cc)
ad1f37
+                     : [r0] "r" (r0)
ad1f37
+                     , [r1] "r" (r1)
ad1f37
+                     , [hist] "r" (hist)
ad1f37
+#ifdef HAVE_SYS_SDT_H
ad1f37
+                     , STAP_PROBE_ASM_OPERANDS(5, r2, r3, r4, r5, hist)
ad1f37
+#endif
ad1f37
+                     : "cc", "memory");
ad1f37
+    t2 = r2; t3 = r3; t4 = r4; t5 = r5;
ad1f37
+
ad1f37
+    if (op1)
ad1f37
+        *op1 = t2;
ad1f37
+    if (len1)
ad1f37
+        *len1 = t3;
ad1f37
+    if (op2)
ad1f37
+        *op2 = t4;
ad1f37
+    if (len2)
ad1f37
+        *len2 = t5;
ad1f37
+    return (cc >> 28) & 3;
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Parameter Block for Query Available Functions.
ad1f37
+ */
ad1f37
+#define static_assert(c, msg) \
ad1f37
+        __attribute__((unused)) \
ad1f37
+        static char static_assert_failed_ ## msg[c ? 1 : -1]
ad1f37
+
ad1f37
+struct dfltcc_qaf_param {
ad1f37
+    char fns[16];
ad1f37
+    char reserved1[8];
ad1f37
+    char fmts[2];
ad1f37
+    char reserved2[6];
ad1f37
+};
ad1f37
+
ad1f37
+static_assert(sizeof(struct dfltcc_qaf_param) == 32,
ad1f37
+              sizeof_struct_dfltcc_qaf_param_is_32);
ad1f37
+
ad1f37
+local inline int is_bit_set OF((const char *bits, int n));
ad1f37
+local inline int is_bit_set(bits, n)
ad1f37
+    const char *bits;
ad1f37
+    int n;
ad1f37
+{
ad1f37
+    return bits[n / 8] & (1 << (7 - (n % 8)));
ad1f37
+}
ad1f37
+
ad1f37
+local inline void clear_bit OF((char *bits, int n));
ad1f37
+local inline void clear_bit(bits, n)
ad1f37
+    char *bits;
ad1f37
+    int n;
ad1f37
+{
ad1f37
+    bits[n / 8] &= ~(1 << (7 - (n % 8)));
ad1f37
+}
ad1f37
+
ad1f37
+#define DFLTCC_FMT0 0
ad1f37
+
ad1f37
+/*
ad1f37
+   Parameter Block for Generate Dynamic-Huffman Table, Compress and Expand.
ad1f37
+ */
ad1f37
+#define CVT_CRC32 0
ad1f37
+#define CVT_ADLER32 1
ad1f37
+#define HTT_FIXED 0
ad1f37
+#define HTT_DYNAMIC 1
ad1f37
+
ad1f37
+struct dfltcc_param_v0 {
ad1f37
+    uint16_t pbvn;                     /* Parameter-Block-Version Number */
ad1f37
+    uint8_t mvn;                       /* Model-Version Number */
ad1f37
+    uint8_t ribm;                      /* Reserved for IBM use */
ad1f37
+    unsigned reserved32 : 31;
ad1f37
+    unsigned cf : 1;                   /* Continuation Flag */
ad1f37
+    uint8_t reserved64[8];
ad1f37
+    unsigned nt : 1;                   /* New Task */
ad1f37
+    unsigned reserved129 : 1;
ad1f37
+    unsigned cvt : 1;                  /* Check Value Type */
ad1f37
+    unsigned reserved131 : 1;
ad1f37
+    unsigned htt : 1;                  /* Huffman-Table Type */
ad1f37
+    unsigned bcf : 1;                  /* Block-Continuation Flag */
ad1f37
+    unsigned bcc : 1;                  /* Block Closing Control */
ad1f37
+    unsigned bhf : 1;                  /* Block Header Final */
ad1f37
+    unsigned reserved136 : 1;
ad1f37
+    unsigned reserved137 : 1;
ad1f37
+    unsigned dhtgc : 1;                /* DHT Generation Control */
ad1f37
+    unsigned reserved139 : 5;
ad1f37
+    unsigned reserved144 : 5;
ad1f37
+    unsigned sbb : 3;                  /* Sub-Byte Boundary */
ad1f37
+    uint8_t oesc;                      /* Operation-Ending-Supplemental Code */
ad1f37
+    unsigned reserved160 : 12;
ad1f37
+    unsigned ifs : 4;                  /* Incomplete-Function Status */
ad1f37
+    uint16_t ifl;                      /* Incomplete-Function Length */
ad1f37
+    uint8_t reserved192[8];
ad1f37
+    uint8_t reserved256[8];
ad1f37
+    uint8_t reserved320[4];
ad1f37
+    uint16_t hl;                       /* History Length */
ad1f37
+    unsigned reserved368 : 1;
ad1f37
+    uint16_t ho : 15;                  /* History Offset */
ad1f37
+    uint32_t cv;                       /* Check Value */
ad1f37
+    unsigned eobs : 15;                /* End-of-block Symbol */
ad1f37
+    unsigned reserved431: 1;
ad1f37
+    uint8_t eobl : 4;                  /* End-of-block Length */
ad1f37
+    unsigned reserved436 : 12;
ad1f37
+    unsigned reserved448 : 4;
ad1f37
+    uint16_t cdhtl : 12;               /* Compressed-Dynamic-Huffman Table
ad1f37
+                                          Length */
ad1f37
+    uint8_t reserved464[6];
ad1f37
+    uint8_t cdht[288];
ad1f37
+    uint8_t reserved[32];
ad1f37
+    uint8_t csb[1152];
ad1f37
+};
ad1f37
+
ad1f37
+static_assert(sizeof(struct dfltcc_param_v0) == 1536,
ad1f37
+              sizeof_struct_dfltcc_param_v0_is_1536);
ad1f37
+
ad1f37
+local z_const char *oesc_msg OF((char *buf, int oesc));
ad1f37
+local z_const char *oesc_msg(buf, oesc)
ad1f37
+    char *buf;
ad1f37
+    int oesc;
ad1f37
+{
ad1f37
+    if (oesc == 0x00)
ad1f37
+        return NULL; /* Successful completion */
ad1f37
+    else {
ad1f37
+        sprintf(buf, "Operation-Ending-Supplemental Code is 0x%.2X", oesc);
ad1f37
+        return buf;
ad1f37
+    }
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Extension of inflate_state and deflate_state. Must be doubleword-aligned.
ad1f37
+*/
ad1f37
+struct dfltcc_state {
ad1f37
+    struct dfltcc_param_v0 param;      /* Parameter block. */
ad1f37
+    struct dfltcc_qaf_param af;        /* Available functions. */
ad1f37
+    uLong level_mask;                  /* Levels on which to use DFLTCC */
ad1f37
+    uLong block_size;                  /* New block each X bytes */
ad1f37
+    uLong block_threshold;             /* New block after total_in > X */
ad1f37
+    uLong dht_threshold;               /* New block only if avail_in >= X */
ad1f37
+    char msg[64];                      /* Buffer for strm->msg */
ad1f37
+};
ad1f37
+
ad1f37
+#define ALIGN_UP(p, size) \
ad1f37
+        (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1))
ad1f37
+
ad1f37
+#define GET_DFLTCC_STATE(state) ((struct dfltcc_state FAR *)( \
ad1f37
+        (char FAR *)(state) + ALIGN_UP(sizeof(*state), 8)))
ad1f37
+
ad1f37
+/*
ad1f37
+   Compress.
ad1f37
+ */
ad1f37
+local inline int dfltcc_are_params_ok(int level,
ad1f37
+                                      uInt window_bits,
ad1f37
+                                      int strategy,
ad1f37
+                                      uLong level_mask);
ad1f37
+local inline int dfltcc_are_params_ok(level, window_bits, strategy, level_mask)
ad1f37
+    int level;
ad1f37
+    uInt window_bits;
ad1f37
+    int strategy;
ad1f37
+    uLong level_mask;
ad1f37
+{
ad1f37
+    return (level_mask & (1 << level)) != 0 &&
ad1f37
+        (window_bits == HB_BITS) &&
ad1f37
+        (strategy == Z_FIXED || strategy == Z_DEFAULT_STRATEGY);
ad1f37
+}
ad1f37
+
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_can_deflate(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+
ad1f37
+    /* Unsupported compression settings */
ad1f37
+    if (!dfltcc_are_params_ok(state->level, state->w_bits, state->strategy,
ad1f37
+                              dfltcc_state->level_mask))
ad1f37
+        return 0;
ad1f37
+
ad1f37
+    /* Unsupported hardware */
ad1f37
+    if (!is_bit_set(dfltcc_state->af.fns, DFLTCC_GDHT) ||
ad1f37
+            !is_bit_set(dfltcc_state->af.fns, DFLTCC_CMPR) ||
ad1f37
+            !is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0))
ad1f37
+        return 0;
ad1f37
+
ad1f37
+    return 1;
ad1f37
+}
ad1f37
+
ad1f37
+local void dfltcc_gdht OF((z_streamp strm));
ad1f37
+local void dfltcc_gdht(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_param_v0 FAR *param = &GET_DFLTCC_STATE(state)->param;
ad1f37
+    size_t avail_in = avail_in = strm->avail_in;
ad1f37
+
ad1f37
+    dfltcc(DFLTCC_GDHT,
ad1f37
+           param, NULL, NULL,
ad1f37
+           &strm->next_in, &avail_in, NULL);
ad1f37
+}
ad1f37
+
ad1f37
+local dfltcc_cc dfltcc_cmpr OF((z_streamp strm));
ad1f37
+local dfltcc_cc dfltcc_cmpr(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_param_v0 FAR *param = &GET_DFLTCC_STATE(state)->param;
ad1f37
+    size_t avail_in = strm->avail_in;
ad1f37
+    size_t avail_out = strm->avail_out;
ad1f37
+    dfltcc_cc cc;
ad1f37
+
ad1f37
+    cc = dfltcc(DFLTCC_CMPR | HBT_CIRCULAR,
ad1f37
+                param, &strm->next_out, &avail_out,
ad1f37
+                &strm->next_in, &avail_in, state->window);
ad1f37
+    strm->total_in += (strm->avail_in - avail_in);
ad1f37
+    strm->total_out += (strm->avail_out - avail_out);
ad1f37
+    strm->avail_in = avail_in;
ad1f37
+    strm->avail_out = avail_out;
ad1f37
+    return cc;
ad1f37
+}
ad1f37
+
ad1f37
+local void send_eobs OF((z_streamp strm,
ad1f37
+                         z_const struct dfltcc_param_v0 FAR *param));
ad1f37
+local void send_eobs(strm, param)
ad1f37
+    z_streamp strm;
ad1f37
+    z_const struct dfltcc_param_v0 FAR *param;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+
ad1f37
+    _tr_send_bits(
ad1f37
+          state,
ad1f37
+          bi_reverse(param->eobs >> (15 - param->eobl), param->eobl),
ad1f37
+          param->eobl);
ad1f37
+    flush_pending(strm);
ad1f37
+    if (state->pending != 0) {
ad1f37
+        /* The remaining data is located in pending_out[0:pending]. If someone
ad1f37
+         * calls put_byte() - this might happen in deflate() - the byte will be
ad1f37
+         * placed into pending_buf[pending], which is incorrect. Move the
ad1f37
+         * remaining data to the beginning of pending_buf so that put_byte() is
ad1f37
+         * usable again.
ad1f37
+         */
ad1f37
+        memmove(state->pending_buf, state->pending_out, state->pending);
ad1f37
+        state->pending_out = state->pending_buf;
ad1f37
+    }
ad1f37
+#ifdef ZLIB_DEBUG
ad1f37
+    state->compressed_len += param->eobl;
ad1f37
+#endif
ad1f37
+}
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate(strm, flush, result)
ad1f37
+    z_streamp strm;
ad1f37
+    int flush;
ad1f37
+    block_state *result;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
+    uInt masked_avail_in;
ad1f37
+    dfltcc_cc cc;
ad1f37
+    int need_empty_block;
ad1f37
+    int soft_bcc;
ad1f37
+    int no_flush;
ad1f37
+
ad1f37
+    if (!dfltcc_can_deflate(strm))
ad1f37
+        return 0;
ad1f37
+
ad1f37
+again:
ad1f37
+    masked_avail_in = 0;
ad1f37
+    soft_bcc = 0;
ad1f37
+    no_flush = flush == Z_NO_FLUSH;
ad1f37
+
ad1f37
+    /* Trailing empty block. Switch to software, except when Continuation Flag
ad1f37
+     * is set, which means that DFLTCC has buffered some output in the
ad1f37
+     * parameter block and needs to be called again in order to flush it.
ad1f37
+     */
ad1f37
+    if (flush == Z_FINISH && strm->avail_in == 0 && !param->cf) {
ad1f37
+        if (param->bcf) {
ad1f37
+            /* A block is still open, and the hardware does not support closing
ad1f37
+             * blocks without adding data. Thus, close it manually.
ad1f37
+             */
ad1f37
+            send_eobs(strm, param);
ad1f37
+            param->bcf = 0;
ad1f37
+        }
ad1f37
+        return 0;
ad1f37
+    }
ad1f37
+
ad1f37
+    if (strm->avail_in == 0 && !param->cf) {
ad1f37
+        *result = need_more;
ad1f37
+        return 1;
ad1f37
+    }
ad1f37
+
ad1f37
+    /* There is an open non-BFINAL block, we are not going to close it just
ad1f37
+     * yet, we have compressed more than DFLTCC_BLOCK_SIZE bytes and we see
ad1f37
+     * more than DFLTCC_DHT_MIN_SAMPLE_SIZE bytes. Open a new block with a new
ad1f37
+     * DHT in order to adapt to a possibly changed input data distribution.
ad1f37
+     */
ad1f37
+    if (param->bcf && no_flush &&
ad1f37
+            strm->total_in > dfltcc_state->block_threshold &&
ad1f37
+            strm->avail_in >= dfltcc_state->dht_threshold) {
ad1f37
+        if (param->cf) {
ad1f37
+            /* We need to flush the DFLTCC buffer before writing the
ad1f37
+             * End-of-block Symbol. Mask the input data and proceed as usual.
ad1f37
+             */
ad1f37
+            masked_avail_in += strm->avail_in;
ad1f37
+            strm->avail_in = 0;
ad1f37
+            no_flush = 0;
ad1f37
+        } else {
ad1f37
+            /* DFLTCC buffer is empty, so we can manually write the
ad1f37
+             * End-of-block Symbol right away.
ad1f37
+             */
ad1f37
+            send_eobs(strm, param);
ad1f37
+            param->bcf = 0;
ad1f37
+            dfltcc_state->block_threshold =
ad1f37
+                strm->total_in + dfltcc_state->block_size;
ad1f37
+            if (strm->avail_out == 0) {
ad1f37
+                *result = need_more;
ad1f37
+                return 1;
ad1f37
+            }
ad1f37
+        }
ad1f37
+    }
ad1f37
+
ad1f37
+    /* The caller gave us too much data. Pass only one block worth of
ad1f37
+     * uncompressed data to DFLTCC and mask the rest, so that on the next
ad1f37
+     * iteration we start a new block.
ad1f37
+     */
ad1f37
+    if (no_flush && strm->avail_in > dfltcc_state->block_size) {
ad1f37
+        masked_avail_in += (strm->avail_in - dfltcc_state->block_size);
ad1f37
+        strm->avail_in = dfltcc_state->block_size;
ad1f37
+    }
ad1f37
+
ad1f37
+    /* When we have an open non-BFINAL deflate block and caller indicates that
ad1f37
+     * the stream is ending, we need to close an open deflate block and open a
ad1f37
+     * BFINAL one.
ad1f37
+     */
ad1f37
+    need_empty_block = flush == Z_FINISH && param->bcf && !param->bhf;
ad1f37
+
ad1f37
+    /* Translate stream to parameter block */
ad1f37
+    param->cvt = state->wrap == 2 ? CVT_CRC32 : CVT_ADLER32;
ad1f37
+    if (!no_flush)
ad1f37
+        /* We need to close a block. Always do this in software - when there is
ad1f37
+         * no input data, the hardware will not nohor BCC. */
ad1f37
+        soft_bcc = 1;
ad1f37
+    if (flush == Z_FINISH && !param->bcf)
ad1f37
+        /* We are about to open a BFINAL block, set Block Header Final bit
ad1f37
+         * until the stream ends.
ad1f37
+         */
ad1f37
+        param->bhf = 1;
ad1f37
+    /* DFLTCC-CMPR will write to next_out, so make sure that buffers with
ad1f37
+     * higher precedence are empty.
ad1f37
+     */
ad1f37
+    Assert(state->pending == 0, "There must be no pending bytes");
ad1f37
+    Assert(state->bi_valid < 8, "There must be less than 8 pending bits");
ad1f37
+    param->sbb = (unsigned int)state->bi_valid;
ad1f37
+    if (param->sbb > 0)
ad1f37
+        *strm->next_out = (Bytef)state->bi_buf;
ad1f37
+    if (param->hl)
ad1f37
+        param->nt = 0; /* Honor history */
ad1f37
+    param->cv = state->wrap == 2 ? ZSWAP32(strm->adler) : strm->adler;
ad1f37
+
ad1f37
+    /* When opening a block, choose a Huffman-Table Type */
ad1f37
+    if (!param->bcf) {
ad1f37
+        if (state->strategy == Z_FIXED ||
ad1f37
+                (strm->total_in == 0 && dfltcc_state->block_threshold > 0))
ad1f37
+            param->htt = HTT_FIXED;
ad1f37
+        else {
ad1f37
+            param->htt = HTT_DYNAMIC;
ad1f37
+            dfltcc_gdht(strm);
ad1f37
+        }
ad1f37
+    }
ad1f37
+
ad1f37
+    /* Deflate */
ad1f37
+    do {
ad1f37
+        cc = dfltcc_cmpr(strm);
ad1f37
+        if (strm->avail_in < 4096 && masked_avail_in > 0)
ad1f37
+            /* We are about to call DFLTCC with a small input buffer, which is
ad1f37
+             * inefficient. Since there is masked data, there will be at least
ad1f37
+             * one more DFLTCC call, so skip the current one and make the next
ad1f37
+             * one handle more data.
ad1f37
+             */
ad1f37
+            break;
ad1f37
+    } while (cc == DFLTCC_CC_AGAIN);
ad1f37
+
ad1f37
+    /* Translate parameter block to stream */
ad1f37
+    strm->msg = oesc_msg(dfltcc_state->msg, param->oesc);
ad1f37
+    state->bi_valid = param->sbb;
ad1f37
+    if (state->bi_valid == 0)
ad1f37
+        state->bi_buf = 0; /* Avoid accessing next_out */
ad1f37
+    else
ad1f37
+        state->bi_buf = *strm->next_out & ((1 << state->bi_valid) - 1);
ad1f37
+    strm->adler = state->wrap == 2 ? ZSWAP32(param->cv) : param->cv;
ad1f37
+
ad1f37
+    /* Unmask the input data */
ad1f37
+    strm->avail_in += masked_avail_in;
ad1f37
+    masked_avail_in = 0;
ad1f37
+
ad1f37
+    /* If we encounter an error, it means there is a bug in DFLTCC call */
ad1f37
+    Assert(cc != DFLTCC_CC_OP2_CORRUPT || param->oesc == 0, "BUG");
ad1f37
+
ad1f37
+    /* Update Block-Continuation Flag. It will be used to check whether to call
ad1f37
+     * GDHT the next time.
ad1f37
+     */
ad1f37
+    if (cc == DFLTCC_CC_OK) {
ad1f37
+        if (soft_bcc) {
ad1f37
+            send_eobs(strm, param);
ad1f37
+            param->bcf = 0;
ad1f37
+            dfltcc_state->block_threshold =
ad1f37
+                strm->total_in + dfltcc_state->block_size;
ad1f37
+        } else
ad1f37
+            param->bcf = 1;
ad1f37
+        if (flush == Z_FINISH) {
ad1f37
+            if (need_empty_block)
ad1f37
+                /* Make the current deflate() call also close the stream */
ad1f37
+                return 0;
ad1f37
+            else {
ad1f37
+                bi_windup(state);
ad1f37
+                *result = finish_done;
ad1f37
+            }
ad1f37
+        } else {
ad1f37
+            if (flush == Z_FULL_FLUSH)
ad1f37
+                param->hl = 0; /* Clear history */
ad1f37
+            *result = flush == Z_NO_FLUSH ? need_more : block_done;
ad1f37
+        }
ad1f37
+    } else {
ad1f37
+        param->bcf = 1;
ad1f37
+        *result = need_more;
ad1f37
+    }
ad1f37
+    if (strm->avail_in != 0 && strm->avail_out != 0)
ad1f37
+        goto again; /* deflate() must use all input or all output */
ad1f37
+    return 1;
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Expand.
ad1f37
+ */
ad1f37
+int ZLIB_INTERNAL dfltcc_can_inflate(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+
ad1f37
+    /* Unsupported compression settings */
ad1f37
+    if (state->wbits != HB_BITS)
ad1f37
+        return 0;
ad1f37
+
ad1f37
+    /* Unsupported hardware */
ad1f37
+    return is_bit_set(dfltcc_state->af.fns, DFLTCC_XPND) &&
ad1f37
+               is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0);
ad1f37
+}
ad1f37
+
ad1f37
+local dfltcc_cc dfltcc_xpnd OF((z_streamp strm));
ad1f37
+local dfltcc_cc dfltcc_xpnd(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_param_v0 FAR *param = &GET_DFLTCC_STATE(state)->param;
ad1f37
+    size_t avail_in = strm->avail_in;
ad1f37
+    size_t avail_out = strm->avail_out;
ad1f37
+    dfltcc_cc cc;
ad1f37
+
ad1f37
+    cc = dfltcc(DFLTCC_XPND | HBT_CIRCULAR,
ad1f37
+                param, &strm->next_out, &avail_out,
ad1f37
+                &strm->next_in, &avail_in, state->window);
ad1f37
+    strm->avail_in = avail_in;
ad1f37
+    strm->avail_out = avail_out;
ad1f37
+    return cc;
ad1f37
+}
ad1f37
+
ad1f37
+dfltcc_inflate_action ZLIB_INTERNAL dfltcc_inflate(strm, flush, ret)
ad1f37
+    z_streamp strm;
ad1f37
+    int flush;
ad1f37
+    int *ret;
ad1f37
+{
ad1f37
+    struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
+    dfltcc_cc cc;
ad1f37
+
ad1f37
+    if (flush == Z_BLOCK || flush == Z_TREES) {
ad1f37
+        /* DFLTCC does not support stopping on block boundaries */
ad1f37
+        if (dfltcc_inflate_disable(strm)) {
ad1f37
+            *ret = Z_STREAM_ERROR;
ad1f37
+            return DFLTCC_INFLATE_BREAK;
ad1f37
+        } else
ad1f37
+            return DFLTCC_INFLATE_SOFTWARE;
ad1f37
+    }
ad1f37
+
ad1f37
+    if (state->last) {
ad1f37
+        if (state->bits != 0) {
ad1f37
+            strm->next_in++;
ad1f37
+            strm->avail_in--;
ad1f37
+            state->bits = 0;
ad1f37
+        }
ad1f37
+        state->mode = CHECK;
ad1f37
+        return DFLTCC_INFLATE_CONTINUE;
ad1f37
+    }
ad1f37
+
ad1f37
+    if (strm->avail_in == 0 && !param->cf)
ad1f37
+        return DFLTCC_INFLATE_BREAK;
ad1f37
+
ad1f37
+    if (inflate_ensure_window(state)) {
ad1f37
+        state->mode = MEM;
ad1f37
+        return DFLTCC_INFLATE_CONTINUE;
ad1f37
+    }
ad1f37
+
ad1f37
+    /* Translate stream to parameter block */
ad1f37
+    param->cvt = state->flags ? CVT_CRC32 : CVT_ADLER32;
ad1f37
+    param->sbb = state->bits;
ad1f37
+    param->hl = state->whave; /* Software and hardware history formats match */
ad1f37
+    param->ho = (state->wnext - state->whave) & ((1 << HB_BITS) - 1);
ad1f37
+    if (param->hl)
ad1f37
+        param->nt = 0; /* Honor history for the first block */
ad1f37
+    param->cv = state->flags ? ZSWAP32(state->check) : state->check;
ad1f37
+
ad1f37
+    /* Inflate */
ad1f37
+    do {
ad1f37
+        cc = dfltcc_xpnd(strm);
ad1f37
+    } while (cc == DFLTCC_CC_AGAIN);
ad1f37
+
ad1f37
+    /* Translate parameter block to stream */
ad1f37
+    strm->msg = oesc_msg(dfltcc_state->msg, param->oesc);
ad1f37
+    state->last = cc == DFLTCC_CC_OK;
ad1f37
+    state->bits = param->sbb;
ad1f37
+    state->whave = param->hl;
ad1f37
+    state->wnext = (param->ho + param->hl) & ((1 << HB_BITS) - 1);
ad1f37
+    state->check = state->flags ? ZSWAP32(param->cv) : param->cv;
ad1f37
+    if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0) {
ad1f37
+        /* Report an error if stream is corrupted */
ad1f37
+        state->mode = BAD;
ad1f37
+        return DFLTCC_INFLATE_CONTINUE;
ad1f37
+    }
ad1f37
+    state->mode = TYPEDO;
ad1f37
+    /* Break if operands are exhausted, otherwise continue looping */
ad1f37
+    return (cc == DFLTCC_CC_OP1_TOO_SHORT || cc == DFLTCC_CC_OP2_TOO_SHORT) ?
ad1f37
+        DFLTCC_INFLATE_BREAK : DFLTCC_INFLATE_CONTINUE;
ad1f37
+}
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_was_inflate_used(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_param_v0 FAR *param = &GET_DFLTCC_STATE(state)->param;
ad1f37
+
ad1f37
+    return !param->nt;
ad1f37
+}
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_inflate_disable(strm)
ad1f37
+    z_streamp strm;
ad1f37
+{
ad1f37
+    struct inflate_state FAR *state = (struct inflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+
ad1f37
+    if (!dfltcc_can_inflate(strm))
ad1f37
+        return 0;
ad1f37
+    if (dfltcc_was_inflate_used(strm))
ad1f37
+        /* DFLTCC has already decompressed some data. Since there is not
ad1f37
+         * enough information to resume decompression in software, the call
ad1f37
+         * must fail.
ad1f37
+         */
ad1f37
+        return 1;
ad1f37
+    /* DFLTCC was not used yet - decompress in software */
ad1f37
+    memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
ad1f37
+    return 0;
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Memory management.
ad1f37
+   DFLTCC requires parameter blocks and window to be aligned. zlib allows
ad1f37
+   users to specify their own allocation functions, so using e.g.
ad1f37
+   `posix_memalign' is not an option. Thus, we overallocate and take the
ad1f37
+   aligned portion of the buffer.
ad1f37
+*/
ad1f37
+local inline int is_dfltcc_enabled OF((void));
ad1f37
+local inline int is_dfltcc_enabled(void)
ad1f37
+{
ad1f37
+    const char *env;
ad1f37
+    uint64_t facilities[(DFLTCC_FACILITY / 64) + 1];
ad1f37
+    register char r0 __asm__("r0");
ad1f37
+
ad1f37
+    env = secure_getenv("DFLTCC");
ad1f37
+    if (env && !strcmp(env, "0"))
ad1f37
+      /* User has explicitly disabled DFLTCC. */
ad1f37
+      return 0;
ad1f37
+
ad1f37
+    memset(facilities, 0, sizeof(facilities));
ad1f37
+    r0 = sizeof(facilities) / sizeof(facilities[0]) - 1;
ad1f37
+    /* STFLE is supported since z9-109 and only in z/Architecture mode. When
ad1f37
+     * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
ad1f37
+     * is 64-bit, it's always z/Architecture mode at runtime.
ad1f37
+     */
ad1f37
+    __asm__ volatile(".machinemode push\n"
ad1f37
+                     ".machinemode zarch\n"
ad1f37
+                     "stfle %[facilities]\n"
ad1f37
+                     ".machinemode pop\n"
ad1f37
+                     : [facilities] "=Q" (facilities)
ad1f37
+                     , [r0] "+r" (r0)
ad1f37
+                     :
ad1f37
+                     : "cc");
ad1f37
+    return is_bit_set((const char *)facilities, DFLTCC_FACILITY);
ad1f37
+}
ad1f37
+
ad1f37
+void ZLIB_INTERNAL dfltcc_reset(strm, size)
ad1f37
+    z_streamp strm;
ad1f37
+    uInt size;
ad1f37
+{
ad1f37
+    struct dfltcc_state *dfltcc_state =
ad1f37
+        (struct dfltcc_state *)((char FAR *)strm->state + ALIGN_UP(size, 8));
ad1f37
+    struct dfltcc_qaf_param *param =
ad1f37
+        (struct dfltcc_qaf_param *)&dfltcc_state->param;
ad1f37
+    const char *s;
ad1f37
+
ad1f37
+    /* Initialize available functions */
ad1f37
+    if (is_dfltcc_enabled()) {
ad1f37
+        dfltcc(DFLTCC_QAF, param, NULL, NULL, NULL, NULL, NULL);
ad1f37
+        memmove(&dfltcc_state->af, param, sizeof(dfltcc_state->af));
ad1f37
+    } else
ad1f37
+        memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
ad1f37
+
ad1f37
+    if (secure_getenv("SOURCE_DATE_EPOCH"))
ad1f37
+        /* User needs reproducible results, but the output of DFLTCC_CMPR
ad1f37
+         * depends on buffers' page offsets.
ad1f37
+         */
ad1f37
+        clear_bit(dfltcc_state->af.fns, DFLTCC_CMPR);
ad1f37
+
ad1f37
+    /* Initialize parameter block */
ad1f37
+    memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param));
ad1f37
+    dfltcc_state->param.nt = 1;
ad1f37
+
ad1f37
+    /* Initialize tuning parameters */
ad1f37
+#ifndef DFLTCC_LEVEL_MASK
ad1f37
+#define DFLTCC_LEVEL_MASK 0x2
ad1f37
+#endif
ad1f37
+    s = secure_getenv("DFLTCC_LEVEL_MASK");
ad1f37
+    dfltcc_state->level_mask = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
+                                           DFLTCC_LEVEL_MASK;
ad1f37
+#ifndef DFLTCC_BLOCK_SIZE
ad1f37
+#define DFLTCC_BLOCK_SIZE 1048576
ad1f37
+#endif
ad1f37
+    s = secure_getenv("DFLTCC_BLOCK_SIZE");
ad1f37
+    dfltcc_state->block_size = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
+                                           DFLTCC_BLOCK_SIZE;
ad1f37
+#ifndef DFLTCC_FIRST_FHT_BLOCK_SIZE
ad1f37
+#define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
ad1f37
+#endif
ad1f37
+    s = secure_getenv("DFLTCC_FIRST_FHT_BLOCK_SIZE");
ad1f37
+    dfltcc_state->block_threshold = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
+                                                DFLTCC_FIRST_FHT_BLOCK_SIZE;
ad1f37
+#ifndef DFLTCC_DHT_MIN_SAMPLE_SIZE
ad1f37
+#define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
ad1f37
+#endif
ad1f37
+    s = secure_getenv("DFLTCC_DHT_MIN_SAMPLE_SIZE");
ad1f37
+    dfltcc_state->dht_threshold = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
+                                              DFLTCC_DHT_MIN_SAMPLE_SIZE;
ad1f37
+#ifndef DFLTCC_RIBM
ad1f37
+#define DFLTCC_RIBM 0
ad1f37
+#endif
ad1f37
+    s = secure_getenv("DFLTCC_RIBM");
ad1f37
+    dfltcc_state->param.ribm = (s && *s) ? strtoul(s, NULL, 0) :
ad1f37
+                                           DFLTCC_RIBM;
ad1f37
+}
ad1f37
+
ad1f37
+voidpf ZLIB_INTERNAL dfltcc_alloc_state(strm, items, size)
ad1f37
+    z_streamp strm;
ad1f37
+    uInt items;
ad1f37
+    uInt size;
ad1f37
+{
ad1f37
+    return ZALLOC(strm,
ad1f37
+                  ALIGN_UP(items * size, 8) + sizeof(struct dfltcc_state),
ad1f37
+                  sizeof(unsigned char));
ad1f37
+}
ad1f37
+
ad1f37
+void ZLIB_INTERNAL dfltcc_copy_state(dst, src, size)
ad1f37
+    voidpf dst;
ad1f37
+    const voidpf src;
ad1f37
+    uInt size;
ad1f37
+{
ad1f37
+    zmemcpy(dst, src, ALIGN_UP(size, 8) + sizeof(struct dfltcc_state));
ad1f37
+}
ad1f37
+
ad1f37
+static const int PAGE_ALIGN = 0x1000;
ad1f37
+
ad1f37
+voidpf ZLIB_INTERNAL dfltcc_alloc_window(strm, items, size)
ad1f37
+    z_streamp strm;
ad1f37
+    uInt items;
ad1f37
+    uInt size;
ad1f37
+{
ad1f37
+    voidpf p, w;
ad1f37
+
ad1f37
+    /* To simplify freeing, we store the pointer to the allocated buffer right
ad1f37
+     * before the window.
ad1f37
+     */
ad1f37
+    p = ZALLOC(strm, sizeof(voidpf) + items * size + PAGE_ALIGN,
ad1f37
+               sizeof(unsigned char));
ad1f37
+    if (p == NULL)
ad1f37
+        return NULL;
ad1f37
+    w = ALIGN_UP((char FAR *)p + sizeof(voidpf), PAGE_ALIGN);
ad1f37
+    *(voidpf *)((char FAR *)w - sizeof(voidpf)) = p;
ad1f37
+    return w;
ad1f37
+}
ad1f37
+
ad1f37
+void ZLIB_INTERNAL dfltcc_free_window(strm, w)
ad1f37
+    z_streamp strm;
ad1f37
+    voidpf w;
ad1f37
+{
ad1f37
+    if (w)
ad1f37
+        ZFREE(strm, *(voidpf *)((unsigned char FAR *)w - sizeof(voidpf)));
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Switching between hardware and software compression.
ad1f37
+   DFLTCC does not support all zlib settings, e.g. generation of non-compressed
ad1f37
+   blocks or alternative window sizes. When such settings are applied on the
ad1f37
+   fly with deflateParams, we need to convert between hardware and software
ad1f37
+   window formats.
ad1f37
+*/
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_params(strm, level, strategy)
ad1f37
+    z_streamp strm;
ad1f37
+    int level;
ad1f37
+    int strategy;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
+    int could_deflate = dfltcc_can_deflate(strm);
ad1f37
+    int can_deflate = dfltcc_are_params_ok(level, state->w_bits, strategy,
ad1f37
+                                           dfltcc_state->level_mask);
ad1f37
+
ad1f37
+    if (can_deflate == could_deflate)
ad1f37
+        /* We continue to work in the same mode - no changes needed */
ad1f37
+        return Z_OK;
ad1f37
+
ad1f37
+    if (strm->total_in == 0 && param->nt == 1 && param->hl == 0)
ad1f37
+        /* DFLTCC was not used yet - no changes needed */
ad1f37
+        return Z_OK;
ad1f37
+
ad1f37
+    /* Switching between hardware and software is not implemented */
ad1f37
+    return Z_STREAM_ERROR;
ad1f37
+}
ad1f37
+
ad1f37
+/*
ad1f37
+   Preloading history.
ad1f37
+*/
ad1f37
+local void append_history OF((struct dfltcc_param_v0 FAR *param,
ad1f37
+                              Bytef *history,
ad1f37
+                              const Bytef *buf,
ad1f37
+                              uInt count));
ad1f37
+local void append_history(param, history, buf, count)
ad1f37
+    struct dfltcc_param_v0 FAR *param;
ad1f37
+    Bytef *history;
ad1f37
+    const Bytef *buf;
ad1f37
+    uInt count;
ad1f37
+{
ad1f37
+    size_t offset;
ad1f37
+    size_t n;
ad1f37
+
ad1f37
+    /* Do not use more than 32K */
ad1f37
+    if (count > HB_SIZE) {
ad1f37
+        buf += count - HB_SIZE;
ad1f37
+        count = HB_SIZE;
ad1f37
+    }
ad1f37
+    offset = (param->ho + param->hl) % HB_SIZE;
ad1f37
+    if (offset + count <= HB_SIZE)
ad1f37
+        /* Circular history buffer does not wrap - copy one chunk */
ad1f37
+        zmemcpy(history + offset, buf, count);
ad1f37
+    else {
ad1f37
+        /* Circular history buffer wraps - copy two chunks */
ad1f37
+        n = HB_SIZE - offset;
ad1f37
+        zmemcpy(history + offset, buf, n);
ad1f37
+        zmemcpy(history, buf + n, count - n);
ad1f37
+    }
ad1f37
+    n = param->hl + count;
ad1f37
+    if (n <= HB_SIZE)
ad1f37
+        /* All history fits into buffer - no need to discard anything */
ad1f37
+        param->hl = n;
ad1f37
+    else {
ad1f37
+        /* History does not fit into buffer - discard extra bytes */
ad1f37
+        param->ho = (param->ho + (n - HB_SIZE)) % HB_SIZE;
ad1f37
+        param->hl = HB_SIZE;
ad1f37
+    }
ad1f37
+}
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_set_dictionary(strm, dictionary, dict_length)
ad1f37
+    z_streamp strm;
ad1f37
+    const Bytef *dictionary;
ad1f37
+    uInt dict_length;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
+
ad1f37
+    append_history(param, state->window, dictionary, dict_length);
ad1f37
+    state->strstart = 1; /* Add FDICT to zlib header */
ad1f37
+    return Z_OK;
ad1f37
+}
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_get_dictionary(strm, dictionary, dict_length)
ad1f37
+    z_streamp strm;
ad1f37
+    Bytef *dictionary;
ad1f37
+    uInt *dict_length;
ad1f37
+{
ad1f37
+    deflate_state FAR *state = (deflate_state FAR *)strm->state;
ad1f37
+    struct dfltcc_state FAR *dfltcc_state = GET_DFLTCC_STATE(state);
ad1f37
+    struct dfltcc_param_v0 FAR *param = &dfltcc_state->param;
ad1f37
+
ad1f37
+    if (dictionary) {
ad1f37
+        if (param->ho + param->hl <= HB_SIZE)
ad1f37
+            /* Circular history buffer does not wrap - copy one chunk */
ad1f37
+            zmemcpy(dictionary, state->window + param->ho, param->hl);
ad1f37
+        else {
ad1f37
+            /* Circular history buffer wraps - copy two chunks */
ad1f37
+            zmemcpy(dictionary,
ad1f37
+                    state->window + param->ho,
ad1f37
+                    HB_SIZE - param->ho);
ad1f37
+            zmemcpy(dictionary + HB_SIZE - param->ho,
ad1f37
+                    state->window,
ad1f37
+                    param->ho + param->hl - HB_SIZE);
ad1f37
+        }
ad1f37
+    }
ad1f37
+    if (dict_length)
ad1f37
+        *dict_length = param->hl;
ad1f37
+    return Z_OK;
ad1f37
+}
ad1f37
\ No newline at end of file
ad1f37
diff --git a/contrib/s390/dfltcc.h b/contrib/s390/dfltcc.h
ad1f37
new file mode 100644
ad1f37
index 0000000..574e84c
ad1f37
--- /dev/null
ad1f37
+++ b/contrib/s390/dfltcc.h
ad1f37
@@ -0,0 +1,55 @@
ad1f37
+#ifndef DFLTCC_H
ad1f37
+#define DFLTCC_H
ad1f37
+
ad1f37
+#include "../../zlib.h"
ad1f37
+#include "../../zutil.h"
ad1f37
+
ad1f37
+voidpf ZLIB_INTERNAL dfltcc_alloc_state OF((z_streamp strm, uInt items,
ad1f37
+                                            uInt size));
ad1f37
+void ZLIB_INTERNAL dfltcc_copy_state OF((voidpf dst, const voidpf src,
ad1f37
+                                         uInt size));
ad1f37
+void ZLIB_INTERNAL dfltcc_reset OF((z_streamp strm, uInt size));
ad1f37
+voidpf ZLIB_INTERNAL dfltcc_alloc_window OF((z_streamp strm, uInt items,
ad1f37
+                                             uInt size));
ad1f37
+void ZLIB_INTERNAL dfltcc_free_window OF((z_streamp strm, voidpf w));
ad1f37
+int ZLIB_INTERNAL dfltcc_can_inflate OF((z_streamp strm));
ad1f37
+typedef enum {
ad1f37
+    DFLTCC_INFLATE_CONTINUE,
ad1f37
+    DFLTCC_INFLATE_BREAK,
ad1f37
+    DFLTCC_INFLATE_SOFTWARE,
ad1f37
+} dfltcc_inflate_action;
ad1f37
+dfltcc_inflate_action ZLIB_INTERNAL dfltcc_inflate OF((z_streamp strm,
ad1f37
+                                                       int flush, int *ret));
ad1f37
+int ZLIB_INTERNAL dfltcc_was_inflate_used OF((z_streamp strm));
ad1f37
+int ZLIB_INTERNAL dfltcc_inflate_disable OF((z_streamp strm));
ad1f37
+
ad1f37
+#define ZALLOC_STATE dfltcc_alloc_state
ad1f37
+#define ZFREE_STATE ZFREE
ad1f37
+#define ZCOPY_STATE dfltcc_copy_state
ad1f37
+#define ZALLOC_WINDOW dfltcc_alloc_window
ad1f37
+#define ZFREE_WINDOW dfltcc_free_window
ad1f37
+#define TRY_FREE_WINDOW dfltcc_free_window
ad1f37
+#define INFLATE_RESET_KEEP_HOOK(strm) \
ad1f37
+    dfltcc_reset((strm), sizeof(struct inflate_state))
ad1f37
+#define INFLATE_PRIME_HOOK(strm, bits, value) \
ad1f37
+    do { if (dfltcc_inflate_disable((strm))) return Z_STREAM_ERROR; } while (0)
ad1f37
+#define INFLATE_TYPEDO_HOOK(strm, flush) \
ad1f37
+    if (dfltcc_can_inflate((strm))) { \
ad1f37
+        dfltcc_inflate_action action; \
ad1f37
+\
ad1f37
+        RESTORE(); \
ad1f37
+        action = dfltcc_inflate((strm), (flush), &ret;; \
ad1f37
+        LOAD(); \
ad1f37
+        if (action == DFLTCC_INFLATE_CONTINUE) \
ad1f37
+            break; \
ad1f37
+        else if (action == DFLTCC_INFLATE_BREAK) \
ad1f37
+            goto inf_leave; \
ad1f37
+    }
ad1f37
+#define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm)))
ad1f37
+#define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm)))
ad1f37
+#define INFLATE_MARK_HOOK(strm) \
ad1f37
+    do { \
ad1f37
+        if (dfltcc_was_inflate_used((strm))) return -(1L << 16); \
ad1f37
+    } while (0)
ad1f37
+
ad1f37
+#endif
ad1f37
\ No newline at end of file
ad1f37
diff --git a/contrib/s390/dfltcc_deflate.h b/contrib/s390/dfltcc_deflate.h
ad1f37
new file mode 100644
ad1f37
index 0000000..a129a91
ad1f37
--- /dev/null
ad1f37
+++ b/contrib/s390/dfltcc_deflate.h
ad1f37
@@ -0,0 +1,50 @@
ad1f37
+#ifndef DFLTCC_DEFLATE_H
ad1f37
+#define DFLTCC_DEFLATE_H
ad1f37
+
ad1f37
+#include "dfltcc.h"
ad1f37
+
ad1f37
+int ZLIB_INTERNAL dfltcc_can_deflate OF((z_streamp strm));
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate OF((z_streamp strm,
ad1f37
+                                     int flush,
ad1f37
+                                     block_state *result));
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_params OF((z_streamp strm,
ad1f37
+                                            int level,
ad1f37
+                                            int strategy));
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_set_dictionary OF((z_streamp strm,
ad1f37
+                                                    const Bytef *dictionary,
ad1f37
+                                                    uInt dict_length));
ad1f37
+int ZLIB_INTERNAL dfltcc_deflate_get_dictionary OF((z_streamp strm,
ad1f37
+                                                    Bytef *dictionary,
ad1f37
+                                                    uInt* dict_length));
ad1f37
+
ad1f37
+#define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) \
ad1f37
+    do { \
ad1f37
+        if (dfltcc_can_deflate((strm))) \
ad1f37
+            return dfltcc_deflate_set_dictionary((strm), (dict), (dict_len)); \
ad1f37
+    } while (0)
ad1f37
+#define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) \
ad1f37
+    do { \
ad1f37
+        if (dfltcc_can_deflate((strm))) \
ad1f37
+            return dfltcc_deflate_get_dictionary((strm), (dict), (dict_len)); \
ad1f37
+    } while (0)
ad1f37
+#define DEFLATE_RESET_KEEP_HOOK(strm) \
ad1f37
+    dfltcc_reset((strm), sizeof(deflate_state))
ad1f37
+#define DEFLATE_PARAMS_HOOK(strm, level, strategy) \
ad1f37
+    do { \
ad1f37
+        int err; \
ad1f37
+\
ad1f37
+        err = dfltcc_deflate_params((strm), (level), (strategy)); \
ad1f37
+        if (err == Z_STREAM_ERROR) \
ad1f37
+            return err; \
ad1f37
+    } while (0)
ad1f37
+#define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, source_len) \
ad1f37
+    do { \
ad1f37
+        if (dfltcc_can_deflate((strm))) \
ad1f37
+            (complen) = (3 + 5 + 5 + 4 + 19 * 3 + (286 + 30) * 7 + \
ad1f37
+                         (source_len) * 16 + 15 + 7) >> 3; \
ad1f37
+    } while (0)
ad1f37
+#define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) (dfltcc_can_deflate((strm)))
ad1f37
+#define DEFLATE_HOOK dfltcc_deflate
ad1f37
+#define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm)))
ad1f37
+
ad1f37
+#endif
ad1f37
\ No newline at end of file
ad1f37
diff --git a/deflate.c b/deflate.c
ad1f37
index 1ec7614..089285a 100644
ad1f37
--- a/deflate.c
ad1f37
+++ b/deflate.c
ad1f37
@@ -61,15 +61,29 @@ const char deflate_copyright[] =
ad1f37
  */
ad1f37
 
ad1f37
 /* ===========================================================================
ad1f37
- *  Function prototypes.
ad1f37
+ *  Architecture-specific bits.
ad1f37
  */
ad1f37
-typedef enum {
ad1f37
-    need_more,      /* block not completed, need more input or more output */
ad1f37
-    block_done,     /* block flush performed */
ad1f37
-    finish_started, /* finish started, need only more output at next deflate */
ad1f37
-    finish_done     /* finish done, accept no more input or output */
ad1f37
-} block_state;
ad1f37
+#ifdef DFLTCC
ad1f37
+#  include "contrib/s390/dfltcc_deflate.h"
ad1f37
+#else
ad1f37
+#define ZALLOC_STATE ZALLOC
ad1f37
+#define ZFREE_STATE ZFREE
ad1f37
+#define ZCOPY_STATE zmemcpy
ad1f37
+#define ZALLOC_WINDOW ZALLOC
ad1f37
+#define TRY_FREE_WINDOW TRY_FREE
ad1f37
+#define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
ad1f37
+#define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
ad1f37
+#define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
ad1f37
+#define DEFLATE_PARAMS_HOOK(strm, level, strategy) do {} while (0)
ad1f37
+#define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
ad1f37
+#define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
ad1f37
+#define DEFLATE_HOOK(strm, flush, bstate) 0
ad1f37
+#define DEFLATE_NEED_CHECKSUM(strm) 1
ad1f37
+#endif
ad1f37
 
ad1f37
+/* ===========================================================================
ad1f37
+ *  Function prototypes.
ad1f37
+ */
ad1f37
 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
ad1f37
 /* Compression function. Returns the block state after the call. */
ad1f37
 
ad1f37
@@ -85,7 +99,6 @@ local block_state deflate_rle    OF((deflate_state *s, int flush));
ad1f37
 local block_state deflate_huff   OF((deflate_state *s, int flush));
ad1f37
 local void lm_init        OF((deflate_state *s));
ad1f37
 local void putShortMSB    OF((deflate_state *s, uInt b));
ad1f37
-local void flush_pending  OF((z_streamp strm));
ad1f37
 local unsigned read_buf   OF((z_streamp strm, Bytef *buf, unsigned size));
ad1f37
 #ifdef ASMV
ad1f37
 #  pragma message("Assembler code may have bugs -- use at your own risk")
ad1f37
@@ -301,7 +314,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
ad1f37
         return Z_STREAM_ERROR;
ad1f37
     }
ad1f37
     if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
ad1f37
-    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
ad1f37
+    s = (deflate_state *) ZALLOC_STATE(strm, 1, sizeof(deflate_state));
ad1f37
     if (s == Z_NULL) return Z_MEM_ERROR;
ad1f37
     strm->state = (struct internal_state FAR *)s;
ad1f37
     s->strm = strm;
ad1f37
@@ -318,7 +331,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
ad1f37
     s->hash_mask = s->hash_size - 1;
ad1f37
     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
ad1f37
 
ad1f37
-    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
ad1f37
+    s->window = (Bytef *) ZALLOC_WINDOW(strm, s->w_size, 2*sizeof(Byte));
ad1f37
     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
ad1f37
     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
ad1f37
 
ad1f37
@@ -394,6 +407,7 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
ad1f37
     /* when using zlib wrappers, compute Adler-32 for provided dictionary */
ad1f37
     if (wrap == 1)
ad1f37
         strm->adler = adler32(strm->adler, dictionary, dictLength);
ad1f37
+    DEFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);
ad1f37
     s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
ad1f37
 
ad1f37
     /* if dictionary would fill window, just replace the history */
ad1f37
@@ -452,6 +466,7 @@ int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
ad1f37
 
ad1f37
     if (deflateStateCheck(strm))
ad1f37
         return Z_STREAM_ERROR;
ad1f37
+    DEFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);
ad1f37
     s = strm->state;
ad1f37
     len = s->strstart + s->lookahead;
ad1f37
     if (len > s->w_size)
ad1f37
@@ -498,6 +513,8 @@ int ZEXPORT deflateResetKeep (strm)
ad1f37
 
ad1f37
     _tr_init(s);
ad1f37
 
ad1f37
+    DEFLATE_RESET_KEEP_HOOK(strm);
ad1f37
+
ad1f37
     return Z_OK;
ad1f37
 }
ad1f37
 
ad1f37
@@ -584,6 +601,7 @@ int ZEXPORT deflateParams(strm, level, strategy)
ad1f37
     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
ad1f37
         return Z_STREAM_ERROR;
ad1f37
     }
ad1f37
+    DEFLATE_PARAMS_HOOK(strm, level, strategy);
ad1f37
     func = configuration_table[s->level].func;
ad1f37
 
ad1f37
     if ((strategy != s->strategy || func != configuration_table[level].func) &&
ad1f37
@@ -659,6 +677,7 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
ad1f37
     /* conservative upper bound for compressed data */
ad1f37
     complen = sourceLen +
ad1f37
               ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
ad1f37
+    DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen);
ad1f37
 
ad1f37
     /* if can't get parameters, return conservative bound plus zlib wrapper */
ad1f37
     if (deflateStateCheck(strm))
ad1f37
@@ -700,7 +719,8 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
ad1f37
     }
ad1f37
 
ad1f37
     /* if not default parameters, return conservative bound */
ad1f37
-    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
ad1f37
+    if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) ||
ad1f37
+            s->w_bits != 15 || s->hash_bits != 8 + 7)
ad1f37
         return complen + wraplen;
ad1f37
 
ad1f37
     /* default settings: return tight bound for that case */
ad1f37
@@ -727,7 +747,7 @@ local void putShortMSB (s, b)
ad1f37
  * applications may wish to modify it to avoid allocating a large
ad1f37
  * strm->next_out buffer and copying into it. (See also read_buf()).
ad1f37
  */
ad1f37
-local void flush_pending(strm)
ad1f37
+void ZLIB_INTERNAL flush_pending(strm)
ad1f37
     z_streamp strm;
ad1f37
 {
ad1f37
     unsigned len;
ad1f37
@@ -997,7 +1017,8 @@ int ZEXPORT deflate (strm, flush)
ad1f37
         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
ad1f37
         block_state bstate;
ad1f37
 
ad1f37
-        bstate = s->level == 0 ? deflate_stored(s, flush) :
ad1f37
+        bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate :
ad1f37
+                 s->level == 0 ? deflate_stored(s, flush) :
ad1f37
                  s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
ad1f37
                  s->strategy == Z_RLE ? deflate_rle(s, flush) :
ad1f37
                  (*(configuration_table[s->level].func))(s, flush);
ad1f37
@@ -1086,9 +1107,9 @@ int ZEXPORT deflateEnd (strm)
ad1f37
     TRY_FREE(strm, strm->state->pending_buf);
ad1f37
     TRY_FREE(strm, strm->state->head);
ad1f37
     TRY_FREE(strm, strm->state->prev);
ad1f37
-    TRY_FREE(strm, strm->state->window);
ad1f37
+    TRY_FREE_WINDOW(strm, strm->state->window);
ad1f37
 
ad1f37
-    ZFREE(strm, strm->state);
ad1f37
+    ZFREE_STATE(strm, strm->state);
ad1f37
     strm->state = Z_NULL;
ad1f37
 
ad1f37
     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
ad1f37
@@ -1119,13 +1140,13 @@ int ZEXPORT deflateCopy (dest, source)
ad1f37
 
ad1f37
     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
ad1f37
 
ad1f37
-    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
ad1f37
+    ds = (deflate_state *) ZALLOC_STATE(dest, 1, sizeof(deflate_state));
ad1f37
     if (ds == Z_NULL) return Z_MEM_ERROR;
ad1f37
     dest->state = (struct internal_state FAR *) ds;
ad1f37
-    zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
ad1f37
+    ZCOPY_STATE((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
ad1f37
     ds->strm = dest;
ad1f37
 
ad1f37
-    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
ad1f37
+    ds->window = (Bytef *) ZALLOC_WINDOW(dest, ds->w_size, 2*sizeof(Byte));
ad1f37
     ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
ad1f37
     ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
ad1f37
     overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
ad1f37
@@ -1174,7 +1195,8 @@ local unsigned read_buf(strm, buf, size)
ad1f37
     strm->avail_in  -= len;
ad1f37
 
ad1f37
     zmemcpy(buf, strm->next_in, len);
ad1f37
-    if (strm->state->wrap == 1) {
ad1f37
+        if (!DEFLATE_NEED_CHECKSUM(strm)) {}
ad1f37
+    else if (strm->state->wrap == 1) {
ad1f37
         strm->adler = adler32(strm->adler, buf, len);
ad1f37
     }
ad1f37
 #ifdef GZIP
ad1f37
diff --git a/deflate.h b/deflate.h
ad1f37
index 23ecdd3..821a4b9 100644
ad1f37
--- a/deflate.h
ad1f37
+++ b/deflate.h
ad1f37
@@ -304,6 +304,7 @@ void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
ad1f37
 void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
ad1f37
 void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
ad1f37
                         ulg stored_len, int last));
ad1f37
+void ZLIB_INTERNAL _tr_send_bits OF((deflate_state *s, int value, int length));
ad1f37
 
ad1f37
 #define d_code(dist) \
ad1f37
    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
ad1f37
@@ -346,4 +347,15 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
ad1f37
               flush = _tr_tally(s, distance, length)
ad1f37
 #endif
ad1f37
 
ad1f37
+typedef enum {
ad1f37
+    need_more,      /* block not completed, need more input or more output */
ad1f37
+    block_done,     /* block flush performed */
ad1f37
+    finish_started, /* finish started, need only more output at next deflate */
ad1f37
+    finish_done     /* finish done, accept no more input or output */
ad1f37
+} block_state;
ad1f37
+
ad1f37
+unsigned ZLIB_INTERNAL bi_reverse OF((unsigned code, int len));
ad1f37
+void ZLIB_INTERNAL bi_windup OF((deflate_state *s));
ad1f37
+void ZLIB_INTERNAL flush_pending OF((z_streamp strm));
ad1f37
+
ad1f37
 #endif /* DEFLATE_H */
ad1f37
diff --git a/gzguts.h b/gzguts.h
ad1f37
index 990a4d2..3218395 100644
ad1f37
--- a/gzguts.h
ad1f37
+++ b/gzguts.h
ad1f37
@@ -153,7 +153,11 @@
ad1f37
 
ad1f37
 /* default i/o buffer size -- double this for output when reading (this and
ad1f37
    twice this must be able to fit in an unsigned type) */
ad1f37
+#ifdef DFLTCC
ad1f37
+#define GZBUFSIZE 131072
ad1f37
+#else
ad1f37
 #define GZBUFSIZE 8192
ad1f37
+#endif
ad1f37
 
ad1f37
 /* gzip modes, also provide a little integrity check on the passed structure */
ad1f37
 #define GZ_NONE 0
ad1f37
diff --git a/inflate.c b/inflate.c
ad1f37
index ac333e8..f77c2ae 100644
ad1f37
--- a/inflate.c
ad1f37
+++ b/inflate.c
ad1f37
@@ -85,6 +85,23 @@
ad1f37
 #include "inflate.h"
ad1f37
 #include "inffast.h"
ad1f37
 
ad1f37
+/* architecture-specific bits */
ad1f37
+#ifdef DFLTCC
ad1f37
+#  include "contrib/s390/dfltcc.h"
ad1f37
+#else
ad1f37
+#define ZALLOC_STATE ZALLOC
ad1f37
+#define ZFREE_STATE ZFREE
ad1f37
+#define ZCOPY_STATE zmemcpy
ad1f37
+#define ZALLOC_WINDOW ZALLOC
ad1f37
+#define ZFREE_WINDOW ZFREE
ad1f37
+#define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
ad1f37
+#define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0)
ad1f37
+#define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
ad1f37
+#define INFLATE_NEED_CHECKSUM(strm) 1
ad1f37
+#define INFLATE_NEED_UPDATEWINDOW(strm) 1
ad1f37
+#define INFLATE_MARK_HOOK(strm) do {} while (0)
ad1f37
+#endif
ad1f37
+
ad1f37
 #ifdef MAKEFIXED
ad1f37
 #  ifndef BUILDFIXED
ad1f37
 #    define BUILDFIXED
ad1f37
@@ -137,6 +154,7 @@ z_streamp strm;
ad1f37
     state->lencode = state->distcode = state->next = state->codes;
ad1f37
     state->sane = 1;
ad1f37
     state->back = -1;
ad1f37
+    INFLATE_RESET_KEEP_HOOK(strm);
ad1f37
     Tracev((stderr, "inflate: reset\n"));
ad1f37
     return Z_OK;
ad1f37
 }
ad1f37
@@ -182,7 +200,7 @@ int windowBits;
ad1f37
     if (windowBits && (windowBits < 8 || windowBits > 15))
ad1f37
         return Z_STREAM_ERROR;
ad1f37
     if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
ad1f37
-        ZFREE(strm, state->window);
ad1f37
+        ZFREE_WINDOW(strm, state->window);
ad1f37
         state->window = Z_NULL;
ad1f37
     }
ad1f37
 
ad1f37
@@ -221,7 +239,7 @@ int stream_size;
ad1f37
         strm->zfree = zcfree;
ad1f37
 #endif
ad1f37
     state = (struct inflate_state FAR *)
ad1f37
-            ZALLOC(strm, 1, sizeof(struct inflate_state));
ad1f37
+            ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
ad1f37
     if (state == Z_NULL) return Z_MEM_ERROR;
ad1f37
     Tracev((stderr, "inflate: allocated\n"));
ad1f37
     strm->state = (struct internal_state FAR *)state;
ad1f37
@@ -230,7 +248,7 @@ int stream_size;
ad1f37
     state->mode = HEAD;     /* to pass state test in inflateReset2() */
ad1f37
     ret = inflateReset2(strm, windowBits);
ad1f37
     if (ret != Z_OK) {
ad1f37
-        ZFREE(strm, state);
ad1f37
+        ZFREE_STATE(strm, state);
ad1f37
         strm->state = Z_NULL;
ad1f37
     }
ad1f37
     return ret;
ad1f37
@@ -252,6 +270,7 @@ int value;
ad1f37
     struct inflate_state FAR *state;
ad1f37
 
ad1f37
     if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
ad1f37
+    INFLATE_PRIME_HOOK(strm, bits, value);
ad1f37
     state = (struct inflate_state FAR *)strm->state;
ad1f37
     if (bits < 0) {
ad1f37
         state->hold = 0;
ad1f37
@@ -379,6 +398,27 @@ void makefixed()
ad1f37
 }
ad1f37
 #endif /* MAKEFIXED */
ad1f37
 
ad1f37
+int ZLIB_INTERNAL inflate_ensure_window(state)
ad1f37
+    struct inflate_state *state;
ad1f37
+{
ad1f37
+    /* if it hasn't been done already, allocate space for the window */
ad1f37
+    if (state->window == Z_NULL) {
ad1f37
+        state->window = (unsigned char FAR *)
ad1f37
+                        ZALLOC_WINDOW(state->strm, 1U << state->wbits,
ad1f37
+                                      sizeof(unsigned char));
ad1f37
+        if (state->window == Z_NULL) return 1;
ad1f37
+    }
ad1f37
+
ad1f37
+    /* if window not in use yet, initialize */
ad1f37
+    if (state->wsize == 0) {
ad1f37
+        state->wsize = 1U << state->wbits;
ad1f37
+        state->wnext = 0;
ad1f37
+        state->whave = 0;
ad1f37
+    }
ad1f37
+
ad1f37
+    return 0;
ad1f37
+}
ad1f37
+
ad1f37
 /*
ad1f37
    Update the window with the last wsize (normally 32K) bytes written before
ad1f37
    returning.  If window does not exist yet, create it.  This is only called
ad1f37
@@ -403,20 +443,7 @@ unsigned copy;
ad1f37
 
ad1f37
     state = (struct inflate_state FAR *)strm->state;
ad1f37
 
ad1f37
-    /* if it hasn't been done already, allocate space for the window */
ad1f37
-    if (state->window == Z_NULL) {
ad1f37
-        state->window = (unsigned char FAR *)
ad1f37
-                        ZALLOC(strm, 1U << state->wbits,
ad1f37
-                               sizeof(unsigned char));
ad1f37
-        if (state->window == Z_NULL) return 1;
ad1f37
-    }
ad1f37
-
ad1f37
-    /* if window not in use yet, initialize */
ad1f37
-    if (state->wsize == 0) {
ad1f37
-        state->wsize = 1U << state->wbits;
ad1f37
-        state->wnext = 0;
ad1f37
-        state->whave = 0;
ad1f37
-    }
ad1f37
+    if (inflate_ensure_window(state)) return 1;
ad1f37
 
ad1f37
     /* copy state->wsize or less output bytes into the circular window */
ad1f37
     if (copy >= state->wsize) {
ad1f37
@@ -849,6 +876,7 @@ int flush;
ad1f37
         case TYPE:
ad1f37
             if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
ad1f37
         case TYPEDO:
ad1f37
+            INFLATE_TYPEDO_HOOK(strm, flush);
ad1f37
             if (state->last) {
ad1f37
                 BYTEBITS();
ad1f37
                 state->mode = CHECK;
ad1f37
@@ -1200,7 +1228,7 @@ int flush;
ad1f37
                 out -= left;
ad1f37
                 strm->total_out += out;
ad1f37
                 state->total += out;
ad1f37
-                if ((state->wrap & 4) && out)
ad1f37
+                if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
ad1f37
                     strm->adler = state->check =
ad1f37
                         UPDATE(state->check, put - out, out);
ad1f37
                 out = left;
ad1f37
@@ -1252,8 +1280,9 @@ int flush;
ad1f37
      */
ad1f37
   inf_leave:
ad1f37
     RESTORE();
ad1f37
-    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
ad1f37
-            (state->mode < CHECK || flush != Z_FINISH)))
ad1f37
+    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
ad1f37
+        (state->wsize || (out != strm->avail_out && state->mode < BAD &&
ad1f37
+                (state->mode < CHECK || flush != Z_FINISH))))
ad1f37
         if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
ad1f37
             state->mode = MEM;
ad1f37
             return Z_MEM_ERROR;
ad1f37
@@ -1263,7 +1292,7 @@ int flush;
ad1f37
     strm->total_in += in;
ad1f37
     strm->total_out += out;
ad1f37
     state->total += out;
ad1f37
-    if ((state->wrap & 4) && out)
ad1f37
+    if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
ad1f37
         strm->adler = state->check =
ad1f37
             UPDATE(state->check, strm->next_out - out, out);
ad1f37
     strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
ad1f37
@@ -1281,8 +1310,8 @@ z_streamp strm;
ad1f37
     if (inflateStateCheck(strm))
ad1f37
         return Z_STREAM_ERROR;
ad1f37
     state = (struct inflate_state FAR *)strm->state;
ad1f37
-    if (state->window != Z_NULL) ZFREE(strm, state->window);
ad1f37
-    ZFREE(strm, strm->state);
ad1f37
+    if (state->window != Z_NULL) ZFREE_WINDOW(strm, state->window);
ad1f37
+    ZFREE_STATE(strm, strm->state);
ad1f37
     strm->state = Z_NULL;
ad1f37
     Tracev((stderr, "inflate: end\n"));
ad1f37
     return Z_OK;
ad1f37
@@ -1474,21 +1503,21 @@ z_streamp source;
ad1f37
 
ad1f37
     /* allocate space */
ad1f37
     copy = (struct inflate_state FAR *)
ad1f37
-           ZALLOC(source, 1, sizeof(struct inflate_state));
ad1f37
+           ZALLOC_STATE(source, 1, sizeof(struct inflate_state));
ad1f37
     if (copy == Z_NULL) return Z_MEM_ERROR;
ad1f37
     window = Z_NULL;
ad1f37
     if (state->window != Z_NULL) {
ad1f37
         window = (unsigned char FAR *)
ad1f37
-                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
ad1f37
+                 ZALLOC_WINDOW(source, 1U << state->wbits, sizeof(unsigned char));
ad1f37
         if (window == Z_NULL) {
ad1f37
-            ZFREE(source, copy);
ad1f37
+            ZFREE_STATE(source, copy);
ad1f37
             return Z_MEM_ERROR;
ad1f37
         }
ad1f37
     }
ad1f37
 
ad1f37
     /* copy state */
ad1f37
     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
ad1f37
-    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
ad1f37
+    ZCOPY_STATE((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
ad1f37
     copy->strm = dest;
ad1f37
     if (state->lencode >= state->codes &&
ad1f37
         state->lencode <= state->codes + ENOUGH - 1) {
ad1f37
@@ -1545,6 +1574,7 @@ z_streamp strm;
ad1f37
 
ad1f37
     if (inflateStateCheck(strm))
ad1f37
         return -(1L << 16);
ad1f37
+    INFLATE_MARK_HOOK(strm);
ad1f37
     state = (struct inflate_state FAR *)strm->state;
ad1f37
     return (long)(((unsigned long)((long)state->back)) << 16) +
ad1f37
         (state->mode == COPY ? state->length :
ad1f37
diff --git a/inflate.h b/inflate.h
ad1f37
index a46cce6..7b19617 100644
ad1f37
--- a/inflate.h
ad1f37
+++ b/inflate.h
ad1f37
@@ -123,3 +123,5 @@ struct inflate_state {
ad1f37
     int back;                   /* bits back of last unprocessed length/lit */
ad1f37
     unsigned was;               /* initial length of match */
ad1f37
 };
ad1f37
+
ad1f37
+int ZLIB_INTERNAL inflate_ensure_window OF((struct inflate_state *state));
ad1f37
diff --git a/test/infcover.c b/test/infcover.c
ad1f37
index 2be0164..a34cd17 100644
ad1f37
--- a/test/infcover.c
ad1f37
+++ b/test/infcover.c
ad1f37
@@ -444,7 +444,7 @@ local void cover_wrap(void)
ad1f37
 }
ad1f37
 
ad1f37
 /* input and output functions for inflateBack() */
ad1f37
-local unsigned pull(void *desc, unsigned char **buf)
ad1f37
+local unsigned pull(void *desc, z_const unsigned char **buf)
ad1f37
 {
ad1f37
     static unsigned int next = 0;
ad1f37
     static unsigned char dat[] = {0x63, 0, 2, 0};
ad1f37
diff --git a/test/minigzip.c b/test/minigzip.c
ad1f37
index e22fb08..4b5f4ef 100644
ad1f37
--- a/test/minigzip.c
ad1f37
+++ b/test/minigzip.c
ad1f37
@@ -132,7 +132,11 @@ static void pwinerror (s)
ad1f37
 #endif
ad1f37
 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
ad1f37
 
ad1f37
+#ifdef DFLTCC
ad1f37
+#define BUFLEN      262144
ad1f37
+#else
ad1f37
 #define BUFLEN      16384
ad1f37
+#endif
ad1f37
 #define MAX_NAME_LEN 1024
ad1f37
 
ad1f37
 #ifdef MAXSEG_64K
ad1f37
diff --git a/trees.c b/trees.c
ad1f37
index 50cf4b4..ad51207 100644
ad1f37
--- a/trees.c
ad1f37
+++ b/trees.c
ad1f37
@@ -149,8 +149,6 @@ local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
ad1f37
 local void compress_block OF((deflate_state *s, const ct_data *ltree,
ad1f37
                               const ct_data *dtree));
ad1f37
 local int  detect_data_type OF((deflate_state *s));
ad1f37
-local unsigned bi_reverse OF((unsigned value, int length));
ad1f37
-local void bi_windup      OF((deflate_state *s));
ad1f37
 local void bi_flush       OF((deflate_state *s));
ad1f37
 
ad1f37
 #ifdef GEN_TREES_H
ad1f37
@@ -223,6 +221,13 @@ local void send_bits(s, value, length)
ad1f37
 }
ad1f37
 #endif /* ZLIB_DEBUG */
ad1f37
 
ad1f37
+void ZLIB_INTERNAL _tr_send_bits(s, value, length)
ad1f37
+    deflate_state *s;
ad1f37
+    int value;
ad1f37
+    int length;
ad1f37
+{
ad1f37
+    send_bits(s, value, length);
ad1f37
+}
ad1f37
 
ad1f37
 /* the arguments must not have side effects */
ad1f37
 
ad1f37
@@ -1155,7 +1160,7 @@ local int detect_data_type(s)
ad1f37
  * method would use a table)
ad1f37
  * IN assertion: 1 <= len <= 15
ad1f37
  */
ad1f37
-local unsigned bi_reverse(code, len)
ad1f37
+unsigned ZLIB_INTERNAL bi_reverse(code, len)
ad1f37
     unsigned code; /* the value to invert */
ad1f37
     int len;       /* its bit length */
ad1f37
 {
ad1f37
@@ -1187,7 +1192,7 @@ local void bi_flush(s)
ad1f37
 /* ===========================================================================
ad1f37
  * Flush the bit buffer and align the output on a byte boundary
ad1f37
  */
ad1f37
-local void bi_windup(s)
ad1f37
+void ZLIB_INTERNAL bi_windup(s)
ad1f37
     deflate_state *s;
ad1f37
 {
ad1f37
     if (s->bi_valid > 8) {
ad1f37
-- 
ad1f37
2.19.1
ad1f37