xref: /netbsd/common/dist/zlib/deflate.c (revision d9a827c3)
1 /*	$NetBSD: deflate.c,v 1.6 2022/10/15 19:49:32 christos Exp $	*/
2 
3 /* deflate.c -- compress data using the deflation algorithm
4  * Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
5  * For conditions of distribution and use, see copyright notice in zlib.h
6  */
7 
8 /*
9  *  ALGORITHM
10  *
11  *      The "deflation" process depends on being able to identify portions
12  *      of the input text which are identical to earlier input (within a
13  *      sliding window trailing behind the input currently being processed).
14  *
15  *      The most straightforward technique turns out to be the fastest for
16  *      most input files: try all possible matches and select the longest.
17  *      The key feature of this algorithm is that insertions into the string
18  *      dictionary are very simple and thus fast, and deletions are avoided
19  *      completely. Insertions are performed at each input character, whereas
20  *      string matches are performed only when the previous match ends. So it
21  *      is preferable to spend more time in matches to allow very fast string
22  *      insertions and avoid deletions. The matching algorithm for small
23  *      strings is inspired from that of Rabin & Karp. A brute force approach
24  *      is used to find longer strings when a small match has been found.
25  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
26  *      (by Leonid Broukhis).
27  *         A previous version of this file used a more sophisticated algorithm
28  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
29  *      time, but has a larger average cost, uses more memory and is patented.
30  *      However the F&G algorithm may be faster for some highly redundant
31  *      files if the parameter max_chain_length (described below) is too large.
32  *
33  *  ACKNOWLEDGEMENTS
34  *
35  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
36  *      I found it in 'freeze' written by Leonid Broukhis.
37  *      Thanks to many people for bug reports and testing.
38  *
39  *  REFERENCES
40  *
41  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
42  *      Available in http://tools.ietf.org/html/rfc1951
43  *
44  *      A description of the Rabin and Karp algorithm is given in the book
45  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
46  *
47  *      Fiala,E.R., and Greene,D.H.
48  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
49  *
50  */
51 
52 /* @(#) Id */
53 
54 #include "deflate.h"
55 
56 const char deflate_copyright[] =
57    " deflate 1.2.13 Copyright 1995-2022 Jean-loup Gailly and Mark Adler ";
58 /*
59   If you use the zlib library in a product, an acknowledgment is welcome
60   in the documentation of your product. If for some reason you cannot
61   include such an acknowledgment, I would appreciate that you keep this
62   copyright string in the executable of your product.
63  */
64 
65 /* ===========================================================================
66  *  Function prototypes.
67  */
68 typedef enum {
69     need_more,      /* block not completed, need more input or more output */
70     block_done,     /* block flush performed */
71     finish_started, /* finish started, need only more output at next deflate */
72     finish_done     /* finish done, accept no more input or output */
73 } block_state;
74 
75 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
76 /* Compression function. Returns the block state after the call. */
77 
78 local int deflateStateCheck      OF((z_streamp strm));
79 local void slide_hash     OF((deflate_state *s));
80 local void fill_window    OF((deflate_state *s));
81 local block_state deflate_stored OF((deflate_state *s, int flush));
82 local block_state deflate_fast   OF((deflate_state *s, int flush));
83 #ifndef FASTEST
84 local block_state deflate_slow   OF((deflate_state *s, int flush));
85 #endif
86 local block_state deflate_rle    OF((deflate_state *s, int flush));
87 local block_state deflate_huff   OF((deflate_state *s, int flush));
88 local void lm_init        OF((deflate_state *s));
89 local void putShortMSB    OF((deflate_state *s, uInt b));
90 local void flush_pending  OF((z_streamp strm));
91 local unsigned read_buf   OF((z_streamp strm, Bytef *buf, unsigned size));
92 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
93 
94 #ifdef ZLIB_DEBUG
95 local  void check_match OF((deflate_state *s, IPos start, IPos match,
96                             int length));
97 #endif
98 
99 /* ===========================================================================
100  * Local data
101  */
102 
103 #define NIL 0
104 /* Tail of hash chains */
105 
106 #ifndef TOO_FAR
107 #  define TOO_FAR 4096
108 #endif
109 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
110 
111 /* Values for max_lazy_match, good_match and max_chain_length, depending on
112  * the desired pack level (0..9). The values given below have been tuned to
113  * exclude worst case performance for pathological files. Better values may be
114  * found for specific files.
115  */
116 typedef struct config_s {
117    ush good_length; /* reduce lazy search above this match length */
118    ush max_lazy;    /* do not perform lazy search above this match length */
119    ush nice_length; /* quit search above this match length */
120    ush max_chain;
121    compress_func func;
122 } config;
123 
124 #ifdef FASTEST
125 local const config configuration_table[2] = {
126 /*      good lazy nice chain */
127 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
128 /* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
129 #else
130 local const config configuration_table[10] = {
131 /*      good lazy nice chain */
132 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
133 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
134 /* 2 */ {4,    5, 16,    8, deflate_fast},
135 /* 3 */ {4,    6, 32,   32, deflate_fast},
136 
137 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
138 /* 5 */ {8,   16, 32,   32, deflate_slow},
139 /* 6 */ {8,   16, 128, 128, deflate_slow},
140 /* 7 */ {8,   32, 128, 256, deflate_slow},
141 /* 8 */ {32, 128, 258, 1024, deflate_slow},
142 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
143 #endif
144 
145 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
146  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
147  * meaning.
148  */
149 
150 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
151 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
152 
153 /* ===========================================================================
154  * Update a hash value with the given input byte
155  * IN  assertion: all calls to UPDATE_HASH are made with consecutive input
156  *    characters, so that a running hash key can be computed from the previous
157  *    key instead of complete recalculation each time.
158  */
159 #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
160 
161 
162 /* ===========================================================================
163  * Insert string str in the dictionary and set match_head to the previous head
164  * of the hash chain (the most recent string with same hash key). Return
165  * the previous length of the hash chain.
166  * If this file is compiled with -DFASTEST, the compression level is forced
167  * to 1, and no hash chains are maintained.
168  * IN  assertion: all calls to INSERT_STRING are made with consecutive input
169  *    characters and the first MIN_MATCH bytes of str are valid (except for
170  *    the last MIN_MATCH-1 bytes of the input file).
171  */
172 #ifdef FASTEST
173 #define INSERT_STRING(s, str, match_head) \
174    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
175     match_head = s->head[s->ins_h], \
176     s->head[s->ins_h] = (Pos)(str))
177 #else
178 #define INSERT_STRING(s, str, match_head) \
179    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
180     match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
181     s->head[s->ins_h] = (Pos)(str))
182 #endif
183 
184 /* ===========================================================================
185  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
186  * prev[] will be initialized on the fly.
187  */
188 #define CLEAR_HASH(s) \
189     do { \
190         s->head[s->hash_size - 1] = NIL; \
191         zmemzero((Bytef *)s->head, \
192                  (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
193     } while (0)
194 
195 /* ===========================================================================
196  * Slide the hash table when sliding the window down (could be avoided with 32
197  * bit values at the expense of memory usage). We slide even when level == 0 to
198  * keep the hash table consistent if we switch back to level > 0 later.
199  */
slide_hash(s)200 local void slide_hash(s)
201     deflate_state *s;
202 {
203     unsigned n, m;
204     Posf *p;
205     uInt wsize = s->w_size;
206 
207     n = s->hash_size;
208     p = &s->head[n];
209     do {
210         m = *--p;
211         *p = (Pos)(m >= wsize ? m - wsize : NIL);
212     } while (--n);
213     n = wsize;
214 #ifndef FASTEST
215     p = &s->prev[n];
216     do {
217         m = *--p;
218         *p = (Pos)(m >= wsize ? m - wsize : NIL);
219         /* If n is not on any hash chain, prev[n] is garbage but
220          * its value will never be used.
221          */
222     } while (--n);
223 #endif
224 }
225 
226 /* ========================================================================= */
deflateInit_(strm,level,version,stream_size)227 int ZEXPORT deflateInit_(strm, level, version, stream_size)
228     z_streamp strm;
229     int level;
230     const char *version;
231     int stream_size;
232 {
233     return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
234                          Z_DEFAULT_STRATEGY, version, stream_size);
235     /* To do: ignore strm->next_in if we use it as window */
236 }
237 
238 /* ========================================================================= */
deflateInit2_(strm,level,method,windowBits,memLevel,strategy,version,stream_size)239 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
240                   version, stream_size)
241     z_streamp strm;
242     int  level;
243     int  method;
244     int  windowBits;
245     int  memLevel;
246     int  strategy;
247     const char *version;
248     int stream_size;
249 {
250     deflate_state *s;
251     int wrap = 1;
252     static const char my_version[] = ZLIB_VERSION;
253 
254     if (version == Z_NULL || version[0] != my_version[0] ||
255         stream_size != sizeof(z_stream)) {
256         return Z_VERSION_ERROR;
257     }
258     if (strm == Z_NULL) return Z_STREAM_ERROR;
259 
260     strm->msg = Z_NULL;
261     if (strm->zalloc == (alloc_func)0) {
262 #ifdef Z_SOLO
263         return Z_STREAM_ERROR;
264 #else
265         strm->zalloc = zcalloc;
266         strm->opaque = (voidpf)0;
267 #endif
268     }
269     if (strm->zfree == (free_func)0)
270 #ifdef Z_SOLO
271         return Z_STREAM_ERROR;
272 #else
273         strm->zfree = zcfree;
274 #endif
275 
276 #ifdef FASTEST
277     if (level != 0) level = 1;
278 #else
279     if (level == Z_DEFAULT_COMPRESSION) level = 6;
280 #endif
281 
282     if (windowBits < 0) { /* suppress zlib wrapper */
283         wrap = 0;
284         if (windowBits < -15)
285             return Z_STREAM_ERROR;
286         windowBits = -windowBits;
287     }
288 #ifdef GZIP
289     else if (windowBits > 15) {
290         wrap = 2;       /* write gzip wrapper instead */
291         windowBits -= 16;
292     }
293 #endif
294     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
295         windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
296         strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
297         return Z_STREAM_ERROR;
298     }
299     if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
300     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
301     if (s == Z_NULL) return Z_MEM_ERROR;
302     strm->state = (struct internal_state FAR *)s;
303     s->strm = strm;
304     s->status = INIT_STATE;     /* to pass state test in deflateReset() */
305 
306     s->wrap = wrap;
307     s->gzhead = Z_NULL;
308     s->w_bits = (uInt)windowBits;
309     s->w_size = 1 << s->w_bits;
310     s->w_mask = s->w_size - 1;
311 
312     s->hash_bits = (uInt)memLevel + 7;
313     s->hash_size = 1 << s->hash_bits;
314     s->hash_mask = s->hash_size - 1;
315     s->hash_shift =  ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
316 
317     s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
318     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
319     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
320 
321     s->high_water = 0;      /* nothing written to s->window yet */
322 
323     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
324 
325     /* We overlay pending_buf and sym_buf. This works since the average size
326      * for length/distance pairs over any compressed block is assured to be 31
327      * bits or less.
328      *
329      * Analysis: The longest fixed codes are a length code of 8 bits plus 5
330      * extra bits, for lengths 131 to 257. The longest fixed distance codes are
331      * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
332      * possible fixed-codes length/distance pair is then 31 bits total.
333      *
334      * sym_buf starts one-fourth of the way into pending_buf. So there are
335      * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
336      * in sym_buf is three bytes -- two for the distance and one for the
337      * literal/length. As each symbol is consumed, the pointer to the next
338      * sym_buf value to read moves forward three bytes. From that symbol, up to
339      * 31 bits are written to pending_buf. The closest the written pending_buf
340      * bits gets to the next sym_buf symbol to read is just before the last
341      * code is written. At that time, 31*(n - 2) bits have been written, just
342      * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
343      * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
344      * symbols are written.) The closest the writing gets to what is unread is
345      * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
346      * can range from 128 to 32768.
347      *
348      * Therefore, at a minimum, there are 142 bits of space between what is
349      * written and what is read in the overlain buffers, so the symbols cannot
350      * be overwritten by the compressed data. That space is actually 139 bits,
351      * due to the three-bit fixed-code block header.
352      *
353      * That covers the case where either Z_FIXED is specified, forcing fixed
354      * codes, or when the use of fixed codes is chosen, because that choice
355      * results in a smaller compressed block than dynamic codes. That latter
356      * condition then assures that the above analysis also covers all dynamic
357      * blocks. A dynamic-code block will only be chosen to be emitted if it has
358      * fewer bits than a fixed-code block would for the same set of symbols.
359      * Therefore its average symbol length is assured to be less than 31. So
360      * the compressed data for a dynamic block also cannot overwrite the
361      * symbols from which it is being constructed.
362      */
363 
364     s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
365     s->pending_buf_size = (ulg)s->lit_bufsize * 4;
366 
367     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
368         s->pending_buf == Z_NULL) {
369         s->status = FINISH_STATE;
370         strm->msg = __UNCONST(ERR_MSG(Z_MEM_ERROR));
371         deflateEnd (strm);
372         return Z_MEM_ERROR;
373     }
374     s->sym_buf = s->pending_buf + s->lit_bufsize;
375     s->sym_end = (s->lit_bufsize - 1) * 3;
376     /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
377      * on 16 bit machines and because stored blocks are restricted to
378      * 64K-1 bytes.
379      */
380 
381     s->level = level;
382     s->strategy = strategy;
383     s->method = (Byte)method;
384 
385     return deflateReset(strm);
386 }
387 
388 /* =========================================================================
389  * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
390  */
deflateStateCheck(strm)391 local int deflateStateCheck(strm)
392     z_streamp strm;
393 {
394     deflate_state *s;
395     if (strm == Z_NULL ||
396         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
397         return 1;
398     s = strm->state;
399     if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
400 #ifdef GZIP
401                                            s->status != GZIP_STATE &&
402 #endif
403                                            s->status != EXTRA_STATE &&
404                                            s->status != NAME_STATE &&
405                                            s->status != COMMENT_STATE &&
406                                            s->status != HCRC_STATE &&
407                                            s->status != BUSY_STATE &&
408                                            s->status != FINISH_STATE))
409         return 1;
410     return 0;
411 }
412 
413 /* ========================================================================= */
deflateSetDictionary(strm,dictionary,dictLength)414 int ZEXPORT deflateSetDictionary(strm, dictionary, dictLength)
415     z_streamp strm;
416     const Bytef *dictionary;
417     uInt  dictLength;
418 {
419     deflate_state *s;
420     uInt str, n;
421     int wrap;
422     unsigned avail;
423     z_const unsigned char *next;
424 
425     if (deflateStateCheck(strm) || dictionary == Z_NULL)
426         return Z_STREAM_ERROR;
427     s = strm->state;
428     wrap = s->wrap;
429     if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
430         return Z_STREAM_ERROR;
431 
432     /* when using zlib wrappers, compute Adler-32 for provided dictionary */
433     if (wrap == 1)
434         strm->adler = adler32(strm->adler, dictionary, dictLength);
435     s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
436 
437     /* if dictionary would fill window, just replace the history */
438     if (dictLength >= s->w_size) {
439         if (wrap == 0) {            /* already empty otherwise */
440             CLEAR_HASH(s);
441             s->strstart = 0;
442             s->block_start = 0L;
443             s->insert = 0;
444         }
445         dictionary += dictLength - s->w_size;  /* use the tail */
446         dictLength = s->w_size;
447     }
448 
449     /* insert dictionary into window and hash */
450     avail = strm->avail_in;
451     next = strm->next_in;
452     strm->avail_in = dictLength;
453     strm->next_in = __UNCONST(dictionary);
454     fill_window(s);
455     while (s->lookahead >= MIN_MATCH) {
456         str = s->strstart;
457         n = s->lookahead - (MIN_MATCH-1);
458         do {
459             UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
460 #ifndef FASTEST
461             s->prev[str & s->w_mask] = s->head[s->ins_h];
462 #endif
463             s->head[s->ins_h] = (Pos)str;
464             str++;
465         } while (--n);
466         s->strstart = str;
467         s->lookahead = MIN_MATCH-1;
468         fill_window(s);
469     }
470     s->strstart += s->lookahead;
471     s->block_start = (long)s->strstart;
472     s->insert = s->lookahead;
473     s->lookahead = 0;
474     s->match_length = s->prev_length = MIN_MATCH-1;
475     s->match_available = 0;
476     strm->next_in = next;
477     strm->avail_in = avail;
478     s->wrap = wrap;
479     return Z_OK;
480 }
481 
482 /* ========================================================================= */
deflateGetDictionary(strm,dictionary,dictLength)483 int ZEXPORT deflateGetDictionary(strm, dictionary, dictLength)
484     z_streamp strm;
485     Bytef *dictionary;
486     uInt  *dictLength;
487 {
488     deflate_state *s;
489     uInt len;
490 
491     if (deflateStateCheck(strm))
492         return Z_STREAM_ERROR;
493     s = strm->state;
494     len = s->strstart + s->lookahead;
495     if (len > s->w_size)
496         len = s->w_size;
497     if (dictionary != Z_NULL && len)
498         zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
499     if (dictLength != Z_NULL)
500         *dictLength = len;
501     return Z_OK;
502 }
503 
504 /* ========================================================================= */
deflateResetKeep(strm)505 int ZEXPORT deflateResetKeep(strm)
506     z_streamp strm;
507 {
508     deflate_state *s;
509 
510     if (deflateStateCheck(strm)) {
511         return Z_STREAM_ERROR;
512     }
513 
514     strm->total_in = strm->total_out = 0;
515     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
516     strm->data_type = Z_UNKNOWN;
517 
518     s = (deflate_state *)strm->state;
519     s->pending = 0;
520     s->pending_out = s->pending_buf;
521 
522     if (s->wrap < 0) {
523         s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
524     }
525     s->status =
526 #ifdef GZIP
527         s->wrap == 2 ? GZIP_STATE :
528 #endif
529         INIT_STATE;
530     strm->adler =
531 #ifdef GZIP
532         s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
533 #endif
534         adler32(0L, Z_NULL, 0);
535     s->last_flush = -2;
536 
537     _tr_init(s);
538 
539     return Z_OK;
540 }
541 
542 /* ========================================================================= */
deflateReset(strm)543 int ZEXPORT deflateReset(strm)
544     z_streamp strm;
545 {
546     int ret;
547 
548     ret = deflateResetKeep(strm);
549     if (ret == Z_OK)
550         lm_init(strm->state);
551     return ret;
552 }
553 
554 /* ========================================================================= */
deflateSetHeader(strm,head)555 int ZEXPORT deflateSetHeader(strm, head)
556     z_streamp strm;
557     gz_headerp head;
558 {
559     if (deflateStateCheck(strm) || strm->state->wrap != 2)
560         return Z_STREAM_ERROR;
561     strm->state->gzhead = head;
562     return Z_OK;
563 }
564 
565 /* ========================================================================= */
deflatePending(strm,pending,bits)566 int ZEXPORT deflatePending(strm, pending, bits)
567     unsigned *pending;
568     int *bits;
569     z_streamp strm;
570 {
571     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
572     if (pending != Z_NULL)
573         *pending = strm->state->pending;
574     if (bits != Z_NULL)
575         *bits = strm->state->bi_valid;
576     return Z_OK;
577 }
578 
579 /* ========================================================================= */
deflatePrime(strm,bits,value)580 int ZEXPORT deflatePrime(strm, bits, value)
581     z_streamp strm;
582     int bits;
583     int value;
584 {
585     deflate_state *s;
586     int put;
587 
588     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
589     s = strm->state;
590     if (bits < 0 || bits > 16 ||
591         s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
592         return Z_BUF_ERROR;
593     do {
594         put = Buf_size - s->bi_valid;
595         if (put > bits)
596             put = bits;
597         s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
598         s->bi_valid += put;
599         _tr_flush_bits(s);
600         value >>= put;
601         bits -= put;
602     } while (bits);
603     return Z_OK;
604 }
605 
606 /* ========================================================================= */
deflateParams(strm,level,strategy)607 int ZEXPORT deflateParams(strm, level, strategy)
608     z_streamp strm;
609     int level;
610     int strategy;
611 {
612     deflate_state *s;
613     compress_func func;
614 
615     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
616     s = strm->state;
617 
618 #ifdef FASTEST
619     if (level != 0) level = 1;
620 #else
621     if (level == Z_DEFAULT_COMPRESSION) level = 6;
622 #endif
623     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
624         return Z_STREAM_ERROR;
625     }
626     func = configuration_table[s->level].func;
627 
628     if ((strategy != s->strategy || func != configuration_table[level].func) &&
629         s->last_flush != -2) {
630         /* Flush the last buffer: */
631         int err = deflate(strm, Z_BLOCK);
632         if (err == Z_STREAM_ERROR)
633             return err;
634         if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
635             return Z_BUF_ERROR;
636     }
637     if (s->level != level) {
638         if (s->level == 0 && s->matches != 0) {
639             if (s->matches == 1)
640                 slide_hash(s);
641             else
642                 CLEAR_HASH(s);
643             s->matches = 0;
644         }
645         s->level = level;
646         s->max_lazy_match   = configuration_table[level].max_lazy;
647         s->good_match       = configuration_table[level].good_length;
648         s->nice_match       = configuration_table[level].nice_length;
649         s->max_chain_length = configuration_table[level].max_chain;
650     }
651     s->strategy = strategy;
652     return Z_OK;
653 }
654 
655 /* ========================================================================= */
deflateTune(strm,good_length,max_lazy,nice_length,max_chain)656 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
657     z_streamp strm;
658     int good_length;
659     int max_lazy;
660     int nice_length;
661     int max_chain;
662 {
663     deflate_state *s;
664 
665     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
666     s = strm->state;
667     s->good_match = (uInt)good_length;
668     s->max_lazy_match = (uInt)max_lazy;
669     s->nice_match = nice_length;
670     s->max_chain_length = (uInt)max_chain;
671     return Z_OK;
672 }
673 
674 /* =========================================================================
675  * For the default windowBits of 15 and memLevel of 8, this function returns a
676  * close to exact, as well as small, upper bound on the compressed size. This
677  * is an expansion of ~0.03%, plus a small constant.
678  *
679  * For any setting other than those defaults for windowBits and memLevel, one
680  * of two worst case bounds is returned. This is at most an expansion of ~4% or
681  * ~13%, plus a small constant.
682  *
683  * Both the 0.03% and 4% derive from the overhead of stored blocks. The first
684  * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second
685  * is for stored blocks of 127 bytes (the worst case memLevel == 1). The
686  * expansion results from five bytes of header for each stored block.
687  *
688  * The larger expansion of 13% results from a window size less than or equal to
689  * the symbols buffer size (windowBits <= memLevel + 7). In that case some of
690  * the data being compressed may have slid out of the sliding window, impeding
691  * a stored block from being emitted. Then the only choice is a fixed or
692  * dynamic block, where a fixed block limits the maximum expansion to 9 bits
693  * per 8-bit byte, plus 10 bits for every block. The smallest block size for
694  * which this can occur is 255 (memLevel == 2).
695  *
696  * Shifts are used to approximate divisions, for speed.
697  */
deflateBound(strm,sourceLen)698 uLong ZEXPORT deflateBound(strm, sourceLen)
699     z_streamp strm;
700     uLong sourceLen;
701 {
702     deflate_state *s;
703     uLong fixedlen, storelen, wraplen;
704 
705     /* upper bound for fixed blocks with 9-bit literals and length 255
706        (memLevel == 2, which is the lowest that may not use stored blocks) --
707        ~13% overhead plus a small constant */
708     fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
709                (sourceLen >> 9) + 4;
710 
711     /* upper bound for stored blocks with length 127 (memLevel == 1) --
712        ~4% overhead plus a small constant */
713     storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
714                (sourceLen >> 11) + 7;
715 
716     /* if can't get parameters, return larger bound plus a zlib wrapper */
717     if (deflateStateCheck(strm))
718         return (fixedlen > storelen ? fixedlen : storelen) + 6;
719 
720     /* compute wrapper length */
721     s = strm->state;
722     switch (s->wrap) {
723     case 0:                                 /* raw deflate */
724         wraplen = 0;
725         break;
726     case 1:                                 /* zlib wrapper */
727         wraplen = 6 + (s->strstart ? 4 : 0);
728         break;
729 #ifdef GZIP
730     case 2:                                 /* gzip wrapper */
731         wraplen = 18;
732         if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
733             Bytef *str;
734             if (s->gzhead->extra != Z_NULL)
735                 wraplen += 2 + s->gzhead->extra_len;
736             str = s->gzhead->name;
737             if (str != Z_NULL)
738                 do {
739                     wraplen++;
740                 } while (*str++);
741             str = s->gzhead->comment;
742             if (str != Z_NULL)
743                 do {
744                     wraplen++;
745                 } while (*str++);
746             if (s->gzhead->hcrc)
747                 wraplen += 2;
748         }
749         break;
750 #endif
751     default:                                /* for compiler happiness */
752         wraplen = 6;
753     }
754 
755     /* if not default parameters, return one of the conservative bounds */
756     if (s->w_bits != 15 || s->hash_bits != 8 + 7)
757         return (s->w_bits <= s->hash_bits ? fixedlen : storelen) + wraplen;
758 
759     /* default settings: return tight bound for that case -- ~0.03% overhead
760        plus a small constant */
761     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
762            (sourceLen >> 25) + 13 - 6 + wraplen;
763 }
764 
765 /* =========================================================================
766  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
767  * IN assertion: the stream state is correct and there is enough room in
768  * pending_buf.
769  */
putShortMSB(s,b)770 local void putShortMSB(s, b)
771     deflate_state *s;
772     uInt b;
773 {
774     put_byte(s, (Byte)(b >> 8));
775     put_byte(s, (Byte)(b & 0xff));
776 }
777 
778 /* =========================================================================
779  * Flush as much pending output as possible. All deflate() output, except for
780  * some deflate_stored() output, goes through this function so some
781  * applications may wish to modify it to avoid allocating a large
782  * strm->next_out buffer and copying into it. (See also read_buf()).
783  */
flush_pending(strm)784 local void flush_pending(strm)
785     z_streamp strm;
786 {
787     unsigned len;
788     deflate_state *s = strm->state;
789 
790     _tr_flush_bits(s);
791     len = s->pending;
792     if (len > strm->avail_out) len = strm->avail_out;
793     if (len == 0) return;
794 
795     zmemcpy(strm->next_out, s->pending_out, len);
796     strm->next_out  += len;
797     s->pending_out  += len;
798     strm->total_out += len;
799     strm->avail_out -= len;
800     s->pending      -= len;
801     if (s->pending == 0) {
802         s->pending_out = s->pending_buf;
803     }
804 }
805 
806 /* ===========================================================================
807  * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
808  */
809 #define HCRC_UPDATE(beg) \
810     do { \
811         if (s->gzhead->hcrc && s->pending > (beg)) \
812             strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
813                                 s->pending - (beg)); \
814     } while (0)
815 
816 /* ========================================================================= */
deflate(strm,flush)817 int ZEXPORT deflate(strm, flush)
818     z_streamp strm;
819     int flush;
820 {
821     int old_flush; /* value of flush param for previous deflate call */
822     deflate_state *s;
823 
824     if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
825         return Z_STREAM_ERROR;
826     }
827     s = strm->state;
828 
829     if (strm->next_out == Z_NULL ||
830         (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
831         (s->status == FINISH_STATE && flush != Z_FINISH)) {
832         ERR_RETURN(strm, Z_STREAM_ERROR);
833     }
834     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
835 
836     old_flush = s->last_flush;
837     s->last_flush = flush;
838 
839     /* Flush as much pending output as possible */
840     if (s->pending != 0) {
841         flush_pending(strm);
842         if (strm->avail_out == 0) {
843             /* Since avail_out is 0, deflate will be called again with
844              * more output space, but possibly with both pending and
845              * avail_in equal to zero. There won't be anything to do,
846              * but this is not an error situation so make sure we
847              * return OK instead of BUF_ERROR at next call of deflate:
848              */
849             s->last_flush = -1;
850             return Z_OK;
851         }
852 
853     /* Make sure there is something to do and avoid duplicate consecutive
854      * flushes. For repeated and useless calls with Z_FINISH, we keep
855      * returning Z_STREAM_END instead of Z_BUF_ERROR.
856      */
857     } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
858                flush != Z_FINISH) {
859         ERR_RETURN(strm, Z_BUF_ERROR);
860     }
861 
862     /* User must not provide more input after the first FINISH: */
863     if (s->status == FINISH_STATE && strm->avail_in != 0) {
864         ERR_RETURN(strm, Z_BUF_ERROR);
865     }
866 
867     /* Write the header */
868     if (s->status == INIT_STATE && s->wrap == 0)
869         s->status = BUSY_STATE;
870     if (s->status == INIT_STATE) {
871         /* zlib header */
872         uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
873         uInt level_flags;
874 
875         if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
876             level_flags = 0;
877         else if (s->level < 6)
878             level_flags = 1;
879         else if (s->level == 6)
880             level_flags = 2;
881         else
882             level_flags = 3;
883         header |= (level_flags << 6);
884         if (s->strstart != 0) header |= PRESET_DICT;
885         header += 31 - (header % 31);
886 
887         putShortMSB(s, header);
888 
889         /* Save the adler32 of the preset dictionary: */
890         if (s->strstart != 0) {
891             putShortMSB(s, (uInt)(strm->adler >> 16));
892             putShortMSB(s, (uInt)(strm->adler & 0xffff));
893         }
894         strm->adler = adler32(0L, Z_NULL, 0);
895         s->status = BUSY_STATE;
896 
897         /* Compression must start with an empty pending buffer */
898         flush_pending(strm);
899         if (s->pending != 0) {
900             s->last_flush = -1;
901             return Z_OK;
902         }
903     }
904 #ifdef GZIP
905     if (s->status == GZIP_STATE) {
906         /* gzip header */
907         strm->adler = crc32(0L, Z_NULL, 0);
908         put_byte(s, 31);
909         put_byte(s, 139);
910         put_byte(s, 8);
911         if (s->gzhead == Z_NULL) {
912             put_byte(s, 0);
913             put_byte(s, 0);
914             put_byte(s, 0);
915             put_byte(s, 0);
916             put_byte(s, 0);
917             put_byte(s, s->level == 9 ? 2 :
918                      (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
919                       4 : 0));
920             put_byte(s, OS_CODE);
921             s->status = BUSY_STATE;
922 
923             /* Compression must start with an empty pending buffer */
924             flush_pending(strm);
925             if (s->pending != 0) {
926                 s->last_flush = -1;
927                 return Z_OK;
928             }
929         }
930         else {
931             put_byte(s, (s->gzhead->text ? 1 : 0) +
932                      (s->gzhead->hcrc ? 2 : 0) +
933                      (s->gzhead->extra == Z_NULL ? 0 : 4) +
934                      (s->gzhead->name == Z_NULL ? 0 : 8) +
935                      (s->gzhead->comment == Z_NULL ? 0 : 16)
936                      );
937             put_byte(s, (Byte)(s->gzhead->time & 0xff));
938             put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
939             put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
940             put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
941             put_byte(s, s->level == 9 ? 2 :
942                      (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
943                       4 : 0));
944             put_byte(s, s->gzhead->os & 0xff);
945             if (s->gzhead->extra != Z_NULL) {
946                 put_byte(s, s->gzhead->extra_len & 0xff);
947                 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
948             }
949             if (s->gzhead->hcrc)
950                 strm->adler = crc32(strm->adler, s->pending_buf,
951                                     s->pending);
952             s->gzindex = 0;
953             s->status = EXTRA_STATE;
954         }
955     }
956     if (s->status == EXTRA_STATE) {
957         if (s->gzhead->extra != Z_NULL) {
958             ulg beg = s->pending;   /* start of bytes to update crc */
959             uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
960             while (s->pending + left > s->pending_buf_size) {
961                 uInt copy = s->pending_buf_size - s->pending;
962                 zmemcpy(s->pending_buf + s->pending,
963                         s->gzhead->extra + s->gzindex, copy);
964                 s->pending = s->pending_buf_size;
965                 HCRC_UPDATE(beg);
966                 s->gzindex += copy;
967                 flush_pending(strm);
968                 if (s->pending != 0) {
969                     s->last_flush = -1;
970                     return Z_OK;
971                 }
972                 beg = 0;
973                 left -= copy;
974             }
975             zmemcpy(s->pending_buf + s->pending,
976                     s->gzhead->extra + s->gzindex, left);
977             s->pending += left;
978             HCRC_UPDATE(beg);
979             s->gzindex = 0;
980         }
981         s->status = NAME_STATE;
982     }
983     if (s->status == NAME_STATE) {
984         if (s->gzhead->name != Z_NULL) {
985             ulg beg = s->pending;   /* start of bytes to update crc */
986             int val;
987             do {
988                 if (s->pending == s->pending_buf_size) {
989                     HCRC_UPDATE(beg);
990                     flush_pending(strm);
991                     if (s->pending != 0) {
992                         s->last_flush = -1;
993                         return Z_OK;
994                     }
995                     beg = 0;
996                 }
997                 val = s->gzhead->name[s->gzindex++];
998                 put_byte(s, val);
999             } while (val != 0);
1000             HCRC_UPDATE(beg);
1001             s->gzindex = 0;
1002         }
1003         s->status = COMMENT_STATE;
1004     }
1005     if (s->status == COMMENT_STATE) {
1006         if (s->gzhead->comment != Z_NULL) {
1007             ulg beg = s->pending;   /* start of bytes to update crc */
1008             int val;
1009             do {
1010                 if (s->pending == s->pending_buf_size) {
1011                     HCRC_UPDATE(beg);
1012                     flush_pending(strm);
1013                     if (s->pending != 0) {
1014                         s->last_flush = -1;
1015                         return Z_OK;
1016                     }
1017                     beg = 0;
1018                 }
1019                 val = s->gzhead->comment[s->gzindex++];
1020                 put_byte(s, val);
1021             } while (val != 0);
1022             HCRC_UPDATE(beg);
1023         }
1024         s->status = HCRC_STATE;
1025     }
1026     if (s->status == HCRC_STATE) {
1027         if (s->gzhead->hcrc) {
1028             if (s->pending + 2 > s->pending_buf_size) {
1029                 flush_pending(strm);
1030                 if (s->pending != 0) {
1031                     s->last_flush = -1;
1032                     return Z_OK;
1033                 }
1034             }
1035             put_byte(s, (Byte)(strm->adler & 0xff));
1036             put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1037             strm->adler = crc32(0L, Z_NULL, 0);
1038         }
1039         s->status = BUSY_STATE;
1040 
1041         /* Compression must start with an empty pending buffer */
1042         flush_pending(strm);
1043         if (s->pending != 0) {
1044             s->last_flush = -1;
1045             return Z_OK;
1046         }
1047     }
1048 #endif
1049 
1050     /* Start a new block or continue the current one.
1051      */
1052     if (strm->avail_in != 0 || s->lookahead != 0 ||
1053         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1054         block_state bstate;
1055 
1056         bstate = s->level == 0 ? deflate_stored(s, flush) :
1057                  s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1058                  s->strategy == Z_RLE ? deflate_rle(s, flush) :
1059                  (*(configuration_table[s->level].func))(s, flush);
1060 
1061         if (bstate == finish_started || bstate == finish_done) {
1062             s->status = FINISH_STATE;
1063         }
1064         if (bstate == need_more || bstate == finish_started) {
1065             if (strm->avail_out == 0) {
1066                 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1067             }
1068             return Z_OK;
1069             /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1070              * of deflate should use the same flush parameter to make sure
1071              * that the flush is complete. So we don't have to output an
1072              * empty block here, this will be done at next call. This also
1073              * ensures that for a very small output buffer, we emit at most
1074              * one empty block.
1075              */
1076         }
1077         if (bstate == block_done) {
1078             if (flush == Z_PARTIAL_FLUSH) {
1079                 _tr_align(s);
1080             } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1081                 _tr_stored_block(s, (char*)0, 0L, 0);
1082                 /* For a full flush, this empty block will be recognized
1083                  * as a special marker by inflate_sync().
1084                  */
1085                 if (flush == Z_FULL_FLUSH) {
1086                     CLEAR_HASH(s);             /* forget history */
1087                     if (s->lookahead == 0) {
1088                         s->strstart = 0;
1089                         s->block_start = 0L;
1090                         s->insert = 0;
1091                     }
1092                 }
1093             }
1094             flush_pending(strm);
1095             if (strm->avail_out == 0) {
1096               s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1097               return Z_OK;
1098             }
1099         }
1100     }
1101 
1102     if (flush != Z_FINISH) return Z_OK;
1103     if (s->wrap <= 0) return Z_STREAM_END;
1104 
1105     /* Write the trailer */
1106 #ifdef GZIP
1107     if (s->wrap == 2) {
1108         put_byte(s, (Byte)(strm->adler & 0xff));
1109         put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1110         put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1111         put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1112         put_byte(s, (Byte)(strm->total_in & 0xff));
1113         put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1114         put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1115         put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1116     }
1117     else
1118 #endif
1119     {
1120         putShortMSB(s, (uInt)(strm->adler >> 16));
1121         putShortMSB(s, (uInt)(strm->adler & 0xffff));
1122     }
1123     flush_pending(strm);
1124     /* If avail_out is zero, the application will call deflate again
1125      * to flush the rest.
1126      */
1127     if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1128     return s->pending != 0 ? Z_OK : Z_STREAM_END;
1129 }
1130 
1131 /* ========================================================================= */
deflateEnd(strm)1132 int ZEXPORT deflateEnd(strm)
1133     z_streamp strm;
1134 {
1135     int status;
1136 
1137     if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1138 
1139     status = strm->state->status;
1140 
1141     /* Deallocate in reverse order of allocations: */
1142     TRY_FREE(strm, strm->state->pending_buf);
1143     TRY_FREE(strm, strm->state->head);
1144     TRY_FREE(strm, strm->state->prev);
1145     TRY_FREE(strm, strm->state->window);
1146 
1147     ZFREE(strm, strm->state);
1148     strm->state = Z_NULL;
1149 
1150     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1151 }
1152 
1153 /* =========================================================================
1154  * Copy the source state to the destination state.
1155  * To simplify the source, this is not supported for 16-bit MSDOS (which
1156  * doesn't have enough memory anyway to duplicate compression states).
1157  */
deflateCopy(dest,source)1158 int ZEXPORT deflateCopy(dest, source)
1159     z_streamp dest;
1160     z_streamp source;
1161 {
1162 #ifdef MAXSEG_64K
1163     return Z_STREAM_ERROR;
1164 #else
1165     deflate_state *ds;
1166     deflate_state *ss;
1167 
1168 
1169     if (deflateStateCheck(source) || dest == Z_NULL) {
1170         return Z_STREAM_ERROR;
1171     }
1172 
1173     ss = source->state;
1174 
1175     zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1176 
1177     ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1178     if (ds == Z_NULL) return Z_MEM_ERROR;
1179     dest->state = (struct internal_state FAR *) ds;
1180     zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1181     ds->strm = dest;
1182 
1183     ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1184     ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
1185     ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
1186     ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
1187 
1188     if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1189         ds->pending_buf == Z_NULL) {
1190         deflateEnd (dest);
1191         return Z_MEM_ERROR;
1192     }
1193     /* following zmemcpy do not work for 16-bit MSDOS */
1194     zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1195     zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1196     zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1197     zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1198 
1199     ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1200     ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1201 
1202     ds->l_desc.dyn_tree = ds->dyn_ltree;
1203     ds->d_desc.dyn_tree = ds->dyn_dtree;
1204     ds->bl_desc.dyn_tree = ds->bl_tree;
1205 
1206     return Z_OK;
1207 #endif /* MAXSEG_64K */
1208 }
1209 
1210 /* ===========================================================================
1211  * Read a new buffer from the current input stream, update the adler32
1212  * and total number of bytes read.  All deflate() input goes through
1213  * this function so some applications may wish to modify it to avoid
1214  * allocating a large strm->next_in buffer and copying from it.
1215  * (See also flush_pending()).
1216  */
read_buf(strm,buf,size)1217 local unsigned read_buf(strm, buf, size)
1218     z_streamp strm;
1219     Bytef *buf;
1220     unsigned size;
1221 {
1222     unsigned len = strm->avail_in;
1223 
1224     if (len > size) len = size;
1225     if (len == 0) return 0;
1226 
1227     strm->avail_in  -= len;
1228 
1229     zmemcpy(buf, strm->next_in, len);
1230     if (strm->state->wrap == 1) {
1231         strm->adler = adler32(strm->adler, buf, len);
1232     }
1233 #ifdef GZIP
1234     else if (strm->state->wrap == 2) {
1235         strm->adler = crc32(strm->adler, buf, len);
1236     }
1237 #endif
1238     strm->next_in  += len;
1239     strm->total_in += len;
1240 
1241     return len;
1242 }
1243 
1244 /* ===========================================================================
1245  * Initialize the "longest match" routines for a new zlib stream
1246  */
lm_init(s)1247 local void lm_init(s)
1248     deflate_state *s;
1249 {
1250     s->window_size = (ulg)2L*s->w_size;
1251 
1252     CLEAR_HASH(s);
1253 
1254     /* Set the default configuration parameters:
1255      */
1256     s->max_lazy_match   = configuration_table[s->level].max_lazy;
1257     s->good_match       = configuration_table[s->level].good_length;
1258     s->nice_match       = configuration_table[s->level].nice_length;
1259     s->max_chain_length = configuration_table[s->level].max_chain;
1260 
1261     s->strstart = 0;
1262     s->block_start = 0L;
1263     s->lookahead = 0;
1264     s->insert = 0;
1265     s->match_length = s->prev_length = MIN_MATCH-1;
1266     s->match_available = 0;
1267     s->ins_h = 0;
1268 }
1269 
1270 #ifndef FASTEST
1271 /* ===========================================================================
1272  * Set match_start to the longest match starting at the given string and
1273  * return its length. Matches shorter or equal to prev_length are discarded,
1274  * in which case the result is equal to prev_length and match_start is
1275  * garbage.
1276  * IN assertions: cur_match is the head of the hash chain for the current
1277  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1278  * OUT assertion: the match length is not greater than s->lookahead.
1279  */
longest_match(s,cur_match)1280 local uInt longest_match(s, cur_match)
1281     deflate_state *s;
1282     IPos cur_match;                             /* current match */
1283 {
1284     unsigned chain_length = s->max_chain_length;/* max hash chain length */
1285     register Bytef *scan = s->window + s->strstart; /* current string */
1286     register Bytef *match;                      /* matched string */
1287     register int len;                           /* length of current match */
1288     int best_len = (int)s->prev_length;         /* best match length so far */
1289     int nice_match = s->nice_match;             /* stop if match long enough */
1290     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1291         s->strstart - (IPos)MAX_DIST(s) : NIL;
1292     /* Stop when cur_match becomes <= limit. To simplify the code,
1293      * we prevent matches with the string of window index 0.
1294      */
1295     Posf *prev = s->prev;
1296     uInt wmask = s->w_mask;
1297 
1298 #ifdef UNALIGNED_OK
1299     /* Compare two bytes at a time. Note: this is not always beneficial.
1300      * Try with and without -DUNALIGNED_OK to check.
1301      */
1302     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1303     register ush scan_start = *(ushf*)scan;
1304     register ush scan_end   = *(ushf*)(scan + best_len - 1);
1305 #else
1306     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1307     register Byte scan_end1  = scan[best_len - 1];
1308     register Byte scan_end   = scan[best_len];
1309 #endif
1310 
1311     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1312      * It is easy to get rid of this optimization if necessary.
1313      */
1314     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1315 
1316     /* Do not waste too much time if we already have a good match: */
1317     if (s->prev_length >= s->good_match) {
1318         chain_length >>= 2;
1319     }
1320     /* Do not look for matches beyond the end of the input. This is necessary
1321      * to make deflate deterministic.
1322      */
1323     if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1324 
1325     Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1326            "need lookahead");
1327 
1328     do {
1329         Assert(cur_match < s->strstart, "no future");
1330         match = s->window + cur_match;
1331 
1332         /* Skip to next match if the match length cannot increase
1333          * or if the match length is less than 2.  Note that the checks below
1334          * for insufficient lookahead only occur occasionally for performance
1335          * reasons.  Therefore uninitialized memory will be accessed, and
1336          * conditional jumps will be made that depend on those values.
1337          * However the length of the match is limited to the lookahead, so
1338          * the output of deflate is not affected by the uninitialized values.
1339          */
1340 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1341         /* This code assumes sizeof(unsigned short) == 2. Do not use
1342          * UNALIGNED_OK if your compiler uses a different size.
1343          */
1344         if (*(ushf*)(match + best_len - 1) != scan_end ||
1345             *(ushf*)match != scan_start) continue;
1346 
1347         /* It is not necessary to compare scan[2] and match[2] since they are
1348          * always equal when the other bytes match, given that the hash keys
1349          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1350          * strstart + 3, + 5, up to strstart + 257. We check for insufficient
1351          * lookahead only every 4th comparison; the 128th check will be made
1352          * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
1353          * necessary to put more guard bytes at the end of the window, or
1354          * to check more often for insufficient lookahead.
1355          */
1356         Assert(scan[2] == match[2], "scan[2]?");
1357         scan++, match++;
1358         do {
1359         } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1360                  *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1361                  *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1362                  *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1363                  scan < strend);
1364         /* The funny "do {}" generates better code on most compilers */
1365 
1366         /* Here, scan <= window + strstart + 257 */
1367         Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1368                "wild scan");
1369         if (*scan == *match) scan++;
1370 
1371         len = (MAX_MATCH - 1) - (int)(strend - scan);
1372         scan = strend - (MAX_MATCH-1);
1373 
1374 #else /* UNALIGNED_OK */
1375 
1376         if (match[best_len]     != scan_end  ||
1377             match[best_len - 1] != scan_end1 ||
1378             *match              != *scan     ||
1379             *++match            != scan[1])      continue;
1380 
1381         /* The check at best_len - 1 can be removed because it will be made
1382          * again later. (This heuristic is not always a win.)
1383          * It is not necessary to compare scan[2] and match[2] since they
1384          * are always equal when the other bytes match, given that
1385          * the hash keys are equal and that HASH_BITS >= 8.
1386          */
1387         scan += 2, match++;
1388         Assert(*scan == *match, "match[2]?");
1389 
1390         /* We check for insufficient lookahead only every 8th comparison;
1391          * the 256th check will be made at strstart + 258.
1392          */
1393         do {
1394         } while (*++scan == *++match && *++scan == *++match &&
1395                  *++scan == *++match && *++scan == *++match &&
1396                  *++scan == *++match && *++scan == *++match &&
1397                  *++scan == *++match && *++scan == *++match &&
1398                  scan < strend);
1399 
1400         Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1401                "wild scan");
1402 
1403         len = MAX_MATCH - (int)(strend - scan);
1404         scan = strend - MAX_MATCH;
1405 
1406 #endif /* UNALIGNED_OK */
1407 
1408         if (len > best_len) {
1409             s->match_start = cur_match;
1410             best_len = len;
1411             if (len >= nice_match) break;
1412 #ifdef UNALIGNED_OK
1413             scan_end = *(ushf*)(scan + best_len - 1);
1414 #else
1415             scan_end1  = scan[best_len - 1];
1416             scan_end   = scan[best_len];
1417 #endif
1418         }
1419     } while ((cur_match = prev[cur_match & wmask]) > limit
1420              && --chain_length != 0);
1421 
1422     if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1423     return s->lookahead;
1424 }
1425 
1426 #else /* FASTEST */
1427 
1428 /* ---------------------------------------------------------------------------
1429  * Optimized version for FASTEST only
1430  */
longest_match(s,cur_match)1431 local uInt longest_match(s, cur_match)
1432     deflate_state *s;
1433     IPos cur_match;                             /* current match */
1434 {
1435     register Bytef *scan = s->window + s->strstart; /* current string */
1436     register Bytef *match;                       /* matched string */
1437     register int len;                           /* length of current match */
1438     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1439 
1440     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1441      * It is easy to get rid of this optimization if necessary.
1442      */
1443     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1444 
1445     Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1446            "need lookahead");
1447 
1448     Assert(cur_match < s->strstart, "no future");
1449 
1450     match = s->window + cur_match;
1451 
1452     /* Return failure if the match length is less than 2:
1453      */
1454     if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1455 
1456     /* The check at best_len - 1 can be removed because it will be made
1457      * again later. (This heuristic is not always a win.)
1458      * It is not necessary to compare scan[2] and match[2] since they
1459      * are always equal when the other bytes match, given that
1460      * the hash keys are equal and that HASH_BITS >= 8.
1461      */
1462     scan += 2, match += 2;
1463     Assert(*scan == *match, "match[2]?");
1464 
1465     /* We check for insufficient lookahead only every 8th comparison;
1466      * the 256th check will be made at strstart + 258.
1467      */
1468     do {
1469     } while (*++scan == *++match && *++scan == *++match &&
1470              *++scan == *++match && *++scan == *++match &&
1471              *++scan == *++match && *++scan == *++match &&
1472              *++scan == *++match && *++scan == *++match &&
1473              scan < strend);
1474 
1475     Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan");
1476 
1477     len = MAX_MATCH - (int)(strend - scan);
1478 
1479     if (len < MIN_MATCH) return MIN_MATCH - 1;
1480 
1481     s->match_start = cur_match;
1482     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1483 }
1484 
1485 #endif /* FASTEST */
1486 
1487 #ifdef ZLIB_DEBUG
1488 
1489 #define EQUAL 0
1490 /* result of memcmp for equal strings */
1491 
1492 /* ===========================================================================
1493  * Check that the match at match_start is indeed a match.
1494  */
check_match(s,start,match,length)1495 local void check_match(s, start, match, length)
1496     deflate_state *s;
1497     IPos start, match;
1498     int length;
1499 {
1500     /* check that the match is indeed a match */
1501     if (zmemcmp(s->window + match,
1502                 s->window + start, length) != EQUAL) {
1503         fprintf(stderr, " start %u, match %u, length %d\n",
1504                 start, match, length);
1505         do {
1506             fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1507         } while (--length != 0);
1508         z_error("invalid match");
1509     }
1510     if (z_verbose > 1) {
1511         fprintf(stderr,"\\[%d,%d]", start - match, length);
1512         do { putc(s->window[start++], stderr); } while (--length != 0);
1513     }
1514 }
1515 #else
1516 #  define check_match(s, start, match, length)
1517 #endif /* ZLIB_DEBUG */
1518 
1519 /* ===========================================================================
1520  * Fill the window when the lookahead becomes insufficient.
1521  * Updates strstart and lookahead.
1522  *
1523  * IN assertion: lookahead < MIN_LOOKAHEAD
1524  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1525  *    At least one byte has been read, or avail_in == 0; reads are
1526  *    performed for at least two bytes (required for the zip translate_eol
1527  *    option -- not supported here).
1528  */
fill_window(s)1529 local void fill_window(s)
1530     deflate_state *s;
1531 {
1532     unsigned n;
1533     unsigned more;    /* Amount of free space at the end of the window. */
1534     uInt wsize = s->w_size;
1535 
1536     Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1537 
1538     do {
1539         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1540 
1541         /* Deal with !@#$% 64K limit: */
1542         if (sizeof(int) <= 2) {
1543             if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1544                 more = wsize;
1545 
1546             } else if (more == (unsigned)(-1)) {
1547                 /* Very unlikely, but possible on 16 bit machine if
1548                  * strstart == 0 && lookahead == 1 (input done a byte at time)
1549                  */
1550                 more--;
1551             }
1552         }
1553 
1554         /* If the window is almost full and there is insufficient lookahead,
1555          * move the upper half to the lower one to make room in the upper half.
1556          */
1557         if (s->strstart >= wsize + MAX_DIST(s)) {
1558 
1559             zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
1560             s->match_start -= wsize;
1561             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
1562             s->block_start -= (long) wsize;
1563             if (s->insert > s->strstart)
1564                 s->insert = s->strstart;
1565             slide_hash(s);
1566             more += wsize;
1567         }
1568         if (s->strm->avail_in == 0) break;
1569 
1570         /* If there was no sliding:
1571          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1572          *    more == window_size - lookahead - strstart
1573          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1574          * => more >= window_size - 2*WSIZE + 2
1575          * In the BIG_MEM or MMAP case (not yet supported),
1576          *   window_size == input_size + MIN_LOOKAHEAD  &&
1577          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1578          * Otherwise, window_size == 2*WSIZE so more >= 2.
1579          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1580          */
1581         Assert(more >= 2, "more < 2");
1582 
1583         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1584         s->lookahead += n;
1585 
1586         /* Initialize the hash value now that we have some input: */
1587         if (s->lookahead + s->insert >= MIN_MATCH) {
1588             uInt str = s->strstart - s->insert;
1589             s->ins_h = s->window[str];
1590             UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1591 #if MIN_MATCH != 3
1592             Call UPDATE_HASH() MIN_MATCH-3 more times
1593 #endif
1594             while (s->insert) {
1595                 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1596 #ifndef FASTEST
1597                 s->prev[str & s->w_mask] = s->head[s->ins_h];
1598 #endif
1599                 s->head[s->ins_h] = (Pos)str;
1600                 str++;
1601                 s->insert--;
1602                 if (s->lookahead + s->insert < MIN_MATCH)
1603                     break;
1604             }
1605         }
1606         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1607          * but this is not important since only literal bytes will be emitted.
1608          */
1609 
1610     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1611 
1612     /* If the WIN_INIT bytes after the end of the current data have never been
1613      * written, then zero those bytes in order to avoid memory check reports of
1614      * the use of uninitialized (or uninitialised as Julian writes) bytes by
1615      * the longest match routines.  Update the high water mark for the next
1616      * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
1617      * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1618      */
1619     if (s->high_water < s->window_size) {
1620         ulg curr = s->strstart + (ulg)(s->lookahead);
1621         ulg init;
1622 
1623         if (s->high_water < curr) {
1624             /* Previous high water mark below current data -- zero WIN_INIT
1625              * bytes or up to end of window, whichever is less.
1626              */
1627             init = s->window_size - curr;
1628             if (init > WIN_INIT)
1629                 init = WIN_INIT;
1630             zmemzero(s->window + curr, (unsigned)init);
1631             s->high_water = curr + init;
1632         }
1633         else if (s->high_water < (ulg)curr + WIN_INIT) {
1634             /* High water mark at or above current data, but below current data
1635              * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1636              * to end of window, whichever is less.
1637              */
1638             init = (ulg)curr + WIN_INIT - s->high_water;
1639             if (init > s->window_size - s->high_water)
1640                 init = s->window_size - s->high_water;
1641             zmemzero(s->window + s->high_water, (unsigned)init);
1642             s->high_water += init;
1643         }
1644     }
1645 
1646     Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1647            "not enough room for search");
1648 }
1649 
1650 /* ===========================================================================
1651  * Flush the current block, with given end-of-file flag.
1652  * IN assertion: strstart is set to the end of the current match.
1653  */
1654 #define FLUSH_BLOCK_ONLY(s, last) { \
1655    _tr_flush_block(s, (s->block_start >= 0L ? \
1656                    (charf *)&s->window[(unsigned)s->block_start] : \
1657                    (charf *)Z_NULL), \
1658                 (ulg)((long)s->strstart - s->block_start), \
1659                 (last)); \
1660    s->block_start = s->strstart; \
1661    flush_pending(s->strm); \
1662    Tracev((stderr,"[FLUSH]")); \
1663 }
1664 
1665 /* Same but force premature exit if necessary. */
1666 #define FLUSH_BLOCK(s, last) { \
1667    FLUSH_BLOCK_ONLY(s, last); \
1668    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1669 }
1670 
1671 /* Maximum stored block length in deflate format (not including header). */
1672 #define MAX_STORED 65535
1673 
1674 /* Minimum of a and b. */
1675 #define MIN(a, b) ((a) > (b) ? (b) : (a))
1676 
1677 /* ===========================================================================
1678  * Copy without compression as much as possible from the input stream, return
1679  * the current block state.
1680  *
1681  * In case deflateParams() is used to later switch to a non-zero compression
1682  * level, s->matches (otherwise unused when storing) keeps track of the number
1683  * of hash table slides to perform. If s->matches is 1, then one hash table
1684  * slide will be done when switching. If s->matches is 2, the maximum value
1685  * allowed here, then the hash table will be cleared, since two or more slides
1686  * is the same as a clear.
1687  *
1688  * deflate_stored() is written to minimize the number of times an input byte is
1689  * copied. It is most efficient with large input and output buffers, which
1690  * maximizes the opportunities to have a single copy from next_in to next_out.
1691  */
deflate_stored(s,flush)1692 local block_state deflate_stored(s, flush)
1693     deflate_state *s;
1694     int flush;
1695 {
1696     /* Smallest worthy block size when not flushing or finishing. By default
1697      * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1698      * large input and output buffers, the stored block size will be larger.
1699      */
1700     unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1701 
1702     /* Copy as many min_block or larger stored blocks directly to next_out as
1703      * possible. If flushing, copy the remaining available input to next_out as
1704      * stored blocks, if there is enough space.
1705      */
1706     unsigned len, left, have, last = 0;
1707     unsigned used = s->strm->avail_in;
1708     do {
1709         /* Set len to the maximum size block that we can copy directly with the
1710          * available input data and output space. Set left to how much of that
1711          * would be copied from what's left in the window.
1712          */
1713         len = MAX_STORED;       /* maximum deflate stored block length */
1714         have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
1715         if (s->strm->avail_out < have)          /* need room for header */
1716             break;
1717             /* maximum stored block length that will fit in avail_out: */
1718         have = s->strm->avail_out - have;
1719         left = s->strstart - s->block_start;    /* bytes left in window */
1720         if (len > (ulg)left + s->strm->avail_in)
1721             len = left + s->strm->avail_in;     /* limit len to the input */
1722         if (len > have)
1723             len = have;                         /* limit len to the output */
1724 
1725         /* If the stored block would be less than min_block in length, or if
1726          * unable to copy all of the available input when flushing, then try
1727          * copying to the window and the pending buffer instead. Also don't
1728          * write an empty block when flushing -- deflate() does that.
1729          */
1730         if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1731                                 flush == Z_NO_FLUSH ||
1732                                 len != left + s->strm->avail_in))
1733             break;
1734 
1735         /* Make a dummy stored block in pending to get the header bytes,
1736          * including any pending bits. This also updates the debugging counts.
1737          */
1738         last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1739         _tr_stored_block(s, (char *)0, 0L, last);
1740 
1741         /* Replace the lengths in the dummy stored block with len. */
1742         s->pending_buf[s->pending - 4] = len;
1743         s->pending_buf[s->pending - 3] = len >> 8;
1744         s->pending_buf[s->pending - 2] = ~len;
1745         s->pending_buf[s->pending - 1] = ~len >> 8;
1746 
1747         /* Write the stored block header bytes. */
1748         flush_pending(s->strm);
1749 
1750 #ifdef ZLIB_DEBUG
1751         /* Update debugging counts for the data about to be copied. */
1752         s->compressed_len += len << 3;
1753         s->bits_sent += len << 3;
1754 #endif
1755 
1756         /* Copy uncompressed bytes from the window to next_out. */
1757         if (left) {
1758             if (left > len)
1759                 left = len;
1760             zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1761             s->strm->next_out += left;
1762             s->strm->avail_out -= left;
1763             s->strm->total_out += left;
1764             s->block_start += left;
1765             len -= left;
1766         }
1767 
1768         /* Copy uncompressed bytes directly from next_in to next_out, updating
1769          * the check value.
1770          */
1771         if (len) {
1772             read_buf(s->strm, s->strm->next_out, len);
1773             s->strm->next_out += len;
1774             s->strm->avail_out -= len;
1775             s->strm->total_out += len;
1776         }
1777     } while (last == 0);
1778 
1779     /* Update the sliding window with the last s->w_size bytes of the copied
1780      * data, or append all of the copied data to the existing window if less
1781      * than s->w_size bytes were copied. Also update the number of bytes to
1782      * insert in the hash tables, in the event that deflateParams() switches to
1783      * a non-zero compression level.
1784      */
1785     used -= s->strm->avail_in;      /* number of input bytes directly copied */
1786     if (used) {
1787         /* If any input was used, then no unused input remains in the window,
1788          * therefore s->block_start == s->strstart.
1789          */
1790         if (used >= s->w_size) {    /* supplant the previous history */
1791             s->matches = 2;         /* clear hash */
1792             zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1793             s->strstart = s->w_size;
1794             s->insert = s->strstart;
1795         }
1796         else {
1797             if (s->window_size - s->strstart <= used) {
1798                 /* Slide the window down. */
1799                 s->strstart -= s->w_size;
1800                 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1801                 if (s->matches < 2)
1802                     s->matches++;   /* add a pending slide_hash() */
1803                 if (s->insert > s->strstart)
1804                     s->insert = s->strstart;
1805             }
1806             zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1807             s->strstart += used;
1808             s->insert += MIN(used, s->w_size - s->insert);
1809         }
1810         s->block_start = s->strstart;
1811     }
1812     if (s->high_water < s->strstart)
1813         s->high_water = s->strstart;
1814 
1815     /* If the last block was written to next_out, then done. */
1816     if (last)
1817         return finish_done;
1818 
1819     /* If flushing and all input has been consumed, then done. */
1820     if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1821         s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1822         return block_done;
1823 
1824     /* Fill the window with any remaining input. */
1825     have = s->window_size - s->strstart;
1826     if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1827         /* Slide the window down. */
1828         s->block_start -= s->w_size;
1829         s->strstart -= s->w_size;
1830         zmemcpy(s->window, s->window + s->w_size, s->strstart);
1831         if (s->matches < 2)
1832             s->matches++;           /* add a pending slide_hash() */
1833         have += s->w_size;          /* more space now */
1834         if (s->insert > s->strstart)
1835             s->insert = s->strstart;
1836     }
1837     if (have > s->strm->avail_in)
1838         have = s->strm->avail_in;
1839     if (have) {
1840         read_buf(s->strm, s->window + s->strstart, have);
1841         s->strstart += have;
1842         s->insert += MIN(have, s->w_size - s->insert);
1843     }
1844     if (s->high_water < s->strstart)
1845         s->high_water = s->strstart;
1846 
1847     /* There was not enough avail_out to write a complete worthy or flushed
1848      * stored block to next_out. Write a stored block to pending instead, if we
1849      * have enough input for a worthy block, or if flushing and there is enough
1850      * room for the remaining input as a stored block in the pending buffer.
1851      */
1852     have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
1853         /* maximum stored block length that will fit in pending: */
1854     have = MIN(s->pending_buf_size - have, MAX_STORED);
1855     min_block = MIN(have, s->w_size);
1856     left = s->strstart - s->block_start;
1857     if (left >= min_block ||
1858         ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1859          s->strm->avail_in == 0 && left <= have)) {
1860         len = MIN(left, have);
1861         last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1862                len == left ? 1 : 0;
1863         _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1864         s->block_start += len;
1865         flush_pending(s->strm);
1866     }
1867 
1868     /* We've done all we can with the available input and output. */
1869     return last ? finish_started : need_more;
1870 }
1871 
1872 /* ===========================================================================
1873  * Compress as much as possible from the input stream, return the current
1874  * block state.
1875  * This function does not perform lazy evaluation of matches and inserts
1876  * new strings in the dictionary only for unmatched strings or for short
1877  * matches. It is used only for the fast compression options.
1878  */
deflate_fast(s,flush)1879 local block_state deflate_fast(s, flush)
1880     deflate_state *s;
1881     int flush;
1882 {
1883     IPos hash_head;       /* head of the hash chain */
1884     int bflush;           /* set if current block must be flushed */
1885 
1886     for (;;) {
1887         /* Make sure that we always have enough lookahead, except
1888          * at the end of the input file. We need MAX_MATCH bytes
1889          * for the next match, plus MIN_MATCH bytes to insert the
1890          * string following the next match.
1891          */
1892         if (s->lookahead < MIN_LOOKAHEAD) {
1893             fill_window(s);
1894             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1895                 return need_more;
1896             }
1897             if (s->lookahead == 0) break; /* flush the current block */
1898         }
1899 
1900         /* Insert the string window[strstart .. strstart + 2] in the
1901          * dictionary, and set hash_head to the head of the hash chain:
1902          */
1903         hash_head = NIL;
1904         if (s->lookahead >= MIN_MATCH) {
1905             INSERT_STRING(s, s->strstart, hash_head);
1906         }
1907 
1908         /* Find the longest match, discarding those <= prev_length.
1909          * At this point we have always match_length < MIN_MATCH
1910          */
1911         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1912             /* To simplify the code, we prevent matches with the string
1913              * of window index 0 (in particular we have to avoid a match
1914              * of the string with itself at the start of the input file).
1915              */
1916             s->match_length = longest_match (s, hash_head);
1917             /* longest_match() sets match_start */
1918         }
1919         if (s->match_length >= MIN_MATCH) {
1920             check_match(s, s->strstart, s->match_start, s->match_length);
1921 
1922             _tr_tally_dist(s, s->strstart - s->match_start,
1923                            s->match_length - MIN_MATCH, bflush);
1924 
1925             s->lookahead -= s->match_length;
1926 
1927             /* Insert new strings in the hash table only if the match length
1928              * is not too large. This saves time but degrades compression.
1929              */
1930 #ifndef FASTEST
1931             if (s->match_length <= s->max_insert_length &&
1932                 s->lookahead >= MIN_MATCH) {
1933                 s->match_length--; /* string at strstart already in table */
1934                 do {
1935                     s->strstart++;
1936                     INSERT_STRING(s, s->strstart, hash_head);
1937                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1938                      * always MIN_MATCH bytes ahead.
1939                      */
1940                 } while (--s->match_length != 0);
1941                 s->strstart++;
1942             } else
1943 #endif
1944             {
1945                 s->strstart += s->match_length;
1946                 s->match_length = 0;
1947                 s->ins_h = s->window[s->strstart];
1948                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
1949 #if MIN_MATCH != 3
1950                 Call UPDATE_HASH() MIN_MATCH-3 more times
1951 #endif
1952                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1953                  * matter since it will be recomputed at next deflate call.
1954                  */
1955             }
1956         } else {
1957             /* No match, output a literal byte */
1958             Tracevv((stderr,"%c", s->window[s->strstart]));
1959             _tr_tally_lit(s, s->window[s->strstart], bflush);
1960             s->lookahead--;
1961             s->strstart++;
1962         }
1963         if (bflush) FLUSH_BLOCK(s, 0);
1964     }
1965     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1966     if (flush == Z_FINISH) {
1967         FLUSH_BLOCK(s, 1);
1968         return finish_done;
1969     }
1970     if (s->sym_next)
1971         FLUSH_BLOCK(s, 0);
1972     return block_done;
1973 }
1974 
1975 #ifndef FASTEST
1976 /* ===========================================================================
1977  * Same as above, but achieves better compression. We use a lazy
1978  * evaluation for matches: a match is finally adopted only if there is
1979  * no better match at the next window position.
1980  */
deflate_slow(s,flush)1981 local block_state deflate_slow(s, flush)
1982     deflate_state *s;
1983     int flush;
1984 {
1985     IPos hash_head;          /* head of hash chain */
1986     int bflush;              /* set if current block must be flushed */
1987 
1988     /* Process the input block. */
1989     for (;;) {
1990         /* Make sure that we always have enough lookahead, except
1991          * at the end of the input file. We need MAX_MATCH bytes
1992          * for the next match, plus MIN_MATCH bytes to insert the
1993          * string following the next match.
1994          */
1995         if (s->lookahead < MIN_LOOKAHEAD) {
1996             fill_window(s);
1997             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1998                 return need_more;
1999             }
2000             if (s->lookahead == 0) break; /* flush the current block */
2001         }
2002 
2003         /* Insert the string window[strstart .. strstart + 2] in the
2004          * dictionary, and set hash_head to the head of the hash chain:
2005          */
2006         hash_head = NIL;
2007         if (s->lookahead >= MIN_MATCH) {
2008             INSERT_STRING(s, s->strstart, hash_head);
2009         }
2010 
2011         /* Find the longest match, discarding those <= prev_length.
2012          */
2013         s->prev_length = s->match_length, s->prev_match = s->match_start;
2014         s->match_length = MIN_MATCH-1;
2015 
2016         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
2017             s->strstart - hash_head <= MAX_DIST(s)) {
2018             /* To simplify the code, we prevent matches with the string
2019              * of window index 0 (in particular we have to avoid a match
2020              * of the string with itself at the start of the input file).
2021              */
2022             s->match_length = longest_match (s, hash_head);
2023             /* longest_match() sets match_start */
2024 
2025             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
2026 #if TOO_FAR <= 32767
2027                 || (s->match_length == MIN_MATCH &&
2028                     s->strstart - s->match_start > TOO_FAR)
2029 #endif
2030                 )) {
2031 
2032                 /* If prev_match is also MIN_MATCH, match_start is garbage
2033                  * but we will ignore the current match anyway.
2034                  */
2035                 s->match_length = MIN_MATCH-1;
2036             }
2037         }
2038         /* If there was a match at the previous step and the current
2039          * match is not better, output the previous match:
2040          */
2041         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
2042             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
2043             /* Do not insert strings in hash table beyond this. */
2044 
2045             check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
2046 
2047             _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
2048                            s->prev_length - MIN_MATCH, bflush);
2049 
2050             /* Insert in hash table all strings up to the end of the match.
2051              * strstart - 1 and strstart are already inserted. If there is not
2052              * enough lookahead, the last two strings are not inserted in
2053              * the hash table.
2054              */
2055             s->lookahead -= s->prev_length - 1;
2056             s->prev_length -= 2;
2057             do {
2058                 if (++s->strstart <= max_insert) {
2059                     INSERT_STRING(s, s->strstart, hash_head);
2060                 }
2061             } while (--s->prev_length != 0);
2062             s->match_available = 0;
2063             s->match_length = MIN_MATCH-1;
2064             s->strstart++;
2065 
2066             if (bflush) FLUSH_BLOCK(s, 0);
2067 
2068         } else if (s->match_available) {
2069             /* If there was no match at the previous position, output a
2070              * single literal. If there was a match but the current match
2071              * is longer, truncate the previous match to a single literal.
2072              */
2073             Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2074             _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2075             if (bflush) {
2076                 FLUSH_BLOCK_ONLY(s, 0);
2077             }
2078             s->strstart++;
2079             s->lookahead--;
2080             if (s->strm->avail_out == 0) return need_more;
2081         } else {
2082             /* There is no previous match to compare with, wait for
2083              * the next step to decide.
2084              */
2085             s->match_available = 1;
2086             s->strstart++;
2087             s->lookahead--;
2088         }
2089     }
2090     Assert (flush != Z_NO_FLUSH, "no flush?");
2091     if (s->match_available) {
2092         Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2093         _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2094         s->match_available = 0;
2095     }
2096     s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2097     if (flush == Z_FINISH) {
2098         FLUSH_BLOCK(s, 1);
2099         return finish_done;
2100     }
2101     if (s->sym_next)
2102         FLUSH_BLOCK(s, 0);
2103     return block_done;
2104 }
2105 #endif /* FASTEST */
2106 
2107 /* ===========================================================================
2108  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2109  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
2110  * deflate switches away from Z_RLE.)
2111  */
deflate_rle(s,flush)2112 local block_state deflate_rle(s, flush)
2113     deflate_state *s;
2114     int flush;
2115 {
2116     int bflush;             /* set if current block must be flushed */
2117     uInt prev;              /* byte at distance one to match */
2118     Bytef *scan, *strend;   /* scan goes up to strend for length of run */
2119 
2120     for (;;) {
2121         /* Make sure that we always have enough lookahead, except
2122          * at the end of the input file. We need MAX_MATCH bytes
2123          * for the longest run, plus one for the unrolled loop.
2124          */
2125         if (s->lookahead <= MAX_MATCH) {
2126             fill_window(s);
2127             if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2128                 return need_more;
2129             }
2130             if (s->lookahead == 0) break; /* flush the current block */
2131         }
2132 
2133         /* See how many times the previous byte repeats */
2134         s->match_length = 0;
2135         if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2136             scan = s->window + s->strstart - 1;
2137             prev = *scan;
2138             if (prev == *++scan && prev == *++scan && prev == *++scan) {
2139                 strend = s->window + s->strstart + MAX_MATCH;
2140                 do {
2141                 } while (prev == *++scan && prev == *++scan &&
2142                          prev == *++scan && prev == *++scan &&
2143                          prev == *++scan && prev == *++scan &&
2144                          prev == *++scan && prev == *++scan &&
2145                          scan < strend);
2146                 s->match_length = MAX_MATCH - (uInt)(strend - scan);
2147                 if (s->match_length > s->lookahead)
2148                     s->match_length = s->lookahead;
2149             }
2150             Assert(scan <= s->window + (uInt)(s->window_size - 1),
2151                    "wild scan");
2152         }
2153 
2154         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2155         if (s->match_length >= MIN_MATCH) {
2156             check_match(s, s->strstart, s->strstart - 1, s->match_length);
2157 
2158             _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2159 
2160             s->lookahead -= s->match_length;
2161             s->strstart += s->match_length;
2162             s->match_length = 0;
2163         } else {
2164             /* No match, output a literal byte */
2165             Tracevv((stderr,"%c", s->window[s->strstart]));
2166             _tr_tally_lit(s, s->window[s->strstart], bflush);
2167             s->lookahead--;
2168             s->strstart++;
2169         }
2170         if (bflush) FLUSH_BLOCK(s, 0);
2171     }
2172     s->insert = 0;
2173     if (flush == Z_FINISH) {
2174         FLUSH_BLOCK(s, 1);
2175         return finish_done;
2176     }
2177     if (s->sym_next)
2178         FLUSH_BLOCK(s, 0);
2179     return block_done;
2180 }
2181 
2182 /* ===========================================================================
2183  * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
2184  * (It will be regenerated if this run of deflate switches away from Huffman.)
2185  */
deflate_huff(s,flush)2186 local block_state deflate_huff(s, flush)
2187     deflate_state *s;
2188     int flush;
2189 {
2190     int bflush;             /* set if current block must be flushed */
2191 
2192     for (;;) {
2193         /* Make sure that we have a literal to write. */
2194         if (s->lookahead == 0) {
2195             fill_window(s);
2196             if (s->lookahead == 0) {
2197                 if (flush == Z_NO_FLUSH)
2198                     return need_more;
2199                 break;      /* flush the current block */
2200             }
2201         }
2202 
2203         /* Output a literal byte */
2204         s->match_length = 0;
2205         Tracevv((stderr,"%c", s->window[s->strstart]));
2206         _tr_tally_lit(s, s->window[s->strstart], bflush);
2207         s->lookahead--;
2208         s->strstart++;
2209         if (bflush) FLUSH_BLOCK(s, 0);
2210     }
2211     s->insert = 0;
2212     if (flush == Z_FINISH) {
2213         FLUSH_BLOCK(s, 1);
2214         return finish_done;
2215     }
2216     if (s->sym_next)
2217         FLUSH_BLOCK(s, 0);
2218     return block_done;
2219 }
2220