diff options
Diffstat (limited to 'deflate.c')
-rw-r--r-- | deflate.c | 932 |
1 files changed, 932 insertions, 0 deletions
diff --git a/deflate.c b/deflate.c new file mode 100644 index 0000000..2409f07 --- /dev/null +++ b/deflate.c | |||
@@ -0,0 +1,932 @@ | |||
1 | /* deflate.c -- compress data using the deflation algorithm | ||
2 | * Copyright (C) 1995 Jean-loup Gailly. | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * ALGORITHM | ||
8 | * | ||
9 | * The "deflation" process depends on being able to identify portions | ||
10 | * of the input text which are identical to earlier input (within a | ||
11 | * sliding window trailing behind the input currently being processed). | ||
12 | * | ||
13 | * The most straightforward technique turns out to be the fastest for | ||
14 | * most input files: try all possible matches and select the longest. | ||
15 | * The key feature of this algorithm is that insertions into the string | ||
16 | * dictionary are very simple and thus fast, and deletions are avoided | ||
17 | * completely. Insertions are performed at each input character, whereas | ||
18 | * string matches are performed only when the previous match ends. So it | ||
19 | * is preferable to spend more time in matches to allow very fast string | ||
20 | * insertions and avoid deletions. The matching algorithm for small | ||
21 | * strings is inspired from that of Rabin & Karp. A brute force approach | ||
22 | * is used to find longer strings when a small match has been found. | ||
23 | * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze | ||
24 | * (by Leonid Broukhis). | ||
25 | * A previous version of this file used a more sophisticated algorithm | ||
26 | * (by Fiala and Greene) which is guaranteed to run in linear amortized | ||
27 | * time, but has a larger average cost, uses more memory and is patented. | ||
28 | * However the F&G algorithm may be faster for some highly redundant | ||
29 | * files if the parameter max_chain_length (described below) is too large. | ||
30 | * | ||
31 | * ACKNOWLEDGEMENTS | ||
32 | * | ||
33 | * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and | ||
34 | * I found it in 'freeze' written by Leonid Broukhis. | ||
35 | * Thanks to many people for bug reports and testing. | ||
36 | * | ||
37 | * REFERENCES | ||
38 | * | ||
39 | * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". | ||
40 | * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc | ||
41 | * | ||
42 | * A description of the Rabin and Karp algorithm is given in the book | ||
43 | * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. | ||
44 | * | ||
45 | * Fiala,E.R., and Greene,D.H. | ||
46 | * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 | ||
47 | * | ||
48 | */ | ||
49 | |||
50 | /* $Id: deflate.c,v 1.3 1995/04/10 16:03:45 jloup Exp $ */ | ||
51 | |||
52 | #include "deflate.h" | ||
53 | |||
54 | char copyright[] = " deflate Copyright 1995 Jean-loup Gailly "; | ||
55 | /* | ||
56 | If you use the zlib library in a product, an acknowledgment is welcome | ||
57 | in the documentation of your product. If for some reason you cannot | ||
58 | include such an acknowledgment, I would appreciate that you keep this | ||
59 | copyright string in the executable of your product. | ||
60 | */ | ||
61 | |||
62 | #define NIL 0 | ||
63 | /* Tail of hash chains */ | ||
64 | |||
65 | #ifndef TOO_FAR | ||
66 | # define TOO_FAR 4096 | ||
67 | #endif | ||
68 | /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ | ||
69 | |||
70 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) | ||
71 | /* Minimum amount of lookahead, except at the end of the input file. | ||
72 | * See deflate.c for comments about the MIN_MATCH+1. | ||
73 | */ | ||
74 | |||
75 | /* Values for max_lazy_match, good_match and max_chain_length, depending on | ||
76 | * the desired pack level (0..9). The values given below have been tuned to | ||
77 | * exclude worst case performance for pathological files. Better values may be | ||
78 | * found for specific files. | ||
79 | */ | ||
80 | |||
81 | typedef struct config_s { | ||
82 | ush good_length; /* reduce lazy search above this match length */ | ||
83 | ush max_lazy; /* do not perform lazy search above this match length */ | ||
84 | ush nice_length; /* quit search above this match length */ | ||
85 | ush max_chain; | ||
86 | } config; | ||
87 | |||
88 | local config configuration_table[10] = { | ||
89 | /* good lazy nice chain */ | ||
90 | /* 0 */ {0, 0, 0, 0}, /* store only */ | ||
91 | /* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */ | ||
92 | /* 2 */ {4, 5, 16, 8}, | ||
93 | /* 3 */ {4, 6, 32, 32}, | ||
94 | |||
95 | /* 4 */ {4, 4, 16, 16}, /* lazy matches */ | ||
96 | /* 5 */ {8, 16, 32, 32}, | ||
97 | /* 6 */ {8, 16, 128, 128}, | ||
98 | /* 7 */ {8, 32, 128, 256}, | ||
99 | /* 8 */ {32, 128, 258, 1024}, | ||
100 | /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */ | ||
101 | |||
102 | /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 | ||
103 | * For deflate_fast() (levels <= 3) good is ignored and lazy has a different | ||
104 | * meaning. | ||
105 | */ | ||
106 | |||
107 | #define EQUAL 0 | ||
108 | /* result of memcmp for equal strings */ | ||
109 | |||
110 | struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ | ||
111 | |||
112 | /* =========================================================================== | ||
113 | * Prototypes for local functions. | ||
114 | */ | ||
115 | |||
116 | local void fill_window __P((deflate_state *s)); | ||
117 | local int deflate_fast __P((deflate_state *s, int flush)); | ||
118 | local int deflate_slow __P((deflate_state *s, int flush)); | ||
119 | local void lm_init __P((deflate_state *s)); | ||
120 | |||
121 | local int longest_match __P((deflate_state *s, IPos cur_match)); | ||
122 | #ifdef ASMV | ||
123 | void match_init __P((void)); /* asm code initialization */ | ||
124 | #endif | ||
125 | |||
126 | #ifdef DEBUG | ||
127 | local void check_match __P((deflate_state *s, IPos start, IPos match, | ||
128 | int length)); | ||
129 | #endif | ||
130 | |||
131 | |||
132 | /* =========================================================================== | ||
133 | * Update a hash value with the given input byte | ||
134 | * IN assertion: all calls to to UPDATE_HASH are made with consecutive | ||
135 | * input characters, so that a running hash key can be computed from the | ||
136 | * previous key instead of complete recalculation each time. | ||
137 | */ | ||
138 | #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) | ||
139 | |||
140 | /* =========================================================================== | ||
141 | * Insert string str in the dictionary and set match_head to the previous head | ||
142 | * of the hash chain (the most recent string with same hash key). Return | ||
143 | * the previous length of the hash chain. | ||
144 | * IN assertion: all calls to to INSERT_STRING are made with consecutive | ||
145 | * input characters and the first MIN_MATCH bytes of str are valid | ||
146 | * (except for the last MIN_MATCH-1 bytes of the input file). | ||
147 | */ | ||
148 | #define INSERT_STRING(s, str, match_head) \ | ||
149 | (UPDATE_HASH(s, s->ins_h, s->window[(str) + MIN_MATCH-1]), \ | ||
150 | s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \ | ||
151 | s->head[s->ins_h] = (str)) | ||
152 | |||
153 | /* ========================================================================= */ | ||
154 | int deflateInit (strm, level) | ||
155 | z_stream *strm; | ||
156 | int level; | ||
157 | { | ||
158 | return deflateInit2 (strm, level, DEFLATED, WBITS, MEM_LEVEL, 0); | ||
159 | /* To do: ignore strm->next_in if we use it as window */ | ||
160 | } | ||
161 | |||
162 | /* ========================================================================= */ | ||
163 | int deflateInit2 (strm, level, method, windowBits, memLevel, strategy) | ||
164 | z_stream *strm; | ||
165 | int level; | ||
166 | int method; | ||
167 | int windowBits; | ||
168 | int memLevel; | ||
169 | int strategy; | ||
170 | { | ||
171 | deflate_state *s; | ||
172 | int noheader = 0; | ||
173 | |||
174 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
175 | |||
176 | strm->msg = Z_NULL; | ||
177 | if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; | ||
178 | if (strm->zfree == Z_NULL) strm->zfree = zcfree; | ||
179 | |||
180 | if (level == Z_DEFAULT_COMPRESSION) level = 6; | ||
181 | |||
182 | if (windowBits < 0) { /* undocumented feature: suppress zlib header */ | ||
183 | noheader = 1; | ||
184 | windowBits = -windowBits; | ||
185 | } | ||
186 | if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED || | ||
187 | windowBits < 8 || windowBits > 15 || level < 1 || level > 9) { | ||
188 | return Z_STREAM_ERROR; | ||
189 | } | ||
190 | s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); | ||
191 | if (s == Z_NULL) return Z_MEM_ERROR; | ||
192 | strm->state = (struct internal_state *)s; | ||
193 | s->strm = strm; | ||
194 | |||
195 | s->noheader = noheader; | ||
196 | s->w_bits = windowBits; | ||
197 | s->w_size = 1 << s->w_bits; | ||
198 | |||
199 | s->hash_bits = memLevel + 7; | ||
200 | s->hash_size = 1 << s->hash_bits; | ||
201 | s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); | ||
202 | |||
203 | s->window = (Byte*) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); | ||
204 | s->prev = (Pos*) ZALLOC(strm, s->w_size, sizeof(Pos)); | ||
205 | s->head = (Pos*) ZALLOC(strm, s->hash_size, sizeof(Pos)); | ||
206 | |||
207 | s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | ||
208 | |||
209 | s->pending_buf = (uch*) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush)); | ||
210 | |||
211 | if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || | ||
212 | s->pending_buf == Z_NULL) { | ||
213 | strm->msg = z_errmsg[1-Z_MEM_ERROR]; | ||
214 | deflateEnd (strm); | ||
215 | return Z_MEM_ERROR; | ||
216 | } | ||
217 | s->d_buf = (ush*) &(s->pending_buf[s->lit_bufsize]); | ||
218 | s->l_buf = (uch*) &(s->pending_buf[3*s->lit_bufsize]); | ||
219 | /* We overlay pending_buf and d_buf+l_buf. This works since the average | ||
220 | * output size for (length,distance) codes is <= 32 bits (worst case | ||
221 | * is 15+15+13=33). | ||
222 | */ | ||
223 | |||
224 | s->level = level; | ||
225 | s->strategy = strategy; | ||
226 | s->method = method; | ||
227 | |||
228 | return deflateReset(strm); | ||
229 | } | ||
230 | |||
231 | /* ========================================================================= */ | ||
232 | int deflateReset (strm) | ||
233 | z_stream *strm; | ||
234 | { | ||
235 | deflate_state *s; | ||
236 | |||
237 | if (strm == Z_NULL || strm->state == Z_NULL || | ||
238 | strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR; | ||
239 | |||
240 | strm->total_in = strm->total_out = 0; | ||
241 | strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ | ||
242 | strm->data_type = Z_UNKNOWN; | ||
243 | |||
244 | s = (deflate_state *)strm->state; | ||
245 | s->pending = 0; | ||
246 | s->pending_out = s->pending_buf; | ||
247 | |||
248 | s->status = s->noheader ? BUSY_STATE : INIT_STATE; | ||
249 | s->adler = 1; | ||
250 | |||
251 | ct_init(s); | ||
252 | lm_init(s); | ||
253 | |||
254 | return Z_OK; | ||
255 | } | ||
256 | |||
257 | /* ========================================================================= | ||
258 | * Put a short the pending_out buffer. The 16-bit value is put in MSB order. | ||
259 | * IN assertion: the stream state is correct and there is enough room in | ||
260 | * the pending_out buffer. | ||
261 | */ | ||
262 | local void putShortMSB (s, b) | ||
263 | deflate_state *s; | ||
264 | uInt b; | ||
265 | { | ||
266 | put_byte(s, b >> 8); | ||
267 | put_byte(s, b & 0xff); | ||
268 | } | ||
269 | |||
270 | /* ========================================================================= | ||
271 | * Flush as much pending output as possible. | ||
272 | */ | ||
273 | local void flush_pending(strm) | ||
274 | z_stream *strm; | ||
275 | { | ||
276 | unsigned len = strm->state->pending; | ||
277 | |||
278 | if (len > strm->avail_out) len = strm->avail_out; | ||
279 | if (len == 0) return; | ||
280 | |||
281 | zmemcpy(strm->next_out, strm->state->pending_out, len); | ||
282 | strm->next_out += len; | ||
283 | strm->state->pending_out += len; | ||
284 | strm->total_out += len; | ||
285 | strm->avail_out -= len; | ||
286 | strm->state->pending -= len; | ||
287 | if (strm->state->pending == 0) { | ||
288 | strm->state->pending_out = strm->state->pending_buf; | ||
289 | } | ||
290 | } | ||
291 | |||
292 | /* ========================================================================= */ | ||
293 | int deflate (strm, flush) | ||
294 | z_stream *strm; | ||
295 | int flush; | ||
296 | { | ||
297 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | ||
298 | |||
299 | if (strm->next_out == Z_NULL || strm->next_in == Z_NULL) { | ||
300 | ERR_RETURN(strm, Z_STREAM_ERROR); | ||
301 | } | ||
302 | if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); | ||
303 | |||
304 | strm->state->strm = strm; /* just in case */ | ||
305 | |||
306 | /* Write the zlib header */ | ||
307 | if (strm->state->status == INIT_STATE) { | ||
308 | |||
309 | uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; | ||
310 | uInt level_flags = (strm->state->level-1) >> 1; | ||
311 | |||
312 | if (level_flags > 3) level_flags = 3; | ||
313 | header |= (level_flags << 6); | ||
314 | header += 31 - (header % 31); | ||
315 | |||
316 | strm->state->status = BUSY_STATE; | ||
317 | putShortMSB(strm->state, header); | ||
318 | } | ||
319 | |||
320 | /* Flush as much pending output as possible */ | ||
321 | if (strm->state->pending != 0) { | ||
322 | flush_pending(strm); | ||
323 | if (strm->avail_out == 0) return Z_OK; | ||
324 | } | ||
325 | |||
326 | /* User must not provide more input after the first FINISH: */ | ||
327 | if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { | ||
328 | ERR_RETURN(strm, Z_BUF_ERROR); | ||
329 | } | ||
330 | |||
331 | /* Start a new block or continue the current one. | ||
332 | */ | ||
333 | if (strm->avail_in != 0 || | ||
334 | (flush == Z_FINISH && strm->state->status != FINISH_STATE)) { | ||
335 | |||
336 | if (flush == Z_FINISH) { | ||
337 | strm->state->status = FINISH_STATE; | ||
338 | } | ||
339 | if (strm->state->level <= 3) { | ||
340 | if (deflate_fast(strm->state, flush)) return Z_OK; | ||
341 | } else { | ||
342 | if (deflate_slow(strm->state, flush)) return Z_OK; | ||
343 | } | ||
344 | } | ||
345 | Assert(strm->avail_out > 0, "bug2"); | ||
346 | |||
347 | if (flush != Z_FINISH || strm->state->noheader) return Z_OK; | ||
348 | |||
349 | /* Write the zlib trailer (adler32) */ | ||
350 | putShortMSB(strm->state, strm->state->adler >> 16); | ||
351 | putShortMSB(strm->state, strm->state->adler & 0xffff); | ||
352 | flush_pending(strm); | ||
353 | /* If avail_out is zero, the application will call deflate again | ||
354 | * to flush the rest. | ||
355 | */ | ||
356 | strm->state->noheader = 1; /* write the trailer only once! */ | ||
357 | return Z_OK; | ||
358 | } | ||
359 | |||
360 | /* ========================================================================= */ | ||
361 | int deflateEnd (strm) | ||
362 | z_stream *strm; | ||
363 | { | ||
364 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | ||
365 | |||
366 | TRY_FREE(strm, strm->state->window); | ||
367 | TRY_FREE(strm, strm->state->prev); | ||
368 | TRY_FREE(strm, strm->state->head); | ||
369 | TRY_FREE(strm, strm->state->pending_buf); | ||
370 | |||
371 | ZFREE(strm, strm->state); | ||
372 | strm->state = Z_NULL; | ||
373 | |||
374 | return Z_OK; | ||
375 | } | ||
376 | |||
377 | /* ========================================================================= */ | ||
378 | int deflateCopy (dest, source) | ||
379 | z_stream *dest; | ||
380 | z_stream *source; | ||
381 | { | ||
382 | if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { | ||
383 | return Z_STREAM_ERROR; | ||
384 | } | ||
385 | *dest = *source; | ||
386 | return Z_STREAM_ERROR; /* to be implemented */ | ||
387 | #if 0 | ||
388 | dest->state = (struct internal_state *) | ||
389 | (*dest->zalloc)(1, sizeof(deflate_state)); | ||
390 | if (dest->state == Z_NULL) return Z_MEM_ERROR; | ||
391 | |||
392 | *(dest->state) = *(source->state); | ||
393 | return Z_OK; | ||
394 | #endif | ||
395 | } | ||
396 | |||
397 | /* =========================================================================== | ||
398 | * Read a new buffer from the current input stream, update the adler32 | ||
399 | * and total number of bytes read. | ||
400 | */ | ||
401 | local int read_buf(strm, buf, size) | ||
402 | z_stream *strm; | ||
403 | char *buf; | ||
404 | unsigned size; | ||
405 | { | ||
406 | unsigned len = strm->avail_in; | ||
407 | |||
408 | if (len > size) len = size; | ||
409 | if (len == 0) return 0; | ||
410 | |||
411 | strm->avail_in -= len; | ||
412 | |||
413 | if (!strm->state->noheader) { | ||
414 | strm->state->adler = adler32(strm->state->adler, strm->next_in, len); | ||
415 | } | ||
416 | zmemcpy(buf, strm->next_in, len); | ||
417 | strm->next_in += len; | ||
418 | strm->total_in += len; | ||
419 | |||
420 | return (int)len; | ||
421 | } | ||
422 | |||
423 | /* =========================================================================== | ||
424 | * Initialize the "longest match" routines for a new zlib stream | ||
425 | */ | ||
426 | local void lm_init (s) | ||
427 | deflate_state *s; | ||
428 | { | ||
429 | register unsigned j; | ||
430 | |||
431 | s->window_size = (ulg)2L*s->w_size; | ||
432 | |||
433 | |||
434 | /* Initialize the hash table (avoiding 64K overflow for 16 bit systems). | ||
435 | * prev[] will be initialized on the fly. | ||
436 | */ | ||
437 | s->head[s->hash_size-1] = NIL; | ||
438 | zmemzero((char*)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); | ||
439 | |||
440 | /* Set the default configuration parameters: | ||
441 | */ | ||
442 | s->max_lazy_match = configuration_table[s->level].max_lazy; | ||
443 | s->good_match = configuration_table[s->level].good_length; | ||
444 | s->nice_match = configuration_table[s->level].nice_length; | ||
445 | s->max_chain_length = configuration_table[s->level].max_chain; | ||
446 | |||
447 | s->strstart = 0; | ||
448 | s->block_start = 0L; | ||
449 | s->lookahead = 0; | ||
450 | s->match_length = MIN_MATCH-1; | ||
451 | s->match_available = 0; | ||
452 | #ifdef ASMV | ||
453 | match_init(); /* initialize the asm code */ | ||
454 | #endif | ||
455 | |||
456 | s->ins_h = 0; | ||
457 | for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(s, s->ins_h, s->window[j]); | ||
458 | /* If lookahead < MIN_MATCH, ins_h is garbage, but this is | ||
459 | * not important since only literal bytes will be emitted. | ||
460 | */ | ||
461 | } | ||
462 | |||
463 | /* =========================================================================== | ||
464 | * Set match_start to the longest match starting at the given string and | ||
465 | * return its length. Matches shorter or equal to prev_length are discarded, | ||
466 | * in which case the result is equal to prev_length and match_start is | ||
467 | * garbage. | ||
468 | * IN assertions: cur_match is the head of the hash chain for the current | ||
469 | * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 | ||
470 | */ | ||
471 | #ifndef ASMV | ||
472 | /* For 80x86 and 680x0, an optimized version will be provided in match.asm or | ||
473 | * match.S. The code will be functionally equivalent. | ||
474 | */ | ||
475 | local int longest_match(s, cur_match) | ||
476 | deflate_state *s; | ||
477 | IPos cur_match; /* current match */ | ||
478 | { | ||
479 | unsigned chain_length = s->max_chain_length;/* max hash chain length */ | ||
480 | register Byte *scan = s->window + s->strstart; /* current string */ | ||
481 | register Byte *match; /* matched string */ | ||
482 | register int len; /* length of current match */ | ||
483 | int best_len = s->prev_length; /* best match length so far */ | ||
484 | IPos limit = s->strstart > (IPos)MAX_DIST(s) ? | ||
485 | s->strstart - (IPos)MAX_DIST(s) : NIL; | ||
486 | /* Stop when cur_match becomes <= limit. To simplify the code, | ||
487 | * we prevent matches with the string of window index 0. | ||
488 | */ | ||
489 | |||
490 | #ifdef UNALIGNED_OK | ||
491 | /* Compare two bytes at a time. Note: this is not always beneficial. | ||
492 | * Try with and without -DUNALIGNED_OK to check. | ||
493 | */ | ||
494 | register Byte *strend = s->window + s->strstart + MAX_MATCH - 1; | ||
495 | register ush scan_start = *(ush*)scan; | ||
496 | register ush scan_end = *(ush*)(scan+best_len-1); | ||
497 | #else | ||
498 | register Byte *strend = s->window + s->strstart + MAX_MATCH; | ||
499 | register Byte scan_end1 = scan[best_len-1]; | ||
500 | register Byte scan_end = scan[best_len]; | ||
501 | #endif | ||
502 | |||
503 | /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. | ||
504 | * It is easy to get rid of this optimization if necessary. | ||
505 | */ | ||
506 | Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); | ||
507 | |||
508 | /* Do not waste too much time if we already have a good match: */ | ||
509 | if (s->prev_length >= s->good_match) { | ||
510 | chain_length >>= 2; | ||
511 | } | ||
512 | Assert(s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); | ||
513 | |||
514 | do { | ||
515 | Assert(cur_match < s->strstart, "no future"); | ||
516 | match = s->window + cur_match; | ||
517 | |||
518 | /* Skip to next match if the match length cannot increase | ||
519 | * or if the match length is less than 2: | ||
520 | */ | ||
521 | #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) | ||
522 | /* This code assumes sizeof(unsigned short) == 2. Do not use | ||
523 | * UNALIGNED_OK if your compiler uses a different size. | ||
524 | */ | ||
525 | if (*(ush*)(match+best_len-1) != scan_end || | ||
526 | *(ush*)match != scan_start) continue; | ||
527 | |||
528 | /* It is not necessary to compare scan[2] and match[2] since they are | ||
529 | * always equal when the other bytes match, given that the hash keys | ||
530 | * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at | ||
531 | * strstart+3, +5, ... up to strstart+257. We check for insufficient | ||
532 | * lookahead only every 4th comparison; the 128th check will be made | ||
533 | * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is | ||
534 | * necessary to put more guard bytes at the end of the window, or | ||
535 | * to check more often for insufficient lookahead. | ||
536 | */ | ||
537 | scan++, match++; | ||
538 | do { | ||
539 | } while (*(ush*)(scan+=2) == *(ush*)(match+=2) && | ||
540 | *(ush*)(scan+=2) == *(ush*)(match+=2) && | ||
541 | *(ush*)(scan+=2) == *(ush*)(match+=2) && | ||
542 | *(ush*)(scan+=2) == *(ush*)(match+=2) && | ||
543 | scan < strend); | ||
544 | /* The funny "do {}" generates better code on most compilers */ | ||
545 | |||
546 | /* Here, scan <= window+strstart+257 */ | ||
547 | Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); | ||
548 | if (*scan == *match) scan++; | ||
549 | |||
550 | len = (MAX_MATCH - 1) - (int)(strend-scan); | ||
551 | scan = strend - (MAX_MATCH-1); | ||
552 | |||
553 | #else /* UNALIGNED_OK */ | ||
554 | |||
555 | if (match[best_len] != scan_end || | ||
556 | match[best_len-1] != scan_end1 || | ||
557 | *match != *scan || | ||
558 | *++match != scan[1]) continue; | ||
559 | |||
560 | /* The check at best_len-1 can be removed because it will be made | ||
561 | * again later. (This heuristic is not always a win.) | ||
562 | * It is not necessary to compare scan[2] and match[2] since they | ||
563 | * are always equal when the other bytes match, given that | ||
564 | * the hash keys are equal and that HASH_BITS >= 8. | ||
565 | */ | ||
566 | scan += 2, match++; | ||
567 | |||
568 | /* We check for insufficient lookahead only every 8th comparison; | ||
569 | * the 256th check will be made at strstart+258. | ||
570 | */ | ||
571 | do { | ||
572 | } while (*++scan == *++match && *++scan == *++match && | ||
573 | *++scan == *++match && *++scan == *++match && | ||
574 | *++scan == *++match && *++scan == *++match && | ||
575 | *++scan == *++match && *++scan == *++match && | ||
576 | scan < strend); | ||
577 | |||
578 | Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); | ||
579 | |||
580 | len = MAX_MATCH - (int)(strend - scan); | ||
581 | scan = strend - MAX_MATCH; | ||
582 | |||
583 | #endif /* UNALIGNED_OK */ | ||
584 | |||
585 | if (len > best_len) { | ||
586 | s->match_start = cur_match; | ||
587 | best_len = len; | ||
588 | if (len >= s->nice_match) break; | ||
589 | #ifdef UNALIGNED_OK | ||
590 | scan_end = *(ush*)(scan+best_len-1); | ||
591 | #else | ||
592 | scan_end1 = scan[best_len-1]; | ||
593 | scan_end = scan[best_len]; | ||
594 | #endif | ||
595 | } | ||
596 | } while ((cur_match = s->prev[cur_match & s->w_mask]) > limit | ||
597 | && --chain_length != 0); | ||
598 | |||
599 | return best_len; | ||
600 | } | ||
601 | #endif /* ASMV */ | ||
602 | |||
603 | #ifdef DEBUG | ||
604 | /* =========================================================================== | ||
605 | * Check that the match at match_start is indeed a match. | ||
606 | */ | ||
607 | local void check_match(s, start, match, length) | ||
608 | deflate_state *s; | ||
609 | IPos start, match; | ||
610 | int length; | ||
611 | { | ||
612 | /* check that the match is indeed a match */ | ||
613 | if (memcmp((char*)s->window + match, | ||
614 | (char*)s->window + start, length) != EQUAL) { | ||
615 | fprintf(stderr, | ||
616 | " start %d, match %d, length %d\n", | ||
617 | start, match, length); | ||
618 | z_error("invalid match"); | ||
619 | } | ||
620 | if (verbose > 1) { | ||
621 | fprintf(stderr,"\\[%d,%d]", start-match, length); | ||
622 | do { putc(s->window[start++], stderr); } while (--length != 0); | ||
623 | } | ||
624 | } | ||
625 | #else | ||
626 | # define check_match(s, start, match, length) | ||
627 | #endif | ||
628 | |||
629 | /* =========================================================================== | ||
630 | * Fill the window when the lookahead becomes insufficient. | ||
631 | * Updates strstart and lookahead. | ||
632 | * | ||
633 | * IN assertion: lookahead < MIN_LOOKAHEAD | ||
634 | * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD | ||
635 | * At least one byte has been read, or avail_in == 0; reads are | ||
636 | * performed for at least two bytes (required for the zip translate_eol | ||
637 | * option -- not supported here). | ||
638 | */ | ||
639 | local void fill_window(s) | ||
640 | deflate_state *s; | ||
641 | { | ||
642 | register unsigned n, m; | ||
643 | unsigned more; /* Amount of free space at the end of the window. */ | ||
644 | |||
645 | do { | ||
646 | more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | ||
647 | |||
648 | /* Deal with !@#$% 64K limit: */ | ||
649 | if (more == 0 && s->strstart == 0 && s->lookahead == 0) { | ||
650 | more = s->w_size; | ||
651 | } else if (more == (unsigned)(-1)) { | ||
652 | /* Very unlikely, but possible on 16 bit machine if strstart == 0 | ||
653 | * and lookahead == 1 (input done one byte at time) | ||
654 | */ | ||
655 | more--; | ||
656 | |||
657 | /* If the window is almost full and there is insufficient lookahead, | ||
658 | * move the upper half to the lower one to make room in the upper half. | ||
659 | */ | ||
660 | } else if (s->strstart >= s->w_size+MAX_DIST(s)) { | ||
661 | |||
662 | /* By the IN assertion, the window is not empty so we can't confuse | ||
663 | * more == 0 with more == 64K on a 16 bit machine. | ||
664 | */ | ||
665 | memcpy((char*)s->window, (char*)s->window+s->w_size, | ||
666 | (unsigned)s->w_size); | ||
667 | s->match_start -= s->w_size; | ||
668 | s->strstart -= s->w_size; /* we now have strstart >= MAX_DIST */ | ||
669 | |||
670 | s->block_start -= (long) s->w_size; | ||
671 | |||
672 | for (n = 0; n < s->hash_size; n++) { | ||
673 | m = s->head[n]; | ||
674 | s->head[n] = (Pos)(m >= s->w_size ? m-s->w_size : NIL); | ||
675 | } | ||
676 | for (n = 0; n < s->w_size; n++) { | ||
677 | m = s->prev[n]; | ||
678 | s->prev[n] = (Pos)(m >= s->w_size ? m-s->w_size : NIL); | ||
679 | /* If n is not on any hash chain, prev[n] is garbage but | ||
680 | * its value will never be used. | ||
681 | */ | ||
682 | } | ||
683 | more += s->w_size; | ||
684 | } | ||
685 | if (s->strm->avail_in == 0) return; | ||
686 | |||
687 | /* If there was no sliding: | ||
688 | * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && | ||
689 | * more == window_size - lookahead - strstart | ||
690 | * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) | ||
691 | * => more >= window_size - 2*WSIZE + 2 | ||
692 | * In the BIG_MEM or MMAP case (not yet supported), | ||
693 | * window_size == input_size + MIN_LOOKAHEAD && | ||
694 | * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. | ||
695 | * Otherwise, window_size == 2*WSIZE so more >= 2. | ||
696 | * If there was sliding, more >= WSIZE. So in all cases, more >= 2. | ||
697 | */ | ||
698 | Assert(more >= 2, "more < 2"); | ||
699 | |||
700 | n = read_buf(s->strm, (char*)s->window + s->strstart + s->lookahead, | ||
701 | more); | ||
702 | s->lookahead += n; | ||
703 | |||
704 | } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); | ||
705 | } | ||
706 | |||
707 | /* =========================================================================== | ||
708 | * Flush the current block, with given end-of-file flag. | ||
709 | * IN assertion: strstart is set to the end of the current match. | ||
710 | */ | ||
711 | #define FLUSH_BLOCK_ONLY(s, eof) { \ | ||
712 | ct_flush_block(s, (s->block_start >= 0L ? \ | ||
713 | (char*)&s->window[(unsigned)s->block_start] : \ | ||
714 | (char*)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ | ||
715 | s->block_start = s->strstart; \ | ||
716 | flush_pending(s->strm); \ | ||
717 | } | ||
718 | |||
719 | /* Same but force premature exit if necessary. */ | ||
720 | #define FLUSH_BLOCK(s, eof) { \ | ||
721 | FLUSH_BLOCK_ONLY(s, eof); \ | ||
722 | if (s->strm->avail_out == 0) return 1; \ | ||
723 | } | ||
724 | |||
725 | /* =========================================================================== | ||
726 | * Compress as much as possible from the input stream, return true if | ||
727 | * processing was terminated prematurely (no more input or output space). | ||
728 | * This function does not perform lazy evaluationof matches and inserts | ||
729 | * new strings in the dictionary only for unmatched strings or for short | ||
730 | * matches. It is used only for the fast compression options. | ||
731 | */ | ||
732 | local int deflate_fast(s, flush) | ||
733 | deflate_state *s; | ||
734 | int flush; | ||
735 | { | ||
736 | IPos hash_head; /* head of the hash chain */ | ||
737 | int bflush; /* set if current block must be flushed */ | ||
738 | |||
739 | s->prev_length = MIN_MATCH-1; | ||
740 | |||
741 | for (;;) { | ||
742 | /* Make sure that we always have enough lookahead, except | ||
743 | * at the end of the input file. We need MAX_MATCH bytes | ||
744 | * for the next match, plus MIN_MATCH bytes to insert the | ||
745 | * string following the next match. | ||
746 | */ | ||
747 | if (s->lookahead < MIN_LOOKAHEAD) { | ||
748 | fill_window(s); | ||
749 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; | ||
750 | |||
751 | if (s->lookahead == 0) break; /* flush the current block */ | ||
752 | } | ||
753 | |||
754 | /* Insert the string window[strstart .. strstart+2] in the | ||
755 | * dictionary, and set hash_head to the head of the hash chain: | ||
756 | */ | ||
757 | INSERT_STRING(s, s->strstart, hash_head); | ||
758 | |||
759 | /* Find the longest match, discarding those <= prev_length. | ||
760 | * At this point we have always match_length < MIN_MATCH | ||
761 | */ | ||
762 | if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { | ||
763 | /* To simplify the code, we prevent matches with the string | ||
764 | * of window index 0 (in particular we have to avoid a match | ||
765 | * of the string with itself at the start of the input file). | ||
766 | */ | ||
767 | if (s->strategy != Z_HUFFMAN_ONLY) { | ||
768 | s->match_length = longest_match (s, hash_head); | ||
769 | } | ||
770 | /* longest_match() sets match_start */ | ||
771 | |||
772 | if (s->match_length > s->lookahead) s->match_length = s->lookahead; | ||
773 | } | ||
774 | if (s->match_length >= MIN_MATCH) { | ||
775 | check_match(s, s->strstart, s->match_start, s->match_length); | ||
776 | |||
777 | bflush = ct_tally(s, s->strstart - s->match_start, | ||
778 | s->match_length - MIN_MATCH); | ||
779 | |||
780 | s->lookahead -= s->match_length; | ||
781 | |||
782 | /* Insert new strings in the hash table only if the match length | ||
783 | * is not too large. This saves time but degrades compression. | ||
784 | */ | ||
785 | if (s->match_length <= s->max_insert_length) { | ||
786 | s->match_length--; /* string at strstart already in hash table */ | ||
787 | do { | ||
788 | s->strstart++; | ||
789 | INSERT_STRING(s, s->strstart, hash_head); | ||
790 | /* strstart never exceeds WSIZE-MAX_MATCH, so there are | ||
791 | * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH | ||
792 | * these bytes are garbage, but it does not matter since | ||
793 | * the next lookahead bytes will be emitted as literals. | ||
794 | */ | ||
795 | } while (--s->match_length != 0); | ||
796 | s->strstart++; | ||
797 | } else { | ||
798 | s->strstart += s->match_length; | ||
799 | s->match_length = 0; | ||
800 | s->ins_h = s->window[s->strstart]; | ||
801 | UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); | ||
802 | #if MIN_MATCH != 3 | ||
803 | Call UPDATE_HASH() MIN_MATCH-3 more times | ||
804 | #endif | ||
805 | } | ||
806 | } else { | ||
807 | /* No match, output a literal byte */ | ||
808 | Tracevv((stderr,"%c", s->window[s->strstart])); | ||
809 | bflush = ct_tally (s, 0, s->window[s->strstart]); | ||
810 | s->lookahead--; | ||
811 | s->strstart++; | ||
812 | } | ||
813 | if (bflush) FLUSH_BLOCK(s, 0); | ||
814 | } | ||
815 | FLUSH_BLOCK(s, flush == Z_FINISH); | ||
816 | return 0; /* normal exit */ | ||
817 | } | ||
818 | |||
819 | /* =========================================================================== | ||
820 | * Same as above, but achieves better compression. We use a lazy | ||
821 | * evaluation for matches: a match is finally adopted only if there is | ||
822 | * no better match at the next window position. | ||
823 | */ | ||
824 | local int deflate_slow(s, flush) | ||
825 | deflate_state *s; | ||
826 | int flush; | ||
827 | { | ||
828 | IPos hash_head; /* head of hash chain */ | ||
829 | int bflush; /* set if current block must be flushed */ | ||
830 | |||
831 | /* Process the input block. */ | ||
832 | for (;;) { | ||
833 | /* Make sure that we always have enough lookahead, except | ||
834 | * at the end of the input file. We need MAX_MATCH bytes | ||
835 | * for the next match, plus MIN_MATCH bytes to insert the | ||
836 | * string following the next match. | ||
837 | */ | ||
838 | if (s->lookahead < MIN_LOOKAHEAD) { | ||
839 | fill_window(s); | ||
840 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; | ||
841 | |||
842 | if (s->lookahead == 0) break; /* flush the current block */ | ||
843 | } | ||
844 | |||
845 | /* Insert the string window[strstart .. strstart+2] in the | ||
846 | * dictionary, and set hash_head to the head of the hash chain: | ||
847 | */ | ||
848 | INSERT_STRING(s, s->strstart, hash_head); | ||
849 | |||
850 | /* Find the longest match, discarding those <= prev_length. | ||
851 | */ | ||
852 | s->prev_length = s->match_length, s->prev_match = s->match_start; | ||
853 | s->match_length = MIN_MATCH-1; | ||
854 | |||
855 | if (hash_head != NIL && s->prev_length < s->max_lazy_match && | ||
856 | s->strstart - hash_head <= MAX_DIST(s)) { | ||
857 | /* To simplify the code, we prevent matches with the string | ||
858 | * of window index 0 (in particular we have to avoid a match | ||
859 | * of the string with itself at the start of the input file). | ||
860 | */ | ||
861 | if (s->strategy != Z_HUFFMAN_ONLY) { | ||
862 | s->match_length = longest_match (s, hash_head); | ||
863 | } | ||
864 | /* longest_match() sets match_start */ | ||
865 | if (s->match_length > s->lookahead) s->match_length = s->lookahead; | ||
866 | |||
867 | if (s->match_length <= 5 && (s->strategy == Z_FILTERED || | ||
868 | (s->match_length == MIN_MATCH && | ||
869 | s->strstart - s->match_start > TOO_FAR))) { | ||
870 | |||
871 | /* If prev_match is also MIN_MATCH, match_start is garbage | ||
872 | * but we will ignore the current match anyway. | ||
873 | */ | ||
874 | s->match_length = MIN_MATCH-1; | ||
875 | } | ||
876 | } | ||
877 | /* If there was a match at the previous step and the current | ||
878 | * match is not better, output the previous match: | ||
879 | */ | ||
880 | if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { | ||
881 | |||
882 | check_match(s, s->strstart-1, s->prev_match, s->prev_length); | ||
883 | |||
884 | bflush = ct_tally(s, s->strstart -1 - s->prev_match, | ||
885 | s->prev_length - MIN_MATCH); | ||
886 | |||
887 | /* Insert in hash table all strings up to the end of the match. | ||
888 | * strstart-1 and strstart are already inserted. | ||
889 | */ | ||
890 | s->lookahead -= s->prev_length-1; | ||
891 | s->prev_length -= 2; | ||
892 | do { | ||
893 | s->strstart++; | ||
894 | INSERT_STRING(s, s->strstart, hash_head); | ||
895 | /* strstart never exceeds WSIZE-MAX_MATCH, so there are | ||
896 | * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH | ||
897 | * these bytes are garbage, but it does not matter since the | ||
898 | * next lookahead bytes will always be emitted as literals. | ||
899 | */ | ||
900 | } while (--s->prev_length != 0); | ||
901 | s->match_available = 0; | ||
902 | s->match_length = MIN_MATCH-1; | ||
903 | s->strstart++; | ||
904 | |||
905 | if (bflush) FLUSH_BLOCK(s, 0); | ||
906 | |||
907 | } else if (s->match_available) { | ||
908 | /* If there was no match at the previous position, output a | ||
909 | * single literal. If there was a match but the current match | ||
910 | * is longer, truncate the previous match to a single literal. | ||
911 | */ | ||
912 | Tracevv((stderr,"%c", s->window[s->strstart-1])); | ||
913 | if (ct_tally (s, 0, s->window[s->strstart-1])) { | ||
914 | FLUSH_BLOCK_ONLY(s, 0); | ||
915 | } | ||
916 | s->strstart++; | ||
917 | s->lookahead--; | ||
918 | if (s->strm->avail_out == 0) return 1; | ||
919 | } else { | ||
920 | /* There is no previous match to compare with, wait for | ||
921 | * the next step to decide. | ||
922 | */ | ||
923 | s->match_available = 1; | ||
924 | s->strstart++; | ||
925 | s->lookahead--; | ||
926 | } | ||
927 | } | ||
928 | if (s->match_available) ct_tally (s, 0, s->window[s->strstart-1]); | ||
929 | |||
930 | FLUSH_BLOCK(s, flush == Z_FINISH); | ||
931 | return 0; | ||
932 | } | ||