git: cd8822075a38 - main - MFV c144cc54795d: zlib 1.2.12.

From: Xin LI <delphij_at_FreeBSD.org>
Date: Thu, 31 Mar 2022 14:26:36 UTC
The branch main has been updated by delphij:

URL: https://cgit.FreeBSD.org/src/commit/?id=cd8822075a38d0734e74b1735e4b5dbef9789170

commit cd8822075a38d0734e74b1735e4b5dbef9789170
Merge: e57f6630d9d4 c144cc54795d
Author:     Xin LI <delphij@FreeBSD.org>
AuthorDate: 2022-03-31 14:25:53 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2022-03-31 14:25:53 +0000

    MFV c144cc54795d: zlib 1.2.12.
    
    MFC after:      3 days
    Relnotes:       yes

 lib/libz/Makefile                              |    2 +-
 lib/libz/Symbol.map                            |    6 +
 lib/libz/Versions.def                          |    3 +
 sys/contrib/zlib/ChangeLog                     |  161 +-
 sys/contrib/zlib/README                        |   11 +-
 sys/contrib/zlib/contrib/README.contrib        |   21 -
 sys/contrib/zlib/contrib/asm686/README.686     |   51 -
 sys/contrib/zlib/contrib/asm686/match.S        |  357 -
 sys/contrib/zlib/contrib/gcc_gvmat64/gvmat64.S | 1148 +--
 sys/contrib/zlib/crc32.c                       | 1258 ++-
 sys/contrib/zlib/crc32.h                       | 9877 ++++++++++++++++++++++--
 sys/contrib/zlib/deflate.c                     |   95 +-
 sys/contrib/zlib/deflate.h                     |   27 +-
 sys/contrib/zlib/doc/crc-doc.1.0.pdf           |  Bin 0 -> 776142 bytes
 sys/contrib/zlib/doc/txtvsbin.txt              |   12 +-
 sys/contrib/zlib/gzguts.h                      |    5 +-
 sys/contrib/zlib/gzlib.c                       |    8 +-
 sys/contrib/zlib/gzread.c                      |   12 +-
 sys/contrib/zlib/gzwrite.c                     |   38 +-
 sys/contrib/zlib/infback.c                     |    3 +-
 sys/contrib/zlib/inffast.c                     |   28 +-
 sys/contrib/zlib/inflate.c                     |   47 +-
 sys/contrib/zlib/inflate.h                     |    5 +-
 sys/contrib/zlib/inftrees.c                    |    6 +-
 sys/contrib/zlib/test/example.c                |    5 +-
 sys/contrib/zlib/trees.c                       |   75 +-
 sys/contrib/zlib/zlib.3                        |    6 +-
 sys/contrib/zlib/zlib.h                        |  212 +-
 sys/contrib/zlib/zlib.map                      |    6 +
 sys/contrib/zlib/zutil.c                       |    4 +-
 sys/contrib/zlib/zutil.h                       |   24 +-
 31 files changed, 11466 insertions(+), 2047 deletions(-)

diff --cc lib/libz/Makefile
index a954fee95118,000000000000..d6155b6955a6
mode 100644,000000..100644
--- a/lib/libz/Makefile
+++ b/lib/libz/Makefile
@@@ -1,85 -1,0 +1,85 @@@
 +#
 +# $FreeBSD$
 +#
 +
 +PACKAGE=	runtime
 +LIB=		z
 +SHLIBDIR?=	/lib
 +SHLIB_MAJOR=	6
 +MAN=		zlib.3 zopen.3
 +MLINKS+=	zopen.3 zdopen.3
 +
 +ZLIBSRC=	${SRCTOP}/sys/contrib/zlib
 +
 +.PATH:		${ZLIBSRC}
 +
 +#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
 +#CFLAGS=-g -DDEBUG
 +#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
 +#           -Wstrict-prototypes -Wmissing-prototypes
 +
