]> git.lizzy.rs Git - zlib.git/blob - deflate.c
zlib 0.92
[zlib.git] / deflate.c
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.8 1995/05/03 17:27:08 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 local int  longest_match __P((deflate_state *s, IPos cur_match));
121 local void putShortMSB   __P((deflate_state *s, uInt b));
122 local void flush_pending __P((z_stream *strm));
123 local int read_buf       __P((z_stream *strm, char *buf, unsigned size));
124 #ifdef ASMV
125       void match_init __P((void)); /* asm code initialization */
126 #endif
127
128 #ifdef DEBUG
129 local  void check_match __P((deflate_state *s, IPos start, IPos match,
130                              int length));
131 #endif
132
133
134 /* ===========================================================================
135  * Update a hash value with the given input byte
136  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
137  *    input characters, so that a running hash key can be computed from the
138  *    previous key instead of complete recalculation each time.
139  */
140 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
141
142 /* ===========================================================================
143  * Insert string str in the dictionary and set match_head to the previous head
144  * of the hash chain (the most recent string with same hash key). Return
145  * the previous length of the hash chain.
146  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
147  *    input characters and the first MIN_MATCH bytes of str are valid
148  *    (except for the last MIN_MATCH-1 bytes of the input file).
149  */
150 #define INSERT_STRING(s, str, match_head) \
151    (UPDATE_HASH(s, s->ins_h, s->window[(str) + MIN_MATCH-1]), \
152     s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
153     s->head[s->ins_h] = (str))
154
155 /* ===========================================================================
156  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
157  * prev[] will be initialized on the fly.
158  */
159 #define CLEAR_HASH(s) \
160     s->head[s->hash_size-1] = NIL; \
161     zmemzero((char*)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
162
163 /* ========================================================================= */
164 int deflateInit (strm, level)
165     z_stream *strm;
166     int level;
167 {
168     return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0);
169     /* To do: ignore strm->next_in if we use it as window */
170 }
171
172 /* ========================================================================= */
173 int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
174     z_stream *strm;
175     int  level;
176     int  method;
177     int  windowBits;
178     int  memLevel;
179     int  strategy;
180 {
181     deflate_state *s;
182     int noheader = 0;
183
184     if (strm == Z_NULL) return Z_STREAM_ERROR;
185
186     strm->msg = Z_NULL;
187     if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc;
188     if (strm->zfree == Z_NULL) strm->zfree = zcfree;
189
190     if (level == Z_DEFAULT_COMPRESSION) level = 6;
191
192     if (windowBits < 0) { /* undocumented feature: suppress zlib header */
193         noheader = 1;
194         windowBits = -windowBits;
195     }
196     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED ||
197         windowBits < 8 || windowBits > 15 || level < 1 || level > 9) {
198         return Z_STREAM_ERROR;
199     }
200     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
201     if (s == Z_NULL) return Z_MEM_ERROR;
202     strm->state = (struct internal_state *)s;
203     s->strm = strm;
204
205     s->noheader = noheader;
206     s->w_bits = windowBits;
207     s->w_size = 1 << s->w_bits;
208     s->w_mask = s->w_size - 1;
209
210     s->hash_bits = memLevel + 7;
211     s->hash_size = 1 << s->hash_bits;
212     s->hash_mask = s->hash_size - 1;
213     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
214
215     s->window = (Byte*) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
216     s->prev   = (Pos*)  ZALLOC(strm, s->w_size, sizeof(Pos));
217     s->head   = (Pos*)  ZALLOC(strm, s->hash_size, sizeof(Pos));
218
219     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
220
221     s->pending_buf = (uch*) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush));
222
223     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
224         s->pending_buf == Z_NULL) {
225         strm->msg = z_errmsg[1-Z_MEM_ERROR];
226         deflateEnd (strm);
227         return Z_MEM_ERROR;
228     }
229     s->d_buf = (ush*) &(s->pending_buf[s->lit_bufsize]);
230     s->l_buf = (uch*) &(s->pending_buf[3*s->lit_bufsize]);
231     /* We overlay pending_buf and d_buf+l_buf. This works since the average
232      * output size for (length,distance) codes is <= 32 bits (worst case
233      * is 15+15+13=33).
234      */
235
236     s->level = level;
237     s->strategy = strategy;
238     s->method = (Byte)method;
239
240     return deflateReset(strm);
241 }
242
243 /* ========================================================================= */
244 int deflateReset (strm)
245     z_stream *strm;
246 {
247     deflate_state *s;
248     
249     if (strm == Z_NULL || strm->state == Z_NULL ||
250         strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
251
252     strm->total_in = strm->total_out = 0;
253     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
254     strm->data_type = Z_UNKNOWN;
255
256     s = (deflate_state *)strm->state;
257     s->pending = 0;
258     s->pending_out = s->pending_buf;
259
260     s->status = s->noheader ? BUSY_STATE : INIT_STATE;
261     s->adler = 1;
262
263     ct_init(s);
264     lm_init(s);
265
266     return Z_OK;
267 }
268
269 /* =========================================================================
270  * Put a short the pending_out buffer. The 16-bit value is put in MSB order.
271  * IN assertion: the stream state is correct and there is enough room in
272  * the pending_out buffer.
273  */
274 local void putShortMSB (s, b)
275     deflate_state *s;
276     uInt b;
277 {
278     put_byte(s, (Byte)(b >> 8));
279     put_byte(s, (Byte)(b & 0xff));
280 }   
281
282 /* =========================================================================
283  * Flush as much pending output as possible.
284  */
285 local void flush_pending(strm)
286     z_stream *strm;
287 {
288     unsigned len = strm->state->pending;
289
290     if (len > strm->avail_out) len = strm->avail_out;
291     if (len == 0) return;
292
293     zmemcpy(strm->next_out, strm->state->pending_out, len);
294     strm->next_out  += len;
295     strm->state->pending_out  += len;
296     strm->total_out += len;
297     strm->avail_out  -= len;
298     strm->state->pending -= len;
299     if (strm->state->pending == 0) {
300         strm->state->pending_out = strm->state->pending_buf;
301     }
302 }
303
304 /* ========================================================================= */
305 int deflate (strm, flush)
306     z_stream *strm;
307     int flush;
308 {
309     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
310     
311     if (strm->next_out == Z_NULL || strm->next_in == Z_NULL) {
312         ERR_RETURN(strm, Z_STREAM_ERROR);
313     }
314     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
315
316     strm->state->strm = strm; /* just in case */
317
318     /* Write the zlib header */
319     if (strm->state->status == INIT_STATE) {
320
321         uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8;
322         uInt level_flags = (strm->state->level-1) >> 1;
323
324         if (level_flags > 3) level_flags = 3;
325         header |= (level_flags << 6);
326         header += 31 - (header % 31);
327
328         strm->state->status = BUSY_STATE;
329         putShortMSB(strm->state, header);
330     }
331
332     /* Flush as much pending output as possible */
333     if (strm->state->pending != 0) {
334         flush_pending(strm);
335         if (strm->avail_out == 0) return Z_OK;
336     }
337
338     /* User must not provide more input after the first FINISH: */
339     if (strm->state->status == FINISH_STATE && strm->avail_in != 0) {
340         ERR_RETURN(strm, Z_BUF_ERROR);
341     }
342
343     /* Start a new block or continue the current one.
344      */
345     if (strm->avail_in != 0 ||
346         (flush == Z_FINISH && strm->state->status != FINISH_STATE)) {
347         int quit;
348         
349         if (flush == Z_FINISH) {
350             strm->state->status = FINISH_STATE;
351         }
352         if (strm->state->level <= 3) {
353             quit = deflate_fast(strm->state, flush);
354         } else {
355             quit = deflate_slow(strm->state, flush);
356         }
357         if (flush == Z_FULL_FLUSH) {
358             ct_stored_block(strm->state, (char*)0, 0L, 0); /* special marker */
359             flush_pending(strm);
360             CLEAR_HASH(strm->state);             /* forget history */
361             if (strm->avail_out == 0) return Z_OK;
362         }
363         if (quit) return Z_OK;
364     }
365     Assert(strm->avail_out > 0, "bug2");
366
367     if (flush != Z_FINISH) return Z_OK;
368     if (strm->state->noheader) return Z_STREAM_END;
369
370     /* Write the zlib trailer (adler32) */
371     putShortMSB(strm->state, (uInt)(strm->state->adler >> 16));
372     putShortMSB(strm->state, (uInt)(strm->state->adler & 0xffff));
373     flush_pending(strm);
374     /* If avail_out is zero, the application will call deflate again
375      * to flush the rest.
376      */
377     strm->state->noheader = 1; /* write the trailer only once! */
378     return strm->state->pending != 0 ? Z_OK : Z_STREAM_END;
379 }
380
381 /* ========================================================================= */
382 int deflateEnd (strm)
383     z_stream *strm;
384 {
385     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
386
387     TRY_FREE(strm, strm->state->window);
388     TRY_FREE(strm, strm->state->prev);
389     TRY_FREE(strm, strm->state->head);
390     TRY_FREE(strm, strm->state->pending_buf);
391
392     ZFREE(strm, strm->state);
393     strm->state = Z_NULL;
394
395     return Z_OK;
396 }
397
398 /* ========================================================================= */
399 int deflateCopy (dest, source)
400     z_stream *dest;
401     z_stream *source;
402 {
403     if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
404         return Z_STREAM_ERROR;
405     }
406     *dest = *source;
407     return Z_STREAM_ERROR; /* to be implemented */
408 #if 0
409     dest->state = (struct internal_state *)
410         (*dest->zalloc)(1, sizeof(deflate_state));
411     if (dest->state == Z_NULL) return Z_MEM_ERROR;
412
413     *(dest->state) = *(source->state);
414     return Z_OK;
415 #endif
416 }
417
418 /* ===========================================================================
419  * Read a new buffer from the current input stream, update the adler32
420  * and total number of bytes read.
421  */
422 local int read_buf(strm, buf, size)
423     z_stream *strm;
424     char *buf;
425     unsigned size;
426 {
427     unsigned len = strm->avail_in;
428
429     if (len > size) len = size;
430     if (len == 0) return 0;
431
432     strm->avail_in  -= len;
433
434     if (!strm->state->noheader) {
435         strm->state->adler = adler32(strm->state->adler, strm->next_in, len);
436     }
437     zmemcpy(buf, strm->next_in, len);
438     strm->next_in  += len;
439     strm->total_in += len;
440
441     return (int)len;
442 }
443
444 /* ===========================================================================
445  * Initialize the "longest match" routines for a new zlib stream
446  */
447 local void lm_init (s)
448     deflate_state *s;
449 {
450     register unsigned j;
451
452     s->window_size = (ulg)2L*s->w_size;
453
454     CLEAR_HASH(s);
455
456     /* Set the default configuration parameters:
457      */
458     s->max_lazy_match   = configuration_table[s->level].max_lazy;
459     s->good_match       = configuration_table[s->level].good_length;
460     s->nice_match       = configuration_table[s->level].nice_length;
461     s->max_chain_length = configuration_table[s->level].max_chain;
462
463     s->strstart = 0;
464     s->block_start = 0L;
465     s->lookahead = 0;
466     s->match_length = MIN_MATCH-1;
467     s->match_available = 0;
468 #ifdef ASMV
469     match_init(); /* initialize the asm code */
470 #endif
471
472     s->ins_h = 0;
473     for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(s, s->ins_h, s->window[j]);
474     /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
475      * not important since only literal bytes will be emitted.
476      */
477 }
478
479 /* ===========================================================================
480  * Set match_start to the longest match starting at the given string and
481  * return its length. Matches shorter or equal to prev_length are discarded,
482  * in which case the result is equal to prev_length and match_start is
483  * garbage.
484  * IN assertions: cur_match is the head of the hash chain for the current
485  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
486  */
487 #ifndef ASMV
488 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
489  * match.S. The code will be functionally equivalent.
490  */
491 local INLINE int longest_match(s, cur_match)
492     deflate_state *s;
493     IPos cur_match;                             /* current match */
494 {
495     unsigned chain_length = s->max_chain_length;/* max hash chain length */
496     register Byte *scan = s->window + s->strstart; /* current string */
497     register Byte *match;                       /* matched string */
498     register int len;                           /* length of current match */
499     int best_len = s->prev_length;              /* best match length so far */
500     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
501         s->strstart - (IPos)MAX_DIST(s) : NIL;
502     /* Stop when cur_match becomes <= limit. To simplify the code,
503      * we prevent matches with the string of window index 0.
504      */
505     Pos *prev = s->prev;
506     uInt wmask = s->w_mask;
507
508 #ifdef UNALIGNED_OK
509     /* Compare two bytes at a time. Note: this is not always beneficial.
510      * Try with and without -DUNALIGNED_OK to check.
511      */
512     register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
513     register ush scan_start = *(ush*)scan;
514     register ush scan_end   = *(ush*)(scan+best_len-1);
515 #else
516     register Byte *strend = s->window + s->strstart + MAX_MATCH;
517     register Byte scan_end1  = scan[best_len-1];
518     register Byte scan_end   = scan[best_len];
519 #endif
520
521     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
522      * It is easy to get rid of this optimization if necessary.
523      */
524     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
525
526     /* Do not waste too much time if we already have a good match: */
527     if (s->prev_length >= s->good_match) {
528         chain_length >>= 2;
529     }
530     Assert(s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
531
532     do {
533         Assert(cur_match < s->strstart, "no future");
534         match = s->window + cur_match;
535
536         /* Skip to next match if the match length cannot increase
537          * or if the match length is less than 2:
538          */
539 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
540         /* This code assumes sizeof(unsigned short) == 2. Do not use
541          * UNALIGNED_OK if your compiler uses a different size.
542          */
543         if (*(ush*)(match+best_len-1) != scan_end ||
544             *(ush*)match != scan_start) continue;
545
546         /* It is not necessary to compare scan[2] and match[2] since they are
547          * always equal when the other bytes match, given that the hash keys
548          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
549          * strstart+3, +5, ... up to strstart+257. We check for insufficient
550          * lookahead only every 4th comparison; the 128th check will be made
551          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
552          * necessary to put more guard bytes at the end of the window, or
553          * to check more often for insufficient lookahead.
554          */
555         scan++, match++;
556         do {
557         } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
558                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
559                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
560                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
561                  scan < strend);
562         /* The funny "do {}" generates better code on most compilers */
563
564         /* Here, scan <= window+strstart+257 */
565         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
566         if (*scan == *match) scan++;
567
568         len = (MAX_MATCH - 1) - (int)(strend-scan);
569         scan = strend - (MAX_MATCH-1);
570
571 #else /* UNALIGNED_OK */
572
573         if (match[best_len]   != scan_end  ||
574             match[best_len-1] != scan_end1 ||
575             *match            != *scan     ||
576             *++match          != scan[1])      continue;
577
578         /* The check at best_len-1 can be removed because it will be made
579          * again later. (This heuristic is not always a win.)
580          * It is not necessary to compare scan[2] and match[2] since they
581          * are always equal when the other bytes match, given that
582          * the hash keys are equal and that HASH_BITS >= 8.
583          */
584         scan += 2, match++;
585
586         /* We check for insufficient lookahead only every 8th comparison;
587          * the 256th check will be made at strstart+258.
588          */
589         do {
590         } while (*++scan == *++match && *++scan == *++match &&
591                  *++scan == *++match && *++scan == *++match &&
592                  *++scan == *++match && *++scan == *++match &&
593                  *++scan == *++match && *++scan == *++match &&
594                  scan < strend);
595
596         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
597
598         len = MAX_MATCH - (int)(strend - scan);
599         scan = strend - MAX_MATCH;
600
601 #endif /* UNALIGNED_OK */
602
603         if (len > best_len) {
604             s->match_start = cur_match;
605             best_len = len;
606             if (len >= s->nice_match) break;
607 #ifdef UNALIGNED_OK
608             scan_end = *(ush*)(scan+best_len-1);
609 #else
610             scan_end1  = scan[best_len-1];
611             scan_end   = scan[best_len];
612 #endif
613         }
614     } while ((cur_match = prev[cur_match & wmask]) > limit
615              && --chain_length != 0);
616
617     return best_len;
618 }
619 #endif /* ASMV */
620
621 #ifdef DEBUG
622 /* ===========================================================================
623  * Check that the match at match_start is indeed a match.
624  */
625 local void check_match(s, start, match, length)
626     deflate_state *s;
627     IPos start, match;
628     int length;
629 {
630     /* check that the match is indeed a match */
631     if (memcmp((char*)s->window + match,
632                 (char*)s->window + start, length) != EQUAL) {
633         fprintf(stderr,
634             " start %d, match %d, length %d\n",
635             start, match, length);
636         z_error("invalid match");
637     }
638     if (verbose > 1) {
639         fprintf(stderr,"\\[%d,%d]", start-match, length);
640         do { putc(s->window[start++], stderr); } while (--length != 0);
641     }
642 }
643 #else
644 #  define check_match(s, start, match, length)
645 #endif
646
647 /* ===========================================================================
648  * Fill the window when the lookahead becomes insufficient.
649  * Updates strstart and lookahead.
650  *
651  * IN assertion: lookahead < MIN_LOOKAHEAD
652  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
653  *    At least one byte has been read, or avail_in == 0; reads are
654  *    performed for at least two bytes (required for the zip translate_eol
655  *    option -- not supported here).
656  */
657 local void fill_window(s)
658     deflate_state *s;
659 {
660     register unsigned n, m;
661     register Pos *p;
662     unsigned more;    /* Amount of free space at the end of the window. */
663     uInt wsize = s->w_size;
664
665     do {
666         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
667
668         /* Deal with !@#$% 64K limit: */
669         if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
670             more = wsize;
671         } else if (more == (unsigned)(-1)) {
672             /* Very unlikely, but possible on 16 bit machine if strstart == 0
673              * and lookahead == 1 (input done one byte at time)
674              */
675             more--;
676
677         /* If the window is almost full and there is insufficient lookahead,
678          * move the upper half to the lower one to make room in the upper half.
679          */
680         } else if (s->strstart >= wsize+MAX_DIST(s)) {
681
682             /* By the IN assertion, the window is not empty so we can't confuse
683              * more == 0 with more == 64K on a 16 bit machine.
684              */
685             zmemcpy((char*)s->window, (char*)s->window+wsize,
686                    (unsigned)wsize);
687             s->match_start -= wsize;
688             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
689
690             s->block_start -= (long) wsize;
691
692             /* Slide the hash table (could be avoided with 32 bit values
693                at the expense of memory usage):
694              */
695             n = s->hash_size;
696             p = &s->head[n-1];
697             do {
698                 m = *p;
699                 *p-- = (Pos)(m >= wsize ? m-wsize : NIL);
700             } while (--n);
701
702             n = wsize;
703             p = &s->prev[n-1];
704             do {
705                 m = *p;
706                 *p-- = (Pos)(m >= wsize ? m-wsize : NIL);
707                 /* If n is not on any hash chain, prev[n] is garbage but
708                  * its value will never be used.
709                  */
710             } while (--n);
711
712             more += wsize;
713         }
714         if (s->strm->avail_in == 0) return;
715
716         /* If there was no sliding:
717          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
718          *    more == window_size - lookahead - strstart
719          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
720          * => more >= window_size - 2*WSIZE + 2
721          * In the BIG_MEM or MMAP case (not yet supported),
722          *   window_size == input_size + MIN_LOOKAHEAD  &&
723          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
724          * Otherwise, window_size == 2*WSIZE so more >= 2.
725          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
726          */
727         Assert(more >= 2, "more < 2");
728
729         n = read_buf(s->strm, (char*)s->window + s->strstart + s->lookahead,
730                      more);
731         s->lookahead += n;
732
733     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
734 }
735
736 /* ===========================================================================
737  * Flush the current block, with given end-of-file flag.
738  * IN assertion: strstart is set to the end of the current match.
739  */
740 #define FLUSH_BLOCK_ONLY(s, eof) { \
741    ct_flush_block(s, (s->block_start >= 0L ? \
742                (char*)&s->window[(unsigned)s->block_start] : \
743                (char*)Z_NULL), (long)s->strstart - s->block_start, (eof)); \
744    s->block_start = s->strstart; \
745    flush_pending(s->strm); \
746 }
747
748 /* Same but force premature exit if necessary. */
749 #define FLUSH_BLOCK(s, eof) { \
750    FLUSH_BLOCK_ONLY(s, eof); \
751    if (s->strm->avail_out == 0) return 1; \
752 }
753
754 /* ===========================================================================
755  * Compress as much as possible from the input stream, return true if
756  * processing was terminated prematurely (no more input or output space).
757  * This function does not perform lazy evaluationof matches and inserts
758  * new strings in the dictionary only for unmatched strings or for short
759  * matches. It is used only for the fast compression options.
760  */
761 local int deflate_fast(s, flush)
762     deflate_state *s;
763     int flush;
764 {
765     IPos hash_head; /* head of the hash chain */
766     int bflush;     /* set if current block must be flushed */
767
768     s->prev_length = MIN_MATCH-1;
769
770     for (;;) {
771         /* Make sure that we always have enough lookahead, except
772          * at the end of the input file. We need MAX_MATCH bytes
773          * for the next match, plus MIN_MATCH bytes to insert the
774          * string following the next match.
775          */
776         if (s->lookahead < MIN_LOOKAHEAD) {
777             fill_window(s);
778             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
779
780             if (s->lookahead == 0) break; /* flush the current block */
781         }
782
783         /* Insert the string window[strstart .. strstart+2] in the
784          * dictionary, and set hash_head to the head of the hash chain:
785          */
786         INSERT_STRING(s, s->strstart, hash_head);
787
788         /* Find the longest match, discarding those <= prev_length.
789          * At this point we have always match_length < MIN_MATCH
790          */
791         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
792             /* To simplify the code, we prevent matches with the string
793              * of window index 0 (in particular we have to avoid a match
794              * of the string with itself at the start of the input file).
795              */
796             if (s->strategy != Z_HUFFMAN_ONLY) {
797                 s->match_length = longest_match (s, hash_head);
798             }
799             /* longest_match() sets match_start */
800
801             if (s->match_length > s->lookahead) s->match_length = s->lookahead;
802         }
803         if (s->match_length >= MIN_MATCH) {
804             check_match(s, s->strstart, s->match_start, s->match_length);
805
806             bflush = ct_tally(s, s->strstart - s->match_start,
807                               s->match_length - MIN_MATCH);
808
809             s->lookahead -= s->match_length;
810
811             /* Insert new strings in the hash table only if the match length
812              * is not too large. This saves time but degrades compression.
813              */
814             if (s->match_length <= s->max_insert_length) {
815                 s->match_length--; /* string at strstart already in hash table */
816                 do {
817                     s->strstart++;
818                     INSERT_STRING(s, s->strstart, hash_head);
819                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
820                      * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
821                      * these bytes are garbage, but it does not matter since
822                      * the next lookahead bytes will be emitted as literals.
823                      */
824                 } while (--s->match_length != 0);
825                 s->strstart++; 
826             } else {
827                 s->strstart += s->match_length;
828                 s->match_length = 0;
829                 s->ins_h = s->window[s->strstart];
830                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
831 #if MIN_MATCH != 3
832                 Call UPDATE_HASH() MIN_MATCH-3 more times
833 #endif
834             }
835         } else {
836             /* No match, output a literal byte */
837             Tracevv((stderr,"%c", s->window[s->strstart]));
838             bflush = ct_tally (s, 0, s->window[s->strstart]);
839             s->lookahead--;
840             s->strstart++; 
841         }
842         if (bflush) FLUSH_BLOCK(s, 0);
843     }
844     FLUSH_BLOCK(s, flush == Z_FINISH);
845     return 0; /* normal exit */
846 }
847
848 /* ===========================================================================
849  * Same as above, but achieves better compression. We use a lazy
850  * evaluation for matches: a match is finally adopted only if there is
851  * no better match at the next window position.
852  */
853 local int deflate_slow(s, flush)
854     deflate_state *s;
855     int flush;
856 {
857     IPos hash_head;          /* head of hash chain */
858     int bflush;              /* set if current block must be flushed */
859
860     /* Process the input block. */
861     for (;;) {
862         /* Make sure that we always have enough lookahead, except
863          * at the end of the input file. We need MAX_MATCH bytes
864          * for the next match, plus MIN_MATCH bytes to insert the
865          * string following the next match.
866          */
867         if (s->lookahead < MIN_LOOKAHEAD) {
868             fill_window(s);
869             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1;
870
871             if (s->lookahead == 0) break; /* flush the current block */
872         }
873
874         /* Insert the string window[strstart .. strstart+2] in the
875          * dictionary, and set hash_head to the head of the hash chain:
876          */
877         INSERT_STRING(s, s->strstart, hash_head);
878
879         /* Find the longest match, discarding those <= prev_length.
880          */
881         s->prev_length = s->match_length, s->prev_match = s->match_start;
882         s->match_length = MIN_MATCH-1;
883
884         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
885             s->strstart - hash_head <= MAX_DIST(s)) {
886             /* To simplify the code, we prevent matches with the string
887              * of window index 0 (in particular we have to avoid a match
888              * of the string with itself at the start of the input file).
889              */
890             if (s->strategy != Z_HUFFMAN_ONLY) {
891                 s->match_length = longest_match (s, hash_head);
892             }
893             /* longest_match() sets match_start */
894             if (s->match_length > s->lookahead) s->match_length = s->lookahead;
895
896             if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
897                  (s->match_length == MIN_MATCH &&
898                   s->strstart - s->match_start > TOO_FAR))) {
899
900                 /* If prev_match is also MIN_MATCH, match_start is garbage
901                  * but we will ignore the current match anyway.
902                  */
903                 s->match_length = MIN_MATCH-1;
904             }
905         }
906         /* If there was a match at the previous step and the current
907          * match is not better, output the previous match:
908          */
909         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
910
911             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
912
913             bflush = ct_tally(s, s->strstart -1 - s->prev_match,
914                               s->prev_length - MIN_MATCH);
915
916             /* Insert in hash table all strings up to the end of the match.
917              * strstart-1 and strstart are already inserted.
918              */
919             s->lookahead -= s->prev_length-1;
920             s->prev_length -= 2;
921             do {
922                 s->strstart++;
923                 INSERT_STRING(s, s->strstart, hash_head);
924                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
925                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
926                  * these bytes are garbage, but it does not matter since the
927                  * next lookahead bytes will always be emitted as literals.
928                  */
929             } while (--s->prev_length != 0);
930             s->match_available = 0;
931             s->match_length = MIN_MATCH-1;
932             s->strstart++;
933
934             if (bflush) FLUSH_BLOCK(s, 0);
935
936         } else if (s->match_available) {
937             /* If there was no match at the previous position, output a
938              * single literal. If there was a match but the current match
939              * is longer, truncate the previous match to a single literal.
940              */
941             Tracevv((stderr,"%c", s->window[s->strstart-1]));
942             if (ct_tally (s, 0, s->window[s->strstart-1])) {
943                 FLUSH_BLOCK_ONLY(s, 0);
944             }
945             s->strstart++;
946             s->lookahead--;
947             if (s->strm->avail_out == 0) return 1;
948         } else {
949             /* There is no previous match to compare with, wait for
950              * the next step to decide.
951              */
952             s->match_available = 1;
953             s->strstart++;
954             s->lookahead--;
955         }
956     }
957     if (s->match_available) {
958         ct_tally (s, 0, s->window[s->strstart-1]);
959         s->match_available = 0;
960     }
961     FLUSH_BLOCK(s, flush == Z_FINISH);
962     return 0;
963 }