1 /* deflate.c -- compress data using the deflation algorithm
2  * Copyright (C) 1995-2016 Jean-loup Gailly and Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 /*
7  *  ALGORITHM
8  *
9  *      The "deflation" process depends on being able to identify portions
10  *      of the input text which are identical to earlier input (within a
11  *      sliding window trailing behind the input currently being processed).
12  *
13  *      The most straightforward technique turns out to be the fastest for
14  *      most input files: try all possible matches and select the longest.
15  *      The key feature of this algorithm is that insertions into the string
16  *      dictionary are very simple and thus fast, and deletions are avoided
17  *      completely. Insertions are performed at each input character, whereas
18  *      string matches are performed only when the previous match ends. So it
19  *      is preferable to spend more time in matches to allow very fast string
20  *      insertions and avoid deletions. The matching algorithm for small
21  *      strings is inspired from that of Rabin & Karp. A brute force approach
22  *      is used to find longer strings when a small match has been found.
23  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24  *      (by Leonid Broukhis).
25  *         A previous version of this file used a more sophisticated algorithm
26  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
27  *      time, but has a larger average cost, uses more memory and is patented.
28  *      However the F&G algorithm may be faster for some highly redundant
29  *      files if the parameter max_chain_length (described below) is too large.
30  *
31  *  ACKNOWLEDGEMENTS
32  *
33  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34  *      I found it in 'freeze' written by Leonid Broukhis.
35  *      Thanks to many people for bug reports and testing.
36  *
37  *  REFERENCES
38  *
39  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40  *      Available in https://tools.ietf.org/html/rfc1951
41  *
42  *      A description of the Rabin and Karp algorithm is given in the book
43  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44  *
45  *      Fiala,E.R., and Greene,D.H.
46  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47  *
48  */
49 
50 #include "zbuild.h"
51 #include "cpu_features.h"
52 #include "deflate.h"
53 #include "deflate_p.h"
54 #include "functable.h"
55 
56 const char PREFIX(deflate_copyright)[] = " deflate 1.2.11.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
57 /*
58   If you use the zlib library in a product, an acknowledgment is welcome
59   in the documentation of your product. If for some reason you cannot
60   include such an acknowledgment, I would appreciate that you keep this
61   copyright string in the executable of your product.
62  */
63 
64 /* ===========================================================================
65  *  Architecture-specific hooks.
66  */
67 #ifdef S390_DFLTCC_DEFLATE
68 #  include "arch/s390/dfltcc_deflate.h"
69 #else
70 /* Memory management for the deflate state. Useful for allocating arch-specific extension blocks. */
71 #  define ZALLOC_DEFLATE_STATE(strm) ((deflate_state *)ZALLOC(strm, 1, sizeof(deflate_state)))
72 #  define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
73 #  define ZCOPY_DEFLATE_STATE(dst, src) memcpy(dst, src, sizeof(deflate_state))
74 /* Memory management for the window. Useful for allocation the aligned window. */
75 #  define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
76 #  define TRY_FREE_WINDOW(strm, addr) TRY_FREE(strm, addr)
77 /* Invoked at the beginning of deflateSetDictionary(). Useful for checking arch-specific window data. */
78 #  define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
79 /* Invoked at the beginning of deflateGetDictionary(). Useful for adjusting arch-specific window data. */
80 #  define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
81 /* Invoked at the end of deflateResetKeep(). Useful for initializing arch-specific extension blocks. */
82 #  define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
83 /* Invoked at the beginning of deflateParams(). Useful for updating arch-specific compression parameters. */
84 #  define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) do {} while (0)
85 /* Returns whether the last deflate(flush) operation did everything it's supposed to do. */
86 #  define DEFLATE_DONE(strm, flush) 1
87 /* Adjusts the upper bound on compressed data length based on compression parameters and uncompressed data length.
88  * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
89 #  define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
90 /* Returns whether an optimistic upper bound on compressed data length should *not* be used.
91  * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
92 #  define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
93 /* Invoked for each deflate() call. Useful for plugging arch-specific deflation code. */
94 #  define DEFLATE_HOOK(strm, flush, bstate) 0
95 /* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific deflation code already does that. */
96 #  define DEFLATE_NEED_CHECKSUM(strm) 1
97 /* Returns whether reproducibility parameter can be set to a given value. */
98 #  define DEFLATE_CAN_SET_REPRODUCIBLE(strm, reproducible) 1
99 #endif
100 
101 /* ===========================================================================
102  *  Function prototypes.
103  */
104 static int deflateStateCheck      (PREFIX3(stream) *strm);
105 Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush);
106 Z_INTERNAL block_state deflate_fast  (deflate_state *s, int flush);
107 Z_INTERNAL block_state deflate_quick (deflate_state *s, int flush);
108 #ifndef NO_MEDIUM_STRATEGY
109 Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush);
110 #endif
111 Z_INTERNAL block_state deflate_slow  (deflate_state *s, int flush);
112 Z_INTERNAL block_state deflate_rle   (deflate_state *s, int flush);
113 Z_INTERNAL block_state deflate_huff  (deflate_state *s, int flush);
114 static void lm_set_level         (deflate_state *s, int level);
115 static void lm_init              (deflate_state *s);
116 Z_INTERNAL unsigned read_buf  (PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
117 
118 extern uint32_t update_hash_roll        (deflate_state *const s, uint32_t h, uint32_t val);
119 extern void     insert_string_roll      (deflate_state *const s, uint32_t str, uint32_t count);
120 extern Pos      quick_insert_string_roll(deflate_state *const s, uint32_t str);
121 
122 /* ===========================================================================
123  * Local data
124  */
125 
126 /* Values for max_lazy_match, good_match and max_chain_length, depending on
127  * the desired pack level (0..9). The values given below have been tuned to
128  * exclude worst case performance for pathological files. Better values may be
129  * found for specific files.
130  */
131 typedef struct config_s {
132     uint16_t good_length; /* reduce lazy search above this match length */
133     uint16_t max_lazy;    /* do not perform lazy search above this match length */
134     uint16_t nice_length; /* quit search above this match length */
135     uint16_t max_chain;
136     compress_func func;
137 } config;
138 
139 static const config configuration_table[10] = {
140 /*      good lazy nice chain */
141 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
142 
143 #ifdef NO_QUICK_STRATEGY
144 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
145 /* 2 */ {4,    5, 16,    8, deflate_fast},
146 #else
147 /* 1 */ {0,    0,  0,    0, deflate_quick},
148 /* 2 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
149 #endif
150 
151 #ifdef NO_MEDIUM_STRATEGY
152 /* 3 */ {4,    6, 32,   32, deflate_fast},
153 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
154 /* 5 */ {8,   16, 32,   32, deflate_slow},
155 /* 6 */ {8,   16, 128, 128, deflate_slow},
156 #else
157 /* 3 */ {4,    6, 16,    6, deflate_medium},
158 /* 4 */ {4,   12, 32,   24, deflate_medium},  /* lazy matches */
159 /* 5 */ {8,   16, 32,   32, deflate_medium},
160 /* 6 */ {8,   16, 128, 128, deflate_medium},
161 #endif
162 
163 /* 7 */ {8,   32, 128,  256, deflate_slow},
164 /* 8 */ {32, 128, 258, 1024, deflate_slow},
165 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
166 
167 /* Note: the deflate() code requires max_lazy >= STD_MIN_MATCH and max_chain >= 4
168  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
169  * meaning.
170  */
171 
172 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
173 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
174 
175 
176 /* ===========================================================================
177  * Initialize the hash table. prev[] will be initialized on the fly.
178  */
179 #define CLEAR_HASH(s) do { \
180     memset((unsigned char *)s->head, 0, HASH_SIZE * sizeof(*s->head)); \
181   } while (0)
182 
183 /* ========================================================================= */
PREFIX(deflateInit_)184 int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
185     return PREFIX(deflateInit2_)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
186     /* Todo: ignore strm->next_in if we use it as window */
187 }
188 
189 /* ========================================================================= */
PREFIX(deflateInit2_)190 int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
191                            int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
192     uint32_t window_padding = 0;
193     deflate_state *s;
194     int wrap = 1;
195     static const char my_version[] = PREFIX2(VERSION);
196 
197     cpu_check_features();
198 
199     if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(PREFIX3(stream))) {
200         return Z_VERSION_ERROR;
201     }
202     if (strm == NULL)
203         return Z_STREAM_ERROR;
204 
205     strm->msg = NULL;
206     if (strm->zalloc == NULL) {
207         strm->zalloc = zng_calloc;
208         strm->opaque = NULL;
209     }
210     if (strm->zfree == NULL)
211         strm->zfree = zng_cfree;
212 
213     if (level == Z_DEFAULT_COMPRESSION)
214         level = 6;
215 
216     if (windowBits < 0) { /* suppress zlib wrapper */
217         wrap = 0;
218         windowBits = -windowBits;
219 #ifdef GZIP
220     } else if (windowBits > 15) {
221         wrap = 2;       /* write gzip wrapper instead */
222         windowBits -= 16;
223 #endif
224     }
225     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
226         windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
227         (windowBits == 8 && wrap != 1)) {
228         return Z_STREAM_ERROR;
229     }
230     if (windowBits == 8)
231         windowBits = 9;  /* until 256-byte window bug fixed */
232 
233     s = ZALLOC_DEFLATE_STATE(strm);
234     if (s == NULL)
235         return Z_MEM_ERROR;
236     strm->state = (struct internal_state *)s;
237     s->strm = strm;
238     s->status = INIT_STATE;     /* to pass state test in deflateReset() */
239 
240     s->wrap = wrap;
241     s->gzhead = NULL;
242     s->w_bits = (unsigned int)windowBits;
243     s->w_size = 1 << s->w_bits;
244     s->w_mask = s->w_size - 1;
245 
246 #ifdef X86_PCLMULQDQ_CRC
247     window_padding = 8;
248 #endif
249 
250     s->window = (unsigned char *) ZALLOC_WINDOW(strm, s->w_size + window_padding, 2*sizeof(unsigned char));
251     s->prev   = (Pos *)  ZALLOC(strm, s->w_size, sizeof(Pos));
252     memset(s->prev, 0, s->w_size * sizeof(Pos));
253     s->head   = (Pos *)  ZALLOC(strm, HASH_SIZE, sizeof(Pos));
254 
255     s->high_water = 0;      /* nothing written to s->window yet */
256 
257     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
258 
259     /* We overlay pending_buf and sym_buf. This works since the average size
260      * for length/distance pairs over any compressed block is assured to be 31
261      * bits or less.
262      *
263      * Analysis: The longest fixed codes are a length code of 8 bits plus 5
264      * extra bits, for lengths 131 to 257. The longest fixed distance codes are
265      * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
266      * possible fixed-codes length/distance pair is then 31 bits total.
267      *
268      * sym_buf starts one-fourth of the way into pending_buf. So there are
269      * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
270      * in sym_buf is three bytes -- two for the distance and one for the
271      * literal/length. As each symbol is consumed, the pointer to the next
272      * sym_buf value to read moves forward three bytes. From that symbol, up to
273      * 31 bits are written to pending_buf. The closest the written pending_buf
274      * bits gets to the next sym_buf symbol to read is just before the last
275      * code is written. At that time, 31*(n-2) bits have been written, just
276      * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
277      * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
278      * symbols are written.) The closest the writing gets to what is unread is
279      * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
280      * can range from 128 to 32768.
281      *
282      * Therefore, at a minimum, there are 142 bits of space between what is
283      * written and what is read in the overlain buffers, so the symbols cannot
284      * be overwritten by the compressed data. That space is actually 139 bits,
285      * due to the three-bit fixed-code block header.
286      *
287      * That covers the case where either Z_FIXED is specified, forcing fixed
288      * codes, or when the use of fixed codes is chosen, because that choice
289      * results in a smaller compressed block than dynamic codes. That latter
290      * condition then assures that the above analysis also covers all dynamic
291      * blocks. A dynamic-code block will only be chosen to be emitted if it has
292      * fewer bits than a fixed-code block would for the same set of symbols.
293      * Therefore its average symbol length is assured to be less than 31. So
294      * the compressed data for a dynamic block also cannot overwrite the
295      * symbols from which it is being constructed.
296      */
297 
298     s->pending_buf = (unsigned char *) ZALLOC(strm, s->lit_bufsize, 4);
299     s->pending_buf_size = s->lit_bufsize * 4;
300 
301     if (s->window == NULL || s->prev == NULL || s->head == NULL || s->pending_buf == NULL) {
302         s->status = FINISH_STATE;
303         strm->msg = ERR_MSG(Z_MEM_ERROR);
304         PREFIX(deflateEnd)(strm);
305         return Z_MEM_ERROR;
306     }
307     s->sym_buf = s->pending_buf + s->lit_bufsize;
308     s->sym_end = (s->lit_bufsize - 1) * 3;
309     /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
310      * on 16 bit machines and because stored blocks are restricted to
311      * 64K-1 bytes.
312      */
313 
314     s->level = level;
315     s->strategy = strategy;
316     s->block_open = 0;
317     s->reproducible = 0;
318 
319     return PREFIX(deflateReset)(strm);
320 }
321 
322 /* =========================================================================
323  * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
324  */
deflateStateCheck(PREFIX3 (stream)* strm)325 static int deflateStateCheck (PREFIX3(stream) *strm) {
326     deflate_state *s;
327     if (strm == NULL || strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
328         return 1;
329     s = strm->state;
330     if (s == NULL || s->strm != strm || (s->status != INIT_STATE &&
331 #ifdef GZIP
332                                            s->status != GZIP_STATE &&
333 #endif
334                                            s->status != EXTRA_STATE &&
335                                            s->status != NAME_STATE &&
336                                            s->status != COMMENT_STATE &&
337                                            s->status != HCRC_STATE &&
338                                            s->status != BUSY_STATE &&
339                                            s->status != FINISH_STATE))
340         return 1;
341     return 0;
342 }
343 
344 /* ========================================================================= */
PREFIX(deflateSetDictionary)345 int32_t Z_EXPORT PREFIX(deflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
346     deflate_state *s;
347     unsigned int str, n;
348     int wrap;
349     uint32_t avail;
350     const unsigned char *next;
351 
352     if (deflateStateCheck(strm) || dictionary == NULL)
353         return Z_STREAM_ERROR;
354     s = strm->state;
355     wrap = s->wrap;
356     if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
357         return Z_STREAM_ERROR;
358 
359     /* when using zlib wrappers, compute Adler-32 for provided dictionary */
360     if (wrap == 1)
361         strm->adler = functable.adler32(strm->adler, dictionary, dictLength);
362     DEFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
363     s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
364 
365     /* if dictionary would fill window, just replace the history */
366     if (dictLength >= s->w_size) {
367         if (wrap == 0) {            /* already empty otherwise */
368             CLEAR_HASH(s);
369             s->strstart = 0;
370             s->block_start = 0;
371             s->insert = 0;
372         }
373         dictionary += dictLength - s->w_size;  /* use the tail */
374         dictLength = s->w_size;
375     }
376 
377     /* insert dictionary into window and hash */
378     avail = strm->avail_in;
379     next = strm->next_in;
380     strm->avail_in = dictLength;
381     strm->next_in = (z_const unsigned char *)dictionary;
382     fill_window(s);
383     while (s->lookahead >= STD_MIN_MATCH) {
384         str = s->strstart;
385         n = s->lookahead - (STD_MIN_MATCH - 1);
386         functable.insert_string(s, str, n);
387         s->strstart = str + n;
388         s->lookahead = STD_MIN_MATCH - 1;
389         fill_window(s);
390     }
391     s->strstart += s->lookahead;
392     s->block_start = (int)s->strstart;
393     s->insert = s->lookahead;
394     s->lookahead = 0;
395     s->prev_length = 0;
396     s->match_available = 0;
397     strm->next_in = (z_const unsigned char *)next;
398     strm->avail_in = avail;
399     s->wrap = wrap;
400     return Z_OK;
401 }
402 
403 /* ========================================================================= */
PREFIX(deflateGetDictionary)404 int32_t Z_EXPORT PREFIX(deflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
405     deflate_state *s;
406     unsigned int len;
407 
408     if (deflateStateCheck(strm))
409         return Z_STREAM_ERROR;
410     DEFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
411     s = strm->state;
412     len = s->strstart + s->lookahead;
413     if (len > s->w_size)
414         len = s->w_size;
415     if (dictionary != NULL && len)
416         memcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
417     if (dictLength != NULL)
418         *dictLength = len;
419     return Z_OK;
420 }
421 
422 /* ========================================================================= */
PREFIX(deflateResetKeep)423 int32_t Z_EXPORT PREFIX(deflateResetKeep)(PREFIX3(stream) *strm) {
424     deflate_state *s;
425 
426     if (deflateStateCheck(strm))
427         return Z_STREAM_ERROR;
428 
429     strm->total_in = strm->total_out = 0;
430     strm->msg = NULL; /* use zfree if we ever allocate msg dynamically */
431     strm->data_type = Z_UNKNOWN;
432 
433     s = (deflate_state *)strm->state;
434     s->pending = 0;
435     s->pending_out = s->pending_buf;
436 
437     if (s->wrap < 0)
438         s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
439 
440     s->status =
441 #ifdef GZIP
442         s->wrap == 2 ? GZIP_STATE :
443 #endif
444         INIT_STATE;
445 
446 #ifdef GZIP
447     if (s->wrap == 2) {
448         strm->adler = functable.crc32_fold_reset(&s->crc_fold);
449     } else
450 #endif
451         strm->adler = ADLER32_INITIAL_VALUE;
452     s->last_flush = -2;
453 
454     zng_tr_init(s);
455 
456     DEFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */
457 
458     return Z_OK;
459 }
460 
461 /* ========================================================================= */
PREFIX(deflateReset)462 int32_t Z_EXPORT PREFIX(deflateReset)(PREFIX3(stream) *strm) {
463     int ret;
464 
465     ret = PREFIX(deflateResetKeep)(strm);
466     if (ret == Z_OK)
467         lm_init(strm->state);
468     return ret;
469 }
470 
471 /* ========================================================================= */
PREFIX(deflateSetHeader)472 int32_t Z_EXPORT PREFIX(deflateSetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
473     if (deflateStateCheck(strm) || strm->state->wrap != 2)
474         return Z_STREAM_ERROR;
475     strm->state->gzhead = head;
476     return Z_OK;
477 }
478 
479 /* ========================================================================= */
PREFIX(deflatePending)480 int32_t Z_EXPORT PREFIX(deflatePending)(PREFIX3(stream) *strm, uint32_t *pending, int32_t *bits) {
481     if (deflateStateCheck(strm))
482         return Z_STREAM_ERROR;
483     if (pending != NULL)
484         *pending = strm->state->pending;
485     if (bits != NULL)
486         *bits = strm->state->bi_valid;
487     return Z_OK;
488 }
489 
490 /* ========================================================================= */
PREFIX(deflatePrime)491 int32_t Z_EXPORT PREFIX(deflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
492     deflate_state *s;
493     uint64_t value64 = (uint64_t)value;
494     int32_t put;
495 
496     if (deflateStateCheck(strm))
497         return Z_STREAM_ERROR;
498     s = strm->state;
499     if (bits < 0 || bits > BIT_BUF_SIZE || bits > (int32_t)(sizeof(value) << 3) ||
500         s->sym_buf < s->pending_out + ((BIT_BUF_SIZE + 7) >> 3))
501         return Z_BUF_ERROR;
502     do {
503         put = BIT_BUF_SIZE - s->bi_valid;
504         put = MIN(put, bits);
505 
506         if (s->bi_valid == 0)
507             s->bi_buf = value64;
508         else
509             s->bi_buf |= (value64 & ((UINT64_C(1) << put) - 1)) << s->bi_valid;
510         s->bi_valid += put;
511         zng_tr_flush_bits(s);
512         value64 >>= put;
513         bits -= put;
514     } while (bits);
515     return Z_OK;
516 }
517 
518 /* ========================================================================= */
PREFIX(deflateParams)519 int32_t Z_EXPORT PREFIX(deflateParams)(PREFIX3(stream) *strm, int32_t level, int32_t strategy) {
520     deflate_state *s;
521     compress_func func;
522     int hook_flush = Z_NO_FLUSH;
523 
524     if (deflateStateCheck(strm))
525         return Z_STREAM_ERROR;
526     s = strm->state;
527 
528     if (level == Z_DEFAULT_COMPRESSION)
529         level = 6;
530     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED)
531         return Z_STREAM_ERROR;
532     DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush);  /* hook for IBM Z DFLTCC */
533     func = configuration_table[s->level].func;
534 
535     if (((strategy != s->strategy || func != configuration_table[level].func) && s->last_flush != -2)
536         || hook_flush != Z_NO_FLUSH) {
537         /* Flush the last buffer. Use Z_BLOCK mode, unless the hook requests a "stronger" one. */
538         int flush = RANK(hook_flush) > RANK(Z_BLOCK) ? hook_flush : Z_BLOCK;
539         int err = PREFIX(deflate)(strm, flush);
540         if (err == Z_STREAM_ERROR)
541             return err;
542         if (strm->avail_in || ((int)s->strstart - s->block_start) + s->lookahead || !DEFLATE_DONE(strm, flush))
543             return Z_BUF_ERROR;
544     }
545     if (s->level != level) {
546         if (s->level == 0 && s->matches != 0) {
547             if (s->matches == 1) {
548                 functable.slide_hash(s);
549             } else {
550                 CLEAR_HASH(s);
551             }
552             s->matches = 0;
553         }
554 
555         lm_set_level(s, level);
556     }
557     s->strategy = strategy;
558     return Z_OK;
559 }
560 
561 /* ========================================================================= */
PREFIX(deflateTune)562 int32_t Z_EXPORT PREFIX(deflateTune)(PREFIX3(stream) *strm, int32_t good_length, int32_t max_lazy, int32_t nice_length, int32_t max_chain) {
563     deflate_state *s;
564 
565     if (deflateStateCheck(strm))
566         return Z_STREAM_ERROR;
567     s = strm->state;
568     s->good_match = (unsigned int)good_length;
569     s->max_lazy_match = (unsigned int)max_lazy;
570     s->nice_match = nice_length;
571     s->max_chain_length = (unsigned int)max_chain;
572     return Z_OK;
573 }
574 
575 /* =========================================================================
576  * For the default windowBits of 15 and memLevel of 8, this function returns
577  * a close to exact, as well as small, upper bound on the compressed size.
578  * They are coded as constants here for a reason--if the #define's are
579  * changed, then this function needs to be changed as well.  The return
580  * value for 15 and 8 only works for those exact settings.
581  *
582  * For any setting other than those defaults for windowBits and memLevel,
583  * the value returned is a conservative worst case for the maximum expansion
584  * resulting from using fixed blocks instead of stored blocks, which deflate
585  * can emit on compressed data for some combinations of the parameters.
586  *
587  * This function could be more sophisticated to provide closer upper bounds for
588  * every combination of windowBits and memLevel.  But even the conservative
589  * upper bound of about 14% expansion does not seem onerous for output buffer
590  * allocation.
591  */
PREFIX(deflateBound)592 unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long sourceLen) {
593     deflate_state *s;
594     unsigned long complen, wraplen;
595 
596     /* conservative upper bound for compressed data */
597     complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
598     DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen);  /* hook for IBM Z DFLTCC */
599 
600     /* if can't get parameters, return conservative bound plus zlib wrapper */
601     if (deflateStateCheck(strm))
602         return complen + 6;
603 
604     /* compute wrapper length */
605     s = strm->state;
606     switch (s->wrap) {
607     case 0:                                 /* raw deflate */
608         wraplen = 0;
609         break;
610     case 1:                                 /* zlib wrapper */
611         wraplen = ZLIB_WRAPLEN + (s->strstart ? 4 : 0);
612         break;
613 #ifdef GZIP
614     case 2:                                 /* gzip wrapper */
615         wraplen = GZIP_WRAPLEN;
616         if (s->gzhead != NULL) {            /* user-supplied gzip header */
617             unsigned char *str;
618             if (s->gzhead->extra != NULL) {
619                 wraplen += 2 + s->gzhead->extra_len;
620             }
621             str = s->gzhead->name;
622             if (str != NULL) {
623                 do {
624                     wraplen++;
625                 } while (*str++);
626             }
627             str = s->gzhead->comment;
628             if (str != NULL) {
629                 do {
630                     wraplen++;
631                 } while (*str++);
632             }
633             if (s->gzhead->hcrc)
634                 wraplen += 2;
635         }
636         break;
637 #endif
638     default:                                /* for compiler happiness */
639         wraplen = ZLIB_WRAPLEN;
640     }
641 
642     /* if not default parameters, return conservative bound */
643     if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) ||  /* hook for IBM Z DFLTCC */
644             s->w_bits != 15 || HASH_BITS < 15)
645         return complen + wraplen;
646 
647 #ifndef NO_QUICK_STRATEGY
648     return sourceLen                       /* The source size itself */
649       + (sourceLen == 0 ? 1 : 0)           /* Always at least one byte for any input */
650       + (sourceLen < 9 ? 1 : 0)            /* One extra byte for lengths less than 9 */
651       + DEFLATE_QUICK_OVERHEAD(sourceLen)  /* Source encoding overhead, padded to next full byte */
652       + DEFLATE_BLOCK_OVERHEAD             /* Deflate block overhead bytes */
653       + wraplen;                           /* none, zlib or gzip wrapper */
654 #else
655     return sourceLen + (sourceLen >> 4) + 7 + wraplen;
656 #endif
657 }
658 
659 /* =========================================================================
660  * Flush as much pending output as possible. All deflate() output, except for
661  * some deflate_stored() output, goes through this function so some
662  * applications may wish to modify it to avoid allocating a large
663  * strm->next_out buffer and copying into it. (See also read_buf()).
664  */
PREFIX(flush_pending)665 Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm) {
666     uint32_t len;
667     deflate_state *s = strm->state;
668 
669     zng_tr_flush_bits(s);
670     len = s->pending;
671     if (len > strm->avail_out)
672         len = strm->avail_out;
673     if (len == 0)
674         return;
675 
676     Tracev((stderr, "[FLUSH]"));
677     memcpy(strm->next_out, s->pending_out, len);
678     strm->next_out  += len;
679     s->pending_out  += len;
680     strm->total_out += len;
681     strm->avail_out -= len;
682     s->pending      -= len;
683     if (s->pending == 0)
684         s->pending_out = s->pending_buf;
685 }
686 
687 /* ===========================================================================
688  * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
689  */
690 #define HCRC_UPDATE(beg) \
691     do { \
692         if (s->gzhead->hcrc && s->pending > (beg)) \
693             strm->adler = PREFIX(crc32)(strm->adler, s->pending_buf + (beg), s->pending - (beg)); \
694     } while (0)
695 
696 /* ========================================================================= */
PREFIX(deflate)697 int32_t Z_EXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int32_t flush) {
698     int32_t old_flush; /* value of flush param for previous deflate call */
699     deflate_state *s;
700 
701     if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0)
702         return Z_STREAM_ERROR;
703     s = strm->state;
704 
705     if (strm->next_out == NULL || (strm->avail_in != 0 && strm->next_in == NULL)
706         || (s->status == FINISH_STATE && flush != Z_FINISH)) {
707         ERR_RETURN(strm, Z_STREAM_ERROR);
708     }
709     if (strm->avail_out == 0) {
710         ERR_RETURN(strm, Z_BUF_ERROR);
711     }
712 
713     old_flush = s->last_flush;
714     s->last_flush = flush;
715 
716     /* Flush as much pending output as possible */
717     if (s->pending != 0) {
718         PREFIX(flush_pending)(strm);
719         if (strm->avail_out == 0) {
720             /* Since avail_out is 0, deflate will be called again with
721              * more output space, but possibly with both pending and
722              * avail_in equal to zero. There won't be anything to do,
723              * but this is not an error situation so make sure we
724              * return OK instead of BUF_ERROR at next call of deflate:
725              */
726             s->last_flush = -1;
727             return Z_OK;
728         }
729 
730         /* Make sure there is something to do and avoid duplicate consecutive
731          * flushes. For repeated and useless calls with Z_FINISH, we keep
732          * returning Z_STREAM_END instead of Z_BUF_ERROR.
733          */
734     } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) {
735         ERR_RETURN(strm, Z_BUF_ERROR);
736     }
737 
738     /* User must not provide more input after the first FINISH: */
739     if (s->status == FINISH_STATE && strm->avail_in != 0)   {
740         ERR_RETURN(strm, Z_BUF_ERROR);
741     }
742 
743     /* Write the header */
744     if (s->status == INIT_STATE && s->wrap == 0)
745         s->status = BUSY_STATE;
746     if (s->status == INIT_STATE) {
747         /* zlib header */
748         unsigned int header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
749         unsigned int level_flags;
750 
751         if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
752             level_flags = 0;
753         else if (s->level < 6)
754             level_flags = 1;
755         else if (s->level == 6)
756             level_flags = 2;
757         else
758             level_flags = 3;
759         header |= (level_flags << 6);
760         if (s->strstart != 0)
761             header |= PRESET_DICT;
762         header += 31 - (header % 31);
763 
764         put_short_msb(s, (uint16_t)header);
765 
766         /* Save the adler32 of the preset dictionary: */
767         if (s->strstart != 0)
768             put_uint32_msb(s, strm->adler);
769         strm->adler = ADLER32_INITIAL_VALUE;
770         s->status = BUSY_STATE;
771 
772         /* Compression must start with an empty pending buffer */
773         PREFIX(flush_pending)(strm);
774         if (s->pending != 0) {
775             s->last_flush = -1;
776             return Z_OK;
777         }
778     }
779 #ifdef GZIP
780     if (s->status == GZIP_STATE) {
781         /* gzip header */
782         functable.crc32_fold_reset(&s->crc_fold);
783         put_byte(s, 31);
784         put_byte(s, 139);
785         put_byte(s, 8);
786         if (s->gzhead == NULL) {
787             put_uint32(s, 0);
788             put_byte(s, 0);
789             put_byte(s, s->level == 9 ? 2 :
790                      (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
791             put_byte(s, OS_CODE);
792             s->status = BUSY_STATE;
793 
794             /* Compression must start with an empty pending buffer */
795             PREFIX(flush_pending)(strm);
796             if (s->pending != 0) {
797                 s->last_flush = -1;
798                 return Z_OK;
799             }
800         } else {
801             put_byte(s, (s->gzhead->text ? 1 : 0) +
802                      (s->gzhead->hcrc ? 2 : 0) +
803                      (s->gzhead->extra == NULL ? 0 : 4) +
804                      (s->gzhead->name == NULL ? 0 : 8) +
805                      (s->gzhead->comment == NULL ? 0 : 16)
806                      );
807             put_uint32(s, s->gzhead->time);
808             put_byte(s, s->level == 9 ? 2 : (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
809             put_byte(s, s->gzhead->os & 0xff);
810             if (s->gzhead->extra != NULL)
811                 put_short(s, (uint16_t)s->gzhead->extra_len);
812             if (s->gzhead->hcrc)
813                 strm->adler = PREFIX(crc32)(strm->adler, s->pending_buf, s->pending);
814             s->gzindex = 0;
815             s->status = EXTRA_STATE;
816         }
817     }
818     if (s->status == EXTRA_STATE) {
819         if (s->gzhead->extra != NULL) {
820             uint32_t beg = s->pending;   /* start of bytes to update crc */
821             uint32_t left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
822 
823             while (s->pending + left > s->pending_buf_size) {
824                 uint32_t copy = s->pending_buf_size - s->pending;
825                 memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, copy);
826                 s->pending = s->pending_buf_size;
827                 HCRC_UPDATE(beg);
828                 s->gzindex += copy;
829                 PREFIX(flush_pending)(strm);
830                 if (s->pending != 0) {
831                     s->last_flush = -1;
832                     return Z_OK;
833                 }
834                 beg = 0;
835                 left -= copy;
836             }
837             memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, left);
838             s->pending += left;
839             HCRC_UPDATE(beg);
840             s->gzindex = 0;
841         }
842         s->status = NAME_STATE;
843     }
844     if (s->status == NAME_STATE) {
845         if (s->gzhead->name != NULL) {
846             uint32_t beg = s->pending;   /* start of bytes to update crc */
847             unsigned char val;
848 
849             do {
850                 if (s->pending == s->pending_buf_size) {
851                     HCRC_UPDATE(beg);
852                     PREFIX(flush_pending)(strm);
853                     if (s->pending != 0) {
854                         s->last_flush = -1;
855                         return Z_OK;
856                     }
857                     beg = 0;
858                 }
859                 val = s->gzhead->name[s->gzindex++];
860                 put_byte(s, val);
861             } while (val != 0);
862             HCRC_UPDATE(beg);
863             s->gzindex = 0;
864         }
865         s->status = COMMENT_STATE;
866     }
867     if (s->status == COMMENT_STATE) {
868         if (s->gzhead->comment != NULL) {
869             uint32_t beg = s->pending;  /* start of bytes to update crc */
870             unsigned char val;
871 
872             do {
873                 if (s->pending == s->pending_buf_size) {
874                     HCRC_UPDATE(beg);
875                     PREFIX(flush_pending)(strm);
876                     if (s->pending != 0) {
877                         s->last_flush = -1;
878                         return Z_OK;
879                     }
880                     beg = 0;
881                 }
882                 val = s->gzhead->comment[s->gzindex++];
883                 put_byte(s, val);
884             } while (val != 0);
885             HCRC_UPDATE(beg);
886         }
887         s->status = HCRC_STATE;
888     }
889     if (s->status == HCRC_STATE) {
890         if (s->gzhead->hcrc) {
891             if (s->pending + 2 > s->pending_buf_size) {
892                 PREFIX(flush_pending)(strm);
893                 if (s->pending != 0) {
894                     s->last_flush = -1;
895                     return Z_OK;
896                 }
897             }
898             put_short(s, (uint16_t)strm->adler);
899             functable.crc32_fold_reset(&s->crc_fold);
900         }
901         s->status = BUSY_STATE;
902 
903         /* Compression must start with an empty pending buffer */
904         PREFIX(flush_pending)(strm);
905         if (s->pending != 0) {
906             s->last_flush = -1;
907             return Z_OK;
908         }
909     }
910 #endif
911 
912     /* Start a new block or continue the current one.
913      */
914     if (strm->avail_in != 0 || s->lookahead != 0 || (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
915         block_state bstate;
916 
917         bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate :  /* hook for IBM Z DFLTCC */
918                  s->level == 0 ? deflate_stored(s, flush) :
919                  s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
920                  s->strategy == Z_RLE ? deflate_rle(s, flush) :
921                  (*(configuration_table[s->level].func))(s, flush);
922 
923         if (bstate == finish_started || bstate == finish_done) {
924             s->status = FINISH_STATE;
925         }
926         if (bstate == need_more || bstate == finish_started) {
927             if (strm->avail_out == 0) {
928                 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
929             }
930             return Z_OK;
931             /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
932              * of deflate should use the same flush parameter to make sure
933              * that the flush is complete. So we don't have to output an
934              * empty block here, this will be done at next call. This also
935              * ensures that for a very small output buffer, we emit at most
936              * one empty block.
937              */
938         }
939         if (bstate == block_done) {
940             if (flush == Z_PARTIAL_FLUSH) {
941                 zng_tr_align(s);
942             } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
943                 zng_tr_stored_block(s, (char*)0, 0L, 0);
944                 /* For a full flush, this empty block will be recognized
945                  * as a special marker by inflate_sync().
946                  */
947                 if (flush == Z_FULL_FLUSH) {
948                     CLEAR_HASH(s);             /* forget history */
949                     if (s->lookahead == 0) {
950                         s->strstart = 0;
951                         s->block_start = 0;
952                         s->insert = 0;
953                     }
954                 }
955             }
956             PREFIX(flush_pending)(strm);
957             if (strm->avail_out == 0) {
958                 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
959                 return Z_OK;
960             }
961         }
962     }
963 
964     if (flush != Z_FINISH)
965         return Z_OK;
966 
967     /* Write the trailer */
968 #ifdef GZIP
969     if (s->wrap == 2) {
970         strm->adler = functable.crc32_fold_final(&s->crc_fold);
971 
972         put_uint32(s, strm->adler);
973         put_uint32(s, (uint32_t)strm->total_in);
974     } else
975 #endif
976     {
977         if (s->wrap == 1)
978             put_uint32_msb(s, strm->adler);
979     }
980     PREFIX(flush_pending)(strm);
981     /* If avail_out is zero, the application will call deflate again
982      * to flush the rest.
983      */
984     if (s->wrap > 0)
985         s->wrap = -s->wrap; /* write the trailer only once! */
986     if (s->pending == 0) {
987         Assert(s->bi_valid == 0, "bi_buf not flushed");
988         return Z_STREAM_END;
989     }
990     return Z_OK;
991 }
992 
993 /* ========================================================================= */
PREFIX(deflateEnd)994 int32_t Z_EXPORT PREFIX(deflateEnd)(PREFIX3(stream) *strm) {
995     int32_t status;
996 
997     if (deflateStateCheck(strm))
998         return Z_STREAM_ERROR;
999 
1000     status = strm->state->status;
1001 
1002     /* Deallocate in reverse order of allocations: */
1003     TRY_FREE(strm, strm->state->pending_buf);
1004     TRY_FREE(strm, strm->state->head);
1005     TRY_FREE(strm, strm->state->prev);
1006     TRY_FREE_WINDOW(strm, strm->state->window);
1007 
1008     ZFREE_STATE(strm, strm->state);
1009     strm->state = NULL;
1010 
1011     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1012 }
1013 
1014 /* =========================================================================
1015  * Copy the source state to the destination state.
1016  */
PREFIX(deflateCopy)1017 int32_t Z_EXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
1018     deflate_state *ds;
1019     deflate_state *ss;
1020     uint32_t window_padding = 0;
1021 
1022     if (deflateStateCheck(source) || dest == NULL)
1023         return Z_STREAM_ERROR;
1024 
1025     ss = source->state;
1026 
1027     memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
1028 
1029     ds = ZALLOC_DEFLATE_STATE(dest);
1030     if (ds == NULL)
1031         return Z_MEM_ERROR;
1032     dest->state = (struct internal_state *) ds;
1033     ZCOPY_DEFLATE_STATE(ds, ss);
1034     ds->strm = dest;
1035 
1036 #ifdef X86_PCLMULQDQ_CRC
1037     window_padding = 8;
1038 #endif
1039 
1040     ds->window = (unsigned char *) ZALLOC_WINDOW(dest, ds->w_size + window_padding, 2*sizeof(unsigned char));
1041     ds->prev   = (Pos *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
1042     ds->head   = (Pos *)  ZALLOC(dest, HASH_SIZE, sizeof(Pos));
1043     ds->pending_buf = (unsigned char *) ZALLOC(dest, ds->lit_bufsize, 4);
1044 
1045     if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) {
1046         PREFIX(deflateEnd)(dest);
1047         return Z_MEM_ERROR;
1048     }
1049 
1050     memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(unsigned char));
1051     memcpy((void *)ds->prev, (void *)ss->prev, ds->w_size * sizeof(Pos));
1052     memcpy((void *)ds->head, (void *)ss->head, HASH_SIZE * sizeof(Pos));
1053     memcpy(ds->pending_buf, ss->pending_buf, ds->pending_buf_size);
1054 
1055     ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1056     ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1057 
1058     ds->l_desc.dyn_tree = ds->dyn_ltree;
1059     ds->d_desc.dyn_tree = ds->dyn_dtree;
1060     ds->bl_desc.dyn_tree = ds->bl_tree;
1061 
1062     return Z_OK;
1063 }
1064 
1065 /* ===========================================================================
1066  * Read a new buffer from the current input stream, update the adler32
1067  * and total number of bytes read.  All deflate() input goes through
1068  * this function so some applications may wish to modify it to avoid
1069  * allocating a large strm->next_in buffer and copying from it.
1070  * (See also flush_pending()).
1071  */
read_buf(PREFIX3 (stream)* strm,unsigned char * buf,unsigned size)1072 Z_INTERNAL unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
1073     uint32_t len = strm->avail_in;
1074 
1075     len = MIN(len, size);
1076     if (len == 0)
1077         return 0;
1078 
1079     strm->avail_in  -= len;
1080 
1081     if (!DEFLATE_NEED_CHECKSUM(strm)) {
1082         memcpy(buf, strm->next_in, len);
1083 #ifdef GZIP
1084     } else if (strm->state->wrap == 2) {
1085         functable.crc32_fold_copy(&strm->state->crc_fold, buf, strm->next_in, len);
1086 #endif
1087     } else {
1088         if (strm->state->wrap == 1)
1089             strm->adler = functable.adler32_fold_copy(strm->adler, buf, strm->next_in, len);
1090         else
1091             memcpy(buf, strm->next_in, len);
1092     }
1093     strm->next_in  += len;
1094     strm->total_in += len;
1095 
1096     return len;
1097 }
1098 
1099 /* ===========================================================================
1100  * Set longest match variables based on level configuration
1101  */
lm_set_level(deflate_state * s,int level)1102 static void lm_set_level(deflate_state *s, int level) {
1103     s->max_lazy_match   = configuration_table[level].max_lazy;
1104     s->good_match       = configuration_table[level].good_length;
1105     s->nice_match       = configuration_table[level].nice_length;
1106     s->max_chain_length = configuration_table[level].max_chain;
1107 
1108     /* Use rolling hash for deflate_slow algorithm with level 9. It allows us to
1109      * properly lookup different hash chains to speed up longest_match search. Since hashing
1110      * method changes depending on the level we cannot put this into functable. */
1111     if (s->max_chain_length > 1024) {
1112         s->update_hash = &update_hash_roll;
1113         s->insert_string = &insert_string_roll;
1114         s->quick_insert_string = &quick_insert_string_roll;
1115     } else {
1116         s->update_hash = functable.update_hash;
1117         s->insert_string = functable.insert_string;
1118         s->quick_insert_string = functable.quick_insert_string;
1119     }
1120 
1121     s->level = level;
1122 }
1123 
1124 /* ===========================================================================
1125  * Initialize the "longest match" routines for a new zlib stream
1126  */
lm_init(deflate_state * s)1127 static void lm_init(deflate_state *s) {
1128     s->window_size = 2 * s->w_size;
1129 
1130     CLEAR_HASH(s);
1131 
1132     /* Set the default configuration parameters:
1133      */
1134     lm_set_level(s, s->level);
1135 
1136     s->strstart = 0;
1137     s->block_start = 0;
1138     s->lookahead = 0;
1139     s->insert = 0;
1140     s->prev_length = 0;
1141     s->match_available = 0;
1142     s->match_start = 0;
1143     s->ins_h = 0;
1144 }
1145 
1146 /* ===========================================================================
1147  * Fill the window when the lookahead becomes insufficient.
1148  * Updates strstart and lookahead.
1149  *
1150  * IN assertion: lookahead < MIN_LOOKAHEAD
1151  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1152  *    At least one byte has been read, or avail_in == 0; reads are
1153  *    performed for at least two bytes (required for the zip translate_eol
1154  *    option -- not supported here).
1155  */
1156 
fill_window(deflate_state * s)1157 void Z_INTERNAL fill_window(deflate_state *s) {
1158     unsigned n;
1159     unsigned int more;    /* Amount of free space at the end of the window. */
1160     unsigned int wsize = s->w_size;
1161 
1162     Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1163 
1164     do {
1165         more = s->window_size - s->lookahead - s->strstart;
1166 
1167         /* If the window is almost full and there is insufficient lookahead,
1168          * move the upper half to the lower one to make room in the upper half.
1169          */
1170         if (s->strstart >= wsize+MAX_DIST(s)) {
1171             memcpy(s->window, s->window+wsize, (unsigned)wsize);
1172             if (s->match_start >= wsize) {
1173                 s->match_start -= wsize;
1174             } else {
1175                 s->match_start = 0;
1176                 s->prev_length = 0;
1177             }
1178             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
1179             s->block_start -= (int)wsize;
1180             if (s->insert > s->strstart)
1181                 s->insert = s->strstart;
1182             functable.slide_hash(s);
1183             more += wsize;
1184         }
1185         if (s->strm->avail_in == 0)
1186             break;
1187 
1188         /* If there was no sliding:
1189          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1190          *    more == window_size - lookahead - strstart
1191          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1192          * => more >= window_size - 2*WSIZE + 2
1193          * In the BIG_MEM or MMAP case (not yet supported),
1194          *   window_size == input_size + MIN_LOOKAHEAD  &&
1195          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1196          * Otherwise, window_size == 2*WSIZE so more >= 2.
1197          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1198          */
1199         Assert(more >= 2, "more < 2");
1200 
1201         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1202         s->lookahead += n;
1203 
1204         /* Initialize the hash value now that we have some input: */
1205         if (s->lookahead + s->insert >= STD_MIN_MATCH) {
1206             unsigned int str = s->strstart - s->insert;
1207             if (UNLIKELY(s->max_chain_length > 1024)) {
1208                 s->ins_h = s->update_hash(s, s->window[str], s->window[str+1]);
1209             } else if (str >= 1) {
1210                 s->quick_insert_string(s, str + 2 - STD_MIN_MATCH);
1211             }
1212             unsigned int count;
1213             if (UNLIKELY(s->lookahead == 1)) {
1214                 count = s->insert - 1;
1215             } else {
1216                 count = s->insert;
1217             }
1218             if (count > 0) {
1219                 s->insert_string(s, str, count);
1220                 s->insert -= count;
1221             }
1222         }
1223         /* If the whole input has less than STD_MIN_MATCH bytes, ins_h is garbage,
1224          * but this is not important since only literal bytes will be emitted.
1225          */
1226     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1227 
1228     /* If the WIN_INIT bytes after the end of the current data have never been
1229      * written, then zero those bytes in order to avoid memory check reports of
1230      * the use of uninitialized (or uninitialised as Julian writes) bytes by
1231      * the longest match routines.  Update the high water mark for the next
1232      * time through here.  WIN_INIT is set to STD_MAX_MATCH since the longest match
1233      * routines allow scanning to strstart + STD_MAX_MATCH, ignoring lookahead.
1234      */
1235     if (s->high_water < s->window_size) {
1236         unsigned int curr = s->strstart + s->lookahead;
1237         unsigned int init;
1238 
1239         if (s->high_water < curr) {
1240             /* Previous high water mark below current data -- zero WIN_INIT
1241              * bytes or up to end of window, whichever is less.
1242              */
1243             init = s->window_size - curr;
1244             if (init > WIN_INIT)
1245                 init = WIN_INIT;
1246             memset(s->window + curr, 0, init);
1247             s->high_water = curr + init;
1248         } else if (s->high_water < curr + WIN_INIT) {
1249             /* High water mark at or above current data, but below current data
1250              * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1251              * to end of window, whichever is less.
1252              */
1253             init = curr + WIN_INIT - s->high_water;
1254             if (init > s->window_size - s->high_water)
1255                 init = s->window_size - s->high_water;
1256             memset(s->window + s->high_water, 0, init);
1257             s->high_water += init;
1258         }
1259     }
1260 
1261     Assert((unsigned long)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1262            "not enough room for search");
1263 }
1264 
1265 #ifndef ZLIB_COMPAT
1266 /* =========================================================================
1267  * Checks whether buffer size is sufficient and whether this parameter is a duplicate.
1268  */
deflateSetParamPre(zng_deflate_param_value ** out,size_t min_size,zng_deflate_param_value * param)1269 static int32_t deflateSetParamPre(zng_deflate_param_value **out, size_t min_size, zng_deflate_param_value *param) {
1270     int32_t buf_error = param->size < min_size;
1271 
1272     if (*out != NULL) {
1273         (*out)->status = Z_BUF_ERROR;
1274         buf_error = 1;
1275     }
1276     *out = param;
1277     return buf_error;
1278 }
1279 
1280 /* ========================================================================= */
zng_deflateSetParams(zng_stream * strm,zng_deflate_param_value * params,size_t count)1281 int32_t Z_EXPORT zng_deflateSetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
1282     size_t i;
1283     deflate_state *s;
1284     zng_deflate_param_value *new_level = NULL;
1285     zng_deflate_param_value *new_strategy = NULL;
1286     zng_deflate_param_value *new_reproducible = NULL;
1287     int param_buf_error;
1288     int version_error = 0;
1289     int buf_error = 0;
1290     int stream_error = 0;
1291     int ret;
1292     int val;
1293 
1294     /* Initialize the statuses. */
1295     for (i = 0; i < count; i++)
1296         params[i].status = Z_OK;
1297 
1298     /* Check whether the stream state is consistent. */
1299     if (deflateStateCheck(strm))
1300         return Z_STREAM_ERROR;
1301     s = strm->state;
1302 
1303     /* Check buffer sizes and detect duplicates. */
1304     for (i = 0; i < count; i++) {
1305         switch (params[i].param) {
1306             case Z_DEFLATE_LEVEL:
1307                 param_buf_error = deflateSetParamPre(&new_level, sizeof(int), &params[i]);
1308                 break;
1309             case Z_DEFLATE_STRATEGY:
1310                 param_buf_error = deflateSetParamPre(&new_strategy, sizeof(int), &params[i]);
1311                 break;
1312             case Z_DEFLATE_REPRODUCIBLE:
1313                 param_buf_error = deflateSetParamPre(&new_reproducible, sizeof(int), &params[i]);
1314                 break;
1315             default:
1316                 params[i].status = Z_VERSION_ERROR;
1317                 version_error = 1;
1318                 param_buf_error = 0;
1319                 break;
1320         }
1321         if (param_buf_error) {
1322             params[i].status = Z_BUF_ERROR;
1323             buf_error = 1;
1324         }
1325     }
1326     /* Exit early if small buffers or duplicates are detected. */
1327     if (buf_error)
1328         return Z_BUF_ERROR;
1329 
1330     /* Apply changes, remember if there were errors. */
1331     if (new_level != NULL || new_strategy != NULL) {
1332         ret = PREFIX(deflateParams)(strm, new_level == NULL ? s->level : *(int *)new_level->buf,
1333                                     new_strategy == NULL ? s->strategy : *(int *)new_strategy->buf);
1334         if (ret != Z_OK) {
1335             if (new_level != NULL)
1336                 new_level->status = Z_STREAM_ERROR;
1337             if (new_strategy != NULL)
1338                 new_strategy->status = Z_STREAM_ERROR;
1339             stream_error = 1;
1340         }
1341     }
1342     if (new_reproducible != NULL) {
1343         val = *(int *)new_reproducible->buf;
1344         if (DEFLATE_CAN_SET_REPRODUCIBLE(strm, val)) {
1345             s->reproducible = val;
1346         } else {
1347             new_reproducible->status = Z_STREAM_ERROR;
1348             stream_error = 1;
1349         }
1350     }
1351 
1352     /* Report version errors only if there are no real errors. */
1353     return stream_error ? Z_STREAM_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
1354 }
1355 
1356 /* ========================================================================= */
zng_deflateGetParams(zng_stream * strm,zng_deflate_param_value * params,size_t count)1357 int32_t Z_EXPORT zng_deflateGetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
1358     deflate_state *s;
1359     size_t i;
1360     int32_t buf_error = 0;
1361     int32_t version_error = 0;
1362 
1363     /* Initialize the statuses. */
1364     for (i = 0; i < count; i++)
1365         params[i].status = Z_OK;
1366 
1367     /* Check whether the stream state is consistent. */
1368     if (deflateStateCheck(strm))
1369         return Z_STREAM_ERROR;
1370     s = strm->state;
1371 
1372     for (i = 0; i < count; i++) {
1373         switch (params[i].param) {
1374             case Z_DEFLATE_LEVEL:
1375                 if (params[i].size < sizeof(int))
1376                     params[i].status = Z_BUF_ERROR;
1377                 else
1378                     *(int *)params[i].buf = s->level;
1379                 break;
1380             case Z_DEFLATE_STRATEGY:
1381                 if (params[i].size < sizeof(int))
1382                     params[i].status = Z_BUF_ERROR;
1383                 else
1384                     *(int *)params[i].buf = s->strategy;
1385                 break;
1386             case Z_DEFLATE_REPRODUCIBLE:
1387                 if (params[i].size < sizeof(int))
1388                     params[i].status = Z_BUF_ERROR;
1389                 else
1390                     *(int *)params[i].buf = s->reproducible;
1391                 break;
1392             default:
1393                 params[i].status = Z_VERSION_ERROR;
1394                 version_error = 1;
1395                 break;
1396         }
1397         if (params[i].status == Z_BUF_ERROR)
1398             buf_error = 1;
1399     }
1400     return buf_error ? Z_BUF_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
1401 }
1402 #endif
1403