Debian Codegraph @

zgz/gzip/deflate.c

raw
1 /* deflate.c -- compress data using the deflation algorithm
2
3 Copyright (C) 1999, 2006 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
5 Copyright (C) 2008 Josh Triplett
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 /*
22 * PURPOSE
23 *
24 * Identify new text as repetitions of old text within a fixed-
25 * length sliding window trailing behind the new text.
26 *
27 * DISCUSSION
28 *
29 * The "deflation" process depends on being able to identify portions
30 * of the input text which are identical to earlier input (within a
31 * sliding window trailing behind the input currently being processed).
32 *
33 * The most straightforward technique turns out to be the fastest for
34 * most input files: try all possible matches and select the longest.
35 * The key feature of this algorithm is that insertions into the string
36 * dictionary are very simple and thus fast, and deletions are avoided
37 * completely. Insertions are performed at each input character, whereas
38 * string matches are performed only when the previous match ends. So it
39 * is preferable to spend more time in matches to allow very fast string
40 * insertions and avoid deletions. The matching algorithm for small
41 * strings is inspired from that of Rabin & Karp. A brute force approach
42 * is used to find longer strings when a small match has been found.
43 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
44 * (by Leonid Broukhis).
45 * A previous version of this file used a more sophisticated algorithm
46 * (by Fiala and Greene) which is guaranteed to run in linear amortized
47 * time, but has a larger average cost, uses more memory and is patented.
48 * However the F&G algorithm may be faster for some highly redundant
49 * files if the parameter max_chain_length (described below) is too large.
50 *
51 * ACKNOWLEDGEMENTS
52 *
53 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
54 * I found it in 'freeze' written by Leonid Broukhis.
55 * Thanks to many info-zippers for bug reports and testing.
56 *
57 * REFERENCES
58 *
59 * APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
60 *
61 * A description of the Rabin and Karp algorithm is given in the book
62 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
63 *
64 * Fiala,E.R., and Greene,D.H.
65 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
66 *
67 * INTERFACE
68 *
69 * void lm_init (int pack_level, ush *flags)
70 * Initialize the "longest match" routines for a new file
71 *
72 * void deflate (void)
73 * Processes a new input file. Sets the compressed length, crc,
74 * deflate flags and internal file attributes.
75 */
76
77 #include <stdio.h>
78
79 #include "gzip.h"
80
81 /* ===========================================================================
82 * Configuration parameters
83 */
84
85 #define HASH_BITS 15
86
87 #define HASH_SIZE (unsigned)(1<<HASH_BITS)
88 #define HASH_MASK (HASH_SIZE-1)
89 #define WMASK (WSIZE-1)
90 /* HASH_SIZE and WSIZE must be powers of two */
91
92 #define NIL 0
93 /* Tail of hash chains */
94
95 #define FAST 4
96 #define SLOW 2
97 /* speed options for the general purpose bit flag */
98
99 #ifndef TOO_FAR
100 # define TOO_FAR 4096
101 #endif
102 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
103
104 int RSYNC_WIN = 4096;
105 /* Size of rsync window, must be < MAX_DIST */
106
107 /* Whether to enable compatibility with Debian's old rsyncable patch. */
108 int debian_rsyncable = 1;
109
110 void set_14_rsyncable () {
111 debian_rsyncable = 0;
112 RSYNC_WIN = 8192;
113 }
114
115 /* gzip 1.6 uses a different rsyncable that is a hybrid between the
116 * original and the gzip 1.4 version
117 */
118 void set_16_rsyncable () {
119 debian_rsyncable = 2;
120 RSYNC_WIN = 8192;
121 }
122
123 #define RSYNC_SUM_MATCH_DEBIAN(sum) ((sum) % RSYNC_WIN == 0)
124 #define RSYNC_SUM_MATCH(sum) (((sum) & (RSYNC_WIN - 1)) == 0)
125 /* Whether window sum matches magic value */
126
127 /* ===========================================================================
128 * Local data used by the "longest match" routines.
129 */
130
131 typedef ush Pos;
132 typedef unsigned IPos;
133 /* A Pos is an index in the character window. We use short instead of int to
134 * save space in the various tables. IPos is used only for parameter passing.
135 */
136
137 static uch window[2L*WSIZE];
138 /* Sliding window. Input bytes are read into the second half of the window,
139 * and move to the first half later to keep a dictionary of at least WSIZE
140 * bytes. With this organization, matches are limited to a distance of
141 * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
142 * performed with a length multiple of the block size. Also, it limits
143 * the window size to 64K, which is quite useful on MSDOS.
144 */
145
146 Pos prev[WSIZE];
147 /* Link to older string with same hash index. To limit the size of this
148 * array to 64K, this link is maintained only for the last 32K strings.
149 * An index in this array is thus a window index modulo 32K.
150 */
151
152 Pos head[1<<HASH_BITS];
153 /* Heads of the hash chains or NIL. */
154
155 ulg window_size = (ulg)2*WSIZE;
156 /* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
157 * input file length plus MIN_LOOKAHEAD.
158 */
159
160 long block_start;
161 /* window position at the beginning of the current output block. Gets
162 * negative when the window is moved backwards.
163 */
164
165 static unsigned ins_h; /* hash index of string to be inserted */
166
167 #define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
168 /* Number of bits by which ins_h and del_h must be shifted at each
169 * input step. It must be such that after MIN_MATCH steps, the oldest
170 * byte no longer takes part in the hash key, that is:
171 * H_SHIFT * MIN_MATCH >= HASH_BITS
172 */
173
174 unsigned int prev_length;
175 /* Length of the best match at previous step. Matches not greater than this
176 * are discarded. This is used in the lazy match evaluation.
177 */
178
179 unsigned strstart; /* start of string to insert */
180 unsigned match_start; /* start of matching string */
181 static int eofile; /* flag set at end of input file */
182 static unsigned lookahead; /* number of valid bytes ahead in window */
183
184 unsigned max_chain_length;
185 /* To speed up deflation, hash chains are never searched beyond this length.
186 * A higher limit improves compression ratio but degrades the speed.
187 */
188
189 static unsigned int max_lazy_match;
190 /* Attempt to find a better match only when the current match is strictly
191 * smaller than this value. This mechanism is used only for compression
192 * levels >= 4.
193 */
194 #define max_insert_length max_lazy_match
195 /* Insert new strings in the hash table only if the match length
196 * is not greater than this length. This saves time but degrades compression.
197 * max_insert_length is used only for compression levels <= 3.
198 */
199
200 static unsigned good_match;
201 /* Use a faster search when the previous match is longer than this */
202
203 static ulg rsync_sum; /* rolling sum of rsync window */
204 static ulg rsync_chunk_end; /* next rsync sequence point */
205
206 /* Values for max_lazy_match, good_match and max_chain_length, depending on
207 * the desired pack level (0..9). The values given below have been tuned to
208 * exclude worst case performance for pathological files. Better values may be
209 * found for specific files.
210 */
211
212 typedef struct config {
213 ush good_length; /* reduce lazy search above this match length */
214 ush max_lazy; /* do not perform lazy search above this match length */
215 ush nice_length; /* quit search above this match length */
216 ush max_chain;
217 } config;
218
219 #ifdef FULL_SEARCH
220 # define nice_match MAX_MATCH
221 #else
222 int nice_match; /* Stop searching when current match exceeds this */
223 #endif
224
225 static config configuration_table[10] = {
226 /* good lazy nice chain */
227 /* 0 */ {0, 0, 0, 0}, /* store only */
228 /* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */
229 /* 2 */ {4, 5, 16, 8},
230 /* 3 */ {4, 6, 32, 32},
231
232 /* 4 */ {4, 4, 16, 16}, /* lazy matches */
233 /* 5 */ {8, 16, 32, 32},
234 /* 6 */ {8, 16, 128, 128},
235 /* 7 */ {8, 32, 128, 256},
236 /* 8 */ {32, 128, 258, 1024},
237 /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
238
239 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
240 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
241 * meaning.
242 */
243
244 #define EQUAL 0
245 /* result of memcmp for equal strings */
246
247 /* ===========================================================================
248 * Prototypes for local functions.
249 */
250 static void fill_window(void);
251
252 int longest_match(IPos cur_match);
253
254 /* ===========================================================================
255 * Update a hash value with the given input byte
256 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
257 * input characters, so that a running hash key can be computed from the
258 * previous key instead of complete recalculation each time.
259 */
260 #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
261
262 /* ===========================================================================
263 * Insert string s in the dictionary and set match_head to the previous head
264 * of the hash chain (the most recent string with same hash key). Return
265 * the previous length of the hash chain.
266 * IN assertion: all calls to to INSERT_STRING are made with consecutive
267 * input characters and the first MIN_MATCH bytes of s are valid
268 * (except for the last MIN_MATCH-1 bytes of the input file).
269 */
270 #define INSERT_STRING(s, match_head) \
271 (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
272 prev[(s) & WMASK] = match_head = head[ins_h], \
273 head[ins_h] = (s))
274
275 /* ===========================================================================
276 * Initialize the "longest match" routines for a new file
277 */
278 void lm_init (int pack_level, /* 1: best speed, 9: best compression */
279 ush *flags) /* general purpose bit flag */
280 {
281 register unsigned j;
282
283 if (pack_level < 1 || pack_level > 9) gzip_error ("bad pack level");
284
285 /* Initialize the hash table. */
286 memzero((char*)head, HASH_SIZE*sizeof(*head));
287 /* prev will be initialized on the fly */
288
289 /* rsync params */
290 rsync_chunk_end = 0xFFFFFFFFUL;
291 rsync_sum = 0;
292
293 /* Set the default configuration parameters:
294 */
295 max_lazy_match = configuration_table[pack_level].max_lazy;
296 good_match = configuration_table[pack_level].good_length;
297 #ifndef FULL_SEARCH
298 nice_match = configuration_table[pack_level].nice_length;
299 #endif
300 max_chain_length = configuration_table[pack_level].max_chain;
301 if (pack_level == 1) {
302 *flags |= FAST;
303 } else if (pack_level == 9) {
304 *flags |= SLOW;
305 }
306 /* ??? reduce max_chain_length for binary files */
307
308 strstart = 0;
309 block_start = 0L;
310
311 lookahead = read_buf((char*)window,
312 sizeof(int) <= 2 ? (unsigned)WSIZE : 2*WSIZE);
313
314 if (lookahead == 0 || lookahead == (unsigned)EOF) {
315 eofile = 1, lookahead = 0;
316 return;
317 }
318 eofile = 0;
319 /* Make sure that we always have enough lookahead. This is important
320 * if input comes from a device such as a tty.
321 */
322 while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
323
324 ins_h = 0;
325 for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
326 /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
327 * not important since only literal bytes will be emitted.
328 */
329 }
330
331 /* ===========================================================================
332 * Set match_start to the longest match starting at the given string and
333 * return its length. Matches shorter or equal to prev_length are discarded,
334 * in which case the result is equal to prev_length and match_start is
335 * garbage.
336 * IN assertions: cur_match is the head of the hash chain for the current
337 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
338 */
339 int longest_match(IPos cur_match)
340 {
341 unsigned chain_length = max_chain_length; /* max hash chain length */
342 register uch *scan = window + strstart; /* current string */
343 register uch *match; /* matched string */
344 register int len; /* length of current match */
345 int best_len = prev_length; /* best match length so far */
346 IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
347 /* Stop when cur_match becomes <= limit. To simplify the code,
348 * we prevent matches with the string of window index 0.
349 */
350
351 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
352 * It is easy to get rid of this optimization if necessary.
353 */
354 #if HASH_BITS < 8 || MAX_MATCH != 258
355 error: Code too clever
356 #endif
357
358 #ifdef UNALIGNED_OK
359 /* Compare two bytes at a time. Note: this is not always beneficial.
360 * Try with and without -DUNALIGNED_OK to check.
361 */
362 register uch *strend = window + strstart + MAX_MATCH - 1;
363 register ush scan_start = *(ush*)scan;
364 register ush scan_end = *(ush*)(scan+best_len-1);
365 #else
366 register uch *strend = window + strstart + MAX_MATCH;
367 register uch scan_end1 = scan[best_len-1];
368 register uch scan_end = scan[best_len];
369 #endif
370
371 /* Do not waste too much time if we already have a good match: */
372 if (prev_length >= good_match) {
373 chain_length >>= 2;
374 }
375 Assert(strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
376
377 do {
378 Assert(cur_match < strstart, "no future");
379 match = window + cur_match;
380
381 /* Skip to next match if the match length cannot increase
382 * or if the match length is less than 2:
383 */
384 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
385 /* This code assumes sizeof(unsigned short) == 2. Do not use
386 * UNALIGNED_OK if your compiler uses a different size.
387 */
388 if (*(ush*)(match+best_len-1) != scan_end ||
389 *(ush*)match != scan_start) continue;
390
391 /* It is not necessary to compare scan[2] and match[2] since they are
392 * always equal when the other bytes match, given that the hash keys
393 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
394 * strstart+3, +5, ... up to strstart+257. We check for insufficient
395 * lookahead only every 4th comparison; the 128th check will be made
396 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
397 * necessary to put more guard bytes at the end of the window, or
398 * to check more often for insufficient lookahead.
399 */
400 scan++, match++;
401 do {
402 } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
403 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
404 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
405 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
406 scan < strend);
407 /* The funny "do {}" generates better code on most compilers */
408
409 /* Here, scan <= window+strstart+257 */
410 Assert(scan <= window+(unsigned)(window_size-1), "wild scan");
411 if (*scan == *match) scan++;
412
413 len = (MAX_MATCH - 1) - (int)(strend-scan);
414 scan = strend - (MAX_MATCH-1);
415
416 #else /* UNALIGNED_OK */
417
418 if (match[best_len] != scan_end ||
419 match[best_len-1] != scan_end1 ||
420 *match != *scan ||
421 *++match != scan[1]) continue;
422
423 /* The check at best_len-1 can be removed because it will be made
424 * again later. (This heuristic is not always a win.)
425 * It is not necessary to compare scan[2] and match[2] since they
426 * are always equal when the other bytes match, given that
427 * the hash keys are equal and that HASH_BITS >= 8.
428 */
429 scan += 2, match++;
430
431 /* We check for insufficient lookahead only every 8th comparison;
432 * the 256th check will be made at strstart+258.
433 */
434 do {
435 } while (*++scan == *++match && *++scan == *++match &&
436 *++scan == *++match && *++scan == *++match &&
437 *++scan == *++match && *++scan == *++match &&
438 *++scan == *++match && *++scan == *++match &&
439 scan < strend);
440
441 len = MAX_MATCH - (int)(strend - scan);
442 scan = strend - MAX_MATCH;
443
444 #endif /* UNALIGNED_OK */
445
446 if (len > best_len) {
447 match_start = cur_match;
448 best_len = len;
449 if (len >= nice_match) break;
450 #ifdef UNALIGNED_OK
451 scan_end = *(ush*)(scan+best_len-1);
452 #else
453 scan_end1 = scan[best_len-1];
454 scan_end = scan[best_len];
455 #endif
456 }
457 } while ((cur_match = prev[cur_match & WMASK]) > limit
458 && --chain_length != 0);
459
460 return best_len;
461 }
462
463 /* ===========================================================================
464 * Fill the window when the lookahead becomes insufficient.
465 * Updates strstart and lookahead, and sets eofile if end of input file.
466 * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
467 * OUT assertions: at least one byte has been read, or eofile is set;
468 * file reads are performed for at least two bytes (required for the
469 * translate_eol option).
470 */
471 static void fill_window(void)
472 {
473 register unsigned n, m;
474 unsigned more = (unsigned)(window_size - (ulg)lookahead - (ulg)strstart);
475 /* Amount of free space at the end of the window. */
476
477 /* If the window is almost full and there is insufficient lookahead,
478 * move the upper half to the lower one to make room in the upper half.
479 */
480 if (more == (unsigned)EOF) {
481 /* Very unlikely, but possible on 16 bit machine if strstart == 0
482 * and lookahead == 1 (input done one byte at time)
483 */
484 more--;
485 } else if (strstart >= WSIZE+MAX_DIST) {
486 /* By the IN assertion, the window is not empty so we can't confuse
487 * more == 0 with more == 64K on a 16 bit machine.
488 */
489 Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
490
491 memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
492 match_start -= WSIZE;
493 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
494 if (rsync_chunk_end != 0xFFFFFFFFUL)
495 rsync_chunk_end -= WSIZE;
496
497 block_start -= (long) WSIZE;
498
499 for (n = 0; n < HASH_SIZE; n++) {
500 m = head[n];
501 head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
502 }
503 for (n = 0; n < WSIZE; n++) {
504 m = prev[n];
505 prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
506 /* If n is not on any hash chain, prev[n] is garbage but
507 * its value will never be used.
508 */
509 }
510 more += WSIZE;
511 }
512 /* At this point, more >= 2 */
513 if (!eofile) {
514 n = read_buf((char*)window+strstart+lookahead, more);
515 if (n == 0 || n == (unsigned)EOF) {
516 eofile = 1;
517 } else {
518 lookahead += n;
519 }
520 }
521 }
522
523 static void rsync_roll(unsigned start, unsigned num)
524 {
525 unsigned i;
526
527 if (start < RSYNC_WIN) {
528 /* before window fills. */
529 for (i = start; i < RSYNC_WIN; i++) {
530 if (i == start + num) return;
531 rsync_sum += (ulg)window[i];
532 }
533 num -= (RSYNC_WIN - start);
534 start = RSYNC_WIN;
535 }
536
537 /* buffer after window full */
538 for (i = start; i < start+num; i++) {
539 /* New character in */
540 rsync_sum += (ulg)window[i];
541 /* Old character out */
542 rsync_sum -= (ulg)window[i - RSYNC_WIN];
543 if (debian_rsyncable == 1 && rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH_DEBIAN(rsync_sum))
544 rsync_chunk_end = i;
545 if (debian_rsyncable != 1 && rsync_chunk_end == 0xFFFFFFFFUL && RSYNC_SUM_MATCH(rsync_sum))
546 rsync_chunk_end = i;
547 }
548 }
549
550 /* ===========================================================================
551 * Set rsync_chunk_end if window sum matches magic value.
552 */
553 #define RSYNC_ROLL(s, n) \
554 do { if (rsync) rsync_roll((s), (n)); } while(0)
555
556 /* ===========================================================================
557 * Flush the current block, with given end-of-file flag.
558 * IN assertion: strstart is set to the end of the current match.
559 */
560 #define FLUSH_BLOCK(eof) \
561 flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
562 (char*)NULL, (long)strstart - block_start, flush-1, (eof))
563
564 /* ===========================================================================
565 * Processes a new input file and return its compressed length. This
566 * function does not perform lazy evaluationof matches and inserts
567 * new strings in the dictionary only for unmatched strings or for short
568 * matches. It is used only for the fast compression options.
569 */
570 static void deflate_fast(int pack_level, int rsync)
571 {
572 IPos hash_head; /* head of the hash chain */
573 int flush = 0; /* set if current block must be flushed, 2=>and padded */
574 unsigned match_length = 0; /* length of best match */
575
576 prev_length = MIN_MATCH-1;
577 while (lookahead != 0) {
578 /* Insert the string window[strstart .. strstart+2] in the
579 * dictionary, and set hash_head to the head of the hash chain:
580 */
581 INSERT_STRING(strstart, hash_head);
582
583 /* Find the longest match, discarding those <= prev_length.
584 * At this point we have always match_length < MIN_MATCH
585 */
586 if (hash_head != NIL && strstart - hash_head <= MAX_DIST
587 && strstart <= window_size - MIN_LOOKAHEAD) {
588 /* To simplify the code, we prevent matches with the string
589 * of window index 0 (in particular we have to avoid a match
590 * of the string with itself at the start of the input file).
591 */
592 match_length = longest_match (hash_head);
593 /* longest_match() sets match_start */
594 if (match_length > lookahead) match_length = lookahead;
595 }
596 if (match_length >= MIN_MATCH) {
597 flush = ct_tally(pack_level, strstart-match_start, match_length - MIN_MATCH);
598
599 lookahead -= match_length;
600
601 RSYNC_ROLL(strstart, match_length);
602 /* Insert new strings in the hash table only if the match length
603 * is not too large. This saves time but degrades compression.
604 */
605 if (match_length <= max_insert_length) {
606 match_length--; /* string at strstart already in hash table */
607 do {
608 strstart++;
609 INSERT_STRING(strstart, hash_head);
610 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
611 * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
612 * these bytes are garbage, but it does not matter since
613 * the next lookahead bytes will be emitted as literals.
614 */
615 } while (--match_length != 0);
616 strstart++;
617 } else {
618 strstart += match_length;
619 match_length = 0;
620 ins_h = window[strstart];
621 UPDATE_HASH(ins_h, window[strstart+1]);
622 #if MIN_MATCH != 3
623 Call UPDATE_HASH() MIN_MATCH-3 more times
624 #endif
625 }
626 } else {
627 /* No match, output a literal byte */
628 Tracevv((stderr,"%c",window[strstart]));
629 flush = ct_tally (pack_level, 0, window[strstart]);
630 RSYNC_ROLL(strstart, 1);
631 lookahead--;
632 strstart++;
633 }
634 if (rsync && debian_rsyncable && strstart > rsync_chunk_end) {
635 rsync_chunk_end = 0xFFFFFFFFUL;
636 flush = 2;
637 }
638 if (rsync && ! debian_rsyncable && strstart > rsync_chunk_end) {
639 flush = 1;
640 ct_init();
641 rsync_chunk_end = 0xFFFFFFFFUL;
642 }
643 if (flush) FLUSH_BLOCK(0), block_start = strstart;
644
645 /* Make sure that we always have enough lookahead, except
646 * at the end of the input file. We need MAX_MATCH bytes
647 * for the next match, plus MIN_MATCH bytes to insert the
648 * string following the next match.
649 */
650 while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
651
652 }
653 FLUSH_BLOCK(1); /* eof */
654 }
655
656 /* ===========================================================================
657 * Same as above, but achieves better compression. We use a lazy
658 * evaluation for matches: a match is finally adopted only if there is
659 * no better match at the next window position.
660 */
661 void gnu_deflate(int pack_level, int rsync, int rsync14, int rsync16)
662 {
663 IPos hash_head; /* head of hash chain */
664 IPos prev_match; /* previous match */
665 int flush = 0; /* set if current block must be flushed */
666 int match_available = 0; /* set if previous match exists */
667 register unsigned match_length = MIN_MATCH-1; /* length of best match */
668
669 /* The rsync14 mode superscedes Debian's old, somewhat broken
670 * rsync patch. */
671 if (rsync14) {
672 rsync=1;
673 set_14_rsyncable();
674 }
675 /* The rsync16 mode superscedes the others yet again */
676 if (rsync16) {
677 rsync=1;
678 set_16_rsyncable();
679 }
680
681 if (pack_level <= 3) {
682 deflate_fast(pack_level, rsync); /* optimized for speed */
683 return;
684 }
685
686 /* Process the input block. */
687 while (lookahead != 0) {
688 /* Insert the string window[strstart .. strstart+2] in the
689 * dictionary, and set hash_head to the head of the hash chain:
690 */
691 INSERT_STRING(strstart, hash_head);
692
693 /* Find the longest match, discarding those <= prev_length.
694 */
695 prev_length = match_length, prev_match = match_start;
696 match_length = MIN_MATCH-1;
697
698 if (hash_head != NIL && prev_length < max_lazy_match &&
699 strstart - hash_head <= MAX_DIST &&
700 strstart <= window_size - MIN_LOOKAHEAD) {
701 /* To simplify the code, we prevent matches with the string
702 * of window index 0 (in particular we have to avoid a match
703 * of the string with itself at the start of the input file).
704 */
705 match_length = longest_match (hash_head);
706 /* longest_match() sets match_start */
707 if (match_length > lookahead) match_length = lookahead;
708
709 /* Ignore a length 3 match if it is too distant: */
710 if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
711 /* If prev_match is also MIN_MATCH, match_start is garbage
712 * but we will ignore the current match anyway.
713 */
714 match_length--;
715 }
716 }
717 /* If there was a match at the previous step and the current
718 * match is not better, output the previous match:
719 */
720 if (prev_length >= MIN_MATCH && match_length <= prev_length) {
721 flush = ct_tally(pack_level, strstart-1-prev_match, prev_length - MIN_MATCH);
722
723 /* Insert in hash table all strings up to the end of the match.
724 * strstart-1 and strstart are already inserted.
725 */
726 lookahead -= prev_length-1;
727 prev_length -= 2;
728 RSYNC_ROLL(strstart, prev_length+1);
729 do {
730 strstart++;
731 INSERT_STRING(strstart, hash_head);
732 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
733 * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
734 * these bytes are garbage, but it does not matter since the
735 * next lookahead bytes will always be emitted as literals.
736 */
737 } while (--prev_length != 0);
738 match_available = 0;
739 match_length = MIN_MATCH-1;
740 strstart++;
741
742 if (rsync && debian_rsyncable && strstart > rsync_chunk_end) {
743 rsync_chunk_end = 0xFFFFFFFFUL;
744 flush = 2;
745 }
746 if (rsync && ! debian_rsyncable && strstart > rsync_chunk_end) {
747 ct_init();
748 rsync_chunk_end = 0xFFFFFFFFUL;
749 flush = 1;
750 }
751 if (flush) FLUSH_BLOCK(0), block_start = strstart;
752 } else if (match_available) {
753 /* If there was no match at the previous position, output a
754 * single literal. If there was a match but the current match
755 * is longer, truncate the previous match to a single literal.
756 */
757 Tracevv((stderr,"%c",window[strstart-1]));
758 flush = ct_tally (pack_level, 0, window[strstart-1]);
759 if (rsync && debian_rsyncable && strstart > rsync_chunk_end) {
760 rsync_chunk_end = 0xFFFFFFFFUL;
761 flush = 2;
762 }
763 if (rsync && ! debian_rsyncable && strstart > rsync_chunk_end) {
764 ct_init();
765 rsync_chunk_end = 0xFFFFFFFFUL;
766 flush = 1;
767 }
768 if (flush) FLUSH_BLOCK(0), block_start = strstart;
769 RSYNC_ROLL(strstart, 1);
770 strstart++;
771 lookahead--;
772 } else {
773 /* There is no previous match to compare with, wait for
774 * the next step to decide.
775 */
776 if (rsync && debian_rsyncable && strstart > rsync_chunk_end) {
777 /* Reset huffman tree */
778 rsync_chunk_end = 0xFFFFFFFFUL;
779 flush = 2;
780 FLUSH_BLOCK(0), block_start = strstart;
781 }
782 if (rsync && ! debian_rsyncable && strstart > rsync_chunk_end) {
783 ct_init();
784 /* Reset huffman tree */
785 rsync_chunk_end = 0xFFFFFFFFUL;
786 FLUSH_BLOCK(0), block_start = strstart;
787 }
788 match_available = 1;
789 RSYNC_ROLL(strstart, 1);
790 strstart++;
791 lookahead--;
792 }
793 /* Assert (strstart <= bytes_in && lookahead <= bytes_in, "a bit too far"); */
794
795 /* Make sure that we always have enough lookahead, except
796 * at the end of the input file. We need MAX_MATCH bytes
797 * for the next match, plus MIN_MATCH bytes to insert the
798 * string following the next match.
799 */
800 while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
801 }
802 if (match_available) ct_tally (pack_level, 0, window[strstart-1]);
803
804 FLUSH_BLOCK(1); /* eof */
805 }