1*3aa7d58aSMatthew Dillon /* deflate.c -- compress data using the deflation algorithm
2*3aa7d58aSMatthew Dillon * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
3*3aa7d58aSMatthew Dillon * For conditions of distribution and use, see copyright notice in zlib.h
4*3aa7d58aSMatthew Dillon */
5*3aa7d58aSMatthew Dillon
6*3aa7d58aSMatthew Dillon /*
7*3aa7d58aSMatthew Dillon * ALGORITHM
8*3aa7d58aSMatthew Dillon *
9*3aa7d58aSMatthew Dillon * The "deflation" process depends on being able to identify portions
10*3aa7d58aSMatthew Dillon * of the input text which are identical to earlier input (within a
11*3aa7d58aSMatthew Dillon * sliding window trailing behind the input currently being processed).
12*3aa7d58aSMatthew Dillon *
13*3aa7d58aSMatthew Dillon * The most straightforward technique turns out to be the fastest for
14*3aa7d58aSMatthew Dillon * most input files: try all possible matches and select the longest.
15*3aa7d58aSMatthew Dillon * The key feature of this algorithm is that insertions into the string
16*3aa7d58aSMatthew Dillon * dictionary are very simple and thus fast, and deletions are avoided
17*3aa7d58aSMatthew Dillon * completely. Insertions are performed at each input character, whereas
18*3aa7d58aSMatthew Dillon * string matches are performed only when the previous match ends. So it
19*3aa7d58aSMatthew Dillon * is preferable to spend more time in matches to allow very fast string
20*3aa7d58aSMatthew Dillon * insertions and avoid deletions. The matching algorithm for small
21*3aa7d58aSMatthew Dillon * strings is inspired from that of Rabin & Karp. A brute force approach
22*3aa7d58aSMatthew Dillon * is used to find longer strings when a small match has been found.
23*3aa7d58aSMatthew Dillon * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24*3aa7d58aSMatthew Dillon * (by Leonid Broukhis).
25*3aa7d58aSMatthew Dillon * A previous version of this file used a more sophisticated algorithm
26*3aa7d58aSMatthew Dillon * (by Fiala and Greene) which is guaranteed to run in linear amortized
27*3aa7d58aSMatthew Dillon * time, but has a larger average cost, uses more memory and is patented.
28*3aa7d58aSMatthew Dillon * However the F&G algorithm may be faster for some highly redundant
29*3aa7d58aSMatthew Dillon * files if the parameter max_chain_length (described below) is too large.
30*3aa7d58aSMatthew Dillon *
31*3aa7d58aSMatthew Dillon * ACKNOWLEDGEMENTS
32*3aa7d58aSMatthew Dillon *
33*3aa7d58aSMatthew Dillon * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34*3aa7d58aSMatthew Dillon * I found it in 'freeze' written by Leonid Broukhis.
35*3aa7d58aSMatthew Dillon * Thanks to many people for bug reports and testing.
36*3aa7d58aSMatthew Dillon *
37*3aa7d58aSMatthew Dillon * REFERENCES
38*3aa7d58aSMatthew Dillon *
39*3aa7d58aSMatthew Dillon * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40*3aa7d58aSMatthew Dillon * Available in http://tools.ietf.org/html/rfc1951
41*3aa7d58aSMatthew Dillon *
42*3aa7d58aSMatthew Dillon * A description of the Rabin and Karp algorithm is given in the book
43*3aa7d58aSMatthew Dillon * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44*3aa7d58aSMatthew Dillon *
45*3aa7d58aSMatthew Dillon * Fiala,E.R., and Greene,D.H.
46*3aa7d58aSMatthew Dillon * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47*3aa7d58aSMatthew Dillon *
48*3aa7d58aSMatthew Dillon */
49*3aa7d58aSMatthew Dillon
50*3aa7d58aSMatthew Dillon /* @(#) $Id$ */
51*3aa7d58aSMatthew Dillon
52*3aa7d58aSMatthew Dillon #include "hammer2_zlib_deflate.h"
53*3aa7d58aSMatthew Dillon #include "../hammer2.h"
54*3aa7d58aSMatthew Dillon
55*3aa7d58aSMatthew Dillon const char deflate_copyright[] =
56*3aa7d58aSMatthew Dillon " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
57*3aa7d58aSMatthew Dillon /*
58*3aa7d58aSMatthew Dillon If you use the zlib library in a product, an acknowledgment is welcome
59*3aa7d58aSMatthew Dillon in the documentation of your product. If for some reason you cannot
60*3aa7d58aSMatthew Dillon include such an acknowledgment, I would appreciate that you keep this
61*3aa7d58aSMatthew Dillon copyright string in the executable of your product.
62*3aa7d58aSMatthew Dillon */
63*3aa7d58aSMatthew Dillon
64*3aa7d58aSMatthew Dillon /* ===========================================================================
65*3aa7d58aSMatthew Dillon * Function prototypes.
66*3aa7d58aSMatthew Dillon */
67*3aa7d58aSMatthew Dillon typedef enum {
68*3aa7d58aSMatthew Dillon need_more, /* block not completed, need more input or more output */
69*3aa7d58aSMatthew Dillon block_done, /* block flush performed */
70*3aa7d58aSMatthew Dillon finish_started, /* finish started, need only more output at next deflate */
71*3aa7d58aSMatthew Dillon finish_done /* finish done, accept no more input or output */
72*3aa7d58aSMatthew Dillon } block_state;
73*3aa7d58aSMatthew Dillon
74*3aa7d58aSMatthew Dillon typedef block_state (*compress_func)(deflate_state *s, int flush);
75*3aa7d58aSMatthew Dillon /* Compression function. Returns the block state after the call. */
76*3aa7d58aSMatthew Dillon
77*3aa7d58aSMatthew Dillon local void fill_window (deflate_state *s);
78*3aa7d58aSMatthew Dillon #ifndef FASTEST
79*3aa7d58aSMatthew Dillon local block_state deflate_slow(deflate_state *s, int flush);
80*3aa7d58aSMatthew Dillon #endif
81*3aa7d58aSMatthew Dillon local block_state deflate_rle(deflate_state *s, int flush);
82*3aa7d58aSMatthew Dillon local block_state deflate_huff(deflate_state *s, int flush);
83*3aa7d58aSMatthew Dillon local void lm_init(deflate_state *s);
84*3aa7d58aSMatthew Dillon local void putShortMSB(deflate_state *s, uInt b);
85*3aa7d58aSMatthew Dillon local void flush_pending(z_streamp strm);
86*3aa7d58aSMatthew Dillon local int read_buf(z_streamp strm, Bytef *buf, unsigned size);
87*3aa7d58aSMatthew Dillon #ifdef ASMV
88*3aa7d58aSMatthew Dillon void match_init(void); /* asm code initialization */
89*3aa7d58aSMatthew Dillon uInt longest_match(deflate_state *s, IPos cur_match);
90*3aa7d58aSMatthew Dillon #else
91*3aa7d58aSMatthew Dillon local uInt longest_match(deflate_state *s, IPos cur_match);
92*3aa7d58aSMatthew Dillon #endif
93*3aa7d58aSMatthew Dillon
94*3aa7d58aSMatthew Dillon #ifdef H2_ZLIB_DEBUG
95*3aa7d58aSMatthew Dillon local void check_match(deflate_state *s, IPos start, IPos match,
96*3aa7d58aSMatthew Dillon int length);
97*3aa7d58aSMatthew Dillon #endif
98*3aa7d58aSMatthew Dillon
99*3aa7d58aSMatthew Dillon int deflateInit2_(z_streamp strm, int level, int method, int windowBits,
100*3aa7d58aSMatthew Dillon int memLevel, int strategy, const char *version,
101*3aa7d58aSMatthew Dillon int stream_size);
102*3aa7d58aSMatthew Dillon int deflateReset (z_streamp strm);
103*3aa7d58aSMatthew Dillon int deflateResetKeep (z_streamp strm);
104*3aa7d58aSMatthew Dillon
105*3aa7d58aSMatthew Dillon /* ===========================================================================
106*3aa7d58aSMatthew Dillon * Local data
107*3aa7d58aSMatthew Dillon */
108*3aa7d58aSMatthew Dillon
109*3aa7d58aSMatthew Dillon #define NIL 0
110*3aa7d58aSMatthew Dillon /* Tail of hash chains */
111*3aa7d58aSMatthew Dillon
112*3aa7d58aSMatthew Dillon #ifndef TOO_FAR
113*3aa7d58aSMatthew Dillon # define TOO_FAR 4096
114*3aa7d58aSMatthew Dillon #endif
115*3aa7d58aSMatthew Dillon /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
116*3aa7d58aSMatthew Dillon
117*3aa7d58aSMatthew Dillon /* Values for max_lazy_match, good_match and max_chain_length, depending on
118*3aa7d58aSMatthew Dillon * the desired pack level (0..9). The values given below have been tuned to
119*3aa7d58aSMatthew Dillon * exclude worst case performance for pathological files. Better values may be
120*3aa7d58aSMatthew Dillon * found for specific files.
121*3aa7d58aSMatthew Dillon */
122*3aa7d58aSMatthew Dillon typedef struct config_s {
123*3aa7d58aSMatthew Dillon ush good_length; /* reduce lazy search above this match length */
124*3aa7d58aSMatthew Dillon ush max_lazy; /* do not perform lazy search above this match length */
125*3aa7d58aSMatthew Dillon ush nice_length; /* quit search above this match length */
126*3aa7d58aSMatthew Dillon ush max_chain;
127*3aa7d58aSMatthew Dillon compress_func func;
128*3aa7d58aSMatthew Dillon } config;
129*3aa7d58aSMatthew Dillon
130*3aa7d58aSMatthew Dillon local const config configuration_table[10] = {
131*3aa7d58aSMatthew Dillon /* good lazy nice chain */
132*3aa7d58aSMatthew Dillon /* 0 */ {0, 0, 0, 0, deflate_slow/*deflate_stored*/}, /* store only */
133*3aa7d58aSMatthew Dillon /* 1 */ {4, 4, 8, 4, deflate_slow/*deflate_fast*/}, /* max speed, no lazy matches */
134*3aa7d58aSMatthew Dillon /* 2 */ {4, 5, 16, 8, deflate_slow/*deflate_fast*/},
135*3aa7d58aSMatthew Dillon /* 3 */ {4, 6, 32, 32, deflate_slow/*deflate_fast*/},
136*3aa7d58aSMatthew Dillon
137*3aa7d58aSMatthew Dillon /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
138*3aa7d58aSMatthew Dillon /* 5 */ {8, 16, 32, 32, deflate_slow},
139*3aa7d58aSMatthew Dillon /* 6 */ {8, 16, 128, 128, deflate_slow},
140*3aa7d58aSMatthew Dillon /* 7 */ {8, 32, 128, 256, deflate_slow},
141*3aa7d58aSMatthew Dillon /* 8 */ {32, 128, 258, 1024, deflate_slow},
142*3aa7d58aSMatthew Dillon /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
143*3aa7d58aSMatthew Dillon
144*3aa7d58aSMatthew Dillon /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
145*3aa7d58aSMatthew Dillon * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
146*3aa7d58aSMatthew Dillon * meaning.
147*3aa7d58aSMatthew Dillon */
148*3aa7d58aSMatthew Dillon
149*3aa7d58aSMatthew Dillon #define EQUAL 0
150*3aa7d58aSMatthew Dillon /* result of memcmp for equal strings */
151*3aa7d58aSMatthew Dillon
152*3aa7d58aSMatthew Dillon #ifndef NO_DUMMY_DECL
153*3aa7d58aSMatthew Dillon struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
154*3aa7d58aSMatthew Dillon #endif
155*3aa7d58aSMatthew Dillon
156*3aa7d58aSMatthew Dillon /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
157*3aa7d58aSMatthew Dillon #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
158*3aa7d58aSMatthew Dillon
159*3aa7d58aSMatthew Dillon /* ===========================================================================
160*3aa7d58aSMatthew Dillon * Update a hash value with the given input byte
161*3aa7d58aSMatthew Dillon * IN assertion: all calls to to UPDATE_HASH are made with consecutive
162*3aa7d58aSMatthew Dillon * input characters, so that a running hash key can be computed from the
163*3aa7d58aSMatthew Dillon * previous key instead of complete recalculation each time.
164*3aa7d58aSMatthew Dillon */
165*3aa7d58aSMatthew Dillon #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
166*3aa7d58aSMatthew Dillon
167*3aa7d58aSMatthew Dillon
168*3aa7d58aSMatthew Dillon /* ===========================================================================
169*3aa7d58aSMatthew Dillon * Insert string str in the dictionary and set match_head to the previous head
170*3aa7d58aSMatthew Dillon * of the hash chain (the most recent string with same hash key). Return
171*3aa7d58aSMatthew Dillon * the previous length of the hash chain.
172*3aa7d58aSMatthew Dillon * If this file is compiled with -DFASTEST, the compression level is forced
173*3aa7d58aSMatthew Dillon * to 1, and no hash chains are maintained.
174*3aa7d58aSMatthew Dillon * IN assertion: all calls to to INSERT_STRING are made with consecutive
175*3aa7d58aSMatthew Dillon * input characters and the first MIN_MATCH bytes of str are valid
176*3aa7d58aSMatthew Dillon * (except for the last MIN_MATCH-1 bytes of the input file).
177*3aa7d58aSMatthew Dillon */
178*3aa7d58aSMatthew Dillon #define INSERT_STRING(s, str, match_head) \
179*3aa7d58aSMatthew Dillon (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
180*3aa7d58aSMatthew Dillon match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
181*3aa7d58aSMatthew Dillon s->head[s->ins_h] = (Pos)(str))
182*3aa7d58aSMatthew Dillon
183*3aa7d58aSMatthew Dillon /* ===========================================================================
184*3aa7d58aSMatthew Dillon * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
185*3aa7d58aSMatthew Dillon * prev[] will be initialized on the fly.
186*3aa7d58aSMatthew Dillon */
187*3aa7d58aSMatthew Dillon #define CLEAR_HASH(s) \
188*3aa7d58aSMatthew Dillon s->head[s->hash_size-1] = NIL; \
189*3aa7d58aSMatthew Dillon zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
190*3aa7d58aSMatthew Dillon
191*3aa7d58aSMatthew Dillon /* ========================================================================= */
192*3aa7d58aSMatthew Dillon int
deflateInit_(z_streamp strm,int level,const char * version,int stream_size)193*3aa7d58aSMatthew Dillon deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
194*3aa7d58aSMatthew Dillon {
195*3aa7d58aSMatthew Dillon return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
196*3aa7d58aSMatthew Dillon Z_DEFAULT_STRATEGY, version, stream_size);
197*3aa7d58aSMatthew Dillon /* To do: ignore strm->next_in if we use it as window */
198*3aa7d58aSMatthew Dillon }
199*3aa7d58aSMatthew Dillon
200*3aa7d58aSMatthew Dillon /* ========================================================================= */
201*3aa7d58aSMatthew Dillon int
deflateInit2_(z_streamp strm,int level,int method,int windowBits,int memLevel,int strategy,const char * version,int stream_size)202*3aa7d58aSMatthew Dillon deflateInit2_(z_streamp strm, int level, int method, int windowBits,
203*3aa7d58aSMatthew Dillon int memLevel, int strategy, const char *version, int stream_size)
204*3aa7d58aSMatthew Dillon {
205*3aa7d58aSMatthew Dillon deflate_state *s;
206*3aa7d58aSMatthew Dillon int wrap = 1;
207*3aa7d58aSMatthew Dillon static const char my_version[] = ZLIB_VERSION;
208*3aa7d58aSMatthew Dillon
209*3aa7d58aSMatthew Dillon ushf *overlay;
210*3aa7d58aSMatthew Dillon /* We overlay pending_buf and d_buf+l_buf. This works since the average
211*3aa7d58aSMatthew Dillon * output size for (length,distance) codes is <= 24 bits.
212*3aa7d58aSMatthew Dillon */
213*3aa7d58aSMatthew Dillon
214*3aa7d58aSMatthew Dillon if (version == Z_NULL || version[0] != my_version[0] ||
215*3aa7d58aSMatthew Dillon stream_size != sizeof(z_stream)) {
216*3aa7d58aSMatthew Dillon return Z_VERSION_ERROR;
217*3aa7d58aSMatthew Dillon }
218*3aa7d58aSMatthew Dillon if (strm == Z_NULL) return Z_STREAM_ERROR;
219*3aa7d58aSMatthew Dillon
220*3aa7d58aSMatthew Dillon strm->msg = Z_NULL;
221*3aa7d58aSMatthew Dillon
222*3aa7d58aSMatthew Dillon if (level == Z_DEFAULT_COMPRESSION) level = 6;
223*3aa7d58aSMatthew Dillon
224*3aa7d58aSMatthew Dillon if (windowBits < 0) { /* suppress zlib wrapper */
225*3aa7d58aSMatthew Dillon wrap = 0;
226*3aa7d58aSMatthew Dillon windowBits = -windowBits;
227*3aa7d58aSMatthew Dillon }
228*3aa7d58aSMatthew Dillon if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
229*3aa7d58aSMatthew Dillon windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
230*3aa7d58aSMatthew Dillon strategy < 0 || strategy > Z_FIXED) {
231*3aa7d58aSMatthew Dillon return Z_STREAM_ERROR;
232*3aa7d58aSMatthew Dillon }
233*3aa7d58aSMatthew Dillon if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
234*3aa7d58aSMatthew Dillon s = (deflate_state *) malloc(sizeof(*s));
235*3aa7d58aSMatthew Dillon if (s == Z_NULL) return Z_MEM_ERROR;
236*3aa7d58aSMatthew Dillon strm->state = (struct internal_state FAR *)s;
237*3aa7d58aSMatthew Dillon s->strm = strm;
238*3aa7d58aSMatthew Dillon
239*3aa7d58aSMatthew Dillon s->wrap = wrap;
240*3aa7d58aSMatthew Dillon s->w_bits = windowBits;
241*3aa7d58aSMatthew Dillon s->w_size = 1 << s->w_bits;
242*3aa7d58aSMatthew Dillon s->w_mask = s->w_size - 1;
243*3aa7d58aSMatthew Dillon
244*3aa7d58aSMatthew Dillon s->hash_bits = memLevel + 7;
245*3aa7d58aSMatthew Dillon s->hash_size = 1 << s->hash_bits;
246*3aa7d58aSMatthew Dillon s->hash_mask = s->hash_size - 1;
247*3aa7d58aSMatthew Dillon s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
248*3aa7d58aSMatthew Dillon
249*3aa7d58aSMatthew Dillon s->window = (Bytef *) malloc((s->w_size)*2*sizeof(Byte));
250*3aa7d58aSMatthew Dillon s->prev = (Posf *) malloc((s->w_size)*sizeof(Pos));
251*3aa7d58aSMatthew Dillon s->head = (Posf *) malloc((s->hash_size)*sizeof(Pos));
252*3aa7d58aSMatthew Dillon
253*3aa7d58aSMatthew Dillon s->high_water = 0; /* nothing written to s->window yet */
254*3aa7d58aSMatthew Dillon
255*3aa7d58aSMatthew Dillon s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
256*3aa7d58aSMatthew Dillon
257*3aa7d58aSMatthew Dillon overlay = (ushf *) malloc((s->lit_bufsize)*(sizeof(ush)+2));
258*3aa7d58aSMatthew Dillon s->pending_buf = (uchf *) overlay;
259*3aa7d58aSMatthew Dillon s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
260*3aa7d58aSMatthew Dillon
261*3aa7d58aSMatthew Dillon if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
262*3aa7d58aSMatthew Dillon s->pending_buf == Z_NULL) {
263*3aa7d58aSMatthew Dillon s->status = FINISH_STATE;
264*3aa7d58aSMatthew Dillon strm->msg = ERR_MSG(Z_MEM_ERROR);
265*3aa7d58aSMatthew Dillon deflateEnd (strm);
266*3aa7d58aSMatthew Dillon return Z_MEM_ERROR;
267*3aa7d58aSMatthew Dillon }
268*3aa7d58aSMatthew Dillon s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
269*3aa7d58aSMatthew Dillon s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
270*3aa7d58aSMatthew Dillon
271*3aa7d58aSMatthew Dillon s->level = level;
272*3aa7d58aSMatthew Dillon s->strategy = strategy;
273*3aa7d58aSMatthew Dillon s->method = (Byte)method;
274*3aa7d58aSMatthew Dillon
275*3aa7d58aSMatthew Dillon return deflateReset(strm);
276*3aa7d58aSMatthew Dillon }
277*3aa7d58aSMatthew Dillon
278*3aa7d58aSMatthew Dillon /* ========================================================================= */
279*3aa7d58aSMatthew Dillon int
deflateResetKeep(z_streamp strm)280*3aa7d58aSMatthew Dillon deflateResetKeep (z_streamp strm)
281*3aa7d58aSMatthew Dillon {
282*3aa7d58aSMatthew Dillon deflate_state *s;
283*3aa7d58aSMatthew Dillon
284*3aa7d58aSMatthew Dillon if (strm == Z_NULL || strm->state == Z_NULL) {
285*3aa7d58aSMatthew Dillon return Z_STREAM_ERROR;
286*3aa7d58aSMatthew Dillon }
287*3aa7d58aSMatthew Dillon
288*3aa7d58aSMatthew Dillon strm->total_in = strm->total_out = 0;
289*3aa7d58aSMatthew Dillon strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
290*3aa7d58aSMatthew Dillon strm->data_type = Z_UNKNOWN;
291*3aa7d58aSMatthew Dillon
292*3aa7d58aSMatthew Dillon s = (deflate_state *)strm->state;
293*3aa7d58aSMatthew Dillon s->pending = 0;
294*3aa7d58aSMatthew Dillon s->pending_out = s->pending_buf;
295*3aa7d58aSMatthew Dillon
296*3aa7d58aSMatthew Dillon if (s->wrap < 0) {
297*3aa7d58aSMatthew Dillon s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
298*3aa7d58aSMatthew Dillon }
299*3aa7d58aSMatthew Dillon s->status = s->wrap ? INIT_STATE : BUSY_STATE;
300*3aa7d58aSMatthew Dillon strm->adler = adler32(0L, Z_NULL, 0);
301*3aa7d58aSMatthew Dillon s->last_flush = Z_NO_FLUSH;
302*3aa7d58aSMatthew Dillon
303*3aa7d58aSMatthew Dillon _tr_init(s);
304*3aa7d58aSMatthew Dillon
305*3aa7d58aSMatthew Dillon return Z_OK;
306*3aa7d58aSMatthew Dillon }
307*3aa7d58aSMatthew Dillon
308*3aa7d58aSMatthew Dillon /* ========================================================================= */
309*3aa7d58aSMatthew Dillon int
deflateReset(z_streamp strm)310*3aa7d58aSMatthew Dillon deflateReset (z_streamp strm)
311*3aa7d58aSMatthew Dillon {
312*3aa7d58aSMatthew Dillon int ret;
313*3aa7d58aSMatthew Dillon
314*3aa7d58aSMatthew Dillon ret = deflateResetKeep(strm);
315*3aa7d58aSMatthew Dillon if (ret == Z_OK)
316*3aa7d58aSMatthew Dillon lm_init(strm->state);
317*3aa7d58aSMatthew Dillon return ret;
318*3aa7d58aSMatthew Dillon }
319*3aa7d58aSMatthew Dillon
320*3aa7d58aSMatthew Dillon /* =========================================================================
321*3aa7d58aSMatthew Dillon * Put a short in the pending buffer. The 16-bit value is put in MSB order.
322*3aa7d58aSMatthew Dillon * IN assertion: the stream state is correct and there is enough room in
323*3aa7d58aSMatthew Dillon * pending_buf.
324*3aa7d58aSMatthew Dillon */
325*3aa7d58aSMatthew Dillon local
326*3aa7d58aSMatthew Dillon void
putShortMSB(deflate_state * s,uInt b)327*3aa7d58aSMatthew Dillon putShortMSB (deflate_state *s, uInt b)
328*3aa7d58aSMatthew Dillon {
329*3aa7d58aSMatthew Dillon put_byte(s, (Byte)(b >> 8));
330*3aa7d58aSMatthew Dillon put_byte(s, (Byte)(b & 0xff));
331*3aa7d58aSMatthew Dillon }
332*3aa7d58aSMatthew Dillon
333*3aa7d58aSMatthew Dillon /* =========================================================================
334*3aa7d58aSMatthew Dillon * Flush as much pending output as possible. All deflate() output goes
335*3aa7d58aSMatthew Dillon * through this function so some applications may wish to modify it
336*3aa7d58aSMatthew Dillon * to avoid allocating a large strm->next_out buffer and copying into it.
337*3aa7d58aSMatthew Dillon * (See also read_buf()).
338*3aa7d58aSMatthew Dillon */
339*3aa7d58aSMatthew Dillon local
340*3aa7d58aSMatthew Dillon void
flush_pending(z_streamp strm)341*3aa7d58aSMatthew Dillon flush_pending(z_streamp strm)
342*3aa7d58aSMatthew Dillon {
343*3aa7d58aSMatthew Dillon unsigned len;
344*3aa7d58aSMatthew Dillon deflate_state *s = strm->state;
345*3aa7d58aSMatthew Dillon
346*3aa7d58aSMatthew Dillon _tr_flush_bits(s);
347*3aa7d58aSMatthew Dillon len = s->pending;
348*3aa7d58aSMatthew Dillon if (len > strm->avail_out) len = strm->avail_out;
349*3aa7d58aSMatthew Dillon if (len == 0) return;
350*3aa7d58aSMatthew Dillon
351*3aa7d58aSMatthew Dillon zmemcpy(strm->next_out, s->pending_out, len);
352*3aa7d58aSMatthew Dillon strm->next_out += len;
353*3aa7d58aSMatthew Dillon s->pending_out += len;
354*3aa7d58aSMatthew Dillon strm->total_out += len;
355*3aa7d58aSMatthew Dillon strm->avail_out -= len;
356*3aa7d58aSMatthew Dillon s->pending -= len;
357*3aa7d58aSMatthew Dillon if (s->pending == 0) {
358*3aa7d58aSMatthew Dillon s->pending_out = s->pending_buf;
359*3aa7d58aSMatthew Dillon }
360*3aa7d58aSMatthew Dillon }
361*3aa7d58aSMatthew Dillon
362*3aa7d58aSMatthew Dillon /* ========================================================================= */
363*3aa7d58aSMatthew Dillon int
deflate(z_streamp strm,int flush)364*3aa7d58aSMatthew Dillon deflate (z_streamp strm, int flush)
365*3aa7d58aSMatthew Dillon {
366*3aa7d58aSMatthew Dillon int old_flush; /* value of flush param for previous deflate call */
367*3aa7d58aSMatthew Dillon deflate_state *s;
368*3aa7d58aSMatthew Dillon
369*3aa7d58aSMatthew Dillon if (strm == Z_NULL || strm->state == Z_NULL ||
370*3aa7d58aSMatthew Dillon flush > Z_BLOCK || flush < 0) {
371*3aa7d58aSMatthew Dillon return Z_STREAM_ERROR;
372*3aa7d58aSMatthew Dillon }
373*3aa7d58aSMatthew Dillon s = strm->state;
374*3aa7d58aSMatthew Dillon
375*3aa7d58aSMatthew Dillon if (strm->next_out == Z_NULL ||
376*3aa7d58aSMatthew Dillon (strm->next_in == Z_NULL && strm->avail_in != 0) ||
377*3aa7d58aSMatthew Dillon (s->status == FINISH_STATE && flush != Z_FINISH)) {
378*3aa7d58aSMatthew Dillon ERR_RETURN(strm, Z_STREAM_ERROR);
379*3aa7d58aSMatthew Dillon }
380*3aa7d58aSMatthew Dillon if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
381*3aa7d58aSMatthew Dillon
382*3aa7d58aSMatthew Dillon s->strm = strm; /* just in case */
383*3aa7d58aSMatthew Dillon old_flush = s->last_flush;
384*3aa7d58aSMatthew Dillon s->last_flush = flush;
385*3aa7d58aSMatthew Dillon
386*3aa7d58aSMatthew Dillon /* Write the header */
387*3aa7d58aSMatthew Dillon uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
388*3aa7d58aSMatthew Dillon uInt level_flags;
389*3aa7d58aSMatthew Dillon
390*3aa7d58aSMatthew Dillon if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
391*3aa7d58aSMatthew Dillon level_flags = 0;
392*3aa7d58aSMatthew Dillon else if (s->level < 6)
393*3aa7d58aSMatthew Dillon level_flags = 1;
394*3aa7d58aSMatthew Dillon else if (s->level == 6)
395*3aa7d58aSMatthew Dillon level_flags = 2;
396*3aa7d58aSMatthew Dillon else
397*3aa7d58aSMatthew Dillon level_flags = 3;
398*3aa7d58aSMatthew Dillon header |= (level_flags << 6);
399*3aa7d58aSMatthew Dillon if (s->strstart != 0) header |= PRESET_DICT;
400*3aa7d58aSMatthew Dillon header += 31 - (header % 31);
401*3aa7d58aSMatthew Dillon
402*3aa7d58aSMatthew Dillon s->status = BUSY_STATE;
403*3aa7d58aSMatthew Dillon putShortMSB(s, header);
404*3aa7d58aSMatthew Dillon
405*3aa7d58aSMatthew Dillon /* Save the adler32 of the preset dictionary: */
406*3aa7d58aSMatthew Dillon if (s->strstart != 0) {
407*3aa7d58aSMatthew Dillon putShortMSB(s, (uInt)(strm->adler >> 16));
408*3aa7d58aSMatthew Dillon putShortMSB(s, (uInt)(strm->adler & 0xffff));
409*3aa7d58aSMatthew Dillon }
410*3aa7d58aSMatthew Dillon strm->adler = adler32(0L, Z_NULL, 0);
411*3aa7d58aSMatthew Dillon
412*3aa7d58aSMatthew Dillon /* Flush as much pending output as possible */
413*3aa7d58aSMatthew Dillon if (s->pending != 0) {
414*3aa7d58aSMatthew Dillon flush_pending(strm);
415*3aa7d58aSMatthew Dillon if (strm->avail_out == 0) {
416*3aa7d58aSMatthew Dillon /* Since avail_out is 0, deflate will be called again with
417*3aa7d58aSMatthew Dillon * more output space, but possibly with both pending and
418*3aa7d58aSMatthew Dillon * avail_in equal to zero. There won't be anything to do,
419*3aa7d58aSMatthew Dillon * but this is not an error situation so make sure we
420*3aa7d58aSMatthew Dillon * return OK instead of BUF_ERROR at next call of deflate:
421*3aa7d58aSMatthew Dillon */
422*3aa7d58aSMatthew Dillon s->last_flush = -1;
423*3aa7d58aSMatthew Dillon return Z_OK;
424*3aa7d58aSMatthew Dillon }
425*3aa7d58aSMatthew Dillon
426*3aa7d58aSMatthew Dillon /* Make sure there is something to do and avoid duplicate consecutive
427*3aa7d58aSMatthew Dillon * flushes. For repeated and useless calls with Z_FINISH, we keep
428*3aa7d58aSMatthew Dillon * returning Z_STREAM_END instead of Z_BUF_ERROR.
429*3aa7d58aSMatthew Dillon */
430*3aa7d58aSMatthew Dillon } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
431*3aa7d58aSMatthew Dillon flush != Z_FINISH) {
432*3aa7d58aSMatthew Dillon ERR_RETURN(strm, Z_BUF_ERROR);
433*3aa7d58aSMatthew Dillon }
434*3aa7d58aSMatthew Dillon
435*3aa7d58aSMatthew Dillon /* User must not provide more input after the first FINISH: */
436*3aa7d58aSMatthew Dillon if (s->status == FINISH_STATE && strm->avail_in != 0) {
437*3aa7d58aSMatthew Dillon ERR_RETURN(strm, Z_BUF_ERROR);
438*3aa7d58aSMatthew Dillon }
439*3aa7d58aSMatthew Dillon
440*3aa7d58aSMatthew Dillon /* Start a new block or continue the current one.
441*3aa7d58aSMatthew Dillon */
442*3aa7d58aSMatthew Dillon if (strm->avail_in != 0 || s->lookahead != 0 ||
443*3aa7d58aSMatthew Dillon (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
444*3aa7d58aSMatthew Dillon block_state bstate;
445*3aa7d58aSMatthew Dillon
446*3aa7d58aSMatthew Dillon bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
447*3aa7d58aSMatthew Dillon (s->strategy == Z_RLE ? deflate_rle(s, flush) :
448*3aa7d58aSMatthew Dillon (*(configuration_table[s->level].func))(s, flush));
449*3aa7d58aSMatthew Dillon
450*3aa7d58aSMatthew Dillon if (bstate == finish_started || bstate == finish_done) {
451*3aa7d58aSMatthew Dillon s->status = FINISH_STATE;
452*3aa7d58aSMatthew Dillon }
453*3aa7d58aSMatthew Dillon if (bstate == need_more || bstate == finish_started) {
454*3aa7d58aSMatthew Dillon if (strm->avail_out == 0) {
455*3aa7d58aSMatthew Dillon s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
456*3aa7d58aSMatthew Dillon }
457*3aa7d58aSMatthew Dillon return Z_OK;
458*3aa7d58aSMatthew Dillon /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
459*3aa7d58aSMatthew Dillon * of deflate should use the same flush parameter to make sure
460*3aa7d58aSMatthew Dillon * that the flush is complete. So we don't have to output an
461*3aa7d58aSMatthew Dillon * empty block here, this will be done at next call. This also
462*3aa7d58aSMatthew Dillon * ensures that for a very small output buffer, we emit at most
463*3aa7d58aSMatthew Dillon * one empty block.
464*3aa7d58aSMatthew Dillon */
465*3aa7d58aSMatthew Dillon }
466*3aa7d58aSMatthew Dillon if (bstate == block_done) {
467*3aa7d58aSMatthew Dillon if (flush == Z_PARTIAL_FLUSH) {
468*3aa7d58aSMatthew Dillon _tr_align(s);
469*3aa7d58aSMatthew Dillon } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
470*3aa7d58aSMatthew Dillon _tr_stored_block(s, (char*)0, 0L, 0);
471*3aa7d58aSMatthew Dillon /* For a full flush, this empty block will be recognized
472*3aa7d58aSMatthew Dillon * as a special marker by inflate_sync().
473*3aa7d58aSMatthew Dillon */
474*3aa7d58aSMatthew Dillon if (flush == Z_FULL_FLUSH) {
475*3aa7d58aSMatthew Dillon CLEAR_HASH(s); /* forget history */
476*3aa7d58aSMatthew Dillon if (s->lookahead == 0) {
477*3aa7d58aSMatthew Dillon s->strstart = 0;
478*3aa7d58aSMatthew Dillon s->block_start = 0L;
479*3aa7d58aSMatthew Dillon s->insert = 0;
480*3aa7d58aSMatthew Dillon }
481*3aa7d58aSMatthew Dillon }
482*3aa7d58aSMatthew Dillon }
483*3aa7d58aSMatthew Dillon flush_pending(strm);
484*3aa7d58aSMatthew Dillon if (strm->avail_out == 0) {
485*3aa7d58aSMatthew Dillon s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
486*3aa7d58aSMatthew Dillon return Z_OK;
487*3aa7d58aSMatthew Dillon }
488*3aa7d58aSMatthew Dillon }
489*3aa7d58aSMatthew Dillon }
490*3aa7d58aSMatthew Dillon Assert(strm->avail_out > 0, "bug2");
491*3aa7d58aSMatthew Dillon
492*3aa7d58aSMatthew Dillon if (flush != Z_FINISH) return Z_OK;
493*3aa7d58aSMatthew Dillon if (s->wrap <= 0) return Z_STREAM_END;
494*3aa7d58aSMatthew Dillon
495*3aa7d58aSMatthew Dillon /* Write the trailer */
496*3aa7d58aSMatthew Dillon putShortMSB(s, (uInt)(strm->adler >> 16));
497*3aa7d58aSMatthew Dillon putShortMSB(s, (uInt)(strm->adler & 0xffff));
498*3aa7d58aSMatthew Dillon
499*3aa7d58aSMatthew Dillon flush_pending(strm);
500*3aa7d58aSMatthew Dillon /* If avail_out is zero, the application will call deflate again
501*3aa7d58aSMatthew Dillon * to flush the rest.
502*3aa7d58aSMatthew Dillon */
503*3aa7d58aSMatthew Dillon if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
504*3aa7d58aSMatthew Dillon return s->pending != 0 ? Z_OK : Z_STREAM_END;
505*3aa7d58aSMatthew Dillon }
506*3aa7d58aSMatthew Dillon
507*3aa7d58aSMatthew Dillon /* ========================================================================= */
508*3aa7d58aSMatthew Dillon int
deflateEnd(z_streamp strm)509*3aa7d58aSMatthew Dillon deflateEnd (z_streamp strm)
510*3aa7d58aSMatthew Dillon {
511*3aa7d58aSMatthew Dillon int status;
512*3aa7d58aSMatthew Dillon
513*3aa7d58aSMatthew Dillon if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
514*3aa7d58aSMatthew Dillon
515*3aa7d58aSMatthew Dillon status = strm->state->status;
516*3aa7d58aSMatthew Dillon if (status != INIT_STATE &&
517*3aa7d58aSMatthew Dillon status != EXTRA_STATE &&
518*3aa7d58aSMatthew Dillon status != NAME_STATE &&
519*3aa7d58aSMatthew Dillon status != COMMENT_STATE &&
520*3aa7d58aSMatthew Dillon status != HCRC_STATE &&
521*3aa7d58aSMatthew Dillon status != BUSY_STATE &&
522*3aa7d58aSMatthew Dillon status != FINISH_STATE) {
523*3aa7d58aSMatthew Dillon return Z_STREAM_ERROR;
524*3aa7d58aSMatthew Dillon }
525*3aa7d58aSMatthew Dillon
526*3aa7d58aSMatthew Dillon /* Deallocate in reverse order of allocations: */
527*3aa7d58aSMatthew Dillon free(strm->state->pending_buf);
528*3aa7d58aSMatthew Dillon free(strm->state->head);
529*3aa7d58aSMatthew Dillon free(strm->state->prev);
530*3aa7d58aSMatthew Dillon free(strm->state->window);
531*3aa7d58aSMatthew Dillon
532*3aa7d58aSMatthew Dillon free(strm->state);
533*3aa7d58aSMatthew Dillon strm->state = Z_NULL;
534*3aa7d58aSMatthew Dillon
535*3aa7d58aSMatthew Dillon return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
536*3aa7d58aSMatthew Dillon }
537*3aa7d58aSMatthew Dillon
538*3aa7d58aSMatthew Dillon /* ===========================================================================
539*3aa7d58aSMatthew Dillon * Read a new buffer from the current input stream, update the adler32
540*3aa7d58aSMatthew Dillon * and total number of bytes read. All deflate() input goes through
541*3aa7d58aSMatthew Dillon * this function so some applications may wish to modify it to avoid
542*3aa7d58aSMatthew Dillon * allocating a large strm->next_in buffer and copying from it.
543*3aa7d58aSMatthew Dillon * (See also flush_pending()).
544*3aa7d58aSMatthew Dillon */
545*3aa7d58aSMatthew Dillon local
546*3aa7d58aSMatthew Dillon int
read_buf(z_streamp strm,Bytef * buf,unsigned size)547*3aa7d58aSMatthew Dillon read_buf(z_streamp strm, Bytef *buf, unsigned size)
548*3aa7d58aSMatthew Dillon {
549*3aa7d58aSMatthew Dillon unsigned len = strm->avail_in;
550*3aa7d58aSMatthew Dillon
551*3aa7d58aSMatthew Dillon if (len > size) len = size;
552*3aa7d58aSMatthew Dillon if (len == 0) return 0;
553*3aa7d58aSMatthew Dillon
554*3aa7d58aSMatthew Dillon strm->avail_in -= len;
555*3aa7d58aSMatthew Dillon
556*3aa7d58aSMatthew Dillon zmemcpy(buf, strm->next_in, len);
557*3aa7d58aSMatthew Dillon if (strm->state->wrap == 1) {
558*3aa7d58aSMatthew Dillon strm->adler = adler32(strm->adler, buf, len);
559*3aa7d58aSMatthew Dillon }
560*3aa7d58aSMatthew Dillon
561*3aa7d58aSMatthew Dillon strm->next_in += len;
562*3aa7d58aSMatthew Dillon strm->total_in += len;
563*3aa7d58aSMatthew Dillon
564*3aa7d58aSMatthew Dillon return (int)len;
565*3aa7d58aSMatthew Dillon }
566*3aa7d58aSMatthew Dillon
567*3aa7d58aSMatthew Dillon /* ===========================================================================
568*3aa7d58aSMatthew Dillon * Initialize the "longest match" routines for a new zlib stream
569*3aa7d58aSMatthew Dillon */
570*3aa7d58aSMatthew Dillon local
571*3aa7d58aSMatthew Dillon void
lm_init(deflate_state * s)572*3aa7d58aSMatthew Dillon lm_init (deflate_state *s)
573*3aa7d58aSMatthew Dillon {
574*3aa7d58aSMatthew Dillon s->window_size = (ulg)2L*s->w_size;
575*3aa7d58aSMatthew Dillon
576*3aa7d58aSMatthew Dillon CLEAR_HASH(s);
577*3aa7d58aSMatthew Dillon
578*3aa7d58aSMatthew Dillon /* Set the default configuration parameters:
579*3aa7d58aSMatthew Dillon */
580*3aa7d58aSMatthew Dillon s->max_lazy_match = configuration_table[s->level].max_lazy;
581*3aa7d58aSMatthew Dillon s->good_match = configuration_table[s->level].good_length;
582*3aa7d58aSMatthew Dillon s->nice_match = configuration_table[s->level].nice_length;
583*3aa7d58aSMatthew Dillon s->max_chain_length = configuration_table[s->level].max_chain;
584*3aa7d58aSMatthew Dillon
585*3aa7d58aSMatthew Dillon s->strstart = 0;
586*3aa7d58aSMatthew Dillon s->block_start = 0L;
587*3aa7d58aSMatthew Dillon s->lookahead = 0;
588*3aa7d58aSMatthew Dillon s->insert = 0;
589*3aa7d58aSMatthew Dillon s->match_length = s->prev_length = MIN_MATCH-1;
590*3aa7d58aSMatthew Dillon s->match_available = 0;
591*3aa7d58aSMatthew Dillon s->ins_h = 0;
592*3aa7d58aSMatthew Dillon #ifndef FASTEST
593*3aa7d58aSMatthew Dillon #ifdef ASMV
594*3aa7d58aSMatthew Dillon match_init(); /* initialize the asm code */
595*3aa7d58aSMatthew Dillon #endif
596*3aa7d58aSMatthew Dillon #endif
597*3aa7d58aSMatthew Dillon }
598*3aa7d58aSMatthew Dillon
599*3aa7d58aSMatthew Dillon #ifndef FASTEST
600*3aa7d58aSMatthew Dillon /* ===========================================================================
601*3aa7d58aSMatthew Dillon * Set match_start to the longest match starting at the given string and
602*3aa7d58aSMatthew Dillon * return its length. Matches shorter or equal to prev_length are discarded,
603*3aa7d58aSMatthew Dillon * in which case the result is equal to prev_length and match_start is
604*3aa7d58aSMatthew Dillon * garbage.
605*3aa7d58aSMatthew Dillon * IN assertions: cur_match is the head of the hash chain for the current
606*3aa7d58aSMatthew Dillon * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
607*3aa7d58aSMatthew Dillon * OUT assertion: the match length is not greater than s->lookahead.
608*3aa7d58aSMatthew Dillon */
609*3aa7d58aSMatthew Dillon #ifndef ASMV
610*3aa7d58aSMatthew Dillon /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
611*3aa7d58aSMatthew Dillon * match.S. The code will be functionally equivalent.
612*3aa7d58aSMatthew Dillon */
613*3aa7d58aSMatthew Dillon local
614*3aa7d58aSMatthew Dillon uInt
longest_match(deflate_state * s,IPos cur_match)615*3aa7d58aSMatthew Dillon longest_match(deflate_state *s, IPos cur_match) /* cur_match = current match */
616*3aa7d58aSMatthew Dillon {
617*3aa7d58aSMatthew Dillon unsigned chain_length = s->max_chain_length;/* max hash chain length */
618*3aa7d58aSMatthew Dillon register Bytef *scan = s->window + s->strstart; /* current string */
619*3aa7d58aSMatthew Dillon register Bytef *match; /* matched string */
620*3aa7d58aSMatthew Dillon register int len; /* length of current match */
621*3aa7d58aSMatthew Dillon int best_len = s->prev_length; /* best match length so far */
622*3aa7d58aSMatthew Dillon int nice_match = s->nice_match; /* stop if match long enough */
623*3aa7d58aSMatthew Dillon IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
624*3aa7d58aSMatthew Dillon s->strstart - (IPos)MAX_DIST(s) : NIL;
625*3aa7d58aSMatthew Dillon /* Stop when cur_match becomes <= limit. To simplify the code,
626*3aa7d58aSMatthew Dillon * we prevent matches with the string of window index 0.
627*3aa7d58aSMatthew Dillon */
628*3aa7d58aSMatthew Dillon Posf *prev = s->prev;
629*3aa7d58aSMatthew Dillon uInt wmask = s->w_mask;
630*3aa7d58aSMatthew Dillon
631*3aa7d58aSMatthew Dillon #ifdef UNALIGNED_OK
632*3aa7d58aSMatthew Dillon /* Compare two bytes at a time. Note: this is not always beneficial.
633*3aa7d58aSMatthew Dillon * Try with and without -DUNALIGNED_OK to check.
634*3aa7d58aSMatthew Dillon */
635*3aa7d58aSMatthew Dillon register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
636*3aa7d58aSMatthew Dillon register ush scan_start = *(ushf*)scan;
637*3aa7d58aSMatthew Dillon register ush scan_end = *(ushf*)(scan+best_len-1);
638*3aa7d58aSMatthew Dillon #else
639*3aa7d58aSMatthew Dillon register Bytef *strend = s->window + s->strstart + MAX_MATCH;
640*3aa7d58aSMatthew Dillon register Byte scan_end1 = scan[best_len-1];
641*3aa7d58aSMatthew Dillon register Byte scan_end = scan[best_len];
642*3aa7d58aSMatthew Dillon #endif
643*3aa7d58aSMatthew Dillon
644*3aa7d58aSMatthew Dillon /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
645*3aa7d58aSMatthew Dillon * It is easy to get rid of this optimization if necessary.
646*3aa7d58aSMatthew Dillon */
647*3aa7d58aSMatthew Dillon Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
648*3aa7d58aSMatthew Dillon
649*3aa7d58aSMatthew Dillon /* Do not waste too much time if we already have a good match: */
650*3aa7d58aSMatthew Dillon if (s->prev_length >= s->good_match) {
651*3aa7d58aSMatthew Dillon chain_length >>= 2;
652*3aa7d58aSMatthew Dillon }
653*3aa7d58aSMatthew Dillon /* Do not look for matches beyond the end of the input. This is necessary
654*3aa7d58aSMatthew Dillon * to make deflate deterministic.
655*3aa7d58aSMatthew Dillon */
656*3aa7d58aSMatthew Dillon if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
657*3aa7d58aSMatthew Dillon
658*3aa7d58aSMatthew Dillon Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
659*3aa7d58aSMatthew Dillon
660*3aa7d58aSMatthew Dillon do {
661*3aa7d58aSMatthew Dillon Assert(cur_match < s->strstart, "no future");
662*3aa7d58aSMatthew Dillon match = s->window + cur_match;
663*3aa7d58aSMatthew Dillon
664*3aa7d58aSMatthew Dillon /* Skip to next match if the match length cannot increase
665*3aa7d58aSMatthew Dillon * or if the match length is less than 2. Note that the checks below
666*3aa7d58aSMatthew Dillon * for insufficient lookahead only occur occasionally for performance
667*3aa7d58aSMatthew Dillon * reasons. Therefore uninitialized memory will be accessed, and
668*3aa7d58aSMatthew Dillon * conditional jumps will be made that depend on those values.
669*3aa7d58aSMatthew Dillon * However the length of the match is limited to the lookahead, so
670*3aa7d58aSMatthew Dillon * the output of deflate is not affected by the uninitialized values.
671*3aa7d58aSMatthew Dillon */
672*3aa7d58aSMatthew Dillon #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
673*3aa7d58aSMatthew Dillon /* This code assumes sizeof(unsigned short) == 2. Do not use
674*3aa7d58aSMatthew Dillon * UNALIGNED_OK if your compiler uses a different size.
675*3aa7d58aSMatthew Dillon */
676*3aa7d58aSMatthew Dillon if (*(ushf*)(match+best_len-1) != scan_end ||
677*3aa7d58aSMatthew Dillon *(ushf*)match != scan_start) continue;
678*3aa7d58aSMatthew Dillon
679*3aa7d58aSMatthew Dillon /* It is not necessary to compare scan[2] and match[2] since they are
680*3aa7d58aSMatthew Dillon * always equal when the other bytes match, given that the hash keys
681*3aa7d58aSMatthew Dillon * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
682*3aa7d58aSMatthew Dillon * strstart+3, +5, ... up to strstart+257. We check for insufficient
683*3aa7d58aSMatthew Dillon * lookahead only every 4th comparison; the 128th check will be made
684*3aa7d58aSMatthew Dillon * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
685*3aa7d58aSMatthew Dillon * necessary to put more guard bytes at the end of the window, or
686*3aa7d58aSMatthew Dillon * to check more often for insufficient lookahead.
687*3aa7d58aSMatthew Dillon */
688*3aa7d58aSMatthew Dillon Assert(scan[2] == match[2], "scan[2]?");
689*3aa7d58aSMatthew Dillon scan++, match++;
690*3aa7d58aSMatthew Dillon do {
691*3aa7d58aSMatthew Dillon } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
692*3aa7d58aSMatthew Dillon *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
693*3aa7d58aSMatthew Dillon *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
694*3aa7d58aSMatthew Dillon *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
695*3aa7d58aSMatthew Dillon scan < strend);
696*3aa7d58aSMatthew Dillon /* The funny "do {}" generates better code on most compilers */
697*3aa7d58aSMatthew Dillon
698*3aa7d58aSMatthew Dillon /* Here, scan <= window+strstart+257 */
699*3aa7d58aSMatthew Dillon Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
700*3aa7d58aSMatthew Dillon if (*scan == *match) scan++;
701*3aa7d58aSMatthew Dillon
702*3aa7d58aSMatthew Dillon len = (MAX_MATCH - 1) - (int)(strend-scan);
703*3aa7d58aSMatthew Dillon scan = strend - (MAX_MATCH-1);
704*3aa7d58aSMatthew Dillon
705*3aa7d58aSMatthew Dillon #else /* UNALIGNED_OK */
706*3aa7d58aSMatthew Dillon
707*3aa7d58aSMatthew Dillon if (match[best_len] != scan_end ||
708*3aa7d58aSMatthew Dillon match[best_len-1] != scan_end1 ||
709*3aa7d58aSMatthew Dillon *match != *scan ||
710*3aa7d58aSMatthew Dillon *++match != scan[1]) continue;
711*3aa7d58aSMatthew Dillon
712*3aa7d58aSMatthew Dillon /* The check at best_len-1 can be removed because it will be made
713*3aa7d58aSMatthew Dillon * again later. (This heuristic is not always a win.)
714*3aa7d58aSMatthew Dillon * It is not necessary to compare scan[2] and match[2] since they
715*3aa7d58aSMatthew Dillon * are always equal when the other bytes match, given that
716*3aa7d58aSMatthew Dillon * the hash keys are equal and that HASH_BITS >= 8.
717*3aa7d58aSMatthew Dillon */
718*3aa7d58aSMatthew Dillon scan += 2, match++;
719*3aa7d58aSMatthew Dillon Assert(*scan == *match, "match[2]?");
720*3aa7d58aSMatthew Dillon
721*3aa7d58aSMatthew Dillon /* We check for insufficient lookahead only every 8th comparison;
722*3aa7d58aSMatthew Dillon * the 256th check will be made at strstart+258.
723*3aa7d58aSMatthew Dillon */
724*3aa7d58aSMatthew Dillon do {
725*3aa7d58aSMatthew Dillon } while (*++scan == *++match && *++scan == *++match &&
726*3aa7d58aSMatthew Dillon *++scan == *++match && *++scan == *++match &&
727*3aa7d58aSMatthew Dillon *++scan == *++match && *++scan == *++match &&
728*3aa7d58aSMatthew Dillon *++scan == *++match && *++scan == *++match &&
729*3aa7d58aSMatthew Dillon scan < strend);
730*3aa7d58aSMatthew Dillon
731*3aa7d58aSMatthew Dillon Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
732*3aa7d58aSMatthew Dillon
733*3aa7d58aSMatthew Dillon len = MAX_MATCH - (int)(strend - scan);
734*3aa7d58aSMatthew Dillon scan = strend - MAX_MATCH;
735*3aa7d58aSMatthew Dillon
736*3aa7d58aSMatthew Dillon #endif /* UNALIGNED_OK */
737*3aa7d58aSMatthew Dillon
738*3aa7d58aSMatthew Dillon if (len > best_len) {
739*3aa7d58aSMatthew Dillon s->match_start = cur_match;
740*3aa7d58aSMatthew Dillon best_len = len;
741*3aa7d58aSMatthew Dillon if (len >= nice_match) break;
742*3aa7d58aSMatthew Dillon #ifdef UNALIGNED_OK
743*3aa7d58aSMatthew Dillon scan_end = *(ushf*)(scan+best_len-1);
744*3aa7d58aSMatthew Dillon #else
745*3aa7d58aSMatthew Dillon scan_end1 = scan[best_len-1];
746*3aa7d58aSMatthew Dillon scan_end = scan[best_len];
747*3aa7d58aSMatthew Dillon #endif
748*3aa7d58aSMatthew Dillon }
749*3aa7d58aSMatthew Dillon } while ((cur_match = prev[cur_match & wmask]) > limit
750*3aa7d58aSMatthew Dillon && --chain_length != 0);
751*3aa7d58aSMatthew Dillon
752*3aa7d58aSMatthew Dillon if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
753*3aa7d58aSMatthew Dillon return s->lookahead;
754*3aa7d58aSMatthew Dillon }
755*3aa7d58aSMatthew Dillon #endif /* ASMV */
756*3aa7d58aSMatthew Dillon
757*3aa7d58aSMatthew Dillon #endif /* FASTEST */
758*3aa7d58aSMatthew Dillon
759*3aa7d58aSMatthew Dillon #ifdef H2_ZLIB_DEBUG
760*3aa7d58aSMatthew Dillon /* ===========================================================================
761*3aa7d58aSMatthew Dillon * Check that the match at match_start is indeed a match.
762*3aa7d58aSMatthew Dillon */
763*3aa7d58aSMatthew Dillon local
764*3aa7d58aSMatthew Dillon void
check_match(deflate_state * s,IPos start,IPos match,int length)765*3aa7d58aSMatthew Dillon check_match(deflate_state *s, IPos start, IPos match, int length)
766*3aa7d58aSMatthew Dillon {
767*3aa7d58aSMatthew Dillon /* check that the match is indeed a match */
768*3aa7d58aSMatthew Dillon if (zmemcmp(s->window + match,
769*3aa7d58aSMatthew Dillon s->window + start, length) != EQUAL) {
770*3aa7d58aSMatthew Dillon fprintf(stderr, " start %u, match %u, length %d\n",
771*3aa7d58aSMatthew Dillon start, match, length);
772*3aa7d58aSMatthew Dillon do {
773*3aa7d58aSMatthew Dillon fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
774*3aa7d58aSMatthew Dillon } while (--length != 0);
775*3aa7d58aSMatthew Dillon z_error("invalid match");
776*3aa7d58aSMatthew Dillon }
777*3aa7d58aSMatthew Dillon if (z_verbose > 1) {
778*3aa7d58aSMatthew Dillon fprintf(stderr,"\\[%d,%d]", start-match, length);
779*3aa7d58aSMatthew Dillon do { putc(s->window[start++], stderr); } while (--length != 0);
780*3aa7d58aSMatthew Dillon }
781*3aa7d58aSMatthew Dillon }
782*3aa7d58aSMatthew Dillon #else
783*3aa7d58aSMatthew Dillon # define check_match(s, start, match, length)
784*3aa7d58aSMatthew Dillon #endif /* H2_ZLIB_DEBUG */
785*3aa7d58aSMatthew Dillon
786*3aa7d58aSMatthew Dillon /* ===========================================================================
787*3aa7d58aSMatthew Dillon * Fill the window when the lookahead becomes insufficient.
788*3aa7d58aSMatthew Dillon * Updates strstart and lookahead.
789*3aa7d58aSMatthew Dillon *
790*3aa7d58aSMatthew Dillon * IN assertion: lookahead < MIN_LOOKAHEAD
791*3aa7d58aSMatthew Dillon * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
792*3aa7d58aSMatthew Dillon * At least one byte has been read, or avail_in == 0; reads are
793*3aa7d58aSMatthew Dillon * performed for at least two bytes (required for the zip translate_eol
794*3aa7d58aSMatthew Dillon * option -- not supported here).
795*3aa7d58aSMatthew Dillon */
796*3aa7d58aSMatthew Dillon local
797*3aa7d58aSMatthew Dillon void
fill_window(deflate_state * s)798*3aa7d58aSMatthew Dillon fill_window(deflate_state *s)
799*3aa7d58aSMatthew Dillon {
800*3aa7d58aSMatthew Dillon register unsigned n, m;
801*3aa7d58aSMatthew Dillon register Posf *p;
802*3aa7d58aSMatthew Dillon unsigned more; /* Amount of free space at the end of the window. */
803*3aa7d58aSMatthew Dillon uInt wsize = s->w_size;
804*3aa7d58aSMatthew Dillon
805*3aa7d58aSMatthew Dillon Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
806*3aa7d58aSMatthew Dillon
807*3aa7d58aSMatthew Dillon do {
808*3aa7d58aSMatthew Dillon more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
809*3aa7d58aSMatthew Dillon
810*3aa7d58aSMatthew Dillon /* Deal with !@#$% 64K limit: */
811*3aa7d58aSMatthew Dillon if (sizeof(int) <= 2) {
812*3aa7d58aSMatthew Dillon if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
813*3aa7d58aSMatthew Dillon more = wsize;
814*3aa7d58aSMatthew Dillon
815*3aa7d58aSMatthew Dillon } else if (more == (unsigned)(-1)) {
816*3aa7d58aSMatthew Dillon /* Very unlikely, but possible on 16 bit machine if
817*3aa7d58aSMatthew Dillon * strstart == 0 && lookahead == 1 (input done a byte at time)
818*3aa7d58aSMatthew Dillon */
819*3aa7d58aSMatthew Dillon more--;
820*3aa7d58aSMatthew Dillon }
821*3aa7d58aSMatthew Dillon }
822*3aa7d58aSMatthew Dillon
823*3aa7d58aSMatthew Dillon /* If the window is almost full and there is insufficient lookahead,
824*3aa7d58aSMatthew Dillon * move the upper half to the lower one to make room in the upper half.
825*3aa7d58aSMatthew Dillon */
826*3aa7d58aSMatthew Dillon if (s->strstart >= wsize+MAX_DIST(s)) {
827*3aa7d58aSMatthew Dillon
828*3aa7d58aSMatthew Dillon zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
829*3aa7d58aSMatthew Dillon s->match_start -= wsize;
830*3aa7d58aSMatthew Dillon s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
831*3aa7d58aSMatthew Dillon s->block_start -= (long) wsize;
832*3aa7d58aSMatthew Dillon
833*3aa7d58aSMatthew Dillon /* Slide the hash table (could be avoided with 32 bit values
834*3aa7d58aSMatthew Dillon at the expense of memory usage). We slide even when level == 0
835*3aa7d58aSMatthew Dillon to keep the hash table consistent if we switch back to level > 0
836*3aa7d58aSMatthew Dillon later. (Using level 0 permanently is not an optimal usage of
837*3aa7d58aSMatthew Dillon zlib, so we don't care about this pathological case.)
838*3aa7d58aSMatthew Dillon */
839*3aa7d58aSMatthew Dillon n = s->hash_size;
840*3aa7d58aSMatthew Dillon p = &s->head[n];
841*3aa7d58aSMatthew Dillon do {
842*3aa7d58aSMatthew Dillon m = *--p;
843*3aa7d58aSMatthew Dillon *p = (Pos)(m >= wsize ? m-wsize : NIL);
844*3aa7d58aSMatthew Dillon } while (--n);
845*3aa7d58aSMatthew Dillon
846*3aa7d58aSMatthew Dillon n = wsize;
847*3aa7d58aSMatthew Dillon #ifndef FASTEST
848*3aa7d58aSMatthew Dillon p = &s->prev[n];
849*3aa7d58aSMatthew Dillon do {
850*3aa7d58aSMatthew Dillon m = *--p;
851*3aa7d58aSMatthew Dillon *p = (Pos)(m >= wsize ? m-wsize : NIL);
852*3aa7d58aSMatthew Dillon /* If n is not on any hash chain, prev[n] is garbage but
853*3aa7d58aSMatthew Dillon * its value will never be used.
854*3aa7d58aSMatthew Dillon */
855*3aa7d58aSMatthew Dillon } while (--n);
856*3aa7d58aSMatthew Dillon #endif
857*3aa7d58aSMatthew Dillon more += wsize;
858*3aa7d58aSMatthew Dillon }
859*3aa7d58aSMatthew Dillon if (s->strm->avail_in == 0) break;
860*3aa7d58aSMatthew Dillon
861*3aa7d58aSMatthew Dillon /* If there was no sliding:
862*3aa7d58aSMatthew Dillon * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
863*3aa7d58aSMatthew Dillon * more == window_size - lookahead - strstart
864*3aa7d58aSMatthew Dillon * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
865*3aa7d58aSMatthew Dillon * => more >= window_size - 2*WSIZE + 2
866*3aa7d58aSMatthew Dillon * In the BIG_MEM or MMAP case (not yet supported),
867*3aa7d58aSMatthew Dillon * window_size == input_size + MIN_LOOKAHEAD &&
868*3aa7d58aSMatthew Dillon * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
869*3aa7d58aSMatthew Dillon * Otherwise, window_size == 2*WSIZE so more >= 2.
870*3aa7d58aSMatthew Dillon * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
871*3aa7d58aSMatthew Dillon */
872*3aa7d58aSMatthew Dillon Assert(more >= 2, "more < 2");
873*3aa7d58aSMatthew Dillon
874*3aa7d58aSMatthew Dillon n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
875*3aa7d58aSMatthew Dillon s->lookahead += n;
876*3aa7d58aSMatthew Dillon
877*3aa7d58aSMatthew Dillon /* Initialize the hash value now that we have some input: */
878*3aa7d58aSMatthew Dillon if (s->lookahead + s->insert >= MIN_MATCH) {
879*3aa7d58aSMatthew Dillon uInt str = s->strstart - s->insert;
880*3aa7d58aSMatthew Dillon s->ins_h = s->window[str];
881*3aa7d58aSMatthew Dillon UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
882*3aa7d58aSMatthew Dillon #if MIN_MATCH != 3
883*3aa7d58aSMatthew Dillon Call UPDATE_HASH() MIN_MATCH-3 more times
884*3aa7d58aSMatthew Dillon #endif
885*3aa7d58aSMatthew Dillon while (s->insert) {
886*3aa7d58aSMatthew Dillon UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
887*3aa7d58aSMatthew Dillon #ifndef FASTEST
888*3aa7d58aSMatthew Dillon s->prev[str & s->w_mask] = s->head[s->ins_h];
889*3aa7d58aSMatthew Dillon #endif
890*3aa7d58aSMatthew Dillon s->head[s->ins_h] = (Pos)str;
891*3aa7d58aSMatthew Dillon str++;
892*3aa7d58aSMatthew Dillon s->insert--;
893*3aa7d58aSMatthew Dillon if (s->lookahead + s->insert < MIN_MATCH)
894*3aa7d58aSMatthew Dillon break;
895*3aa7d58aSMatthew Dillon }
896*3aa7d58aSMatthew Dillon }
897*3aa7d58aSMatthew Dillon /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
898*3aa7d58aSMatthew Dillon * but this is not important since only literal bytes will be emitted.
899*3aa7d58aSMatthew Dillon */
900*3aa7d58aSMatthew Dillon
901*3aa7d58aSMatthew Dillon } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
902*3aa7d58aSMatthew Dillon
903*3aa7d58aSMatthew Dillon /* If the WIN_INIT bytes after the end of the current data have never been
904*3aa7d58aSMatthew Dillon * written, then zero those bytes in order to avoid memory check reports of
905*3aa7d58aSMatthew Dillon * the use of uninitialized (or uninitialised as Julian writes) bytes by
906*3aa7d58aSMatthew Dillon * the longest match routines. Update the high water mark for the next
907*3aa7d58aSMatthew Dillon * time through here. WIN_INIT is set to MAX_MATCH since the longest match
908*3aa7d58aSMatthew Dillon * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
909*3aa7d58aSMatthew Dillon */
910*3aa7d58aSMatthew Dillon if (s->high_water < s->window_size) {
911*3aa7d58aSMatthew Dillon ulg curr = s->strstart + (ulg)(s->lookahead);
912*3aa7d58aSMatthew Dillon ulg init;
913*3aa7d58aSMatthew Dillon
914*3aa7d58aSMatthew Dillon if (s->high_water < curr) {
915*3aa7d58aSMatthew Dillon /* Previous high water mark below current data -- zero WIN_INIT
916*3aa7d58aSMatthew Dillon * bytes or up to end of window, whichever is less.
917*3aa7d58aSMatthew Dillon */
918*3aa7d58aSMatthew Dillon init = s->window_size - curr;
919*3aa7d58aSMatthew Dillon if (init > WIN_INIT)
920*3aa7d58aSMatthew Dillon init = WIN_INIT;
921*3aa7d58aSMatthew Dillon zmemzero(s->window + curr, (unsigned)init);
922*3aa7d58aSMatthew Dillon s->high_water = curr + init;
923*3aa7d58aSMatthew Dillon }
924*3aa7d58aSMatthew Dillon else if (s->high_water < (ulg)curr + WIN_INIT) {
925*3aa7d58aSMatthew Dillon /* High water mark at or above current data, but below current data
926*3aa7d58aSMatthew Dillon * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
927*3aa7d58aSMatthew Dillon * to end of window, whichever is less.
928*3aa7d58aSMatthew Dillon */
929*3aa7d58aSMatthew Dillon init = (ulg)curr + WIN_INIT - s->high_water;
930*3aa7d58aSMatthew Dillon if (init > s->window_size - s->high_water)
931*3aa7d58aSMatthew Dillon init = s->window_size - s->high_water;
932*3aa7d58aSMatthew Dillon zmemzero(s->window + s->high_water, (unsigned)init);
933*3aa7d58aSMatthew Dillon s->high_water += init;
934*3aa7d58aSMatthew Dillon }
935*3aa7d58aSMatthew Dillon }
936*3aa7d58aSMatthew Dillon
937*3aa7d58aSMatthew Dillon Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
938*3aa7d58aSMatthew Dillon "not enough room for search");
939*3aa7d58aSMatthew Dillon }
940*3aa7d58aSMatthew Dillon
941*3aa7d58aSMatthew Dillon /* ===========================================================================
942*3aa7d58aSMatthew Dillon * Flush the current block, with given end-of-file flag.
943*3aa7d58aSMatthew Dillon * IN assertion: strstart is set to the end of the current match.
944*3aa7d58aSMatthew Dillon */
945*3aa7d58aSMatthew Dillon #define FLUSH_BLOCK_ONLY(s, last) { \
946*3aa7d58aSMatthew Dillon _tr_flush_block(s, (s->block_start >= 0L ? \
947*3aa7d58aSMatthew Dillon (charf *)&s->window[(unsigned)s->block_start] : \
948*3aa7d58aSMatthew Dillon (charf *)Z_NULL), \
949*3aa7d58aSMatthew Dillon (ulg)((long)s->strstart - s->block_start), \
950*3aa7d58aSMatthew Dillon (last)); \
951*3aa7d58aSMatthew Dillon s->block_start = s->strstart; \
952*3aa7d58aSMatthew Dillon flush_pending(s->strm); \
953*3aa7d58aSMatthew Dillon Tracev((stderr,"[FLUSH]")); \
954*3aa7d58aSMatthew Dillon }
955*3aa7d58aSMatthew Dillon
956*3aa7d58aSMatthew Dillon /* Same but force premature exit if necessary. */
957*3aa7d58aSMatthew Dillon #define FLUSH_BLOCK(s, last) { \
958*3aa7d58aSMatthew Dillon FLUSH_BLOCK_ONLY(s, last); \
959*3aa7d58aSMatthew Dillon if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
960*3aa7d58aSMatthew Dillon }
961*3aa7d58aSMatthew Dillon
962*3aa7d58aSMatthew Dillon #ifndef FASTEST
963*3aa7d58aSMatthew Dillon /* ===========================================================================
964*3aa7d58aSMatthew Dillon * Same as above, but achieves better compression. We use a lazy
965*3aa7d58aSMatthew Dillon * evaluation for matches: a match is finally adopted only if there is
966*3aa7d58aSMatthew Dillon * no better match at the next window position.
967*3aa7d58aSMatthew Dillon */
968*3aa7d58aSMatthew Dillon local
969*3aa7d58aSMatthew Dillon block_state
deflate_slow(deflate_state * s,int flush)970*3aa7d58aSMatthew Dillon deflate_slow(deflate_state *s, int flush)
971*3aa7d58aSMatthew Dillon {
972*3aa7d58aSMatthew Dillon IPos hash_head; /* head of hash chain */
973*3aa7d58aSMatthew Dillon int bflush; /* set if current block must be flushed */
974*3aa7d58aSMatthew Dillon
975*3aa7d58aSMatthew Dillon /* Process the input block. */
976*3aa7d58aSMatthew Dillon for (;;) {
977*3aa7d58aSMatthew Dillon /* Make sure that we always have enough lookahead, except
978*3aa7d58aSMatthew Dillon * at the end of the input file. We need MAX_MATCH bytes
979*3aa7d58aSMatthew Dillon * for the next match, plus MIN_MATCH bytes to insert the
980*3aa7d58aSMatthew Dillon * string following the next match.
981*3aa7d58aSMatthew Dillon */
982*3aa7d58aSMatthew Dillon if (s->lookahead < MIN_LOOKAHEAD) {
983*3aa7d58aSMatthew Dillon fill_window(s);
984*3aa7d58aSMatthew Dillon if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
985*3aa7d58aSMatthew Dillon return need_more;
986*3aa7d58aSMatthew Dillon }
987*3aa7d58aSMatthew Dillon if (s->lookahead == 0) break; /* flush the current block */
988*3aa7d58aSMatthew Dillon }
989*3aa7d58aSMatthew Dillon
990*3aa7d58aSMatthew Dillon /* Insert the string window[strstart .. strstart+2] in the
991*3aa7d58aSMatthew Dillon * dictionary, and set hash_head to the head of the hash chain:
992*3aa7d58aSMatthew Dillon */
993*3aa7d58aSMatthew Dillon hash_head = NIL;
994*3aa7d58aSMatthew Dillon if (s->lookahead >= MIN_MATCH) {
995*3aa7d58aSMatthew Dillon INSERT_STRING(s, s->strstart, hash_head);
996*3aa7d58aSMatthew Dillon }
997*3aa7d58aSMatthew Dillon
998*3aa7d58aSMatthew Dillon /* Find the longest match, discarding those <= prev_length.
999*3aa7d58aSMatthew Dillon */
1000*3aa7d58aSMatthew Dillon s->prev_length = s->match_length, s->prev_match = s->match_start;
1001*3aa7d58aSMatthew Dillon s->match_length = MIN_MATCH-1;
1002*3aa7d58aSMatthew Dillon
1003*3aa7d58aSMatthew Dillon if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1004*3aa7d58aSMatthew Dillon s->strstart - hash_head <= MAX_DIST(s)) {
1005*3aa7d58aSMatthew Dillon /* To simplify the code, we prevent matches with the string
1006*3aa7d58aSMatthew Dillon * of window index 0 (in particular we have to avoid a match
1007*3aa7d58aSMatthew Dillon * of the string with itself at the start of the input file).
1008*3aa7d58aSMatthew Dillon */
1009*3aa7d58aSMatthew Dillon s->match_length = longest_match (s, hash_head);
1010*3aa7d58aSMatthew Dillon /* longest_match() sets match_start */
1011*3aa7d58aSMatthew Dillon
1012*3aa7d58aSMatthew Dillon if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1013*3aa7d58aSMatthew Dillon #if TOO_FAR <= 32767
1014*3aa7d58aSMatthew Dillon || (s->match_length == MIN_MATCH &&
1015*3aa7d58aSMatthew Dillon s->strstart - s->match_start > TOO_FAR)
1016*3aa7d58aSMatthew Dillon #endif
1017*3aa7d58aSMatthew Dillon )) {
1018*3aa7d58aSMatthew Dillon
1019*3aa7d58aSMatthew Dillon /* If prev_match is also MIN_MATCH, match_start is garbage
1020*3aa7d58aSMatthew Dillon * but we will ignore the current match anyway.
1021*3aa7d58aSMatthew Dillon */
1022*3aa7d58aSMatthew Dillon s->match_length = MIN_MATCH-1;
1023*3aa7d58aSMatthew Dillon }
1024*3aa7d58aSMatthew Dillon }
1025*3aa7d58aSMatthew Dillon /* If there was a match at the previous step and the current
1026*3aa7d58aSMatthew Dillon * match is not better, output the previous match:
1027*3aa7d58aSMatthew Dillon */
1028*3aa7d58aSMatthew Dillon if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1029*3aa7d58aSMatthew Dillon uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1030*3aa7d58aSMatthew Dillon /* Do not insert strings in hash table beyond this. */
1031*3aa7d58aSMatthew Dillon
1032*3aa7d58aSMatthew Dillon check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1033*3aa7d58aSMatthew Dillon
1034*3aa7d58aSMatthew Dillon _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1035*3aa7d58aSMatthew Dillon s->prev_length - MIN_MATCH, bflush);
1036*3aa7d58aSMatthew Dillon
1037*3aa7d58aSMatthew Dillon /* Insert in hash table all strings up to the end of the match.
1038*3aa7d58aSMatthew Dillon * strstart-1 and strstart are already inserted. If there is not
1039*3aa7d58aSMatthew Dillon * enough lookahead, the last two strings are not inserted in
1040*3aa7d58aSMatthew Dillon * the hash table.
1041*3aa7d58aSMatthew Dillon */
1042*3aa7d58aSMatthew Dillon s->lookahead -= s->prev_length-1;
1043*3aa7d58aSMatthew Dillon s->prev_length -= 2;
1044*3aa7d58aSMatthew Dillon do {
1045*3aa7d58aSMatthew Dillon if (++s->strstart <= max_insert) {
1046*3aa7d58aSMatthew Dillon INSERT_STRING(s, s->strstart, hash_head);
1047*3aa7d58aSMatthew Dillon }
1048*3aa7d58aSMatthew Dillon } while (--s->prev_length != 0);
1049*3aa7d58aSMatthew Dillon s->match_available = 0;
1050*3aa7d58aSMatthew Dillon s->match_length = MIN_MATCH-1;
1051*3aa7d58aSMatthew Dillon s->strstart++;
1052*3aa7d58aSMatthew Dillon
1053*3aa7d58aSMatthew Dillon if (bflush) FLUSH_BLOCK(s, 0);
1054*3aa7d58aSMatthew Dillon
1055*3aa7d58aSMatthew Dillon } else if (s->match_available) {
1056*3aa7d58aSMatthew Dillon /* If there was no match at the previous position, output a
1057*3aa7d58aSMatthew Dillon * single literal. If there was a match but the current match
1058*3aa7d58aSMatthew Dillon * is longer, truncate the previous match to a single literal.
1059*3aa7d58aSMatthew Dillon */
1060*3aa7d58aSMatthew Dillon Tracevv((stderr,"%c", s->window[s->strstart-1]));
1061*3aa7d58aSMatthew Dillon _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1062*3aa7d58aSMatthew Dillon if (bflush) {
1063*3aa7d58aSMatthew Dillon FLUSH_BLOCK_ONLY(s, 0);
1064*3aa7d58aSMatthew Dillon }
1065*3aa7d58aSMatthew Dillon s->strstart++;
1066*3aa7d58aSMatthew Dillon s->lookahead--;
1067*3aa7d58aSMatthew Dillon if (s->strm->avail_out == 0) return need_more;
1068*3aa7d58aSMatthew Dillon } else {
1069*3aa7d58aSMatthew Dillon /* There is no previous match to compare with, wait for
1070*3aa7d58aSMatthew Dillon * the next step to decide.
1071*3aa7d58aSMatthew Dillon */
1072*3aa7d58aSMatthew Dillon s->match_available = 1;
1073*3aa7d58aSMatthew Dillon s->strstart++;
1074*3aa7d58aSMatthew Dillon s->lookahead--;
1075*3aa7d58aSMatthew Dillon }
1076*3aa7d58aSMatthew Dillon }
1077*3aa7d58aSMatthew Dillon Assert (flush != Z_NO_FLUSH, "no flush?");
1078*3aa7d58aSMatthew Dillon if (s->match_available) {
1079*3aa7d58aSMatthew Dillon Tracevv((stderr,"%c", s->window[s->strstart-1]));
1080*3aa7d58aSMatthew Dillon _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1081*3aa7d58aSMatthew Dillon s->match_available = 0;
1082*3aa7d58aSMatthew Dillon }
1083*3aa7d58aSMatthew Dillon s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1084*3aa7d58aSMatthew Dillon if (flush == Z_FINISH) {
1085*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 1);
1086*3aa7d58aSMatthew Dillon return finish_done;
1087*3aa7d58aSMatthew Dillon }
1088*3aa7d58aSMatthew Dillon if (s->last_lit)
1089*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 0);
1090*3aa7d58aSMatthew Dillon return block_done;
1091*3aa7d58aSMatthew Dillon }
1092*3aa7d58aSMatthew Dillon #endif /* FASTEST */
1093*3aa7d58aSMatthew Dillon
1094*3aa7d58aSMatthew Dillon /* ===========================================================================
1095*3aa7d58aSMatthew Dillon * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1096*3aa7d58aSMatthew Dillon * one. Do not maintain a hash table. (It will be regenerated if this run of
1097*3aa7d58aSMatthew Dillon * deflate switches away from Z_RLE.)
1098*3aa7d58aSMatthew Dillon */
1099*3aa7d58aSMatthew Dillon local
1100*3aa7d58aSMatthew Dillon block_state
deflate_rle(deflate_state * s,int flush)1101*3aa7d58aSMatthew Dillon deflate_rle(deflate_state *s, int flush)
1102*3aa7d58aSMatthew Dillon {
1103*3aa7d58aSMatthew Dillon int bflush; /* set if current block must be flushed */
1104*3aa7d58aSMatthew Dillon uInt prev; /* byte at distance one to match */
1105*3aa7d58aSMatthew Dillon Bytef *scan, *strend; /* scan goes up to strend for length of run */
1106*3aa7d58aSMatthew Dillon
1107*3aa7d58aSMatthew Dillon for (;;) {
1108*3aa7d58aSMatthew Dillon /* Make sure that we always have enough lookahead, except
1109*3aa7d58aSMatthew Dillon * at the end of the input file. We need MAX_MATCH bytes
1110*3aa7d58aSMatthew Dillon * for the longest run, plus one for the unrolled loop.
1111*3aa7d58aSMatthew Dillon */
1112*3aa7d58aSMatthew Dillon if (s->lookahead <= MAX_MATCH) {
1113*3aa7d58aSMatthew Dillon fill_window(s);
1114*3aa7d58aSMatthew Dillon if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
1115*3aa7d58aSMatthew Dillon return need_more;
1116*3aa7d58aSMatthew Dillon }
1117*3aa7d58aSMatthew Dillon if (s->lookahead == 0) break; /* flush the current block */
1118*3aa7d58aSMatthew Dillon }
1119*3aa7d58aSMatthew Dillon
1120*3aa7d58aSMatthew Dillon /* See how many times the previous byte repeats */
1121*3aa7d58aSMatthew Dillon s->match_length = 0;
1122*3aa7d58aSMatthew Dillon if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1123*3aa7d58aSMatthew Dillon scan = s->window + s->strstart - 1;
1124*3aa7d58aSMatthew Dillon prev = *scan;
1125*3aa7d58aSMatthew Dillon if (prev == *++scan && prev == *++scan && prev == *++scan) {
1126*3aa7d58aSMatthew Dillon strend = s->window + s->strstart + MAX_MATCH;
1127*3aa7d58aSMatthew Dillon do {
1128*3aa7d58aSMatthew Dillon } while (prev == *++scan && prev == *++scan &&
1129*3aa7d58aSMatthew Dillon prev == *++scan && prev == *++scan &&
1130*3aa7d58aSMatthew Dillon prev == *++scan && prev == *++scan &&
1131*3aa7d58aSMatthew Dillon prev == *++scan && prev == *++scan &&
1132*3aa7d58aSMatthew Dillon scan < strend);
1133*3aa7d58aSMatthew Dillon s->match_length = MAX_MATCH - (int)(strend - scan);
1134*3aa7d58aSMatthew Dillon if (s->match_length > s->lookahead)
1135*3aa7d58aSMatthew Dillon s->match_length = s->lookahead;
1136*3aa7d58aSMatthew Dillon }
1137*3aa7d58aSMatthew Dillon Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
1138*3aa7d58aSMatthew Dillon }
1139*3aa7d58aSMatthew Dillon
1140*3aa7d58aSMatthew Dillon /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1141*3aa7d58aSMatthew Dillon if (s->match_length >= MIN_MATCH) {
1142*3aa7d58aSMatthew Dillon check_match(s, s->strstart, s->strstart - 1, s->match_length);
1143*3aa7d58aSMatthew Dillon
1144*3aa7d58aSMatthew Dillon _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1145*3aa7d58aSMatthew Dillon
1146*3aa7d58aSMatthew Dillon s->lookahead -= s->match_length;
1147*3aa7d58aSMatthew Dillon s->strstart += s->match_length;
1148*3aa7d58aSMatthew Dillon s->match_length = 0;
1149*3aa7d58aSMatthew Dillon } else {
1150*3aa7d58aSMatthew Dillon /* No match, output a literal byte */
1151*3aa7d58aSMatthew Dillon Tracevv((stderr,"%c", s->window[s->strstart]));
1152*3aa7d58aSMatthew Dillon _tr_tally_lit (s, s->window[s->strstart], bflush);
1153*3aa7d58aSMatthew Dillon s->lookahead--;
1154*3aa7d58aSMatthew Dillon s->strstart++;
1155*3aa7d58aSMatthew Dillon }
1156*3aa7d58aSMatthew Dillon if (bflush) FLUSH_BLOCK(s, 0);
1157*3aa7d58aSMatthew Dillon }
1158*3aa7d58aSMatthew Dillon s->insert = 0;
1159*3aa7d58aSMatthew Dillon if (flush == Z_FINISH) {
1160*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 1);
1161*3aa7d58aSMatthew Dillon return finish_done;
1162*3aa7d58aSMatthew Dillon }
1163*3aa7d58aSMatthew Dillon if (s->last_lit)
1164*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 0);
1165*3aa7d58aSMatthew Dillon return block_done;
1166*3aa7d58aSMatthew Dillon }
1167*3aa7d58aSMatthew Dillon
1168*3aa7d58aSMatthew Dillon /* ===========================================================================
1169*3aa7d58aSMatthew Dillon * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
1170*3aa7d58aSMatthew Dillon * (It will be regenerated if this run of deflate switches away from Huffman.)
1171*3aa7d58aSMatthew Dillon */
1172*3aa7d58aSMatthew Dillon local
1173*3aa7d58aSMatthew Dillon block_state
deflate_huff(deflate_state * s,int flush)1174*3aa7d58aSMatthew Dillon deflate_huff(deflate_state *s, int flush)
1175*3aa7d58aSMatthew Dillon {
1176*3aa7d58aSMatthew Dillon int bflush; /* set if current block must be flushed */
1177*3aa7d58aSMatthew Dillon
1178*3aa7d58aSMatthew Dillon for (;;) {
1179*3aa7d58aSMatthew Dillon /* Make sure that we have a literal to write. */
1180*3aa7d58aSMatthew Dillon if (s->lookahead == 0) {
1181*3aa7d58aSMatthew Dillon fill_window(s);
1182*3aa7d58aSMatthew Dillon if (s->lookahead == 0) {
1183*3aa7d58aSMatthew Dillon if (flush == Z_NO_FLUSH)
1184*3aa7d58aSMatthew Dillon return need_more;
1185*3aa7d58aSMatthew Dillon break; /* flush the current block */
1186*3aa7d58aSMatthew Dillon }
1187*3aa7d58aSMatthew Dillon }
1188*3aa7d58aSMatthew Dillon
1189*3aa7d58aSMatthew Dillon /* Output a literal byte */
1190*3aa7d58aSMatthew Dillon s->match_length = 0;
1191*3aa7d58aSMatthew Dillon Tracevv((stderr,"%c", s->window[s->strstart]));
1192*3aa7d58aSMatthew Dillon _tr_tally_lit (s, s->window[s->strstart], bflush);
1193*3aa7d58aSMatthew Dillon s->lookahead--;
1194*3aa7d58aSMatthew Dillon s->strstart++;
1195*3aa7d58aSMatthew Dillon if (bflush) FLUSH_BLOCK(s, 0);
1196*3aa7d58aSMatthew Dillon }
1197*3aa7d58aSMatthew Dillon s->insert = 0;
1198*3aa7d58aSMatthew Dillon if (flush == Z_FINISH) {
1199*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 1);
1200*3aa7d58aSMatthew Dillon return finish_done;
1201*3aa7d58aSMatthew Dillon }
1202*3aa7d58aSMatthew Dillon if (s->last_lit)
1203*3aa7d58aSMatthew Dillon FLUSH_BLOCK(s, 0);
1204*3aa7d58aSMatthew Dillon return block_done;
1205*3aa7d58aSMatthew Dillon }
1206