adenilson / rpms / zlib

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