- CFLAGS+=	-DHAS_snprintf -DHAS_vsnprintf -I${.CURDIR}
++CFLAGS+=	-D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -I${.CURDIR}
 +
 +WARNS?=		3
 +
 +CLEANFILES+=	example.o example foo.gz minigzip.o minigzip
 +
 +SRCS+=		adler32.c
 +SRCS+=		compress.c
 +SRCS+=		crc32.c
 +SRCS+=		deflate.c
 +SRCS+=		gzclose.c
 +SRCS+=		gzlib.c
 +SRCS+=		gzread.c
 +SRCS+=		gzwrite.c
 +SRCS+=		infback.c
 +SRCS+=		inffast.c
 +SRCS+=		inflate.c
 +SRCS+=		inftrees.c
 +SRCS+=		trees.c
 +SRCS+=		uncompr.c
 +SRCS+=		zutil.c
 +
 +# Wrapper relies on FreeBSD-specific fpos_t representation and non-portable
 +# funopen. Not needed for bootstrapping, so just disable it.
 +.if ${.MAKE.OS} == "FreeBSD" || !defined(BOOTSTRAPPING)
 +SRCS+=		zopen.c
 +.endif
 +
 +.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64"
 +CFLAGS+=	-DUNALIGNED_OK
 +.endif
 +
 +VERSION_DEF=	${.CURDIR}/Versions.def
 +SYMBOL_MAPS=	${.CURDIR}/Symbol.map
 +CFLAGS+=	-DSYMBOL_VERSIONING
 +
 +INCS=		zconf.h zlib.h
 +
 +.PATH:		${ZLIBSRC}/test
 +
 +ZLIB_VERSION!=	sed -n '/define.*ZLIB_VERSION/{s,[^0-9.],,gp;q;}' ${ZLIBSRC}/zlib.h
 +
 +zlib.pc: zlib.pc.in
 +	sed -e 's,@prefix@,/usr,g ; \
 +		s,@exec_prefix@,$${prefix},g ; \
 +		s,@libdir@,$${exec_prefix}/lib,g ; \
 +		s,@sharedlibdir@,$${libdir},g ; \
 +		s,@includedir@,$${prefix}/include,g ; \
 +		s,@VERSION@,${ZLIB_VERSION},g ;' \
 +		${.ALLSRC} > ${.TARGET}
 +
 +minigzip:	all minigzip.o
 +	$(CC) -o minigzip minigzip.o -L. -lz
 +
 +example:	all example.o
 +	$(CC) -o example example.o -L. -lz
 +
 +test: example minigzip
 +	(export LD_LIBRARY_PATH=. ; ./example )
 +	(export LD_LIBRARY_PATH=. ; \
 +		echo hello world | ./minigzip | ./minigzip -d )
 +
 +PCFILES=	zlib.pc
 +
 +.include <bsd.lib.mk>
diff --cc lib/libz/Symbol.map
index 0a91537272c1,000000000000..9e38aee72ed2
mode 100644,000000..100644
--- a/lib/libz/Symbol.map
+++ b/lib/libz/Symbol.map
@@@ -1,123 -1,0 +1,129 @@@
 +/*
 + * $FreeBSD$
 + */
 +
