annotate lib/zlib/deflate.c @ 2235:6ab7d7c112bb

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