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