++ZLIB_1.2.12 {
++	crc32_combine_gen;
++	crc32_combine_gen64;
++	crc32_combine_op;
++};
++
 +ZLIB_1.2.9 {
 +	inflateCodesUsed;
 +	inflateValidate;
 +	uncompress2;
 +	gzfread;
 +	gzfwrite;
 +	deflateGetDictionary;
 +	adler32_z;
 +	crc32_z;
 +};
 +
 +ZLIB_1.2.7.1 {
 +	inflateGetDictionary;
 +	gzvprintf;
 +};
 +
 +ZLIB_1.2.7.0 {
 +	deflatePending;
 +	deflateResetKeep;
 +	gzgetc_;
 +	inflateResetKeep;
 +};
 +
 +ZLIB_1.2.4.0 {
 +	adler32;
 +	adler32_combine;
 +	adler32_combine64;
 +	compress;
 +	compress2;
 +	compressBound;
 +	crc32;
 +	crc32_combine;
 +	crc32_combine64;
 +	deflate;
 +	deflateBound;
 +	deflateCopy;
 +	deflateEnd;
 +	deflateInit2_;
 +	deflateInit_;
 +	deflateParams;
 +	deflatePrime;
 +	deflateReset;
 +	deflateSetDictionary;
 +	deflateSetHeader;
 +	deflateTune;
 +	get_crc_table;
 +	gzbuffer;
 +	gzclearerr;
 +	gzclose;
 +	gzclose_r;
 +	gzclose_w;
 +	gzdirect;
 +	gzdopen;
 +	gzeof;
 +	gzerror;
 +	gzflush;
 +	gzgetc;
 +	gzgets;
 +	gzoffset;
 +	gzoffset64;
 +	gzopen;
 +	gzopen64;
 +	gzprintf;
 +	gzputc;
 +	gzputs;
 +	gzread;
 +	gzrewind;
 +	gzseek;
 +	gzseek64;
 +	gzsetparams;
 +	gztell;
 +	gztell64;
 +	gzungetc;
 +	gzwrite;
 +	inflate;
 +	inflateBack;
 +	inflateBackEnd;
 +	inflateBackInit_;
 +	inflateCopy;
 +	inflateEnd;
 +	inflateGetHeader;
 +	inflateInit2_;
 +	inflateInit_;
 +	inflateMark;
 +	inflatePrime;
 +	inflateReset;
 +	inflateReset2;
 +	inflateSetDictionary;
 +	inflateSync;
 +	inflateSyncPoint;
 +	inflateUndermine;
 +	uncompress;
 +	zError;
 +	zlibCompileFlags;
 +	zlibVersion;
 +};
 +
 +FBSD_1.2 {
 +	zopen;
 +};
 +
 +FBSD_1.6 {
 +	zdopen;
 +};
 +
 +ZLIBprivate_1.0 {
 +	_tr_align;
 +	_tr_flush_block;
 +	_tr_init;
 +	_tr_stored_block;
 +	_tr_tally;
 +	gz_error;
 +	inflate_fast;
 +	inflate_table;
 +	longest_match;
 +	match_init;
 +	zcalloc;
 +	zcfree;
 +};
diff --cc lib/libz/Versions.def
index 9a67602185b8,000000000000..34124ad99fd7
mode 100644,000000..100644
--- a/lib/libz/Versions.def
+++ b/lib/libz/Versions.def
@@@ -1,23 -1,0 +1,26 @@@
 +# $FreeBSD$
 +
 +ZLIB_1.2.4.0 {
 +};
 +
 +ZLIB_1.2.7.0 {
 +} ZLIB_1.2.4.0;
 +
 +ZLIB_1.2.7.1 {
 +} ZLIB_1.2.7.0;
 +
 +ZLIB_1.2.9 {
 +} ZLIB_1.2.7.1;
 +
++ZLIB_1.2.12 {
++} ZLIB_1.2.9;
++
 +FBSD_1.2 {
 +} ZLIB_1.2.4.0;
 +
 +FBSD_1.6 {
 +} FBSD_1.2;
 +
 +ZLIBprivate_1.0 {
 +} ZLIB_1.2.4.0;
 +
diff --cc sys/contrib/zlib/deflate.c
index 061ad0d66d86,000000000000..af9e4bcf2918
mode 100644,000000..100644
--- a/sys/contrib/zlib/deflate.c
+++ b/sys/contrib/zlib/deflate.c
@@@ -1,2168 -1,0 +1,2213 @@@
 +/* deflate.c -- compress data using the deflation algorithm
-  * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
++ * Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
 + * For conditions of distribution and use, see copyright notice in zlib.h
 + */
 +
 +/*
 + *  ALGORITHM
 + *
 + *      The "deflation" process depends on being able to identify portions
 + *      of the input text which are identical to earlier input (within a
 + *      sliding window trailing behind the input currently being processed).
 + *
 + *      The most straightforward technique turns out to be the fastest for
 + *      most input files: try all possible matches and select the longest.
 + *      The key feature of this algorithm is that insertions into the string
 + *      dictionary are very simple and thus fast, and deletions are avoided
 + *      completely. Insertions are performed at each input character, whereas
 + *      string matches are performed only when the previous match ends. So it
 + *      is preferable to spend more time in matches to allow very fast string
 + *      insertions and avoid deletions. The matching algorithm for small
 + *      strings is inspired from that of Rabin & Karp. A brute force approach
 + *      is used to find longer strings when a small match has been found.
 + *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
 + *      (by Leonid Broukhis).
 + *         A previous version of this file used a more sophisticated algorithm
 + *      (by Fiala and Greene) which is guaranteed to run in linear amortized
 + *      time, but has a larger average cost, uses more memory and is patented.
 + *      However the F&G algorithm may be faster for some highly redundant
 + *      files if the parameter max_chain_length (described below) is too large.
 + *
 + *  ACKNOWLEDGEMENTS
 + *
 + *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
 + *      I found it in 'freeze' written by Leonid Broukhis.
 + *      Thanks to many people for bug reports and testing.
 + *
 + *  REFERENCES
 + *
 + *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
 + *      Available in http://tools.ietf.org/html/rfc1951
 + *
 + *      A description of the Rabin and Karp algorithm is given in the book
 + *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
 + *
 + *      Fiala,E.R., and Greene,D.H.
 + *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
 + *
 + */
 +
 +/* @(#) $Id$ */
 +
 +#include "deflate.h"
 +
 +const char deflate_copyright[] =
