Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/gzip/deflate.c

Parent Directory Parent Directory | Revision Log Revision Log


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