xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/pppdump/zlib.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * This file is derived from various .h and .c files from the zlib-0.95
3*7c478bd9Sstevel@tonic-gate  * distribution by Jean-loup Gailly and Mark Adler, with some additions
4*7c478bd9Sstevel@tonic-gate  * by Paul Mackerras to aid in implementing Deflate compression and
5*7c478bd9Sstevel@tonic-gate  * decompression for PPP packets.  See zlib.h for conditions of
6*7c478bd9Sstevel@tonic-gate  * distribution and use.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * Changes that have been made include:
9*7c478bd9Sstevel@tonic-gate  * - changed functions not used outside this file to "local"
10*7c478bd9Sstevel@tonic-gate  * - added minCompression parameter to deflateInit2
11*7c478bd9Sstevel@tonic-gate  * - added Z_PACKET_FLUSH (see zlib.h for details)
12*7c478bd9Sstevel@tonic-gate  * - added inflateIncomp
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * $Id: zlib.c,v 1.2 1999/04/01 07:26:30 paulus Exp $
15*7c478bd9Sstevel@tonic-gate  */
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate /*+++++*/
19*7c478bd9Sstevel@tonic-gate /* zutil.h -- internal interface and configuration of the compression library
20*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Jean-loup Gailly.
21*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
22*7c478bd9Sstevel@tonic-gate  */
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
25*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
26*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #define _Z_UTIL_H
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "zlib.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef STDC
36*7c478bd9Sstevel@tonic-gate #  include <string.h>
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #ifndef local
40*7c478bd9Sstevel@tonic-gate #  define local static
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate /* compile with -Dlocal if your debugger can't find static symbols */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define FAR
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate typedef unsigned char  uch;
47*7c478bd9Sstevel@tonic-gate typedef uch FAR uchf;
48*7c478bd9Sstevel@tonic-gate typedef unsigned short ush;
49*7c478bd9Sstevel@tonic-gate typedef ush FAR ushf;
50*7c478bd9Sstevel@tonic-gate typedef unsigned long  ulg;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate extern char *z_errmsg[]; /* indexed by 1-zlib_error */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
55*7c478bd9Sstevel@tonic-gate /* To be used only when the state is known to be valid */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #ifndef NULL
58*7c478bd9Sstevel@tonic-gate #define NULL	((void *) 0)
59*7c478bd9Sstevel@tonic-gate #endif
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate         /* common constants */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #define DEFLATED   8
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #ifndef DEF_WBITS
66*7c478bd9Sstevel@tonic-gate #  define DEF_WBITS MAX_WBITS
67*7c478bd9Sstevel@tonic-gate #endif
68*7c478bd9Sstevel@tonic-gate /* default windowBits for decompression. MAX_WBITS is for compression only */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #if MAX_MEM_LEVEL >= 8
71*7c478bd9Sstevel@tonic-gate #  define DEF_MEM_LEVEL 8
72*7c478bd9Sstevel@tonic-gate #else
73*7c478bd9Sstevel@tonic-gate #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate /* default memLevel */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate #define STORED_BLOCK 0
78*7c478bd9Sstevel@tonic-gate #define STATIC_TREES 1
79*7c478bd9Sstevel@tonic-gate #define DYN_TREES    2
80*7c478bd9Sstevel@tonic-gate /* The three kinds of block type */
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate #define MIN_MATCH  3
83*7c478bd9Sstevel@tonic-gate #define MAX_MATCH  258
84*7c478bd9Sstevel@tonic-gate /* The minimum and maximum match lengths */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate          /* functions */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
89*7c478bd9Sstevel@tonic-gate #  define HAVE_MEMCPY
90*7c478bd9Sstevel@tonic-gate #endif
91*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMCPY
92*7c478bd9Sstevel@tonic-gate #  define zmemcpy memcpy
93*7c478bd9Sstevel@tonic-gate #  define zmemzero(dest, len) memset(dest, 0, len)
94*7c478bd9Sstevel@tonic-gate #else
95*7c478bd9Sstevel@tonic-gate #  define zmemcpy(d, s, n)	bcopy((s), (d), (n))
96*7c478bd9Sstevel@tonic-gate #  define zmemzero		bzero
97*7c478bd9Sstevel@tonic-gate #endif
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /* Diagnostic functions */
100*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
101*7c478bd9Sstevel@tonic-gate #  include <stdio.h>
102*7c478bd9Sstevel@tonic-gate #  ifndef verbose
103*7c478bd9Sstevel@tonic-gate #    define verbose 0
104*7c478bd9Sstevel@tonic-gate #  endif
105*7c478bd9Sstevel@tonic-gate #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
106*7c478bd9Sstevel@tonic-gate #  define Trace(x) fprintf x
107*7c478bd9Sstevel@tonic-gate #  define Tracev(x) {if (verbose) fprintf x ;}
108*7c478bd9Sstevel@tonic-gate #  define Tracevv(x) {if (verbose>1) fprintf x ;}
109*7c478bd9Sstevel@tonic-gate #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
110*7c478bd9Sstevel@tonic-gate #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
111*7c478bd9Sstevel@tonic-gate #else
112*7c478bd9Sstevel@tonic-gate #  define Assert(cond,msg)
113*7c478bd9Sstevel@tonic-gate #  define Trace(x)
114*7c478bd9Sstevel@tonic-gate #  define Tracev(x)
115*7c478bd9Sstevel@tonic-gate #  define Tracevv(x)
116*7c478bd9Sstevel@tonic-gate #  define Tracec(c,x)
117*7c478bd9Sstevel@tonic-gate #  define Tracecv(c,x)
118*7c478bd9Sstevel@tonic-gate #endif
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
124*7c478bd9Sstevel@tonic-gate /* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate #define ZALLOC(strm, items, size) \
127*7c478bd9Sstevel@tonic-gate            (*((strm)->zalloc))((strm)->opaque, (items), (size))
128*7c478bd9Sstevel@tonic-gate #define ZFREE(strm, addr, size)	\
129*7c478bd9Sstevel@tonic-gate 	   (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
130*7c478bd9Sstevel@tonic-gate #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /* deflate.h -- internal compression state
133*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Jean-loup Gailly
134*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
138*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
139*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*+++++*/
144*7c478bd9Sstevel@tonic-gate /* From: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate /* ===========================================================================
147*7c478bd9Sstevel@tonic-gate  * Internal compression state.
148*7c478bd9Sstevel@tonic-gate  */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /* Data type */
151*7c478bd9Sstevel@tonic-gate #define BINARY  0
152*7c478bd9Sstevel@tonic-gate #define ASCII   1
153*7c478bd9Sstevel@tonic-gate #define UNKNOWN 2
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate #define LENGTH_CODES 29
156*7c478bd9Sstevel@tonic-gate /* number of length codes, not counting the special END_BLOCK code */
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate #define LITERALS  256
159*7c478bd9Sstevel@tonic-gate /* number of literal bytes 0..255 */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate #define L_CODES (LITERALS+1+LENGTH_CODES)
162*7c478bd9Sstevel@tonic-gate /* number of Literal or Length codes, including the END_BLOCK code */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate #define D_CODES   30
165*7c478bd9Sstevel@tonic-gate /* number of distance codes */
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #define BL_CODES  19
168*7c478bd9Sstevel@tonic-gate /* number of codes used to transfer the bit lengths */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate #define HEAP_SIZE (2*L_CODES+1)
171*7c478bd9Sstevel@tonic-gate /* maximum heap size */
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate #define MAX_BITS 15
174*7c478bd9Sstevel@tonic-gate /* All codes must not exceed MAX_BITS bits */
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate #define INIT_STATE    42
177*7c478bd9Sstevel@tonic-gate #define BUSY_STATE   113
178*7c478bd9Sstevel@tonic-gate #define FLUSH_STATE  124
179*7c478bd9Sstevel@tonic-gate #define FINISH_STATE 666
180*7c478bd9Sstevel@tonic-gate /* Stream status */
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /* Data structure describing a single value and its code string. */
184*7c478bd9Sstevel@tonic-gate typedef struct ct_data_s {
185*7c478bd9Sstevel@tonic-gate     union {
186*7c478bd9Sstevel@tonic-gate         ush  freq;       /* frequency count */
187*7c478bd9Sstevel@tonic-gate         ush  code;       /* bit string */
188*7c478bd9Sstevel@tonic-gate     } fc;
189*7c478bd9Sstevel@tonic-gate     union {
190*7c478bd9Sstevel@tonic-gate         ush  dad;        /* father node in Huffman tree */
191*7c478bd9Sstevel@tonic-gate         ush  len;        /* length of bit string */
192*7c478bd9Sstevel@tonic-gate     } dl;
193*7c478bd9Sstevel@tonic-gate } FAR ct_data;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate #define Freq fc.freq
196*7c478bd9Sstevel@tonic-gate #define Code fc.code
197*7c478bd9Sstevel@tonic-gate #define Dad  dl.dad
198*7c478bd9Sstevel@tonic-gate #define Len  dl.len
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate typedef struct static_tree_desc_s  static_tree_desc;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate typedef struct tree_desc_s {
203*7c478bd9Sstevel@tonic-gate     ct_data *dyn_tree;           /* the dynamic tree */
204*7c478bd9Sstevel@tonic-gate     int     max_code;            /* largest code with non zero frequency */
205*7c478bd9Sstevel@tonic-gate     static_tree_desc *stat_desc; /* the corresponding static tree */
206*7c478bd9Sstevel@tonic-gate } FAR tree_desc;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate typedef ush Pos;
209*7c478bd9Sstevel@tonic-gate typedef Pos FAR Posf;
210*7c478bd9Sstevel@tonic-gate typedef unsigned IPos;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /* A Pos is an index in the character window. We use short instead of int to
213*7c478bd9Sstevel@tonic-gate  * save space in the various tables. IPos is used only for parameter passing.
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate typedef struct deflate_state {
217*7c478bd9Sstevel@tonic-gate     z_stream *strm;      /* pointer back to this zlib stream */
218*7c478bd9Sstevel@tonic-gate     int   status;        /* as the name implies */
219*7c478bd9Sstevel@tonic-gate     Bytef *pending_buf;  /* output still pending */
220*7c478bd9Sstevel@tonic-gate     Bytef *pending_out;  /* next pending byte to output to the stream */
221*7c478bd9Sstevel@tonic-gate     int   pending;       /* nb of bytes in the pending buffer */
222*7c478bd9Sstevel@tonic-gate     uLong adler;         /* adler32 of uncompressed data */
223*7c478bd9Sstevel@tonic-gate     int   noheader;      /* suppress zlib header and adler32 */
224*7c478bd9Sstevel@tonic-gate     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
225*7c478bd9Sstevel@tonic-gate     Byte  method;        /* STORED (for zip only) or DEFLATED */
226*7c478bd9Sstevel@tonic-gate     int	  minCompr;	 /* min size decrease for Z_FLUSH_NOSTORE */
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate                 /* used by deflate.c: */
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate     uInt  w_size;        /* LZ77 window size (32K by default) */
231*7c478bd9Sstevel@tonic-gate     uInt  w_bits;        /* log2(w_size)  (8..16) */
232*7c478bd9Sstevel@tonic-gate     uInt  w_mask;        /* w_size - 1 */
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate     Bytef *window;
235*7c478bd9Sstevel@tonic-gate     /* Sliding window. Input bytes are read into the second half of the window,
236*7c478bd9Sstevel@tonic-gate      * and move to the first half later to keep a dictionary of at least wSize
237*7c478bd9Sstevel@tonic-gate      * bytes. With this organization, matches are limited to a distance of
238*7c478bd9Sstevel@tonic-gate      * wSize-MAX_MATCH bytes, but this ensures that IO is always
239*7c478bd9Sstevel@tonic-gate      * performed with a length multiple of the block size. Also, it limits
240*7c478bd9Sstevel@tonic-gate      * the window size to 64K, which is quite useful on MSDOS.
241*7c478bd9Sstevel@tonic-gate      * To do: use the user input buffer as sliding window.
242*7c478bd9Sstevel@tonic-gate      */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate     ulg window_size;
245*7c478bd9Sstevel@tonic-gate     /* Actual size of window: 2*wSize, except when the user input buffer
246*7c478bd9Sstevel@tonic-gate      * is directly used as sliding window.
247*7c478bd9Sstevel@tonic-gate      */
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate     Posf *prev;
250*7c478bd9Sstevel@tonic-gate     /* Link to older string with same hash index. To limit the size of this
251*7c478bd9Sstevel@tonic-gate      * array to 64K, this link is maintained only for the last 32K strings.
252*7c478bd9Sstevel@tonic-gate      * An index in this array is thus a window index modulo 32K.
253*7c478bd9Sstevel@tonic-gate      */
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate     Posf *head; /* Heads of the hash chains or NIL. */
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate     uInt  ins_h;          /* hash index of string to be inserted */
258*7c478bd9Sstevel@tonic-gate     uInt  hash_size;      /* number of elements in hash table */
259*7c478bd9Sstevel@tonic-gate     uInt  hash_bits;      /* log2(hash_size) */
260*7c478bd9Sstevel@tonic-gate     uInt  hash_mask;      /* hash_size-1 */
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate     uInt  hash_shift;
263*7c478bd9Sstevel@tonic-gate     /* Number of bits by which ins_h must be shifted at each input
264*7c478bd9Sstevel@tonic-gate      * step. It must be such that after MIN_MATCH steps, the oldest
265*7c478bd9Sstevel@tonic-gate      * byte no longer takes part in the hash key, that is:
266*7c478bd9Sstevel@tonic-gate      *   hash_shift * MIN_MATCH >= hash_bits
267*7c478bd9Sstevel@tonic-gate      */
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate     long block_start;
270*7c478bd9Sstevel@tonic-gate     /* Window position at the beginning of the current output block. Gets
271*7c478bd9Sstevel@tonic-gate      * negative when the window is moved backwards.
272*7c478bd9Sstevel@tonic-gate      */
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate     uInt match_length;           /* length of best match */
275*7c478bd9Sstevel@tonic-gate     IPos prev_match;             /* previous match */
276*7c478bd9Sstevel@tonic-gate     int match_available;         /* set if previous match exists */
277*7c478bd9Sstevel@tonic-gate     uInt strstart;               /* start of string to insert */
278*7c478bd9Sstevel@tonic-gate     uInt match_start;            /* start of matching string */
279*7c478bd9Sstevel@tonic-gate     uInt lookahead;              /* number of valid bytes ahead in window */
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate     uInt prev_length;
282*7c478bd9Sstevel@tonic-gate     /* Length of the best match at previous step. Matches not greater than this
283*7c478bd9Sstevel@tonic-gate      * are discarded. This is used in the lazy match evaluation.
284*7c478bd9Sstevel@tonic-gate      */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate     uInt max_chain_length;
287*7c478bd9Sstevel@tonic-gate     /* To speed up deflation, hash chains are never searched beyond this
288*7c478bd9Sstevel@tonic-gate      * length.  A higher limit improves compression ratio but degrades the
289*7c478bd9Sstevel@tonic-gate      * speed.
290*7c478bd9Sstevel@tonic-gate      */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate     uInt max_lazy_match;
293*7c478bd9Sstevel@tonic-gate     /* Attempt to find a better match only when the current match is strictly
294*7c478bd9Sstevel@tonic-gate      * smaller than this value. This mechanism is used only for compression
295*7c478bd9Sstevel@tonic-gate      * levels >= 4.
296*7c478bd9Sstevel@tonic-gate      */
297*7c478bd9Sstevel@tonic-gate #   define max_insert_length  max_lazy_match
298*7c478bd9Sstevel@tonic-gate     /* Insert new strings in the hash table only if the match length is not
299*7c478bd9Sstevel@tonic-gate      * greater than this length. This saves time but degrades compression.
300*7c478bd9Sstevel@tonic-gate      * max_insert_length is used only for compression levels <= 3.
301*7c478bd9Sstevel@tonic-gate      */
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate     int level;    /* compression level (1..9) */
304*7c478bd9Sstevel@tonic-gate     int strategy; /* favor or force Huffman coding*/
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate     uInt good_match;
307*7c478bd9Sstevel@tonic-gate     /* Use a faster search when the previous match is longer than this */
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate      int nice_match; /* Stop searching when current match exceeds this */
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate                 /* used by trees.c: */
312*7c478bd9Sstevel@tonic-gate     /* Didn't use ct_data typedef below to supress compiler warning */
313*7c478bd9Sstevel@tonic-gate     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
314*7c478bd9Sstevel@tonic-gate     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
315*7c478bd9Sstevel@tonic-gate     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate     struct tree_desc_s l_desc;               /* desc. for literal tree */
318*7c478bd9Sstevel@tonic-gate     struct tree_desc_s d_desc;               /* desc. for distance tree */
319*7c478bd9Sstevel@tonic-gate     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate     ush bl_count[MAX_BITS+1];
322*7c478bd9Sstevel@tonic-gate     /* number of codes at each bit length for an optimal tree */
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
325*7c478bd9Sstevel@tonic-gate     int heap_len;               /* number of elements in the heap */
326*7c478bd9Sstevel@tonic-gate     int heap_max;               /* element of largest frequency */
327*7c478bd9Sstevel@tonic-gate     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
328*7c478bd9Sstevel@tonic-gate      * The same heap array is used to build all trees.
329*7c478bd9Sstevel@tonic-gate      */
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate     uch depth[2*L_CODES+1];
332*7c478bd9Sstevel@tonic-gate     /* Depth of each subtree used as tie breaker for trees of equal frequency
333*7c478bd9Sstevel@tonic-gate      */
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate     uchf *l_buf;          /* buffer for literals or lengths */
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate     uInt  lit_bufsize;
338*7c478bd9Sstevel@tonic-gate     /* Size of match buffer for literals/lengths.  There are 4 reasons for
339*7c478bd9Sstevel@tonic-gate      * limiting lit_bufsize to 64K:
340*7c478bd9Sstevel@tonic-gate      *   - frequencies can be kept in 16 bit counters
341*7c478bd9Sstevel@tonic-gate      *   - if compression is not successful for the first block, all input
342*7c478bd9Sstevel@tonic-gate      *     data is still in the window so we can still emit a stored block even
343*7c478bd9Sstevel@tonic-gate      *     when input comes from standard input.  (This can also be done for
344*7c478bd9Sstevel@tonic-gate      *     all blocks if lit_bufsize is not greater than 32K.)
345*7c478bd9Sstevel@tonic-gate      *   - if compression is not successful for a file smaller than 64K, we can
346*7c478bd9Sstevel@tonic-gate      *     even emit a stored file instead of a stored block (saving 5 bytes).
347*7c478bd9Sstevel@tonic-gate      *     This is applicable only for zip (not gzip or zlib).
348*7c478bd9Sstevel@tonic-gate      *   - creating new Huffman trees less frequently may not provide fast
349*7c478bd9Sstevel@tonic-gate      *     adaptation to changes in the input data statistics. (Take for
350*7c478bd9Sstevel@tonic-gate      *     example a binary file with poorly compressible code followed by
351*7c478bd9Sstevel@tonic-gate      *     a highly compressible string table.) Smaller buffer sizes give
352*7c478bd9Sstevel@tonic-gate      *     fast adaptation but have of course the overhead of transmitting
353*7c478bd9Sstevel@tonic-gate      *     trees more frequently.
354*7c478bd9Sstevel@tonic-gate      *   - I can't count above 4
355*7c478bd9Sstevel@tonic-gate      */
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate     uInt last_lit;      /* running index in l_buf */
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate     ushf *d_buf;
360*7c478bd9Sstevel@tonic-gate     /* Buffer for distances. To simplify the code, d_buf and l_buf have
361*7c478bd9Sstevel@tonic-gate      * the same number of elements. To use different lengths, an extra flag
362*7c478bd9Sstevel@tonic-gate      * array would be necessary.
363*7c478bd9Sstevel@tonic-gate      */
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate     ulg opt_len;        /* bit length of current block with optimal trees */
366*7c478bd9Sstevel@tonic-gate     ulg static_len;     /* bit length of current block with static trees */
367*7c478bd9Sstevel@tonic-gate     ulg compressed_len; /* total bit length of compressed file */
368*7c478bd9Sstevel@tonic-gate     uInt matches;       /* number of string matches in current block */
369*7c478bd9Sstevel@tonic-gate     int last_eob_len;   /* bit length of EOB code for last block */
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
372*7c478bd9Sstevel@tonic-gate     ulg bits_sent;      /* bit length of the compressed data */
373*7c478bd9Sstevel@tonic-gate #endif
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate     ush bi_buf;
376*7c478bd9Sstevel@tonic-gate     /* Output buffer. bits are inserted starting at the bottom (least
377*7c478bd9Sstevel@tonic-gate      * significant bits).
378*7c478bd9Sstevel@tonic-gate      */
379*7c478bd9Sstevel@tonic-gate     int bi_valid;
380*7c478bd9Sstevel@tonic-gate     /* Number of valid bits in bi_buf.  All bits above the last valid bit
381*7c478bd9Sstevel@tonic-gate      * are always zero.
382*7c478bd9Sstevel@tonic-gate      */
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate     uInt blocks_in_packet;
385*7c478bd9Sstevel@tonic-gate     /* Number of blocks produced since the last time Z_PACKET_FLUSH
386*7c478bd9Sstevel@tonic-gate      * was used.
387*7c478bd9Sstevel@tonic-gate      */
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate } FAR deflate_state;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate /* Output a byte on the stream.
392*7c478bd9Sstevel@tonic-gate  * IN assertion: there is enough room in pending_buf.
393*7c478bd9Sstevel@tonic-gate  */
394*7c478bd9Sstevel@tonic-gate #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
398*7c478bd9Sstevel@tonic-gate /* Minimum amount of lookahead, except at the end of the input file.
399*7c478bd9Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
400*7c478bd9Sstevel@tonic-gate  */
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
403*7c478bd9Sstevel@tonic-gate /* In order to simplify the code, particularly on 16 bit machines, match
404*7c478bd9Sstevel@tonic-gate  * distances are limited to MAX_DIST instead of WSIZE.
405*7c478bd9Sstevel@tonic-gate  */
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate         /* in trees.c */
408*7c478bd9Sstevel@tonic-gate local void ct_init       OF((deflate_state *s));
409*7c478bd9Sstevel@tonic-gate local int  ct_tally      OF((deflate_state *s, int dist, int lc));
410*7c478bd9Sstevel@tonic-gate local ulg ct_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
411*7c478bd9Sstevel@tonic-gate 			     int flush));
412*7c478bd9Sstevel@tonic-gate local void ct_align      OF((deflate_state *s));
413*7c478bd9Sstevel@tonic-gate local void ct_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
414*7c478bd9Sstevel@tonic-gate                           int eof));
415*7c478bd9Sstevel@tonic-gate local void ct_stored_type_only OF((deflate_state *s));
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate /*+++++*/
419*7c478bd9Sstevel@tonic-gate /* deflate.c -- compress data using the deflation algorithm
420*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Jean-loup Gailly.
421*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
422*7c478bd9Sstevel@tonic-gate  */
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /*
425*7c478bd9Sstevel@tonic-gate  *  ALGORITHM
426*7c478bd9Sstevel@tonic-gate  *
427*7c478bd9Sstevel@tonic-gate  *      The "deflation" process depends on being able to identify portions
428*7c478bd9Sstevel@tonic-gate  *      of the input text which are identical to earlier input (within a
429*7c478bd9Sstevel@tonic-gate  *      sliding window trailing behind the input currently being processed).
430*7c478bd9Sstevel@tonic-gate  *
431*7c478bd9Sstevel@tonic-gate  *      The most straightforward technique turns out to be the fastest for
432*7c478bd9Sstevel@tonic-gate  *      most input files: try all possible matches and select the longest.
433*7c478bd9Sstevel@tonic-gate  *      The key feature of this algorithm is that insertions into the string
434*7c478bd9Sstevel@tonic-gate  *      dictionary are very simple and thus fast, and deletions are avoided
435*7c478bd9Sstevel@tonic-gate  *      completely. Insertions are performed at each input character, whereas
436*7c478bd9Sstevel@tonic-gate  *      string matches are performed only when the previous match ends. So it
437*7c478bd9Sstevel@tonic-gate  *      is preferable to spend more time in matches to allow very fast string
438*7c478bd9Sstevel@tonic-gate  *      insertions and avoid deletions. The matching algorithm for small
439*7c478bd9Sstevel@tonic-gate  *      strings is inspired from that of Rabin & Karp. A brute force approach
440*7c478bd9Sstevel@tonic-gate  *      is used to find longer strings when a small match has been found.
441*7c478bd9Sstevel@tonic-gate  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
442*7c478bd9Sstevel@tonic-gate  *      (by Leonid Broukhis).
443*7c478bd9Sstevel@tonic-gate  *         A previous version of this file used a more sophisticated algorithm
444*7c478bd9Sstevel@tonic-gate  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
445*7c478bd9Sstevel@tonic-gate  *      time, but has a larger average cost, uses more memory and is patented.
446*7c478bd9Sstevel@tonic-gate  *      However the F&G algorithm may be faster for some highly redundant
447*7c478bd9Sstevel@tonic-gate  *      files if the parameter max_chain_length (described below) is too large.
448*7c478bd9Sstevel@tonic-gate  *
449*7c478bd9Sstevel@tonic-gate  *  ACKNOWLEDGEMENTS
450*7c478bd9Sstevel@tonic-gate  *
451*7c478bd9Sstevel@tonic-gate  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
452*7c478bd9Sstevel@tonic-gate  *      I found it in 'freeze' written by Leonid Broukhis.
453*7c478bd9Sstevel@tonic-gate  *      Thanks to many people for bug reports and testing.
454*7c478bd9Sstevel@tonic-gate  *
455*7c478bd9Sstevel@tonic-gate  *  REFERENCES
456*7c478bd9Sstevel@tonic-gate  *
457*7c478bd9Sstevel@tonic-gate  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
458*7c478bd9Sstevel@tonic-gate  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
459*7c478bd9Sstevel@tonic-gate  *
460*7c478bd9Sstevel@tonic-gate  *      A description of the Rabin and Karp algorithm is given in the book
461*7c478bd9Sstevel@tonic-gate  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
462*7c478bd9Sstevel@tonic-gate  *
463*7c478bd9Sstevel@tonic-gate  *      Fiala,E.R., and Greene,D.H.
464*7c478bd9Sstevel@tonic-gate  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
465*7c478bd9Sstevel@tonic-gate  *
466*7c478bd9Sstevel@tonic-gate  */
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate /* From: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp */
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate local char zlib_copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
471*7c478bd9Sstevel@tonic-gate /*
472*7c478bd9Sstevel@tonic-gate   If you use the zlib library in a product, an acknowledgment is welcome
473*7c478bd9Sstevel@tonic-gate   in the documentation of your product. If for some reason you cannot
474*7c478bd9Sstevel@tonic-gate   include such an acknowledgment, I would appreciate that you keep this
475*7c478bd9Sstevel@tonic-gate   copyright string in the executable of your product.
476*7c478bd9Sstevel@tonic-gate  */
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate #define NIL 0
479*7c478bd9Sstevel@tonic-gate /* Tail of hash chains */
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate #ifndef TOO_FAR
482*7c478bd9Sstevel@tonic-gate #  define TOO_FAR 4096
483*7c478bd9Sstevel@tonic-gate #endif
484*7c478bd9Sstevel@tonic-gate /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
487*7c478bd9Sstevel@tonic-gate /* Minimum amount of lookahead, except at the end of the input file.
488*7c478bd9Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
489*7c478bd9Sstevel@tonic-gate  */
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate /* Values for max_lazy_match, good_match and max_chain_length, depending on
492*7c478bd9Sstevel@tonic-gate  * the desired pack level (0..9). The values given below have been tuned to
493*7c478bd9Sstevel@tonic-gate  * exclude worst case performance for pathological files. Better values may be
494*7c478bd9Sstevel@tonic-gate  * found for specific files.
495*7c478bd9Sstevel@tonic-gate  */
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate typedef struct config_s {
498*7c478bd9Sstevel@tonic-gate    ush good_length; /* reduce lazy search above this match length */
499*7c478bd9Sstevel@tonic-gate    ush max_lazy;    /* do not perform lazy search above this match length */
500*7c478bd9Sstevel@tonic-gate    ush nice_length; /* quit search above this match length */
501*7c478bd9Sstevel@tonic-gate    ush max_chain;
502*7c478bd9Sstevel@tonic-gate } config;
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate local config configuration_table[10] = {
505*7c478bd9Sstevel@tonic-gate /*      good lazy nice chain */
506*7c478bd9Sstevel@tonic-gate /* 0 */ {0,    0,  0,    0},  /* store only */
507*7c478bd9Sstevel@tonic-gate /* 1 */ {4,    4,  8,    4},  /* maximum speed, no lazy matches */
508*7c478bd9Sstevel@tonic-gate /* 2 */ {4,    5, 16,    8},
509*7c478bd9Sstevel@tonic-gate /* 3 */ {4,    6, 32,   32},
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate /* 4 */ {4,    4, 16,   16},  /* lazy matches */
512*7c478bd9Sstevel@tonic-gate /* 5 */ {8,   16, 32,   32},
513*7c478bd9Sstevel@tonic-gate /* 6 */ {8,   16, 128, 128},
514*7c478bd9Sstevel@tonic-gate /* 7 */ {8,   32, 128, 256},
515*7c478bd9Sstevel@tonic-gate /* 8 */ {32, 128, 258, 1024},
516*7c478bd9Sstevel@tonic-gate /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
519*7c478bd9Sstevel@tonic-gate  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
520*7c478bd9Sstevel@tonic-gate  * meaning.
521*7c478bd9Sstevel@tonic-gate  */
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate #define EQUAL 0
524*7c478bd9Sstevel@tonic-gate /* result of memcmp for equal strings */
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate /* ===========================================================================
527*7c478bd9Sstevel@tonic-gate  *  Prototypes for local functions.
528*7c478bd9Sstevel@tonic-gate  */
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate local void fill_window   OF((deflate_state *s));
531*7c478bd9Sstevel@tonic-gate local int  deflate_fast  OF((deflate_state *s, int flush));
532*7c478bd9Sstevel@tonic-gate local int  deflate_slow  OF((deflate_state *s, int flush));
533*7c478bd9Sstevel@tonic-gate local void lm_init       OF((deflate_state *s));
534*7c478bd9Sstevel@tonic-gate local int longest_match  OF((deflate_state *s, IPos cur_match));
535*7c478bd9Sstevel@tonic-gate local void putShortMSB   OF((deflate_state *s, uInt b));
536*7c478bd9Sstevel@tonic-gate local void flush_pending OF((z_stream *strm));
537*7c478bd9Sstevel@tonic-gate local int read_buf       OF((z_stream *strm, charf *buf, unsigned size));
538*7c478bd9Sstevel@tonic-gate #ifdef ASMV
539*7c478bd9Sstevel@tonic-gate       void match_init OF((void)); /* asm code initialization */
540*7c478bd9Sstevel@tonic-gate #endif
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
543*7c478bd9Sstevel@tonic-gate local  void check_match OF((deflate_state *s, IPos start, IPos match,
544*7c478bd9Sstevel@tonic-gate                             int length));
545*7c478bd9Sstevel@tonic-gate #endif
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate /* ===========================================================================
549*7c478bd9Sstevel@tonic-gate  * Update a hash value with the given input byte
550*7c478bd9Sstevel@tonic-gate  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
551*7c478bd9Sstevel@tonic-gate  *    input characters, so that a running hash key can be computed from the
552*7c478bd9Sstevel@tonic-gate  *    previous key instead of complete recalculation each time.
553*7c478bd9Sstevel@tonic-gate  */
554*7c478bd9Sstevel@tonic-gate #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate /* ===========================================================================
558*7c478bd9Sstevel@tonic-gate  * Insert string str in the dictionary and set match_head to the previous head
559*7c478bd9Sstevel@tonic-gate  * of the hash chain (the most recent string with same hash key). Return
560*7c478bd9Sstevel@tonic-gate  * the previous length of the hash chain.
561*7c478bd9Sstevel@tonic-gate  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
562*7c478bd9Sstevel@tonic-gate  *    input characters and the first MIN_MATCH bytes of str are valid
563*7c478bd9Sstevel@tonic-gate  *    (except for the last MIN_MATCH-1 bytes of the input file).
564*7c478bd9Sstevel@tonic-gate  */
565*7c478bd9Sstevel@tonic-gate #define INSERT_STRING(s, str, match_head) \
566*7c478bd9Sstevel@tonic-gate    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
567*7c478bd9Sstevel@tonic-gate     s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
568*7c478bd9Sstevel@tonic-gate     s->head[s->ins_h] = (str))
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate /* ===========================================================================
571*7c478bd9Sstevel@tonic-gate  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
572*7c478bd9Sstevel@tonic-gate  * prev[] will be initialized on the fly.
573*7c478bd9Sstevel@tonic-gate  */
574*7c478bd9Sstevel@tonic-gate #define CLEAR_HASH(s) \
575*7c478bd9Sstevel@tonic-gate     s->head[s->hash_size-1] = NIL; \
576*7c478bd9Sstevel@tonic-gate     zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
579*7c478bd9Sstevel@tonic-gate int deflateInit (strm, level)
580*7c478bd9Sstevel@tonic-gate     z_stream *strm;
581*7c478bd9Sstevel@tonic-gate     int level;
582*7c478bd9Sstevel@tonic-gate {
583*7c478bd9Sstevel@tonic-gate     return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
584*7c478bd9Sstevel@tonic-gate 			 0, 0);
585*7c478bd9Sstevel@tonic-gate     /* To do: ignore strm->next_in if we use it as window */
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
589*7c478bd9Sstevel@tonic-gate int deflateInit2 (strm, level, method, windowBits, memLevel,
590*7c478bd9Sstevel@tonic-gate 		  strategy, minCompression)
591*7c478bd9Sstevel@tonic-gate     z_stream *strm;
592*7c478bd9Sstevel@tonic-gate     int  level;
593*7c478bd9Sstevel@tonic-gate     int  method;
594*7c478bd9Sstevel@tonic-gate     int  windowBits;
595*7c478bd9Sstevel@tonic-gate     int  memLevel;
596*7c478bd9Sstevel@tonic-gate     int  strategy;
597*7c478bd9Sstevel@tonic-gate     int  minCompression;
598*7c478bd9Sstevel@tonic-gate {
599*7c478bd9Sstevel@tonic-gate     deflate_state *s;
600*7c478bd9Sstevel@tonic-gate     int noheader = 0;
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate     if (strm == Z_NULL) return Z_STREAM_ERROR;
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate     strm->msg = Z_NULL;
605*7c478bd9Sstevel@tonic-gate /*    if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; */
606*7c478bd9Sstevel@tonic-gate /*    if (strm->zfree == Z_NULL) strm->zfree = zcfree; */
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate     if (level == Z_DEFAULT_COMPRESSION) level = 6;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate     if (windowBits < 0) { /* undocumented feature: suppress zlib header */
611*7c478bd9Sstevel@tonic-gate         noheader = 1;
612*7c478bd9Sstevel@tonic-gate         windowBits = -windowBits;
613*7c478bd9Sstevel@tonic-gate     }
614*7c478bd9Sstevel@tonic-gate     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED ||
615*7c478bd9Sstevel@tonic-gate         windowBits < 8 || windowBits > 15 || level < 1 || level > 9) {
616*7c478bd9Sstevel@tonic-gate         return Z_STREAM_ERROR;
617*7c478bd9Sstevel@tonic-gate     }
618*7c478bd9Sstevel@tonic-gate     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
619*7c478bd9Sstevel@tonic-gate     if (s == Z_NULL) return Z_MEM_ERROR;
620*7c478bd9Sstevel@tonic-gate     strm->state = (struct internal_state FAR *)s;
621*7c478bd9Sstevel@tonic-gate     s->strm = strm;
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate     s->noheader = noheader;
624*7c478bd9Sstevel@tonic-gate     s->w_bits = windowBits;
625*7c478bd9Sstevel@tonic-gate     s->w_size = 1 << s->w_bits;
626*7c478bd9Sstevel@tonic-gate     s->w_mask = s->w_size - 1;
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate     s->hash_bits = memLevel + 7;
629*7c478bd9Sstevel@tonic-gate     s->hash_size = 1 << s->hash_bits;
630*7c478bd9Sstevel@tonic-gate     s->hash_mask = s->hash_size - 1;
631*7c478bd9Sstevel@tonic-gate     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate     s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
634*7c478bd9Sstevel@tonic-gate     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
635*7c478bd9Sstevel@tonic-gate     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate     s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush));
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
642*7c478bd9Sstevel@tonic-gate         s->pending_buf == Z_NULL) {
643*7c478bd9Sstevel@tonic-gate         strm->msg = z_errmsg[1-Z_MEM_ERROR];
644*7c478bd9Sstevel@tonic-gate         deflateEnd (strm);
645*7c478bd9Sstevel@tonic-gate         return Z_MEM_ERROR;
646*7c478bd9Sstevel@tonic-gate     }
647*7c478bd9Sstevel@tonic-gate     s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]);
648*7c478bd9Sstevel@tonic-gate     s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]);
649*7c478bd9Sstevel@tonic-gate     /* We overlay pending_buf and d_buf+l_buf. This works since the average
650*7c478bd9Sstevel@tonic-gate      * output size for (length,distance) codes is <= 32 bits (worst case
651*7c478bd9Sstevel@tonic-gate      * is 15+15+13=33).
652*7c478bd9Sstevel@tonic-gate      */
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate     s->level = level;
655*7c478bd9Sstevel@tonic-gate     s->strategy = strategy;
656*7c478bd9Sstevel@tonic-gate     s->method = (Byte)method;
657*7c478bd9Sstevel@tonic-gate     s->minCompr = minCompression;
658*7c478bd9Sstevel@tonic-gate     s->blocks_in_packet = 0;
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate     return deflateReset(strm);
661*7c478bd9Sstevel@tonic-gate }
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
664*7c478bd9Sstevel@tonic-gate int deflateReset (strm)
665*7c478bd9Sstevel@tonic-gate     z_stream *strm;
666*7c478bd9Sstevel@tonic-gate {
667*7c478bd9Sstevel@tonic-gate     deflate_state *s;
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate     if (strm == Z_NULL || strm->state == Z_NULL ||
670*7c478bd9Sstevel@tonic-gate         strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate     strm->total_in = strm->total_out = 0;
673*7c478bd9Sstevel@tonic-gate     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
674*7c478bd9Sstevel@tonic-gate     strm->data_type = Z_UNKNOWN;
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate     s = (deflate_state *)strm->state;
677*7c478bd9Sstevel@tonic-gate     s->pending = 0;
678*7c478bd9Sstevel@tonic-gate     s->pending_out = s->pending_buf;
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate     if (s->noheader < 0) {
681*7c478bd9Sstevel@tonic-gate         s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
682*7c478bd9Sstevel@tonic-gate     }
683*7c478bd9Sstevel@tonic-gate     s->status = s->noheader ? BUSY_STATE : INIT_STATE;
684*7c478bd9Sstevel@tonic-gate     s->adler = 1;
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate     ct_init(s);
687*7c478bd9Sstevel@tonic-gate     lm_init(s);
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate     return Z_OK;
690*7c478bd9Sstevel@tonic-gate }
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate /* =========================================================================
693*7c478bd9Sstevel@tonic-gate  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
694*7c478bd9Sstevel@tonic-gate  * IN assertion: the stream state is correct and there is enough room in
695*7c478bd9Sstevel@tonic-gate  * pending_buf.
696*7c478bd9Sstevel@tonic-gate  */
697*7c478bd9Sstevel@tonic-gate local void putShortMSB (s, b)
698*7c478bd9Sstevel@tonic-gate     deflate_state *s;
699*7c478bd9Sstevel@tonic-gate     uInt b;
700*7c478bd9Sstevel@tonic-gate {
701*7c478bd9Sstevel@tonic-gate     put_byte(s, (Byte)(b >> 8));
702*7c478bd9Sstevel@tonic-gate     put_byte(s, (Byte)(b & 0xff));
703*7c478bd9Sstevel@tonic-gate }
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate /* =========================================================================
706*7c478bd9Sstevel@tonic-gate  * Flush as much pending output as possible.
707*7c478bd9Sstevel@tonic-gate  */
708*7c478bd9Sstevel@tonic-gate local void flush_pending(strm)
709*7c478bd9Sstevel@tonic-gate     z_stream *strm;
710*7c478bd9Sstevel@tonic-gate {
711*7c478bd9Sstevel@tonic-gate     deflate_state *state = (deflate_state *) strm->state;
712*7c478bd9Sstevel@tonic-gate     unsigned len = state->pending;
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate     if (len > strm->avail_out) len = strm->avail_out;
715*7c478bd9Sstevel@tonic-gate     if (len == 0) return;
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate     if (strm->next_out != NULL) {
718*7c478bd9Sstevel@tonic-gate 	zmemcpy(strm->next_out, state->pending_out, len);
719*7c478bd9Sstevel@tonic-gate 	strm->next_out += len;
720*7c478bd9Sstevel@tonic-gate     }
721*7c478bd9Sstevel@tonic-gate     state->pending_out += len;
722*7c478bd9Sstevel@tonic-gate     strm->total_out += len;
723*7c478bd9Sstevel@tonic-gate     strm->avail_out -= len;
724*7c478bd9Sstevel@tonic-gate     state->pending -= len;
725*7c478bd9Sstevel@tonic-gate     if (state->pending == 0) {
726*7c478bd9Sstevel@tonic-gate         state->pending_out = state->pending_buf;
727*7c478bd9Sstevel@tonic-gate     }
728*7c478bd9Sstevel@tonic-gate }
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
731*7c478bd9Sstevel@tonic-gate int deflate (strm, flush)
732*7c478bd9Sstevel@tonic-gate     z_stream *strm;
733*7c478bd9Sstevel@tonic-gate     int flush;
734*7c478bd9Sstevel@tonic-gate {
735*7c478bd9Sstevel@tonic-gate     deflate_state *state = (deflate_state *) strm->state;
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate     if (strm == Z_NULL || state == Z_NULL) return Z_STREAM_ERROR;
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate     if (strm->next_in == Z_NULL && strm->avail_in != 0) {
740*7c478bd9Sstevel@tonic-gate         ERR_RETURN(strm, Z_STREAM_ERROR);
741*7c478bd9Sstevel@tonic-gate     }
742*7c478bd9Sstevel@tonic-gate     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate     state->strm = strm; /* just in case */
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate     /* Write the zlib header */
747*7c478bd9Sstevel@tonic-gate     if (state->status == INIT_STATE) {
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate         uInt header = (DEFLATED + ((state->w_bits-8)<<4)) << 8;
750*7c478bd9Sstevel@tonic-gate         uInt level_flags = (state->level-1) >> 1;
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate         if (level_flags > 3) level_flags = 3;
753*7c478bd9Sstevel@tonic-gate         header |= (level_flags << 6);
754*7c478bd9Sstevel@tonic-gate         header += 31 - (header % 31);
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate         state->status = BUSY_STATE;
757*7c478bd9Sstevel@tonic-gate         putShortMSB(state, header);
758*7c478bd9Sstevel@tonic-gate     }
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate     /* Flush as much pending output as possible */
761*7c478bd9Sstevel@tonic-gate     if (state->pending != 0) {
762*7c478bd9Sstevel@tonic-gate         flush_pending(strm);
763*7c478bd9Sstevel@tonic-gate         if (strm->avail_out == 0) return Z_OK;
764*7c478bd9Sstevel@tonic-gate     }
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate     /* If we came back in here to get the last output from
767*7c478bd9Sstevel@tonic-gate      * a previous flush, we're done for now.
768*7c478bd9Sstevel@tonic-gate      */
769*7c478bd9Sstevel@tonic-gate     if (state->status == FLUSH_STATE) {
770*7c478bd9Sstevel@tonic-gate 	state->status = BUSY_STATE;
771*7c478bd9Sstevel@tonic-gate 	if (flush != Z_NO_FLUSH && flush != Z_FINISH)
772*7c478bd9Sstevel@tonic-gate 	    return Z_OK;
773*7c478bd9Sstevel@tonic-gate     }
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate     /* User must not provide more input after the first FINISH: */
776*7c478bd9Sstevel@tonic-gate     if (state->status == FINISH_STATE && strm->avail_in != 0) {
777*7c478bd9Sstevel@tonic-gate         ERR_RETURN(strm, Z_BUF_ERROR);
778*7c478bd9Sstevel@tonic-gate     }
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate     /* Start a new block or continue the current one.
781*7c478bd9Sstevel@tonic-gate      */
782*7c478bd9Sstevel@tonic-gate     if (strm->avail_in != 0 || state->lookahead != 0 ||
783*7c478bd9Sstevel@tonic-gate         (flush == Z_FINISH && state->status != FINISH_STATE)) {
784*7c478bd9Sstevel@tonic-gate         int quit;
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate         if (flush == Z_FINISH) {
787*7c478bd9Sstevel@tonic-gate             state->status = FINISH_STATE;
788*7c478bd9Sstevel@tonic-gate         }
789*7c478bd9Sstevel@tonic-gate         if (state->level <= 3) {
790*7c478bd9Sstevel@tonic-gate             quit = deflate_fast(state, flush);
791*7c478bd9Sstevel@tonic-gate         } else {
792*7c478bd9Sstevel@tonic-gate             quit = deflate_slow(state, flush);
793*7c478bd9Sstevel@tonic-gate         }
794*7c478bd9Sstevel@tonic-gate         if (quit || strm->avail_out == 0)
795*7c478bd9Sstevel@tonic-gate 	    return Z_OK;
796*7c478bd9Sstevel@tonic-gate         /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
797*7c478bd9Sstevel@tonic-gate          * of deflate should use the same flush parameter to make sure
798*7c478bd9Sstevel@tonic-gate          * that the flush is complete. So we don't have to output an
799*7c478bd9Sstevel@tonic-gate          * empty block here, this will be done at next call. This also
800*7c478bd9Sstevel@tonic-gate          * ensures that for a very small output buffer, we emit at most
801*7c478bd9Sstevel@tonic-gate          * one empty block.
802*7c478bd9Sstevel@tonic-gate          */
803*7c478bd9Sstevel@tonic-gate     }
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate     /* If a flush was requested, we have a little more to output now. */
806*7c478bd9Sstevel@tonic-gate     if (flush != Z_NO_FLUSH && flush != Z_FINISH
807*7c478bd9Sstevel@tonic-gate 	&& state->status != FINISH_STATE) {
808*7c478bd9Sstevel@tonic-gate 	switch (flush) {
809*7c478bd9Sstevel@tonic-gate 	case Z_PARTIAL_FLUSH:
810*7c478bd9Sstevel@tonic-gate 	    ct_align(state);
811*7c478bd9Sstevel@tonic-gate 	    break;
812*7c478bd9Sstevel@tonic-gate 	case Z_PACKET_FLUSH:
813*7c478bd9Sstevel@tonic-gate 	    /* Output just the 3-bit `stored' block type value,
814*7c478bd9Sstevel@tonic-gate 	       but not a zero length. */
815*7c478bd9Sstevel@tonic-gate 	    ct_stored_type_only(state);
816*7c478bd9Sstevel@tonic-gate 	    break;
817*7c478bd9Sstevel@tonic-gate 	default:
818*7c478bd9Sstevel@tonic-gate 	    ct_stored_block(state, (char*)0, 0L, 0);
819*7c478bd9Sstevel@tonic-gate 	    /* For a full flush, this empty block will be recognized
820*7c478bd9Sstevel@tonic-gate 	     * as a special marker by inflate_sync().
821*7c478bd9Sstevel@tonic-gate 	     */
822*7c478bd9Sstevel@tonic-gate 	    if (flush == Z_FULL_FLUSH) {
823*7c478bd9Sstevel@tonic-gate 		CLEAR_HASH(state);             /* forget history */
824*7c478bd9Sstevel@tonic-gate 	    }
825*7c478bd9Sstevel@tonic-gate 	}
826*7c478bd9Sstevel@tonic-gate 	flush_pending(strm);
827*7c478bd9Sstevel@tonic-gate 	if (strm->avail_out == 0) {
828*7c478bd9Sstevel@tonic-gate 	    /* We'll have to come back to get the rest of the output;
829*7c478bd9Sstevel@tonic-gate 	     * this ensures we don't output a second zero-length stored
830*7c478bd9Sstevel@tonic-gate 	     * block (or whatever).
831*7c478bd9Sstevel@tonic-gate 	     */
832*7c478bd9Sstevel@tonic-gate 	    state->status = FLUSH_STATE;
833*7c478bd9Sstevel@tonic-gate 	    return Z_OK;
834*7c478bd9Sstevel@tonic-gate 	}
835*7c478bd9Sstevel@tonic-gate     }
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate     Assert(strm->avail_out > 0, "bug2");
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate     if (flush != Z_FINISH) return Z_OK;
840*7c478bd9Sstevel@tonic-gate     if (state->noheader) return Z_STREAM_END;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate     /* Write the zlib trailer (adler32) */
843*7c478bd9Sstevel@tonic-gate     putShortMSB(state, (uInt)(state->adler >> 16));
844*7c478bd9Sstevel@tonic-gate     putShortMSB(state, (uInt)(state->adler & 0xffff));
845*7c478bd9Sstevel@tonic-gate     flush_pending(strm);
846*7c478bd9Sstevel@tonic-gate     /* If avail_out is zero, the application will call deflate again
847*7c478bd9Sstevel@tonic-gate      * to flush the rest.
848*7c478bd9Sstevel@tonic-gate      */
849*7c478bd9Sstevel@tonic-gate     state->noheader = -1; /* write the trailer only once! */
850*7c478bd9Sstevel@tonic-gate     return state->pending != 0 ? Z_OK : Z_STREAM_END;
851*7c478bd9Sstevel@tonic-gate }
852*7c478bd9Sstevel@tonic-gate 
853*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
854*7c478bd9Sstevel@tonic-gate int deflateEnd (strm)
855*7c478bd9Sstevel@tonic-gate     z_stream *strm;
856*7c478bd9Sstevel@tonic-gate {
857*7c478bd9Sstevel@tonic-gate     deflate_state *state = (deflate_state *) strm->state;
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate     if (strm == Z_NULL || state == Z_NULL) return Z_STREAM_ERROR;
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate     TRY_FREE(strm, state->window, state->w_size * 2 * sizeof(Byte));
862*7c478bd9Sstevel@tonic-gate     TRY_FREE(strm, state->prev, state->w_size * sizeof(Pos));
863*7c478bd9Sstevel@tonic-gate     TRY_FREE(strm, state->head, state->hash_size * sizeof(Pos));
864*7c478bd9Sstevel@tonic-gate     TRY_FREE(strm, state->pending_buf, state->lit_bufsize * 2 * sizeof(ush));
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate     ZFREE(strm, state, sizeof(deflate_state));
867*7c478bd9Sstevel@tonic-gate     strm->state = Z_NULL;
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate     return Z_OK;
870*7c478bd9Sstevel@tonic-gate }
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate /* ===========================================================================
873*7c478bd9Sstevel@tonic-gate  * Read a new buffer from the current input stream, update the adler32
874*7c478bd9Sstevel@tonic-gate  * and total number of bytes read.
875*7c478bd9Sstevel@tonic-gate  */
876*7c478bd9Sstevel@tonic-gate local int read_buf(strm, buf, size)
877*7c478bd9Sstevel@tonic-gate     z_stream *strm;
878*7c478bd9Sstevel@tonic-gate     charf *buf;
879*7c478bd9Sstevel@tonic-gate     unsigned size;
880*7c478bd9Sstevel@tonic-gate {
881*7c478bd9Sstevel@tonic-gate     unsigned len = strm->avail_in;
882*7c478bd9Sstevel@tonic-gate     deflate_state *state = (deflate_state *) strm->state;
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate     if (len > size) len = size;
885*7c478bd9Sstevel@tonic-gate     if (len == 0) return 0;
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate     strm->avail_in  -= len;
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate     if (!state->noheader) {
890*7c478bd9Sstevel@tonic-gate         state->adler = adler32(state->adler, strm->next_in, len);
891*7c478bd9Sstevel@tonic-gate     }
892*7c478bd9Sstevel@tonic-gate     zmemcpy(buf, strm->next_in, len);
893*7c478bd9Sstevel@tonic-gate     strm->next_in  += len;
894*7c478bd9Sstevel@tonic-gate     strm->total_in += len;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate     return (int)len;
897*7c478bd9Sstevel@tonic-gate }
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate /* ===========================================================================
900*7c478bd9Sstevel@tonic-gate  * Initialize the "longest match" routines for a new zlib stream
901*7c478bd9Sstevel@tonic-gate  */
902*7c478bd9Sstevel@tonic-gate local void lm_init (s)
903*7c478bd9Sstevel@tonic-gate     deflate_state *s;
904*7c478bd9Sstevel@tonic-gate {
905*7c478bd9Sstevel@tonic-gate     s->window_size = (ulg)2L*s->w_size;
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate     CLEAR_HASH(s);
908*7c478bd9Sstevel@tonic-gate 
909*7c478bd9Sstevel@tonic-gate     /* Set the default configuration parameters:
910*7c478bd9Sstevel@tonic-gate      */
911*7c478bd9Sstevel@tonic-gate     s->max_lazy_match   = configuration_table[s->level].max_lazy;
912*7c478bd9Sstevel@tonic-gate     s->good_match       = configuration_table[s->level].good_length;
913*7c478bd9Sstevel@tonic-gate     s->nice_match       = configuration_table[s->level].nice_length;
914*7c478bd9Sstevel@tonic-gate     s->max_chain_length = configuration_table[s->level].max_chain;
915*7c478bd9Sstevel@tonic-gate 
916*7c478bd9Sstevel@tonic-gate     s->strstart = 0;
917*7c478bd9Sstevel@tonic-gate     s->block_start = 0L;
918*7c478bd9Sstevel@tonic-gate     s->lookahead = 0;
919*7c478bd9Sstevel@tonic-gate     s->match_length = MIN_MATCH-1;
920*7c478bd9Sstevel@tonic-gate     s->match_available = 0;
921*7c478bd9Sstevel@tonic-gate     s->ins_h = 0;
922*7c478bd9Sstevel@tonic-gate #ifdef ASMV
923*7c478bd9Sstevel@tonic-gate     match_init(); /* initialize the asm code */
924*7c478bd9Sstevel@tonic-gate #endif
925*7c478bd9Sstevel@tonic-gate }
926*7c478bd9Sstevel@tonic-gate 
927*7c478bd9Sstevel@tonic-gate /* ===========================================================================
928*7c478bd9Sstevel@tonic-gate  * Set match_start to the longest match starting at the given string and
929*7c478bd9Sstevel@tonic-gate  * return its length. Matches shorter or equal to prev_length are discarded,
930*7c478bd9Sstevel@tonic-gate  * in which case the result is equal to prev_length and match_start is
931*7c478bd9Sstevel@tonic-gate  * garbage.
932*7c478bd9Sstevel@tonic-gate  * IN assertions: cur_match is the head of the hash chain for the current
933*7c478bd9Sstevel@tonic-gate  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
934*7c478bd9Sstevel@tonic-gate  */
935*7c478bd9Sstevel@tonic-gate #ifndef ASMV
936*7c478bd9Sstevel@tonic-gate /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
937*7c478bd9Sstevel@tonic-gate  * match.S. The code will be functionally equivalent.
938*7c478bd9Sstevel@tonic-gate  */
939*7c478bd9Sstevel@tonic-gate local int longest_match(s, cur_match)
940*7c478bd9Sstevel@tonic-gate     deflate_state *s;
941*7c478bd9Sstevel@tonic-gate     IPos cur_match;                             /* current match */
942*7c478bd9Sstevel@tonic-gate {
943*7c478bd9Sstevel@tonic-gate     unsigned chain_length = s->max_chain_length;/* max hash chain length */
944*7c478bd9Sstevel@tonic-gate     register Bytef *scan = s->window + s->strstart; /* current string */
945*7c478bd9Sstevel@tonic-gate     register Bytef *match;                       /* matched string */
946*7c478bd9Sstevel@tonic-gate     register int len;                           /* length of current match */
947*7c478bd9Sstevel@tonic-gate     int best_len = s->prev_length;              /* best match length so far */
948*7c478bd9Sstevel@tonic-gate     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
949*7c478bd9Sstevel@tonic-gate         s->strstart - (IPos)MAX_DIST(s) : NIL;
950*7c478bd9Sstevel@tonic-gate     /* Stop when cur_match becomes <= limit. To simplify the code,
951*7c478bd9Sstevel@tonic-gate      * we prevent matches with the string of window index 0.
952*7c478bd9Sstevel@tonic-gate      */
953*7c478bd9Sstevel@tonic-gate     Posf *prev = s->prev;
954*7c478bd9Sstevel@tonic-gate     uInt wmask = s->w_mask;
955*7c478bd9Sstevel@tonic-gate 
956*7c478bd9Sstevel@tonic-gate #ifdef UNALIGNED_OK
957*7c478bd9Sstevel@tonic-gate     /* Compare two bytes at a time. Note: this is not always beneficial.
958*7c478bd9Sstevel@tonic-gate      * Try with and without -DUNALIGNED_OK to check.
959*7c478bd9Sstevel@tonic-gate      */
960*7c478bd9Sstevel@tonic-gate     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
961*7c478bd9Sstevel@tonic-gate     register ush scan_start = *(ushf*)scan;
962*7c478bd9Sstevel@tonic-gate     register ush scan_end   = *(ushf*)(scan+best_len-1);
963*7c478bd9Sstevel@tonic-gate #else
964*7c478bd9Sstevel@tonic-gate     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
965*7c478bd9Sstevel@tonic-gate     register Byte scan_end1  = scan[best_len-1];
966*7c478bd9Sstevel@tonic-gate     register Byte scan_end   = scan[best_len];
967*7c478bd9Sstevel@tonic-gate #endif
968*7c478bd9Sstevel@tonic-gate 
969*7c478bd9Sstevel@tonic-gate     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
970*7c478bd9Sstevel@tonic-gate      * It is easy to get rid of this optimization if necessary.
971*7c478bd9Sstevel@tonic-gate      */
972*7c478bd9Sstevel@tonic-gate     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate     /* Do not waste too much time if we already have a good match: */
975*7c478bd9Sstevel@tonic-gate     if (s->prev_length >= s->good_match) {
976*7c478bd9Sstevel@tonic-gate         chain_length >>= 2;
977*7c478bd9Sstevel@tonic-gate     }
978*7c478bd9Sstevel@tonic-gate     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate     do {
981*7c478bd9Sstevel@tonic-gate         Assert(cur_match < s->strstart, "no future");
982*7c478bd9Sstevel@tonic-gate         match = s->window + cur_match;
983*7c478bd9Sstevel@tonic-gate 
984*7c478bd9Sstevel@tonic-gate         /* Skip to next match if the match length cannot increase
985*7c478bd9Sstevel@tonic-gate          * or if the match length is less than 2:
986*7c478bd9Sstevel@tonic-gate          */
987*7c478bd9Sstevel@tonic-gate #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
988*7c478bd9Sstevel@tonic-gate         /* This code assumes sizeof(unsigned short) == 2. Do not use
989*7c478bd9Sstevel@tonic-gate          * UNALIGNED_OK if your compiler uses a different size.
990*7c478bd9Sstevel@tonic-gate          */
991*7c478bd9Sstevel@tonic-gate         if (*(ushf*)(match+best_len-1) != scan_end ||
992*7c478bd9Sstevel@tonic-gate             *(ushf*)match != scan_start) continue;
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate         /* It is not necessary to compare scan[2] and match[2] since they are
995*7c478bd9Sstevel@tonic-gate          * always equal when the other bytes match, given that the hash keys
996*7c478bd9Sstevel@tonic-gate          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
997*7c478bd9Sstevel@tonic-gate          * strstart+3, +5, ... up to strstart+257. We check for insufficient
998*7c478bd9Sstevel@tonic-gate          * lookahead only every 4th comparison; the 128th check will be made
999*7c478bd9Sstevel@tonic-gate          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1000*7c478bd9Sstevel@tonic-gate          * necessary to put more guard bytes at the end of the window, or
1001*7c478bd9Sstevel@tonic-gate          * to check more often for insufficient lookahead.
1002*7c478bd9Sstevel@tonic-gate          */
1003*7c478bd9Sstevel@tonic-gate         Assert(scan[2] == match[2], "scan[2]?");
1004*7c478bd9Sstevel@tonic-gate         scan++, match++;
1005*7c478bd9Sstevel@tonic-gate         do {
1006*7c478bd9Sstevel@tonic-gate         } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1007*7c478bd9Sstevel@tonic-gate                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1008*7c478bd9Sstevel@tonic-gate                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1009*7c478bd9Sstevel@tonic-gate                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1010*7c478bd9Sstevel@tonic-gate                  scan < strend);
1011*7c478bd9Sstevel@tonic-gate         /* The funny "do {}" generates better code on most compilers */
1012*7c478bd9Sstevel@tonic-gate 
1013*7c478bd9Sstevel@tonic-gate         /* Here, scan <= window+strstart+257 */
1014*7c478bd9Sstevel@tonic-gate         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1015*7c478bd9Sstevel@tonic-gate         if (*scan == *match) scan++;
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate         len = (MAX_MATCH - 1) - (int)(strend-scan);
1018*7c478bd9Sstevel@tonic-gate         scan = strend - (MAX_MATCH-1);
1019*7c478bd9Sstevel@tonic-gate 
1020*7c478bd9Sstevel@tonic-gate #else /* UNALIGNED_OK */
1021*7c478bd9Sstevel@tonic-gate 
1022*7c478bd9Sstevel@tonic-gate         if (match[best_len]   != scan_end  ||
1023*7c478bd9Sstevel@tonic-gate             match[best_len-1] != scan_end1 ||
1024*7c478bd9Sstevel@tonic-gate             *match            != *scan     ||
1025*7c478bd9Sstevel@tonic-gate             *++match          != scan[1])      continue;
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate         /* The check at best_len-1 can be removed because it will be made
1028*7c478bd9Sstevel@tonic-gate          * again later. (This heuristic is not always a win.)
1029*7c478bd9Sstevel@tonic-gate          * It is not necessary to compare scan[2] and match[2] since they
1030*7c478bd9Sstevel@tonic-gate          * are always equal when the other bytes match, given that
1031*7c478bd9Sstevel@tonic-gate          * the hash keys are equal and that HASH_BITS >= 8.
1032*7c478bd9Sstevel@tonic-gate          */
1033*7c478bd9Sstevel@tonic-gate         scan += 2, match++;
1034*7c478bd9Sstevel@tonic-gate         Assert(*scan == *match, "match[2]?");
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate         /* We check for insufficient lookahead only every 8th comparison;
1037*7c478bd9Sstevel@tonic-gate          * the 256th check will be made at strstart+258.
1038*7c478bd9Sstevel@tonic-gate          */
1039*7c478bd9Sstevel@tonic-gate         do {
1040*7c478bd9Sstevel@tonic-gate         } while (*++scan == *++match && *++scan == *++match &&
1041*7c478bd9Sstevel@tonic-gate                  *++scan == *++match && *++scan == *++match &&
1042*7c478bd9Sstevel@tonic-gate                  *++scan == *++match && *++scan == *++match &&
1043*7c478bd9Sstevel@tonic-gate                  *++scan == *++match && *++scan == *++match &&
1044*7c478bd9Sstevel@tonic-gate                  scan < strend);
1045*7c478bd9Sstevel@tonic-gate 
1046*7c478bd9Sstevel@tonic-gate         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate         len = MAX_MATCH - (int)(strend - scan);
1049*7c478bd9Sstevel@tonic-gate         scan = strend - MAX_MATCH;
1050*7c478bd9Sstevel@tonic-gate 
1051*7c478bd9Sstevel@tonic-gate #endif /* UNALIGNED_OK */
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate         if (len > best_len) {
1054*7c478bd9Sstevel@tonic-gate             s->match_start = cur_match;
1055*7c478bd9Sstevel@tonic-gate             best_len = len;
1056*7c478bd9Sstevel@tonic-gate             if (len >= s->nice_match) break;
1057*7c478bd9Sstevel@tonic-gate #ifdef UNALIGNED_OK
1058*7c478bd9Sstevel@tonic-gate             scan_end = *(ushf*)(scan+best_len-1);
1059*7c478bd9Sstevel@tonic-gate #else
1060*7c478bd9Sstevel@tonic-gate             scan_end1  = scan[best_len-1];
1061*7c478bd9Sstevel@tonic-gate             scan_end   = scan[best_len];
1062*7c478bd9Sstevel@tonic-gate #endif
1063*7c478bd9Sstevel@tonic-gate         }
1064*7c478bd9Sstevel@tonic-gate     } while ((cur_match = prev[cur_match & wmask]) > limit
1065*7c478bd9Sstevel@tonic-gate              && --chain_length != 0);
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate     return best_len;
1068*7c478bd9Sstevel@tonic-gate }
1069*7c478bd9Sstevel@tonic-gate #endif /* ASMV */
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
1072*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1073*7c478bd9Sstevel@tonic-gate  * Check that the match at match_start is indeed a match.
1074*7c478bd9Sstevel@tonic-gate  */
1075*7c478bd9Sstevel@tonic-gate local void check_match(s, start, match, length)
1076*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1077*7c478bd9Sstevel@tonic-gate     IPos start, match;
1078*7c478bd9Sstevel@tonic-gate     int length;
1079*7c478bd9Sstevel@tonic-gate {
1080*7c478bd9Sstevel@tonic-gate     /* check that the match is indeed a match */
1081*7c478bd9Sstevel@tonic-gate     if (memcmp((charf *)s->window + match,
1082*7c478bd9Sstevel@tonic-gate                 (charf *)s->window + start, length) != EQUAL) {
1083*7c478bd9Sstevel@tonic-gate         fprintf(stderr,
1084*7c478bd9Sstevel@tonic-gate             " start %u, match %u, length %d\n",
1085*7c478bd9Sstevel@tonic-gate             start, match, length);
1086*7c478bd9Sstevel@tonic-gate         do { fprintf(stderr, "%c%c", s->window[match++],
1087*7c478bd9Sstevel@tonic-gate                      s->window[start++]); } while (--length != 0);
1088*7c478bd9Sstevel@tonic-gate         z_error("invalid match");
1089*7c478bd9Sstevel@tonic-gate     }
1090*7c478bd9Sstevel@tonic-gate     if (verbose > 1) {
1091*7c478bd9Sstevel@tonic-gate         fprintf(stderr,"\\[%d,%d]", start-match, length);
1092*7c478bd9Sstevel@tonic-gate         do { putc(s->window[start++], stderr); } while (--length != 0);
1093*7c478bd9Sstevel@tonic-gate     }
1094*7c478bd9Sstevel@tonic-gate }
1095*7c478bd9Sstevel@tonic-gate #else
1096*7c478bd9Sstevel@tonic-gate #  define check_match(s, start, match, length)
1097*7c478bd9Sstevel@tonic-gate #endif
1098*7c478bd9Sstevel@tonic-gate 
1099*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1100*7c478bd9Sstevel@tonic-gate  * Fill the window when the lookahead becomes insufficient.
1101*7c478bd9Sstevel@tonic-gate  * Updates strstart and lookahead.
1102*7c478bd9Sstevel@tonic-gate  *
1103*7c478bd9Sstevel@tonic-gate  * IN assertion: lookahead < MIN_LOOKAHEAD
1104*7c478bd9Sstevel@tonic-gate  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1105*7c478bd9Sstevel@tonic-gate  *    At least one byte has been read, or avail_in == 0; reads are
1106*7c478bd9Sstevel@tonic-gate  *    performed for at least two bytes (required for the zip translate_eol
1107*7c478bd9Sstevel@tonic-gate  *    option -- not supported here).
1108*7c478bd9Sstevel@tonic-gate  */
1109*7c478bd9Sstevel@tonic-gate local void fill_window(s)
1110*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1111*7c478bd9Sstevel@tonic-gate {
1112*7c478bd9Sstevel@tonic-gate     register unsigned n, m;
1113*7c478bd9Sstevel@tonic-gate     register Posf *p;
1114*7c478bd9Sstevel@tonic-gate     unsigned more;    /* Amount of free space at the end of the window. */
1115*7c478bd9Sstevel@tonic-gate     uInt wsize = s->w_size;
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate     do {
1118*7c478bd9Sstevel@tonic-gate         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1119*7c478bd9Sstevel@tonic-gate 
1120*7c478bd9Sstevel@tonic-gate         /* Deal with !@#$% 64K limit: */
1121*7c478bd9Sstevel@tonic-gate         if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1122*7c478bd9Sstevel@tonic-gate             more = wsize;
1123*7c478bd9Sstevel@tonic-gate         } else if (more == (unsigned)(-1)) {
1124*7c478bd9Sstevel@tonic-gate             /* Very unlikely, but possible on 16 bit machine if strstart == 0
1125*7c478bd9Sstevel@tonic-gate              * and lookahead == 1 (input done one byte at time)
1126*7c478bd9Sstevel@tonic-gate              */
1127*7c478bd9Sstevel@tonic-gate             more--;
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate         /* If the window is almost full and there is insufficient lookahead,
1130*7c478bd9Sstevel@tonic-gate          * move the upper half to the lower one to make room in the upper half.
1131*7c478bd9Sstevel@tonic-gate          */
1132*7c478bd9Sstevel@tonic-gate         } else if (s->strstart >= wsize+MAX_DIST(s)) {
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate             /* By the IN assertion, the window is not empty so we can't confuse
1135*7c478bd9Sstevel@tonic-gate              * more == 0 with more == 64K on a 16 bit machine.
1136*7c478bd9Sstevel@tonic-gate              */
1137*7c478bd9Sstevel@tonic-gate             zmemcpy((charf *)s->window, (charf *)s->window+wsize,
1138*7c478bd9Sstevel@tonic-gate                    (unsigned)wsize);
1139*7c478bd9Sstevel@tonic-gate             s->match_start -= wsize;
1140*7c478bd9Sstevel@tonic-gate             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
1141*7c478bd9Sstevel@tonic-gate 
1142*7c478bd9Sstevel@tonic-gate             s->block_start -= (long) wsize;
1143*7c478bd9Sstevel@tonic-gate 
1144*7c478bd9Sstevel@tonic-gate             /* Slide the hash table (could be avoided with 32 bit values
1145*7c478bd9Sstevel@tonic-gate                at the expense of memory usage):
1146*7c478bd9Sstevel@tonic-gate              */
1147*7c478bd9Sstevel@tonic-gate             n = s->hash_size;
1148*7c478bd9Sstevel@tonic-gate             p = &s->head[n];
1149*7c478bd9Sstevel@tonic-gate             do {
1150*7c478bd9Sstevel@tonic-gate                 m = *--p;
1151*7c478bd9Sstevel@tonic-gate                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1152*7c478bd9Sstevel@tonic-gate             } while (--n);
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate             n = wsize;
1155*7c478bd9Sstevel@tonic-gate             p = &s->prev[n];
1156*7c478bd9Sstevel@tonic-gate             do {
1157*7c478bd9Sstevel@tonic-gate                 m = *--p;
1158*7c478bd9Sstevel@tonic-gate                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1159*7c478bd9Sstevel@tonic-gate                 /* If n is not on any hash chain, prev[n] is garbage but
1160*7c478bd9Sstevel@tonic-gate                  * its value will never be used.
1161*7c478bd9Sstevel@tonic-gate                  */
1162*7c478bd9Sstevel@tonic-gate             } while (--n);
1163*7c478bd9Sstevel@tonic-gate 
1164*7c478bd9Sstevel@tonic-gate             more += wsize;
1165*7c478bd9Sstevel@tonic-gate         }
1166*7c478bd9Sstevel@tonic-gate         if (s->strm->avail_in == 0) return;
1167*7c478bd9Sstevel@tonic-gate 
1168*7c478bd9Sstevel@tonic-gate         /* If there was no sliding:
1169*7c478bd9Sstevel@tonic-gate          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1170*7c478bd9Sstevel@tonic-gate          *    more == window_size - lookahead - strstart
1171*7c478bd9Sstevel@tonic-gate          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1172*7c478bd9Sstevel@tonic-gate          * => more >= window_size - 2*WSIZE + 2
1173*7c478bd9Sstevel@tonic-gate          * In the BIG_MEM or MMAP case (not yet supported),
1174*7c478bd9Sstevel@tonic-gate          *   window_size == input_size + MIN_LOOKAHEAD  &&
1175*7c478bd9Sstevel@tonic-gate          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1176*7c478bd9Sstevel@tonic-gate          * Otherwise, window_size == 2*WSIZE so more >= 2.
1177*7c478bd9Sstevel@tonic-gate          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1178*7c478bd9Sstevel@tonic-gate          */
1179*7c478bd9Sstevel@tonic-gate         Assert(more >= 2, "more < 2");
1180*7c478bd9Sstevel@tonic-gate 
1181*7c478bd9Sstevel@tonic-gate         n = read_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead,
1182*7c478bd9Sstevel@tonic-gate                      more);
1183*7c478bd9Sstevel@tonic-gate         s->lookahead += n;
1184*7c478bd9Sstevel@tonic-gate 
1185*7c478bd9Sstevel@tonic-gate         /* Initialize the hash value now that we have some input: */
1186*7c478bd9Sstevel@tonic-gate         if (s->lookahead >= MIN_MATCH) {
1187*7c478bd9Sstevel@tonic-gate             s->ins_h = s->window[s->strstart];
1188*7c478bd9Sstevel@tonic-gate             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1189*7c478bd9Sstevel@tonic-gate #if MIN_MATCH != 3
1190*7c478bd9Sstevel@tonic-gate             Call UPDATE_HASH() MIN_MATCH-3 more times
1191*7c478bd9Sstevel@tonic-gate #endif
1192*7c478bd9Sstevel@tonic-gate         }
1193*7c478bd9Sstevel@tonic-gate         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1194*7c478bd9Sstevel@tonic-gate          * but this is not important since only literal bytes will be emitted.
1195*7c478bd9Sstevel@tonic-gate          */
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1198*7c478bd9Sstevel@tonic-gate }
1199*7c478bd9Sstevel@tonic-gate 
1200*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1201*7c478bd9Sstevel@tonic-gate  * Flush the current block, with given end-of-file flag.
1202*7c478bd9Sstevel@tonic-gate  * IN assertion: strstart is set to the end of the current match.
1203*7c478bd9Sstevel@tonic-gate  */
1204*7c478bd9Sstevel@tonic-gate #define FLUSH_BLOCK_ONLY(s, flush) { \
1205*7c478bd9Sstevel@tonic-gate    ct_flush_block(s, (s->block_start >= 0L ? \
1206*7c478bd9Sstevel@tonic-gate            (charf *)&s->window[(unsigned)s->block_start] : \
1207*7c478bd9Sstevel@tonic-gate            (charf *)Z_NULL), (long)s->strstart - s->block_start, (flush)); \
1208*7c478bd9Sstevel@tonic-gate    s->block_start = s->strstart; \
1209*7c478bd9Sstevel@tonic-gate    flush_pending(s->strm); \
1210*7c478bd9Sstevel@tonic-gate    Tracev((stderr,"[FLUSH]")); \
1211*7c478bd9Sstevel@tonic-gate }
1212*7c478bd9Sstevel@tonic-gate 
1213*7c478bd9Sstevel@tonic-gate /* Same but force premature exit if necessary. */
1214*7c478bd9Sstevel@tonic-gate #define FLUSH_BLOCK(s, flush) { \
1215*7c478bd9Sstevel@tonic-gate    FLUSH_BLOCK_ONLY(s, flush); \
1216*7c478bd9Sstevel@tonic-gate    if (s->strm->avail_out == 0) return 1; \
1217*7c478bd9Sstevel@tonic-gate }
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1220*7c478bd9Sstevel@tonic-gate  * Compress as much as possible from the input stream, return true if
1221*7c478bd9Sstevel@tonic-gate  * processing was terminated prematurely (no more input or output space).
1222*7c478bd9Sstevel@tonic-gate  * This function does not perform lazy evaluationof matches and inserts
1223*7c478bd9Sstevel@tonic-gate  * new strings in the dictionary only for unmatched strings or for short
1224*7c478bd9Sstevel@tonic-gate  * matches. It is used only for the fast compression options.
1225*7c478bd9Sstevel@tonic-gate  */
1226*7c478bd9Sstevel@tonic-gate local int deflate_fast(s, flush)
1227*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1228*7c478bd9Sstevel@tonic-gate     int flush;
1229*7c478bd9Sstevel@tonic-gate {
1230*7c478bd9Sstevel@tonic-gate     IPos hash_head = NIL; /* head of the hash chain */
1231*7c478bd9Sstevel@tonic-gate     int bflush;     /* set if current block must be flushed */
1232*7c478bd9Sstevel@tonic-gate 
1233*7c478bd9Sstevel@tonic-gate     s->prev_length = MIN_MATCH-1;
1234*7c478bd9Sstevel@tonic-gate 
1235*7c478bd9Sstevel@tonic-gate     for (;;) {
1236*7c478bd9Sstevel@tonic-gate         /* Make sure that we always have enough lookahead, except
1237*7c478bd9Sstevel@tonic-gate          * at the end of the input file. We need MAX_MATCH bytes
1238*7c478bd9Sstevel@tonic-gate          * for the next match, plus MIN_MATCH bytes to insert the
1239*7c478bd9Sstevel@tonic-gate          * string following the next match.
1240*7c478bd9Sstevel@tonic-gate          */
1241*7c478bd9Sstevel@tonic-gate         if (s->lookahead < MIN_LOOKAHEAD) {
1242*7c478bd9Sstevel@tonic-gate             fill_window(s);
1243*7c478bd9Sstevel@tonic-gate             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
1244*7c478bd9Sstevel@tonic-gate 
1245*7c478bd9Sstevel@tonic-gate             if (s->lookahead == 0) break; /* flush the current block */
1246*7c478bd9Sstevel@tonic-gate         }
1247*7c478bd9Sstevel@tonic-gate 
1248*7c478bd9Sstevel@tonic-gate         /* Insert the string window[strstart .. strstart+2] in the
1249*7c478bd9Sstevel@tonic-gate          * dictionary, and set hash_head to the head of the hash chain:
1250*7c478bd9Sstevel@tonic-gate          */
1251*7c478bd9Sstevel@tonic-gate         if (s->lookahead >= MIN_MATCH) {
1252*7c478bd9Sstevel@tonic-gate             INSERT_STRING(s, s->strstart, hash_head);
1253*7c478bd9Sstevel@tonic-gate         }
1254*7c478bd9Sstevel@tonic-gate 
1255*7c478bd9Sstevel@tonic-gate         /* Find the longest match, discarding those <= prev_length.
1256*7c478bd9Sstevel@tonic-gate          * At this point we have always match_length < MIN_MATCH
1257*7c478bd9Sstevel@tonic-gate          */
1258*7c478bd9Sstevel@tonic-gate         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1259*7c478bd9Sstevel@tonic-gate             /* To simplify the code, we prevent matches with the string
1260*7c478bd9Sstevel@tonic-gate              * of window index 0 (in particular we have to avoid a match
1261*7c478bd9Sstevel@tonic-gate              * of the string with itself at the start of the input file).
1262*7c478bd9Sstevel@tonic-gate              */
1263*7c478bd9Sstevel@tonic-gate             if (s->strategy != Z_HUFFMAN_ONLY) {
1264*7c478bd9Sstevel@tonic-gate                 s->match_length = longest_match (s, hash_head);
1265*7c478bd9Sstevel@tonic-gate             }
1266*7c478bd9Sstevel@tonic-gate             /* longest_match() sets match_start */
1267*7c478bd9Sstevel@tonic-gate 
1268*7c478bd9Sstevel@tonic-gate             if (s->match_length > s->lookahead) s->match_length = s->lookahead;
1269*7c478bd9Sstevel@tonic-gate         }
1270*7c478bd9Sstevel@tonic-gate         if (s->match_length >= MIN_MATCH) {
1271*7c478bd9Sstevel@tonic-gate             check_match(s, s->strstart, s->match_start, s->match_length);
1272*7c478bd9Sstevel@tonic-gate 
1273*7c478bd9Sstevel@tonic-gate             bflush = ct_tally(s, s->strstart - s->match_start,
1274*7c478bd9Sstevel@tonic-gate                               s->match_length - MIN_MATCH);
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate             s->lookahead -= s->match_length;
1277*7c478bd9Sstevel@tonic-gate 
1278*7c478bd9Sstevel@tonic-gate             /* Insert new strings in the hash table only if the match length
1279*7c478bd9Sstevel@tonic-gate              * is not too large. This saves time but degrades compression.
1280*7c478bd9Sstevel@tonic-gate              */
1281*7c478bd9Sstevel@tonic-gate             if (s->match_length <= s->max_insert_length &&
1282*7c478bd9Sstevel@tonic-gate                 s->lookahead >= MIN_MATCH) {
1283*7c478bd9Sstevel@tonic-gate                 s->match_length--; /* string at strstart already in hash table */
1284*7c478bd9Sstevel@tonic-gate                 do {
1285*7c478bd9Sstevel@tonic-gate                     s->strstart++;
1286*7c478bd9Sstevel@tonic-gate                     INSERT_STRING(s, s->strstart, hash_head);
1287*7c478bd9Sstevel@tonic-gate                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1288*7c478bd9Sstevel@tonic-gate                      * always MIN_MATCH bytes ahead.
1289*7c478bd9Sstevel@tonic-gate                      */
1290*7c478bd9Sstevel@tonic-gate                 } while (--s->match_length != 0);
1291*7c478bd9Sstevel@tonic-gate                 s->strstart++;
1292*7c478bd9Sstevel@tonic-gate             } else {
1293*7c478bd9Sstevel@tonic-gate                 s->strstart += s->match_length;
1294*7c478bd9Sstevel@tonic-gate                 s->match_length = 0;
1295*7c478bd9Sstevel@tonic-gate                 s->ins_h = s->window[s->strstart];
1296*7c478bd9Sstevel@tonic-gate                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1297*7c478bd9Sstevel@tonic-gate #if MIN_MATCH != 3
1298*7c478bd9Sstevel@tonic-gate                 Call UPDATE_HASH() MIN_MATCH-3 more times
1299*7c478bd9Sstevel@tonic-gate #endif
1300*7c478bd9Sstevel@tonic-gate                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1301*7c478bd9Sstevel@tonic-gate                  * matter since it will be recomputed at next deflate call.
1302*7c478bd9Sstevel@tonic-gate                  */
1303*7c478bd9Sstevel@tonic-gate             }
1304*7c478bd9Sstevel@tonic-gate         } else {
1305*7c478bd9Sstevel@tonic-gate             /* No match, output a literal byte */
1306*7c478bd9Sstevel@tonic-gate             Tracevv((stderr,"%c", s->window[s->strstart]));
1307*7c478bd9Sstevel@tonic-gate             bflush = ct_tally (s, 0, s->window[s->strstart]);
1308*7c478bd9Sstevel@tonic-gate             s->lookahead--;
1309*7c478bd9Sstevel@tonic-gate             s->strstart++;
1310*7c478bd9Sstevel@tonic-gate         }
1311*7c478bd9Sstevel@tonic-gate         if (bflush) FLUSH_BLOCK(s, Z_NO_FLUSH);
1312*7c478bd9Sstevel@tonic-gate     }
1313*7c478bd9Sstevel@tonic-gate     FLUSH_BLOCK(s, flush);
1314*7c478bd9Sstevel@tonic-gate     return 0; /* normal exit */
1315*7c478bd9Sstevel@tonic-gate }
1316*7c478bd9Sstevel@tonic-gate 
1317*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1318*7c478bd9Sstevel@tonic-gate  * Same as above, but achieves better compression. We use a lazy
1319*7c478bd9Sstevel@tonic-gate  * evaluation for matches: a match is finally adopted only if there is
1320*7c478bd9Sstevel@tonic-gate  * no better match at the next window position.
1321*7c478bd9Sstevel@tonic-gate  */
1322*7c478bd9Sstevel@tonic-gate local int deflate_slow(s, flush)
1323*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1324*7c478bd9Sstevel@tonic-gate     int flush;
1325*7c478bd9Sstevel@tonic-gate {
1326*7c478bd9Sstevel@tonic-gate     IPos hash_head = NIL;    /* head of hash chain */
1327*7c478bd9Sstevel@tonic-gate     int bflush;              /* set if current block must be flushed */
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate     /* Process the input block. */
1330*7c478bd9Sstevel@tonic-gate     for (;;) {
1331*7c478bd9Sstevel@tonic-gate         /* Make sure that we always have enough lookahead, except
1332*7c478bd9Sstevel@tonic-gate          * at the end of the input file. We need MAX_MATCH bytes
1333*7c478bd9Sstevel@tonic-gate          * for the next match, plus MIN_MATCH bytes to insert the
1334*7c478bd9Sstevel@tonic-gate          * string following the next match.
1335*7c478bd9Sstevel@tonic-gate          */
1336*7c478bd9Sstevel@tonic-gate         if (s->lookahead < MIN_LOOKAHEAD) {
1337*7c478bd9Sstevel@tonic-gate             fill_window(s);
1338*7c478bd9Sstevel@tonic-gate             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
1339*7c478bd9Sstevel@tonic-gate 
1340*7c478bd9Sstevel@tonic-gate             if (s->lookahead == 0) break; /* flush the current block */
1341*7c478bd9Sstevel@tonic-gate         }
1342*7c478bd9Sstevel@tonic-gate 
1343*7c478bd9Sstevel@tonic-gate         /* Insert the string window[strstart .. strstart+2] in the
1344*7c478bd9Sstevel@tonic-gate          * dictionary, and set hash_head to the head of the hash chain:
1345*7c478bd9Sstevel@tonic-gate          */
1346*7c478bd9Sstevel@tonic-gate         if (s->lookahead >= MIN_MATCH) {
1347*7c478bd9Sstevel@tonic-gate             INSERT_STRING(s, s->strstart, hash_head);
1348*7c478bd9Sstevel@tonic-gate         }
1349*7c478bd9Sstevel@tonic-gate 
1350*7c478bd9Sstevel@tonic-gate         /* Find the longest match, discarding those <= prev_length.
1351*7c478bd9Sstevel@tonic-gate          */
1352*7c478bd9Sstevel@tonic-gate         s->prev_length = s->match_length, s->prev_match = s->match_start;
1353*7c478bd9Sstevel@tonic-gate         s->match_length = MIN_MATCH-1;
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1356*7c478bd9Sstevel@tonic-gate             s->strstart - hash_head <= MAX_DIST(s)) {
1357*7c478bd9Sstevel@tonic-gate             /* To simplify the code, we prevent matches with the string
1358*7c478bd9Sstevel@tonic-gate              * of window index 0 (in particular we have to avoid a match
1359*7c478bd9Sstevel@tonic-gate              * of the string with itself at the start of the input file).
1360*7c478bd9Sstevel@tonic-gate              */
1361*7c478bd9Sstevel@tonic-gate             if (s->strategy != Z_HUFFMAN_ONLY) {
1362*7c478bd9Sstevel@tonic-gate                 s->match_length = longest_match (s, hash_head);
1363*7c478bd9Sstevel@tonic-gate             }
1364*7c478bd9Sstevel@tonic-gate             /* longest_match() sets match_start */
1365*7c478bd9Sstevel@tonic-gate             if (s->match_length > s->lookahead) s->match_length = s->lookahead;
1366*7c478bd9Sstevel@tonic-gate 
1367*7c478bd9Sstevel@tonic-gate             if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1368*7c478bd9Sstevel@tonic-gate                  (s->match_length == MIN_MATCH &&
1369*7c478bd9Sstevel@tonic-gate                   s->strstart - s->match_start > TOO_FAR))) {
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate                 /* If prev_match is also MIN_MATCH, match_start is garbage
1372*7c478bd9Sstevel@tonic-gate                  * but we will ignore the current match anyway.
1373*7c478bd9Sstevel@tonic-gate                  */
1374*7c478bd9Sstevel@tonic-gate                 s->match_length = MIN_MATCH-1;
1375*7c478bd9Sstevel@tonic-gate             }
1376*7c478bd9Sstevel@tonic-gate         }
1377*7c478bd9Sstevel@tonic-gate         /* If there was a match at the previous step and the current
1378*7c478bd9Sstevel@tonic-gate          * match is not better, output the previous match:
1379*7c478bd9Sstevel@tonic-gate          */
1380*7c478bd9Sstevel@tonic-gate         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1381*7c478bd9Sstevel@tonic-gate             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1382*7c478bd9Sstevel@tonic-gate             /* Do not insert strings in hash table beyond this. */
1383*7c478bd9Sstevel@tonic-gate 
1384*7c478bd9Sstevel@tonic-gate             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate             bflush = ct_tally(s, s->strstart -1 - s->prev_match,
1387*7c478bd9Sstevel@tonic-gate                               s->prev_length - MIN_MATCH);
1388*7c478bd9Sstevel@tonic-gate 
1389*7c478bd9Sstevel@tonic-gate             /* Insert in hash table all strings up to the end of the match.
1390*7c478bd9Sstevel@tonic-gate              * strstart-1 and strstart are already inserted. If there is not
1391*7c478bd9Sstevel@tonic-gate              * enough lookahead, the last two strings are not inserted in
1392*7c478bd9Sstevel@tonic-gate              * the hash table.
1393*7c478bd9Sstevel@tonic-gate              */
1394*7c478bd9Sstevel@tonic-gate             s->lookahead -= s->prev_length-1;
1395*7c478bd9Sstevel@tonic-gate             s->prev_length -= 2;
1396*7c478bd9Sstevel@tonic-gate             do {
1397*7c478bd9Sstevel@tonic-gate                 if (++s->strstart <= max_insert) {
1398*7c478bd9Sstevel@tonic-gate                     INSERT_STRING(s, s->strstart, hash_head);
1399*7c478bd9Sstevel@tonic-gate                 }
1400*7c478bd9Sstevel@tonic-gate             } while (--s->prev_length != 0);
1401*7c478bd9Sstevel@tonic-gate             s->match_available = 0;
1402*7c478bd9Sstevel@tonic-gate             s->match_length = MIN_MATCH-1;
1403*7c478bd9Sstevel@tonic-gate             s->strstart++;
1404*7c478bd9Sstevel@tonic-gate 
1405*7c478bd9Sstevel@tonic-gate             if (bflush) FLUSH_BLOCK(s, Z_NO_FLUSH);
1406*7c478bd9Sstevel@tonic-gate 
1407*7c478bd9Sstevel@tonic-gate         } else if (s->match_available) {
1408*7c478bd9Sstevel@tonic-gate             /* If there was no match at the previous position, output a
1409*7c478bd9Sstevel@tonic-gate              * single literal. If there was a match but the current match
1410*7c478bd9Sstevel@tonic-gate              * is longer, truncate the previous match to a single literal.
1411*7c478bd9Sstevel@tonic-gate              */
1412*7c478bd9Sstevel@tonic-gate             Tracevv((stderr,"%c", s->window[s->strstart-1]));
1413*7c478bd9Sstevel@tonic-gate             if (ct_tally (s, 0, s->window[s->strstart-1])) {
1414*7c478bd9Sstevel@tonic-gate                 FLUSH_BLOCK_ONLY(s, Z_NO_FLUSH);
1415*7c478bd9Sstevel@tonic-gate             }
1416*7c478bd9Sstevel@tonic-gate             s->strstart++;
1417*7c478bd9Sstevel@tonic-gate             s->lookahead--;
1418*7c478bd9Sstevel@tonic-gate             if (s->strm->avail_out == 0) return 1;
1419*7c478bd9Sstevel@tonic-gate         } else {
1420*7c478bd9Sstevel@tonic-gate             /* There is no previous match to compare with, wait for
1421*7c478bd9Sstevel@tonic-gate              * the next step to decide.
1422*7c478bd9Sstevel@tonic-gate              */
1423*7c478bd9Sstevel@tonic-gate             s->match_available = 1;
1424*7c478bd9Sstevel@tonic-gate             s->strstart++;
1425*7c478bd9Sstevel@tonic-gate             s->lookahead--;
1426*7c478bd9Sstevel@tonic-gate         }
1427*7c478bd9Sstevel@tonic-gate     }
1428*7c478bd9Sstevel@tonic-gate     Assert (flush != Z_NO_FLUSH, "no flush?");
1429*7c478bd9Sstevel@tonic-gate     if (s->match_available) {
1430*7c478bd9Sstevel@tonic-gate         Tracevv((stderr,"%c", s->window[s->strstart-1]));
1431*7c478bd9Sstevel@tonic-gate         ct_tally (s, 0, s->window[s->strstart-1]);
1432*7c478bd9Sstevel@tonic-gate         s->match_available = 0;
1433*7c478bd9Sstevel@tonic-gate     }
1434*7c478bd9Sstevel@tonic-gate     FLUSH_BLOCK(s, flush);
1435*7c478bd9Sstevel@tonic-gate     return 0;
1436*7c478bd9Sstevel@tonic-gate }
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 
1439*7c478bd9Sstevel@tonic-gate /*+++++*/
1440*7c478bd9Sstevel@tonic-gate /* trees.c -- output deflated data using Huffman coding
1441*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Jean-loup Gailly
1442*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
1443*7c478bd9Sstevel@tonic-gate  */
1444*7c478bd9Sstevel@tonic-gate 
1445*7c478bd9Sstevel@tonic-gate /*
1446*7c478bd9Sstevel@tonic-gate  *  ALGORITHM
1447*7c478bd9Sstevel@tonic-gate  *
1448*7c478bd9Sstevel@tonic-gate  *      The "deflation" process uses several Huffman trees. The more
1449*7c478bd9Sstevel@tonic-gate  *      common source values are represented by shorter bit sequences.
1450*7c478bd9Sstevel@tonic-gate  *
1451*7c478bd9Sstevel@tonic-gate  *      Each code tree is stored in a compressed form which is itself
1452*7c478bd9Sstevel@tonic-gate  * a Huffman encoding of the lengths of all the code strings (in
1453*7c478bd9Sstevel@tonic-gate  * ascending order by source values).  The actual code strings are
1454*7c478bd9Sstevel@tonic-gate  * reconstructed from the lengths in the inflate process, as described
1455*7c478bd9Sstevel@tonic-gate  * in the deflate specification.
1456*7c478bd9Sstevel@tonic-gate  *
1457*7c478bd9Sstevel@tonic-gate  *  REFERENCES
1458*7c478bd9Sstevel@tonic-gate  *
1459*7c478bd9Sstevel@tonic-gate  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
1460*7c478bd9Sstevel@tonic-gate  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
1461*7c478bd9Sstevel@tonic-gate  *
1462*7c478bd9Sstevel@tonic-gate  *      Storer, James A.
1463*7c478bd9Sstevel@tonic-gate  *          Data Compression:  Methods and Theory, pp. 49-50.
1464*7c478bd9Sstevel@tonic-gate  *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
1465*7c478bd9Sstevel@tonic-gate  *
1466*7c478bd9Sstevel@tonic-gate  *      Sedgewick, R.
1467*7c478bd9Sstevel@tonic-gate  *          Algorithms, p290.
1468*7c478bd9Sstevel@tonic-gate  *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
1469*7c478bd9Sstevel@tonic-gate  */
1470*7c478bd9Sstevel@tonic-gate 
1471*7c478bd9Sstevel@tonic-gate /* From: trees.c,v 1.5 1995/05/03 17:27:12 jloup Exp */
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
1474*7c478bd9Sstevel@tonic-gate #  include <ctype.h>
1475*7c478bd9Sstevel@tonic-gate #endif
1476*7c478bd9Sstevel@tonic-gate 
1477*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1478*7c478bd9Sstevel@tonic-gate  * Constants
1479*7c478bd9Sstevel@tonic-gate  */
1480*7c478bd9Sstevel@tonic-gate 
1481*7c478bd9Sstevel@tonic-gate #define MAX_BL_BITS 7
1482*7c478bd9Sstevel@tonic-gate /* Bit length codes must not exceed MAX_BL_BITS bits */
1483*7c478bd9Sstevel@tonic-gate 
1484*7c478bd9Sstevel@tonic-gate #define END_BLOCK 256
1485*7c478bd9Sstevel@tonic-gate /* end of block literal code */
1486*7c478bd9Sstevel@tonic-gate 
1487*7c478bd9Sstevel@tonic-gate #define REP_3_6      16
1488*7c478bd9Sstevel@tonic-gate /* repeat previous bit length 3-6 times (2 bits of repeat count) */
1489*7c478bd9Sstevel@tonic-gate 
1490*7c478bd9Sstevel@tonic-gate #define REPZ_3_10    17
1491*7c478bd9Sstevel@tonic-gate /* repeat a zero length 3-10 times  (3 bits of repeat count) */
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate #define REPZ_11_138  18
1494*7c478bd9Sstevel@tonic-gate /* repeat a zero length 11-138 times  (7 bits of repeat count) */
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate local int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
1497*7c478bd9Sstevel@tonic-gate    = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
1498*7c478bd9Sstevel@tonic-gate 
1499*7c478bd9Sstevel@tonic-gate local int extra_dbits[D_CODES] /* extra bits for each distance code */
1500*7c478bd9Sstevel@tonic-gate    = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
1501*7c478bd9Sstevel@tonic-gate 
1502*7c478bd9Sstevel@tonic-gate local int extra_blbits[BL_CODES]/* extra bits for each bit length code */
1503*7c478bd9Sstevel@tonic-gate    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
1504*7c478bd9Sstevel@tonic-gate 
1505*7c478bd9Sstevel@tonic-gate local uch bl_order[BL_CODES]
1506*7c478bd9Sstevel@tonic-gate    = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
1507*7c478bd9Sstevel@tonic-gate /* The lengths of the bit length codes are sent in order of decreasing
1508*7c478bd9Sstevel@tonic-gate  * probability, to avoid transmitting the lengths for unused bit length codes.
1509*7c478bd9Sstevel@tonic-gate  */
1510*7c478bd9Sstevel@tonic-gate 
1511*7c478bd9Sstevel@tonic-gate #define Buf_size (8 * 2*sizeof(char))
1512*7c478bd9Sstevel@tonic-gate /* Number of bits used within bi_buf. (bi_buf might be implemented on
1513*7c478bd9Sstevel@tonic-gate  * more than 16 bits on some systems.)
1514*7c478bd9Sstevel@tonic-gate  */
1515*7c478bd9Sstevel@tonic-gate 
1516*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1517*7c478bd9Sstevel@tonic-gate  * Local data. These are initialized only once.
1518*7c478bd9Sstevel@tonic-gate  * To do: initialize at compile time to be completely reentrant. ???
1519*7c478bd9Sstevel@tonic-gate  */
1520*7c478bd9Sstevel@tonic-gate 
1521*7c478bd9Sstevel@tonic-gate local ct_data static_ltree[L_CODES+2];
1522*7c478bd9Sstevel@tonic-gate /* The static literal tree. Since the bit lengths are imposed, there is no
1523*7c478bd9Sstevel@tonic-gate  * need for the L_CODES extra codes used during heap construction. However
1524*7c478bd9Sstevel@tonic-gate  * The codes 286 and 287 are needed to build a canonical tree (see ct_init
1525*7c478bd9Sstevel@tonic-gate  * below).
1526*7c478bd9Sstevel@tonic-gate  */
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate local ct_data static_dtree[D_CODES];
1529*7c478bd9Sstevel@tonic-gate /* The static distance tree. (Actually a trivial tree since all codes use
1530*7c478bd9Sstevel@tonic-gate  * 5 bits.)
1531*7c478bd9Sstevel@tonic-gate  */
1532*7c478bd9Sstevel@tonic-gate 
1533*7c478bd9Sstevel@tonic-gate local uch dist_code[512];
1534*7c478bd9Sstevel@tonic-gate /* distance codes. The first 256 values correspond to the distances
1535*7c478bd9Sstevel@tonic-gate  * 3 .. 258, the last 256 values correspond to the top 8 bits of
1536*7c478bd9Sstevel@tonic-gate  * the 15 bit distances.
1537*7c478bd9Sstevel@tonic-gate  */
1538*7c478bd9Sstevel@tonic-gate 
1539*7c478bd9Sstevel@tonic-gate local uch length_code[MAX_MATCH-MIN_MATCH+1];
1540*7c478bd9Sstevel@tonic-gate /* length code for each normalized match length (0 == MIN_MATCH) */
1541*7c478bd9Sstevel@tonic-gate 
1542*7c478bd9Sstevel@tonic-gate local int base_length[LENGTH_CODES];
1543*7c478bd9Sstevel@tonic-gate /* First normalized length for each code (0 = MIN_MATCH) */
1544*7c478bd9Sstevel@tonic-gate 
1545*7c478bd9Sstevel@tonic-gate local int base_dist[D_CODES];
1546*7c478bd9Sstevel@tonic-gate /* First normalized distance for each code (0 = distance of 1) */
1547*7c478bd9Sstevel@tonic-gate 
1548*7c478bd9Sstevel@tonic-gate struct static_tree_desc_s {
1549*7c478bd9Sstevel@tonic-gate     ct_data *static_tree;        /* static tree or NULL */
1550*7c478bd9Sstevel@tonic-gate     intf    *extra_bits;         /* extra bits for each code or NULL */
1551*7c478bd9Sstevel@tonic-gate     int     extra_base;          /* base index for extra_bits */
1552*7c478bd9Sstevel@tonic-gate     int     elems;               /* max number of elements in the tree */
1553*7c478bd9Sstevel@tonic-gate     int     max_length;          /* max bit length for the codes */
1554*7c478bd9Sstevel@tonic-gate };
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate local static_tree_desc  static_l_desc =
1557*7c478bd9Sstevel@tonic-gate {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
1558*7c478bd9Sstevel@tonic-gate 
1559*7c478bd9Sstevel@tonic-gate local static_tree_desc  static_d_desc =
1560*7c478bd9Sstevel@tonic-gate {static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
1561*7c478bd9Sstevel@tonic-gate 
1562*7c478bd9Sstevel@tonic-gate local static_tree_desc  static_bl_desc =
1563*7c478bd9Sstevel@tonic-gate {(ct_data *)0, extra_blbits, 0,      BL_CODES, MAX_BL_BITS};
1564*7c478bd9Sstevel@tonic-gate 
1565*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1566*7c478bd9Sstevel@tonic-gate  * Local (static) routines in this file.
1567*7c478bd9Sstevel@tonic-gate  */
1568*7c478bd9Sstevel@tonic-gate 
1569*7c478bd9Sstevel@tonic-gate local void ct_static_init OF((void));
1570*7c478bd9Sstevel@tonic-gate local void init_block     OF((deflate_state *s));
1571*7c478bd9Sstevel@tonic-gate local void pqdownheap     OF((deflate_state *s, ct_data *tree, int k));
1572*7c478bd9Sstevel@tonic-gate local void gen_bitlen     OF((deflate_state *s, tree_desc *desc));
1573*7c478bd9Sstevel@tonic-gate local void gen_codes      OF((ct_data *tree, int max_code, ushf *bl_count));
1574*7c478bd9Sstevel@tonic-gate local void build_tree     OF((deflate_state *s, tree_desc *desc));
1575*7c478bd9Sstevel@tonic-gate local void scan_tree      OF((deflate_state *s, ct_data *tree, int max_code));
1576*7c478bd9Sstevel@tonic-gate local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
1577*7c478bd9Sstevel@tonic-gate local int  build_bl_tree  OF((deflate_state *s));
1578*7c478bd9Sstevel@tonic-gate local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
1579*7c478bd9Sstevel@tonic-gate                               int blcodes));
1580*7c478bd9Sstevel@tonic-gate local void compress_block OF((deflate_state *s, ct_data *ltree,
1581*7c478bd9Sstevel@tonic-gate                               ct_data *dtree));
1582*7c478bd9Sstevel@tonic-gate local void set_data_type  OF((deflate_state *s));
1583*7c478bd9Sstevel@tonic-gate local unsigned bi_reverse OF((unsigned value, int length));
1584*7c478bd9Sstevel@tonic-gate local void bi_windup      OF((deflate_state *s));
1585*7c478bd9Sstevel@tonic-gate local void bi_flush       OF((deflate_state *s));
1586*7c478bd9Sstevel@tonic-gate local void copy_block     OF((deflate_state *s, charf *buf, unsigned len,
1587*7c478bd9Sstevel@tonic-gate                               int header));
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate #ifndef DEBUG_ZLIB
1590*7c478bd9Sstevel@tonic-gate #  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
1591*7c478bd9Sstevel@tonic-gate    /* Send a code of the given tree. c and tree must not have side effects */
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate #else /* DEBUG_ZLIB */
1594*7c478bd9Sstevel@tonic-gate #  define send_code(s, c, tree) \
1595*7c478bd9Sstevel@tonic-gate      { if (verbose>1) fprintf(stderr,"\ncd %3d ",(c)); \
1596*7c478bd9Sstevel@tonic-gate        send_bits(s, tree[c].Code, tree[c].Len); }
1597*7c478bd9Sstevel@tonic-gate #endif
1598*7c478bd9Sstevel@tonic-gate 
1599*7c478bd9Sstevel@tonic-gate #define d_code(dist) \
1600*7c478bd9Sstevel@tonic-gate    ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
1601*7c478bd9Sstevel@tonic-gate /* Mapping from a distance to a distance code. dist is the distance - 1 and
1602*7c478bd9Sstevel@tonic-gate  * must not have side effects. dist_code[256] and dist_code[257] are never
1603*7c478bd9Sstevel@tonic-gate  * used.
1604*7c478bd9Sstevel@tonic-gate  */
1605*7c478bd9Sstevel@tonic-gate 
1606*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1607*7c478bd9Sstevel@tonic-gate  * Output a short LSB first on the stream.
1608*7c478bd9Sstevel@tonic-gate  * IN assertion: there is enough room in pendingBuf.
1609*7c478bd9Sstevel@tonic-gate  */
1610*7c478bd9Sstevel@tonic-gate #define put_short(s, w) { \
1611*7c478bd9Sstevel@tonic-gate     put_byte(s, (uch)((w) & 0xff)); \
1612*7c478bd9Sstevel@tonic-gate     put_byte(s, (uch)((ush)(w) >> 8)); \
1613*7c478bd9Sstevel@tonic-gate }
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1616*7c478bd9Sstevel@tonic-gate  * Send a value on a given number of bits.
1617*7c478bd9Sstevel@tonic-gate  * IN assertion: length <= 16 and value fits in length bits.
1618*7c478bd9Sstevel@tonic-gate  */
1619*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
1620*7c478bd9Sstevel@tonic-gate local void send_bits      OF((deflate_state *s, int value, int length));
1621*7c478bd9Sstevel@tonic-gate 
1622*7c478bd9Sstevel@tonic-gate local void send_bits(s, value, length)
1623*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1624*7c478bd9Sstevel@tonic-gate     int value;  /* value to send */
1625*7c478bd9Sstevel@tonic-gate     int length; /* number of bits */
1626*7c478bd9Sstevel@tonic-gate {
1627*7c478bd9Sstevel@tonic-gate     Tracev((stderr," l %2d v %4x ", length, value));
1628*7c478bd9Sstevel@tonic-gate     Assert(length > 0 && length <= 15, "invalid length");
1629*7c478bd9Sstevel@tonic-gate     s->bits_sent += (ulg)length;
1630*7c478bd9Sstevel@tonic-gate 
1631*7c478bd9Sstevel@tonic-gate     /* If not enough room in bi_buf, use (valid) bits from bi_buf and
1632*7c478bd9Sstevel@tonic-gate      * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
1633*7c478bd9Sstevel@tonic-gate      * unused bits in value.
1634*7c478bd9Sstevel@tonic-gate      */
1635*7c478bd9Sstevel@tonic-gate     if (s->bi_valid > (int)Buf_size - length) {
1636*7c478bd9Sstevel@tonic-gate         s->bi_buf |= (value << s->bi_valid);
1637*7c478bd9Sstevel@tonic-gate         put_short(s, s->bi_buf);
1638*7c478bd9Sstevel@tonic-gate         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
1639*7c478bd9Sstevel@tonic-gate         s->bi_valid += length - Buf_size;
1640*7c478bd9Sstevel@tonic-gate     } else {
1641*7c478bd9Sstevel@tonic-gate         s->bi_buf |= value << s->bi_valid;
1642*7c478bd9Sstevel@tonic-gate         s->bi_valid += length;
1643*7c478bd9Sstevel@tonic-gate     }
1644*7c478bd9Sstevel@tonic-gate }
1645*7c478bd9Sstevel@tonic-gate #else /* !DEBUG_ZLIB */
1646*7c478bd9Sstevel@tonic-gate 
1647*7c478bd9Sstevel@tonic-gate #define send_bits(s, value, length) \
1648*7c478bd9Sstevel@tonic-gate { int len = length;\
1649*7c478bd9Sstevel@tonic-gate   if (s->bi_valid > (int)Buf_size - len) {\
1650*7c478bd9Sstevel@tonic-gate     int val = value;\
1651*7c478bd9Sstevel@tonic-gate     s->bi_buf |= (val << s->bi_valid);\
1652*7c478bd9Sstevel@tonic-gate     put_short(s, s->bi_buf);\
1653*7c478bd9Sstevel@tonic-gate     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
1654*7c478bd9Sstevel@tonic-gate     s->bi_valid += len - Buf_size;\
1655*7c478bd9Sstevel@tonic-gate   } else {\
1656*7c478bd9Sstevel@tonic-gate     s->bi_buf |= (value) << s->bi_valid;\
1657*7c478bd9Sstevel@tonic-gate     s->bi_valid += len;\
1658*7c478bd9Sstevel@tonic-gate   }\
1659*7c478bd9Sstevel@tonic-gate }
1660*7c478bd9Sstevel@tonic-gate #endif /* DEBUG_ZLIB */
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 
1663*7c478bd9Sstevel@tonic-gate #define MAX(a,b) (a >= b ? a : b)
1664*7c478bd9Sstevel@tonic-gate /* the arguments must not have side effects */
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1667*7c478bd9Sstevel@tonic-gate  * Initialize the various 'constant' tables.
1668*7c478bd9Sstevel@tonic-gate  * To do: do this at compile time.
1669*7c478bd9Sstevel@tonic-gate  */
1670*7c478bd9Sstevel@tonic-gate local void ct_static_init()
1671*7c478bd9Sstevel@tonic-gate {
1672*7c478bd9Sstevel@tonic-gate     int n;        /* iterates over tree elements */
1673*7c478bd9Sstevel@tonic-gate     int bits;     /* bit counter */
1674*7c478bd9Sstevel@tonic-gate     int length;   /* length value */
1675*7c478bd9Sstevel@tonic-gate     int code;     /* code value */
1676*7c478bd9Sstevel@tonic-gate     int dist;     /* distance index */
1677*7c478bd9Sstevel@tonic-gate     ush bl_count[MAX_BITS+1];
1678*7c478bd9Sstevel@tonic-gate     /* number of codes at each bit length for an optimal tree */
1679*7c478bd9Sstevel@tonic-gate 
1680*7c478bd9Sstevel@tonic-gate     /* Initialize the mapping length (0..255) -> length code (0..28) */
1681*7c478bd9Sstevel@tonic-gate     length = 0;
1682*7c478bd9Sstevel@tonic-gate     for (code = 0; code < LENGTH_CODES-1; code++) {
1683*7c478bd9Sstevel@tonic-gate         base_length[code] = length;
1684*7c478bd9Sstevel@tonic-gate         for (n = 0; n < (1<<extra_lbits[code]); n++) {
1685*7c478bd9Sstevel@tonic-gate             length_code[length++] = (uch)code;
1686*7c478bd9Sstevel@tonic-gate         }
1687*7c478bd9Sstevel@tonic-gate     }
1688*7c478bd9Sstevel@tonic-gate     Assert (length == 256, "ct_static_init: length != 256");
1689*7c478bd9Sstevel@tonic-gate     /* Note that the length 255 (match length 258) can be represented
1690*7c478bd9Sstevel@tonic-gate      * in two different ways: code 284 + 5 bits or code 285, so we
1691*7c478bd9Sstevel@tonic-gate      * overwrite length_code[255] to use the best encoding:
1692*7c478bd9Sstevel@tonic-gate      */
1693*7c478bd9Sstevel@tonic-gate     length_code[length-1] = (uch)code;
1694*7c478bd9Sstevel@tonic-gate 
1695*7c478bd9Sstevel@tonic-gate     /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
1696*7c478bd9Sstevel@tonic-gate     dist = 0;
1697*7c478bd9Sstevel@tonic-gate     for (code = 0 ; code < 16; code++) {
1698*7c478bd9Sstevel@tonic-gate         base_dist[code] = dist;
1699*7c478bd9Sstevel@tonic-gate         for (n = 0; n < (1<<extra_dbits[code]); n++) {
1700*7c478bd9Sstevel@tonic-gate             dist_code[dist++] = (uch)code;
1701*7c478bd9Sstevel@tonic-gate         }
1702*7c478bd9Sstevel@tonic-gate     }
1703*7c478bd9Sstevel@tonic-gate     Assert (dist == 256, "ct_static_init: dist != 256");
1704*7c478bd9Sstevel@tonic-gate     dist >>= 7; /* from now on, all distances are divided by 128 */
1705*7c478bd9Sstevel@tonic-gate     for ( ; code < D_CODES; code++) {
1706*7c478bd9Sstevel@tonic-gate         base_dist[code] = dist << 7;
1707*7c478bd9Sstevel@tonic-gate         for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
1708*7c478bd9Sstevel@tonic-gate             dist_code[256 + dist++] = (uch)code;
1709*7c478bd9Sstevel@tonic-gate         }
1710*7c478bd9Sstevel@tonic-gate     }
1711*7c478bd9Sstevel@tonic-gate     Assert (dist == 256, "ct_static_init: 256+dist != 512");
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate     /* Construct the codes of the static literal tree */
1714*7c478bd9Sstevel@tonic-gate     for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
1715*7c478bd9Sstevel@tonic-gate     n = 0;
1716*7c478bd9Sstevel@tonic-gate     while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
1717*7c478bd9Sstevel@tonic-gate     while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
1718*7c478bd9Sstevel@tonic-gate     while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
1719*7c478bd9Sstevel@tonic-gate     while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
1720*7c478bd9Sstevel@tonic-gate     /* Codes 286 and 287 do not exist, but we must include them in the
1721*7c478bd9Sstevel@tonic-gate      * tree construction to get a canonical Huffman tree (longest code
1722*7c478bd9Sstevel@tonic-gate      * all ones)
1723*7c478bd9Sstevel@tonic-gate      */
1724*7c478bd9Sstevel@tonic-gate     gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate     /* The static distance tree is trivial: */
1727*7c478bd9Sstevel@tonic-gate     for (n = 0; n < D_CODES; n++) {
1728*7c478bd9Sstevel@tonic-gate         static_dtree[n].Len = 5;
1729*7c478bd9Sstevel@tonic-gate         static_dtree[n].Code = bi_reverse(n, 5);
1730*7c478bd9Sstevel@tonic-gate     }
1731*7c478bd9Sstevel@tonic-gate }
1732*7c478bd9Sstevel@tonic-gate 
1733*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1734*7c478bd9Sstevel@tonic-gate  * Initialize the tree data structures for a new zlib stream.
1735*7c478bd9Sstevel@tonic-gate  */
1736*7c478bd9Sstevel@tonic-gate local void ct_init(s)
1737*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1738*7c478bd9Sstevel@tonic-gate {
1739*7c478bd9Sstevel@tonic-gate     if (static_dtree[0].Len == 0) {
1740*7c478bd9Sstevel@tonic-gate         ct_static_init();              /* To do: at compile time */
1741*7c478bd9Sstevel@tonic-gate     }
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate     s->compressed_len = 0L;
1744*7c478bd9Sstevel@tonic-gate 
1745*7c478bd9Sstevel@tonic-gate     s->l_desc.dyn_tree = s->dyn_ltree;
1746*7c478bd9Sstevel@tonic-gate     s->l_desc.stat_desc = &static_l_desc;
1747*7c478bd9Sstevel@tonic-gate 
1748*7c478bd9Sstevel@tonic-gate     s->d_desc.dyn_tree = s->dyn_dtree;
1749*7c478bd9Sstevel@tonic-gate     s->d_desc.stat_desc = &static_d_desc;
1750*7c478bd9Sstevel@tonic-gate 
1751*7c478bd9Sstevel@tonic-gate     s->bl_desc.dyn_tree = s->bl_tree;
1752*7c478bd9Sstevel@tonic-gate     s->bl_desc.stat_desc = &static_bl_desc;
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate     s->bi_buf = 0;
1755*7c478bd9Sstevel@tonic-gate     s->bi_valid = 0;
1756*7c478bd9Sstevel@tonic-gate     s->last_eob_len = 8; /* enough lookahead for inflate */
1757*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
1758*7c478bd9Sstevel@tonic-gate     s->bits_sent = 0L;
1759*7c478bd9Sstevel@tonic-gate #endif
1760*7c478bd9Sstevel@tonic-gate     s->blocks_in_packet = 0;
1761*7c478bd9Sstevel@tonic-gate 
1762*7c478bd9Sstevel@tonic-gate     /* Initialize the first block of the first file: */
1763*7c478bd9Sstevel@tonic-gate     init_block(s);
1764*7c478bd9Sstevel@tonic-gate }
1765*7c478bd9Sstevel@tonic-gate 
1766*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1767*7c478bd9Sstevel@tonic-gate  * Initialize a new block.
1768*7c478bd9Sstevel@tonic-gate  */
1769*7c478bd9Sstevel@tonic-gate local void init_block(s)
1770*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1771*7c478bd9Sstevel@tonic-gate {
1772*7c478bd9Sstevel@tonic-gate     int n; /* iterates over tree elements */
1773*7c478bd9Sstevel@tonic-gate 
1774*7c478bd9Sstevel@tonic-gate     /* Initialize the trees. */
1775*7c478bd9Sstevel@tonic-gate     for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
1776*7c478bd9Sstevel@tonic-gate     for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
1777*7c478bd9Sstevel@tonic-gate     for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
1778*7c478bd9Sstevel@tonic-gate 
1779*7c478bd9Sstevel@tonic-gate     s->dyn_ltree[END_BLOCK].Freq = 1;
1780*7c478bd9Sstevel@tonic-gate     s->opt_len = s->static_len = 0L;
1781*7c478bd9Sstevel@tonic-gate     s->last_lit = s->matches = 0;
1782*7c478bd9Sstevel@tonic-gate }
1783*7c478bd9Sstevel@tonic-gate 
1784*7c478bd9Sstevel@tonic-gate #define SMALLEST 1
1785*7c478bd9Sstevel@tonic-gate /* Index within the heap array of least frequent node in the Huffman tree */
1786*7c478bd9Sstevel@tonic-gate 
1787*7c478bd9Sstevel@tonic-gate 
1788*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1789*7c478bd9Sstevel@tonic-gate  * Remove the smallest element from the heap and recreate the heap with
1790*7c478bd9Sstevel@tonic-gate  * one less element. Updates heap and heap_len.
1791*7c478bd9Sstevel@tonic-gate  */
1792*7c478bd9Sstevel@tonic-gate #define pqremove(s, tree, top) \
1793*7c478bd9Sstevel@tonic-gate {\
1794*7c478bd9Sstevel@tonic-gate     top = s->heap[SMALLEST]; \
1795*7c478bd9Sstevel@tonic-gate     s->heap[SMALLEST] = s->heap[s->heap_len--]; \
1796*7c478bd9Sstevel@tonic-gate     pqdownheap(s, tree, SMALLEST); \
1797*7c478bd9Sstevel@tonic-gate }
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1800*7c478bd9Sstevel@tonic-gate  * Compares to subtrees, using the tree depth as tie breaker when
1801*7c478bd9Sstevel@tonic-gate  * the subtrees have equal frequency. This minimizes the worst case length.
1802*7c478bd9Sstevel@tonic-gate  */
1803*7c478bd9Sstevel@tonic-gate #define smaller(tree, n, m, depth) \
1804*7c478bd9Sstevel@tonic-gate    (tree[n].Freq < tree[m].Freq || \
1805*7c478bd9Sstevel@tonic-gate    (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
1806*7c478bd9Sstevel@tonic-gate 
1807*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1808*7c478bd9Sstevel@tonic-gate  * Restore the heap property by moving down the tree starting at node k,
1809*7c478bd9Sstevel@tonic-gate  * exchanging a node with the smallest of its two sons if necessary, stopping
1810*7c478bd9Sstevel@tonic-gate  * when the heap property is re-established (each father smaller than its
1811*7c478bd9Sstevel@tonic-gate  * two sons).
1812*7c478bd9Sstevel@tonic-gate  */
1813*7c478bd9Sstevel@tonic-gate local void pqdownheap(s, tree, k)
1814*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1815*7c478bd9Sstevel@tonic-gate     ct_data *tree;  /* the tree to restore */
1816*7c478bd9Sstevel@tonic-gate     int k;               /* node to move down */
1817*7c478bd9Sstevel@tonic-gate {
1818*7c478bd9Sstevel@tonic-gate     int v = s->heap[k];
1819*7c478bd9Sstevel@tonic-gate     int j = k << 1;  /* left son of k */
1820*7c478bd9Sstevel@tonic-gate     while (j <= s->heap_len) {
1821*7c478bd9Sstevel@tonic-gate         /* Set j to the smallest of the two sons: */
1822*7c478bd9Sstevel@tonic-gate         if (j < s->heap_len &&
1823*7c478bd9Sstevel@tonic-gate             smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
1824*7c478bd9Sstevel@tonic-gate             j++;
1825*7c478bd9Sstevel@tonic-gate         }
1826*7c478bd9Sstevel@tonic-gate         /* Exit if v is smaller than both sons */
1827*7c478bd9Sstevel@tonic-gate         if (smaller(tree, v, s->heap[j], s->depth)) break;
1828*7c478bd9Sstevel@tonic-gate 
1829*7c478bd9Sstevel@tonic-gate         /* Exchange v with the smallest son */
1830*7c478bd9Sstevel@tonic-gate         s->heap[k] = s->heap[j];  k = j;
1831*7c478bd9Sstevel@tonic-gate 
1832*7c478bd9Sstevel@tonic-gate         /* And continue down the tree, setting j to the left son of k */
1833*7c478bd9Sstevel@tonic-gate         j <<= 1;
1834*7c478bd9Sstevel@tonic-gate     }
1835*7c478bd9Sstevel@tonic-gate     s->heap[k] = v;
1836*7c478bd9Sstevel@tonic-gate }
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1839*7c478bd9Sstevel@tonic-gate  * Compute the optimal bit lengths for a tree and update the total bit length
1840*7c478bd9Sstevel@tonic-gate  * for the current block.
1841*7c478bd9Sstevel@tonic-gate  * IN assertion: the fields freq and dad are set, heap[heap_max] and
1842*7c478bd9Sstevel@tonic-gate  *    above are the tree nodes sorted by increasing frequency.
1843*7c478bd9Sstevel@tonic-gate  * OUT assertions: the field len is set to the optimal bit length, the
1844*7c478bd9Sstevel@tonic-gate  *     array bl_count contains the frequencies for each bit length.
1845*7c478bd9Sstevel@tonic-gate  *     The length opt_len is updated; static_len is also updated if stree is
1846*7c478bd9Sstevel@tonic-gate  *     not null.
1847*7c478bd9Sstevel@tonic-gate  */
1848*7c478bd9Sstevel@tonic-gate local void gen_bitlen(s, desc)
1849*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1850*7c478bd9Sstevel@tonic-gate     tree_desc *desc;    /* the tree descriptor */
1851*7c478bd9Sstevel@tonic-gate {
1852*7c478bd9Sstevel@tonic-gate     ct_data *tree  = desc->dyn_tree;
1853*7c478bd9Sstevel@tonic-gate     int max_code   = desc->max_code;
1854*7c478bd9Sstevel@tonic-gate     ct_data *stree = desc->stat_desc->static_tree;
1855*7c478bd9Sstevel@tonic-gate     intf *extra    = desc->stat_desc->extra_bits;
1856*7c478bd9Sstevel@tonic-gate     int base       = desc->stat_desc->extra_base;
1857*7c478bd9Sstevel@tonic-gate     int max_length = desc->stat_desc->max_length;
1858*7c478bd9Sstevel@tonic-gate     int h;              /* heap index */
1859*7c478bd9Sstevel@tonic-gate     int n, m;           /* iterate over the tree elements */
1860*7c478bd9Sstevel@tonic-gate     int bits;           /* bit length */
1861*7c478bd9Sstevel@tonic-gate     int xbits;          /* extra bits */
1862*7c478bd9Sstevel@tonic-gate     ush f;              /* frequency */
1863*7c478bd9Sstevel@tonic-gate     int overflow = 0;   /* number of elements with bit length too large */
1864*7c478bd9Sstevel@tonic-gate 
1865*7c478bd9Sstevel@tonic-gate     for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
1866*7c478bd9Sstevel@tonic-gate 
1867*7c478bd9Sstevel@tonic-gate     /* In a first pass, compute the optimal bit lengths (which may
1868*7c478bd9Sstevel@tonic-gate      * overflow in the case of the bit length tree).
1869*7c478bd9Sstevel@tonic-gate      */
1870*7c478bd9Sstevel@tonic-gate     tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
1871*7c478bd9Sstevel@tonic-gate 
1872*7c478bd9Sstevel@tonic-gate     for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
1873*7c478bd9Sstevel@tonic-gate         n = s->heap[h];
1874*7c478bd9Sstevel@tonic-gate         bits = tree[tree[n].Dad].Len + 1;
1875*7c478bd9Sstevel@tonic-gate         if (bits > max_length) bits = max_length, overflow++;
1876*7c478bd9Sstevel@tonic-gate         tree[n].Len = (ush)bits;
1877*7c478bd9Sstevel@tonic-gate         /* We overwrite tree[n].Dad which is no longer needed */
1878*7c478bd9Sstevel@tonic-gate 
1879*7c478bd9Sstevel@tonic-gate         if (n > max_code) continue; /* not a leaf node */
1880*7c478bd9Sstevel@tonic-gate 
1881*7c478bd9Sstevel@tonic-gate         s->bl_count[bits]++;
1882*7c478bd9Sstevel@tonic-gate         xbits = 0;
1883*7c478bd9Sstevel@tonic-gate         if (n >= base) xbits = extra[n-base];
1884*7c478bd9Sstevel@tonic-gate         f = tree[n].Freq;
1885*7c478bd9Sstevel@tonic-gate         s->opt_len += (ulg)f * (bits + xbits);
1886*7c478bd9Sstevel@tonic-gate         if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
1887*7c478bd9Sstevel@tonic-gate     }
1888*7c478bd9Sstevel@tonic-gate     if (overflow == 0) return;
1889*7c478bd9Sstevel@tonic-gate 
1890*7c478bd9Sstevel@tonic-gate     Trace((stderr,"\nbit length overflow\n"));
1891*7c478bd9Sstevel@tonic-gate     /* This happens for example on obj2 and pic of the Calgary corpus */
1892*7c478bd9Sstevel@tonic-gate 
1893*7c478bd9Sstevel@tonic-gate     /* Find the first bit length which could increase: */
1894*7c478bd9Sstevel@tonic-gate     do {
1895*7c478bd9Sstevel@tonic-gate         bits = max_length-1;
1896*7c478bd9Sstevel@tonic-gate         while (s->bl_count[bits] == 0) bits--;
1897*7c478bd9Sstevel@tonic-gate         s->bl_count[bits]--;      /* move one leaf down the tree */
1898*7c478bd9Sstevel@tonic-gate         s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
1899*7c478bd9Sstevel@tonic-gate         s->bl_count[max_length]--;
1900*7c478bd9Sstevel@tonic-gate         /* The brother of the overflow item also moves one step up,
1901*7c478bd9Sstevel@tonic-gate          * but this does not affect bl_count[max_length]
1902*7c478bd9Sstevel@tonic-gate          */
1903*7c478bd9Sstevel@tonic-gate         overflow -= 2;
1904*7c478bd9Sstevel@tonic-gate     } while (overflow > 0);
1905*7c478bd9Sstevel@tonic-gate 
1906*7c478bd9Sstevel@tonic-gate     /* Now recompute all bit lengths, scanning in increasing frequency.
1907*7c478bd9Sstevel@tonic-gate      * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
1908*7c478bd9Sstevel@tonic-gate      * lengths instead of fixing only the wrong ones. This idea is taken
1909*7c478bd9Sstevel@tonic-gate      * from 'ar' written by Haruhiko Okumura.)
1910*7c478bd9Sstevel@tonic-gate      */
1911*7c478bd9Sstevel@tonic-gate     for (bits = max_length; bits != 0; bits--) {
1912*7c478bd9Sstevel@tonic-gate         n = s->bl_count[bits];
1913*7c478bd9Sstevel@tonic-gate         while (n != 0) {
1914*7c478bd9Sstevel@tonic-gate             m = s->heap[--h];
1915*7c478bd9Sstevel@tonic-gate             if (m > max_code) continue;
1916*7c478bd9Sstevel@tonic-gate             if (tree[m].Len != (unsigned) bits) {
1917*7c478bd9Sstevel@tonic-gate                 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
1918*7c478bd9Sstevel@tonic-gate                 s->opt_len += ((long)bits - (long)tree[m].Len)
1919*7c478bd9Sstevel@tonic-gate                               *(long)tree[m].Freq;
1920*7c478bd9Sstevel@tonic-gate                 tree[m].Len = (ush)bits;
1921*7c478bd9Sstevel@tonic-gate             }
1922*7c478bd9Sstevel@tonic-gate             n--;
1923*7c478bd9Sstevel@tonic-gate         }
1924*7c478bd9Sstevel@tonic-gate     }
1925*7c478bd9Sstevel@tonic-gate }
1926*7c478bd9Sstevel@tonic-gate 
1927*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1928*7c478bd9Sstevel@tonic-gate  * Generate the codes for a given tree and bit counts (which need not be
1929*7c478bd9Sstevel@tonic-gate  * optimal).
1930*7c478bd9Sstevel@tonic-gate  * IN assertion: the array bl_count contains the bit length statistics for
1931*7c478bd9Sstevel@tonic-gate  * the given tree and the field len is set for all tree elements.
1932*7c478bd9Sstevel@tonic-gate  * OUT assertion: the field code is set for all tree elements of non
1933*7c478bd9Sstevel@tonic-gate  *     zero code length.
1934*7c478bd9Sstevel@tonic-gate  */
1935*7c478bd9Sstevel@tonic-gate local void gen_codes (tree, max_code, bl_count)
1936*7c478bd9Sstevel@tonic-gate     ct_data *tree;             /* the tree to decorate */
1937*7c478bd9Sstevel@tonic-gate     int max_code;              /* largest code with non zero frequency */
1938*7c478bd9Sstevel@tonic-gate     ushf *bl_count;            /* number of codes at each bit length */
1939*7c478bd9Sstevel@tonic-gate {
1940*7c478bd9Sstevel@tonic-gate     ush next_code[MAX_BITS+1]; /* next code value for each bit length */
1941*7c478bd9Sstevel@tonic-gate     ush code = 0;              /* running code value */
1942*7c478bd9Sstevel@tonic-gate     int bits;                  /* bit index */
1943*7c478bd9Sstevel@tonic-gate     int n;                     /* code index */
1944*7c478bd9Sstevel@tonic-gate 
1945*7c478bd9Sstevel@tonic-gate     /* The distribution counts are first used to generate the code values
1946*7c478bd9Sstevel@tonic-gate      * without bit reversal.
1947*7c478bd9Sstevel@tonic-gate      */
1948*7c478bd9Sstevel@tonic-gate     for (bits = 1; bits <= MAX_BITS; bits++) {
1949*7c478bd9Sstevel@tonic-gate         next_code[bits] = code = (code + bl_count[bits-1]) << 1;
1950*7c478bd9Sstevel@tonic-gate     }
1951*7c478bd9Sstevel@tonic-gate     /* Check that the bit counts in bl_count are consistent. The last code
1952*7c478bd9Sstevel@tonic-gate      * must be all ones.
1953*7c478bd9Sstevel@tonic-gate      */
1954*7c478bd9Sstevel@tonic-gate     Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
1955*7c478bd9Sstevel@tonic-gate             "inconsistent bit counts");
1956*7c478bd9Sstevel@tonic-gate     Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate     for (n = 0;  n <= max_code; n++) {
1959*7c478bd9Sstevel@tonic-gate         int len = tree[n].Len;
1960*7c478bd9Sstevel@tonic-gate         if (len == 0) continue;
1961*7c478bd9Sstevel@tonic-gate         /* Now reverse the bits */
1962*7c478bd9Sstevel@tonic-gate         tree[n].Code = bi_reverse(next_code[len]++, len);
1963*7c478bd9Sstevel@tonic-gate 
1964*7c478bd9Sstevel@tonic-gate         Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
1965*7c478bd9Sstevel@tonic-gate              n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
1966*7c478bd9Sstevel@tonic-gate     }
1967*7c478bd9Sstevel@tonic-gate }
1968*7c478bd9Sstevel@tonic-gate 
1969*7c478bd9Sstevel@tonic-gate /* ===========================================================================
1970*7c478bd9Sstevel@tonic-gate  * Construct one Huffman tree and assigns the code bit strings and lengths.
1971*7c478bd9Sstevel@tonic-gate  * Update the total bit length for the current block.
1972*7c478bd9Sstevel@tonic-gate  * IN assertion: the field freq is set for all tree elements.
1973*7c478bd9Sstevel@tonic-gate  * OUT assertions: the fields len and code are set to the optimal bit length
1974*7c478bd9Sstevel@tonic-gate  *     and corresponding code. The length opt_len is updated; static_len is
1975*7c478bd9Sstevel@tonic-gate  *     also updated if stree is not null. The field max_code is set.
1976*7c478bd9Sstevel@tonic-gate  */
1977*7c478bd9Sstevel@tonic-gate local void build_tree(s, desc)
1978*7c478bd9Sstevel@tonic-gate     deflate_state *s;
1979*7c478bd9Sstevel@tonic-gate     tree_desc *desc; /* the tree descriptor */
1980*7c478bd9Sstevel@tonic-gate {
1981*7c478bd9Sstevel@tonic-gate     ct_data *tree   = desc->dyn_tree;
1982*7c478bd9Sstevel@tonic-gate     ct_data *stree  = desc->stat_desc->static_tree;
1983*7c478bd9Sstevel@tonic-gate     int elems       = desc->stat_desc->elems;
1984*7c478bd9Sstevel@tonic-gate     int n, m;          /* iterate over heap elements */
1985*7c478bd9Sstevel@tonic-gate     int max_code = -1; /* largest code with non zero frequency */
1986*7c478bd9Sstevel@tonic-gate     int node;          /* new node being created */
1987*7c478bd9Sstevel@tonic-gate 
1988*7c478bd9Sstevel@tonic-gate     /* Construct the initial heap, with least frequent element in
1989*7c478bd9Sstevel@tonic-gate      * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
1990*7c478bd9Sstevel@tonic-gate      * heap[0] is not used.
1991*7c478bd9Sstevel@tonic-gate      */
1992*7c478bd9Sstevel@tonic-gate     s->heap_len = 0, s->heap_max = HEAP_SIZE;
1993*7c478bd9Sstevel@tonic-gate 
1994*7c478bd9Sstevel@tonic-gate     for (n = 0; n < elems; n++) {
1995*7c478bd9Sstevel@tonic-gate         if (tree[n].Freq != 0) {
1996*7c478bd9Sstevel@tonic-gate             s->heap[++(s->heap_len)] = max_code = n;
1997*7c478bd9Sstevel@tonic-gate             s->depth[n] = 0;
1998*7c478bd9Sstevel@tonic-gate         } else {
1999*7c478bd9Sstevel@tonic-gate             tree[n].Len = 0;
2000*7c478bd9Sstevel@tonic-gate         }
2001*7c478bd9Sstevel@tonic-gate     }
2002*7c478bd9Sstevel@tonic-gate 
2003*7c478bd9Sstevel@tonic-gate     /* The pkzip format requires that at least one distance code exists,
2004*7c478bd9Sstevel@tonic-gate      * and that at least one bit should be sent even if there is only one
2005*7c478bd9Sstevel@tonic-gate      * possible code. So to avoid special checks later on we force at least
2006*7c478bd9Sstevel@tonic-gate      * two codes of non zero frequency.
2007*7c478bd9Sstevel@tonic-gate      */
2008*7c478bd9Sstevel@tonic-gate     while (s->heap_len < 2) {
2009*7c478bd9Sstevel@tonic-gate         node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
2010*7c478bd9Sstevel@tonic-gate         tree[node].Freq = 1;
2011*7c478bd9Sstevel@tonic-gate         s->depth[node] = 0;
2012*7c478bd9Sstevel@tonic-gate         s->opt_len--; if (stree) s->static_len -= stree[node].Len;
2013*7c478bd9Sstevel@tonic-gate         /* node is 0 or 1 so it does not have extra bits */
2014*7c478bd9Sstevel@tonic-gate     }
2015*7c478bd9Sstevel@tonic-gate     desc->max_code = max_code;
2016*7c478bd9Sstevel@tonic-gate 
2017*7c478bd9Sstevel@tonic-gate     /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
2018*7c478bd9Sstevel@tonic-gate      * establish sub-heaps of increasing lengths:
2019*7c478bd9Sstevel@tonic-gate      */
2020*7c478bd9Sstevel@tonic-gate     for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
2021*7c478bd9Sstevel@tonic-gate 
2022*7c478bd9Sstevel@tonic-gate     /* Construct the Huffman tree by repeatedly combining the least two
2023*7c478bd9Sstevel@tonic-gate      * frequent nodes.
2024*7c478bd9Sstevel@tonic-gate      */
2025*7c478bd9Sstevel@tonic-gate     node = elems;              /* next internal node of the tree */
2026*7c478bd9Sstevel@tonic-gate     do {
2027*7c478bd9Sstevel@tonic-gate         pqremove(s, tree, n);  /* n = node of least frequency */
2028*7c478bd9Sstevel@tonic-gate         m = s->heap[SMALLEST]; /* m = node of next least frequency */
2029*7c478bd9Sstevel@tonic-gate 
2030*7c478bd9Sstevel@tonic-gate         s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
2031*7c478bd9Sstevel@tonic-gate         s->heap[--(s->heap_max)] = m;
2032*7c478bd9Sstevel@tonic-gate 
2033*7c478bd9Sstevel@tonic-gate         /* Create a new node father of n and m */
2034*7c478bd9Sstevel@tonic-gate         tree[node].Freq = tree[n].Freq + tree[m].Freq;
2035*7c478bd9Sstevel@tonic-gate         s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
2036*7c478bd9Sstevel@tonic-gate         tree[n].Dad = tree[m].Dad = (ush)node;
2037*7c478bd9Sstevel@tonic-gate #ifdef DUMP_BL_TREE
2038*7c478bd9Sstevel@tonic-gate         if (tree == s->bl_tree) {
2039*7c478bd9Sstevel@tonic-gate             fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
2040*7c478bd9Sstevel@tonic-gate                     node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
2041*7c478bd9Sstevel@tonic-gate         }
2042*7c478bd9Sstevel@tonic-gate #endif
2043*7c478bd9Sstevel@tonic-gate         /* and insert the new node in the heap */
2044*7c478bd9Sstevel@tonic-gate         s->heap[SMALLEST] = node++;
2045*7c478bd9Sstevel@tonic-gate         pqdownheap(s, tree, SMALLEST);
2046*7c478bd9Sstevel@tonic-gate 
2047*7c478bd9Sstevel@tonic-gate     } while (s->heap_len >= 2);
2048*7c478bd9Sstevel@tonic-gate 
2049*7c478bd9Sstevel@tonic-gate     s->heap[--(s->heap_max)] = s->heap[SMALLEST];
2050*7c478bd9Sstevel@tonic-gate 
2051*7c478bd9Sstevel@tonic-gate     /* At this point, the fields freq and dad are set. We can now
2052*7c478bd9Sstevel@tonic-gate      * generate the bit lengths.
2053*7c478bd9Sstevel@tonic-gate      */
2054*7c478bd9Sstevel@tonic-gate     gen_bitlen(s, (tree_desc *)desc);
2055*7c478bd9Sstevel@tonic-gate 
2056*7c478bd9Sstevel@tonic-gate     /* The field len is now set, we can generate the bit codes */
2057*7c478bd9Sstevel@tonic-gate     gen_codes ((ct_data *)tree, max_code, s->bl_count);
2058*7c478bd9Sstevel@tonic-gate }
2059*7c478bd9Sstevel@tonic-gate 
2060*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2061*7c478bd9Sstevel@tonic-gate  * Scan a literal or distance tree to determine the frequencies of the codes
2062*7c478bd9Sstevel@tonic-gate  * in the bit length tree.
2063*7c478bd9Sstevel@tonic-gate  */
2064*7c478bd9Sstevel@tonic-gate local void scan_tree (s, tree, max_code)
2065*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2066*7c478bd9Sstevel@tonic-gate     ct_data *tree;   /* the tree to be scanned */
2067*7c478bd9Sstevel@tonic-gate     int max_code;    /* and its largest code of non zero frequency */
2068*7c478bd9Sstevel@tonic-gate {
2069*7c478bd9Sstevel@tonic-gate     int n;                     /* iterates over all tree elements */
2070*7c478bd9Sstevel@tonic-gate     int prevlen = -1;          /* last emitted length */
2071*7c478bd9Sstevel@tonic-gate     int curlen;                /* length of current code */
2072*7c478bd9Sstevel@tonic-gate     int nextlen = tree[0].Len; /* length of next code */
2073*7c478bd9Sstevel@tonic-gate     int count = 0;             /* repeat count of the current code */
2074*7c478bd9Sstevel@tonic-gate     int max_count = 7;         /* max repeat count */
2075*7c478bd9Sstevel@tonic-gate     int min_count = 4;         /* min repeat count */
2076*7c478bd9Sstevel@tonic-gate 
2077*7c478bd9Sstevel@tonic-gate     if (nextlen == 0) max_count = 138, min_count = 3;
2078*7c478bd9Sstevel@tonic-gate     tree[max_code+1].Len = (ush)0xffff; /* guard */
2079*7c478bd9Sstevel@tonic-gate 
2080*7c478bd9Sstevel@tonic-gate     for (n = 0; n <= max_code; n++) {
2081*7c478bd9Sstevel@tonic-gate         curlen = nextlen; nextlen = tree[n+1].Len;
2082*7c478bd9Sstevel@tonic-gate         if (++count < max_count && curlen == nextlen) {
2083*7c478bd9Sstevel@tonic-gate             continue;
2084*7c478bd9Sstevel@tonic-gate         } else if (count < min_count) {
2085*7c478bd9Sstevel@tonic-gate             s->bl_tree[curlen].Freq += count;
2086*7c478bd9Sstevel@tonic-gate         } else if (curlen != 0) {
2087*7c478bd9Sstevel@tonic-gate             if (curlen != prevlen) s->bl_tree[curlen].Freq++;
2088*7c478bd9Sstevel@tonic-gate             s->bl_tree[REP_3_6].Freq++;
2089*7c478bd9Sstevel@tonic-gate         } else if (count <= 10) {
2090*7c478bd9Sstevel@tonic-gate             s->bl_tree[REPZ_3_10].Freq++;
2091*7c478bd9Sstevel@tonic-gate         } else {
2092*7c478bd9Sstevel@tonic-gate             s->bl_tree[REPZ_11_138].Freq++;
2093*7c478bd9Sstevel@tonic-gate         }
2094*7c478bd9Sstevel@tonic-gate         count = 0; prevlen = curlen;
2095*7c478bd9Sstevel@tonic-gate         if (nextlen == 0) {
2096*7c478bd9Sstevel@tonic-gate             max_count = 138, min_count = 3;
2097*7c478bd9Sstevel@tonic-gate         } else if (curlen == nextlen) {
2098*7c478bd9Sstevel@tonic-gate             max_count = 6, min_count = 3;
2099*7c478bd9Sstevel@tonic-gate         } else {
2100*7c478bd9Sstevel@tonic-gate             max_count = 7, min_count = 4;
2101*7c478bd9Sstevel@tonic-gate         }
2102*7c478bd9Sstevel@tonic-gate     }
2103*7c478bd9Sstevel@tonic-gate }
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2106*7c478bd9Sstevel@tonic-gate  * Send a literal or distance tree in compressed form, using the codes in
2107*7c478bd9Sstevel@tonic-gate  * bl_tree.
2108*7c478bd9Sstevel@tonic-gate  */
2109*7c478bd9Sstevel@tonic-gate local void send_tree (s, tree, max_code)
2110*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2111*7c478bd9Sstevel@tonic-gate     ct_data *tree; /* the tree to be scanned */
2112*7c478bd9Sstevel@tonic-gate     int max_code;       /* and its largest code of non zero frequency */
2113*7c478bd9Sstevel@tonic-gate {
2114*7c478bd9Sstevel@tonic-gate     int n;                     /* iterates over all tree elements */
2115*7c478bd9Sstevel@tonic-gate     int prevlen = -1;          /* last emitted length */
2116*7c478bd9Sstevel@tonic-gate     int curlen;                /* length of current code */
2117*7c478bd9Sstevel@tonic-gate     int nextlen = tree[0].Len; /* length of next code */
2118*7c478bd9Sstevel@tonic-gate     int count = 0;             /* repeat count of the current code */
2119*7c478bd9Sstevel@tonic-gate     int max_count = 7;         /* max repeat count */
2120*7c478bd9Sstevel@tonic-gate     int min_count = 4;         /* min repeat count */
2121*7c478bd9Sstevel@tonic-gate 
2122*7c478bd9Sstevel@tonic-gate     /* tree[max_code+1].Len = -1; */  /* guard already set */
2123*7c478bd9Sstevel@tonic-gate     if (nextlen == 0) max_count = 138, min_count = 3;
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate     for (n = 0; n <= max_code; n++) {
2126*7c478bd9Sstevel@tonic-gate         curlen = nextlen; nextlen = tree[n+1].Len;
2127*7c478bd9Sstevel@tonic-gate         if (++count < max_count && curlen == nextlen) {
2128*7c478bd9Sstevel@tonic-gate             continue;
2129*7c478bd9Sstevel@tonic-gate         } else if (count < min_count) {
2130*7c478bd9Sstevel@tonic-gate             do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate         } else if (curlen != 0) {
2133*7c478bd9Sstevel@tonic-gate             if (curlen != prevlen) {
2134*7c478bd9Sstevel@tonic-gate                 send_code(s, curlen, s->bl_tree); count--;
2135*7c478bd9Sstevel@tonic-gate             }
2136*7c478bd9Sstevel@tonic-gate             Assert(count >= 3 && count <= 6, " 3_6?");
2137*7c478bd9Sstevel@tonic-gate             send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate         } else if (count <= 10) {
2140*7c478bd9Sstevel@tonic-gate             send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
2141*7c478bd9Sstevel@tonic-gate 
2142*7c478bd9Sstevel@tonic-gate         } else {
2143*7c478bd9Sstevel@tonic-gate             send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
2144*7c478bd9Sstevel@tonic-gate         }
2145*7c478bd9Sstevel@tonic-gate         count = 0; prevlen = curlen;
2146*7c478bd9Sstevel@tonic-gate         if (nextlen == 0) {
2147*7c478bd9Sstevel@tonic-gate             max_count = 138, min_count = 3;
2148*7c478bd9Sstevel@tonic-gate         } else if (curlen == nextlen) {
2149*7c478bd9Sstevel@tonic-gate             max_count = 6, min_count = 3;
2150*7c478bd9Sstevel@tonic-gate         } else {
2151*7c478bd9Sstevel@tonic-gate             max_count = 7, min_count = 4;
2152*7c478bd9Sstevel@tonic-gate         }
2153*7c478bd9Sstevel@tonic-gate     }
2154*7c478bd9Sstevel@tonic-gate }
2155*7c478bd9Sstevel@tonic-gate 
2156*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2157*7c478bd9Sstevel@tonic-gate  * Construct the Huffman tree for the bit lengths and return the index in
2158*7c478bd9Sstevel@tonic-gate  * bl_order of the last bit length code to send.
2159*7c478bd9Sstevel@tonic-gate  */
2160*7c478bd9Sstevel@tonic-gate local int build_bl_tree(s)
2161*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2162*7c478bd9Sstevel@tonic-gate {
2163*7c478bd9Sstevel@tonic-gate     int max_blindex;  /* index of last bit length code of non zero freq */
2164*7c478bd9Sstevel@tonic-gate 
2165*7c478bd9Sstevel@tonic-gate     /* Determine the bit length frequencies for literal and distance trees */
2166*7c478bd9Sstevel@tonic-gate     scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
2167*7c478bd9Sstevel@tonic-gate     scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
2168*7c478bd9Sstevel@tonic-gate 
2169*7c478bd9Sstevel@tonic-gate     /* Build the bit length tree: */
2170*7c478bd9Sstevel@tonic-gate     build_tree(s, (tree_desc *)(&(s->bl_desc)));
2171*7c478bd9Sstevel@tonic-gate     /* opt_len now includes the length of the tree representations, except
2172*7c478bd9Sstevel@tonic-gate      * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
2173*7c478bd9Sstevel@tonic-gate      */
2174*7c478bd9Sstevel@tonic-gate 
2175*7c478bd9Sstevel@tonic-gate     /* Determine the number of bit length codes to send. The pkzip format
2176*7c478bd9Sstevel@tonic-gate      * requires that at least 4 bit length codes be sent. (appnote.txt says
2177*7c478bd9Sstevel@tonic-gate      * 3 but the actual value used is 4.)
2178*7c478bd9Sstevel@tonic-gate      */
2179*7c478bd9Sstevel@tonic-gate     for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
2180*7c478bd9Sstevel@tonic-gate         if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
2181*7c478bd9Sstevel@tonic-gate     }
2182*7c478bd9Sstevel@tonic-gate     /* Update opt_len to include the bit length tree and counts */
2183*7c478bd9Sstevel@tonic-gate     s->opt_len += 3*(max_blindex+1) + 5+5+4;
2184*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
2185*7c478bd9Sstevel@tonic-gate             s->opt_len, s->static_len));
2186*7c478bd9Sstevel@tonic-gate 
2187*7c478bd9Sstevel@tonic-gate     return max_blindex;
2188*7c478bd9Sstevel@tonic-gate }
2189*7c478bd9Sstevel@tonic-gate 
2190*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2191*7c478bd9Sstevel@tonic-gate  * Send the header for a block using dynamic Huffman trees: the counts, the
2192*7c478bd9Sstevel@tonic-gate  * lengths of the bit length codes, the literal tree and the distance tree.
2193*7c478bd9Sstevel@tonic-gate  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
2194*7c478bd9Sstevel@tonic-gate  */
2195*7c478bd9Sstevel@tonic-gate local void send_all_trees(s, lcodes, dcodes, blcodes)
2196*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2197*7c478bd9Sstevel@tonic-gate     int lcodes, dcodes, blcodes; /* number of codes for each tree */
2198*7c478bd9Sstevel@tonic-gate {
2199*7c478bd9Sstevel@tonic-gate     int rank;                    /* index in bl_order */
2200*7c478bd9Sstevel@tonic-gate 
2201*7c478bd9Sstevel@tonic-gate     Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
2202*7c478bd9Sstevel@tonic-gate     Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
2203*7c478bd9Sstevel@tonic-gate             "too many codes");
2204*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\nbl counts: "));
2205*7c478bd9Sstevel@tonic-gate     send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
2206*7c478bd9Sstevel@tonic-gate     send_bits(s, dcodes-1,   5);
2207*7c478bd9Sstevel@tonic-gate     send_bits(s, blcodes-4,  4); /* not -3 as stated in appnote.txt */
2208*7c478bd9Sstevel@tonic-gate     for (rank = 0; rank < blcodes; rank++) {
2209*7c478bd9Sstevel@tonic-gate         Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
2210*7c478bd9Sstevel@tonic-gate         send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
2211*7c478bd9Sstevel@tonic-gate     }
2212*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
2213*7c478bd9Sstevel@tonic-gate 
2214*7c478bd9Sstevel@tonic-gate     send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
2215*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
2216*7c478bd9Sstevel@tonic-gate 
2217*7c478bd9Sstevel@tonic-gate     send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
2218*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
2219*7c478bd9Sstevel@tonic-gate }
2220*7c478bd9Sstevel@tonic-gate 
2221*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2222*7c478bd9Sstevel@tonic-gate  * Send a stored block
2223*7c478bd9Sstevel@tonic-gate  */
2224*7c478bd9Sstevel@tonic-gate local void ct_stored_block(s, buf, stored_len, eof)
2225*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2226*7c478bd9Sstevel@tonic-gate     charf *buf;       /* input block */
2227*7c478bd9Sstevel@tonic-gate     ulg stored_len;   /* length of input block */
2228*7c478bd9Sstevel@tonic-gate     int eof;          /* true if this is the last block for a file */
2229*7c478bd9Sstevel@tonic-gate {
2230*7c478bd9Sstevel@tonic-gate     send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
2231*7c478bd9Sstevel@tonic-gate     s->compressed_len = (s->compressed_len + 3 + 7) & ~7L;
2232*7c478bd9Sstevel@tonic-gate     s->compressed_len += (stored_len + 4) << 3;
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate     copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
2235*7c478bd9Sstevel@tonic-gate }
2236*7c478bd9Sstevel@tonic-gate 
2237*7c478bd9Sstevel@tonic-gate /* Send just the `stored block' type code without any length bytes or data.
2238*7c478bd9Sstevel@tonic-gate  */
2239*7c478bd9Sstevel@tonic-gate local void ct_stored_type_only(s)
2240*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2241*7c478bd9Sstevel@tonic-gate {
2242*7c478bd9Sstevel@tonic-gate     send_bits(s, (STORED_BLOCK << 1), 3);
2243*7c478bd9Sstevel@tonic-gate     bi_windup(s);
2244*7c478bd9Sstevel@tonic-gate     s->compressed_len = (s->compressed_len + 3) & ~7L;
2245*7c478bd9Sstevel@tonic-gate }
2246*7c478bd9Sstevel@tonic-gate 
2247*7c478bd9Sstevel@tonic-gate 
2248*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2249*7c478bd9Sstevel@tonic-gate  * Send one empty static block to give enough lookahead for inflate.
2250*7c478bd9Sstevel@tonic-gate  * This takes 10 bits, of which 7 may remain in the bit buffer.
2251*7c478bd9Sstevel@tonic-gate  * The current inflate code requires 9 bits of lookahead. If the EOB
2252*7c478bd9Sstevel@tonic-gate  * code for the previous block was coded on 5 bits or less, inflate
2253*7c478bd9Sstevel@tonic-gate  * may have only 5+3 bits of lookahead to decode this EOB.
2254*7c478bd9Sstevel@tonic-gate  * (There are no problems if the previous block is stored or fixed.)
2255*7c478bd9Sstevel@tonic-gate  */
2256*7c478bd9Sstevel@tonic-gate local void ct_align(s)
2257*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2258*7c478bd9Sstevel@tonic-gate {
2259*7c478bd9Sstevel@tonic-gate     send_bits(s, STATIC_TREES<<1, 3);
2260*7c478bd9Sstevel@tonic-gate     send_code(s, END_BLOCK, static_ltree);
2261*7c478bd9Sstevel@tonic-gate     s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
2262*7c478bd9Sstevel@tonic-gate     bi_flush(s);
2263*7c478bd9Sstevel@tonic-gate     /* Of the 10 bits for the empty block, we have already sent
2264*7c478bd9Sstevel@tonic-gate      * (10 - bi_valid) bits. The lookahead for the EOB of the previous
2265*7c478bd9Sstevel@tonic-gate      * block was thus its length plus what we have just sent.
2266*7c478bd9Sstevel@tonic-gate      */
2267*7c478bd9Sstevel@tonic-gate     if (s->last_eob_len + 10 - s->bi_valid < 9) {
2268*7c478bd9Sstevel@tonic-gate         send_bits(s, STATIC_TREES<<1, 3);
2269*7c478bd9Sstevel@tonic-gate         send_code(s, END_BLOCK, static_ltree);
2270*7c478bd9Sstevel@tonic-gate         s->compressed_len += 10L;
2271*7c478bd9Sstevel@tonic-gate         bi_flush(s);
2272*7c478bd9Sstevel@tonic-gate     }
2273*7c478bd9Sstevel@tonic-gate     s->last_eob_len = 7;
2274*7c478bd9Sstevel@tonic-gate }
2275*7c478bd9Sstevel@tonic-gate 
2276*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2277*7c478bd9Sstevel@tonic-gate  * Determine the best encoding for the current block: dynamic trees, static
2278*7c478bd9Sstevel@tonic-gate  * trees or store, and output the encoded block to the zip file. This function
2279*7c478bd9Sstevel@tonic-gate  * returns the total compressed length for the file so far.
2280*7c478bd9Sstevel@tonic-gate  */
2281*7c478bd9Sstevel@tonic-gate local ulg ct_flush_block(s, buf, stored_len, flush)
2282*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2283*7c478bd9Sstevel@tonic-gate     charf *buf;       /* input block, or NULL if too old */
2284*7c478bd9Sstevel@tonic-gate     ulg stored_len;   /* length of input block */
2285*7c478bd9Sstevel@tonic-gate     int flush;        /* Z_FINISH if this is the last block for a file */
2286*7c478bd9Sstevel@tonic-gate {
2287*7c478bd9Sstevel@tonic-gate     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
2288*7c478bd9Sstevel@tonic-gate     int max_blindex;  /* index of last bit length code of non zero freq */
2289*7c478bd9Sstevel@tonic-gate     int eof = flush == Z_FINISH;
2290*7c478bd9Sstevel@tonic-gate 
2291*7c478bd9Sstevel@tonic-gate     ++s->blocks_in_packet;
2292*7c478bd9Sstevel@tonic-gate 
2293*7c478bd9Sstevel@tonic-gate     /* Check if the file is ascii or binary */
2294*7c478bd9Sstevel@tonic-gate     if (s->data_type == UNKNOWN) set_data_type(s);
2295*7c478bd9Sstevel@tonic-gate 
2296*7c478bd9Sstevel@tonic-gate     /* Construct the literal and distance trees */
2297*7c478bd9Sstevel@tonic-gate     build_tree(s, (tree_desc *)(&(s->l_desc)));
2298*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
2299*7c478bd9Sstevel@tonic-gate             s->static_len));
2300*7c478bd9Sstevel@tonic-gate 
2301*7c478bd9Sstevel@tonic-gate     build_tree(s, (tree_desc *)(&(s->d_desc)));
2302*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
2303*7c478bd9Sstevel@tonic-gate             s->static_len));
2304*7c478bd9Sstevel@tonic-gate     /* At this point, opt_len and static_len are the total bit lengths of
2305*7c478bd9Sstevel@tonic-gate      * the compressed block data, excluding the tree representations.
2306*7c478bd9Sstevel@tonic-gate      */
2307*7c478bd9Sstevel@tonic-gate 
2308*7c478bd9Sstevel@tonic-gate     /* Build the bit length tree for the above two trees, and get the index
2309*7c478bd9Sstevel@tonic-gate      * in bl_order of the last bit length code to send.
2310*7c478bd9Sstevel@tonic-gate      */
2311*7c478bd9Sstevel@tonic-gate     max_blindex = build_bl_tree(s);
2312*7c478bd9Sstevel@tonic-gate 
2313*7c478bd9Sstevel@tonic-gate     /* Determine the best encoding. Compute first the block length in bytes */
2314*7c478bd9Sstevel@tonic-gate     opt_lenb = (s->opt_len+3+7)>>3;
2315*7c478bd9Sstevel@tonic-gate     static_lenb = (s->static_len+3+7)>>3;
2316*7c478bd9Sstevel@tonic-gate 
2317*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
2318*7c478bd9Sstevel@tonic-gate             opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
2319*7c478bd9Sstevel@tonic-gate             s->last_lit));
2320*7c478bd9Sstevel@tonic-gate 
2321*7c478bd9Sstevel@tonic-gate     if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
2322*7c478bd9Sstevel@tonic-gate 
2323*7c478bd9Sstevel@tonic-gate     /* If compression failed and this is the first and last block,
2324*7c478bd9Sstevel@tonic-gate      * and if the .zip file can be seeked (to rewrite the local header),
2325*7c478bd9Sstevel@tonic-gate      * the whole file is transformed into a stored file:
2326*7c478bd9Sstevel@tonic-gate      */
2327*7c478bd9Sstevel@tonic-gate #ifdef STORED_FILE_OK
2328*7c478bd9Sstevel@tonic-gate #  ifdef FORCE_STORED_FILE
2329*7c478bd9Sstevel@tonic-gate     if (eof && compressed_len == 0L) /* force stored file */
2330*7c478bd9Sstevel@tonic-gate #  else
2331*7c478bd9Sstevel@tonic-gate     if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable())
2332*7c478bd9Sstevel@tonic-gate #  endif
2333*7c478bd9Sstevel@tonic-gate     {
2334*7c478bd9Sstevel@tonic-gate         /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
2335*7c478bd9Sstevel@tonic-gate         if (buf == (charf*)0) error ("block vanished");
2336*7c478bd9Sstevel@tonic-gate 
2337*7c478bd9Sstevel@tonic-gate         copy_block(buf, (unsigned)stored_len, 0); /* without header */
2338*7c478bd9Sstevel@tonic-gate         s->compressed_len = stored_len << 3;
2339*7c478bd9Sstevel@tonic-gate         s->method = STORED;
2340*7c478bd9Sstevel@tonic-gate     } else
2341*7c478bd9Sstevel@tonic-gate #endif /* STORED_FILE_OK */
2342*7c478bd9Sstevel@tonic-gate 
2343*7c478bd9Sstevel@tonic-gate     /* For Z_PACKET_FLUSH, if we don't achieve the required minimum
2344*7c478bd9Sstevel@tonic-gate      * compression, and this block contains all the data since the last
2345*7c478bd9Sstevel@tonic-gate      * time we used Z_PACKET_FLUSH, then just omit this block completely
2346*7c478bd9Sstevel@tonic-gate      * from the output.
2347*7c478bd9Sstevel@tonic-gate      */
2348*7c478bd9Sstevel@tonic-gate     if (flush == Z_PACKET_FLUSH && s->blocks_in_packet == 1
2349*7c478bd9Sstevel@tonic-gate 	&& opt_lenb > stored_len - s->minCompr) {
2350*7c478bd9Sstevel@tonic-gate 	s->blocks_in_packet = 0;
2351*7c478bd9Sstevel@tonic-gate 	/* output nothing */
2352*7c478bd9Sstevel@tonic-gate     } else
2353*7c478bd9Sstevel@tonic-gate 
2354*7c478bd9Sstevel@tonic-gate #ifdef FORCE_STORED
2355*7c478bd9Sstevel@tonic-gate     if (buf != (char*)0) /* force stored block */
2356*7c478bd9Sstevel@tonic-gate #else
2357*7c478bd9Sstevel@tonic-gate     if (stored_len+4 <= opt_lenb && buf != (char*)0)
2358*7c478bd9Sstevel@tonic-gate                        /* 4: two words for the lengths */
2359*7c478bd9Sstevel@tonic-gate #endif
2360*7c478bd9Sstevel@tonic-gate     {
2361*7c478bd9Sstevel@tonic-gate         /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
2362*7c478bd9Sstevel@tonic-gate          * Otherwise we can't have processed more than WSIZE input bytes since
2363*7c478bd9Sstevel@tonic-gate          * the last block flush, because compression would have been
2364*7c478bd9Sstevel@tonic-gate          * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
2365*7c478bd9Sstevel@tonic-gate          * transform a block into a stored block.
2366*7c478bd9Sstevel@tonic-gate          */
2367*7c478bd9Sstevel@tonic-gate         ct_stored_block(s, buf, stored_len, eof);
2368*7c478bd9Sstevel@tonic-gate     } else
2369*7c478bd9Sstevel@tonic-gate 
2370*7c478bd9Sstevel@tonic-gate #ifdef FORCE_STATIC
2371*7c478bd9Sstevel@tonic-gate     if (static_lenb >= 0) /* force static trees */
2372*7c478bd9Sstevel@tonic-gate #else
2373*7c478bd9Sstevel@tonic-gate     if (static_lenb == opt_lenb)
2374*7c478bd9Sstevel@tonic-gate #endif
2375*7c478bd9Sstevel@tonic-gate     {
2376*7c478bd9Sstevel@tonic-gate         send_bits(s, (STATIC_TREES<<1)+eof, 3);
2377*7c478bd9Sstevel@tonic-gate         compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
2378*7c478bd9Sstevel@tonic-gate         s->compressed_len += 3 + s->static_len;
2379*7c478bd9Sstevel@tonic-gate     } else {
2380*7c478bd9Sstevel@tonic-gate         send_bits(s, (DYN_TREES<<1)+eof, 3);
2381*7c478bd9Sstevel@tonic-gate         send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
2382*7c478bd9Sstevel@tonic-gate                        max_blindex+1);
2383*7c478bd9Sstevel@tonic-gate         compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
2384*7c478bd9Sstevel@tonic-gate         s->compressed_len += 3 + s->opt_len;
2385*7c478bd9Sstevel@tonic-gate     }
2386*7c478bd9Sstevel@tonic-gate     Assert (s->compressed_len == s->bits_sent, "bad compressed size");
2387*7c478bd9Sstevel@tonic-gate     init_block(s);
2388*7c478bd9Sstevel@tonic-gate 
2389*7c478bd9Sstevel@tonic-gate     if (eof) {
2390*7c478bd9Sstevel@tonic-gate         bi_windup(s);
2391*7c478bd9Sstevel@tonic-gate         s->compressed_len += 7;  /* align on byte boundary */
2392*7c478bd9Sstevel@tonic-gate     }
2393*7c478bd9Sstevel@tonic-gate     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
2394*7c478bd9Sstevel@tonic-gate            s->compressed_len-7*eof));
2395*7c478bd9Sstevel@tonic-gate 
2396*7c478bd9Sstevel@tonic-gate     return s->compressed_len >> 3;
2397*7c478bd9Sstevel@tonic-gate }
2398*7c478bd9Sstevel@tonic-gate 
2399*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2400*7c478bd9Sstevel@tonic-gate  * Save the match info and tally the frequency counts. Return true if
2401*7c478bd9Sstevel@tonic-gate  * the current block must be flushed.
2402*7c478bd9Sstevel@tonic-gate  */
2403*7c478bd9Sstevel@tonic-gate local int ct_tally (s, dist, lc)
2404*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2405*7c478bd9Sstevel@tonic-gate     int dist;  /* distance of matched string */
2406*7c478bd9Sstevel@tonic-gate     int lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
2407*7c478bd9Sstevel@tonic-gate {
2408*7c478bd9Sstevel@tonic-gate     s->d_buf[s->last_lit] = (ush)dist;
2409*7c478bd9Sstevel@tonic-gate     s->l_buf[s->last_lit++] = (uch)lc;
2410*7c478bd9Sstevel@tonic-gate     if (dist == 0) {
2411*7c478bd9Sstevel@tonic-gate         /* lc is the unmatched char */
2412*7c478bd9Sstevel@tonic-gate         s->dyn_ltree[lc].Freq++;
2413*7c478bd9Sstevel@tonic-gate     } else {
2414*7c478bd9Sstevel@tonic-gate         s->matches++;
2415*7c478bd9Sstevel@tonic-gate         /* Here, lc is the match length - MIN_MATCH */
2416*7c478bd9Sstevel@tonic-gate         dist--;             /* dist = match distance - 1 */
2417*7c478bd9Sstevel@tonic-gate         Assert((ush)dist < (ush)MAX_DIST(s) &&
2418*7c478bd9Sstevel@tonic-gate                (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
2419*7c478bd9Sstevel@tonic-gate                (ush)d_code(dist) < (ush)D_CODES,  "ct_tally: bad match");
2420*7c478bd9Sstevel@tonic-gate 
2421*7c478bd9Sstevel@tonic-gate         s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
2422*7c478bd9Sstevel@tonic-gate         s->dyn_dtree[d_code(dist)].Freq++;
2423*7c478bd9Sstevel@tonic-gate     }
2424*7c478bd9Sstevel@tonic-gate 
2425*7c478bd9Sstevel@tonic-gate     /* Try to guess if it is profitable to stop the current block here */
2426*7c478bd9Sstevel@tonic-gate     if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
2427*7c478bd9Sstevel@tonic-gate         /* Compute an upper bound for the compressed length */
2428*7c478bd9Sstevel@tonic-gate         ulg out_length = (ulg)s->last_lit*8L;
2429*7c478bd9Sstevel@tonic-gate         ulg in_length = (ulg)s->strstart - s->block_start;
2430*7c478bd9Sstevel@tonic-gate         int dcode;
2431*7c478bd9Sstevel@tonic-gate         for (dcode = 0; dcode < D_CODES; dcode++) {
2432*7c478bd9Sstevel@tonic-gate             out_length += (ulg)s->dyn_dtree[dcode].Freq *
2433*7c478bd9Sstevel@tonic-gate                 (5L+extra_dbits[dcode]);
2434*7c478bd9Sstevel@tonic-gate         }
2435*7c478bd9Sstevel@tonic-gate         out_length >>= 3;
2436*7c478bd9Sstevel@tonic-gate         Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
2437*7c478bd9Sstevel@tonic-gate                s->last_lit, in_length, out_length,
2438*7c478bd9Sstevel@tonic-gate                100L - out_length*100L/in_length));
2439*7c478bd9Sstevel@tonic-gate         if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
2440*7c478bd9Sstevel@tonic-gate     }
2441*7c478bd9Sstevel@tonic-gate     return (s->last_lit == s->lit_bufsize-1);
2442*7c478bd9Sstevel@tonic-gate     /* We avoid equality with lit_bufsize because of wraparound at 64K
2443*7c478bd9Sstevel@tonic-gate      * on 16 bit machines and because stored blocks are restricted to
2444*7c478bd9Sstevel@tonic-gate      * 64K-1 bytes.
2445*7c478bd9Sstevel@tonic-gate      */
2446*7c478bd9Sstevel@tonic-gate }
2447*7c478bd9Sstevel@tonic-gate 
2448*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2449*7c478bd9Sstevel@tonic-gate  * Send the block data compressed using the given Huffman trees
2450*7c478bd9Sstevel@tonic-gate  */
2451*7c478bd9Sstevel@tonic-gate local void compress_block(s, ltree, dtree)
2452*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2453*7c478bd9Sstevel@tonic-gate     ct_data *ltree; /* literal tree */
2454*7c478bd9Sstevel@tonic-gate     ct_data *dtree; /* distance tree */
2455*7c478bd9Sstevel@tonic-gate {
2456*7c478bd9Sstevel@tonic-gate     unsigned dist;      /* distance of matched string */
2457*7c478bd9Sstevel@tonic-gate     int lc;             /* match length or unmatched char (if dist == 0) */
2458*7c478bd9Sstevel@tonic-gate     unsigned lx = 0;    /* running index in l_buf */
2459*7c478bd9Sstevel@tonic-gate     unsigned code;      /* the code to send */
2460*7c478bd9Sstevel@tonic-gate     int extra;          /* number of extra bits to send */
2461*7c478bd9Sstevel@tonic-gate 
2462*7c478bd9Sstevel@tonic-gate     if (s->last_lit != 0) do {
2463*7c478bd9Sstevel@tonic-gate         dist = s->d_buf[lx];
2464*7c478bd9Sstevel@tonic-gate         lc = s->l_buf[lx++];
2465*7c478bd9Sstevel@tonic-gate         if (dist == 0) {
2466*7c478bd9Sstevel@tonic-gate             send_code(s, lc, ltree); /* send a literal byte */
2467*7c478bd9Sstevel@tonic-gate             Tracecv(isgraph(lc), (stderr," '%c' ", lc));
2468*7c478bd9Sstevel@tonic-gate         } else {
2469*7c478bd9Sstevel@tonic-gate             /* Here, lc is the match length - MIN_MATCH */
2470*7c478bd9Sstevel@tonic-gate             code = length_code[lc];
2471*7c478bd9Sstevel@tonic-gate             send_code(s, code+LITERALS+1, ltree); /* send the length code */
2472*7c478bd9Sstevel@tonic-gate             extra = extra_lbits[code];
2473*7c478bd9Sstevel@tonic-gate             if (extra != 0) {
2474*7c478bd9Sstevel@tonic-gate                 lc -= base_length[code];
2475*7c478bd9Sstevel@tonic-gate                 send_bits(s, lc, extra);       /* send the extra length bits */
2476*7c478bd9Sstevel@tonic-gate             }
2477*7c478bd9Sstevel@tonic-gate             dist--; /* dist is now the match distance - 1 */
2478*7c478bd9Sstevel@tonic-gate             code = d_code(dist);
2479*7c478bd9Sstevel@tonic-gate             Assert (code < D_CODES, "bad d_code");
2480*7c478bd9Sstevel@tonic-gate 
2481*7c478bd9Sstevel@tonic-gate             send_code(s, code, dtree);       /* send the distance code */
2482*7c478bd9Sstevel@tonic-gate             extra = extra_dbits[code];
2483*7c478bd9Sstevel@tonic-gate             if (extra != 0) {
2484*7c478bd9Sstevel@tonic-gate                 dist -= base_dist[code];
2485*7c478bd9Sstevel@tonic-gate                 send_bits(s, dist, extra);   /* send the extra distance bits */
2486*7c478bd9Sstevel@tonic-gate             }
2487*7c478bd9Sstevel@tonic-gate         } /* literal or match pair ? */
2488*7c478bd9Sstevel@tonic-gate 
2489*7c478bd9Sstevel@tonic-gate         /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
2490*7c478bd9Sstevel@tonic-gate         Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
2491*7c478bd9Sstevel@tonic-gate 
2492*7c478bd9Sstevel@tonic-gate     } while (lx < s->last_lit);
2493*7c478bd9Sstevel@tonic-gate 
2494*7c478bd9Sstevel@tonic-gate     send_code(s, END_BLOCK, ltree);
2495*7c478bd9Sstevel@tonic-gate     s->last_eob_len = ltree[END_BLOCK].Len;
2496*7c478bd9Sstevel@tonic-gate }
2497*7c478bd9Sstevel@tonic-gate 
2498*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2499*7c478bd9Sstevel@tonic-gate  * Set the data type to ASCII or BINARY, using a crude approximation:
2500*7c478bd9Sstevel@tonic-gate  * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
2501*7c478bd9Sstevel@tonic-gate  * IN assertion: the fields freq of dyn_ltree are set and the total of all
2502*7c478bd9Sstevel@tonic-gate  * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
2503*7c478bd9Sstevel@tonic-gate  */
2504*7c478bd9Sstevel@tonic-gate local void set_data_type(s)
2505*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2506*7c478bd9Sstevel@tonic-gate {
2507*7c478bd9Sstevel@tonic-gate     int n = 0;
2508*7c478bd9Sstevel@tonic-gate     unsigned ascii_freq = 0;
2509*7c478bd9Sstevel@tonic-gate     unsigned bin_freq = 0;
2510*7c478bd9Sstevel@tonic-gate     while (n < 7)        bin_freq += s->dyn_ltree[n++].Freq;
2511*7c478bd9Sstevel@tonic-gate     while (n < 128)    ascii_freq += s->dyn_ltree[n++].Freq;
2512*7c478bd9Sstevel@tonic-gate     while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
2513*7c478bd9Sstevel@tonic-gate     s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? BINARY : ASCII);
2514*7c478bd9Sstevel@tonic-gate }
2515*7c478bd9Sstevel@tonic-gate 
2516*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2517*7c478bd9Sstevel@tonic-gate  * Reverse the first len bits of a code, using straightforward code (a faster
2518*7c478bd9Sstevel@tonic-gate  * method would use a table)
2519*7c478bd9Sstevel@tonic-gate  * IN assertion: 1 <= len <= 15
2520*7c478bd9Sstevel@tonic-gate  */
2521*7c478bd9Sstevel@tonic-gate local unsigned bi_reverse(code, len)
2522*7c478bd9Sstevel@tonic-gate     unsigned code; /* the value to invert */
2523*7c478bd9Sstevel@tonic-gate     int len;       /* its bit length */
2524*7c478bd9Sstevel@tonic-gate {
2525*7c478bd9Sstevel@tonic-gate     register unsigned res = 0;
2526*7c478bd9Sstevel@tonic-gate     do {
2527*7c478bd9Sstevel@tonic-gate         res |= code & 1;
2528*7c478bd9Sstevel@tonic-gate         code >>= 1, res <<= 1;
2529*7c478bd9Sstevel@tonic-gate     } while (--len > 0);
2530*7c478bd9Sstevel@tonic-gate     return res >> 1;
2531*7c478bd9Sstevel@tonic-gate }
2532*7c478bd9Sstevel@tonic-gate 
2533*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2534*7c478bd9Sstevel@tonic-gate  * Flush the bit buffer, keeping at most 7 bits in it.
2535*7c478bd9Sstevel@tonic-gate  */
2536*7c478bd9Sstevel@tonic-gate local void bi_flush(s)
2537*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2538*7c478bd9Sstevel@tonic-gate {
2539*7c478bd9Sstevel@tonic-gate     if (s->bi_valid == 16) {
2540*7c478bd9Sstevel@tonic-gate         put_short(s, s->bi_buf);
2541*7c478bd9Sstevel@tonic-gate         s->bi_buf = 0;
2542*7c478bd9Sstevel@tonic-gate         s->bi_valid = 0;
2543*7c478bd9Sstevel@tonic-gate     } else if (s->bi_valid >= 8) {
2544*7c478bd9Sstevel@tonic-gate         put_byte(s, (Byte)s->bi_buf);
2545*7c478bd9Sstevel@tonic-gate         s->bi_buf >>= 8;
2546*7c478bd9Sstevel@tonic-gate         s->bi_valid -= 8;
2547*7c478bd9Sstevel@tonic-gate     }
2548*7c478bd9Sstevel@tonic-gate }
2549*7c478bd9Sstevel@tonic-gate 
2550*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2551*7c478bd9Sstevel@tonic-gate  * Flush the bit buffer and align the output on a byte boundary
2552*7c478bd9Sstevel@tonic-gate  */
2553*7c478bd9Sstevel@tonic-gate local void bi_windup(s)
2554*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2555*7c478bd9Sstevel@tonic-gate {
2556*7c478bd9Sstevel@tonic-gate     if (s->bi_valid > 8) {
2557*7c478bd9Sstevel@tonic-gate         put_short(s, s->bi_buf);
2558*7c478bd9Sstevel@tonic-gate     } else if (s->bi_valid > 0) {
2559*7c478bd9Sstevel@tonic-gate         put_byte(s, (Byte)s->bi_buf);
2560*7c478bd9Sstevel@tonic-gate     }
2561*7c478bd9Sstevel@tonic-gate     s->bi_buf = 0;
2562*7c478bd9Sstevel@tonic-gate     s->bi_valid = 0;
2563*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2564*7c478bd9Sstevel@tonic-gate     s->bits_sent = (s->bits_sent+7) & ~7;
2565*7c478bd9Sstevel@tonic-gate #endif
2566*7c478bd9Sstevel@tonic-gate }
2567*7c478bd9Sstevel@tonic-gate 
2568*7c478bd9Sstevel@tonic-gate /* ===========================================================================
2569*7c478bd9Sstevel@tonic-gate  * Copy a stored block, storing first the length and its
2570*7c478bd9Sstevel@tonic-gate  * one's complement if requested.
2571*7c478bd9Sstevel@tonic-gate  */
2572*7c478bd9Sstevel@tonic-gate local void copy_block(s, buf, len, header)
2573*7c478bd9Sstevel@tonic-gate     deflate_state *s;
2574*7c478bd9Sstevel@tonic-gate     charf    *buf;    /* the input data */
2575*7c478bd9Sstevel@tonic-gate     unsigned len;     /* its length */
2576*7c478bd9Sstevel@tonic-gate     int      header;  /* true if block header must be written */
2577*7c478bd9Sstevel@tonic-gate {
2578*7c478bd9Sstevel@tonic-gate     bi_windup(s);        /* align on byte boundary */
2579*7c478bd9Sstevel@tonic-gate     s->last_eob_len = 8; /* enough lookahead for inflate */
2580*7c478bd9Sstevel@tonic-gate 
2581*7c478bd9Sstevel@tonic-gate     if (header) {
2582*7c478bd9Sstevel@tonic-gate         put_short(s, (ush)len);
2583*7c478bd9Sstevel@tonic-gate         put_short(s, (ush)~len);
2584*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2585*7c478bd9Sstevel@tonic-gate         s->bits_sent += 2*16;
2586*7c478bd9Sstevel@tonic-gate #endif
2587*7c478bd9Sstevel@tonic-gate     }
2588*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2589*7c478bd9Sstevel@tonic-gate     s->bits_sent += (ulg)len<<3;
2590*7c478bd9Sstevel@tonic-gate #endif
2591*7c478bd9Sstevel@tonic-gate     while (len--) {
2592*7c478bd9Sstevel@tonic-gate         put_byte(s, *buf++);
2593*7c478bd9Sstevel@tonic-gate     }
2594*7c478bd9Sstevel@tonic-gate }
2595*7c478bd9Sstevel@tonic-gate 
2596*7c478bd9Sstevel@tonic-gate 
2597*7c478bd9Sstevel@tonic-gate /*+++++*/
2598*7c478bd9Sstevel@tonic-gate /* infblock.h -- header to use infblock.c
2599*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
2600*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2601*7c478bd9Sstevel@tonic-gate  */
2602*7c478bd9Sstevel@tonic-gate 
2603*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
2604*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
2605*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
2606*7c478bd9Sstevel@tonic-gate  */
2607*7c478bd9Sstevel@tonic-gate 
2608*7c478bd9Sstevel@tonic-gate struct inflate_blocks_state;
2609*7c478bd9Sstevel@tonic-gate typedef struct inflate_blocks_state FAR inflate_blocks_statef;
2610*7c478bd9Sstevel@tonic-gate 
2611*7c478bd9Sstevel@tonic-gate local inflate_blocks_statef * inflate_blocks_new OF((
2612*7c478bd9Sstevel@tonic-gate     z_stream *z,
2613*7c478bd9Sstevel@tonic-gate     check_func c,               /* check function */
2614*7c478bd9Sstevel@tonic-gate     uInt w));                   /* window size */
2615*7c478bd9Sstevel@tonic-gate 
2616*7c478bd9Sstevel@tonic-gate local int inflate_blocks OF((
2617*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
2618*7c478bd9Sstevel@tonic-gate     z_stream *,
2619*7c478bd9Sstevel@tonic-gate     int));                      /* initial return code */
2620*7c478bd9Sstevel@tonic-gate 
2621*7c478bd9Sstevel@tonic-gate local void inflate_blocks_reset OF((
2622*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
2623*7c478bd9Sstevel@tonic-gate     z_stream *,
2624*7c478bd9Sstevel@tonic-gate     uLongf *));                  /* check value on output */
2625*7c478bd9Sstevel@tonic-gate 
2626*7c478bd9Sstevel@tonic-gate local int inflate_blocks_free OF((
2627*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
2628*7c478bd9Sstevel@tonic-gate     z_stream *,
2629*7c478bd9Sstevel@tonic-gate     uLongf *));                  /* check value on output */
2630*7c478bd9Sstevel@tonic-gate 
2631*7c478bd9Sstevel@tonic-gate local int inflate_addhistory OF((
2632*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
2633*7c478bd9Sstevel@tonic-gate     z_stream *));
2634*7c478bd9Sstevel@tonic-gate 
2635*7c478bd9Sstevel@tonic-gate local int inflate_packet_flush OF((
2636*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *));
2637*7c478bd9Sstevel@tonic-gate 
2638*7c478bd9Sstevel@tonic-gate /*+++++*/
2639*7c478bd9Sstevel@tonic-gate /* inftrees.h -- header to use inftrees.c
2640*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
2641*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2642*7c478bd9Sstevel@tonic-gate  */
2643*7c478bd9Sstevel@tonic-gate 
2644*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
2645*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
2646*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
2647*7c478bd9Sstevel@tonic-gate  */
2648*7c478bd9Sstevel@tonic-gate 
2649*7c478bd9Sstevel@tonic-gate /* Huffman code lookup table entry--this entry is four bytes for machines
2650*7c478bd9Sstevel@tonic-gate    that have 16-bit pointers (e.g. PC's in the small or medium model). */
2651*7c478bd9Sstevel@tonic-gate 
2652*7c478bd9Sstevel@tonic-gate typedef struct inflate_huft_s FAR inflate_huft;
2653*7c478bd9Sstevel@tonic-gate 
2654*7c478bd9Sstevel@tonic-gate struct inflate_huft_s {
2655*7c478bd9Sstevel@tonic-gate   union {
2656*7c478bd9Sstevel@tonic-gate     struct {
2657*7c478bd9Sstevel@tonic-gate       Byte Exop;        /* number of extra bits or operation */
2658*7c478bd9Sstevel@tonic-gate       Byte Bits;        /* number of bits in this code or subcode */
2659*7c478bd9Sstevel@tonic-gate     } what;
2660*7c478bd9Sstevel@tonic-gate     uInt Nalloc;	/* number of these allocated here */
2661*7c478bd9Sstevel@tonic-gate     Bytef *pad;         /* pad structure to a power of 2 (4 bytes for */
2662*7c478bd9Sstevel@tonic-gate   } word;               /*  16-bit, 8 bytes for 32-bit machines) */
2663*7c478bd9Sstevel@tonic-gate   union {
2664*7c478bd9Sstevel@tonic-gate     uInt Base;          /* literal, length base, or distance base */
2665*7c478bd9Sstevel@tonic-gate     inflate_huft *Next; /* pointer to next level of table */
2666*7c478bd9Sstevel@tonic-gate   } more;
2667*7c478bd9Sstevel@tonic-gate };
2668*7c478bd9Sstevel@tonic-gate 
2669*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2670*7c478bd9Sstevel@tonic-gate   local uInt inflate_hufts;
2671*7c478bd9Sstevel@tonic-gate #endif
2672*7c478bd9Sstevel@tonic-gate 
2673*7c478bd9Sstevel@tonic-gate local int inflate_trees_bits OF((
2674*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* 19 code lengths */
2675*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* bits tree desired/actual depth */
2676*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,       /* bits tree result */
2677*7c478bd9Sstevel@tonic-gate     z_stream *));               /* for zalloc, zfree functions */
2678*7c478bd9Sstevel@tonic-gate 
2679*7c478bd9Sstevel@tonic-gate local int inflate_trees_dynamic OF((
2680*7c478bd9Sstevel@tonic-gate     uInt,                       /* number of literal/length codes */
2681*7c478bd9Sstevel@tonic-gate     uInt,                       /* number of distance codes */
2682*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* that many (total) code lengths */
2683*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* literal desired/actual bit depth */
2684*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* distance desired/actual bit depth */
2685*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,       /* literal/length tree result */
2686*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,       /* distance tree result */
2687*7c478bd9Sstevel@tonic-gate     z_stream *));               /* for zalloc, zfree functions */
2688*7c478bd9Sstevel@tonic-gate 
2689*7c478bd9Sstevel@tonic-gate local int inflate_trees_fixed OF((
2690*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* literal desired/actual bit depth */
2691*7c478bd9Sstevel@tonic-gate     uIntf *,                    /* distance desired/actual bit depth */
2692*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,       /* literal/length tree result */
2693*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR *));     /* distance tree result */
2694*7c478bd9Sstevel@tonic-gate 
2695*7c478bd9Sstevel@tonic-gate local int inflate_trees_free OF((
2696*7c478bd9Sstevel@tonic-gate     inflate_huft *,             /* tables to free */
2697*7c478bd9Sstevel@tonic-gate     z_stream *));               /* for zfree function */
2698*7c478bd9Sstevel@tonic-gate 
2699*7c478bd9Sstevel@tonic-gate 
2700*7c478bd9Sstevel@tonic-gate /*+++++*/
2701*7c478bd9Sstevel@tonic-gate /* infcodes.h -- header to use infcodes.c
2702*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
2703*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2704*7c478bd9Sstevel@tonic-gate  */
2705*7c478bd9Sstevel@tonic-gate 
2706*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
2707*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
2708*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
2709*7c478bd9Sstevel@tonic-gate  */
2710*7c478bd9Sstevel@tonic-gate 
2711*7c478bd9Sstevel@tonic-gate struct inflate_codes_state;
2712*7c478bd9Sstevel@tonic-gate typedef struct inflate_codes_state FAR inflate_codes_statef;
2713*7c478bd9Sstevel@tonic-gate 
2714*7c478bd9Sstevel@tonic-gate local inflate_codes_statef *inflate_codes_new OF((
2715*7c478bd9Sstevel@tonic-gate     uInt, uInt,
2716*7c478bd9Sstevel@tonic-gate     inflate_huft *, inflate_huft *,
2717*7c478bd9Sstevel@tonic-gate     z_stream *));
2718*7c478bd9Sstevel@tonic-gate 
2719*7c478bd9Sstevel@tonic-gate local int inflate_codes OF((
2720*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
2721*7c478bd9Sstevel@tonic-gate     z_stream *,
2722*7c478bd9Sstevel@tonic-gate     int));
2723*7c478bd9Sstevel@tonic-gate 
2724*7c478bd9Sstevel@tonic-gate local void inflate_codes_free OF((
2725*7c478bd9Sstevel@tonic-gate     inflate_codes_statef *,
2726*7c478bd9Sstevel@tonic-gate     z_stream *));
2727*7c478bd9Sstevel@tonic-gate 
2728*7c478bd9Sstevel@tonic-gate 
2729*7c478bd9Sstevel@tonic-gate /*+++++*/
2730*7c478bd9Sstevel@tonic-gate /* inflate.c -- zlib interface to inflate modules
2731*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
2732*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2733*7c478bd9Sstevel@tonic-gate  */
2734*7c478bd9Sstevel@tonic-gate 
2735*7c478bd9Sstevel@tonic-gate /* inflate private state */
2736*7c478bd9Sstevel@tonic-gate struct internal_state {
2737*7c478bd9Sstevel@tonic-gate 
2738*7c478bd9Sstevel@tonic-gate   /* mode */
2739*7c478bd9Sstevel@tonic-gate   enum {
2740*7c478bd9Sstevel@tonic-gate       METHOD,   /* waiting for method byte */
2741*7c478bd9Sstevel@tonic-gate       FLAG,     /* waiting for flag byte */
2742*7c478bd9Sstevel@tonic-gate       BLOCKS,   /* decompressing blocks */
2743*7c478bd9Sstevel@tonic-gate       CHECK4,   /* four check bytes to go */
2744*7c478bd9Sstevel@tonic-gate       CHECK3,   /* three check bytes to go */
2745*7c478bd9Sstevel@tonic-gate       CHECK2,   /* two check bytes to go */
2746*7c478bd9Sstevel@tonic-gate       CHECK1,   /* one check byte to go */
2747*7c478bd9Sstevel@tonic-gate       DONE,     /* finished check, done */
2748*7c478bd9Sstevel@tonic-gate       BAD}      /* got an error--stay here */
2749*7c478bd9Sstevel@tonic-gate     mode;               /* current inflate mode */
2750*7c478bd9Sstevel@tonic-gate 
2751*7c478bd9Sstevel@tonic-gate   /* mode dependent information */
2752*7c478bd9Sstevel@tonic-gate   union {
2753*7c478bd9Sstevel@tonic-gate     uInt method;        /* if FLAGS, method byte */
2754*7c478bd9Sstevel@tonic-gate     struct {
2755*7c478bd9Sstevel@tonic-gate       uLong was;                /* computed check value */
2756*7c478bd9Sstevel@tonic-gate       uLong need;               /* stream check value */
2757*7c478bd9Sstevel@tonic-gate     } check;            /* if CHECK, check values to compare */
2758*7c478bd9Sstevel@tonic-gate     uInt marker;        /* if BAD, inflateSync's marker bytes count */
2759*7c478bd9Sstevel@tonic-gate   } sub;        /* submode */
2760*7c478bd9Sstevel@tonic-gate 
2761*7c478bd9Sstevel@tonic-gate   /* mode independent information */
2762*7c478bd9Sstevel@tonic-gate   int  nowrap;          /* flag for no wrapper */
2763*7c478bd9Sstevel@tonic-gate   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
2764*7c478bd9Sstevel@tonic-gate   inflate_blocks_statef
2765*7c478bd9Sstevel@tonic-gate     *blocks;            /* current inflate_blocks state */
2766*7c478bd9Sstevel@tonic-gate 
2767*7c478bd9Sstevel@tonic-gate };
2768*7c478bd9Sstevel@tonic-gate 
2769*7c478bd9Sstevel@tonic-gate 
2770*7c478bd9Sstevel@tonic-gate int inflateReset(z)
2771*7c478bd9Sstevel@tonic-gate z_stream *z;
2772*7c478bd9Sstevel@tonic-gate {
2773*7c478bd9Sstevel@tonic-gate   uLong c;
2774*7c478bd9Sstevel@tonic-gate 
2775*7c478bd9Sstevel@tonic-gate   if (z == Z_NULL || z->state == Z_NULL)
2776*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2777*7c478bd9Sstevel@tonic-gate   z->total_in = z->total_out = 0;
2778*7c478bd9Sstevel@tonic-gate   z->msg = Z_NULL;
2779*7c478bd9Sstevel@tonic-gate   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
2780*7c478bd9Sstevel@tonic-gate   inflate_blocks_reset(z->state->blocks, z, &c);
2781*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate: reset\n"));
2782*7c478bd9Sstevel@tonic-gate   return Z_OK;
2783*7c478bd9Sstevel@tonic-gate }
2784*7c478bd9Sstevel@tonic-gate 
2785*7c478bd9Sstevel@tonic-gate 
2786*7c478bd9Sstevel@tonic-gate int inflateEnd(z)
2787*7c478bd9Sstevel@tonic-gate z_stream *z;
2788*7c478bd9Sstevel@tonic-gate {
2789*7c478bd9Sstevel@tonic-gate   uLong c;
2790*7c478bd9Sstevel@tonic-gate 
2791*7c478bd9Sstevel@tonic-gate   if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
2792*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2793*7c478bd9Sstevel@tonic-gate   if (z->state->blocks != Z_NULL)
2794*7c478bd9Sstevel@tonic-gate     inflate_blocks_free(z->state->blocks, z, &c);
2795*7c478bd9Sstevel@tonic-gate   ZFREE(z, z->state, sizeof(struct internal_state));
2796*7c478bd9Sstevel@tonic-gate   z->state = Z_NULL;
2797*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate: end\n"));
2798*7c478bd9Sstevel@tonic-gate   return Z_OK;
2799*7c478bd9Sstevel@tonic-gate }
2800*7c478bd9Sstevel@tonic-gate 
2801*7c478bd9Sstevel@tonic-gate 
2802*7c478bd9Sstevel@tonic-gate int inflateInit2(z, w)
2803*7c478bd9Sstevel@tonic-gate z_stream *z;
2804*7c478bd9Sstevel@tonic-gate int w;
2805*7c478bd9Sstevel@tonic-gate {
2806*7c478bd9Sstevel@tonic-gate   /* initialize state */
2807*7c478bd9Sstevel@tonic-gate   if (z == Z_NULL)
2808*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2809*7c478bd9Sstevel@tonic-gate /*  if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */
2810*7c478bd9Sstevel@tonic-gate /*  if (z->zfree == Z_NULL) z->zfree = zcfree; */
2811*7c478bd9Sstevel@tonic-gate   if ((z->state = (struct internal_state FAR *)
2812*7c478bd9Sstevel@tonic-gate        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
2813*7c478bd9Sstevel@tonic-gate     return Z_MEM_ERROR;
2814*7c478bd9Sstevel@tonic-gate   z->state->blocks = Z_NULL;
2815*7c478bd9Sstevel@tonic-gate 
2816*7c478bd9Sstevel@tonic-gate   /* handle undocumented nowrap option (no zlib header or check) */
2817*7c478bd9Sstevel@tonic-gate   z->state->nowrap = 0;
2818*7c478bd9Sstevel@tonic-gate   if (w < 0)
2819*7c478bd9Sstevel@tonic-gate   {
2820*7c478bd9Sstevel@tonic-gate     w = - w;
2821*7c478bd9Sstevel@tonic-gate     z->state->nowrap = 1;
2822*7c478bd9Sstevel@tonic-gate   }
2823*7c478bd9Sstevel@tonic-gate 
2824*7c478bd9Sstevel@tonic-gate   /* set window size */
2825*7c478bd9Sstevel@tonic-gate   if (w < 8 || w > 15)
2826*7c478bd9Sstevel@tonic-gate   {
2827*7c478bd9Sstevel@tonic-gate     inflateEnd(z);
2828*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2829*7c478bd9Sstevel@tonic-gate   }
2830*7c478bd9Sstevel@tonic-gate   z->state->wbits = (uInt)w;
2831*7c478bd9Sstevel@tonic-gate 
2832*7c478bd9Sstevel@tonic-gate   /* create inflate_blocks state */
2833*7c478bd9Sstevel@tonic-gate   if ((z->state->blocks =
2834*7c478bd9Sstevel@tonic-gate        inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
2835*7c478bd9Sstevel@tonic-gate       == Z_NULL)
2836*7c478bd9Sstevel@tonic-gate   {
2837*7c478bd9Sstevel@tonic-gate     inflateEnd(z);
2838*7c478bd9Sstevel@tonic-gate     return Z_MEM_ERROR;
2839*7c478bd9Sstevel@tonic-gate   }
2840*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate: allocated\n"));
2841*7c478bd9Sstevel@tonic-gate 
2842*7c478bd9Sstevel@tonic-gate   /* reset state */
2843*7c478bd9Sstevel@tonic-gate   inflateReset(z);
2844*7c478bd9Sstevel@tonic-gate   return Z_OK;
2845*7c478bd9Sstevel@tonic-gate }
2846*7c478bd9Sstevel@tonic-gate 
2847*7c478bd9Sstevel@tonic-gate 
2848*7c478bd9Sstevel@tonic-gate int inflateInit(z)
2849*7c478bd9Sstevel@tonic-gate z_stream *z;
2850*7c478bd9Sstevel@tonic-gate {
2851*7c478bd9Sstevel@tonic-gate   return inflateInit2(z, DEF_WBITS);
2852*7c478bd9Sstevel@tonic-gate }
2853*7c478bd9Sstevel@tonic-gate 
2854*7c478bd9Sstevel@tonic-gate 
2855*7c478bd9Sstevel@tonic-gate #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
2856*7c478bd9Sstevel@tonic-gate #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
2857*7c478bd9Sstevel@tonic-gate 
2858*7c478bd9Sstevel@tonic-gate int inflate(z, f)
2859*7c478bd9Sstevel@tonic-gate z_stream *z;
2860*7c478bd9Sstevel@tonic-gate int f;
2861*7c478bd9Sstevel@tonic-gate {
2862*7c478bd9Sstevel@tonic-gate   int r;
2863*7c478bd9Sstevel@tonic-gate   uInt b;
2864*7c478bd9Sstevel@tonic-gate 
2865*7c478bd9Sstevel@tonic-gate   if (z == Z_NULL || z->next_in == Z_NULL)
2866*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2867*7c478bd9Sstevel@tonic-gate   r = Z_BUF_ERROR;
2868*7c478bd9Sstevel@tonic-gate   while (1) switch (z->state->mode)
2869*7c478bd9Sstevel@tonic-gate   {
2870*7c478bd9Sstevel@tonic-gate     case METHOD:
2871*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2872*7c478bd9Sstevel@tonic-gate       if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
2873*7c478bd9Sstevel@tonic-gate       {
2874*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2875*7c478bd9Sstevel@tonic-gate         z->msg = "unknown compression method";
2876*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 5;       /* can't try inflateSync */
2877*7c478bd9Sstevel@tonic-gate         break;
2878*7c478bd9Sstevel@tonic-gate       }
2879*7c478bd9Sstevel@tonic-gate       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
2880*7c478bd9Sstevel@tonic-gate       {
2881*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2882*7c478bd9Sstevel@tonic-gate         z->msg = "invalid window size";
2883*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 5;       /* can't try inflateSync */
2884*7c478bd9Sstevel@tonic-gate         break;
2885*7c478bd9Sstevel@tonic-gate       }
2886*7c478bd9Sstevel@tonic-gate       z->state->mode = FLAG;
2887*7c478bd9Sstevel@tonic-gate     case FLAG:
2888*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2889*7c478bd9Sstevel@tonic-gate       if ((b = NEXTBYTE) & 0x20)
2890*7c478bd9Sstevel@tonic-gate       {
2891*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2892*7c478bd9Sstevel@tonic-gate         z->msg = "invalid reserved bit";
2893*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 5;       /* can't try inflateSync */
2894*7c478bd9Sstevel@tonic-gate         break;
2895*7c478bd9Sstevel@tonic-gate       }
2896*7c478bd9Sstevel@tonic-gate       if (((z->state->sub.method << 8) + b) % 31)
2897*7c478bd9Sstevel@tonic-gate       {
2898*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2899*7c478bd9Sstevel@tonic-gate         z->msg = "incorrect header check";
2900*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 5;       /* can't try inflateSync */
2901*7c478bd9Sstevel@tonic-gate         break;
2902*7c478bd9Sstevel@tonic-gate       }
2903*7c478bd9Sstevel@tonic-gate       Trace((stderr, "inflate: zlib header ok\n"));
2904*7c478bd9Sstevel@tonic-gate       z->state->mode = BLOCKS;
2905*7c478bd9Sstevel@tonic-gate     case BLOCKS:
2906*7c478bd9Sstevel@tonic-gate       r = inflate_blocks(z->state->blocks, z, r);
2907*7c478bd9Sstevel@tonic-gate       if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
2908*7c478bd9Sstevel@tonic-gate 	  r = inflate_packet_flush(z->state->blocks);
2909*7c478bd9Sstevel@tonic-gate       if (r == Z_DATA_ERROR)
2910*7c478bd9Sstevel@tonic-gate       {
2911*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2912*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 0;       /* can try inflateSync */
2913*7c478bd9Sstevel@tonic-gate         break;
2914*7c478bd9Sstevel@tonic-gate       }
2915*7c478bd9Sstevel@tonic-gate       if (r != Z_STREAM_END)
2916*7c478bd9Sstevel@tonic-gate         return r;
2917*7c478bd9Sstevel@tonic-gate       r = Z_OK;
2918*7c478bd9Sstevel@tonic-gate       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
2919*7c478bd9Sstevel@tonic-gate       if (z->state->nowrap)
2920*7c478bd9Sstevel@tonic-gate       {
2921*7c478bd9Sstevel@tonic-gate         z->state->mode = DONE;
2922*7c478bd9Sstevel@tonic-gate         break;
2923*7c478bd9Sstevel@tonic-gate       }
2924*7c478bd9Sstevel@tonic-gate       z->state->mode = CHECK4;
2925*7c478bd9Sstevel@tonic-gate     case CHECK4:
2926*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2927*7c478bd9Sstevel@tonic-gate       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
2928*7c478bd9Sstevel@tonic-gate       z->state->mode = CHECK3;
2929*7c478bd9Sstevel@tonic-gate     case CHECK3:
2930*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2931*7c478bd9Sstevel@tonic-gate       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
2932*7c478bd9Sstevel@tonic-gate       z->state->mode = CHECK2;
2933*7c478bd9Sstevel@tonic-gate     case CHECK2:
2934*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2935*7c478bd9Sstevel@tonic-gate       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
2936*7c478bd9Sstevel@tonic-gate       z->state->mode = CHECK1;
2937*7c478bd9Sstevel@tonic-gate     case CHECK1:
2938*7c478bd9Sstevel@tonic-gate       NEEDBYTE
2939*7c478bd9Sstevel@tonic-gate       z->state->sub.check.need += (uLong)NEXTBYTE;
2940*7c478bd9Sstevel@tonic-gate 
2941*7c478bd9Sstevel@tonic-gate       if (z->state->sub.check.was != z->state->sub.check.need)
2942*7c478bd9Sstevel@tonic-gate       {
2943*7c478bd9Sstevel@tonic-gate         z->state->mode = BAD;
2944*7c478bd9Sstevel@tonic-gate         z->msg = "incorrect data check";
2945*7c478bd9Sstevel@tonic-gate         z->state->sub.marker = 5;       /* can't try inflateSync */
2946*7c478bd9Sstevel@tonic-gate         break;
2947*7c478bd9Sstevel@tonic-gate       }
2948*7c478bd9Sstevel@tonic-gate       Trace((stderr, "inflate: zlib check ok\n"));
2949*7c478bd9Sstevel@tonic-gate       z->state->mode = DONE;
2950*7c478bd9Sstevel@tonic-gate     case DONE:
2951*7c478bd9Sstevel@tonic-gate       return Z_STREAM_END;
2952*7c478bd9Sstevel@tonic-gate     case BAD:
2953*7c478bd9Sstevel@tonic-gate       return Z_DATA_ERROR;
2954*7c478bd9Sstevel@tonic-gate     default:
2955*7c478bd9Sstevel@tonic-gate       return Z_STREAM_ERROR;
2956*7c478bd9Sstevel@tonic-gate   }
2957*7c478bd9Sstevel@tonic-gate 
2958*7c478bd9Sstevel@tonic-gate  empty:
2959*7c478bd9Sstevel@tonic-gate   if (f != Z_PACKET_FLUSH)
2960*7c478bd9Sstevel@tonic-gate     return r;
2961*7c478bd9Sstevel@tonic-gate   z->state->mode = BAD;
2962*7c478bd9Sstevel@tonic-gate   z->state->sub.marker = 0;       /* can try inflateSync */
2963*7c478bd9Sstevel@tonic-gate   return Z_DATA_ERROR;
2964*7c478bd9Sstevel@tonic-gate }
2965*7c478bd9Sstevel@tonic-gate 
2966*7c478bd9Sstevel@tonic-gate /*
2967*7c478bd9Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
2968*7c478bd9Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
2969*7c478bd9Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
2970*7c478bd9Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
2971*7c478bd9Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
2972*7c478bd9Sstevel@tonic-gate  * will have been updated if need be.
2973*7c478bd9Sstevel@tonic-gate  */
2974*7c478bd9Sstevel@tonic-gate 
2975*7c478bd9Sstevel@tonic-gate int inflateIncomp(z)
2976*7c478bd9Sstevel@tonic-gate z_stream *z;
2977*7c478bd9Sstevel@tonic-gate {
2978*7c478bd9Sstevel@tonic-gate     if (z->state->mode != BLOCKS)
2979*7c478bd9Sstevel@tonic-gate 	return Z_DATA_ERROR;
2980*7c478bd9Sstevel@tonic-gate     return inflate_addhistory(z->state->blocks, z);
2981*7c478bd9Sstevel@tonic-gate }
2982*7c478bd9Sstevel@tonic-gate 
2983*7c478bd9Sstevel@tonic-gate 
2984*7c478bd9Sstevel@tonic-gate int inflateSync(z)
2985*7c478bd9Sstevel@tonic-gate z_stream *z;
2986*7c478bd9Sstevel@tonic-gate {
2987*7c478bd9Sstevel@tonic-gate   uInt n;       /* number of bytes to look at */
2988*7c478bd9Sstevel@tonic-gate   Bytef *p;     /* pointer to bytes */
2989*7c478bd9Sstevel@tonic-gate   uInt m;       /* number of marker bytes found in a row */
2990*7c478bd9Sstevel@tonic-gate   uLong r, w;   /* temporaries to save total_in and total_out */
2991*7c478bd9Sstevel@tonic-gate 
2992*7c478bd9Sstevel@tonic-gate   /* set up */
2993*7c478bd9Sstevel@tonic-gate   if (z == Z_NULL || z->state == Z_NULL)
2994*7c478bd9Sstevel@tonic-gate     return Z_STREAM_ERROR;
2995*7c478bd9Sstevel@tonic-gate   if (z->state->mode != BAD)
2996*7c478bd9Sstevel@tonic-gate   {
2997*7c478bd9Sstevel@tonic-gate     z->state->mode = BAD;
2998*7c478bd9Sstevel@tonic-gate     z->state->sub.marker = 0;
2999*7c478bd9Sstevel@tonic-gate   }
3000*7c478bd9Sstevel@tonic-gate   if ((n = z->avail_in) == 0)
3001*7c478bd9Sstevel@tonic-gate     return Z_BUF_ERROR;
3002*7c478bd9Sstevel@tonic-gate   p = z->next_in;
3003*7c478bd9Sstevel@tonic-gate   m = z->state->sub.marker;
3004*7c478bd9Sstevel@tonic-gate 
3005*7c478bd9Sstevel@tonic-gate   /* search */
3006*7c478bd9Sstevel@tonic-gate   while (n && m < 4)
3007*7c478bd9Sstevel@tonic-gate   {
3008*7c478bd9Sstevel@tonic-gate     if (*p == (Byte)(m < 2 ? 0 : 0xff))
3009*7c478bd9Sstevel@tonic-gate       m++;
3010*7c478bd9Sstevel@tonic-gate     else if (*p)
3011*7c478bd9Sstevel@tonic-gate       m = 0;
3012*7c478bd9Sstevel@tonic-gate     else
3013*7c478bd9Sstevel@tonic-gate       m = 4 - m;
3014*7c478bd9Sstevel@tonic-gate     p++, n--;
3015*7c478bd9Sstevel@tonic-gate   }
3016*7c478bd9Sstevel@tonic-gate 
3017*7c478bd9Sstevel@tonic-gate   /* restore */
3018*7c478bd9Sstevel@tonic-gate   z->total_in += p - z->next_in;
3019*7c478bd9Sstevel@tonic-gate   z->next_in = p;
3020*7c478bd9Sstevel@tonic-gate   z->avail_in = n;
3021*7c478bd9Sstevel@tonic-gate   z->state->sub.marker = m;
3022*7c478bd9Sstevel@tonic-gate 
3023*7c478bd9Sstevel@tonic-gate   /* return no joy or set up to restart on a new block */
3024*7c478bd9Sstevel@tonic-gate   if (m != 4)
3025*7c478bd9Sstevel@tonic-gate     return Z_DATA_ERROR;
3026*7c478bd9Sstevel@tonic-gate   r = z->total_in;  w = z->total_out;
3027*7c478bd9Sstevel@tonic-gate   inflateReset(z);
3028*7c478bd9Sstevel@tonic-gate   z->total_in = r;  z->total_out = w;
3029*7c478bd9Sstevel@tonic-gate   z->state->mode = BLOCKS;
3030*7c478bd9Sstevel@tonic-gate   return Z_OK;
3031*7c478bd9Sstevel@tonic-gate }
3032*7c478bd9Sstevel@tonic-gate 
3033*7c478bd9Sstevel@tonic-gate #undef NEEDBYTE
3034*7c478bd9Sstevel@tonic-gate #undef NEXTBYTE
3035*7c478bd9Sstevel@tonic-gate 
3036*7c478bd9Sstevel@tonic-gate /*+++++*/
3037*7c478bd9Sstevel@tonic-gate /* infutil.h -- types and macros common to blocks and codes
3038*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
3039*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
3040*7c478bd9Sstevel@tonic-gate  */
3041*7c478bd9Sstevel@tonic-gate 
3042*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
3043*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
3044*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
3045*7c478bd9Sstevel@tonic-gate  */
3046*7c478bd9Sstevel@tonic-gate 
3047*7c478bd9Sstevel@tonic-gate /* inflate blocks semi-private state */
3048*7c478bd9Sstevel@tonic-gate struct inflate_blocks_state {
3049*7c478bd9Sstevel@tonic-gate 
3050*7c478bd9Sstevel@tonic-gate   /* mode */
3051*7c478bd9Sstevel@tonic-gate   enum {
3052*7c478bd9Sstevel@tonic-gate       TYPE,     /* get type bits (3, including end bit) */
3053*7c478bd9Sstevel@tonic-gate       LENS,     /* get lengths for stored */
3054*7c478bd9Sstevel@tonic-gate       STORED,   /* processing stored block */
3055*7c478bd9Sstevel@tonic-gate       TABLE,    /* get table lengths */
3056*7c478bd9Sstevel@tonic-gate       BTREE,    /* get bit lengths tree for a dynamic block */
3057*7c478bd9Sstevel@tonic-gate       DTREE,    /* get length, distance trees for a dynamic block */
3058*7c478bd9Sstevel@tonic-gate       CODES,    /* processing fixed or dynamic block */
3059*7c478bd9Sstevel@tonic-gate       DRY,      /* output remaining window bytes */
3060*7c478bd9Sstevel@tonic-gate       DONEB,     /* finished last block, done */
3061*7c478bd9Sstevel@tonic-gate       BADB}      /* got a data error--stuck here */
3062*7c478bd9Sstevel@tonic-gate     mode;               /* current inflate_block mode */
3063*7c478bd9Sstevel@tonic-gate 
3064*7c478bd9Sstevel@tonic-gate   /* mode dependent information */
3065*7c478bd9Sstevel@tonic-gate   union {
3066*7c478bd9Sstevel@tonic-gate     uInt left;          /* if STORED, bytes left to copy */
3067*7c478bd9Sstevel@tonic-gate     struct {
3068*7c478bd9Sstevel@tonic-gate       uInt table;               /* table lengths (14 bits) */
3069*7c478bd9Sstevel@tonic-gate       uInt index;               /* index into blens (or border) */
3070*7c478bd9Sstevel@tonic-gate       uIntf *blens;             /* bit lengths of codes */
3071*7c478bd9Sstevel@tonic-gate       uInt bb;                  /* bit length tree depth */
3072*7c478bd9Sstevel@tonic-gate       inflate_huft *tb;         /* bit length decoding tree */
3073*7c478bd9Sstevel@tonic-gate       int nblens;		/* # elements allocated at blens */
3074*7c478bd9Sstevel@tonic-gate     } trees;            /* if DTREE, decoding info for trees */
3075*7c478bd9Sstevel@tonic-gate     struct {
3076*7c478bd9Sstevel@tonic-gate       inflate_huft *tl, *td;    /* trees to free */
3077*7c478bd9Sstevel@tonic-gate       inflate_codes_statef
3078*7c478bd9Sstevel@tonic-gate          *codes;
3079*7c478bd9Sstevel@tonic-gate     } decode;           /* if CODES, current state */
3080*7c478bd9Sstevel@tonic-gate   } sub;                /* submode */
3081*7c478bd9Sstevel@tonic-gate   uInt last;            /* true if this block is the last block */
3082*7c478bd9Sstevel@tonic-gate 
3083*7c478bd9Sstevel@tonic-gate   /* mode independent information */
3084*7c478bd9Sstevel@tonic-gate   uInt bitk;            /* bits in bit buffer */
3085*7c478bd9Sstevel@tonic-gate   uLong bitb;           /* bit buffer */
3086*7c478bd9Sstevel@tonic-gate   Bytef *window;        /* sliding window */
3087*7c478bd9Sstevel@tonic-gate   Bytef *end;           /* one byte after sliding window */
3088*7c478bd9Sstevel@tonic-gate   Bytef *read;          /* window read pointer */
3089*7c478bd9Sstevel@tonic-gate   Bytef *write;         /* window write pointer */
3090*7c478bd9Sstevel@tonic-gate   check_func checkfn;   /* check function */
3091*7c478bd9Sstevel@tonic-gate   uLong check;          /* check on output */
3092*7c478bd9Sstevel@tonic-gate 
3093*7c478bd9Sstevel@tonic-gate };
3094*7c478bd9Sstevel@tonic-gate 
3095*7c478bd9Sstevel@tonic-gate 
3096*7c478bd9Sstevel@tonic-gate /* defines for inflate input/output */
3097*7c478bd9Sstevel@tonic-gate /*   update pointers and return */
3098*7c478bd9Sstevel@tonic-gate #define UPDBITS {s->bitb=b;s->bitk=k;}
3099*7c478bd9Sstevel@tonic-gate #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
3100*7c478bd9Sstevel@tonic-gate #define UPDOUT {s->write=q;}
3101*7c478bd9Sstevel@tonic-gate #define UPDATE {UPDBITS UPDIN UPDOUT}
3102*7c478bd9Sstevel@tonic-gate #define LEAVE {UPDATE return inflate_flush(s,z,r);}
3103*7c478bd9Sstevel@tonic-gate /*   get bytes and bits */
3104*7c478bd9Sstevel@tonic-gate #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
3105*7c478bd9Sstevel@tonic-gate #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
3106*7c478bd9Sstevel@tonic-gate #define NEXTBYTE (n--,*p++)
3107*7c478bd9Sstevel@tonic-gate #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
3108*7c478bd9Sstevel@tonic-gate #define DUMPBITS(j) {b>>=(j);k-=(j);}
3109*7c478bd9Sstevel@tonic-gate /*   output bytes */
3110*7c478bd9Sstevel@tonic-gate #define WAVAIL (q<s->read?s->read-q-1:s->end-q)
3111*7c478bd9Sstevel@tonic-gate #define LOADOUT {q=s->write;m=WAVAIL;}
3112*7c478bd9Sstevel@tonic-gate #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
3113*7c478bd9Sstevel@tonic-gate #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
3114*7c478bd9Sstevel@tonic-gate #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
3115*7c478bd9Sstevel@tonic-gate #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
3116*7c478bd9Sstevel@tonic-gate /*   load local pointers */
3117*7c478bd9Sstevel@tonic-gate #define LOAD {LOADIN LOADOUT}
3118*7c478bd9Sstevel@tonic-gate 
3119*7c478bd9Sstevel@tonic-gate /* And'ing with mask[n] masks the lower n bits */
3120*7c478bd9Sstevel@tonic-gate local uInt inflate_mask[] = {
3121*7c478bd9Sstevel@tonic-gate     0x0000,
3122*7c478bd9Sstevel@tonic-gate     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
3123*7c478bd9Sstevel@tonic-gate     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
3124*7c478bd9Sstevel@tonic-gate };
3125*7c478bd9Sstevel@tonic-gate 
3126*7c478bd9Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
3127*7c478bd9Sstevel@tonic-gate local int inflate_flush OF((
3128*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
3129*7c478bd9Sstevel@tonic-gate     z_stream *,
3130*7c478bd9Sstevel@tonic-gate     int));
3131*7c478bd9Sstevel@tonic-gate 
3132*7c478bd9Sstevel@tonic-gate /*+++++*/
3133*7c478bd9Sstevel@tonic-gate /* inffast.h -- header to use inffast.c
3134*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
3135*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
3136*7c478bd9Sstevel@tonic-gate  */
3137*7c478bd9Sstevel@tonic-gate 
3138*7c478bd9Sstevel@tonic-gate /* WARNING: this file should *not* be used by applications. It is
3139*7c478bd9Sstevel@tonic-gate    part of the implementation of the compression library and is
3140*7c478bd9Sstevel@tonic-gate    subject to change. Applications should only use zlib.h.
3141*7c478bd9Sstevel@tonic-gate  */
3142*7c478bd9Sstevel@tonic-gate 
3143*7c478bd9Sstevel@tonic-gate local int inflate_fast OF((
3144*7c478bd9Sstevel@tonic-gate     uInt,
3145*7c478bd9Sstevel@tonic-gate     uInt,
3146*7c478bd9Sstevel@tonic-gate     inflate_huft *,
3147*7c478bd9Sstevel@tonic-gate     inflate_huft *,
3148*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
3149*7c478bd9Sstevel@tonic-gate     z_stream *));
3150*7c478bd9Sstevel@tonic-gate 
3151*7c478bd9Sstevel@tonic-gate 
3152*7c478bd9Sstevel@tonic-gate /*+++++*/
3153*7c478bd9Sstevel@tonic-gate /* infblock.c -- interpret and process block types to last block
3154*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
3155*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
3156*7c478bd9Sstevel@tonic-gate  */
3157*7c478bd9Sstevel@tonic-gate 
3158*7c478bd9Sstevel@tonic-gate /* Table for deflate from PKZIP's appnote.txt. */
3159*7c478bd9Sstevel@tonic-gate local uInt border[] = { /* Order of the bit length code lengths */
3160*7c478bd9Sstevel@tonic-gate         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
3161*7c478bd9Sstevel@tonic-gate 
3162*7c478bd9Sstevel@tonic-gate /*
3163*7c478bd9Sstevel@tonic-gate    Notes beyond the 1.93a appnote.txt:
3164*7c478bd9Sstevel@tonic-gate 
3165*7c478bd9Sstevel@tonic-gate    1. Distance pointers never point before the beginning of the output
3166*7c478bd9Sstevel@tonic-gate       stream.
3167*7c478bd9Sstevel@tonic-gate    2. Distance pointers can point back across blocks, up to 32k away.
3168*7c478bd9Sstevel@tonic-gate    3. There is an implied maximum of 7 bits for the bit length table and
3169*7c478bd9Sstevel@tonic-gate       15 bits for the actual data.
3170*7c478bd9Sstevel@tonic-gate    4. If only one code exists, then it is encoded using one bit.  (Zero
3171*7c478bd9Sstevel@tonic-gate       would be more efficient, but perhaps a little confusing.)  If two
3172*7c478bd9Sstevel@tonic-gate       codes exist, they are coded using one bit each (0 and 1).
3173*7c478bd9Sstevel@tonic-gate    5. There is no way of sending zero distance codes--a dummy must be
3174*7c478bd9Sstevel@tonic-gate       sent if there are none.  (History: a pre 2.0 version of PKZIP would
3175*7c478bd9Sstevel@tonic-gate       store blocks with no distance codes, but this was discovered to be
3176*7c478bd9Sstevel@tonic-gate       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
3177*7c478bd9Sstevel@tonic-gate       zero distance codes, which is sent as one code of zero bits in
3178*7c478bd9Sstevel@tonic-gate       length.
3179*7c478bd9Sstevel@tonic-gate    6. There are up to 286 literal/length codes.  Code 256 represents the
3180*7c478bd9Sstevel@tonic-gate       end-of-block.  Note however that the static length tree defines
3181*7c478bd9Sstevel@tonic-gate       288 codes just to fill out the Huffman codes.  Codes 286 and 287
3182*7c478bd9Sstevel@tonic-gate       cannot be used though, since there is no length base or extra bits
3183*7c478bd9Sstevel@tonic-gate       defined for them.  Similarily, there are up to 30 distance codes.
3184*7c478bd9Sstevel@tonic-gate       However, static trees define 32 codes (all 5 bits) to fill out the
3185*7c478bd9Sstevel@tonic-gate       Huffman codes, but the last two had better not show up in the data.
3186*7c478bd9Sstevel@tonic-gate    7. Unzip can check dynamic Huffman blocks for complete code sets.
3187*7c478bd9Sstevel@tonic-gate       The exception is that a single code would not be complete (see #4).
3188*7c478bd9Sstevel@tonic-gate    8. The five bits following the block type is really the number of
3189*7c478bd9Sstevel@tonic-gate       literal codes sent minus 257.
3190*7c478bd9Sstevel@tonic-gate    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
3191*7c478bd9Sstevel@tonic-gate       (1+6+6).  Therefore, to output three times the length, you output
3192*7c478bd9Sstevel@tonic-gate       three codes (1+1+1), whereas to output four times the same length,
3193*7c478bd9Sstevel@tonic-gate       you only need two codes (1+3).  Hmm.
3194*7c478bd9Sstevel@tonic-gate   10. In the tree reconstruction algorithm, Code = Code + Increment
3195*7c478bd9Sstevel@tonic-gate       only if BitLength(i) is not zero.  (Pretty obvious.)
3196*7c478bd9Sstevel@tonic-gate   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
3197*7c478bd9Sstevel@tonic-gate   12. Note: length code 284 can represent 227-258, but length code 285
3198*7c478bd9Sstevel@tonic-gate       really is 258.  The last length deserves its own, short code
3199*7c478bd9Sstevel@tonic-gate       since it gets used a lot in very redundant files.  The length
3200*7c478bd9Sstevel@tonic-gate       258 is special since 258 - 3 (the min match length) is 255.
3201*7c478bd9Sstevel@tonic-gate   13. The literal/length and distance code bit lengths are read as a
3202*7c478bd9Sstevel@tonic-gate       single stream of lengths.  It is possible (and advantageous) for
3203*7c478bd9Sstevel@tonic-gate       a repeat code (16, 17, or 18) to go across the boundary between
3204*7c478bd9Sstevel@tonic-gate       the two sets of lengths.
3205*7c478bd9Sstevel@tonic-gate  */
3206*7c478bd9Sstevel@tonic-gate 
3207*7c478bd9Sstevel@tonic-gate 
3208*7c478bd9Sstevel@tonic-gate local void inflate_blocks_reset(s, z, c)
3209*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
3210*7c478bd9Sstevel@tonic-gate z_stream *z;
3211*7c478bd9Sstevel@tonic-gate uLongf *c;
3212*7c478bd9Sstevel@tonic-gate {
3213*7c478bd9Sstevel@tonic-gate   if (s->checkfn != Z_NULL)
3214*7c478bd9Sstevel@tonic-gate     *c = s->check;
3215*7c478bd9Sstevel@tonic-gate   if (s->mode == BTREE || s->mode == DTREE)
3216*7c478bd9Sstevel@tonic-gate     ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
3217*7c478bd9Sstevel@tonic-gate   if (s->mode == CODES)
3218*7c478bd9Sstevel@tonic-gate   {
3219*7c478bd9Sstevel@tonic-gate     inflate_codes_free(s->sub.decode.codes, z);
3220*7c478bd9Sstevel@tonic-gate     inflate_trees_free(s->sub.decode.td, z);
3221*7c478bd9Sstevel@tonic-gate     inflate_trees_free(s->sub.decode.tl, z);
3222*7c478bd9Sstevel@tonic-gate   }
3223*7c478bd9Sstevel@tonic-gate   s->mode = TYPE;
3224*7c478bd9Sstevel@tonic-gate   s->bitk = 0;
3225*7c478bd9Sstevel@tonic-gate   s->bitb = 0;
3226*7c478bd9Sstevel@tonic-gate   s->read = s->write = s->window;
3227*7c478bd9Sstevel@tonic-gate   if (s->checkfn != Z_NULL)
3228*7c478bd9Sstevel@tonic-gate     s->check = (*s->checkfn)(0L, Z_NULL, 0);
3229*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate:   blocks reset\n"));
3230*7c478bd9Sstevel@tonic-gate }
3231*7c478bd9Sstevel@tonic-gate 
3232*7c478bd9Sstevel@tonic-gate 
3233*7c478bd9Sstevel@tonic-gate local inflate_blocks_statef *inflate_blocks_new(z, c, w)
3234*7c478bd9Sstevel@tonic-gate z_stream *z;
3235*7c478bd9Sstevel@tonic-gate check_func c;
3236*7c478bd9Sstevel@tonic-gate uInt w;
3237*7c478bd9Sstevel@tonic-gate {
3238*7c478bd9Sstevel@tonic-gate   inflate_blocks_statef *s;
3239*7c478bd9Sstevel@tonic-gate 
3240*7c478bd9Sstevel@tonic-gate   if ((s = (inflate_blocks_statef *)ZALLOC
3241*7c478bd9Sstevel@tonic-gate        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
3242*7c478bd9Sstevel@tonic-gate     return s;
3243*7c478bd9Sstevel@tonic-gate   if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
3244*7c478bd9Sstevel@tonic-gate   {
3245*7c478bd9Sstevel@tonic-gate     ZFREE(z, s, sizeof(struct inflate_blocks_state));
3246*7c478bd9Sstevel@tonic-gate     return Z_NULL;
3247*7c478bd9Sstevel@tonic-gate   }
3248*7c478bd9Sstevel@tonic-gate   s->end = s->window + w;
3249*7c478bd9Sstevel@tonic-gate   s->checkfn = c;
3250*7c478bd9Sstevel@tonic-gate   s->mode = TYPE;
3251*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate:   blocks allocated\n"));
3252*7c478bd9Sstevel@tonic-gate   inflate_blocks_reset(s, z, &s->check);
3253*7c478bd9Sstevel@tonic-gate   return s;
3254*7c478bd9Sstevel@tonic-gate }
3255*7c478bd9Sstevel@tonic-gate 
3256*7c478bd9Sstevel@tonic-gate 
3257*7c478bd9Sstevel@tonic-gate local int inflate_blocks(s, z, r)
3258*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
3259*7c478bd9Sstevel@tonic-gate z_stream *z;
3260*7c478bd9Sstevel@tonic-gate int r;
3261*7c478bd9Sstevel@tonic-gate {
3262*7c478bd9Sstevel@tonic-gate   uInt t;               /* temporary storage */
3263*7c478bd9Sstevel@tonic-gate   uLong b;              /* bit buffer */
3264*7c478bd9Sstevel@tonic-gate   uInt k;               /* bits in bit buffer */
3265*7c478bd9Sstevel@tonic-gate   Bytef *p;             /* input data pointer */
3266*7c478bd9Sstevel@tonic-gate   uInt n;               /* bytes available there */
3267*7c478bd9Sstevel@tonic-gate   Bytef *q;             /* output window write pointer */
3268*7c478bd9Sstevel@tonic-gate   uInt m;               /* bytes to end of window or read pointer */
3269*7c478bd9Sstevel@tonic-gate 
3270*7c478bd9Sstevel@tonic-gate   /* copy input/output information to locals (UPDATE macro restores) */
3271*7c478bd9Sstevel@tonic-gate   LOAD
3272*7c478bd9Sstevel@tonic-gate 
3273*7c478bd9Sstevel@tonic-gate   /* process input based on current state */
3274*7c478bd9Sstevel@tonic-gate   while (1) switch (s->mode)
3275*7c478bd9Sstevel@tonic-gate   {
3276*7c478bd9Sstevel@tonic-gate     case TYPE:
3277*7c478bd9Sstevel@tonic-gate       NEEDBITS(3)
3278*7c478bd9Sstevel@tonic-gate       t = (uInt)b & 7;
3279*7c478bd9Sstevel@tonic-gate       s->last = t & 1;
3280*7c478bd9Sstevel@tonic-gate       switch (t >> 1)
3281*7c478bd9Sstevel@tonic-gate       {
3282*7c478bd9Sstevel@tonic-gate         case 0:                         /* stored */
3283*7c478bd9Sstevel@tonic-gate           Trace((stderr, "inflate:     stored block%s\n",
3284*7c478bd9Sstevel@tonic-gate                  s->last ? " (last)" : ""));
3285*7c478bd9Sstevel@tonic-gate           DUMPBITS(3)
3286*7c478bd9Sstevel@tonic-gate           t = k & 7;                    /* go to byte boundary */
3287*7c478bd9Sstevel@tonic-gate           DUMPBITS(t)
3288*7c478bd9Sstevel@tonic-gate           s->mode = LENS;               /* get length of stored block */
3289*7c478bd9Sstevel@tonic-gate           break;
3290*7c478bd9Sstevel@tonic-gate         case 1:                         /* fixed */
3291*7c478bd9Sstevel@tonic-gate           Trace((stderr, "inflate:     fixed codes block%s\n",
3292*7c478bd9Sstevel@tonic-gate                  s->last ? " (last)" : ""));
3293*7c478bd9Sstevel@tonic-gate           {
3294*7c478bd9Sstevel@tonic-gate             uInt bl, bd;
3295*7c478bd9Sstevel@tonic-gate             inflate_huft *tl, *td;
3296*7c478bd9Sstevel@tonic-gate 
3297*7c478bd9Sstevel@tonic-gate             inflate_trees_fixed(&bl, &bd, &tl, &td);
3298*7c478bd9Sstevel@tonic-gate             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
3299*7c478bd9Sstevel@tonic-gate             if (s->sub.decode.codes == Z_NULL)
3300*7c478bd9Sstevel@tonic-gate             {
3301*7c478bd9Sstevel@tonic-gate               r = Z_MEM_ERROR;
3302*7c478bd9Sstevel@tonic-gate               LEAVE
3303*7c478bd9Sstevel@tonic-gate             }
3304*7c478bd9Sstevel@tonic-gate             s->sub.decode.tl = Z_NULL;  /* don't try to free these */
3305*7c478bd9Sstevel@tonic-gate             s->sub.decode.td = Z_NULL;
3306*7c478bd9Sstevel@tonic-gate           }
3307*7c478bd9Sstevel@tonic-gate           DUMPBITS(3)
3308*7c478bd9Sstevel@tonic-gate           s->mode = CODES;
3309*7c478bd9Sstevel@tonic-gate           break;
3310*7c478bd9Sstevel@tonic-gate         case 2:                         /* dynamic */
3311*7c478bd9Sstevel@tonic-gate           Trace((stderr, "inflate:     dynamic codes block%s\n",
3312*7c478bd9Sstevel@tonic-gate                  s->last ? " (last)" : ""));
3313*7c478bd9Sstevel@tonic-gate           DUMPBITS(3)
3314*7c478bd9Sstevel@tonic-gate           s->mode = TABLE;
3315*7c478bd9Sstevel@tonic-gate           break;
3316*7c478bd9Sstevel@tonic-gate         case 3:                         /* illegal */
3317*7c478bd9Sstevel@tonic-gate           DUMPBITS(3)
3318*7c478bd9Sstevel@tonic-gate           s->mode = BADB;
3319*7c478bd9Sstevel@tonic-gate           z->msg = "invalid block type";
3320*7c478bd9Sstevel@tonic-gate           r = Z_DATA_ERROR;
3321*7c478bd9Sstevel@tonic-gate           LEAVE
3322*7c478bd9Sstevel@tonic-gate       }
3323*7c478bd9Sstevel@tonic-gate       break;
3324*7c478bd9Sstevel@tonic-gate     case LENS:
3325*7c478bd9Sstevel@tonic-gate       NEEDBITS(32)
3326*7c478bd9Sstevel@tonic-gate       if (((~b) >> 16) != (b & 0xffff))
3327*7c478bd9Sstevel@tonic-gate       {
3328*7c478bd9Sstevel@tonic-gate         s->mode = BADB;
3329*7c478bd9Sstevel@tonic-gate         z->msg = "invalid stored block lengths";
3330*7c478bd9Sstevel@tonic-gate         r = Z_DATA_ERROR;
3331*7c478bd9Sstevel@tonic-gate         LEAVE
3332*7c478bd9Sstevel@tonic-gate       }
3333*7c478bd9Sstevel@tonic-gate       s->sub.left = (uInt)b & 0xffff;
3334*7c478bd9Sstevel@tonic-gate       b = k = 0;                      /* dump bits */
3335*7c478bd9Sstevel@tonic-gate       Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
3336*7c478bd9Sstevel@tonic-gate       s->mode = s->sub.left ? STORED : TYPE;
3337*7c478bd9Sstevel@tonic-gate       break;
3338*7c478bd9Sstevel@tonic-gate     case STORED:
3339*7c478bd9Sstevel@tonic-gate       if (n == 0)
3340*7c478bd9Sstevel@tonic-gate         LEAVE
3341*7c478bd9Sstevel@tonic-gate       NEEDOUT
3342*7c478bd9Sstevel@tonic-gate       t = s->sub.left;
3343*7c478bd9Sstevel@tonic-gate       if (t > n) t = n;
3344*7c478bd9Sstevel@tonic-gate       if (t > m) t = m;
3345*7c478bd9Sstevel@tonic-gate       zmemcpy(q, p, t);
3346*7c478bd9Sstevel@tonic-gate       p += t;  n -= t;
3347*7c478bd9Sstevel@tonic-gate       q += t;  m -= t;
3348*7c478bd9Sstevel@tonic-gate       if ((s->sub.left -= t) != 0)
3349*7c478bd9Sstevel@tonic-gate         break;
3350*7c478bd9Sstevel@tonic-gate       Tracev((stderr, "inflate:       stored end, %lu total out\n",
3351*7c478bd9Sstevel@tonic-gate               z->total_out + (q >= s->read ? q - s->read :
3352*7c478bd9Sstevel@tonic-gate               (s->end - s->read) + (q - s->window))));
3353*7c478bd9Sstevel@tonic-gate       s->mode = s->last ? DRY : TYPE;
3354*7c478bd9Sstevel@tonic-gate       break;
3355*7c478bd9Sstevel@tonic-gate     case TABLE:
3356*7c478bd9Sstevel@tonic-gate       NEEDBITS(14)
3357*7c478bd9Sstevel@tonic-gate       s->sub.trees.table = t = (uInt)b & 0x3fff;
3358*7c478bd9Sstevel@tonic-gate #ifndef PKZIP_BUG_WORKAROUND
3359*7c478bd9Sstevel@tonic-gate       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
3360*7c478bd9Sstevel@tonic-gate       {
3361*7c478bd9Sstevel@tonic-gate         s->mode = BADB;
3362*7c478bd9Sstevel@tonic-gate         z->msg = "too many length or distance symbols";
3363*7c478bd9Sstevel@tonic-gate         r = Z_DATA_ERROR;
3364*7c478bd9Sstevel@tonic-gate         LEAVE
3365*7c478bd9Sstevel@tonic-gate       }
3366*7c478bd9Sstevel@tonic-gate #endif
3367*7c478bd9Sstevel@tonic-gate       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
3368*7c478bd9Sstevel@tonic-gate       if (t < 19)
3369*7c478bd9Sstevel@tonic-gate         t = 19;
3370*7c478bd9Sstevel@tonic-gate       if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
3371*7c478bd9Sstevel@tonic-gate       {
3372*7c478bd9Sstevel@tonic-gate         r = Z_MEM_ERROR;
3373*7c478bd9Sstevel@tonic-gate         LEAVE
3374*7c478bd9Sstevel@tonic-gate       }
3375*7c478bd9Sstevel@tonic-gate       s->sub.trees.nblens = t;
3376*7c478bd9Sstevel@tonic-gate       DUMPBITS(14)
3377*7c478bd9Sstevel@tonic-gate       s->sub.trees.index = 0;
3378*7c478bd9Sstevel@tonic-gate       Tracev((stderr, "inflate:       table sizes ok\n"));
3379*7c478bd9Sstevel@tonic-gate       s->mode = BTREE;
3380*7c478bd9Sstevel@tonic-gate     case BTREE:
3381*7c478bd9Sstevel@tonic-gate       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
3382*7c478bd9Sstevel@tonic-gate       {
3383*7c478bd9Sstevel@tonic-gate         NEEDBITS(3)
3384*7c478bd9Sstevel@tonic-gate         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
3385*7c478bd9Sstevel@tonic-gate         DUMPBITS(3)
3386*7c478bd9Sstevel@tonic-gate       }
3387*7c478bd9Sstevel@tonic-gate       while (s->sub.trees.index < 19)
3388*7c478bd9Sstevel@tonic-gate         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
3389*7c478bd9Sstevel@tonic-gate       s->sub.trees.bb = 7;
3390*7c478bd9Sstevel@tonic-gate       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
3391*7c478bd9Sstevel@tonic-gate                              &s->sub.trees.tb, z);
3392*7c478bd9Sstevel@tonic-gate       if (t != Z_OK)
3393*7c478bd9Sstevel@tonic-gate       {
3394*7c478bd9Sstevel@tonic-gate         r = t;
3395*7c478bd9Sstevel@tonic-gate         if (r == Z_DATA_ERROR)
3396*7c478bd9Sstevel@tonic-gate           s->mode = BADB;
3397*7c478bd9Sstevel@tonic-gate         LEAVE
3398*7c478bd9Sstevel@tonic-gate       }
3399*7c478bd9Sstevel@tonic-gate       s->sub.trees.index = 0;
3400*7c478bd9Sstevel@tonic-gate       Tracev((stderr, "inflate:       bits tree ok\n"));
3401*7c478bd9Sstevel@tonic-gate       s->mode = DTREE;
3402*7c478bd9Sstevel@tonic-gate     case DTREE:
3403*7c478bd9Sstevel@tonic-gate       while (t = s->sub.trees.table,
3404*7c478bd9Sstevel@tonic-gate              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
3405*7c478bd9Sstevel@tonic-gate       {
3406*7c478bd9Sstevel@tonic-gate         inflate_huft *h;
3407*7c478bd9Sstevel@tonic-gate         uInt i, j, c;
3408*7c478bd9Sstevel@tonic-gate 
3409*7c478bd9Sstevel@tonic-gate         t = s->sub.trees.bb;
3410*7c478bd9Sstevel@tonic-gate         NEEDBITS(t)
3411*7c478bd9Sstevel@tonic-gate         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
3412*7c478bd9Sstevel@tonic-gate         t = h->word.what.Bits;
3413*7c478bd9Sstevel@tonic-gate         c = h->more.Base;
3414*7c478bd9Sstevel@tonic-gate         if (c < 16)
3415*7c478bd9Sstevel@tonic-gate         {
3416*7c478bd9Sstevel@tonic-gate           DUMPBITS(t)
3417*7c478bd9Sstevel@tonic-gate           s->sub.trees.blens[s->sub.trees.index++] = c;
3418*7c478bd9Sstevel@tonic-gate         }
3419*7c478bd9Sstevel@tonic-gate         else /* c == 16..18 */
3420*7c478bd9Sstevel@tonic-gate         {
3421*7c478bd9Sstevel@tonic-gate           i = c == 18 ? 7 : c - 14;
3422*7c478bd9Sstevel@tonic-gate           j = c == 18 ? 11 : 3;
3423*7c478bd9Sstevel@tonic-gate           NEEDBITS(t + i)
3424*7c478bd9Sstevel@tonic-gate           DUMPBITS(t)
3425*7c478bd9Sstevel@tonic-gate           j += (uInt)b & inflate_mask[i];
3426*7c478bd9Sstevel@tonic-gate           DUMPBITS(i)
3427*7c478bd9Sstevel@tonic-gate           i = s->sub.trees.index;
3428*7c478bd9Sstevel@tonic-gate           t = s->sub.trees.table;
3429*7c478bd9Sstevel@tonic-gate           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
3430*7c478bd9Sstevel@tonic-gate               (c == 16 && i < 1))
3431*7c478bd9Sstevel@tonic-gate           {
3432*7c478bd9Sstevel@tonic-gate             s->mode = BADB;
3433*7c478bd9Sstevel@tonic-gate             z->msg = "invalid bit length repeat";
3434*7c478bd9Sstevel@tonic-gate             r = Z_DATA_ERROR;
3435*7c478bd9Sstevel@tonic-gate             LEAVE
3436*7c478bd9Sstevel@tonic-gate           }
3437*7c478bd9Sstevel@tonic-gate           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
3438*7c478bd9Sstevel@tonic-gate           do {
3439*7c478bd9Sstevel@tonic-gate             s->sub.trees.blens[i++] = c;
3440*7c478bd9Sstevel@tonic-gate           } while (--j);
3441*7c478bd9Sstevel@tonic-gate           s->sub.trees.index = i;
3442*7c478bd9Sstevel@tonic-gate         }
3443*7c478bd9Sstevel@tonic-gate       }
3444*7c478bd9Sstevel@tonic-gate       inflate_trees_free(s->sub.trees.tb, z);
3445*7c478bd9Sstevel@tonic-gate       s->sub.trees.tb = Z_NULL;
3446*7c478bd9Sstevel@tonic-gate       {
3447*7c478bd9Sstevel@tonic-gate         uInt bl, bd;
3448*7c478bd9Sstevel@tonic-gate         inflate_huft *tl, *td;
3449*7c478bd9Sstevel@tonic-gate         inflate_codes_statef *c;
3450*7c478bd9Sstevel@tonic-gate 
3451*7c478bd9Sstevel@tonic-gate         bl = 9;         /* must be <= 9 for lookahead assumptions */
3452*7c478bd9Sstevel@tonic-gate         bd = 6;         /* must be <= 9 for lookahead assumptions */
3453*7c478bd9Sstevel@tonic-gate         t = s->sub.trees.table;
3454*7c478bd9Sstevel@tonic-gate         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
3455*7c478bd9Sstevel@tonic-gate                                   s->sub.trees.blens, &bl, &bd, &tl, &td, z);
3456*7c478bd9Sstevel@tonic-gate         if (t != Z_OK)
3457*7c478bd9Sstevel@tonic-gate         {
3458*7c478bd9Sstevel@tonic-gate           if (t == (uInt)Z_DATA_ERROR)
3459*7c478bd9Sstevel@tonic-gate             s->mode = BADB;
3460*7c478bd9Sstevel@tonic-gate           r = t;
3461*7c478bd9Sstevel@tonic-gate           LEAVE
3462*7c478bd9Sstevel@tonic-gate         }
3463*7c478bd9Sstevel@tonic-gate         Tracev((stderr, "inflate:       trees ok\n"));
3464*7c478bd9Sstevel@tonic-gate         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
3465*7c478bd9Sstevel@tonic-gate         {
3466*7c478bd9Sstevel@tonic-gate           inflate_trees_free(td, z);
3467*7c478bd9Sstevel@tonic-gate           inflate_trees_free(tl, z);
3468*7c478bd9Sstevel@tonic-gate           r = Z_MEM_ERROR;
3469*7c478bd9Sstevel@tonic-gate           LEAVE
3470*7c478bd9Sstevel@tonic-gate         }
3471*7c478bd9Sstevel@tonic-gate         ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
3472*7c478bd9Sstevel@tonic-gate         s->sub.decode.codes = c;
3473*7c478bd9Sstevel@tonic-gate         s->sub.decode.tl = tl;
3474*7c478bd9Sstevel@tonic-gate         s->sub.decode.td = td;
3475*7c478bd9Sstevel@tonic-gate       }
3476*7c478bd9Sstevel@tonic-gate       s->mode = CODES;
3477*7c478bd9Sstevel@tonic-gate     case CODES:
3478*7c478bd9Sstevel@tonic-gate       UPDATE
3479*7c478bd9Sstevel@tonic-gate       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
3480*7c478bd9Sstevel@tonic-gate         return inflate_flush(s, z, r);
3481*7c478bd9Sstevel@tonic-gate       r = Z_OK;
3482*7c478bd9Sstevel@tonic-gate       inflate_codes_free(s->sub.decode.codes, z);
3483*7c478bd9Sstevel@tonic-gate       inflate_trees_free(s->sub.decode.td, z);
3484*7c478bd9Sstevel@tonic-gate       inflate_trees_free(s->sub.decode.tl, z);
3485*7c478bd9Sstevel@tonic-gate       LOAD
3486*7c478bd9Sstevel@tonic-gate       Tracev((stderr, "inflate:       codes end, %lu total out\n",
3487*7c478bd9Sstevel@tonic-gate               z->total_out + (q >= s->read ? q - s->read :
3488*7c478bd9Sstevel@tonic-gate               (s->end - s->read) + (q - s->window))));
3489*7c478bd9Sstevel@tonic-gate       if (!s->last)
3490*7c478bd9Sstevel@tonic-gate       {
3491*7c478bd9Sstevel@tonic-gate         s->mode = TYPE;
3492*7c478bd9Sstevel@tonic-gate         break;
3493*7c478bd9Sstevel@tonic-gate       }
3494*7c478bd9Sstevel@tonic-gate       if (k > 7)              /* return unused byte, if any */
3495*7c478bd9Sstevel@tonic-gate       {
3496*7c478bd9Sstevel@tonic-gate         Assert(k < 16, "inflate_codes grabbed too many bytes")
3497*7c478bd9Sstevel@tonic-gate         k -= 8;
3498*7c478bd9Sstevel@tonic-gate         n++;
3499*7c478bd9Sstevel@tonic-gate         p--;                    /* can always return one */
3500*7c478bd9Sstevel@tonic-gate       }
3501*7c478bd9Sstevel@tonic-gate       s->mode = DRY;
3502*7c478bd9Sstevel@tonic-gate     case DRY:
3503*7c478bd9Sstevel@tonic-gate       FLUSH
3504*7c478bd9Sstevel@tonic-gate       if (s->read != s->write)
3505*7c478bd9Sstevel@tonic-gate         LEAVE
3506*7c478bd9Sstevel@tonic-gate       s->mode = DONEB;
3507*7c478bd9Sstevel@tonic-gate     case DONEB:
3508*7c478bd9Sstevel@tonic-gate       r = Z_STREAM_END;
3509*7c478bd9Sstevel@tonic-gate       LEAVE
3510*7c478bd9Sstevel@tonic-gate     case BADB:
3511*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
3512*7c478bd9Sstevel@tonic-gate       LEAVE
3513*7c478bd9Sstevel@tonic-gate     default:
3514*7c478bd9Sstevel@tonic-gate       r = Z_STREAM_ERROR;
3515*7c478bd9Sstevel@tonic-gate       LEAVE
3516*7c478bd9Sstevel@tonic-gate   }
3517*7c478bd9Sstevel@tonic-gate }
3518*7c478bd9Sstevel@tonic-gate 
3519*7c478bd9Sstevel@tonic-gate 
3520*7c478bd9Sstevel@tonic-gate local int inflate_blocks_free(s, z, c)
3521*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
3522*7c478bd9Sstevel@tonic-gate z_stream *z;
3523*7c478bd9Sstevel@tonic-gate uLongf *c;
3524*7c478bd9Sstevel@tonic-gate {
3525*7c478bd9Sstevel@tonic-gate   inflate_blocks_reset(s, z, c);
3526*7c478bd9Sstevel@tonic-gate   ZFREE(z, s->window, s->end - s->window);
3527*7c478bd9Sstevel@tonic-gate   ZFREE(z, s, sizeof(struct inflate_blocks_state));
3528*7c478bd9Sstevel@tonic-gate   Trace((stderr, "inflate:   blocks freed\n"));
3529*7c478bd9Sstevel@tonic-gate   return Z_OK;
3530*7c478bd9Sstevel@tonic-gate }
3531*7c478bd9Sstevel@tonic-gate 
3532*7c478bd9Sstevel@tonic-gate /*
3533*7c478bd9Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
3534*7c478bd9Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
3535*7c478bd9Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
3536*7c478bd9Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
3537*7c478bd9Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
3538*7c478bd9Sstevel@tonic-gate  * will have been updated if need be.
3539*7c478bd9Sstevel@tonic-gate  */
3540*7c478bd9Sstevel@tonic-gate local int inflate_addhistory(s, z)
3541*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
3542*7c478bd9Sstevel@tonic-gate z_stream *z;
3543*7c478bd9Sstevel@tonic-gate {
3544*7c478bd9Sstevel@tonic-gate     uLong b;              /* bit buffer */  /* NOT USED HERE */
3545*7c478bd9Sstevel@tonic-gate     uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
3546*7c478bd9Sstevel@tonic-gate     uInt t;               /* temporary storage */
3547*7c478bd9Sstevel@tonic-gate     Bytef *p;             /* input data pointer */
3548*7c478bd9Sstevel@tonic-gate     uInt n;               /* bytes available there */
3549*7c478bd9Sstevel@tonic-gate     Bytef *q;             /* output window write pointer */
3550*7c478bd9Sstevel@tonic-gate     uInt m;               /* bytes to end of window or read pointer */
3551*7c478bd9Sstevel@tonic-gate 
3552*7c478bd9Sstevel@tonic-gate     if (s->read != s->write)
3553*7c478bd9Sstevel@tonic-gate 	return Z_STREAM_ERROR;
3554*7c478bd9Sstevel@tonic-gate     if (s->mode != TYPE)
3555*7c478bd9Sstevel@tonic-gate 	return Z_DATA_ERROR;
3556*7c478bd9Sstevel@tonic-gate 
3557*7c478bd9Sstevel@tonic-gate     /* we're ready to rock */
3558*7c478bd9Sstevel@tonic-gate     LOAD
3559*7c478bd9Sstevel@tonic-gate     /* while there is input ready, copy to output buffer, moving
3560*7c478bd9Sstevel@tonic-gate      * pointers as needed.
3561*7c478bd9Sstevel@tonic-gate      */
3562*7c478bd9Sstevel@tonic-gate     while (n) {
3563*7c478bd9Sstevel@tonic-gate 	t = n;  /* how many to do */
3564*7c478bd9Sstevel@tonic-gate 	/* is there room until end of buffer? */
3565*7c478bd9Sstevel@tonic-gate 	if (t > m) t = m;
3566*7c478bd9Sstevel@tonic-gate 	/* update check information */
3567*7c478bd9Sstevel@tonic-gate 	if (s->checkfn != Z_NULL)
3568*7c478bd9Sstevel@tonic-gate 	    s->check = (*s->checkfn)(s->check, q, t);
3569*7c478bd9Sstevel@tonic-gate 	zmemcpy(q, p, t);
3570*7c478bd9Sstevel@tonic-gate 	q += t;
3571*7c478bd9Sstevel@tonic-gate 	p += t;
3572*7c478bd9Sstevel@tonic-gate 	n -= t;
3573*7c478bd9Sstevel@tonic-gate 	z->total_out += t;
3574*7c478bd9Sstevel@tonic-gate 	s->read = q;    /* drag read pointer forward */
3575*7c478bd9Sstevel@tonic-gate /*      WRAP  */ 	/* expand WRAP macro by hand to handle s->read */
3576*7c478bd9Sstevel@tonic-gate 	if (q == s->end) {
3577*7c478bd9Sstevel@tonic-gate 	    s->read = q = s->window;
3578*7c478bd9Sstevel@tonic-gate 	    m = WAVAIL;
3579*7c478bd9Sstevel@tonic-gate 	}
3580*7c478bd9Sstevel@tonic-gate     }
3581*7c478bd9Sstevel@tonic-gate     UPDATE
3582*7c478bd9Sstevel@tonic-gate     return Z_OK;
3583*7c478bd9Sstevel@tonic-gate }
3584*7c478bd9Sstevel@tonic-gate 
3585*7c478bd9Sstevel@tonic-gate 
3586*7c478bd9Sstevel@tonic-gate /*
3587*7c478bd9Sstevel@tonic-gate  * At the end of a Deflate-compressed PPP packet, we expect to have seen
3588*7c478bd9Sstevel@tonic-gate  * a `stored' block type value but not the (zero) length bytes.
3589*7c478bd9Sstevel@tonic-gate  */
3590*7c478bd9Sstevel@tonic-gate local int inflate_packet_flush(s)
3591*7c478bd9Sstevel@tonic-gate     inflate_blocks_statef *s;
3592*7c478bd9Sstevel@tonic-gate {
3593*7c478bd9Sstevel@tonic-gate     if (s->mode != LENS)
3594*7c478bd9Sstevel@tonic-gate 	return Z_DATA_ERROR;
3595*7c478bd9Sstevel@tonic-gate     s->mode = TYPE;
3596*7c478bd9Sstevel@tonic-gate     return Z_OK;
3597*7c478bd9Sstevel@tonic-gate }
3598*7c478bd9Sstevel@tonic-gate 
3599*7c478bd9Sstevel@tonic-gate 
3600*7c478bd9Sstevel@tonic-gate /*+++++*/
3601*7c478bd9Sstevel@tonic-gate /* inftrees.c -- generate Huffman trees for efficient decoding
3602*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
3603*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
3604*7c478bd9Sstevel@tonic-gate  */
3605*7c478bd9Sstevel@tonic-gate 
3606*7c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
3607*7c478bd9Sstevel@tonic-gate #define base more.Base
3608*7c478bd9Sstevel@tonic-gate #define next more.Next
3609*7c478bd9Sstevel@tonic-gate #define exop word.what.Exop
3610*7c478bd9Sstevel@tonic-gate #define bits word.what.Bits
3611*7c478bd9Sstevel@tonic-gate 
3612*7c478bd9Sstevel@tonic-gate 
3613*7c478bd9Sstevel@tonic-gate local int huft_build OF((
3614*7c478bd9Sstevel@tonic-gate     uIntf *,            /* code lengths in bits */
3615*7c478bd9Sstevel@tonic-gate     uInt,               /* number of codes */
3616*7c478bd9Sstevel@tonic-gate     uInt,               /* number of "simple" codes */
3617*7c478bd9Sstevel@tonic-gate     uIntf *,            /* list of base values for non-simple codes */
3618*7c478bd9Sstevel@tonic-gate     uIntf *,            /* list of extra bits for non-simple codes */
3619*7c478bd9Sstevel@tonic-gate     inflate_huft * FAR*,/* result: starting table */
3620*7c478bd9Sstevel@tonic-gate     uIntf *,            /* maximum lookup bits (returns actual) */
3621*7c478bd9Sstevel@tonic-gate     z_stream *));       /* for zalloc function */
3622*7c478bd9Sstevel@tonic-gate 
3623*7c478bd9Sstevel@tonic-gate local voidpf falloc OF((
3624*7c478bd9Sstevel@tonic-gate     voidpf,             /* opaque pointer (not used) */
3625*7c478bd9Sstevel@tonic-gate     uInt,               /* number of items */
3626*7c478bd9Sstevel@tonic-gate     uInt));             /* size of item */
3627*7c478bd9Sstevel@tonic-gate 
3628*7c478bd9Sstevel@tonic-gate local void ffree OF((
3629*7c478bd9Sstevel@tonic-gate     voidpf q,           /* opaque pointer (not used) */
3630*7c478bd9Sstevel@tonic-gate     voidpf p,           /* what to free (not used) */
3631*7c478bd9Sstevel@tonic-gate     uInt n));		/* number of bytes (not used) */
3632*7c478bd9Sstevel@tonic-gate 
3633*7c478bd9Sstevel@tonic-gate /* Tables for deflate from PKZIP's appnote.txt. */
3634*7c478bd9Sstevel@tonic-gate local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
3635*7c478bd9Sstevel@tonic-gate         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
3636*7c478bd9Sstevel@tonic-gate         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
3637*7c478bd9Sstevel@tonic-gate         /* actually lengths - 2; also see note #13 above about 258 */
3638*7c478bd9Sstevel@tonic-gate local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
3639*7c478bd9Sstevel@tonic-gate         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3640*7c478bd9Sstevel@tonic-gate         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
3641*7c478bd9Sstevel@tonic-gate local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
3642*7c478bd9Sstevel@tonic-gate         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
3643*7c478bd9Sstevel@tonic-gate         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
3644*7c478bd9Sstevel@tonic-gate         8193, 12289, 16385, 24577};
3645*7c478bd9Sstevel@tonic-gate local uInt cpdext[] = { /* Extra bits for distance codes */
3646*7c478bd9Sstevel@tonic-gate         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
3647*7c478bd9Sstevel@tonic-gate         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
3648*7c478bd9Sstevel@tonic-gate         12, 12, 13, 13};
3649*7c478bd9Sstevel@tonic-gate 
3650*7c478bd9Sstevel@tonic-gate /*
3651*7c478bd9Sstevel@tonic-gate    Huffman code decoding is performed using a multi-level table lookup.
3652*7c478bd9Sstevel@tonic-gate    The fastest way to decode is to simply build a lookup table whose
3653*7c478bd9Sstevel@tonic-gate    size is determined by the longest code.  However, the time it takes
3654*7c478bd9Sstevel@tonic-gate    to build this table can also be a factor if the data being decoded
3655*7c478bd9Sstevel@tonic-gate    is not very long.  The most common codes are necessarily the
3656*7c478bd9Sstevel@tonic-gate    shortest codes, so those codes dominate the decoding time, and hence
3657*7c478bd9Sstevel@tonic-gate    the speed.  The idea is you can have a shorter table that decodes the
3658*7c478bd9Sstevel@tonic-gate    shorter, more probable codes, and then point to subsidiary tables for
3659*7c478bd9Sstevel@tonic-gate    the longer codes.  The time it costs to decode the longer codes is
3660*7c478bd9Sstevel@tonic-gate    then traded against the time it takes to make longer tables.
3661*7c478bd9Sstevel@tonic-gate 
3662*7c478bd9Sstevel@tonic-gate    This results of this trade are in the variables lbits and dbits
3663*7c478bd9Sstevel@tonic-gate    below.  lbits is the number of bits the first level table for literal/
3664*7c478bd9Sstevel@tonic-gate    length codes can decode in one step, and dbits is the same thing for
3665*7c478bd9Sstevel@tonic-gate    the distance codes.  Subsequent tables are also less than or equal to
3666*7c478bd9Sstevel@tonic-gate    those sizes.  These values may be adjusted either when all of the
3667*7c478bd9Sstevel@tonic-gate    codes are shorter than that, in which case the longest code length in
3668*7c478bd9Sstevel@tonic-gate    bits is used, or when the shortest code is *longer* than the requested
3669*7c478bd9Sstevel@tonic-gate    table size, in which case the length of the shortest code in bits is
3670*7c478bd9Sstevel@tonic-gate    used.
3671*7c478bd9Sstevel@tonic-gate 
3672*7c478bd9Sstevel@tonic-gate    There are two different values for the two tables, since they code a
3673*7c478bd9Sstevel@tonic-gate    different number of possibilities each.  The literal/length table
3674*7c478bd9Sstevel@tonic-gate    codes 286 possible values, or in a flat code, a little over eight
3675*7c478bd9Sstevel@tonic-gate    bits.  The distance table codes 30 possible values, or a little less
3676*7c478bd9Sstevel@tonic-gate    than five bits, flat.  The optimum values for speed end up being
3677*7c478bd9Sstevel@tonic-gate    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
3678*7c478bd9Sstevel@tonic-gate    The optimum values may differ though from machine to machine, and
3679*7c478bd9Sstevel@tonic-gate    possibly even between compilers.  Your mileage may vary.
3680*7c478bd9Sstevel@tonic-gate  */
3681*7c478bd9Sstevel@tonic-gate 
3682*7c478bd9Sstevel@tonic-gate 
3683*7c478bd9Sstevel@tonic-gate /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
3684*7c478bd9Sstevel@tonic-gate #define BMAX 15         /* maximum bit length of any code */
3685*7c478bd9Sstevel@tonic-gate #define N_MAX 288       /* maximum number of codes in any set */
3686*7c478bd9Sstevel@tonic-gate 
3687*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
3688*7c478bd9Sstevel@tonic-gate   uInt inflate_hufts;
3689*7c478bd9Sstevel@tonic-gate #endif
3690*7c478bd9Sstevel@tonic-gate 
3691*7c478bd9Sstevel@tonic-gate local int huft_build(b, n, s, d, e, t, m, zs)
3692*7c478bd9Sstevel@tonic-gate uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
3693*7c478bd9Sstevel@tonic-gate uInt n;                 /* number of codes (assumed <= N_MAX) */
3694*7c478bd9Sstevel@tonic-gate uInt s;                 /* number of simple-valued codes (0..s-1) */
3695*7c478bd9Sstevel@tonic-gate uIntf *d;               /* list of base values for non-simple codes */
3696*7c478bd9Sstevel@tonic-gate uIntf *e;               /* list of extra bits for non-simple codes */
3697*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *t;  /* result: starting table */
3698*7c478bd9Sstevel@tonic-gate uIntf *m;               /* maximum lookup bits, returns actual */
3699*7c478bd9Sstevel@tonic-gate z_stream *zs;           /* for zalloc function */
3700*7c478bd9Sstevel@tonic-gate /* Given a list of code lengths and a maximum table size, make a set of
3701*7c478bd9Sstevel@tonic-gate    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
3702*7c478bd9Sstevel@tonic-gate    if the given code set is incomplete (the tables are still built in this
3703*7c478bd9Sstevel@tonic-gate    case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
3704*7c478bd9Sstevel@tonic-gate    over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
3705*7c478bd9Sstevel@tonic-gate {
3706*7c478bd9Sstevel@tonic-gate 
3707*7c478bd9Sstevel@tonic-gate   uInt a;                       /* counter for codes of length k */
3708*7c478bd9Sstevel@tonic-gate   uInt c[BMAX+1];               /* bit length count table */
3709*7c478bd9Sstevel@tonic-gate   uInt f;                       /* i repeats in table every f entries */
3710*7c478bd9Sstevel@tonic-gate   int g;                        /* maximum code length */
3711*7c478bd9Sstevel@tonic-gate   int h;                        /* table level */
3712*7c478bd9Sstevel@tonic-gate   register uInt i;              /* counter, current code */
3713*7c478bd9Sstevel@tonic-gate   register uInt j;              /* counter */
3714*7c478bd9Sstevel@tonic-gate   register int k;               /* number of bits in current code */
3715*7c478bd9Sstevel@tonic-gate   int l;                        /* bits per table (returned in m) */
3716*7c478bd9Sstevel@tonic-gate   register uIntf *p;            /* pointer into c[], b[], or v[] */
3717*7c478bd9Sstevel@tonic-gate   inflate_huft *q;              /* points to current table */
3718*7c478bd9Sstevel@tonic-gate   struct inflate_huft_s r;      /* table entry for structure assignment */
3719*7c478bd9Sstevel@tonic-gate   inflate_huft *u[BMAX];        /* table stack */
3720*7c478bd9Sstevel@tonic-gate   uInt v[N_MAX];                /* values in order of bit length */
3721*7c478bd9Sstevel@tonic-gate   register int w;               /* bits before this table == (l * h) */
3722*7c478bd9Sstevel@tonic-gate   uInt x[BMAX+1];               /* bit offsets, then code stack */
3723*7c478bd9Sstevel@tonic-gate   uIntf *xp;                    /* pointer into x */
3724*7c478bd9Sstevel@tonic-gate   int y;                        /* number of dummy codes added */
3725*7c478bd9Sstevel@tonic-gate   uInt z;                       /* number of entries in current table */
3726*7c478bd9Sstevel@tonic-gate 
3727*7c478bd9Sstevel@tonic-gate 
3728*7c478bd9Sstevel@tonic-gate   /* Generate counts for each bit length */
3729*7c478bd9Sstevel@tonic-gate   p = c;
3730*7c478bd9Sstevel@tonic-gate #define C0 *p++ = 0;
3731*7c478bd9Sstevel@tonic-gate #define C2 C0 C0 C0 C0
3732*7c478bd9Sstevel@tonic-gate #define C4 C2 C2 C2 C2
3733*7c478bd9Sstevel@tonic-gate   C4                            /* clear c[]--assume BMAX+1 is 16 */
3734*7c478bd9Sstevel@tonic-gate   p = b;  i = n;
3735*7c478bd9Sstevel@tonic-gate   do {
3736*7c478bd9Sstevel@tonic-gate     c[*p++]++;                  /* assume all entries <= BMAX */
3737*7c478bd9Sstevel@tonic-gate   } while (--i);
3738*7c478bd9Sstevel@tonic-gate   if (c[0] == n)                /* null input--all zero length codes */
3739*7c478bd9Sstevel@tonic-gate   {
3740*7c478bd9Sstevel@tonic-gate     *t = (inflate_huft *)Z_NULL;
3741*7c478bd9Sstevel@tonic-gate     *m = 0;
3742*7c478bd9Sstevel@tonic-gate     return Z_OK;
3743*7c478bd9Sstevel@tonic-gate   }
3744*7c478bd9Sstevel@tonic-gate 
3745*7c478bd9Sstevel@tonic-gate 
3746*7c478bd9Sstevel@tonic-gate   /* Find minimum and maximum length, bound *m by those */
3747*7c478bd9Sstevel@tonic-gate   l = *m;
3748*7c478bd9Sstevel@tonic-gate   for (j = 1; j <= BMAX; j++)
3749*7c478bd9Sstevel@tonic-gate     if (c[j])
3750*7c478bd9Sstevel@tonic-gate       break;
3751*7c478bd9Sstevel@tonic-gate   k = j;                        /* minimum code length */
3752*7c478bd9Sstevel@tonic-gate   if ((uInt)l < j)
3753*7c478bd9Sstevel@tonic-gate     l = j;
3754*7c478bd9Sstevel@tonic-gate   for (i = BMAX; i; i--)
3755*7c478bd9Sstevel@tonic-gate     if (c[i])
3756*7c478bd9Sstevel@tonic-gate       break;
3757*7c478bd9Sstevel@tonic-gate   g = i;                        /* maximum code length */
3758*7c478bd9Sstevel@tonic-gate   if ((uInt)l > i)
3759*7c478bd9Sstevel@tonic-gate     l = i;
3760*7c478bd9Sstevel@tonic-gate   *m = l;
3761*7c478bd9Sstevel@tonic-gate 
3762*7c478bd9Sstevel@tonic-gate 
3763*7c478bd9Sstevel@tonic-gate   /* Adjust last length count to fill out codes, if needed */
3764*7c478bd9Sstevel@tonic-gate   for (y = 1 << j; j < i; j++, y <<= 1)
3765*7c478bd9Sstevel@tonic-gate     if ((y -= c[j]) < 0)
3766*7c478bd9Sstevel@tonic-gate       return Z_DATA_ERROR;
3767*7c478bd9Sstevel@tonic-gate   if ((y -= c[i]) < 0)
3768*7c478bd9Sstevel@tonic-gate     return Z_DATA_ERROR;
3769*7c478bd9Sstevel@tonic-gate   c[i] += y;
3770*7c478bd9Sstevel@tonic-gate 
3771*7c478bd9Sstevel@tonic-gate 
3772*7c478bd9Sstevel@tonic-gate   /* Generate starting offsets into the value table for each length */
3773*7c478bd9Sstevel@tonic-gate   x[1] = j = 0;
3774*7c478bd9Sstevel@tonic-gate   p = c + 1;  xp = x + 2;
3775*7c478bd9Sstevel@tonic-gate   while (--i) {                 /* note that i == g from above */
3776*7c478bd9Sstevel@tonic-gate     *xp++ = (j += *p++);
3777*7c478bd9Sstevel@tonic-gate   }
3778*7c478bd9Sstevel@tonic-gate 
3779*7c478bd9Sstevel@tonic-gate 
3780*7c478bd9Sstevel@tonic-gate   /* Make a table of values in order of bit lengths */
3781*7c478bd9Sstevel@tonic-gate   p = b;  i = 0;
3782*7c478bd9Sstevel@tonic-gate   do {
3783*7c478bd9Sstevel@tonic-gate     if ((j = *p++) != 0)
3784*7c478bd9Sstevel@tonic-gate       v[x[j]++] = i;
3785*7c478bd9Sstevel@tonic-gate   } while (++i < n);
3786*7c478bd9Sstevel@tonic-gate 
3787*7c478bd9Sstevel@tonic-gate 
3788*7c478bd9Sstevel@tonic-gate   /* Generate the Huffman codes and for each, make the table entries */
3789*7c478bd9Sstevel@tonic-gate   x[0] = i = 0;                 /* first Huffman code is zero */
3790*7c478bd9Sstevel@tonic-gate   p = v;                        /* grab values in bit order */
3791*7c478bd9Sstevel@tonic-gate   h = -1;                       /* no tables yet--level -1 */
3792*7c478bd9Sstevel@tonic-gate   w = -l;                       /* bits decoded == (l * h) */
3793*7c478bd9Sstevel@tonic-gate   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
3794*7c478bd9Sstevel@tonic-gate   q = (inflate_huft *)Z_NULL;   /* ditto */
3795*7c478bd9Sstevel@tonic-gate   z = 0;                        /* ditto */
3796*7c478bd9Sstevel@tonic-gate 
3797*7c478bd9Sstevel@tonic-gate   /* go through the bit lengths (k already is bits in shortest code) */
3798*7c478bd9Sstevel@tonic-gate   for (; k <= g; k++)
3799*7c478bd9Sstevel@tonic-gate   {
3800*7c478bd9Sstevel@tonic-gate     a = c[k];
3801*7c478bd9Sstevel@tonic-gate     while (a--)
3802*7c478bd9Sstevel@tonic-gate     {
3803*7c478bd9Sstevel@tonic-gate       /* here i is the Huffman code of length k bits for value *p */
3804*7c478bd9Sstevel@tonic-gate       /* make tables up to required level */
3805*7c478bd9Sstevel@tonic-gate       while (k > w + l)
3806*7c478bd9Sstevel@tonic-gate       {
3807*7c478bd9Sstevel@tonic-gate         h++;
3808*7c478bd9Sstevel@tonic-gate         w += l;                 /* previous table always l bits */
3809*7c478bd9Sstevel@tonic-gate 
3810*7c478bd9Sstevel@tonic-gate         /* compute minimum size table less than or equal to l bits */
3811*7c478bd9Sstevel@tonic-gate         z = (z = g - w) > (uInt)l ? l : z;      /* table size upper limit */
3812*7c478bd9Sstevel@tonic-gate         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
3813*7c478bd9Sstevel@tonic-gate         {                       /* too few codes for k-w bit table */
3814*7c478bd9Sstevel@tonic-gate           f -= a + 1;           /* deduct codes from patterns left */
3815*7c478bd9Sstevel@tonic-gate           xp = c + k;
3816*7c478bd9Sstevel@tonic-gate           if (j < z)
3817*7c478bd9Sstevel@tonic-gate             while (++j < z)     /* try smaller tables up to z bits */
3818*7c478bd9Sstevel@tonic-gate             {
3819*7c478bd9Sstevel@tonic-gate               if ((f <<= 1) <= *++xp)
3820*7c478bd9Sstevel@tonic-gate                 break;          /* enough codes to use up j bits */
3821*7c478bd9Sstevel@tonic-gate               f -= *xp;         /* else deduct codes from patterns */
3822*7c478bd9Sstevel@tonic-gate             }
3823*7c478bd9Sstevel@tonic-gate         }
3824*7c478bd9Sstevel@tonic-gate         z = 1 << j;             /* table entries for j-bit table */
3825*7c478bd9Sstevel@tonic-gate 
3826*7c478bd9Sstevel@tonic-gate         /* allocate and link in new table */
3827*7c478bd9Sstevel@tonic-gate         if ((q = (inflate_huft *)ZALLOC
3828*7c478bd9Sstevel@tonic-gate              (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
3829*7c478bd9Sstevel@tonic-gate         {
3830*7c478bd9Sstevel@tonic-gate           if (h)
3831*7c478bd9Sstevel@tonic-gate             inflate_trees_free(u[0], zs);
3832*7c478bd9Sstevel@tonic-gate           return Z_MEM_ERROR;   /* not enough memory */
3833*7c478bd9Sstevel@tonic-gate         }
3834*7c478bd9Sstevel@tonic-gate 	q->word.Nalloc = z + 1;
3835*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
3836*7c478bd9Sstevel@tonic-gate         inflate_hufts += z + 1;
3837*7c478bd9Sstevel@tonic-gate #endif
3838*7c478bd9Sstevel@tonic-gate         *t = q + 1;             /* link to list for huft_free() */
3839*7c478bd9Sstevel@tonic-gate         *(t = &(q->next)) = Z_NULL;
3840*7c478bd9Sstevel@tonic-gate         u[h] = ++q;             /* table starts after link */
3841*7c478bd9Sstevel@tonic-gate 
3842*7c478bd9Sstevel@tonic-gate         /* connect to last table, if there is one */
3843*7c478bd9Sstevel@tonic-gate         if (h)
3844*7c478bd9Sstevel@tonic-gate         {
3845*7c478bd9Sstevel@tonic-gate           x[h] = i;             /* save pattern for backing up */
3846*7c478bd9Sstevel@tonic-gate           r.bits = (Byte)l;     /* bits to dump before this table */
3847*7c478bd9Sstevel@tonic-gate           r.exop = (Byte)j;     /* bits in this table */
3848*7c478bd9Sstevel@tonic-gate           r.next = q;           /* pointer to this table */
3849*7c478bd9Sstevel@tonic-gate           j = i >> (w - l);     /* (get around Turbo C bug) */
3850*7c478bd9Sstevel@tonic-gate           u[h-1][j] = r;        /* connect to last table */
3851*7c478bd9Sstevel@tonic-gate         }
3852*7c478bd9Sstevel@tonic-gate       }
3853*7c478bd9Sstevel@tonic-gate 
3854*7c478bd9Sstevel@tonic-gate       /* set up table entry in r */
3855*7c478bd9Sstevel@tonic-gate       r.bits = (Byte)(k - w);
3856*7c478bd9Sstevel@tonic-gate       if (p >= v + n)
3857*7c478bd9Sstevel@tonic-gate         r.exop = 128 + 64;      /* out of values--invalid code */
3858*7c478bd9Sstevel@tonic-gate       else if (*p < s)
3859*7c478bd9Sstevel@tonic-gate       {
3860*7c478bd9Sstevel@tonic-gate         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
3861*7c478bd9Sstevel@tonic-gate         r.base = *p++;          /* simple code is just the value */
3862*7c478bd9Sstevel@tonic-gate       }
3863*7c478bd9Sstevel@tonic-gate       else
3864*7c478bd9Sstevel@tonic-gate       {
3865*7c478bd9Sstevel@tonic-gate         r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
3866*7c478bd9Sstevel@tonic-gate         r.base = d[*p++ - s];
3867*7c478bd9Sstevel@tonic-gate       }
3868*7c478bd9Sstevel@tonic-gate 
3869*7c478bd9Sstevel@tonic-gate       /* fill code-like entries with r */
3870*7c478bd9Sstevel@tonic-gate       f = 1 << (k - w);
3871*7c478bd9Sstevel@tonic-gate       for (j = i >> w; j < z; j += f)
3872*7c478bd9Sstevel@tonic-gate         q[j] = r;
3873*7c478bd9Sstevel@tonic-gate 
3874*7c478bd9Sstevel@tonic-gate       /* backwards increment the k-bit code i */
3875*7c478bd9Sstevel@tonic-gate       for (j = 1 << (k - 1); i & j; j >>= 1)
3876*7c478bd9Sstevel@tonic-gate         i ^= j;
3877*7c478bd9Sstevel@tonic-gate       i ^= j;
3878*7c478bd9Sstevel@tonic-gate 
3879*7c478bd9Sstevel@tonic-gate       /* backup over finished tables */
3880*7c478bd9Sstevel@tonic-gate       while ((i & ((1 << w) - 1)) != x[h])
3881*7c478bd9Sstevel@tonic-gate       {
3882*7c478bd9Sstevel@tonic-gate         h--;                    /* don't need to update q */
3883*7c478bd9Sstevel@tonic-gate         w -= l;
3884*7c478bd9Sstevel@tonic-gate       }
3885*7c478bd9Sstevel@tonic-gate     }
3886*7c478bd9Sstevel@tonic-gate   }
3887*7c478bd9Sstevel@tonic-gate 
3888*7c478bd9Sstevel@tonic-gate 
3889*7c478bd9Sstevel@tonic-gate   /* Return Z_BUF_ERROR if we were given an incomplete table */
3890*7c478bd9Sstevel@tonic-gate   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
3891*7c478bd9Sstevel@tonic-gate }
3892*7c478bd9Sstevel@tonic-gate 
3893*7c478bd9Sstevel@tonic-gate 
3894*7c478bd9Sstevel@tonic-gate local int inflate_trees_bits(c, bb, tb, z)
3895*7c478bd9Sstevel@tonic-gate uIntf *c;               /* 19 code lengths */
3896*7c478bd9Sstevel@tonic-gate uIntf *bb;              /* bits tree desired/actual depth */
3897*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *tb; /* bits tree result */
3898*7c478bd9Sstevel@tonic-gate z_stream *z;            /* for zfree function */
3899*7c478bd9Sstevel@tonic-gate {
3900*7c478bd9Sstevel@tonic-gate   int r;
3901*7c478bd9Sstevel@tonic-gate 
3902*7c478bd9Sstevel@tonic-gate   r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
3903*7c478bd9Sstevel@tonic-gate   if (r == Z_DATA_ERROR)
3904*7c478bd9Sstevel@tonic-gate     z->msg = "oversubscribed dynamic bit lengths tree";
3905*7c478bd9Sstevel@tonic-gate   else if (r == Z_BUF_ERROR)
3906*7c478bd9Sstevel@tonic-gate   {
3907*7c478bd9Sstevel@tonic-gate     inflate_trees_free(*tb, z);
3908*7c478bd9Sstevel@tonic-gate     z->msg = "incomplete dynamic bit lengths tree";
3909*7c478bd9Sstevel@tonic-gate     r = Z_DATA_ERROR;
3910*7c478bd9Sstevel@tonic-gate   }
3911*7c478bd9Sstevel@tonic-gate   return r;
3912*7c478bd9Sstevel@tonic-gate }
3913*7c478bd9Sstevel@tonic-gate 
3914*7c478bd9Sstevel@tonic-gate 
3915*7c478bd9Sstevel@tonic-gate local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
3916*7c478bd9Sstevel@tonic-gate uInt nl;                /* number of literal/length codes */
3917*7c478bd9Sstevel@tonic-gate uInt nd;                /* number of distance codes */
3918*7c478bd9Sstevel@tonic-gate uIntf *c;               /* that many (total) code lengths */
3919*7c478bd9Sstevel@tonic-gate uIntf *bl;              /* literal desired/actual bit depth */
3920*7c478bd9Sstevel@tonic-gate uIntf *bd;              /* distance desired/actual bit depth */
3921*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *tl; /* literal/length tree result */
3922*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *td; /* distance tree result */
3923*7c478bd9Sstevel@tonic-gate z_stream *z;            /* for zfree function */
3924*7c478bd9Sstevel@tonic-gate {
3925*7c478bd9Sstevel@tonic-gate   int r;
3926*7c478bd9Sstevel@tonic-gate 
3927*7c478bd9Sstevel@tonic-gate   /* build literal/length tree */
3928*7c478bd9Sstevel@tonic-gate   if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
3929*7c478bd9Sstevel@tonic-gate   {
3930*7c478bd9Sstevel@tonic-gate     if (r == Z_DATA_ERROR)
3931*7c478bd9Sstevel@tonic-gate       z->msg = "oversubscribed literal/length tree";
3932*7c478bd9Sstevel@tonic-gate     else if (r == Z_BUF_ERROR)
3933*7c478bd9Sstevel@tonic-gate     {
3934*7c478bd9Sstevel@tonic-gate       inflate_trees_free(*tl, z);
3935*7c478bd9Sstevel@tonic-gate       z->msg = "incomplete literal/length tree";
3936*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
3937*7c478bd9Sstevel@tonic-gate     }
3938*7c478bd9Sstevel@tonic-gate     return r;
3939*7c478bd9Sstevel@tonic-gate   }
3940*7c478bd9Sstevel@tonic-gate 
3941*7c478bd9Sstevel@tonic-gate   /* build distance tree */
3942*7c478bd9Sstevel@tonic-gate   if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
3943*7c478bd9Sstevel@tonic-gate   {
3944*7c478bd9Sstevel@tonic-gate     if (r == Z_DATA_ERROR)
3945*7c478bd9Sstevel@tonic-gate       z->msg = "oversubscribed literal/length tree";
3946*7c478bd9Sstevel@tonic-gate     else if (r == Z_BUF_ERROR) {
3947*7c478bd9Sstevel@tonic-gate #ifdef PKZIP_BUG_WORKAROUND
3948*7c478bd9Sstevel@tonic-gate       r = Z_OK;
3949*7c478bd9Sstevel@tonic-gate     }
3950*7c478bd9Sstevel@tonic-gate #else
3951*7c478bd9Sstevel@tonic-gate       inflate_trees_free(*td, z);
3952*7c478bd9Sstevel@tonic-gate       z->msg = "incomplete literal/length tree";
3953*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
3954*7c478bd9Sstevel@tonic-gate     }
3955*7c478bd9Sstevel@tonic-gate     inflate_trees_free(*tl, z);
3956*7c478bd9Sstevel@tonic-gate     return r;
3957*7c478bd9Sstevel@tonic-gate #endif
3958*7c478bd9Sstevel@tonic-gate   }
3959*7c478bd9Sstevel@tonic-gate 
3960*7c478bd9Sstevel@tonic-gate   /* done */
3961*7c478bd9Sstevel@tonic-gate   return Z_OK;
3962*7c478bd9Sstevel@tonic-gate }
3963*7c478bd9Sstevel@tonic-gate 
3964*7c478bd9Sstevel@tonic-gate 
3965*7c478bd9Sstevel@tonic-gate /* build fixed tables only once--keep them here */
3966*7c478bd9Sstevel@tonic-gate local int fixed_lock = 0;
3967*7c478bd9Sstevel@tonic-gate local int fixed_built = 0;
3968*7c478bd9Sstevel@tonic-gate #define FIXEDH 530      /* number of hufts used by fixed tables */
3969*7c478bd9Sstevel@tonic-gate local uInt fixed_left = FIXEDH;
3970*7c478bd9Sstevel@tonic-gate local inflate_huft fixed_mem[FIXEDH];
3971*7c478bd9Sstevel@tonic-gate local uInt fixed_bl;
3972*7c478bd9Sstevel@tonic-gate local uInt fixed_bd;
3973*7c478bd9Sstevel@tonic-gate local inflate_huft *fixed_tl;
3974*7c478bd9Sstevel@tonic-gate local inflate_huft *fixed_td;
3975*7c478bd9Sstevel@tonic-gate 
3976*7c478bd9Sstevel@tonic-gate 
3977*7c478bd9Sstevel@tonic-gate local voidpf falloc(q, n, s)
3978*7c478bd9Sstevel@tonic-gate voidpf q;        /* opaque pointer (not used) */
3979*7c478bd9Sstevel@tonic-gate uInt n;         /* number of items */
3980*7c478bd9Sstevel@tonic-gate uInt s;         /* size of item */
3981*7c478bd9Sstevel@tonic-gate {
3982*7c478bd9Sstevel@tonic-gate   Assert(s == sizeof(inflate_huft) && n <= fixed_left,
3983*7c478bd9Sstevel@tonic-gate          "inflate_trees falloc overflow");
3984*7c478bd9Sstevel@tonic-gate   if (q) s++; /* to make some compilers happy */
3985*7c478bd9Sstevel@tonic-gate   fixed_left -= n;
3986*7c478bd9Sstevel@tonic-gate   return (voidpf)(fixed_mem + fixed_left);
3987*7c478bd9Sstevel@tonic-gate }
3988*7c478bd9Sstevel@tonic-gate 
3989*7c478bd9Sstevel@tonic-gate 
3990*7c478bd9Sstevel@tonic-gate local void ffree(q, p, n)
3991*7c478bd9Sstevel@tonic-gate voidpf q;
3992*7c478bd9Sstevel@tonic-gate voidpf p;
3993*7c478bd9Sstevel@tonic-gate uInt n;
3994*7c478bd9Sstevel@tonic-gate {
3995*7c478bd9Sstevel@tonic-gate   Assert(0, "inflate_trees ffree called!");
3996*7c478bd9Sstevel@tonic-gate   if (q) q = p; /* to make some compilers happy */
3997*7c478bd9Sstevel@tonic-gate }
3998*7c478bd9Sstevel@tonic-gate 
3999*7c478bd9Sstevel@tonic-gate 
4000*7c478bd9Sstevel@tonic-gate local int inflate_trees_fixed(bl, bd, tl, td)
4001*7c478bd9Sstevel@tonic-gate uIntf *bl;               /* literal desired/actual bit depth */
4002*7c478bd9Sstevel@tonic-gate uIntf *bd;               /* distance desired/actual bit depth */
4003*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *tl;  /* literal/length tree result */
4004*7c478bd9Sstevel@tonic-gate inflate_huft * FAR *td;  /* distance tree result */
4005*7c478bd9Sstevel@tonic-gate {
4006*7c478bd9Sstevel@tonic-gate   /* build fixed tables if not built already--lock out other instances */
4007*7c478bd9Sstevel@tonic-gate   while (++fixed_lock > 1)
4008*7c478bd9Sstevel@tonic-gate     fixed_lock--;
4009*7c478bd9Sstevel@tonic-gate   if (!fixed_built)
4010*7c478bd9Sstevel@tonic-gate   {
4011*7c478bd9Sstevel@tonic-gate     int k;              /* temporary variable */
4012*7c478bd9Sstevel@tonic-gate     unsigned c[288];    /* length list for huft_build */
4013*7c478bd9Sstevel@tonic-gate     z_stream z;         /* for falloc function */
4014*7c478bd9Sstevel@tonic-gate 
4015*7c478bd9Sstevel@tonic-gate     /* set up fake z_stream for memory routines */
4016*7c478bd9Sstevel@tonic-gate     z.zalloc = falloc;
4017*7c478bd9Sstevel@tonic-gate     z.zfree = ffree;
4018*7c478bd9Sstevel@tonic-gate     z.opaque = Z_NULL;
4019*7c478bd9Sstevel@tonic-gate 
4020*7c478bd9Sstevel@tonic-gate     /* literal table */
4021*7c478bd9Sstevel@tonic-gate     for (k = 0; k < 144; k++)
4022*7c478bd9Sstevel@tonic-gate       c[k] = 8;
4023*7c478bd9Sstevel@tonic-gate     for (; k < 256; k++)
4024*7c478bd9Sstevel@tonic-gate       c[k] = 9;
4025*7c478bd9Sstevel@tonic-gate     for (; k < 280; k++)
4026*7c478bd9Sstevel@tonic-gate       c[k] = 7;
4027*7c478bd9Sstevel@tonic-gate     for (; k < 288; k++)
4028*7c478bd9Sstevel@tonic-gate       c[k] = 8;
4029*7c478bd9Sstevel@tonic-gate     fixed_bl = 7;
4030*7c478bd9Sstevel@tonic-gate     huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
4031*7c478bd9Sstevel@tonic-gate 
4032*7c478bd9Sstevel@tonic-gate     /* distance table */
4033*7c478bd9Sstevel@tonic-gate     for (k = 0; k < 30; k++)
4034*7c478bd9Sstevel@tonic-gate       c[k] = 5;
4035*7c478bd9Sstevel@tonic-gate     fixed_bd = 5;
4036*7c478bd9Sstevel@tonic-gate     huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
4037*7c478bd9Sstevel@tonic-gate 
4038*7c478bd9Sstevel@tonic-gate     /* done */
4039*7c478bd9Sstevel@tonic-gate     fixed_built = 1;
4040*7c478bd9Sstevel@tonic-gate   }
4041*7c478bd9Sstevel@tonic-gate   fixed_lock--;
4042*7c478bd9Sstevel@tonic-gate   *bl = fixed_bl;
4043*7c478bd9Sstevel@tonic-gate   *bd = fixed_bd;
4044*7c478bd9Sstevel@tonic-gate   *tl = fixed_tl;
4045*7c478bd9Sstevel@tonic-gate   *td = fixed_td;
4046*7c478bd9Sstevel@tonic-gate   return Z_OK;
4047*7c478bd9Sstevel@tonic-gate }
4048*7c478bd9Sstevel@tonic-gate 
4049*7c478bd9Sstevel@tonic-gate 
4050*7c478bd9Sstevel@tonic-gate local int inflate_trees_free(t, z)
4051*7c478bd9Sstevel@tonic-gate inflate_huft *t;        /* table to free */
4052*7c478bd9Sstevel@tonic-gate z_stream *z;            /* for zfree function */
4053*7c478bd9Sstevel@tonic-gate /* Free the malloc'ed tables built by huft_build(), which makes a linked
4054*7c478bd9Sstevel@tonic-gate    list of the tables it made, with the links in a dummy first entry of
4055*7c478bd9Sstevel@tonic-gate    each table. */
4056*7c478bd9Sstevel@tonic-gate {
4057*7c478bd9Sstevel@tonic-gate   register inflate_huft *p, *q;
4058*7c478bd9Sstevel@tonic-gate 
4059*7c478bd9Sstevel@tonic-gate   /* Go through linked list, freeing from the malloced (t[-1]) address. */
4060*7c478bd9Sstevel@tonic-gate   p = t;
4061*7c478bd9Sstevel@tonic-gate   while (p != Z_NULL)
4062*7c478bd9Sstevel@tonic-gate   {
4063*7c478bd9Sstevel@tonic-gate     q = (--p)->next;
4064*7c478bd9Sstevel@tonic-gate     ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
4065*7c478bd9Sstevel@tonic-gate     p = q;
4066*7c478bd9Sstevel@tonic-gate   }
4067*7c478bd9Sstevel@tonic-gate   return Z_OK;
4068*7c478bd9Sstevel@tonic-gate }
4069*7c478bd9Sstevel@tonic-gate 
4070*7c478bd9Sstevel@tonic-gate /*+++++*/
4071*7c478bd9Sstevel@tonic-gate /* infcodes.c -- process literals and length/distance pairs
4072*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
4073*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
4074*7c478bd9Sstevel@tonic-gate  */
4075*7c478bd9Sstevel@tonic-gate 
4076*7c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
4077*7c478bd9Sstevel@tonic-gate #define base more.Base
4078*7c478bd9Sstevel@tonic-gate #define next more.Next
4079*7c478bd9Sstevel@tonic-gate #define exop word.what.Exop
4080*7c478bd9Sstevel@tonic-gate #define bits word.what.Bits
4081*7c478bd9Sstevel@tonic-gate 
4082*7c478bd9Sstevel@tonic-gate /* inflate codes private state */
4083*7c478bd9Sstevel@tonic-gate struct inflate_codes_state {
4084*7c478bd9Sstevel@tonic-gate 
4085*7c478bd9Sstevel@tonic-gate   /* mode */
4086*7c478bd9Sstevel@tonic-gate   enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
4087*7c478bd9Sstevel@tonic-gate       START,    /* x: set up for LEN */
4088*7c478bd9Sstevel@tonic-gate       LEN,      /* i: get length/literal/eob next */
4089*7c478bd9Sstevel@tonic-gate       LENEXT,   /* i: getting length extra (have base) */
4090*7c478bd9Sstevel@tonic-gate       DIST,     /* i: get distance next */
4091*7c478bd9Sstevel@tonic-gate       DISTEXT,  /* i: getting distance extra */
4092*7c478bd9Sstevel@tonic-gate       COPY,     /* o: copying bytes in window, waiting for space */
4093*7c478bd9Sstevel@tonic-gate       LIT,      /* o: got literal, waiting for output space */
4094*7c478bd9Sstevel@tonic-gate       WASH,     /* o: got eob, possibly still output waiting */
4095*7c478bd9Sstevel@tonic-gate       END,      /* x: got eob and all data flushed */
4096*7c478bd9Sstevel@tonic-gate       BADCODE}  /* x: got error */
4097*7c478bd9Sstevel@tonic-gate     mode;               /* current inflate_codes mode */
4098*7c478bd9Sstevel@tonic-gate 
4099*7c478bd9Sstevel@tonic-gate   /* mode dependent information */
4100*7c478bd9Sstevel@tonic-gate   uInt len;
4101*7c478bd9Sstevel@tonic-gate   union {
4102*7c478bd9Sstevel@tonic-gate     struct {
4103*7c478bd9Sstevel@tonic-gate       inflate_huft *tree;       /* pointer into tree */
4104*7c478bd9Sstevel@tonic-gate       uInt need;                /* bits needed */
4105*7c478bd9Sstevel@tonic-gate     } code;             /* if LEN or DIST, where in tree */
4106*7c478bd9Sstevel@tonic-gate     uInt lit;           /* if LIT, literal */
4107*7c478bd9Sstevel@tonic-gate     struct {
4108*7c478bd9Sstevel@tonic-gate       uInt get;                 /* bits to get for extra */
4109*7c478bd9Sstevel@tonic-gate       uInt dist;                /* distance back to copy from */
4110*7c478bd9Sstevel@tonic-gate     } copy;             /* if EXT or COPY, where and how much */
4111*7c478bd9Sstevel@tonic-gate   } sub;                /* submode */
4112*7c478bd9Sstevel@tonic-gate 
4113*7c478bd9Sstevel@tonic-gate   /* mode independent information */
4114*7c478bd9Sstevel@tonic-gate   Byte lbits;           /* ltree bits decoded per branch */
4115*7c478bd9Sstevel@tonic-gate   Byte dbits;           /* dtree bits decoder per branch */
4116*7c478bd9Sstevel@tonic-gate   inflate_huft *ltree;          /* literal/length/eob tree */
4117*7c478bd9Sstevel@tonic-gate   inflate_huft *dtree;          /* distance tree */
4118*7c478bd9Sstevel@tonic-gate 
4119*7c478bd9Sstevel@tonic-gate };
4120*7c478bd9Sstevel@tonic-gate 
4121*7c478bd9Sstevel@tonic-gate 
4122*7c478bd9Sstevel@tonic-gate local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
4123*7c478bd9Sstevel@tonic-gate uInt bl, bd;
4124*7c478bd9Sstevel@tonic-gate inflate_huft *tl, *td;
4125*7c478bd9Sstevel@tonic-gate z_stream *z;
4126*7c478bd9Sstevel@tonic-gate {
4127*7c478bd9Sstevel@tonic-gate   inflate_codes_statef *c;
4128*7c478bd9Sstevel@tonic-gate 
4129*7c478bd9Sstevel@tonic-gate   if ((c = (inflate_codes_statef *)
4130*7c478bd9Sstevel@tonic-gate        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
4131*7c478bd9Sstevel@tonic-gate   {
4132*7c478bd9Sstevel@tonic-gate     c->mode = START;
4133*7c478bd9Sstevel@tonic-gate     c->lbits = (Byte)bl;
4134*7c478bd9Sstevel@tonic-gate     c->dbits = (Byte)bd;
4135*7c478bd9Sstevel@tonic-gate     c->ltree = tl;
4136*7c478bd9Sstevel@tonic-gate     c->dtree = td;
4137*7c478bd9Sstevel@tonic-gate     Tracev((stderr, "inflate:       codes new\n"));
4138*7c478bd9Sstevel@tonic-gate   }
4139*7c478bd9Sstevel@tonic-gate   return c;
4140*7c478bd9Sstevel@tonic-gate }
4141*7c478bd9Sstevel@tonic-gate 
4142*7c478bd9Sstevel@tonic-gate 
4143*7c478bd9Sstevel@tonic-gate local int inflate_codes(s, z, r)
4144*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
4145*7c478bd9Sstevel@tonic-gate z_stream *z;
4146*7c478bd9Sstevel@tonic-gate int r;
4147*7c478bd9Sstevel@tonic-gate {
4148*7c478bd9Sstevel@tonic-gate   uInt j;               /* temporary storage */
4149*7c478bd9Sstevel@tonic-gate   inflate_huft *t;      /* temporary pointer */
4150*7c478bd9Sstevel@tonic-gate   uInt e;               /* extra bits or operation */
4151*7c478bd9Sstevel@tonic-gate   uLong b;              /* bit buffer */
4152*7c478bd9Sstevel@tonic-gate   uInt k;               /* bits in bit buffer */
4153*7c478bd9Sstevel@tonic-gate   Bytef *p;             /* input data pointer */
4154*7c478bd9Sstevel@tonic-gate   uInt n;               /* bytes available there */
4155*7c478bd9Sstevel@tonic-gate   Bytef *q;             /* output window write pointer */
4156*7c478bd9Sstevel@tonic-gate   uInt m;               /* bytes to end of window or read pointer */
4157*7c478bd9Sstevel@tonic-gate   Bytef *f;             /* pointer to copy strings from */
4158*7c478bd9Sstevel@tonic-gate   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
4159*7c478bd9Sstevel@tonic-gate 
4160*7c478bd9Sstevel@tonic-gate   /* copy input/output information to locals (UPDATE macro restores) */
4161*7c478bd9Sstevel@tonic-gate   LOAD
4162*7c478bd9Sstevel@tonic-gate 
4163*7c478bd9Sstevel@tonic-gate   /* process input and output based on current state */
4164*7c478bd9Sstevel@tonic-gate   while (1) switch (c->mode)
4165*7c478bd9Sstevel@tonic-gate   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
4166*7c478bd9Sstevel@tonic-gate     case START:         /* x: set up for LEN */
4167*7c478bd9Sstevel@tonic-gate #ifndef SLOW
4168*7c478bd9Sstevel@tonic-gate       if (m >= 258 && n >= 10)
4169*7c478bd9Sstevel@tonic-gate       {
4170*7c478bd9Sstevel@tonic-gate         UPDATE
4171*7c478bd9Sstevel@tonic-gate         r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
4172*7c478bd9Sstevel@tonic-gate         LOAD
4173*7c478bd9Sstevel@tonic-gate         if (r != Z_OK)
4174*7c478bd9Sstevel@tonic-gate         {
4175*7c478bd9Sstevel@tonic-gate           c->mode = r == Z_STREAM_END ? WASH : BADCODE;
4176*7c478bd9Sstevel@tonic-gate           break;
4177*7c478bd9Sstevel@tonic-gate         }
4178*7c478bd9Sstevel@tonic-gate       }
4179*7c478bd9Sstevel@tonic-gate #endif /* !SLOW */
4180*7c478bd9Sstevel@tonic-gate       c->sub.code.need = c->lbits;
4181*7c478bd9Sstevel@tonic-gate       c->sub.code.tree = c->ltree;
4182*7c478bd9Sstevel@tonic-gate       c->mode = LEN;
4183*7c478bd9Sstevel@tonic-gate     case LEN:           /* i: get length/literal/eob next */
4184*7c478bd9Sstevel@tonic-gate       j = c->sub.code.need;
4185*7c478bd9Sstevel@tonic-gate       NEEDBITS(j)
4186*7c478bd9Sstevel@tonic-gate       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
4187*7c478bd9Sstevel@tonic-gate       DUMPBITS(t->bits)
4188*7c478bd9Sstevel@tonic-gate       e = (uInt)(t->exop);
4189*7c478bd9Sstevel@tonic-gate       if (e == 0)               /* literal */
4190*7c478bd9Sstevel@tonic-gate       {
4191*7c478bd9Sstevel@tonic-gate         c->sub.lit = t->base;
4192*7c478bd9Sstevel@tonic-gate         Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4193*7c478bd9Sstevel@tonic-gate                  "inflate:         literal '%c'\n" :
4194*7c478bd9Sstevel@tonic-gate                  "inflate:         literal 0x%02x\n", t->base));
4195*7c478bd9Sstevel@tonic-gate         c->mode = LIT;
4196*7c478bd9Sstevel@tonic-gate         break;
4197*7c478bd9Sstevel@tonic-gate       }
4198*7c478bd9Sstevel@tonic-gate       if (e & 16)               /* length */
4199*7c478bd9Sstevel@tonic-gate       {
4200*7c478bd9Sstevel@tonic-gate         c->sub.copy.get = e & 15;
4201*7c478bd9Sstevel@tonic-gate         c->len = t->base;
4202*7c478bd9Sstevel@tonic-gate         c->mode = LENEXT;
4203*7c478bd9Sstevel@tonic-gate         break;
4204*7c478bd9Sstevel@tonic-gate       }
4205*7c478bd9Sstevel@tonic-gate       if ((e & 64) == 0)        /* next table */
4206*7c478bd9Sstevel@tonic-gate       {
4207*7c478bd9Sstevel@tonic-gate         c->sub.code.need = e;
4208*7c478bd9Sstevel@tonic-gate         c->sub.code.tree = t->next;
4209*7c478bd9Sstevel@tonic-gate         break;
4210*7c478bd9Sstevel@tonic-gate       }
4211*7c478bd9Sstevel@tonic-gate       if (e & 32)               /* end of block */
4212*7c478bd9Sstevel@tonic-gate       {
4213*7c478bd9Sstevel@tonic-gate         Tracevv((stderr, "inflate:         end of block\n"));
4214*7c478bd9Sstevel@tonic-gate         c->mode = WASH;
4215*7c478bd9Sstevel@tonic-gate         break;
4216*7c478bd9Sstevel@tonic-gate       }
4217*7c478bd9Sstevel@tonic-gate       c->mode = BADCODE;        /* invalid code */
4218*7c478bd9Sstevel@tonic-gate       z->msg = "invalid literal/length code";
4219*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
4220*7c478bd9Sstevel@tonic-gate       LEAVE
4221*7c478bd9Sstevel@tonic-gate     case LENEXT:        /* i: getting length extra (have base) */
4222*7c478bd9Sstevel@tonic-gate       j = c->sub.copy.get;
4223*7c478bd9Sstevel@tonic-gate       NEEDBITS(j)
4224*7c478bd9Sstevel@tonic-gate       c->len += (uInt)b & inflate_mask[j];
4225*7c478bd9Sstevel@tonic-gate       DUMPBITS(j)
4226*7c478bd9Sstevel@tonic-gate       c->sub.code.need = c->dbits;
4227*7c478bd9Sstevel@tonic-gate       c->sub.code.tree = c->dtree;
4228*7c478bd9Sstevel@tonic-gate       Tracevv((stderr, "inflate:         length %u\n", c->len));
4229*7c478bd9Sstevel@tonic-gate       c->mode = DIST;
4230*7c478bd9Sstevel@tonic-gate     case DIST:          /* i: get distance next */
4231*7c478bd9Sstevel@tonic-gate       j = c->sub.code.need;
4232*7c478bd9Sstevel@tonic-gate       NEEDBITS(j)
4233*7c478bd9Sstevel@tonic-gate       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
4234*7c478bd9Sstevel@tonic-gate       DUMPBITS(t->bits)
4235*7c478bd9Sstevel@tonic-gate       e = (uInt)(t->exop);
4236*7c478bd9Sstevel@tonic-gate       if (e & 16)               /* distance */
4237*7c478bd9Sstevel@tonic-gate       {
4238*7c478bd9Sstevel@tonic-gate         c->sub.copy.get = e & 15;
4239*7c478bd9Sstevel@tonic-gate         c->sub.copy.dist = t->base;
4240*7c478bd9Sstevel@tonic-gate         c->mode = DISTEXT;
4241*7c478bd9Sstevel@tonic-gate         break;
4242*7c478bd9Sstevel@tonic-gate       }
4243*7c478bd9Sstevel@tonic-gate       if ((e & 64) == 0)        /* next table */
4244*7c478bd9Sstevel@tonic-gate       {
4245*7c478bd9Sstevel@tonic-gate         c->sub.code.need = e;
4246*7c478bd9Sstevel@tonic-gate         c->sub.code.tree = t->next;
4247*7c478bd9Sstevel@tonic-gate         break;
4248*7c478bd9Sstevel@tonic-gate       }
4249*7c478bd9Sstevel@tonic-gate       c->mode = BADCODE;        /* invalid code */
4250*7c478bd9Sstevel@tonic-gate       z->msg = "invalid distance code";
4251*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
4252*7c478bd9Sstevel@tonic-gate       LEAVE
4253*7c478bd9Sstevel@tonic-gate     case DISTEXT:       /* i: getting distance extra */
4254*7c478bd9Sstevel@tonic-gate       j = c->sub.copy.get;
4255*7c478bd9Sstevel@tonic-gate       NEEDBITS(j)
4256*7c478bd9Sstevel@tonic-gate       c->sub.copy.dist += (uInt)b & inflate_mask[j];
4257*7c478bd9Sstevel@tonic-gate       DUMPBITS(j)
4258*7c478bd9Sstevel@tonic-gate       Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
4259*7c478bd9Sstevel@tonic-gate       c->mode = COPY;
4260*7c478bd9Sstevel@tonic-gate     case COPY:          /* o: copying bytes in window, waiting for space */
4261*7c478bd9Sstevel@tonic-gate #ifndef __TURBOC__ /* Turbo C bug for following expression */
4262*7c478bd9Sstevel@tonic-gate       f = (uInt)(q - s->window) < c->sub.copy.dist ?
4263*7c478bd9Sstevel@tonic-gate           s->end - (c->sub.copy.dist - (q - s->window)) :
4264*7c478bd9Sstevel@tonic-gate           q - c->sub.copy.dist;
4265*7c478bd9Sstevel@tonic-gate #else
4266*7c478bd9Sstevel@tonic-gate       f = q - c->sub.copy.dist;
4267*7c478bd9Sstevel@tonic-gate       if ((uInt)(q - s->window) < c->sub.copy.dist)
4268*7c478bd9Sstevel@tonic-gate         f = s->end - (c->sub.copy.dist - (q - s->window));
4269*7c478bd9Sstevel@tonic-gate #endif
4270*7c478bd9Sstevel@tonic-gate       while (c->len)
4271*7c478bd9Sstevel@tonic-gate       {
4272*7c478bd9Sstevel@tonic-gate         NEEDOUT
4273*7c478bd9Sstevel@tonic-gate         OUTBYTE(*f++)
4274*7c478bd9Sstevel@tonic-gate         if (f == s->end)
4275*7c478bd9Sstevel@tonic-gate           f = s->window;
4276*7c478bd9Sstevel@tonic-gate         c->len--;
4277*7c478bd9Sstevel@tonic-gate       }
4278*7c478bd9Sstevel@tonic-gate       c->mode = START;
4279*7c478bd9Sstevel@tonic-gate       break;
4280*7c478bd9Sstevel@tonic-gate     case LIT:           /* o: got literal, waiting for output space */
4281*7c478bd9Sstevel@tonic-gate       NEEDOUT
4282*7c478bd9Sstevel@tonic-gate       OUTBYTE(c->sub.lit)
4283*7c478bd9Sstevel@tonic-gate       c->mode = START;
4284*7c478bd9Sstevel@tonic-gate       break;
4285*7c478bd9Sstevel@tonic-gate     case WASH:          /* o: got eob, possibly more output */
4286*7c478bd9Sstevel@tonic-gate       FLUSH
4287*7c478bd9Sstevel@tonic-gate       if (s->read != s->write)
4288*7c478bd9Sstevel@tonic-gate         LEAVE
4289*7c478bd9Sstevel@tonic-gate       c->mode = END;
4290*7c478bd9Sstevel@tonic-gate     case END:
4291*7c478bd9Sstevel@tonic-gate       r = Z_STREAM_END;
4292*7c478bd9Sstevel@tonic-gate       LEAVE
4293*7c478bd9Sstevel@tonic-gate     case BADCODE:       /* x: got error */
4294*7c478bd9Sstevel@tonic-gate       r = Z_DATA_ERROR;
4295*7c478bd9Sstevel@tonic-gate       LEAVE
4296*7c478bd9Sstevel@tonic-gate     default:
4297*7c478bd9Sstevel@tonic-gate       r = Z_STREAM_ERROR;
4298*7c478bd9Sstevel@tonic-gate       LEAVE
4299*7c478bd9Sstevel@tonic-gate   }
4300*7c478bd9Sstevel@tonic-gate }
4301*7c478bd9Sstevel@tonic-gate 
4302*7c478bd9Sstevel@tonic-gate 
4303*7c478bd9Sstevel@tonic-gate local void inflate_codes_free(c, z)
4304*7c478bd9Sstevel@tonic-gate inflate_codes_statef *c;
4305*7c478bd9Sstevel@tonic-gate z_stream *z;
4306*7c478bd9Sstevel@tonic-gate {
4307*7c478bd9Sstevel@tonic-gate   ZFREE(z, c, sizeof(struct inflate_codes_state));
4308*7c478bd9Sstevel@tonic-gate   Tracev((stderr, "inflate:       codes free\n"));
4309*7c478bd9Sstevel@tonic-gate }
4310*7c478bd9Sstevel@tonic-gate 
4311*7c478bd9Sstevel@tonic-gate /*+++++*/
4312*7c478bd9Sstevel@tonic-gate /* inflate_util.c -- data and routines common to blocks and codes
4313*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
4314*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
4315*7c478bd9Sstevel@tonic-gate  */
4316*7c478bd9Sstevel@tonic-gate 
4317*7c478bd9Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
4318*7c478bd9Sstevel@tonic-gate local int inflate_flush(s, z, r)
4319*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
4320*7c478bd9Sstevel@tonic-gate z_stream *z;
4321*7c478bd9Sstevel@tonic-gate int r;
4322*7c478bd9Sstevel@tonic-gate {
4323*7c478bd9Sstevel@tonic-gate   uInt n;
4324*7c478bd9Sstevel@tonic-gate   Bytef *p, *q;
4325*7c478bd9Sstevel@tonic-gate 
4326*7c478bd9Sstevel@tonic-gate   /* local copies of source and destination pointers */
4327*7c478bd9Sstevel@tonic-gate   p = z->next_out;
4328*7c478bd9Sstevel@tonic-gate   q = s->read;
4329*7c478bd9Sstevel@tonic-gate 
4330*7c478bd9Sstevel@tonic-gate   /* compute number of bytes to copy as far as end of window */
4331*7c478bd9Sstevel@tonic-gate   n = (uInt)((q <= s->write ? s->write : s->end) - q);
4332*7c478bd9Sstevel@tonic-gate   if (n > z->avail_out) n = z->avail_out;
4333*7c478bd9Sstevel@tonic-gate   if (n && r == Z_BUF_ERROR) r = Z_OK;
4334*7c478bd9Sstevel@tonic-gate 
4335*7c478bd9Sstevel@tonic-gate   /* update counters */
4336*7c478bd9Sstevel@tonic-gate   z->avail_out -= n;
4337*7c478bd9Sstevel@tonic-gate   z->total_out += n;
4338*7c478bd9Sstevel@tonic-gate 
4339*7c478bd9Sstevel@tonic-gate   /* update check information */
4340*7c478bd9Sstevel@tonic-gate   if (s->checkfn != Z_NULL)
4341*7c478bd9Sstevel@tonic-gate     s->check = (*s->checkfn)(s->check, q, n);
4342*7c478bd9Sstevel@tonic-gate 
4343*7c478bd9Sstevel@tonic-gate   /* copy as far as end of window */
4344*7c478bd9Sstevel@tonic-gate   if (p != NULL) {
4345*7c478bd9Sstevel@tonic-gate     zmemcpy(p, q, n);
4346*7c478bd9Sstevel@tonic-gate     p += n;
4347*7c478bd9Sstevel@tonic-gate   }
4348*7c478bd9Sstevel@tonic-gate   q += n;
4349*7c478bd9Sstevel@tonic-gate 
4350*7c478bd9Sstevel@tonic-gate   /* see if more to copy at beginning of window */
4351*7c478bd9Sstevel@tonic-gate   if (q == s->end)
4352*7c478bd9Sstevel@tonic-gate   {
4353*7c478bd9Sstevel@tonic-gate     /* wrap pointers */
4354*7c478bd9Sstevel@tonic-gate     q = s->window;
4355*7c478bd9Sstevel@tonic-gate     if (s->write == s->end)
4356*7c478bd9Sstevel@tonic-gate       s->write = s->window;
4357*7c478bd9Sstevel@tonic-gate 
4358*7c478bd9Sstevel@tonic-gate     /* compute bytes to copy */
4359*7c478bd9Sstevel@tonic-gate     n = (uInt)(s->write - q);
4360*7c478bd9Sstevel@tonic-gate     if (n > z->avail_out) n = z->avail_out;
4361*7c478bd9Sstevel@tonic-gate     if (n && r == Z_BUF_ERROR) r = Z_OK;
4362*7c478bd9Sstevel@tonic-gate 
4363*7c478bd9Sstevel@tonic-gate     /* update counters */
4364*7c478bd9Sstevel@tonic-gate     z->avail_out -= n;
4365*7c478bd9Sstevel@tonic-gate     z->total_out += n;
4366*7c478bd9Sstevel@tonic-gate 
4367*7c478bd9Sstevel@tonic-gate     /* update check information */
4368*7c478bd9Sstevel@tonic-gate     if (s->checkfn != Z_NULL)
4369*7c478bd9Sstevel@tonic-gate       s->check = (*s->checkfn)(s->check, q, n);
4370*7c478bd9Sstevel@tonic-gate 
4371*7c478bd9Sstevel@tonic-gate     /* copy */
4372*7c478bd9Sstevel@tonic-gate     if (p != NULL) {
4373*7c478bd9Sstevel@tonic-gate       zmemcpy(p, q, n);
4374*7c478bd9Sstevel@tonic-gate       p += n;
4375*7c478bd9Sstevel@tonic-gate     }
4376*7c478bd9Sstevel@tonic-gate     q += n;
4377*7c478bd9Sstevel@tonic-gate   }
4378*7c478bd9Sstevel@tonic-gate 
4379*7c478bd9Sstevel@tonic-gate   /* update pointers */
4380*7c478bd9Sstevel@tonic-gate   z->next_out = p;
4381*7c478bd9Sstevel@tonic-gate   s->read = q;
4382*7c478bd9Sstevel@tonic-gate 
4383*7c478bd9Sstevel@tonic-gate   /* done */
4384*7c478bd9Sstevel@tonic-gate   return r;
4385*7c478bd9Sstevel@tonic-gate }
4386*7c478bd9Sstevel@tonic-gate 
4387*7c478bd9Sstevel@tonic-gate 
4388*7c478bd9Sstevel@tonic-gate /*+++++*/
4389*7c478bd9Sstevel@tonic-gate /* inffast.c -- process literals and length/distance pairs fast
4390*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
4391*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
4392*7c478bd9Sstevel@tonic-gate  */
4393*7c478bd9Sstevel@tonic-gate 
4394*7c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
4395*7c478bd9Sstevel@tonic-gate #define base more.Base
4396*7c478bd9Sstevel@tonic-gate #define next more.Next
4397*7c478bd9Sstevel@tonic-gate #define exop word.what.Exop
4398*7c478bd9Sstevel@tonic-gate #define bits word.what.Bits
4399*7c478bd9Sstevel@tonic-gate 
4400*7c478bd9Sstevel@tonic-gate /* macros for bit input with no checking and for returning unused bytes */
4401*7c478bd9Sstevel@tonic-gate #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
4402*7c478bd9Sstevel@tonic-gate #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
4403*7c478bd9Sstevel@tonic-gate 
4404*7c478bd9Sstevel@tonic-gate /* Called with number of bytes left to write in window at least 258
4405*7c478bd9Sstevel@tonic-gate    (the maximum string length) and number of input bytes available
4406*7c478bd9Sstevel@tonic-gate    at least ten.  The ten bytes are six bytes for the longest length/
4407*7c478bd9Sstevel@tonic-gate    distance pair plus four bytes for overloading the bit buffer. */
4408*7c478bd9Sstevel@tonic-gate 
4409*7c478bd9Sstevel@tonic-gate local int inflate_fast(bl, bd, tl, td, s, z)
4410*7c478bd9Sstevel@tonic-gate uInt bl, bd;
4411*7c478bd9Sstevel@tonic-gate inflate_huft *tl, *td;
4412*7c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
4413*7c478bd9Sstevel@tonic-gate z_stream *z;
4414*7c478bd9Sstevel@tonic-gate {
4415*7c478bd9Sstevel@tonic-gate   inflate_huft *t;      /* temporary pointer */
4416*7c478bd9Sstevel@tonic-gate   uInt e;               /* extra bits or operation */
4417*7c478bd9Sstevel@tonic-gate   uLong b;              /* bit buffer */
4418*7c478bd9Sstevel@tonic-gate   uInt k;               /* bits in bit buffer */
4419*7c478bd9Sstevel@tonic-gate   Bytef *p;             /* input data pointer */
4420*7c478bd9Sstevel@tonic-gate   uInt n;               /* bytes available there */
4421*7c478bd9Sstevel@tonic-gate   Bytef *q;             /* output window write pointer */
4422*7c478bd9Sstevel@tonic-gate   uInt m;               /* bytes to end of window or read pointer */
4423*7c478bd9Sstevel@tonic-gate   uInt ml;              /* mask for literal/length tree */
4424*7c478bd9Sstevel@tonic-gate   uInt md;              /* mask for distance tree */
4425*7c478bd9Sstevel@tonic-gate   uInt c;               /* bytes to copy */
4426*7c478bd9Sstevel@tonic-gate   uInt d;               /* distance back to copy from */
4427*7c478bd9Sstevel@tonic-gate   Bytef *r;             /* copy source pointer */
4428*7c478bd9Sstevel@tonic-gate 
4429*7c478bd9Sstevel@tonic-gate   /* load input, output, bit values */
4430*7c478bd9Sstevel@tonic-gate   LOAD
4431*7c478bd9Sstevel@tonic-gate 
4432*7c478bd9Sstevel@tonic-gate   /* initialize masks */
4433*7c478bd9Sstevel@tonic-gate   ml = inflate_mask[bl];
4434*7c478bd9Sstevel@tonic-gate   md = inflate_mask[bd];
4435*7c478bd9Sstevel@tonic-gate 
4436*7c478bd9Sstevel@tonic-gate   /* do until not enough input or output space for fast loop */
4437*7c478bd9Sstevel@tonic-gate   do {                          /* assume called with m >= 258 && n >= 10 */
4438*7c478bd9Sstevel@tonic-gate     /* get literal/length code */
4439*7c478bd9Sstevel@tonic-gate     GRABBITS(20)                /* max bits for literal/length code */
4440*7c478bd9Sstevel@tonic-gate     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
4441*7c478bd9Sstevel@tonic-gate     {
4442*7c478bd9Sstevel@tonic-gate       DUMPBITS(t->bits)
4443*7c478bd9Sstevel@tonic-gate       Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4444*7c478bd9Sstevel@tonic-gate                 "inflate:         * literal '%c'\n" :
4445*7c478bd9Sstevel@tonic-gate                 "inflate:         * literal 0x%02x\n", t->base));
4446*7c478bd9Sstevel@tonic-gate       *q++ = (Byte)t->base;
4447*7c478bd9Sstevel@tonic-gate       m--;
4448*7c478bd9Sstevel@tonic-gate       continue;
4449*7c478bd9Sstevel@tonic-gate     }
4450*7c478bd9Sstevel@tonic-gate     do {
4451*7c478bd9Sstevel@tonic-gate       DUMPBITS(t->bits)
4452*7c478bd9Sstevel@tonic-gate       if (e & 16)
4453*7c478bd9Sstevel@tonic-gate       {
4454*7c478bd9Sstevel@tonic-gate         /* get extra bits for length */
4455*7c478bd9Sstevel@tonic-gate         e &= 15;
4456*7c478bd9Sstevel@tonic-gate         c = t->base + ((uInt)b & inflate_mask[e]);
4457*7c478bd9Sstevel@tonic-gate         DUMPBITS(e)
4458*7c478bd9Sstevel@tonic-gate         Tracevv((stderr, "inflate:         * length %u\n", c));
4459*7c478bd9Sstevel@tonic-gate 
4460*7c478bd9Sstevel@tonic-gate         /* decode distance base of block to copy */
4461*7c478bd9Sstevel@tonic-gate         GRABBITS(15);           /* max bits for distance code */
4462*7c478bd9Sstevel@tonic-gate         e = (t = td + ((uInt)b & md))->exop;
4463*7c478bd9Sstevel@tonic-gate         do {
4464*7c478bd9Sstevel@tonic-gate           DUMPBITS(t->bits)
4465*7c478bd9Sstevel@tonic-gate           if (e & 16)
4466*7c478bd9Sstevel@tonic-gate           {
4467*7c478bd9Sstevel@tonic-gate             /* get extra bits to add to distance base */
4468*7c478bd9Sstevel@tonic-gate             e &= 15;
4469*7c478bd9Sstevel@tonic-gate             GRABBITS(e)         /* get extra bits (up to 13) */
4470*7c478bd9Sstevel@tonic-gate             d = t->base + ((uInt)b & inflate_mask[e]);
4471*7c478bd9Sstevel@tonic-gate             DUMPBITS(e)
4472*7c478bd9Sstevel@tonic-gate             Tracevv((stderr, "inflate:         * distance %u\n", d));
4473*7c478bd9Sstevel@tonic-gate 
4474*7c478bd9Sstevel@tonic-gate             /* do the copy */
4475*7c478bd9Sstevel@tonic-gate             m -= c;
4476*7c478bd9Sstevel@tonic-gate             if ((uInt)(q - s->window) >= d)     /* offset before dest */
4477*7c478bd9Sstevel@tonic-gate             {                                   /*  just copy */
4478*7c478bd9Sstevel@tonic-gate               r = q - d;
4479*7c478bd9Sstevel@tonic-gate               *q++ = *r++;  c--;        /* minimum count is three, */
4480*7c478bd9Sstevel@tonic-gate               *q++ = *r++;  c--;        /*  so unroll loop a little */
4481*7c478bd9Sstevel@tonic-gate             }
4482*7c478bd9Sstevel@tonic-gate             else                        /* else offset after destination */
4483*7c478bd9Sstevel@tonic-gate             {
4484*7c478bd9Sstevel@tonic-gate               e = d - (q - s->window);  /* bytes from offset to end */
4485*7c478bd9Sstevel@tonic-gate               r = s->end - e;           /* pointer to offset */
4486*7c478bd9Sstevel@tonic-gate               if (c > e)                /* if source crosses, */
4487*7c478bd9Sstevel@tonic-gate               {
4488*7c478bd9Sstevel@tonic-gate                 c -= e;                 /* copy to end of window */
4489*7c478bd9Sstevel@tonic-gate                 do {
4490*7c478bd9Sstevel@tonic-gate                   *q++ = *r++;
4491*7c478bd9Sstevel@tonic-gate                 } while (--e);
4492*7c478bd9Sstevel@tonic-gate                 r = s->window;          /* copy rest from start of window */
4493*7c478bd9Sstevel@tonic-gate               }
4494*7c478bd9Sstevel@tonic-gate             }
4495*7c478bd9Sstevel@tonic-gate             do {                        /* copy all or what's left */
4496*7c478bd9Sstevel@tonic-gate               *q++ = *r++;
4497*7c478bd9Sstevel@tonic-gate             } while (--c);
4498*7c478bd9Sstevel@tonic-gate             break;
4499*7c478bd9Sstevel@tonic-gate           }
4500*7c478bd9Sstevel@tonic-gate           else if ((e & 64) == 0)
4501*7c478bd9Sstevel@tonic-gate             e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
4502*7c478bd9Sstevel@tonic-gate           else
4503*7c478bd9Sstevel@tonic-gate           {
4504*7c478bd9Sstevel@tonic-gate             z->msg = "invalid distance code";
4505*7c478bd9Sstevel@tonic-gate             UNGRAB
4506*7c478bd9Sstevel@tonic-gate             UPDATE
4507*7c478bd9Sstevel@tonic-gate             return Z_DATA_ERROR;
4508*7c478bd9Sstevel@tonic-gate           }
4509*7c478bd9Sstevel@tonic-gate         } while (1);
4510*7c478bd9Sstevel@tonic-gate         break;
4511*7c478bd9Sstevel@tonic-gate       }
4512*7c478bd9Sstevel@tonic-gate       if ((e & 64) == 0)
4513*7c478bd9Sstevel@tonic-gate       {
4514*7c478bd9Sstevel@tonic-gate         if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
4515*7c478bd9Sstevel@tonic-gate         {
4516*7c478bd9Sstevel@tonic-gate           DUMPBITS(t->bits)
4517*7c478bd9Sstevel@tonic-gate           Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4518*7c478bd9Sstevel@tonic-gate                     "inflate:         * literal '%c'\n" :
4519*7c478bd9Sstevel@tonic-gate                     "inflate:         * literal 0x%02x\n", t->base));
4520*7c478bd9Sstevel@tonic-gate           *q++ = (Byte)t->base;
4521*7c478bd9Sstevel@tonic-gate           m--;
4522*7c478bd9Sstevel@tonic-gate           break;
4523*7c478bd9Sstevel@tonic-gate         }
4524*7c478bd9Sstevel@tonic-gate       }
4525*7c478bd9Sstevel@tonic-gate       else if (e & 32)
4526*7c478bd9Sstevel@tonic-gate       {
4527*7c478bd9Sstevel@tonic-gate         Tracevv((stderr, "inflate:         * end of block\n"));
4528*7c478bd9Sstevel@tonic-gate         UNGRAB
4529*7c478bd9Sstevel@tonic-gate         UPDATE
4530*7c478bd9Sstevel@tonic-gate         return Z_STREAM_END;
4531*7c478bd9Sstevel@tonic-gate       }
4532*7c478bd9Sstevel@tonic-gate       else
4533*7c478bd9Sstevel@tonic-gate       {
4534*7c478bd9Sstevel@tonic-gate         z->msg = "invalid literal/length code";
4535*7c478bd9Sstevel@tonic-gate         UNGRAB
4536*7c478bd9Sstevel@tonic-gate         UPDATE
4537*7c478bd9Sstevel@tonic-gate         return Z_DATA_ERROR;
4538*7c478bd9Sstevel@tonic-gate       }
4539*7c478bd9Sstevel@tonic-gate     } while (1);
4540*7c478bd9Sstevel@tonic-gate   } while (m >= 258 && n >= 10);
4541*7c478bd9Sstevel@tonic-gate 
4542*7c478bd9Sstevel@tonic-gate   /* not enough input or output--restore pointers and return */
4543*7c478bd9Sstevel@tonic-gate   UNGRAB
4544*7c478bd9Sstevel@tonic-gate   UPDATE
4545*7c478bd9Sstevel@tonic-gate   return Z_OK;
4546*7c478bd9Sstevel@tonic-gate }
4547*7c478bd9Sstevel@tonic-gate 
4548*7c478bd9Sstevel@tonic-gate 
4549*7c478bd9Sstevel@tonic-gate /*+++++*/
4550*7c478bd9Sstevel@tonic-gate /* zutil.c -- target dependent utility functions for the compression library
4551*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Jean-loup Gailly.
4552*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
4553*7c478bd9Sstevel@tonic-gate  */
4554*7c478bd9Sstevel@tonic-gate 
4555*7c478bd9Sstevel@tonic-gate /* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */
4556*7c478bd9Sstevel@tonic-gate 
4557*7c478bd9Sstevel@tonic-gate char *zlib_version = ZLIB_VERSION;
4558*7c478bd9Sstevel@tonic-gate 
4559*7c478bd9Sstevel@tonic-gate char *z_errmsg[] = {
4560*7c478bd9Sstevel@tonic-gate "stream end",          /* Z_STREAM_END    1 */
4561*7c478bd9Sstevel@tonic-gate "",                    /* Z_OK            0 */
4562*7c478bd9Sstevel@tonic-gate "file error",          /* Z_ERRNO        (-1) */
4563*7c478bd9Sstevel@tonic-gate "stream error",        /* Z_STREAM_ERROR (-2) */
4564*7c478bd9Sstevel@tonic-gate "data error",          /* Z_DATA_ERROR   (-3) */
4565*7c478bd9Sstevel@tonic-gate "insufficient memory", /* Z_MEM_ERROR    (-4) */
4566*7c478bd9Sstevel@tonic-gate "buffer error",        /* Z_BUF_ERROR    (-5) */
4567*7c478bd9Sstevel@tonic-gate ""};
4568*7c478bd9Sstevel@tonic-gate 
4569*7c478bd9Sstevel@tonic-gate 
4570*7c478bd9Sstevel@tonic-gate /*+++++*/
4571*7c478bd9Sstevel@tonic-gate /* adler32.c -- compute the Adler-32 checksum of a data stream
4572*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1995 Mark Adler
4573*7c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
4574*7c478bd9Sstevel@tonic-gate  */
4575*7c478bd9Sstevel@tonic-gate 
4576*7c478bd9Sstevel@tonic-gate /* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */
4577*7c478bd9Sstevel@tonic-gate 
4578*7c478bd9Sstevel@tonic-gate #define BASE 65521L /* largest prime smaller than 65536 */
4579*7c478bd9Sstevel@tonic-gate #define NMAX 5552
4580*7c478bd9Sstevel@tonic-gate /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
4581*7c478bd9Sstevel@tonic-gate 
4582*7c478bd9Sstevel@tonic-gate #define DO1(buf)  {s1 += *buf++; s2 += s1;}
4583*7c478bd9Sstevel@tonic-gate #define DO2(buf)  DO1(buf); DO1(buf);
4584*7c478bd9Sstevel@tonic-gate #define DO4(buf)  DO2(buf); DO2(buf);
4585*7c478bd9Sstevel@tonic-gate #define DO8(buf)  DO4(buf); DO4(buf);
4586*7c478bd9Sstevel@tonic-gate #define DO16(buf) DO8(buf); DO8(buf);
4587*7c478bd9Sstevel@tonic-gate 
4588*7c478bd9Sstevel@tonic-gate /* ========================================================================= */
4589*7c478bd9Sstevel@tonic-gate uLong adler32(adler, buf, len)
4590*7c478bd9Sstevel@tonic-gate     uLong adler;
4591*7c478bd9Sstevel@tonic-gate     Bytef *buf;
4592*7c478bd9Sstevel@tonic-gate     uInt len;
4593*7c478bd9Sstevel@tonic-gate {
4594*7c478bd9Sstevel@tonic-gate     unsigned long s1 = adler & 0xffff;
4595*7c478bd9Sstevel@tonic-gate     unsigned long s2 = (adler >> 16) & 0xffff;
4596*7c478bd9Sstevel@tonic-gate     int k;
4597*7c478bd9Sstevel@tonic-gate 
4598*7c478bd9Sstevel@tonic-gate     if (buf == Z_NULL) return 1L;
4599*7c478bd9Sstevel@tonic-gate 
4600*7c478bd9Sstevel@tonic-gate     while (len > 0) {
4601*7c478bd9Sstevel@tonic-gate         k = len < NMAX ? len : NMAX;
4602*7c478bd9Sstevel@tonic-gate         len -= k;
4603*7c478bd9Sstevel@tonic-gate         while (k >= 16) {
4604*7c478bd9Sstevel@tonic-gate             DO16(buf);
4605*7c478bd9Sstevel@tonic-gate             k -= 16;
4606*7c478bd9Sstevel@tonic-gate         }
4607*7c478bd9Sstevel@tonic-gate         if (k != 0) do {
4608*7c478bd9Sstevel@tonic-gate             DO1(buf);
4609*7c478bd9Sstevel@tonic-gate         } while (--k);
4610*7c478bd9Sstevel@tonic-gate         s1 %= BASE;
4611*7c478bd9Sstevel@tonic-gate         s2 %= BASE;
4612*7c478bd9Sstevel@tonic-gate     }
4613*7c478bd9Sstevel@tonic-gate     return (s2 << 16) | s1;
4614*7c478bd9Sstevel@tonic-gate }
4615