-    " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
++   " deflate 1.2.12 Copyright 1995-2022 Jean-loup Gailly and Mark Adler ";
 +/*
 +  If you use the zlib library in a product, an acknowledgment is welcome
 +  in the documentation of your product. If for some reason you cannot
 +  include such an acknowledgment, I would appreciate that you keep this
 +  copyright string in the executable of your product.
 + */
 +
 +/* ===========================================================================
 + *  Function prototypes.
 + */
 +typedef enum {
 +    need_more,      /* block not completed, need more input or more output */
 +    block_done,     /* block flush performed */
 +    finish_started, /* finish started, need only more output at next deflate */
 +    finish_done     /* finish done, accept no more input or output */
 +} block_state;
 +
 +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
 +/* Compression function. Returns the block state after the call. */
 +
 +local int deflateStateCheck      OF((z_streamp strm));
 +local void slide_hash     OF((deflate_state *s));
 +local void fill_window    OF((deflate_state *s));
 +local block_state deflate_stored OF((deflate_state *s, int flush));
 +local block_state deflate_fast   OF((deflate_state *s, int flush));
 +#ifndef FASTEST
 +local block_state deflate_slow   OF((deflate_state *s, int flush));
 +#endif
 +local block_state deflate_rle    OF((deflate_state *s, int flush));
 +local block_state deflate_huff   OF((deflate_state *s, int flush));
 +local void lm_init        OF((deflate_state *s));
 +local void putShortMSB    OF((deflate_state *s, uInt b));
 +local void flush_pending  OF((z_streamp strm));
 +local unsigned read_buf   OF((z_streamp strm, Bytef *buf, unsigned size));
 +#ifdef ASMV
 +#  pragma message("Assembler code may have bugs -- use at your own risk")
 +      void match_init OF((void)); /* asm code initialization */
 +      uInt longest_match  OF((deflate_state *s, IPos cur_match));
 +#else
 +local uInt longest_match  OF((deflate_state *s, IPos cur_match));
 +#endif
 +
 +#ifdef ZLIB_DEBUG
 +local  void check_match OF((deflate_state *s, IPos start, IPos match,
 +                            int length));
 +#endif
 +
 +/* ===========================================================================
 + * Local data
 + */
 +
 +#define NIL 0
 +/* Tail of hash chains */
 +
 +#ifndef TOO_FAR
 +#  define TOO_FAR 4096
 +#endif
 +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
 +
 +/* Values for max_lazy_match, good_match and max_chain_length, depending on
 + * the desired pack level (0..9). The values given below have been tuned to
 + * exclude worst case performance for pathological files. Better values may be
 + * found for specific files.
 + */
 +typedef struct config_s {
 +   ush good_length; /* reduce lazy search above this match length */
 +   ush max_lazy;    /* do not perform lazy search above this match length */
 +   ush nice_length; /* quit search above this match length */
 +   ush max_chain;
 +   compress_func func;
 +} config;
 +
 +#ifdef FASTEST
 +local const config configuration_table[2] = {
 +/*      good lazy nice chain */
 +/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
 +/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
 +#else
 +local const config configuration_table[10] = {
 +/*      good lazy nice chain */
 +/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
 +/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
 +/* 2 */ {4,    5, 16,    8, deflate_fast},
 +/* 3 */ {4,    6, 32,   32, deflate_fast},
 +
 +/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
 +/* 5 */ {8,   16, 32,   32, deflate_slow},
 +/* 6 */ {8,   16, 128, 128, deflate_slow},
 +/* 7 */ {8,   32, 128, 256, deflate_slow},
 +/* 8 */ {32, 128, 258, 1024, deflate_slow},
 +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
 +#endif
 +
 +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
 + * meaning.
 + */
 +
 +/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
 +#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
 +
 +/* ===========================================================================
 + * Update a hash value with the given input byte
 + * IN  assertion: all calls to UPDATE_HASH are made with consecutive input
 + *    characters, so that a running hash key can be computed from the previous
 + *    key instead of complete recalculation each time.
 + */
 +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
 +
 +
 +/* ===========================================================================
 + * Insert string str in the dictionary and set match_head to the previous head
 + * of the hash chain (the most recent string with same hash key). Return
 + * the previous length of the hash chain.
 + * If this file is compiled with -DFASTEST, the compression level is forced
 + * to 1, and no hash chains are maintained.
 + * IN  assertion: all calls to INSERT_STRING are made with consecutive input
 + *    characters and the first MIN_MATCH bytes of str are valid (except for
 + *    the last MIN_MATCH-1 bytes of the input file).
 + */
 +#ifdef FASTEST
 +#define INSERT_STRING(s, str, match_head) \
 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
 +    match_head = s->head[s->ins_h], \
 +    s->head[s->ins_h] = (Pos)(str))
 +#else
 +#define INSERT_STRING(s, str, match_head) \
 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
 +    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
 +    s->head[s->ins_h] = (Pos)(str))
 +#endif
 +
 +/* ===========================================================================
 + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
 + * prev[] will be initialized on the fly.
 + */
 +#define CLEAR_HASH(s) \
 +    do { \
 +        s->head[s->hash_size-1] = NIL; \
 +        zmemzero((Bytef *)s->head, \
 +                 (unsigned)(s->hash_size-1)*sizeof(*s->head)); \
 +    } while (0)
 +
 +/* ===========================================================================
 + * Slide the hash table when sliding the window down (could be avoided with 32
 + * bit values at the expense of memory usage). We slide even when level == 0 to
 + * keep the hash table consistent if we switch back to level > 0 later.
 + */
 +local void slide_hash(s)
 +    deflate_state *s;
 +{
 +    unsigned n, m;
 +    Posf *p;
 +    uInt wsize = s->w_size;
 +
 +    n = s->hash_size;
 +    p = &s->head[n];
 +    do {
 +        m = *--p;
 +        *p = (Pos)(m >= wsize ? m - wsize : NIL);
 +    } while (--n);
 +    n = wsize;
 +#ifndef FASTEST
 +    p = &s->prev[n];
 +    do {
 +        m = *--p;
 +        *p = (Pos)(m >= wsize ? m - wsize : NIL);
 +        /* If n is not on any hash chain, prev[n] is garbage but
 +         * its value will never be used.
 +         */
 +    } while (--n);
 +#endif
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateInit_(strm, level, version, stream_size)
 +    z_streamp strm;
 +    int level;
 +    const char *version;
 +    int stream_size;
 +{
 +    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
 +                         Z_DEFAULT_STRATEGY, version, stream_size);
 +    /* To do: ignore strm->next_in if we use it as window */
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
 +                  version, stream_size)
 +    z_streamp strm;
 +    int  level;
 +    int  method;
 +    int  windowBits;
 +    int  memLevel;
 +    int  strategy;
 +    const char *version;
 +    int stream_size;
 +{
 +    deflate_state *s;
 +    int wrap = 1;
 +    static const char my_version[] = ZLIB_VERSION;
 +
-     ushf *overlay;
-     /* We overlay pending_buf and d_buf+l_buf. This works since the average
-      * output size for (length,distance) codes is <= 24 bits.
-      */
- 
 +    if (version == Z_NULL || version[0] != my_version[0] ||
 +        stream_size != sizeof(z_stream)) {
 +        return Z_VERSION_ERROR;
 +    }
 +    if (strm == Z_NULL) return Z_STREAM_ERROR;
 +
 +    strm->msg = Z_NULL;
 +    if (strm->zalloc == (alloc_func)0) {
 +#if defined(Z_SOLO) && !defined(_KERNEL)
 +        return Z_STREAM_ERROR;
 +#else
 +        strm->zalloc = zcalloc;
 +        strm->opaque = (voidpf)0;
 +#endif
 +    }
 +    if (strm->zfree == (free_func)0)
 +#if defined(Z_SOLO) && !defined(_KERNEL)
 +        return Z_STREAM_ERROR;
 +#else
 +        strm->zfree = zcfree;
 +#endif
 +
 +#ifdef FASTEST
 +    if (level != 0) level = 1;
 +#else
 +    if (level == Z_DEFAULT_COMPRESSION) level = 6;
 +#endif
 +
 +    if (windowBits < 0) { /* suppress zlib wrapper */
 +        wrap = 0;
 +        windowBits = -windowBits;
 +    }
 +#ifdef GZIP
 +    else if (windowBits > 15) {
 +        wrap = 2;       /* write gzip wrapper instead */
 +        windowBits -= 16;
 +    }
 +#endif
 +    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
 +        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
 +        strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
 +        return Z_STREAM_ERROR;
 +    }
 +    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
 +    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
 +    if (s == Z_NULL) return Z_MEM_ERROR;
 +    strm->state = (struct internal_state FAR *)s;
 +    s->strm = strm;
 +    s->status = INIT_STATE;     /* to pass state test in deflateReset() */
 +
 +    s->wrap = wrap;
 +    s->gzhead = Z_NULL;
 +    s->w_bits = (uInt)windowBits;
 +    s->w_size = 1 << s->w_bits;
 +    s->w_mask = s->w_size - 1;
 +
 +    s->hash_bits = (uInt)memLevel + 7;
 +    s->hash_size = 1 << s->hash_bits;
 +    s->hash_mask = s->hash_size - 1;
 +    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
 +
 +    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
 +    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
 +    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
 +
 +    s->high_water = 0;      /* nothing written to s->window yet */
 +
 +    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
 +
