1*4a5d661aSToomas Soome /* deflate.c -- compress data using the deflation algorithm
2*4a5d661aSToomas Soome * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
3*4a5d661aSToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
4*4a5d661aSToomas Soome */
5*4a5d661aSToomas Soome
6*4a5d661aSToomas Soome /*
7*4a5d661aSToomas Soome * ALGORITHM
8*4a5d661aSToomas Soome *
9*4a5d661aSToomas Soome * The "deflation" process depends on being able to identify portions
10*4a5d661aSToomas Soome * of the input text which are identical to earlier input (within a
11*4a5d661aSToomas Soome * sliding window trailing behind the input currently being processed).
12*4a5d661aSToomas Soome *
13*4a5d661aSToomas Soome * The most straightforward technique turns out to be the fastest for
14*4a5d661aSToomas Soome * most input files: try all possible matches and select the longest.
15*4a5d661aSToomas Soome * The key feature of this algorithm is that insertions into the string
16*4a5d661aSToomas Soome * dictionary are very simple and thus fast, and deletions are avoided
17*4a5d661aSToomas Soome * completely. Insertions are performed at each input character, whereas
18*4a5d661aSToomas Soome * string matches are performed only when the previous match ends. So it
19*4a5d661aSToomas Soome * is preferable to spend more time in matches to allow very fast string
20*4a5d661aSToomas Soome * insertions and avoid deletions. The matching algorithm for small
21*4a5d661aSToomas Soome * strings is inspired from that of Rabin & Karp. A brute force approach
22*4a5d661aSToomas Soome * is used to find longer strings when a small match has been found.
23*4a5d661aSToomas Soome * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24*4a5d661aSToomas Soome * (by Leonid Broukhis).
25*4a5d661aSToomas Soome * A previous version of this file used a more sophisticated algorithm
26*4a5d661aSToomas Soome * (by Fiala and Greene) which is guaranteed to run in linear amortized
27*4a5d661aSToomas Soome * time, but has a larger average cost, uses more memory and is patented.
28*4a5d661aSToomas Soome * However the F&G algorithm may be faster for some highly redundant
29*4a5d661aSToomas Soome * files if the parameter max_chain_length (described below) is too large.
30*4a5d661aSToomas Soome *
31*4a5d661aSToomas Soome * ACKNOWLEDGEMENTS
32*4a5d661aSToomas Soome *
33*4a5d661aSToomas Soome * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34*4a5d661aSToomas Soome * I found it in 'freeze' written by Leonid Broukhis.
35*4a5d661aSToomas Soome * Thanks to many people for bug reports and testing.
36*4a5d661aSToomas Soome *
37*4a5d661aSToomas Soome * REFERENCES
38*4a5d661aSToomas Soome *
39*4a5d661aSToomas Soome * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40*4a5d661aSToomas Soome * Available in http://tools.ietf.org/html/rfc1951
41*4a5d661aSToomas Soome *
42*4a5d661aSToomas Soome * A description of the Rabin and Karp algorithm is given in the book
43*4a5d661aSToomas Soome * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44*4a5d661aSToomas Soome *
45*4a5d661aSToomas Soome * Fiala,E.R., and Greene,D.H.
46*4a5d661aSToomas Soome * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47*4a5d661aSToomas Soome *
48*4a5d661aSToomas Soome */
49*4a5d661aSToomas Soome
50*4a5d661aSToomas Soome /* @(#) $Id$ */
51*4a5d661aSToomas Soome
52*4a5d661aSToomas Soome #include "deflate.h"
53*4a5d661aSToomas Soome
54*4a5d661aSToomas Soome const char deflate_copyright[] =
55*4a5d661aSToomas Soome " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
56*4a5d661aSToomas Soome /*
57*4a5d661aSToomas Soome If you use the zlib library in a product, an acknowledgment is welcome
58*4a5d661aSToomas Soome in the documentation of your product. If for some reason you cannot
59*4a5d661aSToomas Soome include such an acknowledgment, I would appreciate that you keep this
60*4a5d661aSToomas Soome copyright string in the executable of your product.
61*4a5d661aSToomas Soome */
62*4a5d661aSToomas Soome
63*4a5d661aSToomas Soome /* ===========================================================================
64*4a5d661aSToomas Soome * Function prototypes.
65*4a5d661aSToomas Soome */
66*4a5d661aSToomas Soome typedef enum {
67*4a5d661aSToomas Soome need_more, /* block not completed, need more input or more output */
68*4a5d661aSToomas Soome block_done, /* block flush performed */
69*4a5d661aSToomas Soome finish_started, /* finish started, need only more output at next deflate */
70*4a5d661aSToomas Soome finish_done /* finish done, accept no more input or output */
71*4a5d661aSToomas Soome } block_state;
72*4a5d661aSToomas Soome
73*4a5d661aSToomas Soome typedef block_state (*compress_func) OF((deflate_state *s, int flush));
74*4a5d661aSToomas Soome /* Compression function. Returns the block state after the call. */
75*4a5d661aSToomas Soome
76*4a5d661aSToomas Soome local void fill_window OF((deflate_state *s));
77*4a5d661aSToomas Soome local block_state deflate_stored OF((deflate_state *s, int flush));
78*4a5d661aSToomas Soome local block_state deflate_fast OF((deflate_state *s, int flush));
79*4a5d661aSToomas Soome #ifndef FASTEST
80*4a5d661aSToomas Soome local block_state deflate_slow OF((deflate_state *s, int flush));
81*4a5d661aSToomas Soome #endif
82*4a5d661aSToomas Soome local block_state deflate_rle OF((deflate_state *s, int flush));
83*4a5d661aSToomas Soome local block_state deflate_huff OF((deflate_state *s, int flush));
84*4a5d661aSToomas Soome local void lm_init OF((deflate_state *s));
85*4a5d661aSToomas Soome local void putShortMSB OF((deflate_state *s, uInt b));
86*4a5d661aSToomas Soome local void flush_pending OF((z_streamp strm));
87*4a5d661aSToomas Soome local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
88*4a5d661aSToomas Soome #ifdef ASMV
89*4a5d661aSToomas Soome void match_init OF((void)); /* asm code initialization */
90*4a5d661aSToomas Soome uInt longest_match OF((deflate_state *s, IPos cur_match));
91*4a5d661aSToomas Soome #else
92*4a5d661aSToomas Soome local uInt longest_match OF((deflate_state *s, IPos cur_match));
93*4a5d661aSToomas Soome #endif
94*4a5d661aSToomas Soome
95*4a5d661aSToomas Soome #ifdef DEBUG
96*4a5d661aSToomas Soome local void check_match OF((deflate_state *s, IPos start, IPos match,
97*4a5d661aSToomas Soome int length));
98*4a5d661aSToomas Soome #endif
99*4a5d661aSToomas Soome
100*4a5d661aSToomas Soome /* ===========================================================================
101*4a5d661aSToomas Soome * Local data
102*4a5d661aSToomas Soome */
103*4a5d661aSToomas Soome
104*4a5d661aSToomas Soome #define NIL 0
105*4a5d661aSToomas Soome /* Tail of hash chains */
106*4a5d661aSToomas Soome
107*4a5d661aSToomas Soome #ifndef TOO_FAR
108*4a5d661aSToomas Soome # define TOO_FAR 4096
109*4a5d661aSToomas Soome #endif
110*4a5d661aSToomas Soome /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
111*4a5d661aSToomas Soome
112*4a5d661aSToomas Soome /* Values for max_lazy_match, good_match and max_chain_length, depending on
113*4a5d661aSToomas Soome * the desired pack level (0..9). The values given below have been tuned to
114*4a5d661aSToomas Soome * exclude worst case performance for pathological files. Better values may be
115*4a5d661aSToomas Soome * found for specific files.
116*4a5d661aSToomas Soome */
117*4a5d661aSToomas Soome typedef struct config_s {
118*4a5d661aSToomas Soome ush good_length; /* reduce lazy search above this match length */
119*4a5d661aSToomas Soome ush max_lazy; /* do not perform lazy search above this match length */
120*4a5d661aSToomas Soome ush nice_length; /* quit search above this match length */
121*4a5d661aSToomas Soome ush max_chain;
122*4a5d661aSToomas Soome compress_func func;
123*4a5d661aSToomas Soome } config;
124*4a5d661aSToomas Soome
125*4a5d661aSToomas Soome #ifdef FASTEST
126*4a5d661aSToomas Soome local const config configuration_table[2] = {
127*4a5d661aSToomas Soome /* good lazy nice chain */
128*4a5d661aSToomas Soome /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
129*4a5d661aSToomas Soome /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
130*4a5d661aSToomas Soome #else
131*4a5d661aSToomas Soome local const config configuration_table[10] = {
132*4a5d661aSToomas Soome /* good lazy nice chain */
133*4a5d661aSToomas Soome /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
134*4a5d661aSToomas Soome /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
135*4a5d661aSToomas Soome /* 2 */ {4, 5, 16, 8, deflate_fast},
136*4a5d661aSToomas Soome /* 3 */ {4, 6, 32, 32, deflate_fast},
137*4a5d661aSToomas Soome
138*4a5d661aSToomas Soome /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
139*4a5d661aSToomas Soome /* 5 */ {8, 16, 32, 32, deflate_slow},
140*4a5d661aSToomas Soome /* 6 */ {8, 16, 128, 128, deflate_slow},
141*4a5d661aSToomas Soome /* 7 */ {8, 32, 128, 256, deflate_slow},
142*4a5d661aSToomas Soome /* 8 */ {32, 128, 258, 1024, deflate_slow},
143*4a5d661aSToomas Soome /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
144*4a5d661aSToomas Soome #endif
145*4a5d661aSToomas Soome
146*4a5d661aSToomas Soome /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
147*4a5d661aSToomas Soome * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
148*4a5d661aSToomas Soome * meaning.
149*4a5d661aSToomas Soome */
150*4a5d661aSToomas Soome
151*4a5d661aSToomas Soome #define EQUAL 0
152*4a5d661aSToomas Soome /* result of memcmp for equal strings */
153*4a5d661aSToomas Soome
154*4a5d661aSToomas Soome #ifndef NO_DUMMY_DECL
155*4a5d661aSToomas Soome struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
156*4a5d661aSToomas Soome #endif
157*4a5d661aSToomas Soome
158*4a5d661aSToomas Soome /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
159*4a5d661aSToomas Soome #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
160*4a5d661aSToomas Soome
161*4a5d661aSToomas Soome /* ===========================================================================
162*4a5d661aSToomas Soome * Update a hash value with the given input byte
163*4a5d661aSToomas Soome * IN assertion: all calls to to UPDATE_HASH are made with consecutive
164*4a5d661aSToomas Soome * input characters, so that a running hash key can be computed from the
165*4a5d661aSToomas Soome * previous key instead of complete recalculation each time.
166*4a5d661aSToomas Soome */
167*4a5d661aSToomas Soome #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
168*4a5d661aSToomas Soome
169*4a5d661aSToomas Soome
170*4a5d661aSToomas Soome /* ===========================================================================
171*4a5d661aSToomas Soome * Insert string str in the dictionary and set match_head to the previous head
172*4a5d661aSToomas Soome * of the hash chain (the most recent string with same hash key). Return
173*4a5d661aSToomas Soome * the previous length of the hash chain.
174*4a5d661aSToomas Soome * If this file is compiled with -DFASTEST, the compression level is forced
175*4a5d661aSToomas Soome * to 1, and no hash chains are maintained.
176*4a5d661aSToomas Soome * IN assertion: all calls to to INSERT_STRING are made with consecutive
177*4a5d661aSToomas Soome * input characters and the first MIN_MATCH bytes of str are valid
178*4a5d661aSToomas Soome * (except for the last MIN_MATCH-1 bytes of the input file).
179*4a5d661aSToomas Soome */
180*4a5d661aSToomas Soome #ifdef FASTEST
181*4a5d661aSToomas Soome #define INSERT_STRING(s, str, match_head) \
182*4a5d661aSToomas Soome (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
183*4a5d661aSToomas Soome match_head = s->head[s->ins_h], \
184*4a5d661aSToomas Soome s->head[s->ins_h] = (Pos)(str))
185*4a5d661aSToomas Soome #else
186*4a5d661aSToomas Soome #define INSERT_STRING(s, str, match_head) \
187*4a5d661aSToomas Soome (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
188*4a5d661aSToomas Soome match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
189*4a5d661aSToomas Soome s->head[s->ins_h] = (Pos)(str))
190*4a5d661aSToomas Soome #endif
191*4a5d661aSToomas Soome
192*4a5d661aSToomas Soome /* ===========================================================================
193*4a5d661aSToomas Soome * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
194*4a5d661aSToomas Soome * prev[] will be initialized on the fly.
195*4a5d661aSToomas Soome */
196*4a5d661aSToomas Soome #define CLEAR_HASH(s) \
197*4a5d661aSToomas Soome s->head[s->hash_size-1] = NIL; \
198*4a5d661aSToomas Soome zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
199*4a5d661aSToomas Soome
200*4a5d661aSToomas Soome /* ========================================================================= */
deflateInit_(strm,level,version,stream_size)201*4a5d661aSToomas Soome int ZEXPORT deflateInit_(strm, level, version, stream_size)
202*4a5d661aSToomas Soome z_streamp strm;
203*4a5d661aSToomas Soome int level;
204*4a5d661aSToomas Soome const char *version;
205*4a5d661aSToomas Soome int stream_size;
206*4a5d661aSToomas Soome {
207*4a5d661aSToomas Soome return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
208*4a5d661aSToomas Soome Z_DEFAULT_STRATEGY, version, stream_size);
209*4a5d661aSToomas Soome /* To do: ignore strm->next_in if we use it as window */
210*4a5d661aSToomas Soome }
211*4a5d661aSToomas Soome
212*4a5d661aSToomas Soome /* ========================================================================= */
deflateInit2_(strm,level,method,windowBits,memLevel,strategy,version,stream_size)213*4a5d661aSToomas Soome int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
214*4a5d661aSToomas Soome version, stream_size)
215*4a5d661aSToomas Soome z_streamp strm;
216*4a5d661aSToomas Soome int level;
217*4a5d661aSToomas Soome int method;
218*4a5d661aSToomas Soome int windowBits;
219*4a5d661aSToomas Soome int memLevel;
220*4a5d661aSToomas Soome int strategy;
221*4a5d661aSToomas Soome const char *version;
222*4a5d661aSToomas Soome int stream_size;
223*4a5d661aSToomas Soome {
224*4a5d661aSToomas Soome deflate_state *s;
225*4a5d661aSToomas Soome int wrap = 1;
226*4a5d661aSToomas Soome static const char my_version[] = ZLIB_VERSION;
227*4a5d661aSToomas Soome
228*4a5d661aSToomas Soome ushf *overlay;
229*4a5d661aSToomas Soome /* We overlay pending_buf and d_buf+l_buf. This works since the average
230*4a5d661aSToomas Soome * output size for (length,distance) codes is <= 24 bits.
231*4a5d661aSToomas Soome */
232*4a5d661aSToomas Soome
233*4a5d661aSToomas Soome if (version == Z_NULL || version[0] != my_version[0] ||
234*4a5d661aSToomas Soome stream_size != sizeof(z_stream)) {
235*4a5d661aSToomas Soome return Z_VERSION_ERROR;
236*4a5d661aSToomas Soome }
237*4a5d661aSToomas Soome if (strm == Z_NULL) return Z_STREAM_ERROR;
238*4a5d661aSToomas Soome
239*4a5d661aSToomas Soome strm->msg = Z_NULL;
240*4a5d661aSToomas Soome if (strm->zalloc == (alloc_func)0) {
241*4a5d661aSToomas Soome #ifdef Z_SOLO
242*4a5d661aSToomas Soome return Z_STREAM_ERROR;
243*4a5d661aSToomas Soome #else
244*4a5d661aSToomas Soome strm->zalloc = zcalloc;
245*4a5d661aSToomas Soome strm->opaque = (voidpf)0;
246*4a5d661aSToomas Soome #endif
247*4a5d661aSToomas Soome }
248*4a5d661aSToomas Soome if (strm->zfree == (free_func)0)
249*4a5d661aSToomas Soome #ifdef Z_SOLO
250*4a5d661aSToomas Soome return Z_STREAM_ERROR;
251*4a5d661aSToomas Soome #else
252*4a5d661aSToomas Soome strm->zfree = zcfree;
253*4a5d661aSToomas Soome #endif
254*4a5d661aSToomas Soome
255*4a5d661aSToomas Soome #ifdef FASTEST
256*4a5d661aSToomas Soome if (level != 0) level = 1;
257*4a5d661aSToomas Soome #else
258*4a5d661aSToomas Soome if (level == Z_DEFAULT_COMPRESSION) level = 6;
259*4a5d661aSToomas Soome #endif
260*4a5d661aSToomas Soome
261*4a5d661aSToomas Soome if (windowBits < 0) { /* suppress zlib wrapper */
262*4a5d661aSToomas Soome wrap = 0;
263*4a5d661aSToomas Soome windowBits = -windowBits;
264*4a5d661aSToomas Soome }
265*4a5d661aSToomas Soome #ifdef GZIP
266*4a5d661aSToomas Soome else if (windowBits > 15) {
267*4a5d661aSToomas Soome wrap = 2; /* write gzip wrapper instead */
268*4a5d661aSToomas Soome windowBits -= 16;
269*4a5d661aSToomas Soome }
270*4a5d661aSToomas Soome #endif
271*4a5d661aSToomas Soome if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
272*4a5d661aSToomas Soome windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
273*4a5d661aSToomas Soome strategy < 0 || strategy > Z_FIXED) {
274*4a5d661aSToomas Soome return Z_STREAM_ERROR;
275*4a5d661aSToomas Soome }
276*4a5d661aSToomas Soome if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
277*4a5d661aSToomas Soome s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
278*4a5d661aSToomas Soome if (s == Z_NULL) return Z_MEM_ERROR;
279*4a5d661aSToomas Soome strm->state = (struct internal_state FAR *)s;
280*4a5d661aSToomas Soome s->strm = strm;
281*4a5d661aSToomas Soome
282*4a5d661aSToomas Soome s->wrap = wrap;
283*4a5d661aSToomas Soome s->gzhead = Z_NULL;
284*4a5d661aSToomas Soome s->w_bits = windowBits;
285*4a5d661aSToomas Soome s->w_size = 1 << s->w_bits;
286*4a5d661aSToomas Soome s->w_mask = s->w_size - 1;
287*4a5d661aSToomas Soome
288*4a5d661aSToomas Soome s->hash_bits = memLevel + 7;
289*4a5d661aSToomas Soome s->hash_size = 1 << s->hash_bits;
290*4a5d661aSToomas Soome s->hash_mask = s->hash_size - 1;
291*4a5d661aSToomas Soome s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
292*4a5d661aSToomas Soome
293*4a5d661aSToomas Soome s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
294*4a5d661aSToomas Soome s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
295*4a5d661aSToomas Soome s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
296*4a5d661aSToomas Soome
297*4a5d661aSToomas Soome s->high_water = 0; /* nothing written to s->window yet */
298*4a5d661aSToomas Soome
299*4a5d661aSToomas Soome s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
300*4a5d661aSToomas Soome
301*4a5d661aSToomas Soome overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
302*4a5d661aSToomas Soome s->pending_buf = (uchf *) overlay;
303*4a5d661aSToomas Soome s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
304*4a5d661aSToomas Soome
305*4a5d661aSToomas Soome if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
306*4a5d661aSToomas Soome s->pending_buf == Z_NULL) {
307*4a5d661aSToomas Soome s->status = FINISH_STATE;
308*4a5d661aSToomas Soome strm->msg = ERR_MSG(Z_MEM_ERROR);
309*4a5d661aSToomas Soome deflateEnd (strm);
310*4a5d661aSToomas Soome return Z_MEM_ERROR;
311*4a5d661aSToomas Soome }
312*4a5d661aSToomas Soome s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
313*4a5d661aSToomas Soome s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
314*4a5d661aSToomas Soome
315*4a5d661aSToomas Soome s->level = level;
316*4a5d661aSToomas Soome s->strategy = strategy;
317*4a5d661aSToomas Soome s->method = (Byte)method;
318*4a5d661aSToomas Soome
319*4a5d661aSToomas Soome return deflateReset(strm);
320*4a5d661aSToomas Soome }
321*4a5d661aSToomas Soome
322*4a5d661aSToomas Soome /* ========================================================================= */
deflateSetDictionary(strm,dictionary,dictLength)323*4a5d661aSToomas Soome int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
324*4a5d661aSToomas Soome z_streamp strm;
325*4a5d661aSToomas Soome const Bytef *dictionary;
326*4a5d661aSToomas Soome uInt dictLength;
327*4a5d661aSToomas Soome {
328*4a5d661aSToomas Soome deflate_state *s;
329*4a5d661aSToomas Soome uInt str, n;
330*4a5d661aSToomas Soome int wrap;
331*4a5d661aSToomas Soome unsigned avail;
332*4a5d661aSToomas Soome z_const unsigned char *next;
333*4a5d661aSToomas Soome
334*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
335*4a5d661aSToomas Soome return Z_STREAM_ERROR;
336*4a5d661aSToomas Soome s = strm->state;
337*4a5d661aSToomas Soome wrap = s->wrap;
338*4a5d661aSToomas Soome if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
339*4a5d661aSToomas Soome return Z_STREAM_ERROR;
340*4a5d661aSToomas Soome
341*4a5d661aSToomas Soome /* when using zlib wrappers, compute Adler-32 for provided dictionary */
342*4a5d661aSToomas Soome if (wrap == 1)
343*4a5d661aSToomas Soome strm->adler = adler32(strm->adler, dictionary, dictLength);
344*4a5d661aSToomas Soome s->wrap = 0; /* avoid computing Adler-32 in read_buf */
345*4a5d661aSToomas Soome
346*4a5d661aSToomas Soome /* if dictionary would fill window, just replace the history */
347*4a5d661aSToomas Soome if (dictLength >= s->w_size) {
348*4a5d661aSToomas Soome if (wrap == 0) { /* already empty otherwise */
349*4a5d661aSToomas Soome CLEAR_HASH(s);
350*4a5d661aSToomas Soome s->strstart = 0;
351*4a5d661aSToomas Soome s->block_start = 0L;
352*4a5d661aSToomas Soome s->insert = 0;
353*4a5d661aSToomas Soome }
354*4a5d661aSToomas Soome dictionary += dictLength - s->w_size; /* use the tail */
355*4a5d661aSToomas Soome dictLength = s->w_size;
356*4a5d661aSToomas Soome }
357*4a5d661aSToomas Soome
358*4a5d661aSToomas Soome /* insert dictionary into window and hash */
359*4a5d661aSToomas Soome avail = strm->avail_in;
360*4a5d661aSToomas Soome next = strm->next_in;
361*4a5d661aSToomas Soome strm->avail_in = dictLength;
362*4a5d661aSToomas Soome strm->next_in = (z_const Bytef *)dictionary;
363*4a5d661aSToomas Soome fill_window(s);
364*4a5d661aSToomas Soome while (s->lookahead >= MIN_MATCH) {
365*4a5d661aSToomas Soome str = s->strstart;
366*4a5d661aSToomas Soome n = s->lookahead - (MIN_MATCH-1);
367*4a5d661aSToomas Soome do {
368*4a5d661aSToomas Soome UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
369*4a5d661aSToomas Soome #ifndef FASTEST
370*4a5d661aSToomas Soome s->prev[str & s->w_mask] = s->head[s->ins_h];
371*4a5d661aSToomas Soome #endif
372*4a5d661aSToomas Soome s->head[s->ins_h] = (Pos)str;
373*4a5d661aSToomas Soome str++;
374*4a5d661aSToomas Soome } while (--n);
375*4a5d661aSToomas Soome s->strstart = str;
376*4a5d661aSToomas Soome s->lookahead = MIN_MATCH-1;
377*4a5d661aSToomas Soome fill_window(s);
378*4a5d661aSToomas Soome }
379*4a5d661aSToomas Soome s->strstart += s->lookahead;
380*4a5d661aSToomas Soome s->block_start = (long)s->strstart;
381*4a5d661aSToomas Soome s->insert = s->lookahead;
382*4a5d661aSToomas Soome s->lookahead = 0;
383*4a5d661aSToomas Soome s->match_length = s->prev_length = MIN_MATCH-1;
384*4a5d661aSToomas Soome s->match_available = 0;
385*4a5d661aSToomas Soome strm->next_in = next;
386*4a5d661aSToomas Soome strm->avail_in = avail;
387*4a5d661aSToomas Soome s->wrap = wrap;
388*4a5d661aSToomas Soome return Z_OK;
389*4a5d661aSToomas Soome }
390*4a5d661aSToomas Soome
391*4a5d661aSToomas Soome /* ========================================================================= */
deflateResetKeep(strm)392*4a5d661aSToomas Soome int ZEXPORT deflateResetKeep (strm)
393*4a5d661aSToomas Soome z_streamp strm;
394*4a5d661aSToomas Soome {
395*4a5d661aSToomas Soome deflate_state *s;
396*4a5d661aSToomas Soome
397*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL ||
398*4a5d661aSToomas Soome strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
399*4a5d661aSToomas Soome return Z_STREAM_ERROR;
400*4a5d661aSToomas Soome }
401*4a5d661aSToomas Soome
402*4a5d661aSToomas Soome strm->total_in = strm->total_out = 0;
403*4a5d661aSToomas Soome strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
404*4a5d661aSToomas Soome strm->data_type = Z_UNKNOWN;
405*4a5d661aSToomas Soome
406*4a5d661aSToomas Soome s = (deflate_state *)strm->state;
407*4a5d661aSToomas Soome s->pending = 0;
408*4a5d661aSToomas Soome s->pending_out = s->pending_buf;
409*4a5d661aSToomas Soome
410*4a5d661aSToomas Soome if (s->wrap < 0) {
411*4a5d661aSToomas Soome s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
412*4a5d661aSToomas Soome }
413*4a5d661aSToomas Soome s->status = s->wrap ? INIT_STATE : BUSY_STATE;
414*4a5d661aSToomas Soome strm->adler =
415*4a5d661aSToomas Soome #ifdef GZIP
416*4a5d661aSToomas Soome s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
417*4a5d661aSToomas Soome #endif
418*4a5d661aSToomas Soome adler32(0L, Z_NULL, 0);
419*4a5d661aSToomas Soome s->last_flush = Z_NO_FLUSH;
420*4a5d661aSToomas Soome
421*4a5d661aSToomas Soome _tr_init(s);
422*4a5d661aSToomas Soome
423*4a5d661aSToomas Soome return Z_OK;
424*4a5d661aSToomas Soome }
425*4a5d661aSToomas Soome
426*4a5d661aSToomas Soome /* ========================================================================= */
deflateReset(strm)427*4a5d661aSToomas Soome int ZEXPORT deflateReset (strm)
428*4a5d661aSToomas Soome z_streamp strm;
429*4a5d661aSToomas Soome {
430*4a5d661aSToomas Soome int ret;
431*4a5d661aSToomas Soome
432*4a5d661aSToomas Soome ret = deflateResetKeep(strm);
433*4a5d661aSToomas Soome if (ret == Z_OK)
434*4a5d661aSToomas Soome lm_init(strm->state);
435*4a5d661aSToomas Soome return ret;
436*4a5d661aSToomas Soome }
437*4a5d661aSToomas Soome
438*4a5d661aSToomas Soome /* ========================================================================= */
deflateSetHeader(strm,head)439*4a5d661aSToomas Soome int ZEXPORT deflateSetHeader (strm, head)
440*4a5d661aSToomas Soome z_streamp strm;
441*4a5d661aSToomas Soome gz_headerp head;
442*4a5d661aSToomas Soome {
443*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
444*4a5d661aSToomas Soome if (strm->state->wrap != 2) return Z_STREAM_ERROR;
445*4a5d661aSToomas Soome strm->state->gzhead = head;
446*4a5d661aSToomas Soome return Z_OK;
447*4a5d661aSToomas Soome }
448*4a5d661aSToomas Soome
449*4a5d661aSToomas Soome /* ========================================================================= */
deflatePending(strm,pending,bits)450*4a5d661aSToomas Soome int ZEXPORT deflatePending (strm, pending, bits)
451*4a5d661aSToomas Soome unsigned *pending;
452*4a5d661aSToomas Soome int *bits;
453*4a5d661aSToomas Soome z_streamp strm;
454*4a5d661aSToomas Soome {
455*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
456*4a5d661aSToomas Soome if (pending != Z_NULL)
457*4a5d661aSToomas Soome *pending = strm->state->pending;
458*4a5d661aSToomas Soome if (bits != Z_NULL)
459*4a5d661aSToomas Soome *bits = strm->state->bi_valid;
460*4a5d661aSToomas Soome return Z_OK;
461*4a5d661aSToomas Soome }
462*4a5d661aSToomas Soome
463*4a5d661aSToomas Soome /* ========================================================================= */
deflatePrime(strm,bits,value)464*4a5d661aSToomas Soome int ZEXPORT deflatePrime (strm, bits, value)
465*4a5d661aSToomas Soome z_streamp strm;
466*4a5d661aSToomas Soome int bits;
467*4a5d661aSToomas Soome int value;
468*4a5d661aSToomas Soome {
469*4a5d661aSToomas Soome deflate_state *s;
470*4a5d661aSToomas Soome int put;
471*4a5d661aSToomas Soome
472*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
473*4a5d661aSToomas Soome s = strm->state;
474*4a5d661aSToomas Soome if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
475*4a5d661aSToomas Soome return Z_BUF_ERROR;
476*4a5d661aSToomas Soome do {
477*4a5d661aSToomas Soome put = Buf_size - s->bi_valid;
478*4a5d661aSToomas Soome if (put > bits)
479*4a5d661aSToomas Soome put = bits;
480*4a5d661aSToomas Soome s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
481*4a5d661aSToomas Soome s->bi_valid += put;
482*4a5d661aSToomas Soome _tr_flush_bits(s);
483*4a5d661aSToomas Soome value >>= put;
484*4a5d661aSToomas Soome bits -= put;
485*4a5d661aSToomas Soome } while (bits);
486*4a5d661aSToomas Soome return Z_OK;
487*4a5d661aSToomas Soome }
488*4a5d661aSToomas Soome
489*4a5d661aSToomas Soome /* ========================================================================= */
deflateParams(strm,level,strategy)490*4a5d661aSToomas Soome int ZEXPORT deflateParams(strm, level, strategy)
491*4a5d661aSToomas Soome z_streamp strm;
492*4a5d661aSToomas Soome int level;
493*4a5d661aSToomas Soome int strategy;
494*4a5d661aSToomas Soome {
495*4a5d661aSToomas Soome deflate_state *s;
496*4a5d661aSToomas Soome compress_func func;
497*4a5d661aSToomas Soome int err = Z_OK;
498*4a5d661aSToomas Soome
499*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
500*4a5d661aSToomas Soome s = strm->state;
501*4a5d661aSToomas Soome
502*4a5d661aSToomas Soome #ifdef FASTEST
503*4a5d661aSToomas Soome if (level != 0) level = 1;
504*4a5d661aSToomas Soome #else
505*4a5d661aSToomas Soome if (level == Z_DEFAULT_COMPRESSION) level = 6;
506*4a5d661aSToomas Soome #endif
507*4a5d661aSToomas Soome if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
508*4a5d661aSToomas Soome return Z_STREAM_ERROR;
509*4a5d661aSToomas Soome }
510*4a5d661aSToomas Soome func = configuration_table[s->level].func;
511*4a5d661aSToomas Soome
512*4a5d661aSToomas Soome if ((strategy != s->strategy || func != configuration_table[level].func) &&
513*4a5d661aSToomas Soome strm->total_in != 0) {
514*4a5d661aSToomas Soome /* Flush the last buffer: */
515*4a5d661aSToomas Soome err = deflate(strm, Z_BLOCK);
516*4a5d661aSToomas Soome if (err == Z_BUF_ERROR && s->pending == 0)
517*4a5d661aSToomas Soome err = Z_OK;
518*4a5d661aSToomas Soome }
519*4a5d661aSToomas Soome if (s->level != level) {
520*4a5d661aSToomas Soome s->level = level;
521*4a5d661aSToomas Soome s->max_lazy_match = configuration_table[level].max_lazy;
522*4a5d661aSToomas Soome s->good_match = configuration_table[level].good_length;
523*4a5d661aSToomas Soome s->nice_match = configuration_table[level].nice_length;
524*4a5d661aSToomas Soome s->max_chain_length = configuration_table[level].max_chain;
525*4a5d661aSToomas Soome }
526*4a5d661aSToomas Soome s->strategy = strategy;
527*4a5d661aSToomas Soome return err;
528*4a5d661aSToomas Soome }
529*4a5d661aSToomas Soome
530*4a5d661aSToomas Soome /* ========================================================================= */
deflateTune(strm,good_length,max_lazy,nice_length,max_chain)531*4a5d661aSToomas Soome int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
532*4a5d661aSToomas Soome z_streamp strm;
533*4a5d661aSToomas Soome int good_length;
534*4a5d661aSToomas Soome int max_lazy;
535*4a5d661aSToomas Soome int nice_length;
536*4a5d661aSToomas Soome int max_chain;
537*4a5d661aSToomas Soome {
538*4a5d661aSToomas Soome deflate_state *s;
539*4a5d661aSToomas Soome
540*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
541*4a5d661aSToomas Soome s = strm->state;
542*4a5d661aSToomas Soome s->good_match = good_length;
543*4a5d661aSToomas Soome s->max_lazy_match = max_lazy;
544*4a5d661aSToomas Soome s->nice_match = nice_length;
545*4a5d661aSToomas Soome s->max_chain_length = max_chain;
546*4a5d661aSToomas Soome return Z_OK;
547*4a5d661aSToomas Soome }
548*4a5d661aSToomas Soome
549*4a5d661aSToomas Soome /* =========================================================================
550*4a5d661aSToomas Soome * For the default windowBits of 15 and memLevel of 8, this function returns
551*4a5d661aSToomas Soome * a close to exact, as well as small, upper bound on the compressed size.
552*4a5d661aSToomas Soome * They are coded as constants here for a reason--if the #define's are
553*4a5d661aSToomas Soome * changed, then this function needs to be changed as well. The return
554*4a5d661aSToomas Soome * value for 15 and 8 only works for those exact settings.
555*4a5d661aSToomas Soome *
556*4a5d661aSToomas Soome * For any setting other than those defaults for windowBits and memLevel,
557*4a5d661aSToomas Soome * the value returned is a conservative worst case for the maximum expansion
558*4a5d661aSToomas Soome * resulting from using fixed blocks instead of stored blocks, which deflate
559*4a5d661aSToomas Soome * can emit on compressed data for some combinations of the parameters.
560*4a5d661aSToomas Soome *
561*4a5d661aSToomas Soome * This function could be more sophisticated to provide closer upper bounds for
562*4a5d661aSToomas Soome * every combination of windowBits and memLevel. But even the conservative
563*4a5d661aSToomas Soome * upper bound of about 14% expansion does not seem onerous for output buffer
564*4a5d661aSToomas Soome * allocation.
565*4a5d661aSToomas Soome */
deflateBound(strm,sourceLen)566*4a5d661aSToomas Soome uLong ZEXPORT deflateBound(strm, sourceLen)
567*4a5d661aSToomas Soome z_streamp strm;
568*4a5d661aSToomas Soome uLong sourceLen;
569*4a5d661aSToomas Soome {
570*4a5d661aSToomas Soome deflate_state *s;
571*4a5d661aSToomas Soome uLong complen, wraplen;
572*4a5d661aSToomas Soome Bytef *str;
573*4a5d661aSToomas Soome
574*4a5d661aSToomas Soome /* conservative upper bound for compressed data */
575*4a5d661aSToomas Soome complen = sourceLen +
576*4a5d661aSToomas Soome ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
577*4a5d661aSToomas Soome
578*4a5d661aSToomas Soome /* if can't get parameters, return conservative bound plus zlib wrapper */
579*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL)
580*4a5d661aSToomas Soome return complen + 6;
581*4a5d661aSToomas Soome
582*4a5d661aSToomas Soome /* compute wrapper length */
583*4a5d661aSToomas Soome s = strm->state;
584*4a5d661aSToomas Soome switch (s->wrap) {
585*4a5d661aSToomas Soome case 0: /* raw deflate */
586*4a5d661aSToomas Soome wraplen = 0;
587*4a5d661aSToomas Soome break;
588*4a5d661aSToomas Soome case 1: /* zlib wrapper */
589*4a5d661aSToomas Soome wraplen = 6 + (s->strstart ? 4 : 0);
590*4a5d661aSToomas Soome break;
591*4a5d661aSToomas Soome case 2: /* gzip wrapper */
592*4a5d661aSToomas Soome wraplen = 18;
593*4a5d661aSToomas Soome if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
594*4a5d661aSToomas Soome if (s->gzhead->extra != Z_NULL)
595*4a5d661aSToomas Soome wraplen += 2 + s->gzhead->extra_len;
596*4a5d661aSToomas Soome str = s->gzhead->name;
597*4a5d661aSToomas Soome if (str != Z_NULL)
598*4a5d661aSToomas Soome do {
599*4a5d661aSToomas Soome wraplen++;
600*4a5d661aSToomas Soome } while (*str++);
601*4a5d661aSToomas Soome str = s->gzhead->comment;
602*4a5d661aSToomas Soome if (str != Z_NULL)
603*4a5d661aSToomas Soome do {
604*4a5d661aSToomas Soome wraplen++;
605*4a5d661aSToomas Soome } while (*str++);
606*4a5d661aSToomas Soome if (s->gzhead->hcrc)
607*4a5d661aSToomas Soome wraplen += 2;
608*4a5d661aSToomas Soome }
609*4a5d661aSToomas Soome break;
610*4a5d661aSToomas Soome default: /* for compiler happiness */
611*4a5d661aSToomas Soome wraplen = 6;
612*4a5d661aSToomas Soome }
613*4a5d661aSToomas Soome
614*4a5d661aSToomas Soome /* if not default parameters, return conservative bound */
615*4a5d661aSToomas Soome if (s->w_bits != 15 || s->hash_bits != 8 + 7)
616*4a5d661aSToomas Soome return complen + wraplen;
617*4a5d661aSToomas Soome
618*4a5d661aSToomas Soome /* default settings: return tight bound for that case */
619*4a5d661aSToomas Soome return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
620*4a5d661aSToomas Soome (sourceLen >> 25) + 13 - 6 + wraplen;
621*4a5d661aSToomas Soome }
622*4a5d661aSToomas Soome
623*4a5d661aSToomas Soome /* =========================================================================
624*4a5d661aSToomas Soome * Put a short in the pending buffer. The 16-bit value is put in MSB order.
625*4a5d661aSToomas Soome * IN assertion: the stream state is correct and there is enough room in
626*4a5d661aSToomas Soome * pending_buf.
627*4a5d661aSToomas Soome */
putShortMSB(s,b)628*4a5d661aSToomas Soome local void putShortMSB (s, b)
629*4a5d661aSToomas Soome deflate_state *s;
630*4a5d661aSToomas Soome uInt b;
631*4a5d661aSToomas Soome {
632*4a5d661aSToomas Soome put_byte(s, (Byte)(b >> 8));
633*4a5d661aSToomas Soome put_byte(s, (Byte)(b & 0xff));
634*4a5d661aSToomas Soome }
635*4a5d661aSToomas Soome
636*4a5d661aSToomas Soome /* =========================================================================
637*4a5d661aSToomas Soome * Flush as much pending output as possible. All deflate() output goes
638*4a5d661aSToomas Soome * through this function so some applications may wish to modify it
639*4a5d661aSToomas Soome * to avoid allocating a large strm->next_out buffer and copying into it.
640*4a5d661aSToomas Soome * (See also read_buf()).
641*4a5d661aSToomas Soome */
flush_pending(strm)642*4a5d661aSToomas Soome local void flush_pending(strm)
643*4a5d661aSToomas Soome z_streamp strm;
644*4a5d661aSToomas Soome {
645*4a5d661aSToomas Soome unsigned len;
646*4a5d661aSToomas Soome deflate_state *s = strm->state;
647*4a5d661aSToomas Soome
648*4a5d661aSToomas Soome _tr_flush_bits(s);
649*4a5d661aSToomas Soome len = s->pending;
650*4a5d661aSToomas Soome if (len > strm->avail_out) len = strm->avail_out;
651*4a5d661aSToomas Soome if (len == 0) return;
652*4a5d661aSToomas Soome
653*4a5d661aSToomas Soome zmemcpy(strm->next_out, s->pending_out, len);
654*4a5d661aSToomas Soome strm->next_out += len;
655*4a5d661aSToomas Soome s->pending_out += len;
656*4a5d661aSToomas Soome strm->total_out += len;
657*4a5d661aSToomas Soome strm->avail_out -= len;
658*4a5d661aSToomas Soome s->pending -= len;
659*4a5d661aSToomas Soome if (s->pending == 0) {
660*4a5d661aSToomas Soome s->pending_out = s->pending_buf;
661*4a5d661aSToomas Soome }
662*4a5d661aSToomas Soome }
663*4a5d661aSToomas Soome
664*4a5d661aSToomas Soome /* ========================================================================= */
deflate(strm,flush)665*4a5d661aSToomas Soome int ZEXPORT deflate (strm, flush)
666*4a5d661aSToomas Soome z_streamp strm;
667*4a5d661aSToomas Soome int flush;
668*4a5d661aSToomas Soome {
669*4a5d661aSToomas Soome int old_flush; /* value of flush param for previous deflate call */
670*4a5d661aSToomas Soome deflate_state *s;
671*4a5d661aSToomas Soome
672*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL ||
673*4a5d661aSToomas Soome flush > Z_BLOCK || flush < 0) {
674*4a5d661aSToomas Soome return Z_STREAM_ERROR;
675*4a5d661aSToomas Soome }
676*4a5d661aSToomas Soome s = strm->state;
677*4a5d661aSToomas Soome
678*4a5d661aSToomas Soome if (strm->next_out == Z_NULL ||
679*4a5d661aSToomas Soome (strm->next_in == Z_NULL && strm->avail_in != 0) ||
680*4a5d661aSToomas Soome (s->status == FINISH_STATE && flush != Z_FINISH)) {
681*4a5d661aSToomas Soome ERR_RETURN(strm, Z_STREAM_ERROR);
682*4a5d661aSToomas Soome }
683*4a5d661aSToomas Soome if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
684*4a5d661aSToomas Soome
685*4a5d661aSToomas Soome s->strm = strm; /* just in case */
686*4a5d661aSToomas Soome old_flush = s->last_flush;
687*4a5d661aSToomas Soome s->last_flush = flush;
688*4a5d661aSToomas Soome
689*4a5d661aSToomas Soome /* Write the header */
690*4a5d661aSToomas Soome if (s->status == INIT_STATE) {
691*4a5d661aSToomas Soome #ifdef GZIP
692*4a5d661aSToomas Soome if (s->wrap == 2) {
693*4a5d661aSToomas Soome strm->adler = crc32(0L, Z_NULL, 0);
694*4a5d661aSToomas Soome put_byte(s, 31);
695*4a5d661aSToomas Soome put_byte(s, 139);
696*4a5d661aSToomas Soome put_byte(s, 8);
697*4a5d661aSToomas Soome if (s->gzhead == Z_NULL) {
698*4a5d661aSToomas Soome put_byte(s, 0);
699*4a5d661aSToomas Soome put_byte(s, 0);
700*4a5d661aSToomas Soome put_byte(s, 0);
701*4a5d661aSToomas Soome put_byte(s, 0);
702*4a5d661aSToomas Soome put_byte(s, 0);
703*4a5d661aSToomas Soome put_byte(s, s->level == 9 ? 2 :
704*4a5d661aSToomas Soome (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
705*4a5d661aSToomas Soome 4 : 0));
706*4a5d661aSToomas Soome put_byte(s, OS_CODE);
707*4a5d661aSToomas Soome s->status = BUSY_STATE;
708*4a5d661aSToomas Soome }
709*4a5d661aSToomas Soome else {
710*4a5d661aSToomas Soome put_byte(s, (s->gzhead->text ? 1 : 0) +
711*4a5d661aSToomas Soome (s->gzhead->hcrc ? 2 : 0) +
712*4a5d661aSToomas Soome (s->gzhead->extra == Z_NULL ? 0 : 4) +
713*4a5d661aSToomas Soome (s->gzhead->name == Z_NULL ? 0 : 8) +
714*4a5d661aSToomas Soome (s->gzhead->comment == Z_NULL ? 0 : 16)
715*4a5d661aSToomas Soome );
716*4a5d661aSToomas Soome put_byte(s, (Byte)(s->gzhead->time & 0xff));
717*4a5d661aSToomas Soome put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
718*4a5d661aSToomas Soome put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
719*4a5d661aSToomas Soome put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
720*4a5d661aSToomas Soome put_byte(s, s->level == 9 ? 2 :
721*4a5d661aSToomas Soome (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
722*4a5d661aSToomas Soome 4 : 0));
723*4a5d661aSToomas Soome put_byte(s, s->gzhead->os & 0xff);
724*4a5d661aSToomas Soome if (s->gzhead->extra != Z_NULL) {
725*4a5d661aSToomas Soome put_byte(s, s->gzhead->extra_len & 0xff);
726*4a5d661aSToomas Soome put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
727*4a5d661aSToomas Soome }
728*4a5d661aSToomas Soome if (s->gzhead->hcrc)
729*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf,
730*4a5d661aSToomas Soome s->pending);
731*4a5d661aSToomas Soome s->gzindex = 0;
732*4a5d661aSToomas Soome s->status = EXTRA_STATE;
733*4a5d661aSToomas Soome }
734*4a5d661aSToomas Soome }
735*4a5d661aSToomas Soome else
736*4a5d661aSToomas Soome #endif
737*4a5d661aSToomas Soome {
738*4a5d661aSToomas Soome uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
739*4a5d661aSToomas Soome uInt level_flags;
740*4a5d661aSToomas Soome
741*4a5d661aSToomas Soome if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
742*4a5d661aSToomas Soome level_flags = 0;
743*4a5d661aSToomas Soome else if (s->level < 6)
744*4a5d661aSToomas Soome level_flags = 1;
745*4a5d661aSToomas Soome else if (s->level == 6)
746*4a5d661aSToomas Soome level_flags = 2;
747*4a5d661aSToomas Soome else
748*4a5d661aSToomas Soome level_flags = 3;
749*4a5d661aSToomas Soome header |= (level_flags << 6);
750*4a5d661aSToomas Soome if (s->strstart != 0) header |= PRESET_DICT;
751*4a5d661aSToomas Soome header += 31 - (header % 31);
752*4a5d661aSToomas Soome
753*4a5d661aSToomas Soome s->status = BUSY_STATE;
754*4a5d661aSToomas Soome putShortMSB(s, header);
755*4a5d661aSToomas Soome
756*4a5d661aSToomas Soome /* Save the adler32 of the preset dictionary: */
757*4a5d661aSToomas Soome if (s->strstart != 0) {
758*4a5d661aSToomas Soome putShortMSB(s, (uInt)(strm->adler >> 16));
759*4a5d661aSToomas Soome putShortMSB(s, (uInt)(strm->adler & 0xffff));
760*4a5d661aSToomas Soome }
761*4a5d661aSToomas Soome strm->adler = adler32(0L, Z_NULL, 0);
762*4a5d661aSToomas Soome }
763*4a5d661aSToomas Soome }
764*4a5d661aSToomas Soome #ifdef GZIP
765*4a5d661aSToomas Soome if (s->status == EXTRA_STATE) {
766*4a5d661aSToomas Soome if (s->gzhead->extra != Z_NULL) {
767*4a5d661aSToomas Soome uInt beg = s->pending; /* start of bytes to update crc */
768*4a5d661aSToomas Soome
769*4a5d661aSToomas Soome while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
770*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size) {
771*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
772*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
773*4a5d661aSToomas Soome s->pending - beg);
774*4a5d661aSToomas Soome flush_pending(strm);
775*4a5d661aSToomas Soome beg = s->pending;
776*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size)
777*4a5d661aSToomas Soome break;
778*4a5d661aSToomas Soome }
779*4a5d661aSToomas Soome put_byte(s, s->gzhead->extra[s->gzindex]);
780*4a5d661aSToomas Soome s->gzindex++;
781*4a5d661aSToomas Soome }
782*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
783*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
784*4a5d661aSToomas Soome s->pending - beg);
785*4a5d661aSToomas Soome if (s->gzindex == s->gzhead->extra_len) {
786*4a5d661aSToomas Soome s->gzindex = 0;
787*4a5d661aSToomas Soome s->status = NAME_STATE;
788*4a5d661aSToomas Soome }
789*4a5d661aSToomas Soome }
790*4a5d661aSToomas Soome else
791*4a5d661aSToomas Soome s->status = NAME_STATE;
792*4a5d661aSToomas Soome }
793*4a5d661aSToomas Soome if (s->status == NAME_STATE) {
794*4a5d661aSToomas Soome if (s->gzhead->name != Z_NULL) {
795*4a5d661aSToomas Soome uInt beg = s->pending; /* start of bytes to update crc */
796*4a5d661aSToomas Soome int val;
797*4a5d661aSToomas Soome
798*4a5d661aSToomas Soome do {
799*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size) {
800*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
801*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
802*4a5d661aSToomas Soome s->pending - beg);
803*4a5d661aSToomas Soome flush_pending(strm);
804*4a5d661aSToomas Soome beg = s->pending;
805*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size) {
806*4a5d661aSToomas Soome val = 1;
807*4a5d661aSToomas Soome break;
808*4a5d661aSToomas Soome }
809*4a5d661aSToomas Soome }
810*4a5d661aSToomas Soome val = s->gzhead->name[s->gzindex++];
811*4a5d661aSToomas Soome put_byte(s, val);
812*4a5d661aSToomas Soome } while (val != 0);
813*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
814*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
815*4a5d661aSToomas Soome s->pending - beg);
816*4a5d661aSToomas Soome if (val == 0) {
817*4a5d661aSToomas Soome s->gzindex = 0;
818*4a5d661aSToomas Soome s->status = COMMENT_STATE;
819*4a5d661aSToomas Soome }
820*4a5d661aSToomas Soome }
821*4a5d661aSToomas Soome else
822*4a5d661aSToomas Soome s->status = COMMENT_STATE;
823*4a5d661aSToomas Soome }
824*4a5d661aSToomas Soome if (s->status == COMMENT_STATE) {
825*4a5d661aSToomas Soome if (s->gzhead->comment != Z_NULL) {
826*4a5d661aSToomas Soome uInt beg = s->pending; /* start of bytes to update crc */
827*4a5d661aSToomas Soome int val;
828*4a5d661aSToomas Soome
829*4a5d661aSToomas Soome do {
830*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size) {
831*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
832*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
833*4a5d661aSToomas Soome s->pending - beg);
834*4a5d661aSToomas Soome flush_pending(strm);
835*4a5d661aSToomas Soome beg = s->pending;
836*4a5d661aSToomas Soome if (s->pending == s->pending_buf_size) {
837*4a5d661aSToomas Soome val = 1;
838*4a5d661aSToomas Soome break;
839*4a5d661aSToomas Soome }
840*4a5d661aSToomas Soome }
841*4a5d661aSToomas Soome val = s->gzhead->comment[s->gzindex++];
842*4a5d661aSToomas Soome put_byte(s, val);
843*4a5d661aSToomas Soome } while (val != 0);
844*4a5d661aSToomas Soome if (s->gzhead->hcrc && s->pending > beg)
845*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, s->pending_buf + beg,
846*4a5d661aSToomas Soome s->pending - beg);
847*4a5d661aSToomas Soome if (val == 0)
848*4a5d661aSToomas Soome s->status = HCRC_STATE;
849*4a5d661aSToomas Soome }
850*4a5d661aSToomas Soome else
851*4a5d661aSToomas Soome s->status = HCRC_STATE;
852*4a5d661aSToomas Soome }
853*4a5d661aSToomas Soome if (s->status == HCRC_STATE) {
854*4a5d661aSToomas Soome if (s->gzhead->hcrc) {
855*4a5d661aSToomas Soome if (s->pending + 2 > s->pending_buf_size)
856*4a5d661aSToomas Soome flush_pending(strm);
857*4a5d661aSToomas Soome if (s->pending + 2 <= s->pending_buf_size) {
858*4a5d661aSToomas Soome put_byte(s, (Byte)(strm->adler & 0xff));
859*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
860*4a5d661aSToomas Soome strm->adler = crc32(0L, Z_NULL, 0);
861*4a5d661aSToomas Soome s->status = BUSY_STATE;
862*4a5d661aSToomas Soome }
863*4a5d661aSToomas Soome }
864*4a5d661aSToomas Soome else
865*4a5d661aSToomas Soome s->status = BUSY_STATE;
866*4a5d661aSToomas Soome }
867*4a5d661aSToomas Soome #endif
868*4a5d661aSToomas Soome
869*4a5d661aSToomas Soome /* Flush as much pending output as possible */
870*4a5d661aSToomas Soome if (s->pending != 0) {
871*4a5d661aSToomas Soome flush_pending(strm);
872*4a5d661aSToomas Soome if (strm->avail_out == 0) {
873*4a5d661aSToomas Soome /* Since avail_out is 0, deflate will be called again with
874*4a5d661aSToomas Soome * more output space, but possibly with both pending and
875*4a5d661aSToomas Soome * avail_in equal to zero. There won't be anything to do,
876*4a5d661aSToomas Soome * but this is not an error situation so make sure we
877*4a5d661aSToomas Soome * return OK instead of BUF_ERROR at next call of deflate:
878*4a5d661aSToomas Soome */
879*4a5d661aSToomas Soome s->last_flush = -1;
880*4a5d661aSToomas Soome return Z_OK;
881*4a5d661aSToomas Soome }
882*4a5d661aSToomas Soome
883*4a5d661aSToomas Soome /* Make sure there is something to do and avoid duplicate consecutive
884*4a5d661aSToomas Soome * flushes. For repeated and useless calls with Z_FINISH, we keep
885*4a5d661aSToomas Soome * returning Z_STREAM_END instead of Z_BUF_ERROR.
886*4a5d661aSToomas Soome */
887*4a5d661aSToomas Soome } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
888*4a5d661aSToomas Soome flush != Z_FINISH) {
889*4a5d661aSToomas Soome ERR_RETURN(strm, Z_BUF_ERROR);
890*4a5d661aSToomas Soome }
891*4a5d661aSToomas Soome
892*4a5d661aSToomas Soome /* User must not provide more input after the first FINISH: */
893*4a5d661aSToomas Soome if (s->status == FINISH_STATE && strm->avail_in != 0) {
894*4a5d661aSToomas Soome ERR_RETURN(strm, Z_BUF_ERROR);
895*4a5d661aSToomas Soome }
896*4a5d661aSToomas Soome
897*4a5d661aSToomas Soome /* Start a new block or continue the current one.
898*4a5d661aSToomas Soome */
899*4a5d661aSToomas Soome if (strm->avail_in != 0 || s->lookahead != 0 ||
900*4a5d661aSToomas Soome (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
901*4a5d661aSToomas Soome block_state bstate;
902*4a5d661aSToomas Soome
903*4a5d661aSToomas Soome bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
904*4a5d661aSToomas Soome (s->strategy == Z_RLE ? deflate_rle(s, flush) :
905*4a5d661aSToomas Soome (*(configuration_table[s->level].func))(s, flush));
906*4a5d661aSToomas Soome
907*4a5d661aSToomas Soome if (bstate == finish_started || bstate == finish_done) {
908*4a5d661aSToomas Soome s->status = FINISH_STATE;
909*4a5d661aSToomas Soome }
910*4a5d661aSToomas Soome if (bstate == need_more || bstate == finish_started) {
911*4a5d661aSToomas Soome if (strm->avail_out == 0) {
912*4a5d661aSToomas Soome s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
913*4a5d661aSToomas Soome }
914*4a5d661aSToomas Soome return Z_OK;
915*4a5d661aSToomas Soome /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
916*4a5d661aSToomas Soome * of deflate should use the same flush parameter to make sure
917*4a5d661aSToomas Soome * that the flush is complete. So we don't have to output an
918*4a5d661aSToomas Soome * empty block here, this will be done at next call. This also
919*4a5d661aSToomas Soome * ensures that for a very small output buffer, we emit at most
920*4a5d661aSToomas Soome * one empty block.
921*4a5d661aSToomas Soome */
922*4a5d661aSToomas Soome }
923*4a5d661aSToomas Soome if (bstate == block_done) {
924*4a5d661aSToomas Soome if (flush == Z_PARTIAL_FLUSH) {
925*4a5d661aSToomas Soome _tr_align(s);
926*4a5d661aSToomas Soome } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
927*4a5d661aSToomas Soome _tr_stored_block(s, (char*)0, 0L, 0);
928*4a5d661aSToomas Soome /* For a full flush, this empty block will be recognized
929*4a5d661aSToomas Soome * as a special marker by inflate_sync().
930*4a5d661aSToomas Soome */
931*4a5d661aSToomas Soome if (flush == Z_FULL_FLUSH) {
932*4a5d661aSToomas Soome CLEAR_HASH(s); /* forget history */
933*4a5d661aSToomas Soome if (s->lookahead == 0) {
934*4a5d661aSToomas Soome s->strstart = 0;
935*4a5d661aSToomas Soome s->block_start = 0L;
936*4a5d661aSToomas Soome s->insert = 0;
937*4a5d661aSToomas Soome }
938*4a5d661aSToomas Soome }
939*4a5d661aSToomas Soome }
940*4a5d661aSToomas Soome flush_pending(strm);
941*4a5d661aSToomas Soome if (strm->avail_out == 0) {
942*4a5d661aSToomas Soome s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
943*4a5d661aSToomas Soome return Z_OK;
944*4a5d661aSToomas Soome }
945*4a5d661aSToomas Soome }
946*4a5d661aSToomas Soome }
947*4a5d661aSToomas Soome Assert(strm->avail_out > 0, "bug2");
948*4a5d661aSToomas Soome
949*4a5d661aSToomas Soome if (flush != Z_FINISH) return Z_OK;
950*4a5d661aSToomas Soome if (s->wrap <= 0) return Z_STREAM_END;
951*4a5d661aSToomas Soome
952*4a5d661aSToomas Soome /* Write the trailer */
953*4a5d661aSToomas Soome #ifdef GZIP
954*4a5d661aSToomas Soome if (s->wrap == 2) {
955*4a5d661aSToomas Soome put_byte(s, (Byte)(strm->adler & 0xff));
956*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
957*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
958*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
959*4a5d661aSToomas Soome put_byte(s, (Byte)(strm->total_in & 0xff));
960*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
961*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
962*4a5d661aSToomas Soome put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
963*4a5d661aSToomas Soome }
964*4a5d661aSToomas Soome else
965*4a5d661aSToomas Soome #endif
966*4a5d661aSToomas Soome {
967*4a5d661aSToomas Soome putShortMSB(s, (uInt)(strm->adler >> 16));
968*4a5d661aSToomas Soome putShortMSB(s, (uInt)(strm->adler & 0xffff));
969*4a5d661aSToomas Soome }
970*4a5d661aSToomas Soome flush_pending(strm);
971*4a5d661aSToomas Soome /* If avail_out is zero, the application will call deflate again
972*4a5d661aSToomas Soome * to flush the rest.
973*4a5d661aSToomas Soome */
974*4a5d661aSToomas Soome if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
975*4a5d661aSToomas Soome return s->pending != 0 ? Z_OK : Z_STREAM_END;
976*4a5d661aSToomas Soome }
977*4a5d661aSToomas Soome
978*4a5d661aSToomas Soome /* ========================================================================= */
deflateEnd(strm)979*4a5d661aSToomas Soome int ZEXPORT deflateEnd (strm)
980*4a5d661aSToomas Soome z_streamp strm;
981*4a5d661aSToomas Soome {
982*4a5d661aSToomas Soome int status;
983*4a5d661aSToomas Soome
984*4a5d661aSToomas Soome if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
985*4a5d661aSToomas Soome
986*4a5d661aSToomas Soome status = strm->state->status;
987*4a5d661aSToomas Soome if (status != INIT_STATE &&
988*4a5d661aSToomas Soome status != EXTRA_STATE &&
989*4a5d661aSToomas Soome status != NAME_STATE &&
990*4a5d661aSToomas Soome status != COMMENT_STATE &&
991*4a5d661aSToomas Soome status != HCRC_STATE &&
992*4a5d661aSToomas Soome status != BUSY_STATE &&
993*4a5d661aSToomas Soome status != FINISH_STATE) {
994*4a5d661aSToomas Soome return Z_STREAM_ERROR;
995*4a5d661aSToomas Soome }
996*4a5d661aSToomas Soome
997*4a5d661aSToomas Soome /* Deallocate in reverse order of allocations: */
998*4a5d661aSToomas Soome TRY_FREE(strm, strm->state->pending_buf);
999*4a5d661aSToomas Soome TRY_FREE(strm, strm->state->head);
1000*4a5d661aSToomas Soome TRY_FREE(strm, strm->state->prev);
1001*4a5d661aSToomas Soome TRY_FREE(strm, strm->state->window);
1002*4a5d661aSToomas Soome
1003*4a5d661aSToomas Soome ZFREE(strm, strm->state);
1004*4a5d661aSToomas Soome strm->state = Z_NULL;
1005*4a5d661aSToomas Soome
1006*4a5d661aSToomas Soome return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1007*4a5d661aSToomas Soome }
1008*4a5d661aSToomas Soome
1009*4a5d661aSToomas Soome /* =========================================================================
1010*4a5d661aSToomas Soome * Copy the source state to the destination state.
1011*4a5d661aSToomas Soome * To simplify the source, this is not supported for 16-bit MSDOS (which
1012*4a5d661aSToomas Soome * doesn't have enough memory anyway to duplicate compression states).
1013*4a5d661aSToomas Soome */
deflateCopy(dest,source)1014*4a5d661aSToomas Soome int ZEXPORT deflateCopy (dest, source)
1015*4a5d661aSToomas Soome z_streamp dest;
1016*4a5d661aSToomas Soome z_streamp source;
1017*4a5d661aSToomas Soome {
1018*4a5d661aSToomas Soome #ifdef MAXSEG_64K
1019*4a5d661aSToomas Soome return Z_STREAM_ERROR;
1020*4a5d661aSToomas Soome #else
1021*4a5d661aSToomas Soome deflate_state *ds;
1022*4a5d661aSToomas Soome deflate_state *ss;
1023*4a5d661aSToomas Soome ushf *overlay;
1024*4a5d661aSToomas Soome
1025*4a5d661aSToomas Soome
1026*4a5d661aSToomas Soome if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
1027*4a5d661aSToomas Soome return Z_STREAM_ERROR;
1028*4a5d661aSToomas Soome }
1029*4a5d661aSToomas Soome
1030*4a5d661aSToomas Soome ss = source->state;
1031*4a5d661aSToomas Soome
1032*4a5d661aSToomas Soome zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1033*4a5d661aSToomas Soome
1034*4a5d661aSToomas Soome ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1035*4a5d661aSToomas Soome if (ds == Z_NULL) return Z_MEM_ERROR;
1036*4a5d661aSToomas Soome dest->state = (struct internal_state FAR *) ds;
1037*4a5d661aSToomas Soome zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1038*4a5d661aSToomas Soome ds->strm = dest;
1039*4a5d661aSToomas Soome
1040*4a5d661aSToomas Soome ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1041*4a5d661aSToomas Soome ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1042*4a5d661aSToomas Soome ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1043*4a5d661aSToomas Soome overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
1044*4a5d661aSToomas Soome ds->pending_buf = (uchf *) overlay;
1045*4a5d661aSToomas Soome
1046*4a5d661aSToomas Soome if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1047*4a5d661aSToomas Soome ds->pending_buf == Z_NULL) {
1048*4a5d661aSToomas Soome deflateEnd (dest);
1049*4a5d661aSToomas Soome return Z_MEM_ERROR;
1050*4a5d661aSToomas Soome }
1051*4a5d661aSToomas Soome /* following zmemcpy do not work for 16-bit MSDOS */
1052*4a5d661aSToomas Soome zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1053*4a5d661aSToomas Soome zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1054*4a5d661aSToomas Soome zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1055*4a5d661aSToomas Soome zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1056*4a5d661aSToomas Soome
1057*4a5d661aSToomas Soome ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1058*4a5d661aSToomas Soome ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
1059*4a5d661aSToomas Soome ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
1060*4a5d661aSToomas Soome
1061*4a5d661aSToomas Soome ds->l_desc.dyn_tree = ds->dyn_ltree;
1062*4a5d661aSToomas Soome ds->d_desc.dyn_tree = ds->dyn_dtree;
1063*4a5d661aSToomas Soome ds->bl_desc.dyn_tree = ds->bl_tree;
1064*4a5d661aSToomas Soome
1065*4a5d661aSToomas Soome return Z_OK;
1066*4a5d661aSToomas Soome #endif /* MAXSEG_64K */
1067*4a5d661aSToomas Soome }
1068*4a5d661aSToomas Soome
1069*4a5d661aSToomas Soome /* ===========================================================================
1070*4a5d661aSToomas Soome * Read a new buffer from the current input stream, update the adler32
1071*4a5d661aSToomas Soome * and total number of bytes read. All deflate() input goes through
1072*4a5d661aSToomas Soome * this function so some applications may wish to modify it to avoid
1073*4a5d661aSToomas Soome * allocating a large strm->next_in buffer and copying from it.
1074*4a5d661aSToomas Soome * (See also flush_pending()).
1075*4a5d661aSToomas Soome */
read_buf(strm,buf,size)1076*4a5d661aSToomas Soome local int read_buf(strm, buf, size)
1077*4a5d661aSToomas Soome z_streamp strm;
1078*4a5d661aSToomas Soome Bytef *buf;
1079*4a5d661aSToomas Soome unsigned size;
1080*4a5d661aSToomas Soome {
1081*4a5d661aSToomas Soome unsigned len = strm->avail_in;
1082*4a5d661aSToomas Soome
1083*4a5d661aSToomas Soome if (len > size) len = size;
1084*4a5d661aSToomas Soome if (len == 0) return 0;
1085*4a5d661aSToomas Soome
1086*4a5d661aSToomas Soome strm->avail_in -= len;
1087*4a5d661aSToomas Soome
1088*4a5d661aSToomas Soome zmemcpy(buf, strm->next_in, len);
1089*4a5d661aSToomas Soome if (strm->state->wrap == 1) {
1090*4a5d661aSToomas Soome strm->adler = adler32(strm->adler, buf, len);
1091*4a5d661aSToomas Soome }
1092*4a5d661aSToomas Soome #ifdef GZIP
1093*4a5d661aSToomas Soome else if (strm->state->wrap == 2) {
1094*4a5d661aSToomas Soome strm->adler = crc32(strm->adler, buf, len);
1095*4a5d661aSToomas Soome }
1096*4a5d661aSToomas Soome #endif
1097*4a5d661aSToomas Soome strm->next_in += len;
1098*4a5d661aSToomas Soome strm->total_in += len;
1099*4a5d661aSToomas Soome
1100*4a5d661aSToomas Soome return (int)len;
1101*4a5d661aSToomas Soome }
1102*4a5d661aSToomas Soome
1103*4a5d661aSToomas Soome /* ===========================================================================
1104*4a5d661aSToomas Soome * Initialize the "longest match" routines for a new zlib stream
1105*4a5d661aSToomas Soome */
lm_init(s)1106*4a5d661aSToomas Soome local void lm_init (s)
1107*4a5d661aSToomas Soome deflate_state *s;
1108*4a5d661aSToomas Soome {
1109*4a5d661aSToomas Soome s->window_size = (ulg)2L*s->w_size;
1110*4a5d661aSToomas Soome
1111*4a5d661aSToomas Soome CLEAR_HASH(s);
1112*4a5d661aSToomas Soome
1113*4a5d661aSToomas Soome /* Set the default configuration parameters:
1114*4a5d661aSToomas Soome */
1115*4a5d661aSToomas Soome s->max_lazy_match = configuration_table[s->level].max_lazy;
1116*4a5d661aSToomas Soome s->good_match = configuration_table[s->level].good_length;
1117*4a5d661aSToomas Soome s->nice_match = configuration_table[s->level].nice_length;
1118*4a5d661aSToomas Soome s->max_chain_length = configuration_table[s->level].max_chain;
1119*4a5d661aSToomas Soome
1120*4a5d661aSToomas Soome s->strstart = 0;
1121*4a5d661aSToomas Soome s->block_start = 0L;
1122*4a5d661aSToomas Soome s->lookahead = 0;
1123*4a5d661aSToomas Soome s->insert = 0;
1124*4a5d661aSToomas Soome s->match_length = s->prev_length = MIN_MATCH-1;
1125*4a5d661aSToomas Soome s->match_available = 0;
1126*4a5d661aSToomas Soome s->ins_h = 0;
1127*4a5d661aSToomas Soome #ifndef FASTEST
1128*4a5d661aSToomas Soome #ifdef ASMV
1129*4a5d661aSToomas Soome match_init(); /* initialize the asm code */
1130*4a5d661aSToomas Soome #endif
1131*4a5d661aSToomas Soome #endif
1132*4a5d661aSToomas Soome }
1133*4a5d661aSToomas Soome
1134*4a5d661aSToomas Soome #ifndef FASTEST
1135*4a5d661aSToomas Soome /* ===========================================================================
1136*4a5d661aSToomas Soome * Set match_start to the longest match starting at the given string and
1137*4a5d661aSToomas Soome * return its length. Matches shorter or equal to prev_length are discarded,
1138*4a5d661aSToomas Soome * in which case the result is equal to prev_length and match_start is
1139*4a5d661aSToomas Soome * garbage.
1140*4a5d661aSToomas Soome * IN assertions: cur_match is the head of the hash chain for the current
1141*4a5d661aSToomas Soome * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1142*4a5d661aSToomas Soome * OUT assertion: the match length is not greater than s->lookahead.
1143*4a5d661aSToomas Soome */
1144*4a5d661aSToomas Soome #ifndef ASMV
1145*4a5d661aSToomas Soome /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1146*4a5d661aSToomas Soome * match.S. The code will be functionally equivalent.
1147*4a5d661aSToomas Soome */
longest_match(s,cur_match)1148*4a5d661aSToomas Soome local uInt longest_match(s, cur_match)
1149*4a5d661aSToomas Soome deflate_state *s;
1150*4a5d661aSToomas Soome IPos cur_match; /* current match */
1151*4a5d661aSToomas Soome {
1152*4a5d661aSToomas Soome unsigned chain_length = s->max_chain_length;/* max hash chain length */
1153*4a5d661aSToomas Soome register Bytef *scan = s->window + s->strstart; /* current string */
1154*4a5d661aSToomas Soome register Bytef *match; /* matched string */
1155*4a5d661aSToomas Soome register int len; /* length of current match */
1156*4a5d661aSToomas Soome int best_len = s->prev_length; /* best match length so far */
1157*4a5d661aSToomas Soome int nice_match = s->nice_match; /* stop if match long enough */
1158*4a5d661aSToomas Soome IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1159*4a5d661aSToomas Soome s->strstart - (IPos)MAX_DIST(s) : NIL;
1160*4a5d661aSToomas Soome /* Stop when cur_match becomes <= limit. To simplify the code,
1161*4a5d661aSToomas Soome * we prevent matches with the string of window index 0.
1162*4a5d661aSToomas Soome */
1163*4a5d661aSToomas Soome Posf *prev = s->prev;
1164*4a5d661aSToomas Soome uInt wmask = s->w_mask;
1165*4a5d661aSToomas Soome
1166*4a5d661aSToomas Soome #ifdef UNALIGNED_OK
1167*4a5d661aSToomas Soome /* Compare two bytes at a time. Note: this is not always beneficial.
1168*4a5d661aSToomas Soome * Try with and without -DUNALIGNED_OK to check.
1169*4a5d661aSToomas Soome */
1170*4a5d661aSToomas Soome register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1171*4a5d661aSToomas Soome register ush scan_start = *(ushf*)scan;
1172*4a5d661aSToomas Soome register ush scan_end = *(ushf*)(scan+best_len-1);
1173*4a5d661aSToomas Soome #else
1174*4a5d661aSToomas Soome register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1175*4a5d661aSToomas Soome register Byte scan_end1 = scan[best_len-1];
1176*4a5d661aSToomas Soome register Byte scan_end = scan[best_len];
1177*4a5d661aSToomas Soome #endif
1178*4a5d661aSToomas Soome
1179*4a5d661aSToomas Soome /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1180*4a5d661aSToomas Soome * It is easy to get rid of this optimization if necessary.
1181*4a5d661aSToomas Soome */
1182*4a5d661aSToomas Soome Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1183*4a5d661aSToomas Soome
1184*4a5d661aSToomas Soome /* Do not waste too much time if we already have a good match: */
1185*4a5d661aSToomas Soome if (s->prev_length >= s->good_match) {
1186*4a5d661aSToomas Soome chain_length >>= 2;
1187*4a5d661aSToomas Soome }
1188*4a5d661aSToomas Soome /* Do not look for matches beyond the end of the input. This is necessary
1189*4a5d661aSToomas Soome * to make deflate deterministic.
1190*4a5d661aSToomas Soome */
1191*4a5d661aSToomas Soome if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1192*4a5d661aSToomas Soome
1193*4a5d661aSToomas Soome Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1194*4a5d661aSToomas Soome
1195*4a5d661aSToomas Soome do {
1196*4a5d661aSToomas Soome Assert(cur_match < s->strstart, "no future");
1197*4a5d661aSToomas Soome match = s->window + cur_match;
1198*4a5d661aSToomas Soome
1199*4a5d661aSToomas Soome /* Skip to next match if the match length cannot increase
1200*4a5d661aSToomas Soome * or if the match length is less than 2. Note that the checks below
1201*4a5d661aSToomas Soome * for insufficient lookahead only occur occasionally for performance
1202*4a5d661aSToomas Soome * reasons. Therefore uninitialized memory will be accessed, and
1203*4a5d661aSToomas Soome * conditional jumps will be made that depend on those values.
1204*4a5d661aSToomas Soome * However the length of the match is limited to the lookahead, so
1205*4a5d661aSToomas Soome * the output of deflate is not affected by the uninitialized values.
1206*4a5d661aSToomas Soome */
1207*4a5d661aSToomas Soome #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1208*4a5d661aSToomas Soome /* This code assumes sizeof(unsigned short) == 2. Do not use
1209*4a5d661aSToomas Soome * UNALIGNED_OK if your compiler uses a different size.
1210*4a5d661aSToomas Soome */
1211*4a5d661aSToomas Soome if (*(ushf*)(match+best_len-1) != scan_end ||
1212*4a5d661aSToomas Soome *(ushf*)match != scan_start) continue;
1213*4a5d661aSToomas Soome
1214*4a5d661aSToomas Soome /* It is not necessary to compare scan[2] and match[2] since they are
1215*4a5d661aSToomas Soome * always equal when the other bytes match, given that the hash keys
1216*4a5d661aSToomas Soome * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1217*4a5d661aSToomas Soome * strstart+3, +5, ... up to strstart+257. We check for insufficient
1218*4a5d661aSToomas Soome * lookahead only every 4th comparison; the 128th check will be made
1219*4a5d661aSToomas Soome * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1220*4a5d661aSToomas Soome * necessary to put more guard bytes at the end of the window, or
1221*4a5d661aSToomas Soome * to check more often for insufficient lookahead.
1222*4a5d661aSToomas Soome */
1223*4a5d661aSToomas Soome Assert(scan[2] == match[2], "scan[2]?");
1224*4a5d661aSToomas Soome scan++, match++;
1225*4a5d661aSToomas Soome do {
1226*4a5d661aSToomas Soome } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1227*4a5d661aSToomas Soome *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1228*4a5d661aSToomas Soome *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1229*4a5d661aSToomas Soome *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1230*4a5d661aSToomas Soome scan < strend);
1231*4a5d661aSToomas Soome /* The funny "do {}" generates better code on most compilers */
1232*4a5d661aSToomas Soome
1233*4a5d661aSToomas Soome /* Here, scan <= window+strstart+257 */
1234*4a5d661aSToomas Soome Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1235*4a5d661aSToomas Soome if (*scan == *match) scan++;
1236*4a5d661aSToomas Soome
1237*4a5d661aSToomas Soome len = (MAX_MATCH - 1) - (int)(strend-scan);
1238*4a5d661aSToomas Soome scan = strend - (MAX_MATCH-1);
1239*4a5d661aSToomas Soome
1240*4a5d661aSToomas Soome #else /* UNALIGNED_OK */
1241*4a5d661aSToomas Soome
1242*4a5d661aSToomas Soome if (match[best_len] != scan_end ||
1243*4a5d661aSToomas Soome match[best_len-1] != scan_end1 ||
1244*4a5d661aSToomas Soome *match != *scan ||
1245*4a5d661aSToomas Soome *++match != scan[1]) continue;
1246*4a5d661aSToomas Soome
1247*4a5d661aSToomas Soome /* The check at best_len-1 can be removed because it will be made
1248*4a5d661aSToomas Soome * again later. (This heuristic is not always a win.)
1249*4a5d661aSToomas Soome * It is not necessary to compare scan[2] and match[2] since they
1250*4a5d661aSToomas Soome * are always equal when the other bytes match, given that
1251*4a5d661aSToomas Soome * the hash keys are equal and that HASH_BITS >= 8.
1252*4a5d661aSToomas Soome */
1253*4a5d661aSToomas Soome scan += 2, match++;
1254*4a5d661aSToomas Soome Assert(*scan == *match, "match[2]?");
1255*4a5d661aSToomas Soome
1256*4a5d661aSToomas Soome /* We check for insufficient lookahead only every 8th comparison;
1257*4a5d661aSToomas Soome * the 256th check will be made at strstart+258.
1258*4a5d661aSToomas Soome */
1259*4a5d661aSToomas Soome do {
1260*4a5d661aSToomas Soome } while (*++scan == *++match && *++scan == *++match &&
1261*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1262*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1263*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1264*4a5d661aSToomas Soome scan < strend);
1265*4a5d661aSToomas Soome
1266*4a5d661aSToomas Soome Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1267*4a5d661aSToomas Soome
1268*4a5d661aSToomas Soome len = MAX_MATCH - (int)(strend - scan);
1269*4a5d661aSToomas Soome scan = strend - MAX_MATCH;
1270*4a5d661aSToomas Soome
1271*4a5d661aSToomas Soome #endif /* UNALIGNED_OK */
1272*4a5d661aSToomas Soome
1273*4a5d661aSToomas Soome if (len > best_len) {
1274*4a5d661aSToomas Soome s->match_start = cur_match;
1275*4a5d661aSToomas Soome best_len = len;
1276*4a5d661aSToomas Soome if (len >= nice_match) break;
1277*4a5d661aSToomas Soome #ifdef UNALIGNED_OK
1278*4a5d661aSToomas Soome scan_end = *(ushf*)(scan+best_len-1);
1279*4a5d661aSToomas Soome #else
1280*4a5d661aSToomas Soome scan_end1 = scan[best_len-1];
1281*4a5d661aSToomas Soome scan_end = scan[best_len];
1282*4a5d661aSToomas Soome #endif
1283*4a5d661aSToomas Soome }
1284*4a5d661aSToomas Soome } while ((cur_match = prev[cur_match & wmask]) > limit
1285*4a5d661aSToomas Soome && --chain_length != 0);
1286*4a5d661aSToomas Soome
1287*4a5d661aSToomas Soome if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1288*4a5d661aSToomas Soome return s->lookahead;
1289*4a5d661aSToomas Soome }
1290*4a5d661aSToomas Soome #endif /* ASMV */
1291*4a5d661aSToomas Soome
1292*4a5d661aSToomas Soome #else /* FASTEST */
1293*4a5d661aSToomas Soome
1294*4a5d661aSToomas Soome /* ---------------------------------------------------------------------------
1295*4a5d661aSToomas Soome * Optimized version for FASTEST only
1296*4a5d661aSToomas Soome */
longest_match(s,cur_match)1297*4a5d661aSToomas Soome local uInt longest_match(s, cur_match)
1298*4a5d661aSToomas Soome deflate_state *s;
1299*4a5d661aSToomas Soome IPos cur_match; /* current match */
1300*4a5d661aSToomas Soome {
1301*4a5d661aSToomas Soome register Bytef *scan = s->window + s->strstart; /* current string */
1302*4a5d661aSToomas Soome register Bytef *match; /* matched string */
1303*4a5d661aSToomas Soome register int len; /* length of current match */
1304*4a5d661aSToomas Soome register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1305*4a5d661aSToomas Soome
1306*4a5d661aSToomas Soome /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1307*4a5d661aSToomas Soome * It is easy to get rid of this optimization if necessary.
1308*4a5d661aSToomas Soome */
1309*4a5d661aSToomas Soome Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1310*4a5d661aSToomas Soome
1311*4a5d661aSToomas Soome Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1312*4a5d661aSToomas Soome
1313*4a5d661aSToomas Soome Assert(cur_match < s->strstart, "no future");
1314*4a5d661aSToomas Soome
1315*4a5d661aSToomas Soome match = s->window + cur_match;
1316*4a5d661aSToomas Soome
1317*4a5d661aSToomas Soome /* Return failure if the match length is less than 2:
1318*4a5d661aSToomas Soome */
1319*4a5d661aSToomas Soome if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1320*4a5d661aSToomas Soome
1321*4a5d661aSToomas Soome /* The check at best_len-1 can be removed because it will be made
1322*4a5d661aSToomas Soome * again later. (This heuristic is not always a win.)
1323*4a5d661aSToomas Soome * It is not necessary to compare scan[2] and match[2] since they
1324*4a5d661aSToomas Soome * are always equal when the other bytes match, given that
1325*4a5d661aSToomas Soome * the hash keys are equal and that HASH_BITS >= 8.
1326*4a5d661aSToomas Soome */
1327*4a5d661aSToomas Soome scan += 2, match += 2;
1328*4a5d661aSToomas Soome Assert(*scan == *match, "match[2]?");
1329*4a5d661aSToomas Soome
1330*4a5d661aSToomas Soome /* We check for insufficient lookahead only every 8th comparison;
1331*4a5d661aSToomas Soome * the 256th check will be made at strstart+258.
1332*4a5d661aSToomas Soome */
1333*4a5d661aSToomas Soome do {
1334*4a5d661aSToomas Soome } while (*++scan == *++match && *++scan == *++match &&
1335*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1336*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1337*4a5d661aSToomas Soome *++scan == *++match && *++scan == *++match &&
1338*4a5d661aSToomas Soome scan < strend);
1339*4a5d661aSToomas Soome
1340*4a5d661aSToomas Soome Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1341*4a5d661aSToomas Soome
1342*4a5d661aSToomas Soome len = MAX_MATCH - (int)(strend - scan);
1343*4a5d661aSToomas Soome
1344*4a5d661aSToomas Soome if (len < MIN_MATCH) return MIN_MATCH - 1;
1345*4a5d661aSToomas Soome
1346*4a5d661aSToomas Soome s->match_start = cur_match;
1347*4a5d661aSToomas Soome return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1348*4a5d661aSToomas Soome }
1349*4a5d661aSToomas Soome
1350*4a5d661aSToomas Soome #endif /* FASTEST */
1351*4a5d661aSToomas Soome
1352*4a5d661aSToomas Soome #ifdef DEBUG
1353*4a5d661aSToomas Soome /* ===========================================================================
1354*4a5d661aSToomas Soome * Check that the match at match_start is indeed a match.
1355*4a5d661aSToomas Soome */
check_match(s,start,match,length)1356*4a5d661aSToomas Soome local void check_match(s, start, match, length)
1357*4a5d661aSToomas Soome deflate_state *s;
1358*4a5d661aSToomas Soome IPos start, match;
1359*4a5d661aSToomas Soome int length;
1360*4a5d661aSToomas Soome {
1361*4a5d661aSToomas Soome /* check that the match is indeed a match */
1362*4a5d661aSToomas Soome if (zmemcmp(s->window + match,
1363*4a5d661aSToomas Soome s->window + start, length) != EQUAL) {
1364*4a5d661aSToomas Soome fprintf(stderr, " start %u, match %u, length %d\n",
1365*4a5d661aSToomas Soome start, match, length);
1366*4a5d661aSToomas Soome do {
1367*4a5d661aSToomas Soome fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1368*4a5d661aSToomas Soome } while (--length != 0);
1369*4a5d661aSToomas Soome z_error("invalid match");
1370*4a5d661aSToomas Soome }
1371*4a5d661aSToomas Soome if (z_verbose > 1) {
1372*4a5d661aSToomas Soome fprintf(stderr,"\\[%d,%d]", start-match, length);
1373*4a5d661aSToomas Soome do { putc(s->window[start++], stderr); } while (--length != 0);
1374*4a5d661aSToomas Soome }
1375*4a5d661aSToomas Soome }
1376*4a5d661aSToomas Soome #else
1377*4a5d661aSToomas Soome # define check_match(s, start, match, length)
1378*4a5d661aSToomas Soome #endif /* DEBUG */
1379*4a5d661aSToomas Soome
1380*4a5d661aSToomas Soome /* ===========================================================================
1381*4a5d661aSToomas Soome * Fill the window when the lookahead becomes insufficient.
1382*4a5d661aSToomas Soome * Updates strstart and lookahead.
1383*4a5d661aSToomas Soome *
1384*4a5d661aSToomas Soome * IN assertion: lookahead < MIN_LOOKAHEAD
1385*4a5d661aSToomas Soome * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1386*4a5d661aSToomas Soome * At least one byte has been read, or avail_in == 0; reads are
1387*4a5d661aSToomas Soome * performed for at least two bytes (required for the zip translate_eol
1388*4a5d661aSToomas Soome * option -- not supported here).
1389*4a5d661aSToomas Soome */
fill_window(s)1390*4a5d661aSToomas Soome local void fill_window(s)
1391*4a5d661aSToomas Soome deflate_state *s;
1392*4a5d661aSToomas Soome {
1393*4a5d661aSToomas Soome register unsigned n, m;
1394*4a5d661aSToomas Soome register Posf *p;
1395*4a5d661aSToomas Soome unsigned more; /* Amount of free space at the end of the window. */
1396*4a5d661aSToomas Soome uInt wsize = s->w_size;
1397*4a5d661aSToomas Soome
1398*4a5d661aSToomas Soome Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1399*4a5d661aSToomas Soome
1400*4a5d661aSToomas Soome do {
1401*4a5d661aSToomas Soome more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1402*4a5d661aSToomas Soome
1403*4a5d661aSToomas Soome /* Deal with !@#$% 64K limit: */
1404*4a5d661aSToomas Soome if (sizeof(int) <= 2) {
1405*4a5d661aSToomas Soome if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1406*4a5d661aSToomas Soome more = wsize;
1407*4a5d661aSToomas Soome
1408*4a5d661aSToomas Soome } else if (more == (unsigned)(-1)) {
1409*4a5d661aSToomas Soome /* Very unlikely, but possible on 16 bit machine if
1410*4a5d661aSToomas Soome * strstart == 0 && lookahead == 1 (input done a byte at time)
1411*4a5d661aSToomas Soome */
1412*4a5d661aSToomas Soome more--;
1413*4a5d661aSToomas Soome }
1414*4a5d661aSToomas Soome }
1415*4a5d661aSToomas Soome
1416*4a5d661aSToomas Soome /* If the window is almost full and there is insufficient lookahead,
1417*4a5d661aSToomas Soome * move the upper half to the lower one to make room in the upper half.
1418*4a5d661aSToomas Soome */
1419*4a5d661aSToomas Soome if (s->strstart >= wsize+MAX_DIST(s)) {
1420*4a5d661aSToomas Soome
1421*4a5d661aSToomas Soome zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1422*4a5d661aSToomas Soome s->match_start -= wsize;
1423*4a5d661aSToomas Soome s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1424*4a5d661aSToomas Soome s->block_start -= (long) wsize;
1425*4a5d661aSToomas Soome
1426*4a5d661aSToomas Soome /* Slide the hash table (could be avoided with 32 bit values
1427*4a5d661aSToomas Soome at the expense of memory usage). We slide even when level == 0
1428*4a5d661aSToomas Soome to keep the hash table consistent if we switch back to level > 0
1429*4a5d661aSToomas Soome later. (Using level 0 permanently is not an optimal usage of
1430*4a5d661aSToomas Soome zlib, so we don't care about this pathological case.)
1431*4a5d661aSToomas Soome */
1432*4a5d661aSToomas Soome n = s->hash_size;
1433*4a5d661aSToomas Soome p = &s->head[n];
1434*4a5d661aSToomas Soome do {
1435*4a5d661aSToomas Soome m = *--p;
1436*4a5d661aSToomas Soome *p = (Pos)(m >= wsize ? m-wsize : NIL);
1437*4a5d661aSToomas Soome } while (--n);
1438*4a5d661aSToomas Soome
1439*4a5d661aSToomas Soome n = wsize;
1440*4a5d661aSToomas Soome #ifndef FASTEST
1441*4a5d661aSToomas Soome p = &s->prev[n];
1442*4a5d661aSToomas Soome do {
1443*4a5d661aSToomas Soome m = *--p;
1444*4a5d661aSToomas Soome *p = (Pos)(m >= wsize ? m-wsize : NIL);
1445*4a5d661aSToomas Soome /* If n is not on any hash chain, prev[n] is garbage but
1446*4a5d661aSToomas Soome * its value will never be used.
1447*4a5d661aSToomas Soome */
1448*4a5d661aSToomas Soome } while (--n);
1449*4a5d661aSToomas Soome #endif
1450*4a5d661aSToomas Soome more += wsize;
1451*4a5d661aSToomas Soome }
1452*4a5d661aSToomas Soome if (s->strm->avail_in == 0) break;
1453*4a5d661aSToomas Soome
1454*4a5d661aSToomas Soome /* If there was no sliding:
1455*4a5d661aSToomas Soome * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1456*4a5d661aSToomas Soome * more == window_size - lookahead - strstart
1457*4a5d661aSToomas Soome * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1458*4a5d661aSToomas Soome * => more >= window_size - 2*WSIZE + 2
1459*4a5d661aSToomas Soome * In the BIG_MEM or MMAP case (not yet supported),
1460*4a5d661aSToomas Soome * window_size == input_size + MIN_LOOKAHEAD &&
1461*4a5d661aSToomas Soome * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1462*4a5d661aSToomas Soome * Otherwise, window_size == 2*WSIZE so more >= 2.
1463*4a5d661aSToomas Soome * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1464*4a5d661aSToomas Soome */
1465*4a5d661aSToomas Soome Assert(more >= 2, "more < 2");
1466*4a5d661aSToomas Soome
1467*4a5d661aSToomas Soome n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1468*4a5d661aSToomas Soome s->lookahead += n;
1469*4a5d661aSToomas Soome
1470*4a5d661aSToomas Soome /* Initialize the hash value now that we have some input: */
1471*4a5d661aSToomas Soome if (s->lookahead + s->insert >= MIN_MATCH) {
1472*4a5d661aSToomas Soome uInt str = s->strstart - s->insert;
1473*4a5d661aSToomas Soome s->ins_h = s->window[str];
1474*4a5d661aSToomas Soome UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1475*4a5d661aSToomas Soome #if MIN_MATCH != 3
1476*4a5d661aSToomas Soome Call UPDATE_HASH() MIN_MATCH-3 more times
1477*4a5d661aSToomas Soome #endif
1478*4a5d661aSToomas Soome while (s->insert) {
1479*4a5d661aSToomas Soome UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1480*4a5d661aSToomas Soome #ifndef FASTEST
1481*4a5d661aSToomas Soome s->prev[str & s->w_mask] = s->head[s->ins_h];
1482*4a5d661aSToomas Soome #endif
1483*4a5d661aSToomas Soome s->head[s->ins_h] = (Pos)str;
1484*4a5d661aSToomas Soome str++;
1485*4a5d661aSToomas Soome s->insert--;
1486*4a5d661aSToomas Soome if (s->lookahead + s->insert < MIN_MATCH)
1487*4a5d661aSToomas Soome break;
1488*4a5d661aSToomas Soome }
1489*4a5d661aSToomas Soome }
1490*4a5d661aSToomas Soome /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1491*4a5d661aSToomas Soome * but this is not important since only literal bytes will be emitted.
1492*4a5d661aSToomas Soome */
1493*4a5d661aSToomas Soome
1494*4a5d661aSToomas Soome } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1495*4a5d661aSToomas Soome
1496*4a5d661aSToomas Soome /* If the WIN_INIT bytes after the end of the current data have never been
1497*4a5d661aSToomas Soome * written, then zero those bytes in order to avoid memory check reports of
1498*4a5d661aSToomas Soome * the use of uninitialized (or uninitialised as Julian writes) bytes by
1499*4a5d661aSToomas Soome * the longest match routines. Update the high water mark for the next
1500*4a5d661aSToomas Soome * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1501*4a5d661aSToomas Soome * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1502*4a5d661aSToomas Soome */
1503*4a5d661aSToomas Soome if (s->high_water < s->window_size) {
1504*4a5d661aSToomas Soome ulg curr = s->strstart + (ulg)(s->lookahead);
1505*4a5d661aSToomas Soome ulg init;
1506*4a5d661aSToomas Soome
1507*4a5d661aSToomas Soome if (s->high_water < curr) {
1508*4a5d661aSToomas Soome /* Previous high water mark below current data -- zero WIN_INIT
1509*4a5d661aSToomas Soome * bytes or up to end of window, whichever is less.
1510*4a5d661aSToomas Soome */
1511*4a5d661aSToomas Soome init = s->window_size - curr;
1512*4a5d661aSToomas Soome if (init > WIN_INIT)
1513*4a5d661aSToomas Soome init = WIN_INIT;
1514*4a5d661aSToomas Soome zmemzero(s->window + curr, (unsigned)init);
1515*4a5d661aSToomas Soome s->high_water = curr + init;
1516*4a5d661aSToomas Soome }
1517*4a5d661aSToomas Soome else if (s->high_water < (ulg)curr + WIN_INIT) {
1518*4a5d661aSToomas Soome /* High water mark at or above current data, but below current data
1519*4a5d661aSToomas Soome * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1520*4a5d661aSToomas Soome * to end of window, whichever is less.
1521*4a5d661aSToomas Soome */
1522*4a5d661aSToomas Soome init = (ulg)curr + WIN_INIT - s->high_water;
1523*4a5d661aSToomas Soome if (init > s->window_size - s->high_water)
1524*4a5d661aSToomas Soome init = s->window_size - s->high_water;
1525*4a5d661aSToomas Soome zmemzero(s->window + s->high_water, (unsigned)init);
1526*4a5d661aSToomas Soome s->high_water += init;
1527*4a5d661aSToomas Soome }
1528*4a5d661aSToomas Soome }
1529*4a5d661aSToomas Soome
1530*4a5d661aSToomas Soome Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1531*4a5d661aSToomas Soome "not enough room for search");
1532*4a5d661aSToomas Soome }
1533*4a5d661aSToomas Soome
1534*4a5d661aSToomas Soome /* ===========================================================================
1535*4a5d661aSToomas Soome * Flush the current block, with given end-of-file flag.
1536*4a5d661aSToomas Soome * IN assertion: strstart is set to the end of the current match.
1537*4a5d661aSToomas Soome */
1538*4a5d661aSToomas Soome #define FLUSH_BLOCK_ONLY(s, last) { \
1539*4a5d661aSToomas Soome _tr_flush_block(s, (s->block_start >= 0L ? \
1540*4a5d661aSToomas Soome (charf *)&s->window[(unsigned)s->block_start] : \
1541*4a5d661aSToomas Soome (charf *)Z_NULL), \
1542*4a5d661aSToomas Soome (ulg)((long)s->strstart - s->block_start), \
1543*4a5d661aSToomas Soome (last)); \
1544*4a5d661aSToomas Soome s->block_start = s->strstart; \
1545*4a5d661aSToomas Soome flush_pending(s->strm); \
1546*4a5d661aSToomas Soome Tracev((stderr,"[FLUSH]")); \
1547*4a5d661aSToomas Soome }
1548*4a5d661aSToomas Soome
1549*4a5d661aSToomas Soome /* Same but force premature exit if necessary. */
1550*4a5d661aSToomas Soome #define FLUSH_BLOCK(s, last) { \
1551*4a5d661aSToomas Soome FLUSH_BLOCK_ONLY(s, last); \
1552*4a5d661aSToomas Soome if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1553*4a5d661aSToomas Soome }
1554*4a5d661aSToomas Soome
1555*4a5d661aSToomas Soome /* ===========================================================================
1556*4a5d661aSToomas Soome * Copy without compression as much as possible from the input stream, return
1557*4a5d661aSToomas Soome * the current block state.
1558*4a5d661aSToomas Soome * This function does not insert new strings in the dictionary since
1559*4a5d661aSToomas Soome * uncompressible data is probably not useful. This function is used
1560*4a5d661aSToomas Soome * only for the level=0 compression option.
1561*4a5d661aSToomas Soome * NOTE: this function should be optimized to avoid extra copying from
1562*4a5d661aSToomas Soome * window to pending_buf.
1563*4a5d661aSToomas Soome */
deflate_stored(s,flush)1564*4a5d661aSToomas Soome local block_state deflate_stored(s, flush)
1565*4a5d661aSToomas Soome deflate_state *s;
1566*4a5d661aSToomas Soome int flush;
1567*4a5d661aSToomas Soome {
1568*4a5d661aSToomas Soome /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1569*4a5d661aSToomas Soome * to pending_buf_size, and each stored block has a 5 byte header:
1570*4a5d661aSToomas Soome */
1571*4a5d661aSToomas Soome ulg max_block_size = 0xffff;
1572*4a5d661aSToomas Soome ulg max_start;
1573*4a5d661aSToomas Soome
1574*4a5d661aSToomas Soome if (max_block_size > s->pending_buf_size - 5) {
1575*4a5d661aSToomas Soome max_block_size = s->pending_buf_size - 5;
1576*4a5d661aSToomas Soome }
1577*4a5d661aSToomas Soome
1578*4a5d661aSToomas Soome /* Copy as much as possible from input to output: */
1579*4a5d661aSToomas Soome for (;;) {
1580*4a5d661aSToomas Soome /* Fill the window as much as possible: */
1581*4a5d661aSToomas Soome if (s->lookahead <= 1) {
1582*4a5d661aSToomas Soome
1583*4a5d661aSToomas Soome Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1584*4a5d661aSToomas Soome s->block_start >= (long)s->w_size, "slide too late");
1585*4a5d661aSToomas Soome
1586*4a5d661aSToomas Soome fill_window(s);
1587*4a5d661aSToomas Soome if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1588*4a5d661aSToomas Soome
1589*4a5d661aSToomas Soome if (s->lookahead == 0) break; /* flush the current block */
1590*4a5d661aSToomas Soome }
1591*4a5d661aSToomas Soome Assert(s->block_start >= 0L, "block gone");
1592*4a5d661aSToomas Soome
1593*4a5d661aSToomas Soome s->strstart += s->lookahead;
1594*4a5d661aSToomas Soome s->lookahead = 0;
1595*4a5d661aSToomas Soome
1596*4a5d661aSToomas Soome /* Emit a stored block if pending_buf will be full: */
1597*4a5d661aSToomas Soome max_start = s->block_start + max_block_size;
1598*4a5d661aSToomas Soome if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1599*4a5d661aSToomas Soome /* strstart == 0 is possible when wraparound on 16-bit machine */
1600*4a5d661aSToomas Soome s->lookahead = (uInt)(s->strstart - max_start);
1601*4a5d661aSToomas Soome s->strstart = (uInt)max_start;
1602*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1603*4a5d661aSToomas Soome }
1604*4a5d661aSToomas Soome /* Flush if we may have to slide, otherwise block_start may become
1605*4a5d661aSToomas Soome * negative and the data will be gone:
1606*4a5d661aSToomas Soome */
1607*4a5d661aSToomas Soome if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1608*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1609*4a5d661aSToomas Soome }
1610*4a5d661aSToomas Soome }
1611*4a5d661aSToomas Soome s->insert = 0;
1612*4a5d661aSToomas Soome if (flush == Z_FINISH) {
1613*4a5d661aSToomas Soome FLUSH_BLOCK(s, 1);
1614*4a5d661aSToomas Soome return finish_done;
1615*4a5d661aSToomas Soome }
1616*4a5d661aSToomas Soome if ((long)s->strstart > s->block_start)
1617*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1618*4a5d661aSToomas Soome return block_done;
1619*4a5d661aSToomas Soome }
1620*4a5d661aSToomas Soome
1621*4a5d661aSToomas Soome /* ===========================================================================
1622*4a5d661aSToomas Soome * Compress as much as possible from the input stream, return the current
1623*4a5d661aSToomas Soome * block state.
1624*4a5d661aSToomas Soome * This function does not perform lazy evaluation of matches and inserts
1625*4a5d661aSToomas Soome * new strings in the dictionary only for unmatched strings or for short
1626*4a5d661aSToomas Soome * matches. It is used only for the fast compression options.
1627*4a5d661aSToomas Soome */
deflate_fast(s,flush)1628*4a5d661aSToomas Soome local block_state deflate_fast(s, flush)
1629*4a5d661aSToomas Soome deflate_state *s;
1630*4a5d661aSToomas Soome int flush;
1631*4a5d661aSToomas Soome {
1632*4a5d661aSToomas Soome IPos hash_head; /* head of the hash chain */
1633*4a5d661aSToomas Soome int bflush; /* set if current block must be flushed */
1634*4a5d661aSToomas Soome
1635*4a5d661aSToomas Soome for (;;) {
1636*4a5d661aSToomas Soome /* Make sure that we always have enough lookahead, except
1637*4a5d661aSToomas Soome * at the end of the input file. We need MAX_MATCH bytes
1638*4a5d661aSToomas Soome * for the next match, plus MIN_MATCH bytes to insert the
1639*4a5d661aSToomas Soome * string following the next match.
1640*4a5d661aSToomas Soome */
1641*4a5d661aSToomas Soome if (s->lookahead < MIN_LOOKAHEAD) {
1642*4a5d661aSToomas Soome fill_window(s);
1643*4a5d661aSToomas Soome if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1644*4a5d661aSToomas Soome return need_more;
1645*4a5d661aSToomas Soome }
1646*4a5d661aSToomas Soome if (s->lookahead == 0) break; /* flush the current block */
1647*4a5d661aSToomas Soome }
1648*4a5d661aSToomas Soome
1649*4a5d661aSToomas Soome /* Insert the string window[strstart .. strstart+2] in the
1650*4a5d661aSToomas Soome * dictionary, and set hash_head to the head of the hash chain:
1651*4a5d661aSToomas Soome */
1652*4a5d661aSToomas Soome hash_head = NIL;
1653*4a5d661aSToomas Soome if (s->lookahead >= MIN_MATCH) {
1654*4a5d661aSToomas Soome INSERT_STRING(s, s->strstart, hash_head);
1655*4a5d661aSToomas Soome }
1656*4a5d661aSToomas Soome
1657*4a5d661aSToomas Soome /* Find the longest match, discarding those <= prev_length.
1658*4a5d661aSToomas Soome * At this point we have always match_length < MIN_MATCH
1659*4a5d661aSToomas Soome */
1660*4a5d661aSToomas Soome if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1661*4a5d661aSToomas Soome /* To simplify the code, we prevent matches with the string
1662*4a5d661aSToomas Soome * of window index 0 (in particular we have to avoid a match
1663*4a5d661aSToomas Soome * of the string with itself at the start of the input file).
1664*4a5d661aSToomas Soome */
1665*4a5d661aSToomas Soome s->match_length = longest_match (s, hash_head);
1666*4a5d661aSToomas Soome /* longest_match() sets match_start */
1667*4a5d661aSToomas Soome }
1668*4a5d661aSToomas Soome if (s->match_length >= MIN_MATCH) {
1669*4a5d661aSToomas Soome check_match(s, s->strstart, s->match_start, s->match_length);
1670*4a5d661aSToomas Soome
1671*4a5d661aSToomas Soome _tr_tally_dist(s, s->strstart - s->match_start,
1672*4a5d661aSToomas Soome s->match_length - MIN_MATCH, bflush);
1673*4a5d661aSToomas Soome
1674*4a5d661aSToomas Soome s->lookahead -= s->match_length;
1675*4a5d661aSToomas Soome
1676*4a5d661aSToomas Soome /* Insert new strings in the hash table only if the match length
1677*4a5d661aSToomas Soome * is not too large. This saves time but degrades compression.
1678*4a5d661aSToomas Soome */
1679*4a5d661aSToomas Soome #ifndef FASTEST
1680*4a5d661aSToomas Soome if (s->match_length <= s->max_insert_length &&
1681*4a5d661aSToomas Soome s->lookahead >= MIN_MATCH) {
1682*4a5d661aSToomas Soome s->match_length--; /* string at strstart already in table */
1683*4a5d661aSToomas Soome do {
1684*4a5d661aSToomas Soome s->strstart++;
1685*4a5d661aSToomas Soome INSERT_STRING(s, s->strstart, hash_head);
1686*4a5d661aSToomas Soome /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1687*4a5d661aSToomas Soome * always MIN_MATCH bytes ahead.
1688*4a5d661aSToomas Soome */
1689*4a5d661aSToomas Soome } while (--s->match_length != 0);
1690*4a5d661aSToomas Soome s->strstart++;
1691*4a5d661aSToomas Soome } else
1692*4a5d661aSToomas Soome #endif
1693*4a5d661aSToomas Soome {
1694*4a5d661aSToomas Soome s->strstart += s->match_length;
1695*4a5d661aSToomas Soome s->match_length = 0;
1696*4a5d661aSToomas Soome s->ins_h = s->window[s->strstart];
1697*4a5d661aSToomas Soome UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1698*4a5d661aSToomas Soome #if MIN_MATCH != 3
1699*4a5d661aSToomas Soome Call UPDATE_HASH() MIN_MATCH-3 more times
1700*4a5d661aSToomas Soome #endif
1701*4a5d661aSToomas Soome /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1702*4a5d661aSToomas Soome * matter since it will be recomputed at next deflate call.
1703*4a5d661aSToomas Soome */
1704*4a5d661aSToomas Soome }
1705*4a5d661aSToomas Soome } else {
1706*4a5d661aSToomas Soome /* No match, output a literal byte */
1707*4a5d661aSToomas Soome Tracevv((stderr,"%c", s->window[s->strstart]));
1708*4a5d661aSToomas Soome _tr_tally_lit (s, s->window[s->strstart], bflush);
1709*4a5d661aSToomas Soome s->lookahead--;
1710*4a5d661aSToomas Soome s->strstart++;
1711*4a5d661aSToomas Soome }
1712*4a5d661aSToomas Soome if (bflush) FLUSH_BLOCK(s, 0);
1713*4a5d661aSToomas Soome }
1714*4a5d661aSToomas Soome s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1715*4a5d661aSToomas Soome if (flush == Z_FINISH) {
1716*4a5d661aSToomas Soome FLUSH_BLOCK(s, 1);
1717*4a5d661aSToomas Soome return finish_done;
1718*4a5d661aSToomas Soome }
1719*4a5d661aSToomas Soome if (s->last_lit)
1720*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1721*4a5d661aSToomas Soome return block_done;
1722*4a5d661aSToomas Soome }
1723*4a5d661aSToomas Soome
1724*4a5d661aSToomas Soome #ifndef FASTEST
1725*4a5d661aSToomas Soome /* ===========================================================================
1726*4a5d661aSToomas Soome * Same as above, but achieves better compression. We use a lazy
1727*4a5d661aSToomas Soome * evaluation for matches: a match is finally adopted only if there is
1728*4a5d661aSToomas Soome * no better match at the next window position.
1729*4a5d661aSToomas Soome */
deflate_slow(s,flush)1730*4a5d661aSToomas Soome local block_state deflate_slow(s, flush)
1731*4a5d661aSToomas Soome deflate_state *s;
1732*4a5d661aSToomas Soome int flush;
1733*4a5d661aSToomas Soome {
1734*4a5d661aSToomas Soome IPos hash_head; /* head of hash chain */
1735*4a5d661aSToomas Soome int bflush; /* set if current block must be flushed */
1736*4a5d661aSToomas Soome
1737*4a5d661aSToomas Soome /* Process the input block. */
1738*4a5d661aSToomas Soome for (;;) {
1739*4a5d661aSToomas Soome /* Make sure that we always have enough lookahead, except
1740*4a5d661aSToomas Soome * at the end of the input file. We need MAX_MATCH bytes
1741*4a5d661aSToomas Soome * for the next match, plus MIN_MATCH bytes to insert the
1742*4a5d661aSToomas Soome * string following the next match.
1743*4a5d661aSToomas Soome */
1744*4a5d661aSToomas Soome if (s->lookahead < MIN_LOOKAHEAD) {
1745*4a5d661aSToomas Soome fill_window(s);
1746*4a5d661aSToomas Soome if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1747*4a5d661aSToomas Soome return need_more;
1748*4a5d661aSToomas Soome }
1749*4a5d661aSToomas Soome if (s->lookahead == 0) break; /* flush the current block */
1750*4a5d661aSToomas Soome }
1751*4a5d661aSToomas Soome
1752*4a5d661aSToomas Soome /* Insert the string window[strstart .. strstart+2] in the
1753*4a5d661aSToomas Soome * dictionary, and set hash_head to the head of the hash chain:
1754*4a5d661aSToomas Soome */
1755*4a5d661aSToomas Soome hash_head = NIL;
1756*4a5d661aSToomas Soome if (s->lookahead >= MIN_MATCH) {
1757*4a5d661aSToomas Soome INSERT_STRING(s, s->strstart, hash_head);
1758*4a5d661aSToomas Soome }
1759*4a5d661aSToomas Soome
1760*4a5d661aSToomas Soome /* Find the longest match, discarding those <= prev_length.
1761*4a5d661aSToomas Soome */
1762*4a5d661aSToomas Soome s->prev_length = s->match_length, s->prev_match = s->match_start;
1763*4a5d661aSToomas Soome s->match_length = MIN_MATCH-1;
1764*4a5d661aSToomas Soome
1765*4a5d661aSToomas Soome if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1766*4a5d661aSToomas Soome s->strstart - hash_head <= MAX_DIST(s)) {
1767*4a5d661aSToomas Soome /* To simplify the code, we prevent matches with the string
1768*4a5d661aSToomas Soome * of window index 0 (in particular we have to avoid a match
1769*4a5d661aSToomas Soome * of the string with itself at the start of the input file).
1770*4a5d661aSToomas Soome */
1771*4a5d661aSToomas Soome s->match_length = longest_match (s, hash_head);
1772*4a5d661aSToomas Soome /* longest_match() sets match_start */
1773*4a5d661aSToomas Soome
1774*4a5d661aSToomas Soome if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1775*4a5d661aSToomas Soome #if TOO_FAR <= 32767
1776*4a5d661aSToomas Soome || (s->match_length == MIN_MATCH &&
1777*4a5d661aSToomas Soome s->strstart - s->match_start > TOO_FAR)
1778*4a5d661aSToomas Soome #endif
1779*4a5d661aSToomas Soome )) {
1780*4a5d661aSToomas Soome
1781*4a5d661aSToomas Soome /* If prev_match is also MIN_MATCH, match_start is garbage
1782*4a5d661aSToomas Soome * but we will ignore the current match anyway.
1783*4a5d661aSToomas Soome */
1784*4a5d661aSToomas Soome s->match_length = MIN_MATCH-1;
1785*4a5d661aSToomas Soome }
1786*4a5d661aSToomas Soome }
1787*4a5d661aSToomas Soome /* If there was a match at the previous step and the current
1788*4a5d661aSToomas Soome * match is not better, output the previous match:
1789*4a5d661aSToomas Soome */
1790*4a5d661aSToomas Soome if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1791*4a5d661aSToomas Soome uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1792*4a5d661aSToomas Soome /* Do not insert strings in hash table beyond this. */
1793*4a5d661aSToomas Soome
1794*4a5d661aSToomas Soome check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1795*4a5d661aSToomas Soome
1796*4a5d661aSToomas Soome _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1797*4a5d661aSToomas Soome s->prev_length - MIN_MATCH, bflush);
1798*4a5d661aSToomas Soome
1799*4a5d661aSToomas Soome /* Insert in hash table all strings up to the end of the match.
1800*4a5d661aSToomas Soome * strstart-1 and strstart are already inserted. If there is not
1801*4a5d661aSToomas Soome * enough lookahead, the last two strings are not inserted in
1802*4a5d661aSToomas Soome * the hash table.
1803*4a5d661aSToomas Soome */
1804*4a5d661aSToomas Soome s->lookahead -= s->prev_length-1;
1805*4a5d661aSToomas Soome s->prev_length -= 2;
1806*4a5d661aSToomas Soome do {
1807*4a5d661aSToomas Soome if (++s->strstart <= max_insert) {
1808*4a5d661aSToomas Soome INSERT_STRING(s, s->strstart, hash_head);
1809*4a5d661aSToomas Soome }
1810*4a5d661aSToomas Soome } while (--s->prev_length != 0);
1811*4a5d661aSToomas Soome s->match_available = 0;
1812*4a5d661aSToomas Soome s->match_length = MIN_MATCH-1;
1813*4a5d661aSToomas Soome s->strstart++;
1814*4a5d661aSToomas Soome
1815*4a5d661aSToomas Soome if (bflush) FLUSH_BLOCK(s, 0);
1816*4a5d661aSToomas Soome
1817*4a5d661aSToomas Soome } else if (s->match_available) {
1818*4a5d661aSToomas Soome /* If there was no match at the previous position, output a
1819*4a5d661aSToomas Soome * single literal. If there was a match but the current match
1820*4a5d661aSToomas Soome * is longer, truncate the previous match to a single literal.
1821*4a5d661aSToomas Soome */
1822*4a5d661aSToomas Soome Tracevv((stderr,"%c", s->window[s->strstart-1]));
1823*4a5d661aSToomas Soome _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1824*4a5d661aSToomas Soome if (bflush) {
1825*4a5d661aSToomas Soome FLUSH_BLOCK_ONLY(s, 0);
1826*4a5d661aSToomas Soome }
1827*4a5d661aSToomas Soome s->strstart++;
1828*4a5d661aSToomas Soome s->lookahead--;
1829*4a5d661aSToomas Soome if (s->strm->avail_out == 0) return need_more;
1830*4a5d661aSToomas Soome } else {
1831*4a5d661aSToomas Soome /* There is no previous match to compare with, wait for
1832*4a5d661aSToomas Soome * the next step to decide.
1833*4a5d661aSToomas Soome */
1834*4a5d661aSToomas Soome s->match_available = 1;
1835*4a5d661aSToomas Soome s->strstart++;
1836*4a5d661aSToomas Soome s->lookahead--;
1837*4a5d661aSToomas Soome }
1838*4a5d661aSToomas Soome }
1839*4a5d661aSToomas Soome Assert (flush != Z_NO_FLUSH, "no flush?");
1840*4a5d661aSToomas Soome if (s->match_available) {
1841*4a5d661aSToomas Soome Tracevv((stderr,"%c", s->window[s->strstart-1]));
1842*4a5d661aSToomas Soome _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1843*4a5d661aSToomas Soome s->match_available = 0;
1844*4a5d661aSToomas Soome }
1845*4a5d661aSToomas Soome s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1846*4a5d661aSToomas Soome if (flush == Z_FINISH) {
1847*4a5d661aSToomas Soome FLUSH_BLOCK(s, 1);
1848*4a5d661aSToomas Soome return finish_done;
1849*4a5d661aSToomas Soome }
1850*4a5d661aSToomas Soome if (s->last_lit)
1851*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1852*4a5d661aSToomas Soome return block_done;
1853*4a5d661aSToomas Soome }
1854*4a5d661aSToomas Soome #endif /* FASTEST */
1855*4a5d661aSToomas Soome
1856*4a5d661aSToomas Soome /* ===========================================================================
1857*4a5d661aSToomas Soome * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1858*4a5d661aSToomas Soome * one. Do not maintain a hash table. (It will be regenerated if this run of
1859*4a5d661aSToomas Soome * deflate switches away from Z_RLE.)
1860*4a5d661aSToomas Soome */
deflate_rle(s,flush)1861*4a5d661aSToomas Soome local block_state deflate_rle(s, flush)
1862*4a5d661aSToomas Soome deflate_state *s;
1863*4a5d661aSToomas Soome int flush;
1864*4a5d661aSToomas Soome {
1865*4a5d661aSToomas Soome int bflush; /* set if current block must be flushed */
1866*4a5d661aSToomas Soome uInt prev; /* byte at distance one to match */
1867*4a5d661aSToomas Soome Bytef *scan, *strend; /* scan goes up to strend for length of run */
1868*4a5d661aSToomas Soome
1869*4a5d661aSToomas Soome for (;;) {
1870*4a5d661aSToomas Soome /* Make sure that we always have enough lookahead, except
1871*4a5d661aSToomas Soome * at the end of the input file. We need MAX_MATCH bytes
1872*4a5d661aSToomas Soome * for the longest run, plus one for the unrolled loop.
1873*4a5d661aSToomas Soome */
1874*4a5d661aSToomas Soome if (s->lookahead <= MAX_MATCH) {
1875*4a5d661aSToomas Soome fill_window(s);
1876*4a5d661aSToomas Soome if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
1877*4a5d661aSToomas Soome return need_more;
1878*4a5d661aSToomas Soome }
1879*4a5d661aSToomas Soome if (s->lookahead == 0) break; /* flush the current block */
1880*4a5d661aSToomas Soome }
1881*4a5d661aSToomas Soome
1882*4a5d661aSToomas Soome /* See how many times the previous byte repeats */
1883*4a5d661aSToomas Soome s->match_length = 0;
1884*4a5d661aSToomas Soome if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1885*4a5d661aSToomas Soome scan = s->window + s->strstart - 1;
1886*4a5d661aSToomas Soome prev = *scan;
1887*4a5d661aSToomas Soome if (prev == *++scan && prev == *++scan && prev == *++scan) {
1888*4a5d661aSToomas Soome strend = s->window + s->strstart + MAX_MATCH;
1889*4a5d661aSToomas Soome do {
1890*4a5d661aSToomas Soome } while (prev == *++scan && prev == *++scan &&
1891*4a5d661aSToomas Soome prev == *++scan && prev == *++scan &&
1892*4a5d661aSToomas Soome prev == *++scan && prev == *++scan &&
1893*4a5d661aSToomas Soome prev == *++scan && prev == *++scan &&
1894*4a5d661aSToomas Soome scan < strend);
1895*4a5d661aSToomas Soome s->match_length = MAX_MATCH - (int)(strend - scan);
1896*4a5d661aSToomas Soome if (s->match_length > s->lookahead)
1897*4a5d661aSToomas Soome s->match_length = s->lookahead;
1898*4a5d661aSToomas Soome }
1899*4a5d661aSToomas Soome Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
1900*4a5d661aSToomas Soome }
1901*4a5d661aSToomas Soome
1902*4a5d661aSToomas Soome /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1903*4a5d661aSToomas Soome if (s->match_length >= MIN_MATCH) {
1904*4a5d661aSToomas Soome check_match(s, s->strstart, s->strstart - 1, s->match_length);
1905*4a5d661aSToomas Soome
1906*4a5d661aSToomas Soome _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1907*4a5d661aSToomas Soome
1908*4a5d661aSToomas Soome s->lookahead -= s->match_length;
1909*4a5d661aSToomas Soome s->strstart += s->match_length;
1910*4a5d661aSToomas Soome s->match_length = 0;
1911*4a5d661aSToomas Soome } else {
1912*4a5d661aSToomas Soome /* No match, output a literal byte */
1913*4a5d661aSToomas Soome Tracevv((stderr,"%c", s->window[s->strstart]));
1914*4a5d661aSToomas Soome _tr_tally_lit (s, s->window[s->strstart], bflush);
1915*4a5d661aSToomas Soome s->lookahead--;
1916*4a5d661aSToomas Soome s->strstart++;
1917*4a5d661aSToomas Soome }
1918*4a5d661aSToomas Soome if (bflush) FLUSH_BLOCK(s, 0);
1919*4a5d661aSToomas Soome }
1920*4a5d661aSToomas Soome s->insert = 0;
1921*4a5d661aSToomas Soome if (flush == Z_FINISH) {
1922*4a5d661aSToomas Soome FLUSH_BLOCK(s, 1);
1923*4a5d661aSToomas Soome return finish_done;
1924*4a5d661aSToomas Soome }
1925*4a5d661aSToomas Soome if (s->last_lit)
1926*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1927*4a5d661aSToomas Soome return block_done;
1928*4a5d661aSToomas Soome }
1929*4a5d661aSToomas Soome
1930*4a5d661aSToomas Soome /* ===========================================================================
1931*4a5d661aSToomas Soome * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
1932*4a5d661aSToomas Soome * (It will be regenerated if this run of deflate switches away from Huffman.)
1933*4a5d661aSToomas Soome */
deflate_huff(s,flush)1934*4a5d661aSToomas Soome local block_state deflate_huff(s, flush)
1935*4a5d661aSToomas Soome deflate_state *s;
1936*4a5d661aSToomas Soome int flush;
1937*4a5d661aSToomas Soome {
1938*4a5d661aSToomas Soome int bflush; /* set if current block must be flushed */
1939*4a5d661aSToomas Soome
1940*4a5d661aSToomas Soome for (;;) {
1941*4a5d661aSToomas Soome /* Make sure that we have a literal to write. */
1942*4a5d661aSToomas Soome if (s->lookahead == 0) {
1943*4a5d661aSToomas Soome fill_window(s);
1944*4a5d661aSToomas Soome if (s->lookahead == 0) {
1945*4a5d661aSToomas Soome if (flush == Z_NO_FLUSH)
1946*4a5d661aSToomas Soome return need_more;
1947*4a5d661aSToomas Soome break; /* flush the current block */
1948*4a5d661aSToomas Soome }
1949*4a5d661aSToomas Soome }
1950*4a5d661aSToomas Soome
1951*4a5d661aSToomas Soome /* Output a literal byte */
1952*4a5d661aSToomas Soome s->match_length = 0;
1953*4a5d661aSToomas Soome Tracevv((stderr,"%c", s->window[s->strstart]));
1954*4a5d661aSToomas Soome _tr_tally_lit (s, s->window[s->strstart], bflush);
1955*4a5d661aSToomas Soome s->lookahead--;
1956*4a5d661aSToomas Soome s->strstart++;
1957*4a5d661aSToomas Soome if (bflush) FLUSH_BLOCK(s, 0);
1958*4a5d661aSToomas Soome }
1959*4a5d661aSToomas Soome s->insert = 0;
1960*4a5d661aSToomas Soome if (flush == Z_FINISH) {
1961*4a5d661aSToomas Soome FLUSH_BLOCK(s, 1);
1962*4a5d661aSToomas Soome return finish_done;
1963*4a5d661aSToomas Soome }
1964*4a5d661aSToomas Soome if (s->last_lit)
1965*4a5d661aSToomas Soome FLUSH_BLOCK(s, 0);
1966*4a5d661aSToomas Soome return block_done;
1967*4a5d661aSToomas Soome }
1968