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