-     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
-     s->pending_buf = (uchf *) overlay;
-     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
++    /* We overlay pending_buf and sym_buf. This works since the average size
++     * for length/distance pairs over any compressed block is assured to be 31
++     * bits or less.
++     *
++     * Analysis: The longest fixed codes are a length code of 8 bits plus 5
++     * extra bits, for lengths 131 to 257. The longest fixed distance codes are
++     * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
++     * possible fixed-codes length/distance pair is then 31 bits total.
++     *
++     * sym_buf starts one-fourth of the way into pending_buf. So there are
++     * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
++     * in sym_buf is three bytes -- two for the distance and one for the
++     * literal/length. As each symbol is consumed, the pointer to the next
++     * sym_buf value to read moves forward three bytes. From that symbol, up to
++     * 31 bits are written to pending_buf. The closest the written pending_buf
++     * bits gets to the next sym_buf symbol to read is just before the last
++     * code is written. At that time, 31*(n-2) bits have been written, just
++     * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
++     * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
++     * symbols are written.) The closest the writing gets to what is unread is
++     * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
++     * can range from 128 to 32768.
++     *
++     * Therefore, at a minimum, there are 142 bits of space between what is
++     * written and what is read in the overlain buffers, so the symbols cannot
++     * be overwritten by the compressed data. That space is actually 139 bits,
++     * due to the three-bit fixed-code block header.
++     *
++     * That covers the case where either Z_FIXED is specified, forcing fixed
++     * codes, or when the use of fixed codes is chosen, because that choice
++     * results in a smaller compressed block than dynamic codes. That latter
++     * condition then assures that the above analysis also covers all dynamic
++     * blocks. A dynamic-code block will only be chosen to be emitted if it has
++     * fewer bits than a fixed-code block would for the same set of symbols.
++     * Therefore its average symbol length is assured to be less than 31. So
++     * the compressed data for a dynamic block also cannot overwrite the
++     * symbols from which it is being constructed.
++     */
++
++    s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
++    s->pending_buf_size = (ulg)s->lit_bufsize * 4;
 +
 +    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
 +        s->pending_buf == Z_NULL) {
 +        s->status = FINISH_STATE;
 +        strm->msg = ERR_MSG(Z_MEM_ERROR);
 +        deflateEnd (strm);
 +        return Z_MEM_ERROR;
 +    }
