adenilson / rpms / zlib

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