xref: /dflybsd-src/sys/vfs/hammer2/zlib/hammer2_zlib_deflate.c (revision a46112e58eff4ec5b4ae351a2a479284215f3d98)
1355d67fcSMatthew Dillon /* deflate.c -- compress data using the deflation algorithm
2355d67fcSMatthew Dillon  * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
3355d67fcSMatthew Dillon  * For conditions of distribution and use, see copyright notice in zlib.h
4355d67fcSMatthew Dillon  */
5355d67fcSMatthew Dillon 
6355d67fcSMatthew Dillon /*
7355d67fcSMatthew Dillon  *  ALGORITHM
8355d67fcSMatthew Dillon  *
9355d67fcSMatthew Dillon  *      The "deflation" process depends on being able to identify portions
10355d67fcSMatthew Dillon  *      of the input text which are identical to earlier input (within a
11355d67fcSMatthew Dillon  *      sliding window trailing behind the input currently being processed).
12355d67fcSMatthew Dillon  *
13355d67fcSMatthew Dillon  *      The most straightforward technique turns out to be the fastest for
14355d67fcSMatthew Dillon  *      most input files: try all possible matches and select the longest.
15355d67fcSMatthew Dillon  *      The key feature of this algorithm is that insertions into the string
16355d67fcSMatthew Dillon  *      dictionary are very simple and thus fast, and deletions are avoided
17355d67fcSMatthew Dillon  *      completely. Insertions are performed at each input character, whereas
18355d67fcSMatthew Dillon  *      string matches are performed only when the previous match ends. So it
19355d67fcSMatthew Dillon  *      is preferable to spend more time in matches to allow very fast string
20355d67fcSMatthew Dillon  *      insertions and avoid deletions. The matching algorithm for small
21355d67fcSMatthew Dillon  *      strings is inspired from that of Rabin & Karp. A brute force approach
22355d67fcSMatthew Dillon  *      is used to find longer strings when a small match has been found.
23355d67fcSMatthew Dillon  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24355d67fcSMatthew Dillon  *      (by Leonid Broukhis).
25355d67fcSMatthew Dillon  *         A previous version of this file used a more sophisticated algorithm
26355d67fcSMatthew Dillon  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
27355d67fcSMatthew Dillon  *      time, but has a larger average cost, uses more memory and is patented.
28355d67fcSMatthew Dillon  *      However the F&G algorithm may be faster for some highly redundant
29355d67fcSMatthew Dillon  *      files if the parameter max_chain_length (described below) is too large.
30355d67fcSMatthew Dillon  *
31355d67fcSMatthew Dillon  *  ACKNOWLEDGEMENTS
32355d67fcSMatthew Dillon  *
33355d67fcSMatthew Dillon  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34355d67fcSMatthew Dillon  *      I found it in 'freeze' written by Leonid Broukhis.
35355d67fcSMatthew Dillon  *      Thanks to many people for bug reports and testing.
36355d67fcSMatthew Dillon  *
37355d67fcSMatthew Dillon  *  REFERENCES
38355d67fcSMatthew Dillon  *
39355d67fcSMatthew Dillon  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40355d67fcSMatthew Dillon  *      Available in http://tools.ietf.org/html/rfc1951
41355d67fcSMatthew Dillon  *
42355d67fcSMatthew Dillon  *      A description of the Rabin and Karp algorithm is given in the book
43355d67fcSMatthew Dillon  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44355d67fcSMatthew Dillon  *
45355d67fcSMatthew Dillon  *      Fiala,E.R., and Greene,D.H.
46355d67fcSMatthew Dillon  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47355d67fcSMatthew Dillon  *
48355d67fcSMatthew Dillon  */
49355d67fcSMatthew Dillon 
50355d67fcSMatthew Dillon /* @(#) $Id$ */
51355d67fcSMatthew Dillon 
52355d67fcSMatthew Dillon #include "hammer2_zlib_deflate.h"
53355d67fcSMatthew Dillon #include "../hammer2.h"
54355d67fcSMatthew Dillon #include <sys/malloc.h> //for malloc macros
55355d67fcSMatthew Dillon 
56355d67fcSMatthew Dillon MALLOC_DECLARE(C_ZLIB_BUFFER_DEFLATE);
57355d67fcSMatthew Dillon MALLOC_DEFINE(C_ZLIB_BUFFER_DEFLATE, "compzlibbufferdeflate",
58355d67fcSMatthew Dillon 	"A private buffer used by zlib library for deflate function.");
59355d67fcSMatthew Dillon 
60355d67fcSMatthew Dillon const char deflate_copyright[] =
61355d67fcSMatthew Dillon    " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
62355d67fcSMatthew Dillon /*
63355d67fcSMatthew Dillon   If you use the zlib library in a product, an acknowledgment is welcome
64355d67fcSMatthew Dillon   in the documentation of your product. If for some reason you cannot
65355d67fcSMatthew Dillon   include such an acknowledgment, I would appreciate that you keep this
66355d67fcSMatthew Dillon   copyright string in the executable of your product.
67355d67fcSMatthew Dillon  */
68355d67fcSMatthew Dillon 
69355d67fcSMatthew Dillon /* ===========================================================================
70355d67fcSMatthew Dillon  *  Function prototypes.
71355d67fcSMatthew Dillon  */
72355d67fcSMatthew Dillon typedef enum {
73355d67fcSMatthew Dillon     need_more,      /* block not completed, need more input or more output */
74355d67fcSMatthew Dillon     block_done,     /* block flush performed */
75355d67fcSMatthew Dillon     finish_started, /* finish started, need only more output at next deflate */
76355d67fcSMatthew Dillon     finish_done     /* finish done, accept no more input or output */
77355d67fcSMatthew Dillon } block_state;
78355d67fcSMatthew Dillon 
79355d67fcSMatthew Dillon typedef block_state (*compress_func)(deflate_state *s, int flush);
80355d67fcSMatthew Dillon /* Compression function. Returns the block state after the call. */
81355d67fcSMatthew Dillon 
82355d67fcSMatthew Dillon local void fill_window (deflate_state *s);
83355d67fcSMatthew Dillon #ifndef FASTEST
84355d67fcSMatthew Dillon local block_state deflate_slow(deflate_state *s, int flush);
85355d67fcSMatthew Dillon #endif
86355d67fcSMatthew Dillon local block_state deflate_rle(deflate_state *s, int flush);
87355d67fcSMatthew Dillon local block_state deflate_huff(deflate_state *s, int flush);
88355d67fcSMatthew Dillon local void lm_init(deflate_state *s);
89355d67fcSMatthew Dillon local void putShortMSB(deflate_state *s, uInt b);
90355d67fcSMatthew Dillon local void flush_pending(z_streamp strm);
91355d67fcSMatthew Dillon local int read_buf(z_streamp strm, Bytef *buf, unsigned size);
92355d67fcSMatthew Dillon #ifdef ASMV
93355d67fcSMatthew Dillon       void match_init(void); /* asm code initialization */
94355d67fcSMatthew Dillon       uInt longest_match(deflate_state *s, IPos cur_match);
95355d67fcSMatthew Dillon #else
96355d67fcSMatthew Dillon local uInt longest_match(deflate_state *s, IPos cur_match);
97355d67fcSMatthew Dillon #endif
98355d67fcSMatthew Dillon 
99*a46112e5SSascha Wildner #ifdef H2_ZLIB_DEBUG
100355d67fcSMatthew Dillon local  void check_match(deflate_state *s, IPos start, IPos match,
101355d67fcSMatthew Dillon                             int length);
102355d67fcSMatthew Dillon #endif
103355d67fcSMatthew Dillon 
104355d67fcSMatthew Dillon int deflateInit2_(z_streamp strm, int level, int method, int windowBits,
105355d67fcSMatthew Dillon 					int memLevel, int strategy, const char *version,
106355d67fcSMatthew Dillon 					int stream_size);
107355d67fcSMatthew Dillon int deflateReset (z_streamp strm);
108355d67fcSMatthew Dillon int deflateResetKeep (z_streamp strm);
109355d67fcSMatthew Dillon 
110355d67fcSMatthew Dillon /* ===========================================================================
111355d67fcSMatthew Dillon  * Local data
112355d67fcSMatthew Dillon  */
113355d67fcSMatthew Dillon 
114355d67fcSMatthew Dillon #define NIL 0
115355d67fcSMatthew Dillon /* Tail of hash chains */
116355d67fcSMatthew Dillon 
117355d67fcSMatthew Dillon #ifndef TOO_FAR
118355d67fcSMatthew Dillon #  define TOO_FAR 4096
119355d67fcSMatthew Dillon #endif
120355d67fcSMatthew Dillon /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
121355d67fcSMatthew Dillon 
122355d67fcSMatthew Dillon /* Values for max_lazy_match, good_match and max_chain_length, depending on
123355d67fcSMatthew Dillon  * the desired pack level (0..9). The values given below have been tuned to
124355d67fcSMatthew Dillon  * exclude worst case performance for pathological files. Better values may be
125355d67fcSMatthew Dillon  * found for specific files.
126355d67fcSMatthew Dillon  */
127355d67fcSMatthew Dillon typedef struct config_s {
128355d67fcSMatthew Dillon    ush good_length; /* reduce lazy search above this match length */
129355d67fcSMatthew Dillon    ush max_lazy;    /* do not perform lazy search above this match length */
130355d67fcSMatthew Dillon    ush nice_length; /* quit search above this match length */
131355d67fcSMatthew Dillon    ush max_chain;
132355d67fcSMatthew Dillon    compress_func func;
133355d67fcSMatthew Dillon } config;
134355d67fcSMatthew Dillon 
135355d67fcSMatthew Dillon local const config configuration_table[10] = {
136355d67fcSMatthew Dillon /*      good lazy nice chain */
137355d67fcSMatthew Dillon /* 0 */ {0,    0,  0,    0, deflate_slow/*deflate_stored*/},  /* store only */
138355d67fcSMatthew Dillon /* 1 */ {4,    4,  8,    4, deflate_slow/*deflate_fast*/}, /* max speed, no lazy matches */
139355d67fcSMatthew Dillon /* 2 */ {4,    5, 16,    8, deflate_slow/*deflate_fast*/},
140355d67fcSMatthew Dillon /* 3 */ {4,    6, 32,   32, deflate_slow/*deflate_fast*/},
141355d67fcSMatthew Dillon 
142355d67fcSMatthew Dillon /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
143355d67fcSMatthew Dillon /* 5 */ {8,   16, 32,   32, deflate_slow},
144355d67fcSMatthew Dillon /* 6 */ {8,   16, 128, 128, deflate_slow},
145355d67fcSMatthew Dillon /* 7 */ {8,   32, 128, 256, deflate_slow},
146355d67fcSMatthew Dillon /* 8 */ {32, 128, 258, 1024, deflate_slow},
147355d67fcSMatthew Dillon /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
148355d67fcSMatthew Dillon 
149355d67fcSMatthew Dillon /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
150355d67fcSMatthew Dillon  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
151355d67fcSMatthew Dillon  * meaning.
152355d67fcSMatthew Dillon  */
153355d67fcSMatthew Dillon 
154355d67fcSMatthew Dillon #define EQUAL 0
155355d67fcSMatthew Dillon /* result of memcmp for equal strings */
156355d67fcSMatthew Dillon 
157355d67fcSMatthew Dillon #ifndef NO_DUMMY_DECL
158355d67fcSMatthew Dillon struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
159355d67fcSMatthew Dillon #endif
160355d67fcSMatthew Dillon 
161355d67fcSMatthew Dillon /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
162355d67fcSMatthew Dillon #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
163355d67fcSMatthew Dillon 
164355d67fcSMatthew Dillon /* ===========================================================================
165355d67fcSMatthew Dillon  * Update a hash value with the given input byte
166355d67fcSMatthew Dillon  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
167355d67fcSMatthew Dillon  *    input characters, so that a running hash key can be computed from the
168355d67fcSMatthew Dillon  *    previous key instead of complete recalculation each time.
169355d67fcSMatthew Dillon  */
170355d67fcSMatthew Dillon #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
171355d67fcSMatthew Dillon 
172355d67fcSMatthew Dillon 
173355d67fcSMatthew Dillon /* ===========================================================================
174355d67fcSMatthew Dillon  * Insert string str in the dictionary and set match_head to the previous head
175355d67fcSMatthew Dillon  * of the hash chain (the most recent string with same hash key). Return
176355d67fcSMatthew Dillon  * the previous length of the hash chain.
177355d67fcSMatthew Dillon  * If this file is compiled with -DFASTEST, the compression level is forced
178355d67fcSMatthew Dillon  * to 1, and no hash chains are maintained.
179355d67fcSMatthew Dillon  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
180355d67fcSMatthew Dillon  *    input characters and the first MIN_MATCH bytes of str are valid
181355d67fcSMatthew Dillon  *    (except for the last MIN_MATCH-1 bytes of the input file).
182355d67fcSMatthew Dillon  */
183355d67fcSMatthew Dillon #define INSERT_STRING(s, str, match_head) \
184355d67fcSMatthew Dillon    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
185355d67fcSMatthew Dillon     match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
186355d67fcSMatthew Dillon     s->head[s->ins_h] = (Pos)(str))
187355d67fcSMatthew Dillon 
188355d67fcSMatthew Dillon /* ===========================================================================
189355d67fcSMatthew Dillon  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
190355d67fcSMatthew Dillon  * prev[] will be initialized on the fly.
191355d67fcSMatthew Dillon  */
192355d67fcSMatthew Dillon #define CLEAR_HASH(s) \
193355d67fcSMatthew Dillon     s->head[s->hash_size-1] = NIL; \
194355d67fcSMatthew Dillon     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
195355d67fcSMatthew Dillon 
196355d67fcSMatthew Dillon /* ========================================================================= */
197355d67fcSMatthew Dillon int
deflateInit_(z_streamp strm,int level,const char * version,int stream_size)198355d67fcSMatthew Dillon deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
199355d67fcSMatthew Dillon {
200355d67fcSMatthew Dillon     return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
201355d67fcSMatthew Dillon                          Z_DEFAULT_STRATEGY, version, stream_size);
202355d67fcSMatthew Dillon     /* To do: ignore strm->next_in if we use it as window */
203355d67fcSMatthew Dillon }
204355d67fcSMatthew Dillon 
205355d67fcSMatthew Dillon /* ========================================================================= */
206355d67fcSMatthew Dillon int
deflateInit2_(z_streamp strm,int level,int method,int windowBits,int memLevel,int strategy,const char * version,int stream_size)207355d67fcSMatthew Dillon deflateInit2_(z_streamp strm, int level, int method, int windowBits,
208355d67fcSMatthew Dillon 	int memLevel, int strategy, const char *version, int stream_size)
209355d67fcSMatthew Dillon {
210355d67fcSMatthew Dillon     deflate_state *s;
211355d67fcSMatthew Dillon     int wrap = 1;
212355d67fcSMatthew Dillon     static const char my_version[] = ZLIB_VERSION;
213355d67fcSMatthew Dillon 
214355d67fcSMatthew Dillon     ushf *overlay;
215355d67fcSMatthew Dillon     /* We overlay pending_buf and d_buf+l_buf. This works since the average
216355d67fcSMatthew Dillon      * output size for (length,distance) codes is <= 24 bits.
217355d67fcSMatthew Dillon      */
218355d67fcSMatthew Dillon 
219355d67fcSMatthew Dillon     if (version == Z_NULL || version[0] != my_version[0] ||
220355d67fcSMatthew Dillon         stream_size != sizeof(z_stream)) {
221355d67fcSMatthew Dillon         return Z_VERSION_ERROR;
222355d67fcSMatthew Dillon     }
223355d67fcSMatthew Dillon     if (strm == Z_NULL) return Z_STREAM_ERROR;
224355d67fcSMatthew Dillon 
225355d67fcSMatthew Dillon     strm->msg = Z_NULL;
226355d67fcSMatthew Dillon 
227355d67fcSMatthew Dillon     if (level == Z_DEFAULT_COMPRESSION) level = 6;
228355d67fcSMatthew Dillon 
229355d67fcSMatthew Dillon     if (windowBits < 0) { /* suppress zlib wrapper */
230355d67fcSMatthew Dillon         wrap = 0;
231355d67fcSMatthew Dillon         windowBits = -windowBits;
232355d67fcSMatthew Dillon     }
233355d67fcSMatthew Dillon     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
234355d67fcSMatthew Dillon         windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
235355d67fcSMatthew Dillon         strategy < 0 || strategy > Z_FIXED) {
236355d67fcSMatthew Dillon         return Z_STREAM_ERROR;
237355d67fcSMatthew Dillon     }
238355d67fcSMatthew Dillon     if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
239355d67fcSMatthew Dillon     s = (deflate_state *) kmalloc(sizeof(*s), C_ZLIB_BUFFER_DEFLATE, M_INTWAIT);
240355d67fcSMatthew Dillon     if (s == Z_NULL) return Z_MEM_ERROR;
241355d67fcSMatthew Dillon     strm->state = (struct internal_state FAR *)s;
242355d67fcSMatthew Dillon     s->strm = strm;
243355d67fcSMatthew Dillon 
244355d67fcSMatthew Dillon     s->wrap = wrap;
245355d67fcSMatthew Dillon     s->w_bits = windowBits;
246355d67fcSMatthew Dillon     s->w_size = 1 << s->w_bits;
247355d67fcSMatthew Dillon     s->w_mask = s->w_size - 1;
248355d67fcSMatthew Dillon 
249355d67fcSMatthew Dillon     s->hash_bits = memLevel + 7;
250355d67fcSMatthew Dillon     s->hash_size = 1 << s->hash_bits;
251355d67fcSMatthew Dillon     s->hash_mask = s->hash_size - 1;
252355d67fcSMatthew Dillon     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
253355d67fcSMatthew Dillon 
254355d67fcSMatthew Dillon     s->window = (Bytef *) kmalloc((s->w_size)*2*sizeof(Byte), C_ZLIB_BUFFER_DEFLATE, M_INTWAIT);
255355d67fcSMatthew Dillon     s->prev   = (Posf *)  kmalloc((s->w_size)*sizeof(Pos), C_ZLIB_BUFFER_DEFLATE, M_INTWAIT);
256355d67fcSMatthew Dillon     s->head   = (Posf *)  kmalloc((s->hash_size)*sizeof(Pos), C_ZLIB_BUFFER_DEFLATE, M_INTWAIT);
257355d67fcSMatthew Dillon 
258355d67fcSMatthew Dillon     s->high_water = 0;      /* nothing written to s->window yet */
259355d67fcSMatthew Dillon 
260355d67fcSMatthew Dillon     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
261355d67fcSMatthew Dillon 
262355d67fcSMatthew Dillon     overlay = (ushf *) kmalloc((s->lit_bufsize)*(sizeof(ush)+2), C_ZLIB_BUFFER_DEFLATE, M_INTWAIT);
263355d67fcSMatthew Dillon     s->pending_buf = (uchf *) overlay;
264355d67fcSMatthew Dillon     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
265355d67fcSMatthew Dillon 
266355d67fcSMatthew Dillon     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
267355d67fcSMatthew Dillon         s->pending_buf == Z_NULL) {
268355d67fcSMatthew Dillon         s->status = FINISH_STATE;
269355d67fcSMatthew Dillon         strm->msg = ERR_MSG(Z_MEM_ERROR);
270355d67fcSMatthew Dillon         deflateEnd (strm);
271355d67fcSMatthew Dillon         return Z_MEM_ERROR;
272355d67fcSMatthew Dillon     }
273355d67fcSMatthew Dillon     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
274355d67fcSMatthew Dillon     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
275355d67fcSMatthew Dillon 
276355d67fcSMatthew Dillon     s->level = level;
277355d67fcSMatthew Dillon     s->strategy = strategy;
278355d67fcSMatthew Dillon     s->method = (Byte)method;
279355d67fcSMatthew Dillon 
280355d67fcSMatthew Dillon     return deflateReset(strm);
281355d67fcSMatthew Dillon }
282355d67fcSMatthew Dillon 
283355d67fcSMatthew Dillon /* ========================================================================= */
284355d67fcSMatthew Dillon int
deflateResetKeep(z_streamp strm)285355d67fcSMatthew Dillon deflateResetKeep (z_streamp strm)
286355d67fcSMatthew Dillon {
287355d67fcSMatthew Dillon     deflate_state *s;
288355d67fcSMatthew Dillon 
289355d67fcSMatthew Dillon     if (strm == Z_NULL || strm->state == Z_NULL) {
290355d67fcSMatthew Dillon         return Z_STREAM_ERROR;
291355d67fcSMatthew Dillon     }
292355d67fcSMatthew Dillon 
293355d67fcSMatthew Dillon     strm->total_in = strm->total_out = 0;
294355d67fcSMatthew Dillon     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
295355d67fcSMatthew Dillon     strm->data_type = Z_UNKNOWN;
296355d67fcSMatthew Dillon 
297355d67fcSMatthew Dillon     s = (deflate_state *)strm->state;
298355d67fcSMatthew Dillon     s->pending = 0;
299355d67fcSMatthew Dillon     s->pending_out = s->pending_buf;
300355d67fcSMatthew Dillon 
301355d67fcSMatthew Dillon     if (s->wrap < 0) {
302355d67fcSMatthew Dillon         s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
303355d67fcSMatthew Dillon     }
304355d67fcSMatthew Dillon     s->status = s->wrap ? INIT_STATE : BUSY_STATE;
305355d67fcSMatthew Dillon     strm->adler = adler32(0L, Z_NULL, 0);
306355d67fcSMatthew Dillon     s->last_flush = Z_NO_FLUSH;
307355d67fcSMatthew Dillon 
308355d67fcSMatthew Dillon     _tr_init(s);
309355d67fcSMatthew Dillon 
310355d67fcSMatthew Dillon     return Z_OK;
311355d67fcSMatthew Dillon }
312355d67fcSMatthew Dillon 
313355d67fcSMatthew Dillon /* ========================================================================= */
314355d67fcSMatthew Dillon int
deflateReset(z_streamp strm)315355d67fcSMatthew Dillon deflateReset (z_streamp strm)
316355d67fcSMatthew Dillon {
317355d67fcSMatthew Dillon     int ret;
318355d67fcSMatthew Dillon 
319355d67fcSMatthew Dillon     ret = deflateResetKeep(strm);
320355d67fcSMatthew Dillon     if (ret == Z_OK)
321355d67fcSMatthew Dillon         lm_init(strm->state);
322355d67fcSMatthew Dillon     return ret;
323355d67fcSMatthew Dillon }
324355d67fcSMatthew Dillon 
325355d67fcSMatthew Dillon /* =========================================================================
326355d67fcSMatthew Dillon  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
327355d67fcSMatthew Dillon  * IN assertion: the stream state is correct and there is enough room in
328355d67fcSMatthew Dillon  * pending_buf.
329355d67fcSMatthew Dillon  */
330355d67fcSMatthew Dillon local
331355d67fcSMatthew Dillon void
putShortMSB(deflate_state * s,uInt b)332355d67fcSMatthew Dillon putShortMSB (deflate_state *s, uInt b)
333355d67fcSMatthew Dillon {
334355d67fcSMatthew Dillon     put_byte(s, (Byte)(b >> 8));
335355d67fcSMatthew Dillon     put_byte(s, (Byte)(b & 0xff));
336355d67fcSMatthew Dillon }
337355d67fcSMatthew Dillon 
338355d67fcSMatthew Dillon /* =========================================================================
339355d67fcSMatthew Dillon  * Flush as much pending output as possible. All deflate() output goes
340355d67fcSMatthew Dillon  * through this function so some applications may wish to modify it
341355d67fcSMatthew Dillon  * to avoid allocating a large strm->next_out buffer and copying into it.
342355d67fcSMatthew Dillon  * (See also read_buf()).
343355d67fcSMatthew Dillon  */
344355d67fcSMatthew Dillon local
345355d67fcSMatthew Dillon void
flush_pending(z_streamp strm)346355d67fcSMatthew Dillon flush_pending(z_streamp strm)
347355d67fcSMatthew Dillon {
348355d67fcSMatthew Dillon     unsigned len;
349355d67fcSMatthew Dillon     deflate_state *s = strm->state;
350355d67fcSMatthew Dillon 
351355d67fcSMatthew Dillon     _tr_flush_bits(s);
352355d67fcSMatthew Dillon     len = s->pending;
353355d67fcSMatthew Dillon     if (len > strm->avail_out) len = strm->avail_out;
354355d67fcSMatthew Dillon     if (len == 0) return;
355355d67fcSMatthew Dillon 
356355d67fcSMatthew Dillon     zmemcpy(strm->next_out, s->pending_out, len);
357355d67fcSMatthew Dillon     strm->next_out  += len;
358355d67fcSMatthew Dillon     s->pending_out  += len;
359355d67fcSMatthew Dillon     strm->total_out += len;
360355d67fcSMatthew Dillon     strm->avail_out  -= len;
361355d67fcSMatthew Dillon     s->pending -= len;
362355d67fcSMatthew Dillon     if (s->pending == 0) {
363355d67fcSMatthew Dillon         s->pending_out = s->pending_buf;
364355d67fcSMatthew Dillon     }
365355d67fcSMatthew Dillon }
366355d67fcSMatthew Dillon 
367355d67fcSMatthew Dillon /* ========================================================================= */
368355d67fcSMatthew Dillon int
deflate(z_streamp strm,int flush)369355d67fcSMatthew Dillon deflate (z_streamp strm, int flush)
370355d67fcSMatthew Dillon {
371355d67fcSMatthew Dillon     int old_flush; /* value of flush param for previous deflate call */
372355d67fcSMatthew Dillon     deflate_state *s;
373355d67fcSMatthew Dillon 
374355d67fcSMatthew Dillon     if (strm == Z_NULL || strm->state == Z_NULL ||
375355d67fcSMatthew Dillon         flush > Z_BLOCK || flush < 0) {
376355d67fcSMatthew Dillon         return Z_STREAM_ERROR;
377355d67fcSMatthew Dillon     }
378355d67fcSMatthew Dillon     s = strm->state;
379355d67fcSMatthew Dillon 
380355d67fcSMatthew Dillon     if (strm->next_out == Z_NULL ||
381355d67fcSMatthew Dillon         (strm->next_in == Z_NULL && strm->avail_in != 0) ||
382355d67fcSMatthew Dillon         (s->status == FINISH_STATE && flush != Z_FINISH)) {
383355d67fcSMatthew Dillon         ERR_RETURN(strm, Z_STREAM_ERROR);
384355d67fcSMatthew Dillon     }
385355d67fcSMatthew Dillon     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
386355d67fcSMatthew Dillon 
387355d67fcSMatthew Dillon     s->strm = strm; /* just in case */
388355d67fcSMatthew Dillon     old_flush = s->last_flush;
389355d67fcSMatthew Dillon     s->last_flush = flush;
390355d67fcSMatthew Dillon 
391355d67fcSMatthew Dillon     /* Write the header */
392355d67fcSMatthew Dillon     uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
393355d67fcSMatthew Dillon     uInt level_flags;
394355d67fcSMatthew Dillon 
395355d67fcSMatthew Dillon     if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
396355d67fcSMatthew Dillon         level_flags = 0;
397355d67fcSMatthew Dillon     else if (s->level < 6)
398355d67fcSMatthew Dillon         level_flags = 1;
399355d67fcSMatthew Dillon     else if (s->level == 6)
400355d67fcSMatthew Dillon         level_flags = 2;
401355d67fcSMatthew Dillon     else
402355d67fcSMatthew Dillon         level_flags = 3;
403355d67fcSMatthew Dillon     header |= (level_flags << 6);
404355d67fcSMatthew Dillon     if (s->strstart != 0) header |= PRESET_DICT;
405355d67fcSMatthew Dillon     header += 31 - (header % 31);
406355d67fcSMatthew Dillon 
407355d67fcSMatthew Dillon     s->status = BUSY_STATE;
408355d67fcSMatthew Dillon     putShortMSB(s, header);
409355d67fcSMatthew Dillon 
410355d67fcSMatthew Dillon     /* Save the adler32 of the preset dictionary: */
411355d67fcSMatthew Dillon     if (s->strstart != 0) {
412355d67fcSMatthew Dillon         putShortMSB(s, (uInt)(strm->adler >> 16));
413355d67fcSMatthew Dillon         putShortMSB(s, (uInt)(strm->adler & 0xffff));
414355d67fcSMatthew Dillon     }
415355d67fcSMatthew Dillon     strm->adler = adler32(0L, Z_NULL, 0);
416355d67fcSMatthew Dillon 
417355d67fcSMatthew Dillon     /* Flush as much pending output as possible */
418355d67fcSMatthew Dillon     if (s->pending != 0) {
419355d67fcSMatthew Dillon         flush_pending(strm);
420355d67fcSMatthew Dillon         if (strm->avail_out == 0) {
421355d67fcSMatthew Dillon             /* Since avail_out is 0, deflate will be called again with
422355d67fcSMatthew Dillon              * more output space, but possibly with both pending and
423355d67fcSMatthew Dillon              * avail_in equal to zero. There won't be anything to do,
424355d67fcSMatthew Dillon              * but this is not an error situation so make sure we
425355d67fcSMatthew Dillon              * return OK instead of BUF_ERROR at next call of deflate:
426355d67fcSMatthew Dillon              */
427355d67fcSMatthew Dillon             s->last_flush = -1;
428355d67fcSMatthew Dillon             return Z_OK;
429355d67fcSMatthew Dillon         }
430355d67fcSMatthew Dillon 
431355d67fcSMatthew Dillon     /* Make sure there is something to do and avoid duplicate consecutive
432355d67fcSMatthew Dillon      * flushes. For repeated and useless calls with Z_FINISH, we keep
433355d67fcSMatthew Dillon      * returning Z_STREAM_END instead of Z_BUF_ERROR.
434355d67fcSMatthew Dillon      */
435355d67fcSMatthew Dillon     } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
436355d67fcSMatthew Dillon                flush != Z_FINISH) {
437355d67fcSMatthew Dillon         ERR_RETURN(strm, Z_BUF_ERROR);
438355d67fcSMatthew Dillon     }
439355d67fcSMatthew Dillon 
440355d67fcSMatthew Dillon     /* User must not provide more input after the first FINISH: */
441355d67fcSMatthew Dillon     if (s->status == FINISH_STATE && strm->avail_in != 0) {
442355d67fcSMatthew Dillon         ERR_RETURN(strm, Z_BUF_ERROR);
443355d67fcSMatthew Dillon     }
444355d67fcSMatthew Dillon 
445355d67fcSMatthew Dillon     /* Start a new block or continue the current one.
446355d67fcSMatthew Dillon      */
447355d67fcSMatthew Dillon     if (strm->avail_in != 0 || s->lookahead != 0 ||
448355d67fcSMatthew Dillon         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
449355d67fcSMatthew Dillon         block_state bstate;
450355d67fcSMatthew Dillon 
451355d67fcSMatthew Dillon         bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
452355d67fcSMatthew Dillon                     (s->strategy == Z_RLE ? deflate_rle(s, flush) :
453355d67fcSMatthew Dillon                         (*(configuration_table[s->level].func))(s, flush));
454355d67fcSMatthew Dillon 
455355d67fcSMatthew Dillon         if (bstate == finish_started || bstate == finish_done) {
456355d67fcSMatthew Dillon             s->status = FINISH_STATE;
457355d67fcSMatthew Dillon         }
458355d67fcSMatthew Dillon         if (bstate == need_more || bstate == finish_started) {
459355d67fcSMatthew Dillon             if (strm->avail_out == 0) {
460355d67fcSMatthew Dillon                 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
461355d67fcSMatthew Dillon             }
462355d67fcSMatthew Dillon             return Z_OK;
463355d67fcSMatthew Dillon             /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
464355d67fcSMatthew Dillon              * of deflate should use the same flush parameter to make sure
465355d67fcSMatthew Dillon              * that the flush is complete. So we don't have to output an
466355d67fcSMatthew Dillon              * empty block here, this will be done at next call. This also
467355d67fcSMatthew Dillon              * ensures that for a very small output buffer, we emit at most
468355d67fcSMatthew Dillon              * one empty block.
469355d67fcSMatthew Dillon              */
470355d67fcSMatthew Dillon         }
471355d67fcSMatthew Dillon         if (bstate == block_done) {
472355d67fcSMatthew Dillon             if (flush == Z_PARTIAL_FLUSH) {
473355d67fcSMatthew Dillon                 _tr_align(s);
474355d67fcSMatthew Dillon             } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
475355d67fcSMatthew Dillon                 _tr_stored_block(s, (char*)0, 0L, 0);
476355d67fcSMatthew Dillon                 /* For a full flush, this empty block will be recognized
477355d67fcSMatthew Dillon                  * as a special marker by inflate_sync().
478355d67fcSMatthew Dillon                  */
479355d67fcSMatthew Dillon                 if (flush == Z_FULL_FLUSH) {
480355d67fcSMatthew Dillon                     CLEAR_HASH(s);             /* forget history */
481355d67fcSMatthew Dillon                     if (s->lookahead == 0) {
482355d67fcSMatthew Dillon                         s->strstart = 0;
483355d67fcSMatthew Dillon                         s->block_start = 0L;
484355d67fcSMatthew Dillon                         s->insert = 0;
485355d67fcSMatthew Dillon                     }
486355d67fcSMatthew Dillon                 }
487355d67fcSMatthew Dillon             }
488355d67fcSMatthew Dillon             flush_pending(strm);
489355d67fcSMatthew Dillon             if (strm->avail_out == 0) {
490355d67fcSMatthew Dillon               s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
491355d67fcSMatthew Dillon               return Z_OK;
492355d67fcSMatthew Dillon             }
493355d67fcSMatthew Dillon         }
494355d67fcSMatthew Dillon     }
495355d67fcSMatthew Dillon     Assert(strm->avail_out > 0, "bug2");
496355d67fcSMatthew Dillon 
497355d67fcSMatthew Dillon     if (flush != Z_FINISH) return Z_OK;
498355d67fcSMatthew Dillon     if (s->wrap <= 0) return Z_STREAM_END;
499355d67fcSMatthew Dillon 
500355d67fcSMatthew Dillon     /* Write the trailer */
501355d67fcSMatthew Dillon     putShortMSB(s, (uInt)(strm->adler >> 16));
502355d67fcSMatthew Dillon     putShortMSB(s, (uInt)(strm->adler & 0xffff));
503355d67fcSMatthew Dillon 
504355d67fcSMatthew Dillon     flush_pending(strm);
505355d67fcSMatthew Dillon     /* If avail_out is zero, the application will call deflate again
506355d67fcSMatthew Dillon      * to flush the rest.
507355d67fcSMatthew Dillon      */
508355d67fcSMatthew Dillon     if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
509355d67fcSMatthew Dillon     return s->pending != 0 ? Z_OK : Z_STREAM_END;
510355d67fcSMatthew Dillon }
511355d67fcSMatthew Dillon 
512355d67fcSMatthew Dillon /* ========================================================================= */
513355d67fcSMatthew Dillon int
deflateEnd(z_streamp strm)514355d67fcSMatthew Dillon deflateEnd (z_streamp strm)
515355d67fcSMatthew Dillon {
516355d67fcSMatthew Dillon     int status;
517355d67fcSMatthew Dillon 
518355d67fcSMatthew Dillon     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
519355d67fcSMatthew Dillon 
520355d67fcSMatthew Dillon     status = strm->state->status;
521355d67fcSMatthew Dillon     if (status != INIT_STATE &&
522355d67fcSMatthew Dillon         status != EXTRA_STATE &&
523355d67fcSMatthew Dillon         status != NAME_STATE &&
524355d67fcSMatthew Dillon         status != COMMENT_STATE &&
525355d67fcSMatthew Dillon         status != HCRC_STATE &&
526355d67fcSMatthew Dillon         status != BUSY_STATE &&
527355d67fcSMatthew Dillon         status != FINISH_STATE) {
528355d67fcSMatthew Dillon       return Z_STREAM_ERROR;
529355d67fcSMatthew Dillon     }
530355d67fcSMatthew Dillon 
531355d67fcSMatthew Dillon     /* Deallocate in reverse order of allocations: */
532355d67fcSMatthew Dillon     kfree(strm->state->pending_buf, C_ZLIB_BUFFER_DEFLATE);
533355d67fcSMatthew Dillon     kfree(strm->state->head, C_ZLIB_BUFFER_DEFLATE);
534355d67fcSMatthew Dillon     kfree(strm->state->prev, C_ZLIB_BUFFER_DEFLATE);
535355d67fcSMatthew Dillon     kfree(strm->state->window, C_ZLIB_BUFFER_DEFLATE);
536355d67fcSMatthew Dillon 
537355d67fcSMatthew Dillon     kfree(strm->state, C_ZLIB_BUFFER_DEFLATE);
538355d67fcSMatthew Dillon     strm->state = Z_NULL;
539355d67fcSMatthew Dillon 
540355d67fcSMatthew Dillon     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
541355d67fcSMatthew Dillon }
542355d67fcSMatthew Dillon 
543355d67fcSMatthew Dillon /* ===========================================================================
544355d67fcSMatthew Dillon  * Read a new buffer from the current input stream, update the adler32
545355d67fcSMatthew Dillon  * and total number of bytes read.  All deflate() input goes through
546355d67fcSMatthew Dillon  * this function so some applications may wish to modify it to avoid
547355d67fcSMatthew Dillon  * allocating a large strm->next_in buffer and copying from it.
548355d67fcSMatthew Dillon  * (See also flush_pending()).
549355d67fcSMatthew Dillon  */
550355d67fcSMatthew Dillon local
551355d67fcSMatthew Dillon int
read_buf(z_streamp strm,Bytef * buf,unsigned size)552355d67fcSMatthew Dillon read_buf(z_streamp strm, Bytef *buf, unsigned size)
553355d67fcSMatthew Dillon {
554355d67fcSMatthew Dillon     unsigned len = strm->avail_in;
555355d67fcSMatthew Dillon 
556355d67fcSMatthew Dillon     if (len > size) len = size;
557355d67fcSMatthew Dillon     if (len == 0) return 0;
558355d67fcSMatthew Dillon 
559355d67fcSMatthew Dillon     strm->avail_in  -= len;
560355d67fcSMatthew Dillon 
561355d67fcSMatthew Dillon     zmemcpy(buf, strm->next_in, len);
562355d67fcSMatthew Dillon     if (strm->state->wrap == 1) {
563355d67fcSMatthew Dillon         strm->adler = adler32(strm->adler, buf, len);
564355d67fcSMatthew Dillon     }
565355d67fcSMatthew Dillon 
566355d67fcSMatthew Dillon     strm->next_in  += len;
567355d67fcSMatthew Dillon     strm->total_in += len;
568355d67fcSMatthew Dillon 
569355d67fcSMatthew Dillon     return (int)len;
570355d67fcSMatthew Dillon }
571355d67fcSMatthew Dillon 
572355d67fcSMatthew Dillon /* ===========================================================================
573355d67fcSMatthew Dillon  * Initialize the "longest match" routines for a new zlib stream
574355d67fcSMatthew Dillon  */
575355d67fcSMatthew Dillon local
576355d67fcSMatthew Dillon void
lm_init(deflate_state * s)577355d67fcSMatthew Dillon lm_init (deflate_state *s)
578355d67fcSMatthew Dillon {
579355d67fcSMatthew Dillon     s->window_size = (ulg)2L*s->w_size;
580355d67fcSMatthew Dillon 
581355d67fcSMatthew Dillon     CLEAR_HASH(s);
582355d67fcSMatthew Dillon 
583355d67fcSMatthew Dillon     /* Set the default configuration parameters:
584355d67fcSMatthew Dillon      */
585355d67fcSMatthew Dillon     s->max_lazy_match   = configuration_table[s->level].max_lazy;
586355d67fcSMatthew Dillon     s->good_match       = configuration_table[s->level].good_length;
587355d67fcSMatthew Dillon     s->nice_match       = configuration_table[s->level].nice_length;
588355d67fcSMatthew Dillon     s->max_chain_length = configuration_table[s->level].max_chain;
589355d67fcSMatthew Dillon 
590355d67fcSMatthew Dillon     s->strstart = 0;
591355d67fcSMatthew Dillon     s->block_start = 0L;
592355d67fcSMatthew Dillon     s->lookahead = 0;
593355d67fcSMatthew Dillon     s->insert = 0;
594355d67fcSMatthew Dillon     s->match_length = s->prev_length = MIN_MATCH-1;
595355d67fcSMatthew Dillon     s->match_available = 0;
596355d67fcSMatthew Dillon     s->ins_h = 0;
597355d67fcSMatthew Dillon #ifndef FASTEST
598355d67fcSMatthew Dillon #ifdef ASMV
599355d67fcSMatthew Dillon     match_init(); /* initialize the asm code */
600355d67fcSMatthew Dillon #endif
601355d67fcSMatthew Dillon #endif
602355d67fcSMatthew Dillon }
603355d67fcSMatthew Dillon 
604355d67fcSMatthew Dillon #ifndef FASTEST
605355d67fcSMatthew Dillon /* ===========================================================================
606355d67fcSMatthew Dillon  * Set match_start to the longest match starting at the given string and
607355d67fcSMatthew Dillon  * return its length. Matches shorter or equal to prev_length are discarded,
608355d67fcSMatthew Dillon  * in which case the result is equal to prev_length and match_start is
609355d67fcSMatthew Dillon  * garbage.
610355d67fcSMatthew Dillon  * IN assertions: cur_match is the head of the hash chain for the current
611355d67fcSMatthew Dillon  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
612355d67fcSMatthew Dillon  * OUT assertion: the match length is not greater than s->lookahead.
613355d67fcSMatthew Dillon  */
614355d67fcSMatthew Dillon #ifndef ASMV
615355d67fcSMatthew Dillon /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
616355d67fcSMatthew Dillon  * match.S. The code will be functionally equivalent.
617355d67fcSMatthew Dillon  */
618355d67fcSMatthew Dillon local
619355d67fcSMatthew Dillon uInt
longest_match(deflate_state * s,IPos cur_match)620355d67fcSMatthew Dillon longest_match(deflate_state *s, IPos cur_match) /* cur_match = current match */
621355d67fcSMatthew Dillon {
622355d67fcSMatthew Dillon     unsigned chain_length = s->max_chain_length;/* max hash chain length */
623355d67fcSMatthew Dillon     register Bytef *scan = s->window + s->strstart; /* current string */
624355d67fcSMatthew Dillon     register Bytef *match;                       /* matched string */
625355d67fcSMatthew Dillon     register int len;                           /* length of current match */
626355d67fcSMatthew Dillon     int best_len = s->prev_length;              /* best match length so far */
627355d67fcSMatthew Dillon     int nice_match = s->nice_match;             /* stop if match long enough */
628355d67fcSMatthew Dillon     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
629355d67fcSMatthew Dillon         s->strstart - (IPos)MAX_DIST(s) : NIL;
630355d67fcSMatthew Dillon     /* Stop when cur_match becomes <= limit. To simplify the code,
631355d67fcSMatthew Dillon      * we prevent matches with the string of window index 0.
632355d67fcSMatthew Dillon      */
633355d67fcSMatthew Dillon     Posf *prev = s->prev;
634355d67fcSMatthew Dillon     uInt wmask = s->w_mask;
635355d67fcSMatthew Dillon 
636355d67fcSMatthew Dillon #ifdef UNALIGNED_OK
637355d67fcSMatthew Dillon     /* Compare two bytes at a time. Note: this is not always beneficial.
638355d67fcSMatthew Dillon      * Try with and without -DUNALIGNED_OK to check.
639355d67fcSMatthew Dillon      */
640355d67fcSMatthew Dillon     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
641355d67fcSMatthew Dillon     register ush scan_start = *(ushf*)scan;
642355d67fcSMatthew Dillon     register ush scan_end   = *(ushf*)(scan+best_len-1);
643355d67fcSMatthew Dillon #else
644355d67fcSMatthew Dillon     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
645355d67fcSMatthew Dillon     register Byte scan_end1  = scan[best_len-1];
646355d67fcSMatthew Dillon     register Byte scan_end   = scan[best_len];
647355d67fcSMatthew Dillon #endif
648355d67fcSMatthew Dillon 
649355d67fcSMatthew Dillon     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
650355d67fcSMatthew Dillon      * It is easy to get rid of this optimization if necessary.
651355d67fcSMatthew Dillon      */
652355d67fcSMatthew Dillon     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
653355d67fcSMatthew Dillon 
654355d67fcSMatthew Dillon     /* Do not waste too much time if we already have a good match: */
655355d67fcSMatthew Dillon     if (s->prev_length >= s->good_match) {
656355d67fcSMatthew Dillon         chain_length >>= 2;
657355d67fcSMatthew Dillon     }
658355d67fcSMatthew Dillon     /* Do not look for matches beyond the end of the input. This is necessary
659355d67fcSMatthew Dillon      * to make deflate deterministic.
660355d67fcSMatthew Dillon      */
661355d67fcSMatthew Dillon     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
662355d67fcSMatthew Dillon 
663355d67fcSMatthew Dillon     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
664355d67fcSMatthew Dillon 
665355d67fcSMatthew Dillon     do {
666355d67fcSMatthew Dillon         Assert(cur_match < s->strstart, "no future");
667355d67fcSMatthew Dillon         match = s->window + cur_match;
668355d67fcSMatthew Dillon 
669355d67fcSMatthew Dillon         /* Skip to next match if the match length cannot increase
670355d67fcSMatthew Dillon          * or if the match length is less than 2.  Note that the checks below
671355d67fcSMatthew Dillon          * for insufficient lookahead only occur occasionally for performance
672355d67fcSMatthew Dillon          * reasons.  Therefore uninitialized memory will be accessed, and
673355d67fcSMatthew Dillon          * conditional jumps will be made that depend on those values.
674355d67fcSMatthew Dillon          * However the length of the match is limited to the lookahead, so
675355d67fcSMatthew Dillon          * the output of deflate is not affected by the uninitialized values.
676355d67fcSMatthew Dillon          */
677355d67fcSMatthew Dillon #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
678355d67fcSMatthew Dillon         /* This code assumes sizeof(unsigned short) == 2. Do not use
679355d67fcSMatthew Dillon          * UNALIGNED_OK if your compiler uses a different size.
680355d67fcSMatthew Dillon          */
681355d67fcSMatthew Dillon         if (*(ushf*)(match+best_len-1) != scan_end ||
682355d67fcSMatthew Dillon             *(ushf*)match != scan_start) continue;
683355d67fcSMatthew Dillon 
684355d67fcSMatthew Dillon         /* It is not necessary to compare scan[2] and match[2] since they are
685355d67fcSMatthew Dillon          * always equal when the other bytes match, given that the hash keys
686355d67fcSMatthew Dillon          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
687355d67fcSMatthew Dillon          * strstart+3, +5, ... up to strstart+257. We check for insufficient
688355d67fcSMatthew Dillon          * lookahead only every 4th comparison; the 128th check will be made
689355d67fcSMatthew Dillon          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
690355d67fcSMatthew Dillon          * necessary to put more guard bytes at the end of the window, or
691355d67fcSMatthew Dillon          * to check more often for insufficient lookahead.
692355d67fcSMatthew Dillon          */
693355d67fcSMatthew Dillon         Assert(scan[2] == match[2], "scan[2]?");
694355d67fcSMatthew Dillon         scan++, match++;
695355d67fcSMatthew Dillon         do {
696355d67fcSMatthew Dillon         } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
697355d67fcSMatthew Dillon                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
698355d67fcSMatthew Dillon                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
699355d67fcSMatthew Dillon                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
700355d67fcSMatthew Dillon                  scan < strend);
701355d67fcSMatthew Dillon         /* The funny "do {}" generates better code on most compilers */
702355d67fcSMatthew Dillon 
703355d67fcSMatthew Dillon         /* Here, scan <= window+strstart+257 */
704355d67fcSMatthew Dillon         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
705355d67fcSMatthew Dillon         if (*scan == *match) scan++;
706355d67fcSMatthew Dillon 
707355d67fcSMatthew Dillon         len = (MAX_MATCH - 1) - (int)(strend-scan);
708355d67fcSMatthew Dillon         scan = strend - (MAX_MATCH-1);
709355d67fcSMatthew Dillon 
710355d67fcSMatthew Dillon #else /* UNALIGNED_OK */
711355d67fcSMatthew Dillon 
712355d67fcSMatthew Dillon         if (match[best_len]   != scan_end  ||
713355d67fcSMatthew Dillon             match[best_len-1] != scan_end1 ||
714355d67fcSMatthew Dillon             *match            != *scan     ||
715355d67fcSMatthew Dillon             *++match          != scan[1])      continue;
716355d67fcSMatthew Dillon 
717355d67fcSMatthew Dillon         /* The check at best_len-1 can be removed because it will be made
718355d67fcSMatthew Dillon          * again later. (This heuristic is not always a win.)
719355d67fcSMatthew Dillon          * It is not necessary to compare scan[2] and match[2] since they
720355d67fcSMatthew Dillon          * are always equal when the other bytes match, given that
721355d67fcSMatthew Dillon          * the hash keys are equal and that HASH_BITS >= 8.
722355d67fcSMatthew Dillon          */
723355d67fcSMatthew Dillon         scan += 2, match++;
724355d67fcSMatthew Dillon         Assert(*scan == *match, "match[2]?");
725355d67fcSMatthew Dillon 
726355d67fcSMatthew Dillon         /* We check for insufficient lookahead only every 8th comparison;
727355d67fcSMatthew Dillon          * the 256th check will be made at strstart+258.
728355d67fcSMatthew Dillon          */
729355d67fcSMatthew Dillon         do {
730355d67fcSMatthew Dillon         } while (*++scan == *++match && *++scan == *++match &&
731355d67fcSMatthew Dillon                  *++scan == *++match && *++scan == *++match &&
732355d67fcSMatthew Dillon                  *++scan == *++match && *++scan == *++match &&
733355d67fcSMatthew Dillon                  *++scan == *++match && *++scan == *++match &&
734355d67fcSMatthew Dillon                  scan < strend);
735355d67fcSMatthew Dillon 
736355d67fcSMatthew Dillon         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
737355d67fcSMatthew Dillon 
738355d67fcSMatthew Dillon         len = MAX_MATCH - (int)(strend - scan);
739355d67fcSMatthew Dillon         scan = strend - MAX_MATCH;
740355d67fcSMatthew Dillon 
741355d67fcSMatthew Dillon #endif /* UNALIGNED_OK */
742355d67fcSMatthew Dillon 
743355d67fcSMatthew Dillon         if (len > best_len) {
744355d67fcSMatthew Dillon             s->match_start = cur_match;
745355d67fcSMatthew Dillon             best_len = len;
746355d67fcSMatthew Dillon             if (len >= nice_match) break;
747355d67fcSMatthew Dillon #ifdef UNALIGNED_OK
748355d67fcSMatthew Dillon             scan_end = *(ushf*)(scan+best_len-1);
749355d67fcSMatthew Dillon #else
750355d67fcSMatthew Dillon             scan_end1  = scan[best_len-1];
751355d67fcSMatthew Dillon             scan_end   = scan[best_len];
752355d67fcSMatthew Dillon #endif
753355d67fcSMatthew Dillon         }
754355d67fcSMatthew Dillon     } while ((cur_match = prev[cur_match & wmask]) > limit
755355d67fcSMatthew Dillon              && --chain_length != 0);
756355d67fcSMatthew Dillon 
757355d67fcSMatthew Dillon     if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
758355d67fcSMatthew Dillon     return s->lookahead;
759355d67fcSMatthew Dillon }
760355d67fcSMatthew Dillon #endif /* ASMV */
761355d67fcSMatthew Dillon 
762355d67fcSMatthew Dillon #endif /* FASTEST */
763355d67fcSMatthew Dillon 
764*a46112e5SSascha Wildner #ifdef H2_ZLIB_DEBUG
765355d67fcSMatthew Dillon /* ===========================================================================
766355d67fcSMatthew Dillon  * Check that the match at match_start is indeed a match.
767355d67fcSMatthew Dillon  */
768355d67fcSMatthew Dillon local
769355d67fcSMatthew Dillon void
check_match(deflate_state * s,IPos start,IPos match,int length)77089202d69Szrj check_match(deflate_state *s, IPos start, IPos match, int length)
771355d67fcSMatthew Dillon {
772355d67fcSMatthew Dillon     /* check that the match is indeed a match */
773355d67fcSMatthew Dillon     if (zmemcmp(s->window + match,
774355d67fcSMatthew Dillon                 s->window + start, length) != EQUAL) {
775355d67fcSMatthew Dillon         fprintf(stderr, " start %u, match %u, length %d\n",
776355d67fcSMatthew Dillon                 start, match, length);
777355d67fcSMatthew Dillon         do {
778355d67fcSMatthew Dillon             fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
779355d67fcSMatthew Dillon         } while (--length != 0);
780355d67fcSMatthew Dillon         z_error("invalid match");
781355d67fcSMatthew Dillon     }
782355d67fcSMatthew Dillon     if (z_verbose > 1) {
783355d67fcSMatthew Dillon         fprintf(stderr,"\\[%d,%d]", start-match, length);
784355d67fcSMatthew Dillon         do { putc(s->window[start++], stderr); } while (--length != 0);
785355d67fcSMatthew Dillon     }
786355d67fcSMatthew Dillon }
787355d67fcSMatthew Dillon #else
788355d67fcSMatthew Dillon #  define check_match(s, start, match, length)
789*a46112e5SSascha Wildner #endif /* H2_ZLIB_DEBUG */
790355d67fcSMatthew Dillon 
791355d67fcSMatthew Dillon /* ===========================================================================
792355d67fcSMatthew Dillon  * Fill the window when the lookahead becomes insufficient.
793355d67fcSMatthew Dillon  * Updates strstart and lookahead.
794355d67fcSMatthew Dillon  *
795355d67fcSMatthew Dillon  * IN assertion: lookahead < MIN_LOOKAHEAD
796355d67fcSMatthew Dillon  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
797355d67fcSMatthew Dillon  *    At least one byte has been read, or avail_in == 0; reads are
798355d67fcSMatthew Dillon  *    performed for at least two bytes (required for the zip translate_eol
799355d67fcSMatthew Dillon  *    option -- not supported here).
800355d67fcSMatthew Dillon  */
801355d67fcSMatthew Dillon local
802355d67fcSMatthew Dillon void
fill_window(deflate_state * s)803355d67fcSMatthew Dillon fill_window(deflate_state *s)
804355d67fcSMatthew Dillon {
805355d67fcSMatthew Dillon     register unsigned n, m;
806355d67fcSMatthew Dillon     register Posf *p;
807355d67fcSMatthew Dillon     unsigned more;    /* Amount of free space at the end of the window. */
808355d67fcSMatthew Dillon     uInt wsize = s->w_size;
809355d67fcSMatthew Dillon 
810355d67fcSMatthew Dillon     Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
811355d67fcSMatthew Dillon 
812355d67fcSMatthew Dillon     do {
813355d67fcSMatthew Dillon         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
814355d67fcSMatthew Dillon 
815355d67fcSMatthew Dillon         /* Deal with !@#$% 64K limit: */
816355d67fcSMatthew Dillon         if (sizeof(int) <= 2) {
817355d67fcSMatthew Dillon             if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
818355d67fcSMatthew Dillon                 more = wsize;
819355d67fcSMatthew Dillon 
820355d67fcSMatthew Dillon             } else if (more == (unsigned)(-1)) {
821355d67fcSMatthew Dillon                 /* Very unlikely, but possible on 16 bit machine if
822355d67fcSMatthew Dillon                  * strstart == 0 && lookahead == 1 (input done a byte at time)
823355d67fcSMatthew Dillon                  */
824355d67fcSMatthew Dillon                 more--;
825355d67fcSMatthew Dillon             }
826355d67fcSMatthew Dillon         }
827355d67fcSMatthew Dillon 
828355d67fcSMatthew Dillon         /* If the window is almost full and there is insufficient lookahead,
829355d67fcSMatthew Dillon          * move the upper half to the lower one to make room in the upper half.
830355d67fcSMatthew Dillon          */
831355d67fcSMatthew Dillon         if (s->strstart >= wsize+MAX_DIST(s)) {
832355d67fcSMatthew Dillon 
833355d67fcSMatthew Dillon             zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
834355d67fcSMatthew Dillon             s->match_start -= wsize;
835355d67fcSMatthew Dillon             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
836355d67fcSMatthew Dillon             s->block_start -= (long) wsize;
837355d67fcSMatthew Dillon 
838355d67fcSMatthew Dillon             /* Slide the hash table (could be avoided with 32 bit values
839355d67fcSMatthew Dillon                at the expense of memory usage). We slide even when level == 0
840355d67fcSMatthew Dillon                to keep the hash table consistent if we switch back to level > 0
841355d67fcSMatthew Dillon                later. (Using level 0 permanently is not an optimal usage of
842355d67fcSMatthew Dillon                zlib, so we don't care about this pathological case.)
843355d67fcSMatthew Dillon              */
844355d67fcSMatthew Dillon             n = s->hash_size;
845355d67fcSMatthew Dillon             p = &s->head[n];
846355d67fcSMatthew Dillon             do {
847355d67fcSMatthew Dillon                 m = *--p;
848355d67fcSMatthew Dillon                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
849355d67fcSMatthew Dillon             } while (--n);
850355d67fcSMatthew Dillon 
851355d67fcSMatthew Dillon             n = wsize;
852355d67fcSMatthew Dillon #ifndef FASTEST
853355d67fcSMatthew Dillon             p = &s->prev[n];
854355d67fcSMatthew Dillon             do {
855355d67fcSMatthew Dillon                 m = *--p;
856355d67fcSMatthew Dillon                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
857355d67fcSMatthew Dillon                 /* If n is not on any hash chain, prev[n] is garbage but
858355d67fcSMatthew Dillon                  * its value will never be used.
859355d67fcSMatthew Dillon                  */
860355d67fcSMatthew Dillon             } while (--n);
861355d67fcSMatthew Dillon #endif
862355d67fcSMatthew Dillon             more += wsize;
863355d67fcSMatthew Dillon         }
864355d67fcSMatthew Dillon         if (s->strm->avail_in == 0) break;
865355d67fcSMatthew Dillon 
866355d67fcSMatthew Dillon         /* If there was no sliding:
867355d67fcSMatthew Dillon          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
868355d67fcSMatthew Dillon          *    more == window_size - lookahead - strstart
869355d67fcSMatthew Dillon          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
870355d67fcSMatthew Dillon          * => more >= window_size - 2*WSIZE + 2
871355d67fcSMatthew Dillon          * In the BIG_MEM or MMAP case (not yet supported),
872355d67fcSMatthew Dillon          *   window_size == input_size + MIN_LOOKAHEAD  &&
873355d67fcSMatthew Dillon          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
874355d67fcSMatthew Dillon          * Otherwise, window_size == 2*WSIZE so more >= 2.
875355d67fcSMatthew Dillon          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
876355d67fcSMatthew Dillon          */
877355d67fcSMatthew Dillon         Assert(more >= 2, "more < 2");
878355d67fcSMatthew Dillon 
879355d67fcSMatthew Dillon         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
880355d67fcSMatthew Dillon         s->lookahead += n;
881355d67fcSMatthew Dillon 
882355d67fcSMatthew Dillon         /* Initialize the hash value now that we have some input: */
883355d67fcSMatthew Dillon         if (s->lookahead + s->insert >= MIN_MATCH) {
884355d67fcSMatthew Dillon             uInt str = s->strstart - s->insert;
885355d67fcSMatthew Dillon             s->ins_h = s->window[str];
886355d67fcSMatthew Dillon             UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
887355d67fcSMatthew Dillon #if MIN_MATCH != 3
888355d67fcSMatthew Dillon             Call UPDATE_HASH() MIN_MATCH-3 more times
889355d67fcSMatthew Dillon #endif
890355d67fcSMatthew Dillon             while (s->insert) {
891355d67fcSMatthew Dillon                 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
892355d67fcSMatthew Dillon #ifndef FASTEST
893355d67fcSMatthew Dillon                 s->prev[str & s->w_mask] = s->head[s->ins_h];
894355d67fcSMatthew Dillon #endif
895355d67fcSMatthew Dillon                 s->head[s->ins_h] = (Pos)str;
896355d67fcSMatthew Dillon                 str++;
897355d67fcSMatthew Dillon                 s->insert--;
898355d67fcSMatthew Dillon                 if (s->lookahead + s->insert < MIN_MATCH)
899355d67fcSMatthew Dillon                     break;
900355d67fcSMatthew Dillon             }
901355d67fcSMatthew Dillon         }
902355d67fcSMatthew Dillon         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
903355d67fcSMatthew Dillon          * but this is not important since only literal bytes will be emitted.
904355d67fcSMatthew Dillon          */
905355d67fcSMatthew Dillon 
906355d67fcSMatthew Dillon     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
907355d67fcSMatthew Dillon 
908355d67fcSMatthew Dillon     /* If the WIN_INIT bytes after the end of the current data have never been
909355d67fcSMatthew Dillon      * written, then zero those bytes in order to avoid memory check reports of
910355d67fcSMatthew Dillon      * the use of uninitialized (or uninitialised as Julian writes) bytes by
911355d67fcSMatthew Dillon      * the longest match routines.  Update the high water mark for the next
912355d67fcSMatthew Dillon      * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
913355d67fcSMatthew Dillon      * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
914355d67fcSMatthew Dillon      */
915355d67fcSMatthew Dillon     if (s->high_water < s->window_size) {
916355d67fcSMatthew Dillon         ulg curr = s->strstart + (ulg)(s->lookahead);
917355d67fcSMatthew Dillon         ulg init;
918355d67fcSMatthew Dillon 
919355d67fcSMatthew Dillon         if (s->high_water < curr) {
920355d67fcSMatthew Dillon             /* Previous high water mark below current data -- zero WIN_INIT
921355d67fcSMatthew Dillon              * bytes or up to end of window, whichever is less.
922355d67fcSMatthew Dillon              */
923355d67fcSMatthew Dillon             init = s->window_size - curr;
924355d67fcSMatthew Dillon             if (init > WIN_INIT)
925355d67fcSMatthew Dillon                 init = WIN_INIT;
926355d67fcSMatthew Dillon             zmemzero(s->window + curr, (unsigned)init);
927355d67fcSMatthew Dillon             s->high_water = curr + init;
928355d67fcSMatthew Dillon         }
929355d67fcSMatthew Dillon         else if (s->high_water < (ulg)curr + WIN_INIT) {
930355d67fcSMatthew Dillon             /* High water mark at or above current data, but below current data
931355d67fcSMatthew Dillon              * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
932355d67fcSMatthew Dillon              * to end of window, whichever is less.
933355d67fcSMatthew Dillon              */
934355d67fcSMatthew Dillon             init = (ulg)curr + WIN_INIT - s->high_water;
935355d67fcSMatthew Dillon             if (init > s->window_size - s->high_water)
936355d67fcSMatthew Dillon                 init = s->window_size - s->high_water;
937355d67fcSMatthew Dillon             zmemzero(s->window + s->high_water, (unsigned)init);
938355d67fcSMatthew Dillon             s->high_water += init;
939355d67fcSMatthew Dillon         }
940355d67fcSMatthew Dillon     }
941355d67fcSMatthew Dillon 
942355d67fcSMatthew Dillon     Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
943355d67fcSMatthew Dillon            "not enough room for search");
944355d67fcSMatthew Dillon }
945355d67fcSMatthew Dillon 
946355d67fcSMatthew Dillon /* ===========================================================================
947355d67fcSMatthew Dillon  * Flush the current block, with given end-of-file flag.
948355d67fcSMatthew Dillon  * IN assertion: strstart is set to the end of the current match.
949355d67fcSMatthew Dillon  */
950355d67fcSMatthew Dillon #define FLUSH_BLOCK_ONLY(s, last) { \
951355d67fcSMatthew Dillon    _tr_flush_block(s, (s->block_start >= 0L ? \
952355d67fcSMatthew Dillon                    (charf *)&s->window[(unsigned)s->block_start] : \
953355d67fcSMatthew Dillon                    (charf *)Z_NULL), \
954355d67fcSMatthew Dillon                 (ulg)((long)s->strstart - s->block_start), \
955355d67fcSMatthew Dillon                 (last)); \
956355d67fcSMatthew Dillon    s->block_start = s->strstart; \
957355d67fcSMatthew Dillon    flush_pending(s->strm); \
958355d67fcSMatthew Dillon    Tracev((stderr,"[FLUSH]")); \
959355d67fcSMatthew Dillon }
960355d67fcSMatthew Dillon 
961355d67fcSMatthew Dillon /* Same but force premature exit if necessary. */
962355d67fcSMatthew Dillon #define FLUSH_BLOCK(s, last) { \
963355d67fcSMatthew Dillon    FLUSH_BLOCK_ONLY(s, last); \
964355d67fcSMatthew Dillon    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
965355d67fcSMatthew Dillon }
966355d67fcSMatthew Dillon 
967355d67fcSMatthew Dillon #ifndef FASTEST
968355d67fcSMatthew Dillon /* ===========================================================================
969355d67fcSMatthew Dillon  * Same as above, but achieves better compression. We use a lazy
970355d67fcSMatthew Dillon  * evaluation for matches: a match is finally adopted only if there is
971355d67fcSMatthew Dillon  * no better match at the next window position.
972355d67fcSMatthew Dillon  */
973355d67fcSMatthew Dillon local
974355d67fcSMatthew Dillon block_state
deflate_slow(deflate_state * s,int flush)975355d67fcSMatthew Dillon deflate_slow(deflate_state *s, int flush)
976355d67fcSMatthew Dillon {
977355d67fcSMatthew Dillon     IPos hash_head;          /* head of hash chain */
978355d67fcSMatthew Dillon     int bflush;              /* set if current block must be flushed */
979355d67fcSMatthew Dillon 
980355d67fcSMatthew Dillon     /* Process the input block. */
981355d67fcSMatthew Dillon     for (;;) {
982355d67fcSMatthew Dillon         /* Make sure that we always have enough lookahead, except
983355d67fcSMatthew Dillon          * at the end of the input file. We need MAX_MATCH bytes
984355d67fcSMatthew Dillon          * for the next match, plus MIN_MATCH bytes to insert the
985355d67fcSMatthew Dillon          * string following the next match.
986355d67fcSMatthew Dillon          */
987355d67fcSMatthew Dillon         if (s->lookahead < MIN_LOOKAHEAD) {
988355d67fcSMatthew Dillon             fill_window(s);
989355d67fcSMatthew Dillon             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
990355d67fcSMatthew Dillon                 return need_more;
991355d67fcSMatthew Dillon             }
992355d67fcSMatthew Dillon             if (s->lookahead == 0) break; /* flush the current block */
993355d67fcSMatthew Dillon         }
994355d67fcSMatthew Dillon 
995355d67fcSMatthew Dillon         /* Insert the string window[strstart .. strstart+2] in the
996355d67fcSMatthew Dillon          * dictionary, and set hash_head to the head of the hash chain:
997355d67fcSMatthew Dillon          */
998355d67fcSMatthew Dillon         hash_head = NIL;
999355d67fcSMatthew Dillon         if (s->lookahead >= MIN_MATCH) {
1000355d67fcSMatthew Dillon             INSERT_STRING(s, s->strstart, hash_head);
1001355d67fcSMatthew Dillon         }
1002355d67fcSMatthew Dillon 
1003355d67fcSMatthew Dillon         /* Find the longest match, discarding those <= prev_length.
1004355d67fcSMatthew Dillon          */
1005355d67fcSMatthew Dillon         s->prev_length = s->match_length, s->prev_match = s->match_start;
1006355d67fcSMatthew Dillon         s->match_length = MIN_MATCH-1;
1007355d67fcSMatthew Dillon 
1008355d67fcSMatthew Dillon         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1009355d67fcSMatthew Dillon             s->strstart - hash_head <= MAX_DIST(s)) {
1010355d67fcSMatthew Dillon             /* To simplify the code, we prevent matches with the string
1011355d67fcSMatthew Dillon              * of window index 0 (in particular we have to avoid a match
1012355d67fcSMatthew Dillon              * of the string with itself at the start of the input file).
1013355d67fcSMatthew Dillon              */
1014355d67fcSMatthew Dillon             s->match_length = longest_match (s, hash_head);
1015355d67fcSMatthew Dillon             /* longest_match() sets match_start */
1016355d67fcSMatthew Dillon 
1017355d67fcSMatthew Dillon             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1018355d67fcSMatthew Dillon #if TOO_FAR <= 32767
1019355d67fcSMatthew Dillon                 || (s->match_length == MIN_MATCH &&
1020355d67fcSMatthew Dillon                     s->strstart - s->match_start > TOO_FAR)
1021355d67fcSMatthew Dillon #endif
1022355d67fcSMatthew Dillon                 )) {
1023355d67fcSMatthew Dillon 
1024355d67fcSMatthew Dillon                 /* If prev_match is also MIN_MATCH, match_start is garbage
1025355d67fcSMatthew Dillon                  * but we will ignore the current match anyway.
1026355d67fcSMatthew Dillon                  */
1027355d67fcSMatthew Dillon                 s->match_length = MIN_MATCH-1;
1028355d67fcSMatthew Dillon             }
1029355d67fcSMatthew Dillon         }
1030355d67fcSMatthew Dillon         /* If there was a match at the previous step and the current
1031355d67fcSMatthew Dillon          * match is not better, output the previous match:
1032355d67fcSMatthew Dillon          */
1033355d67fcSMatthew Dillon         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1034355d67fcSMatthew Dillon             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1035355d67fcSMatthew Dillon             /* Do not insert strings in hash table beyond this. */
1036355d67fcSMatthew Dillon 
1037355d67fcSMatthew Dillon             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1038355d67fcSMatthew Dillon 
1039355d67fcSMatthew Dillon             _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1040355d67fcSMatthew Dillon                            s->prev_length - MIN_MATCH, bflush);
1041355d67fcSMatthew Dillon 
1042355d67fcSMatthew Dillon             /* Insert in hash table all strings up to the end of the match.
1043355d67fcSMatthew Dillon              * strstart-1 and strstart are already inserted. If there is not
1044355d67fcSMatthew Dillon              * enough lookahead, the last two strings are not inserted in
1045355d67fcSMatthew Dillon              * the hash table.
1046355d67fcSMatthew Dillon              */
1047355d67fcSMatthew Dillon             s->lookahead -= s->prev_length-1;
1048355d67fcSMatthew Dillon             s->prev_length -= 2;
1049355d67fcSMatthew Dillon             do {
1050355d67fcSMatthew Dillon                 if (++s->strstart <= max_insert) {
1051355d67fcSMatthew Dillon                     INSERT_STRING(s, s->strstart, hash_head);
1052355d67fcSMatthew Dillon                 }
1053355d67fcSMatthew Dillon             } while (--s->prev_length != 0);
1054355d67fcSMatthew Dillon             s->match_available = 0;
1055355d67fcSMatthew Dillon             s->match_length = MIN_MATCH-1;
1056355d67fcSMatthew Dillon             s->strstart++;
1057355d67fcSMatthew Dillon 
1058355d67fcSMatthew Dillon             if (bflush) FLUSH_BLOCK(s, 0);
1059355d67fcSMatthew Dillon 
1060355d67fcSMatthew Dillon         } else if (s->match_available) {
1061355d67fcSMatthew Dillon             /* If there was no match at the previous position, output a
1062355d67fcSMatthew Dillon              * single literal. If there was a match but the current match
1063355d67fcSMatthew Dillon              * is longer, truncate the previous match to a single literal.
1064355d67fcSMatthew Dillon              */
1065355d67fcSMatthew Dillon             Tracevv((stderr,"%c", s->window[s->strstart-1]));
1066355d67fcSMatthew Dillon             _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1067355d67fcSMatthew Dillon             if (bflush) {
1068355d67fcSMatthew Dillon                 FLUSH_BLOCK_ONLY(s, 0);
1069355d67fcSMatthew Dillon             }
1070355d67fcSMatthew Dillon             s->strstart++;
1071355d67fcSMatthew Dillon             s->lookahead--;
1072355d67fcSMatthew Dillon             if (s->strm->avail_out == 0) return need_more;
1073355d67fcSMatthew Dillon         } else {
1074355d67fcSMatthew Dillon             /* There is no previous match to compare with, wait for
1075355d67fcSMatthew Dillon              * the next step to decide.
1076355d67fcSMatthew Dillon              */
1077355d67fcSMatthew Dillon             s->match_available = 1;
1078355d67fcSMatthew Dillon             s->strstart++;
1079355d67fcSMatthew Dillon             s->lookahead--;
1080355d67fcSMatthew Dillon         }
1081355d67fcSMatthew Dillon     }
1082355d67fcSMatthew Dillon     Assert (flush != Z_NO_FLUSH, "no flush?");
1083355d67fcSMatthew Dillon     if (s->match_available) {
1084355d67fcSMatthew Dillon         Tracevv((stderr,"%c", s->window[s->strstart-1]));
1085355d67fcSMatthew Dillon         _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1086355d67fcSMatthew Dillon         s->match_available = 0;
1087355d67fcSMatthew Dillon     }
1088355d67fcSMatthew Dillon     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1089355d67fcSMatthew Dillon     if (flush == Z_FINISH) {
1090355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 1);
1091355d67fcSMatthew Dillon         return finish_done;
1092355d67fcSMatthew Dillon     }
1093355d67fcSMatthew Dillon     if (s->last_lit)
1094355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 0);
1095355d67fcSMatthew Dillon     return block_done;
1096355d67fcSMatthew Dillon }
1097355d67fcSMatthew Dillon #endif /* FASTEST */
1098355d67fcSMatthew Dillon 
1099355d67fcSMatthew Dillon /* ===========================================================================
1100355d67fcSMatthew Dillon  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1101355d67fcSMatthew Dillon  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
1102355d67fcSMatthew Dillon  * deflate switches away from Z_RLE.)
1103355d67fcSMatthew Dillon  */
1104355d67fcSMatthew Dillon local
1105355d67fcSMatthew Dillon block_state
deflate_rle(deflate_state * s,int flush)1106355d67fcSMatthew Dillon deflate_rle(deflate_state *s, int flush)
1107355d67fcSMatthew Dillon {
1108355d67fcSMatthew Dillon     int bflush;             /* set if current block must be flushed */
1109355d67fcSMatthew Dillon     uInt prev;              /* byte at distance one to match */
1110355d67fcSMatthew Dillon     Bytef *scan, *strend;   /* scan goes up to strend for length of run */
1111355d67fcSMatthew Dillon 
1112355d67fcSMatthew Dillon     for (;;) {
1113355d67fcSMatthew Dillon         /* Make sure that we always have enough lookahead, except
1114355d67fcSMatthew Dillon          * at the end of the input file. We need MAX_MATCH bytes
1115355d67fcSMatthew Dillon          * for the longest run, plus one for the unrolled loop.
1116355d67fcSMatthew Dillon          */
1117355d67fcSMatthew Dillon         if (s->lookahead <= MAX_MATCH) {
1118355d67fcSMatthew Dillon             fill_window(s);
1119355d67fcSMatthew Dillon             if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
1120355d67fcSMatthew Dillon                 return need_more;
1121355d67fcSMatthew Dillon             }
1122355d67fcSMatthew Dillon             if (s->lookahead == 0) break; /* flush the current block */
1123355d67fcSMatthew Dillon         }
1124355d67fcSMatthew Dillon 
1125355d67fcSMatthew Dillon         /* See how many times the previous byte repeats */
1126355d67fcSMatthew Dillon         s->match_length = 0;
1127355d67fcSMatthew Dillon         if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1128355d67fcSMatthew Dillon             scan = s->window + s->strstart - 1;
1129355d67fcSMatthew Dillon             prev = *scan;
1130355d67fcSMatthew Dillon             if (prev == *++scan && prev == *++scan && prev == *++scan) {
1131355d67fcSMatthew Dillon                 strend = s->window + s->strstart + MAX_MATCH;
1132355d67fcSMatthew Dillon                 do {
1133355d67fcSMatthew Dillon                 } while (prev == *++scan && prev == *++scan &&
1134355d67fcSMatthew Dillon                          prev == *++scan && prev == *++scan &&
1135355d67fcSMatthew Dillon                          prev == *++scan && prev == *++scan &&
1136355d67fcSMatthew Dillon                          prev == *++scan && prev == *++scan &&
1137355d67fcSMatthew Dillon                          scan < strend);
1138355d67fcSMatthew Dillon                 s->match_length = MAX_MATCH - (int)(strend - scan);
1139355d67fcSMatthew Dillon                 if (s->match_length > s->lookahead)
1140355d67fcSMatthew Dillon                     s->match_length = s->lookahead;
1141355d67fcSMatthew Dillon             }
1142355d67fcSMatthew Dillon             Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
1143355d67fcSMatthew Dillon         }
1144355d67fcSMatthew Dillon 
1145355d67fcSMatthew Dillon         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1146355d67fcSMatthew Dillon         if (s->match_length >= MIN_MATCH) {
1147355d67fcSMatthew Dillon             check_match(s, s->strstart, s->strstart - 1, s->match_length);
1148355d67fcSMatthew Dillon 
1149355d67fcSMatthew Dillon             _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1150355d67fcSMatthew Dillon 
1151355d67fcSMatthew Dillon             s->lookahead -= s->match_length;
1152355d67fcSMatthew Dillon             s->strstart += s->match_length;
1153355d67fcSMatthew Dillon             s->match_length = 0;
1154355d67fcSMatthew Dillon         } else {
1155355d67fcSMatthew Dillon             /* No match, output a literal byte */
1156355d67fcSMatthew Dillon             Tracevv((stderr,"%c", s->window[s->strstart]));
1157355d67fcSMatthew Dillon             _tr_tally_lit (s, s->window[s->strstart], bflush);
1158355d67fcSMatthew Dillon             s->lookahead--;
1159355d67fcSMatthew Dillon             s->strstart++;
1160355d67fcSMatthew Dillon         }
1161355d67fcSMatthew Dillon         if (bflush) FLUSH_BLOCK(s, 0);
1162355d67fcSMatthew Dillon     }
1163355d67fcSMatthew Dillon     s->insert = 0;
1164355d67fcSMatthew Dillon     if (flush == Z_FINISH) {
1165355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 1);
1166355d67fcSMatthew Dillon         return finish_done;
1167355d67fcSMatthew Dillon     }
1168355d67fcSMatthew Dillon     if (s->last_lit)
1169355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 0);
1170355d67fcSMatthew Dillon     return block_done;
1171355d67fcSMatthew Dillon }
1172355d67fcSMatthew Dillon 
1173355d67fcSMatthew Dillon /* ===========================================================================
1174355d67fcSMatthew Dillon  * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
1175355d67fcSMatthew Dillon  * (It will be regenerated if this run of deflate switches away from Huffman.)
1176355d67fcSMatthew Dillon  */
1177355d67fcSMatthew Dillon local
1178355d67fcSMatthew Dillon block_state
deflate_huff(deflate_state * s,int flush)1179355d67fcSMatthew Dillon deflate_huff(deflate_state *s, int flush)
1180355d67fcSMatthew Dillon {
1181355d67fcSMatthew Dillon     int bflush;             /* set if current block must be flushed */
1182355d67fcSMatthew Dillon 
1183355d67fcSMatthew Dillon     for (;;) {
1184355d67fcSMatthew Dillon         /* Make sure that we have a literal to write. */
1185355d67fcSMatthew Dillon         if (s->lookahead == 0) {
1186355d67fcSMatthew Dillon             fill_window(s);
1187355d67fcSMatthew Dillon             if (s->lookahead == 0) {
1188355d67fcSMatthew Dillon                 if (flush == Z_NO_FLUSH)
1189355d67fcSMatthew Dillon                     return need_more;
1190355d67fcSMatthew Dillon                 break;      /* flush the current block */
1191355d67fcSMatthew Dillon             }
1192355d67fcSMatthew Dillon         }
1193355d67fcSMatthew Dillon 
1194355d67fcSMatthew Dillon         /* Output a literal byte */
1195355d67fcSMatthew Dillon         s->match_length = 0;
1196355d67fcSMatthew Dillon         Tracevv((stderr,"%c", s->window[s->strstart]));
1197355d67fcSMatthew Dillon         _tr_tally_lit (s, s->window[s->strstart], bflush);
1198355d67fcSMatthew Dillon         s->lookahead--;
1199355d67fcSMatthew Dillon         s->strstart++;
1200355d67fcSMatthew Dillon         if (bflush) FLUSH_BLOCK(s, 0);
1201355d67fcSMatthew Dillon     }
1202355d67fcSMatthew Dillon     s->insert = 0;
1203355d67fcSMatthew Dillon     if (flush == Z_FINISH) {
1204355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 1);
1205355d67fcSMatthew Dillon         return finish_done;
1206355d67fcSMatthew Dillon     }
1207355d67fcSMatthew Dillon     if (s->last_lit)
1208355d67fcSMatthew Dillon         FLUSH_BLOCK(s, 0);
1209355d67fcSMatthew Dillon     return block_done;
1210355d67fcSMatthew Dillon }
1211