-     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
-     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
++    s->sym_buf = s->pending_buf + s->lit_bufsize;
++    s->sym_end = (s->lit_bufsize - 1) * 3;
++    /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
++     * on 16 bit machines and because stored blocks are restricted to
++     * 64K-1 bytes.
++     */
 +
 +    s->level = level;
 +    s->strategy = strategy;
 +    s->method = (Byte)method;
 +
 +    return deflateReset(strm);
 +}
 +
 +/* =========================================================================
 + * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
 + */
 +local int deflateStateCheck (strm)
 +    z_streamp strm;
 +{
 +    deflate_state *s;
 +    if (strm == Z_NULL ||
 +        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
 +        return 1;
 +    s = strm->state;
 +    if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
 +#ifdef GZIP
 +                                           s->status != GZIP_STATE &&
 +#endif
 +                                           s->status != EXTRA_STATE &&
 +                                           s->status != NAME_STATE &&
 +                                           s->status != COMMENT_STATE &&
 +                                           s->status != HCRC_STATE &&
 +                                           s->status != BUSY_STATE &&
 +                                           s->status != FINISH_STATE))
 +        return 1;
 +    return 0;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
 +    z_streamp strm;
 +    const Bytef *dictionary;
 +    uInt  dictLength;
 +{
 +    deflate_state *s;
 +    uInt str, n;
 +    int wrap;
 +    unsigned avail;
 +    z_const unsigned char *next;
 +
 +    if (deflateStateCheck(strm) || dictionary == Z_NULL)
 +        return Z_STREAM_ERROR;
 +    s = strm->state;
 +    wrap = s->wrap;
 +    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
 +        return Z_STREAM_ERROR;
 +
 +    /* when using zlib wrappers, compute Adler-32 for provided dictionary */
 +    if (wrap == 1)
 +        strm->adler = adler32(strm->adler, dictionary, dictLength);
 +    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
 +
 +    /* if dictionary would fill window, just replace the history */
 +    if (dictLength >= s->w_size) {
 +        if (wrap == 0) {            /* already empty otherwise */
 +            CLEAR_HASH(s);
 +            s->strstart = 0;
 +            s->block_start = 0L;
 +            s->insert = 0;
 +        }
 +        dictionary += dictLength - s->w_size;  /* use the tail */
 +        dictLength = s->w_size;
 +    }
 +
 +    /* insert dictionary into window and hash */
 +    avail = strm->avail_in;
 +    next = strm->next_in;
 +    strm->avail_in = dictLength;
 +    strm->next_in = (z_const Bytef *)dictionary;
 +    fill_window(s);
 +    while (s->lookahead >= MIN_MATCH) {
 +        str = s->strstart;
 +        n = s->lookahead - (MIN_MATCH-1);
 +        do {
 +            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
 +#ifndef FASTEST
 +            s->prev[str & s->w_mask] = s->head[s->ins_h];
 +#endif
 +            s->head[s->ins_h] = (Pos)str;
 +            str++;
 +        } while (--n);
 +        s->strstart = str;
 +        s->lookahead = MIN_MATCH-1;
 +        fill_window(s);
 +    }
 +    s->strstart += s->lookahead;
 +    s->block_start = (long)s->strstart;
 +    s->insert = s->lookahead;
 +    s->lookahead = 0;
 +    s->match_length = s->prev_length = MIN_MATCH-1;
 +    s->match_available = 0;
 +    strm->next_in = next;
 +    strm->avail_in = avail;
 +    s->wrap = wrap;
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
 +    z_streamp strm;
 +    Bytef *dictionary;
 +    uInt  *dictLength;
 +{
 +    deflate_state *s;
 +    uInt len;
 +
 +    if (deflateStateCheck(strm))
 +        return Z_STREAM_ERROR;
 +    s = strm->state;
 +    len = s->strstart + s->lookahead;
 +    if (len > s->w_size)
 +        len = s->w_size;
 +    if (dictionary != Z_NULL && len)
 +        zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
 +    if (dictLength != Z_NULL)
 +        *dictLength = len;
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateResetKeep (strm)
 +    z_streamp strm;
 +{
 +    deflate_state *s;
 +
 +    if (deflateStateCheck(strm)) {
 +        return Z_STREAM_ERROR;
 +    }
 +
 +    strm->total_in = strm->total_out = 0;
 +    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
 +    strm->data_type = Z_UNKNOWN;
 +
 +    s = (deflate_state *)strm->state;
 +    s->pending = 0;
 +    s->pending_out = s->pending_buf;
 +
 +    if (s->wrap < 0) {
 +        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
 +    }
 +    s->status =
 +#ifdef GZIP
 +        s->wrap == 2 ? GZIP_STATE :
 +#endif
-         s->wrap ? INIT_STATE : BUSY_STATE;
++        INIT_STATE;
 +    strm->adler =
 +#ifdef GZIP
 +        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
 +#endif
 +        adler32(0L, Z_NULL, 0);
 +    s->last_flush = -2;
 +
 +    _tr_init(s);
 +
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateReset (strm)
 +    z_streamp strm;
 +{
 +    int ret;
 +
 +    ret = deflateResetKeep(strm);
 +    if (ret == Z_OK)
 +        lm_init(strm->state);
 +    return ret;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateSetHeader (strm, head)
 +    z_streamp strm;
 +    gz_headerp head;
 +{
 +    if (deflateStateCheck(strm) || strm->state->wrap != 2)
 +        return Z_STREAM_ERROR;
 +    strm->state->gzhead = head;
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflatePending (strm, pending, bits)
 +    unsigned *pending;
 +    int *bits;
 +    z_streamp strm;
 +{
 +    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
 +    if (pending != Z_NULL)
 +        *pending = strm->state->pending;
 +    if (bits != Z_NULL)
 +        *bits = strm->state->bi_valid;
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflatePrime (strm, bits, value)
 +    z_streamp strm;
 +    int bits;
 +    int value;
 +{
 +    deflate_state *s;
 +    int put;
 +
 +    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
 +    s = strm->state;
-     if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
++    if (bits < 0 || bits > 16 ||
++        s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
 +        return Z_BUF_ERROR;
 +    do {
 +        put = Buf_size - s->bi_valid;
 +        if (put > bits)
 +            put = bits;
 +        s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
 +        s->bi_valid += put;
 +        _tr_flush_bits(s);
 +        value >>= put;
 +        bits -= put;
 +    } while (bits);
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateParams(strm, level, strategy)
 +    z_streamp strm;
 +    int level;
 +    int strategy;
 +{
 +    deflate_state *s;
 +    compress_func func;
 +
 +    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
 +    s = strm->state;
 +
 +#ifdef FASTEST
 +    if (level != 0) level = 1;
 +#else
 +    if (level == Z_DEFAULT_COMPRESSION) level = 6;
 +#endif
 +    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
 +        return Z_STREAM_ERROR;
 +    }
 +    func = configuration_table[s->level].func;
 +
 +    if ((strategy != s->strategy || func != configuration_table[level].func) &&
 +        s->last_flush != -2) {
 +        /* Flush the last buffer: */
 +        int err = deflate(strm, Z_BLOCK);
 +        if (err == Z_STREAM_ERROR)
 +            return err;
 +        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
 +            return Z_BUF_ERROR;
 +    }
 +    if (s->level != level) {
 +        if (s->level == 0 && s->matches != 0) {
 +            if (s->matches == 1)
 +                slide_hash(s);
 +            else
 +                CLEAR_HASH(s);
 +            s->matches = 0;
 +        }
 +        s->level = level;
 +        s->max_lazy_match   = configuration_table[level].max_lazy;
 +        s->good_match       = configuration_table[level].good_length;
 +        s->nice_match       = configuration_table[level].nice_length;
 +        s->max_chain_length = configuration_table[level].max_chain;
 +    }
 +    s->strategy = strategy;
 +    return Z_OK;
 +}
 +
 +/* ========================================================================= */
 +int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
 +    z_streamp strm;
*** 3616 LINES SKIPPED ***