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