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 "deflate.h"
52#include "deflate_p.h"
53#include "functable.h"
54
55const char PREFIX(deflate_copyright)[] = " deflate 1.2.11.f Copyright 1995-2016 Jean-loup Gailly and Mark Adler ";
56/*
57 If you use the zlib library in a product, an acknowledgment is welcome
58 in the documentation of your product. If for some reason you cannot
59 include such an acknowledgment, I would appreciate that you keep this
60 copyright string in the executable of your product.
61 */
62
63/* ===========================================================================
64 * Architecture-specific hooks.
65 */
66#ifdef S390_DFLTCC_DEFLATE
67# include "arch/s390/dfltcc_deflate.h"
68#else
69/* Memory management for the deflate state. Useful for allocating arch-specific extension blocks. */
70# define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
71# define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
72# define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
73/* Memory management for the window. Useful for allocation the aligned window. */
74# define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
75# define TRY_FREE_WINDOW(strm, addr) TRY_FREE(strm, addr)
76/* Invoked at the beginning of deflateSetDictionary(). Useful for checking arch-specific window data. */
77# define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
78/* Invoked at the beginning of deflateGetDictionary(). Useful for adjusting arch-specific window data. */
79# define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
80/* Invoked at the end of deflateResetKeep(). Useful for initializing arch-specific extension blocks. */
81# define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
82/* Invoked at the beginning of deflateParams(). Useful for updating arch-specific compression parameters. */
83# define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) do {} while (0)
84/* Returns whether the last deflate(flush) operation did everything it's supposed to do. */
85# define DEFLATE_DONE(strm, flush) 1
86/* Adjusts the upper bound on compressed data length based on compression parameters and uncompressed data length.
87 * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
88# define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
89/* Returns whether an optimistic upper bound on compressed data length should *not* be used.
90 * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
91# define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
92/* Invoked for each deflate() call. Useful for plugging arch-specific deflation code. */
93# define DEFLATE_HOOK(strm, flush, bstate) 0
94/* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific deflation code already does that. */
95# define DEFLATE_NEED_CHECKSUM(strm) 1
96/* Returns whether reproducibility parameter can be set to a given value. */
97# define DEFLATE_CAN_SET_REPRODUCIBLE(strm, reproducible) 1
98#endif
99
100/* ===========================================================================
101 * Function prototypes.
102 */
103typedef block_state (*compress_func) (deflate_state *s, int flush);
104/* Compression function. Returns the block state after the call. */
105
106static int deflateStateCheck (PREFIX3(stream) *strm);
107static block_state deflate_stored (deflate_state *s, int flush);
108Z_INTERNAL block_state deflate_fast (deflate_state *s, int flush);
109Z_INTERNAL block_state deflate_quick (deflate_state *s, int flush);
110#ifndef NO_MEDIUM_STRATEGY
111Z_INTERNAL block_state deflate_medium (deflate_state *s, int flush);
112#endif
113Z_INTERNAL block_state deflate_slow (deflate_state *s, int flush);
114static block_state deflate_rle (deflate_state *s, int flush);
115static block_state deflate_huff (deflate_state *s, int flush);
116static void lm_init (deflate_state *s);
117Z_INTERNAL unsigned read_buf (PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
118
119extern void crc_reset(deflate_state *const s);
120#ifdef X86_PCLMULQDQ_CRC
121extern void crc_finalize(deflate_state *const s);
122#endif
123extern void copy_with_crc(PREFIX3(stream) *strm, unsigned char *dst, unsigned long size);
124
125/* ===========================================================================
126 * Local data
127 */
128
129/* Values for max_lazy_match, good_match and max_chain_length, depending on
130 * the desired pack level (0..9). The values given below have been tuned to
131 * exclude worst case performance for pathological files. Better values may be
132 * found for specific files.
133 */
134typedef struct config_s {
135 uint16_t good_length; /* reduce lazy search above this match length */
136 uint16_t max_lazy; /* do not perform lazy search above this match length */
137 uint16_t nice_length; /* quit search above this match length */
138 uint16_t max_chain;
139 compress_func func;
140} config;
141
142static const config configuration_table[10] = {
143/* good lazy nice chain */
144/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
145
146#ifndef NO_QUICK_STRATEGY
147/* 1 */ {.good_length: 4, .max_lazy: 4, .nice_length: 8, .max_chain: 4, .func: deflate_quick},
148/* 2 */ {.good_length: 4, .max_lazy: 4, .nice_length: 8, .max_chain: 4, .func: deflate_fast}, /* max speed, no lazy matches */
149#else
150/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
151/* 2 */ {4, 5, 16, 8, deflate_fast},
152#endif
153
154/* 3 */ {.good_length: 4, .max_lazy: 6, .nice_length: 32, .max_chain: 32, .func: deflate_fast},
155
156#ifdef NO_MEDIUM_STRATEGY
157/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
158/* 5 */ {8, 16, 32, 32, deflate_slow},
159/* 6 */ {8, 16, 128, 128, deflate_slow},
160#else
161/* 4 */ {.good_length: 4, .max_lazy: 4, .nice_length: 16, .max_chain: 16, .func: deflate_medium}, /* lazy matches */
162/* 5 */ {.good_length: 8, .max_lazy: 16, .nice_length: 32, .max_chain: 32, .func: deflate_medium},
163/* 6 */ {.good_length: 8, .max_lazy: 16, .nice_length: 128, .max_chain: 128, .func: deflate_medium},
164#endif
165
166/* 7 */ {.good_length: 8, .max_lazy: 32, .nice_length: 128, .max_chain: 256, .func: deflate_slow},
167/* 8 */ {.good_length: 32, .max_lazy: 128, .nice_length: 258, .max_chain: 1024, .func: deflate_slow},
168/* 9 */ {.good_length: 32, .max_lazy: 258, .nice_length: 258, .max_chain: 4096, .func: deflate_slow}}; /* max compression */
169
170/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
171 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
172 * meaning.
173 */
174
175/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
176#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
177
178
179/* ===========================================================================
180 * Initialize the hash table. prev[] will be initialized on the fly.
181 */
182#define CLEAR_HASH(s) do { \
183 memset((unsigned char *)s->head, 0, HASH_SIZE * sizeof(*s->head)); \
184 } while (0)
185
186/* ===========================================================================
187 * Slide the hash table when sliding the window down (could be avoided with 32
188 * bit values at the expense of memory usage). We slide even when level == 0 to
189 * keep the hash table consistent if we switch back to level > 0 later.
190 */
191Z_INTERNAL void slide_hash_c(deflate_state *s) {
192 Pos *p;
193 unsigned n;
194 unsigned int wsize = s->w_size;
195
196 n = HASH_SIZE;
197 p = &s->head[n];
198#ifdef NOT_TWEAK_COMPILER
199 do {
200 unsigned m;
201 m = *--p;
202 *p = (Pos)(m >= wsize ? m-wsize : 0);
203 } while (--n);
204#else
205 /* As of I make this change, gcc (4.8.*) isn't able to vectorize
206 * this hot loop using saturated-subtraction on x86-64 architecture.
207 * To avoid this defect, we can change the loop such that
208 * o. the pointer advance forward, and
209 * o. demote the variable 'm' to be local to the loop, and
210 * choose type "Pos" (instead of 'unsigned int') for the
211 * variable to avoid unnecessary zero-extension.
212 */
213 {
214 unsigned int i;
215 Pos *q = p - n;
216 for (i = 0; i < n; i++) {
217 Pos m = *q;
218 Pos t = (Pos)wsize;
219 *q++ = (Pos)(m >= t ? m-t: 0);
220 }
221 }
222#endif /* NOT_TWEAK_COMPILER */
223
224 n = wsize;
225 p = &s->prev[n];
226#ifdef NOT_TWEAK_COMPILER
227 do {
228 unsigned m;
229 m = *--p;
230 *p = (Pos)(m >= wsize ? m-wsize : 0);
231 /* If n is not on any hash chain, prev[n] is garbage but
232 * its value will never be used.
233 */
234 } while (--n);
235#else
236 {
237 unsigned int i;
238 Pos *q = p - n;
239 for (i = 0; i < n; i++) {
240 Pos m = *q;
241 Pos t = (Pos)wsize;
242 *q++ = (Pos)(m >= t ? m-t: 0);
243 }
244 }
245#endif /* NOT_TWEAK_COMPILER */
246}
247
248/* ========================================================================= */
249int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
250 return PREFIX(deflateInit2_)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
251 /* Todo: ignore strm->next_in if we use it as window */
252}
253
254/* ========================================================================= */
255int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
256 int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
257 uint32_t window_padding = 0;
258 deflate_state *s;
259 int wrap = 1;
260 static const char my_version[] = PREFIX2(VERSION);
261
262#if defined(X86_FEATURES)
263 x86_check_features();
264#elif defined(ARM_FEATURES)
265 arm_check_features();
266#endif
267
268 if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(PREFIX3(stream))) {
269 return Z_VERSION_ERROR;
270 }
271 if (strm == NULL)
272 return Z_STREAM_ERROR;
273
274 strm->msg = NULL;
275 if (strm->zalloc == NULL) {
276 strm->zalloc = zng_calloc;
277 strm->opaque = NULL;
278 }
279 if (strm->zfree == NULL)
280 strm->zfree = zng_cfree;
281
282 if (level == Z_DEFAULT_COMPRESSION)
283 level = 6;
284
285 if (windowBits < 0) { /* suppress zlib wrapper */
286 wrap = 0;
287 windowBits = -windowBits;
288#ifdef GZIP
289 } else if (windowBits > 15) {
290 wrap = 2; /* write gzip wrapper instead */
291 windowBits -= 16;
292#endif
293 }
294 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < 8 ||
295 windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
296 (windowBits == 8 && wrap != 1)) {
297 return Z_STREAM_ERROR;
298 }
299 if (windowBits == 8)
300 windowBits = 9; /* until 256-byte window bug fixed */
301
302#if !defined(NO_QUICK_STRATEGY) && !defined(S390_DFLTCC_DEFLATE)
303 if (level == 1)
304 windowBits = 13;
305#endif
306
307 s = (deflate_state *) ZALLOC_STATE(strm, 1, sizeof(deflate_state));
308 if (s == NULL)
309 return Z_MEM_ERROR;
310 strm->state = (struct internal_state *)s;
311 s->strm = strm;
312 s->status = INIT_STATE; /* to pass state test in deflateReset() */
313
314 s->wrap = wrap;
315 s->gzhead = NULL;
316 s->w_bits = (unsigned int)windowBits;
317 s->w_size = 1 << s->w_bits;
318 s->w_mask = s->w_size - 1;
319
320#ifdef X86_PCLMULQDQ_CRC
321 window_padding = 8;
322#endif
323
324 s->window = (unsigned char *) ZALLOC_WINDOW(strm, s->w_size + window_padding, 2*sizeof(unsigned char));
325 s->prev = (Pos *) ZALLOC(strm, s->w_size, sizeof(Pos));
326 memset(s: s->prev, c: 0, n: s->w_size * sizeof(Pos));
327 s->head = (Pos *) ZALLOC(strm, HASH_SIZE, sizeof(Pos));
328
329 s->high_water = 0; /* nothing written to s->window yet */
330
331 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
332
333 /* We overlay pending_buf and sym_buf. This works since the average size
334 * for length/distance pairs over any compressed block is assured to be 31
335 * bits or less.
336 *
337 * Analysis: The longest fixed codes are a length code of 8 bits plus 5
338 * extra bits, for lengths 131 to 257. The longest fixed distance codes are
339 * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
340 * possible fixed-codes length/distance pair is then 31 bits total.
341 *
342 * sym_buf starts one-fourth of the way into pending_buf. So there are
343 * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
344 * in sym_buf is three bytes -- two for the distance and one for the
345 * literal/length. As each symbol is consumed, the pointer to the next
346 * sym_buf value to read moves forward three bytes. From that symbol, up to
347 * 31 bits are written to pending_buf. The closest the written pending_buf
348 * bits gets to the next sym_buf symbol to read is just before the last
349 * code is written. At that time, 31*(n-2) bits have been written, just
350 * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
351 * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
352 * symbols are written.) The closest the writing gets to what is unread is
353 * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
354 * can range from 128 to 32768.
355 *
356 * Therefore, at a minimum, there are 142 bits of space between what is
357 * written and what is read in the overlain buffers, so the symbols cannot
358 * be overwritten by the compressed data. That space is actually 139 bits,
359 * due to the three-bit fixed-code block header.
360 *
361 * That covers the case where either Z_FIXED is specified, forcing fixed
362 * codes, or when the use of fixed codes is chosen, because that choice
363 * results in a smaller compressed block than dynamic codes. That latter
364 * condition then assures that the above analysis also covers all dynamic
365 * blocks. A dynamic-code block will only be chosen to be emitted if it has
366 * fewer bits than a fixed-code block would for the same set of symbols.
367 * Therefore its average symbol length is assured to be less than 31. So
368 * the compressed data for a dynamic block also cannot overwrite the
369 * symbols from which it is being constructed.
370 */
371
372 s->pending_buf = (unsigned char *) ZALLOC(strm, s->lit_bufsize, 4);
373 s->pending_buf_size = s->lit_bufsize * 4;
374
375 if (s->window == NULL || s->prev == NULL || s->head == NULL || s->pending_buf == NULL) {
376 s->status = FINISH_STATE;
377 strm->msg = ERR_MSG(Z_MEM_ERROR);
378 PREFIX(deflateEnd)(strm);
379 return Z_MEM_ERROR;
380 }
381 s->sym_buf = s->pending_buf + s->lit_bufsize;
382 s->sym_end = (s->lit_bufsize - 1) * 3;
383 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
384 * on 16 bit machines and because stored blocks are restricted to
385 * 64K-1 bytes.
386 */
387
388 s->level = level;
389 s->strategy = strategy;
390 s->block_open = 0;
391 s->reproducible = 0;
392
393 return PREFIX(deflateReset)(strm);
394}
395
396/* =========================================================================
397 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
398 */
399static int deflateStateCheck (PREFIX3(stream) *strm) {
400 deflate_state *s;
401 if (strm == NULL || strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
402 return 1;
403 s = strm->state;
404 if (s == NULL || s->strm != strm || (s->status != INIT_STATE &&
405#ifdef GZIP
406 s->status != GZIP_STATE &&
407#endif
408 s->status != EXTRA_STATE &&
409 s->status != NAME_STATE &&
410 s->status != COMMENT_STATE &&
411 s->status != HCRC_STATE &&
412 s->status != BUSY_STATE &&
413 s->status != FINISH_STATE))
414 return 1;
415 return 0;
416}
417
418/* ========================================================================= */
419int32_t Z_EXPORT PREFIX(deflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
420 deflate_state *s;
421 unsigned int str, n;
422 int wrap;
423 uint32_t avail;
424 const unsigned char *next;
425
426 if (deflateStateCheck(strm) || dictionary == NULL)
427 return Z_STREAM_ERROR;
428 s = strm->state;
429 wrap = s->wrap;
430 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
431 return Z_STREAM_ERROR;
432
433 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
434 if (wrap == 1)
435 strm->adler = functable.adler32(strm->adler, dictionary, dictLength);
436 DEFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength); /* hook for IBM Z DFLTCC */
437 s->wrap = 0; /* avoid computing Adler-32 in read_buf */
438
439 /* if dictionary would fill window, just replace the history */
440 if (dictLength >= s->w_size) {
441 if (wrap == 0) { /* already empty otherwise */
442 CLEAR_HASH(s);
443 s->strstart = 0;
444 s->block_start = 0;
445 s->insert = 0;
446 }
447 dictionary += dictLength - s->w_size; /* use the tail */
448 dictLength = s->w_size;
449 }
450
451 /* insert dictionary into window and hash */
452 avail = strm->avail_in;
453 next = strm->next_in;
454 strm->avail_in = dictLength;
455 strm->next_in = (z_const unsigned char *)dictionary;
456 fill_window(s);
457 while (s->lookahead >= MIN_MATCH) {
458 str = s->strstart;
459 n = s->lookahead - (MIN_MATCH-1);
460 functable.insert_string(s, str, n);
461 s->strstart = str + n;
462 s->lookahead = MIN_MATCH-1;
463 fill_window(s);
464 }
465 s->strstart += s->lookahead;
466 s->block_start = (int)s->strstart;
467 s->insert = s->lookahead;
468 s->lookahead = 0;
469 s->prev_length = MIN_MATCH-1;
470 s->match_available = 0;
471 strm->next_in = (z_const unsigned char *)next;
472 strm->avail_in = avail;
473 s->wrap = wrap;
474 return Z_OK;
475}
476
477/* ========================================================================= */
478int32_t Z_EXPORT PREFIX(deflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
479 deflate_state *s;
480 unsigned int len;
481
482 if (deflateStateCheck(strm))
483 return Z_STREAM_ERROR;
484 DEFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength); /* hook for IBM Z DFLTCC */
485 s = strm->state;
486 len = s->strstart + s->lookahead;
487 if (len > s->w_size)
488 len = s->w_size;
489 if (dictionary != NULL && len)
490 memcpy(dest: dictionary, src: s->window + s->strstart + s->lookahead - len, n: len);
491 if (dictLength != NULL)
492 *dictLength = len;
493 return Z_OK;
494}
495
496/* ========================================================================= */
497int32_t Z_EXPORT PREFIX(deflateResetKeep)(PREFIX3(stream) *strm) {
498 deflate_state *s;
499
500 if (deflateStateCheck(strm))
501 return Z_STREAM_ERROR;
502
503 strm->total_in = strm->total_out = 0;
504 strm->msg = NULL; /* use zfree if we ever allocate msg dynamically */
505 strm->data_type = Z_UNKNOWN;
506
507 s = (deflate_state *)strm->state;
508 s->pending = 0;
509 s->pending_out = s->pending_buf;
510
511 if (s->wrap < 0)
512 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
513
514 s->status =
515#ifdef GZIP
516 s->wrap == 2 ? GZIP_STATE :
517#endif
518 INIT_STATE;
519
520#ifdef GZIP
521 if (s->wrap == 2)
522 crc_reset(s);
523 else
524#endif
525 strm->adler = ADLER32_INITIAL_VALUE;
526 s->last_flush = -2;
527
528 zng_tr_init(s);
529
530 DEFLATE_RESET_KEEP_HOOK(strm); /* hook for IBM Z DFLTCC */
531
532 return Z_OK;
533}
534
535/* ========================================================================= */
536int32_t Z_EXPORT PREFIX(deflateReset)(PREFIX3(stream) *strm) {
537 int ret;
538
539 ret = PREFIX(deflateResetKeep)(strm);
540 if (ret == Z_OK)
541 lm_init(s: strm->state);
542 return ret;
543}
544
545/* ========================================================================= */
546int32_t Z_EXPORT PREFIX(deflateSetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
547 if (deflateStateCheck(strm) || strm->state->wrap != 2)
548 return Z_STREAM_ERROR;
549 strm->state->gzhead = head;
550 return Z_OK;
551}
552
553/* ========================================================================= */
554int32_t Z_EXPORT PREFIX(deflatePending)(PREFIX3(stream) *strm, uint32_t *pending, int32_t *bits) {
555 if (deflateStateCheck(strm))
556 return Z_STREAM_ERROR;
557 if (pending != NULL)
558 *pending = strm->state->pending;
559 if (bits != NULL)
560 *bits = strm->state->bi_valid;
561 return Z_OK;
562}
563
564/* ========================================================================= */
565int32_t Z_EXPORT PREFIX(deflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
566 deflate_state *s;
567 uint64_t value64 = (uint64_t)value;
568 int32_t put;
569
570 if (deflateStateCheck(strm))
571 return Z_STREAM_ERROR;
572 s = strm->state;
573 if (bits < 0 || bits > BIT_BUF_SIZE || bits > (int32_t)(sizeof(value) << 3) ||
574 s->sym_buf < s->pending_out + ((BIT_BUF_SIZE + 7) >> 3))
575 return Z_BUF_ERROR;
576 do {
577 put = BIT_BUF_SIZE - s->bi_valid;
578 if (put > bits)
579 put = bits;
580 if (s->bi_valid == 0)
581 s->bi_buf = value64;
582 else
583 s->bi_buf |= (value64 & ((UINT64_C(1) << put) - 1)) << s->bi_valid;
584 s->bi_valid += put;
585 zng_tr_flush_bits(s);
586 value64 >>= put;
587 bits -= put;
588 } while (bits);
589 return Z_OK;
590}
591
592/* ========================================================================= */
593int32_t Z_EXPORT PREFIX(deflateParams)(PREFIX3(stream) *strm, int32_t level, int32_t strategy) {
594 deflate_state *s;
595 compress_func func;
596 int hook_flush = Z_NO_FLUSH;
597
598 if (deflateStateCheck(strm))
599 return Z_STREAM_ERROR;
600 s = strm->state;
601
602 if (level == Z_DEFAULT_COMPRESSION)
603 level = 6;
604 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED)
605 return Z_STREAM_ERROR;
606 DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush); /* hook for IBM Z DFLTCC */
607 func = configuration_table[s->level].func;
608
609 if (((strategy != s->strategy || func != configuration_table[level].func) && s->last_flush != -2)
610 || hook_flush != Z_NO_FLUSH) {
611 /* Flush the last buffer. Use Z_BLOCK mode, unless the hook requests a "stronger" one. */
612 int flush = RANK(hook_flush) > RANK(Z_BLOCK) ? hook_flush : Z_BLOCK;
613 int err = PREFIX(deflate)(strm, flush);
614 if (err == Z_STREAM_ERROR)
615 return err;
616 if (strm->avail_in || ((int)s->strstart - s->block_start) + s->lookahead || !DEFLATE_DONE(strm, flush))
617 return Z_BUF_ERROR;
618 }
619 if (s->level != level) {
620 if (s->level == 0 && s->matches != 0) {
621 if (s->matches == 1) {
622 functable.slide_hash(s);
623 } else {
624 CLEAR_HASH(s);
625 }
626 s->matches = 0;
627 }
628 s->level = level;
629 s->max_lazy_match = configuration_table[level].max_lazy;
630 s->good_match = configuration_table[level].good_length;
631 s->nice_match = configuration_table[level].nice_length;
632 s->max_chain_length = configuration_table[level].max_chain;
633 }
634 s->strategy = strategy;
635 return Z_OK;
636}
637
638/* ========================================================================= */
639int32_t Z_EXPORT PREFIX(deflateTune)(PREFIX3(stream) *strm, int32_t good_length, int32_t max_lazy, int32_t nice_length, int32_t max_chain) {
640 deflate_state *s;
641
642 if (deflateStateCheck(strm))
643 return Z_STREAM_ERROR;
644 s = strm->state;
645 s->good_match = (unsigned int)good_length;
646 s->max_lazy_match = (unsigned int)max_lazy;
647 s->nice_match = nice_length;
648 s->max_chain_length = (unsigned int)max_chain;
649 return Z_OK;
650}
651
652/* =========================================================================
653 * For the default windowBits of 15 and memLevel of 8, this function returns
654 * a close to exact, as well as small, upper bound on the compressed size.
655 * They are coded as constants here for a reason--if the #define's are
656 * changed, then this function needs to be changed as well. The return
657 * value for 15 and 8 only works for those exact settings.
658 *
659 * For any setting other than those defaults for windowBits and memLevel,
660 * the value returned is a conservative worst case for the maximum expansion
661 * resulting from using fixed blocks instead of stored blocks, which deflate
662 * can emit on compressed data for some combinations of the parameters.
663 *
664 * This function could be more sophisticated to provide closer upper bounds for
665 * every combination of windowBits and memLevel. But even the conservative
666 * upper bound of about 14% expansion does not seem onerous for output buffer
667 * allocation.
668 */
669unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long sourceLen) {
670 deflate_state *s;
671 unsigned long complen, wraplen;
672
673 /* conservative upper bound for compressed data */
674 complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
675 DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen); /* hook for IBM Z DFLTCC */
676
677 /* if can't get parameters, return conservative bound plus zlib wrapper */
678 if (deflateStateCheck(strm))
679 return complen + 6;
680
681 /* compute wrapper length */
682 s = strm->state;
683 switch (s->wrap) {
684 case 0: /* raw deflate */
685 wraplen = 0;
686 break;
687 case 1: /* zlib wrapper */
688 wraplen = ZLIB_WRAPLEN + (s->strstart ? 4 : 0);
689 break;
690#ifdef GZIP
691 case 2: /* gzip wrapper */
692 wraplen = GZIP_WRAPLEN;
693 if (s->gzhead != NULL) { /* user-supplied gzip header */
694 unsigned char *str;
695 if (s->gzhead->extra != NULL) {
696 wraplen += 2 + s->gzhead->extra_len;
697 }
698 str = s->gzhead->name;
699 if (str != NULL) {
700 do {
701 wraplen++;
702 } while (*str++);
703 }
704 str = s->gzhead->comment;
705 if (str != NULL) {
706 do {
707 wraplen++;
708 } while (*str++);
709 }
710 if (s->gzhead->hcrc)
711 wraplen += 2;
712 }
713 break;
714#endif
715 default: /* for compiler happiness */
716 wraplen = ZLIB_WRAPLEN;
717 }
718
719 /* if not default parameters, return conservative bound */
720 if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) || /* hook for IBM Z DFLTCC */
721 s->w_bits != 15 || HASH_BITS < 15)
722 return complen + wraplen;
723
724#ifndef NO_QUICK_STRATEGY
725 return sourceLen /* The source size itself */
726 + DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */
727 + DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */
728 + wraplen; /* none, zlib or gzip wrapper */
729#else
730 return sourceLen + (sourceLen >> 4) + 7 + wraplen;
731#endif
732}
733
734/* =========================================================================
735 * Flush as much pending output as possible. All deflate() output, except for
736 * some deflate_stored() output, goes through this function so some
737 * applications may wish to modify it to avoid allocating a large
738 * strm->next_out buffer and copying into it. (See also read_buf()).
739 */
740Z_INTERNAL void flush_pending(PREFIX3(stream) *strm) {
741 uint32_t len;
742 deflate_state *s = strm->state;
743
744 zng_tr_flush_bits(s);
745 len = s->pending;
746 if (len > strm->avail_out)
747 len = strm->avail_out;
748 if (len == 0)
749 return;
750
751 Tracev((stderr, "[FLUSH]"));
752 memcpy(dest: strm->next_out, src: s->pending_out, n: len);
753 strm->next_out += len;
754 s->pending_out += len;
755 strm->total_out += len;
756 strm->avail_out -= len;
757 s->pending -= len;
758 if (s->pending == 0)
759 s->pending_out = s->pending_buf;
760}
761
762/* ===========================================================================
763 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
764 */
765#define HCRC_UPDATE(beg) \
766 do { \
767 if (s->gzhead->hcrc && s->pending > (beg)) \
768 strm->adler = PREFIX(crc32)(strm->adler, s->pending_buf + (beg), s->pending - (beg)); \
769 } while (0)
770
771/* ========================================================================= */
772int32_t Z_EXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int32_t flush) {
773 int32_t old_flush; /* value of flush param for previous deflate call */
774 deflate_state *s;
775
776 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0)
777 return Z_STREAM_ERROR;
778 s = strm->state;
779
780 if (strm->next_out == NULL || (strm->avail_in != 0 && strm->next_in == NULL)
781 || (s->status == FINISH_STATE && flush != Z_FINISH)) {
782 ERR_RETURN(strm, Z_STREAM_ERROR);
783 }
784 if (strm->avail_out == 0) {
785 ERR_RETURN(strm, Z_BUF_ERROR);
786 }
787
788 old_flush = s->last_flush;
789 s->last_flush = flush;
790
791 /* Flush as much pending output as possible */
792 if (s->pending != 0) {
793 flush_pending(strm);
794 if (strm->avail_out == 0) {
795 /* Since avail_out is 0, deflate will be called again with
796 * more output space, but possibly with both pending and
797 * avail_in equal to zero. There won't be anything to do,
798 * but this is not an error situation so make sure we
799 * return OK instead of BUF_ERROR at next call of deflate:
800 */
801 s->last_flush = -1;
802 return Z_OK;
803 }
804
805 /* Make sure there is something to do and avoid duplicate consecutive
806 * flushes. For repeated and useless calls with Z_FINISH, we keep
807 * returning Z_STREAM_END instead of Z_BUF_ERROR.
808 */
809 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) {
810 ERR_RETURN(strm, Z_BUF_ERROR);
811 }
812
813 /* User must not provide more input after the first FINISH: */
814 if (s->status == FINISH_STATE && strm->avail_in != 0) {
815 ERR_RETURN(strm, Z_BUF_ERROR);
816 }
817
818 /* Write the header */
819 if (s->status == INIT_STATE && s->wrap == 0)
820 s->status = BUSY_STATE;
821 if (s->status == INIT_STATE) {
822 /* zlib header */
823 unsigned int header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
824 unsigned int level_flags;
825
826 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
827 level_flags = 0;
828 else if (s->level < 6)
829 level_flags = 1;
830 else if (s->level == 6)
831 level_flags = 2;
832 else
833 level_flags = 3;
834 header |= (level_flags << 6);
835 if (s->strstart != 0)
836 header |= PRESET_DICT;
837 header += 31 - (header % 31);
838
839 put_short_msb(s, w: (uint16_t)header);
840
841 /* Save the adler32 of the preset dictionary: */
842 if (s->strstart != 0)
843 put_uint32_msb(s, dw: strm->adler);
844 strm->adler = ADLER32_INITIAL_VALUE;
845 s->status = BUSY_STATE;
846
847 /* Compression must start with an empty pending buffer */
848 flush_pending(strm);
849 if (s->pending != 0) {
850 s->last_flush = -1;
851 return Z_OK;
852 }
853 }
854#ifdef GZIP
855 if (s->status == GZIP_STATE) {
856 /* gzip header */
857 crc_reset(s);
858 put_byte(s, 31);
859 put_byte(s, 139);
860 put_byte(s, 8);
861 if (s->gzhead == NULL) {
862 put_uint32(s, dw: 0);
863 put_byte(s, 0);
864 put_byte(s, s->level == 9 ? 2 :
865 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
866 put_byte(s, OS_CODE);
867 s->status = BUSY_STATE;
868
869 /* Compression must start with an empty pending buffer */
870 flush_pending(strm);
871 if (s->pending != 0) {
872 s->last_flush = -1;
873 return Z_OK;
874 }
875 } else {
876 put_byte(s, (s->gzhead->text ? 1 : 0) +
877 (s->gzhead->hcrc ? 2 : 0) +
878 (s->gzhead->extra == NULL ? 0 : 4) +
879 (s->gzhead->name == NULL ? 0 : 8) +
880 (s->gzhead->comment == NULL ? 0 : 16)
881 );
882 put_uint32(s, dw: s->gzhead->time);
883 put_byte(s, s->level == 9 ? 2 : (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
884 put_byte(s, s->gzhead->os & 0xff);
885 if (s->gzhead->extra != NULL)
886 put_short(s, w: (uint16_t)s->gzhead->extra_len);
887 if (s->gzhead->hcrc)
888 strm->adler = PREFIX(crc32)(crc: strm->adler, buf: s->pending_buf, len: s->pending);
889 s->gzindex = 0;
890 s->status = EXTRA_STATE;
891 }
892 }
893 if (s->status == EXTRA_STATE) {
894 if (s->gzhead->extra != NULL) {
895 uint32_t beg = s->pending; /* start of bytes to update crc */
896 uint32_t left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
897
898 while (s->pending + left > s->pending_buf_size) {
899 uint32_t copy = s->pending_buf_size - s->pending;
900 memcpy(dest: s->pending_buf + s->pending, src: s->gzhead->extra + s->gzindex, n: copy);
901 s->pending = s->pending_buf_size;
902 HCRC_UPDATE(beg);
903 s->gzindex += copy;
904 flush_pending(strm);
905 if (s->pending != 0) {
906 s->last_flush = -1;
907 return Z_OK;
908 }
909 beg = 0;
910 left -= copy;
911 }
912 memcpy(dest: s->pending_buf + s->pending, src: s->gzhead->extra + s->gzindex, n: left);
913 s->pending += left;
914 HCRC_UPDATE(beg);
915 s->gzindex = 0;
916 }
917 s->status = NAME_STATE;
918 }
919 if (s->status == NAME_STATE) {
920 if (s->gzhead->name != NULL) {
921 uint32_t beg = s->pending; /* start of bytes to update crc */
922 unsigned char val;
923
924 do {
925 if (s->pending == s->pending_buf_size) {
926 HCRC_UPDATE(beg);
927 flush_pending(strm);
928 if (s->pending != 0) {
929 s->last_flush = -1;
930 return Z_OK;
931 }
932 beg = 0;
933 }
934 val = s->gzhead->name[s->gzindex++];
935 put_byte(s, val);
936 } while (val != 0);
937 HCRC_UPDATE(beg);
938 s->gzindex = 0;
939 }
940 s->status = COMMENT_STATE;
941 }
942 if (s->status == COMMENT_STATE) {
943 if (s->gzhead->comment != NULL) {
944 uint32_t beg = s->pending; /* start of bytes to update crc */
945 unsigned char val;
946
947 do {
948 if (s->pending == s->pending_buf_size) {
949 HCRC_UPDATE(beg);
950 flush_pending(strm);
951 if (s->pending != 0) {
952 s->last_flush = -1;
953 return Z_OK;
954 }
955 beg = 0;
956 }
957 val = s->gzhead->comment[s->gzindex++];
958 put_byte(s, val);
959 } while (val != 0);
960 HCRC_UPDATE(beg);
961 }
962 s->status = HCRC_STATE;
963 }
964 if (s->status == HCRC_STATE) {
965 if (s->gzhead->hcrc) {
966 if (s->pending + 2 > s->pending_buf_size) {
967 flush_pending(strm);
968 if (s->pending != 0) {
969 s->last_flush = -1;
970 return Z_OK;
971 }
972 }
973 put_short(s, w: (uint16_t)strm->adler);
974 crc_reset(s);
975 }
976 s->status = BUSY_STATE;
977
978 /* Compression must start with an empty pending buffer */
979 flush_pending(strm);
980 if (s->pending != 0) {
981 s->last_flush = -1;
982 return Z_OK;
983 }
984 }
985#endif
986
987 /* Start a new block or continue the current one.
988 */
989 if (strm->avail_in != 0 || s->lookahead != 0 || (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
990 block_state bstate;
991
992 bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate : /* hook for IBM Z DFLTCC */
993 s->level == 0 ? deflate_stored(s, flush) :
994 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
995 s->strategy == Z_RLE ? deflate_rle(s, flush) :
996 (*(configuration_table[s->level].func))(s, flush);
997
998 if (bstate == finish_started || bstate == finish_done) {
999 s->status = FINISH_STATE;
1000 }
1001 if (bstate == need_more || bstate == finish_started) {
1002 if (strm->avail_out == 0) {
1003 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1004 }
1005 return Z_OK;
1006 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1007 * of deflate should use the same flush parameter to make sure
1008 * that the flush is complete. So we don't have to output an
1009 * empty block here, this will be done at next call. This also
1010 * ensures that for a very small output buffer, we emit at most
1011 * one empty block.
1012 */
1013 }
1014 if (bstate == block_done) {
1015 if (flush == Z_PARTIAL_FLUSH) {
1016 zng_tr_align(s);
1017 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1018 zng_tr_stored_block(s, buf: (char*)0, stored_len: 0L, last: 0);
1019 /* For a full flush, this empty block will be recognized
1020 * as a special marker by inflate_sync().
1021 */
1022 if (flush == Z_FULL_FLUSH) {
1023 CLEAR_HASH(s); /* forget history */
1024 if (s->lookahead == 0) {
1025 s->strstart = 0;
1026 s->block_start = 0;
1027 s->insert = 0;
1028 }
1029 }
1030 }
1031 flush_pending(strm);
1032 if (strm->avail_out == 0) {
1033 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1034 return Z_OK;
1035 }
1036 }
1037 }
1038
1039 if (flush != Z_FINISH)
1040 return Z_OK;
1041
1042 /* Write the trailer */
1043#ifdef GZIP
1044 if (s->wrap == 2) {
1045# ifdef X86_PCLMULQDQ_CRC
1046 crc_finalize(s);
1047# endif
1048 put_uint32(s, dw: strm->adler);
1049 put_uint32(s, dw: (uint32_t)strm->total_in);
1050 } else
1051#endif
1052 if (s->wrap == 1)
1053 put_uint32_msb(s, dw: strm->adler);
1054 flush_pending(strm);
1055 /* If avail_out is zero, the application will call deflate again
1056 * to flush the rest.
1057 */
1058 if (s->wrap > 0)
1059 s->wrap = -s->wrap; /* write the trailer only once! */
1060 if (s->pending == 0) {
1061 Assert(s->bi_valid == 0, "bi_buf not flushed");
1062 return Z_STREAM_END;
1063 }
1064 return Z_OK;
1065}
1066
1067/* ========================================================================= */
1068int32_t Z_EXPORT PREFIX(deflateEnd)(PREFIX3(stream) *strm) {
1069 int32_t status;
1070
1071 if (deflateStateCheck(strm))
1072 return Z_STREAM_ERROR;
1073
1074 status = strm->state->status;
1075
1076 /* Deallocate in reverse order of allocations: */
1077 TRY_FREE(strm, strm->state->pending_buf);
1078 TRY_FREE(strm, strm->state->head);
1079 TRY_FREE(strm, strm->state->prev);
1080 TRY_FREE_WINDOW(strm, strm->state->window);
1081
1082 ZFREE_STATE(strm, strm->state);
1083 strm->state = NULL;
1084
1085 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1086}
1087
1088/* =========================================================================
1089 * Copy the source state to the destination state.
1090 */
1091int32_t Z_EXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
1092 deflate_state *ds;
1093 deflate_state *ss;
1094 uint32_t window_padding = 0;
1095
1096 if (deflateStateCheck(strm: source) || dest == NULL)
1097 return Z_STREAM_ERROR;
1098
1099 ss = source->state;
1100
1101 memcpy(dest: (void *)dest, src: (void *)source, n: sizeof(PREFIX3(stream)));
1102
1103 ds = (deflate_state *) ZALLOC_STATE(dest, 1, sizeof(deflate_state));
1104 if (ds == NULL)
1105 return Z_MEM_ERROR;
1106 dest->state = (struct internal_state *) ds;
1107 ZCOPY_STATE((void *)ds, (void *)ss, sizeof(deflate_state));
1108 ds->strm = dest;
1109
1110#ifdef X86_PCLMULQDQ_CRC
1111 window_padding = 8;
1112#endif
1113
1114 ds->window = (unsigned char *) ZALLOC_WINDOW(dest, ds->w_size + window_padding, 2*sizeof(unsigned char));
1115 ds->prev = (Pos *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1116 ds->head = (Pos *) ZALLOC(dest, HASH_SIZE, sizeof(Pos));
1117 ds->pending_buf = (unsigned char *) ZALLOC(dest, ds->lit_bufsize, 4);
1118
1119 if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) {
1120 PREFIX(deflateEnd)(strm: dest);
1121 return Z_MEM_ERROR;
1122 }
1123
1124 memcpy(dest: ds->window, src: ss->window, n: ds->w_size * 2 * sizeof(unsigned char));
1125 memcpy(dest: (void *)ds->prev, src: (void *)ss->prev, n: ds->w_size * sizeof(Pos));
1126 memcpy(dest: (void *)ds->head, src: (void *)ss->head, HASH_SIZE * sizeof(Pos));
1127 memcpy(dest: ds->pending_buf, src: ss->pending_buf, n: ds->pending_buf_size);
1128
1129 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1130 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1131
1132 ds->l_desc.dyn_tree = ds->dyn_ltree;
1133 ds->d_desc.dyn_tree = ds->dyn_dtree;
1134 ds->bl_desc.dyn_tree = ds->bl_tree;
1135
1136 return Z_OK;
1137}
1138
1139/* ===========================================================================
1140 * Read a new buffer from the current input stream, update the adler32
1141 * and total number of bytes read. All deflate() input goes through
1142 * this function so some applications may wish to modify it to avoid
1143 * allocating a large strm->next_in buffer and copying from it.
1144 * (See also flush_pending()).
1145 */
1146Z_INTERNAL unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
1147 uint32_t len = strm->avail_in;
1148
1149 if (len > size)
1150 len = size;
1151 if (len == 0)
1152 return 0;
1153
1154 strm->avail_in -= len;
1155
1156 if (!DEFLATE_NEED_CHECKSUM(strm)) {
1157 memcpy(dest: buf, src: strm->next_in, n: len);
1158#ifdef GZIP
1159 } else if (strm->state->wrap == 2) {
1160 copy_with_crc(strm, dst: buf, size: len);
1161#endif
1162 } else {
1163 memcpy(dest: buf, src: strm->next_in, n: len);
1164 if (strm->state->wrap == 1)
1165 strm->adler = functable.adler32(strm->adler, buf, len);
1166 }
1167 strm->next_in += len;
1168 strm->total_in += len;
1169
1170 return len;
1171}
1172
1173/* ===========================================================================
1174 * Initialize the "longest match" routines for a new zlib stream
1175 */
1176static void lm_init(deflate_state *s) {
1177 s->window_size = 2 * s->w_size;
1178
1179 CLEAR_HASH(s);
1180
1181 /* Set the default configuration parameters:
1182 */
1183 s->max_lazy_match = configuration_table[s->level].max_lazy;
1184 s->good_match = configuration_table[s->level].good_length;
1185 s->nice_match = configuration_table[s->level].nice_length;
1186 s->max_chain_length = configuration_table[s->level].max_chain;
1187
1188 s->strstart = 0;
1189 s->block_start = 0;
1190 s->lookahead = 0;
1191 s->insert = 0;
1192 s->prev_length = MIN_MATCH-1;
1193 s->match_available = 0;
1194 s->match_start = 0;
1195}
1196
1197#ifdef ZLIB_DEBUG
1198#define EQUAL 0
1199/* result of memcmp for equal strings */
1200
1201/* ===========================================================================
1202 * Check that the match at match_start is indeed a match.
1203 */
1204void check_match(deflate_state *s, Pos start, Pos match, int length) {
1205 /* check that the match length is valid*/
1206 if (length < MIN_MATCH || length > MAX_MATCH) {
1207 fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
1208 z_error("invalid match length");
1209 }
1210 /* check that the match isn't at the same position as the start string */
1211 if (match == start) {
1212 fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
1213 z_error("invalid match position");
1214 }
1215 /* check that the match is indeed a match */
1216 if (memcmp(s->window + match, s->window + start, length) != EQUAL) {
1217 int32_t i = 0;
1218 fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
1219 do {
1220 fprintf(stderr, " %03d: match [%02x] start [%02x]\n", i++, s->window[match++], s->window[start++]);
1221 } while (--length != 0);
1222 z_error("invalid match");
1223 }
1224 if (z_verbose > 1) {
1225 fprintf(stderr, "\\[%u,%d]", start-match, length);
1226 do {
1227 putc(s->window[start++], stderr);
1228 } while (--length != 0);
1229 }
1230}
1231#else
1232# define check_match(s, start, match, length)
1233#endif /* ZLIB_DEBUG */
1234
1235/* ===========================================================================
1236 * Fill the window when the lookahead becomes insufficient.
1237 * Updates strstart and lookahead.
1238 *
1239 * IN assertion: lookahead < MIN_LOOKAHEAD
1240 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1241 * At least one byte has been read, or avail_in == 0; reads are
1242 * performed for at least two bytes (required for the zip translate_eol
1243 * option -- not supported here).
1244 */
1245
1246void Z_INTERNAL fill_window(deflate_state *s) {
1247 unsigned n;
1248 unsigned int more; /* Amount of free space at the end of the window. */
1249 unsigned int wsize = s->w_size;
1250
1251 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1252
1253 do {
1254 more = s->window_size - s->lookahead - s->strstart;
1255
1256 /* If the window is almost full and there is insufficient lookahead,
1257 * move the upper half to the lower one to make room in the upper half.
1258 */
1259 if (s->strstart >= wsize+MAX_DIST(s)) {
1260 memcpy(dest: s->window, src: s->window+wsize, n: (unsigned)wsize);
1261 if (s->match_start >= wsize) {
1262 s->match_start -= wsize;
1263 } else {
1264 s->match_start = 0;
1265 s->prev_length = 0;
1266 }
1267 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1268 s->block_start -= (int)wsize;
1269 if (s->insert > s->strstart)
1270 s->insert = s->strstart;
1271 functable.slide_hash(s);
1272 more += wsize;
1273 }
1274 if (s->strm->avail_in == 0)
1275 break;
1276
1277 /* If there was no sliding:
1278 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1279 * more == window_size - lookahead - strstart
1280 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1281 * => more >= window_size - 2*WSIZE + 2
1282 * In the BIG_MEM or MMAP case (not yet supported),
1283 * window_size == input_size + MIN_LOOKAHEAD &&
1284 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1285 * Otherwise, window_size == 2*WSIZE so more >= 2.
1286 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1287 */
1288 Assert(more >= 2, "more < 2");
1289
1290 n = read_buf(strm: s->strm, buf: s->window + s->strstart + s->lookahead, size: more);
1291 s->lookahead += n;
1292
1293 /* Initialize the hash value now that we have some input: */
1294 if (s->lookahead + s->insert >= MIN_MATCH) {
1295 unsigned int str = s->strstart - s->insert;
1296 if (str >= 1)
1297 functable.quick_insert_string(s, str + 2 - MIN_MATCH);
1298#if MIN_MATCH != 3
1299#error Call insert_string() MIN_MATCH-3 more times
1300 while (s->insert) {
1301 functable.quick_insert_string(s, str);
1302 str++;
1303 s->insert--;
1304 if (s->lookahead + s->insert < MIN_MATCH)
1305 break;
1306 }
1307#else
1308 unsigned int count;
1309 if (UNLIKELY(s->lookahead == 1)) {
1310 count = s->insert - 1;
1311 } else {
1312 count = s->insert;
1313 }
1314 if (count > 0) {
1315 functable.insert_string(s, str, count);
1316 s->insert -= count;
1317 }
1318#endif
1319 }
1320 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1321 * but this is not important since only literal bytes will be emitted.
1322 */
1323 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1324
1325 /* If the WIN_INIT bytes after the end of the current data have never been
1326 * written, then zero those bytes in order to avoid memory check reports of
1327 * the use of uninitialized (or uninitialised as Julian writes) bytes by
1328 * the longest match routines. Update the high water mark for the next
1329 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1330 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1331 */
1332 if (s->high_water < s->window_size) {
1333 unsigned int curr = s->strstart + s->lookahead;
1334 unsigned int init;
1335
1336 if (s->high_water < curr) {
1337 /* Previous high water mark below current data -- zero WIN_INIT
1338 * bytes or up to end of window, whichever is less.
1339 */
1340 init = s->window_size - curr;
1341 if (init > WIN_INIT)
1342 init = WIN_INIT;
1343 memset(s: s->window + curr, c: 0, n: init);
1344 s->high_water = curr + init;
1345 } else if (s->high_water < curr + WIN_INIT) {
1346 /* High water mark at or above current data, but below current data
1347 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1348 * to end of window, whichever is less.
1349 */
1350 init = curr + WIN_INIT - s->high_water;
1351 if (init > s->window_size - s->high_water)
1352 init = s->window_size - s->high_water;
1353 memset(s: s->window + s->high_water, c: 0, n: init);
1354 s->high_water += init;
1355 }
1356 }
1357
1358 Assert((unsigned long)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1359 "not enough room for search");
1360}
1361
1362/* ===========================================================================
1363 * Copy without compression as much as possible from the input stream, return
1364 * the current block state.
1365 *
1366 * In case deflateParams() is used to later switch to a non-zero compression
1367 * level, s->matches (otherwise unused when storing) keeps track of the number
1368 * of hash table slides to perform. If s->matches is 1, then one hash table
1369 * slide will be done when switching. If s->matches is 2, the maximum value
1370 * allowed here, then the hash table will be cleared, since two or more slides
1371 * is the same as a clear.
1372 *
1373 * deflate_stored() is written to minimize the number of times an input byte is
1374 * copied. It is most efficient with large input and output buffers, which
1375 * maximizes the opportunites to have a single copy from next_in to next_out.
1376 */
1377static block_state deflate_stored(deflate_state *s, int flush) {
1378 /* Smallest worthy block size when not flushing or finishing. By default
1379 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1380 * large input and output buffers, the stored block size will be larger.
1381 */
1382 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1383
1384 /* Copy as many min_block or larger stored blocks directly to next_out as
1385 * possible. If flushing, copy the remaining available input to next_out as
1386 * stored blocks, if there is enough space.
1387 */
1388 unsigned len, left, have, last = 0;
1389 unsigned used = s->strm->avail_in;
1390 do {
1391 /* Set len to the maximum size block that we can copy directly with the
1392 * available input data and output space. Set left to how much of that
1393 * would be copied from what's left in the window.
1394 */
1395 len = MAX_STORED; /* maximum deflate stored block length */
1396 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1397 if (s->strm->avail_out < have) /* need room for header */
1398 break;
1399 /* maximum stored block length that will fit in avail_out: */
1400 have = s->strm->avail_out - have;
1401 left = (int)s->strstart - s->block_start; /* bytes left in window */
1402 if (len > (unsigned long)left + s->strm->avail_in)
1403 len = left + s->strm->avail_in; /* limit len to the input */
1404 if (len > have)
1405 len = have; /* limit len to the output */
1406
1407 /* If the stored block would be less than min_block in length, or if
1408 * unable to copy all of the available input when flushing, then try
1409 * copying to the window and the pending buffer instead. Also don't
1410 * write an empty block when flushing -- deflate() does that.
1411 */
1412 if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || len != left + s->strm->avail_in))
1413 break;
1414
1415 /* Make a dummy stored block in pending to get the header bytes,
1416 * including any pending bits. This also updates the debugging counts.
1417 */
1418 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1419 zng_tr_stored_block(s, buf: (char *)0, stored_len: 0L, last);
1420
1421 /* Replace the lengths in the dummy stored block with len. */
1422 s->pending -= 4;
1423 put_short(s, w: (uint16_t)len);
1424 put_short(s, w: (uint16_t)~len);
1425
1426 /* Write the stored block header bytes. */
1427 flush_pending(strm: s->strm);
1428
1429 /* Update debugging counts for the data about to be copied. */
1430 cmpr_bits_add(s, len << 3);
1431 sent_bits_add(s, len << 3);
1432
1433 /* Copy uncompressed bytes from the window to next_out. */
1434 if (left) {
1435 if (left > len)
1436 left = len;
1437 memcpy(dest: s->strm->next_out, src: s->window + s->block_start, n: left);
1438 s->strm->next_out += left;
1439 s->strm->avail_out -= left;
1440 s->strm->total_out += left;
1441 s->block_start += (int)left;
1442 len -= left;
1443 }
1444
1445 /* Copy uncompressed bytes directly from next_in to next_out, updating
1446 * the check value.
1447 */
1448 if (len) {
1449 read_buf(strm: s->strm, buf: s->strm->next_out, size: len);
1450 s->strm->next_out += len;
1451 s->strm->avail_out -= len;
1452 s->strm->total_out += len;
1453 }
1454 } while (last == 0);
1455
1456 /* Update the sliding window with the last s->w_size bytes of the copied
1457 * data, or append all of the copied data to the existing window if less
1458 * than s->w_size bytes were copied. Also update the number of bytes to
1459 * insert in the hash tables, in the event that deflateParams() switches to
1460 * a non-zero compression level.
1461 */
1462 used -= s->strm->avail_in; /* number of input bytes directly copied */
1463 if (used) {
1464 /* If any input was used, then no unused input remains in the window,
1465 * therefore s->block_start == s->strstart.
1466 */
1467 if (used >= s->w_size) { /* supplant the previous history */
1468 s->matches = 2; /* clear hash */
1469 memcpy(dest: s->window, src: s->strm->next_in - s->w_size, n: s->w_size);
1470 s->strstart = s->w_size;
1471 s->insert = s->strstart;
1472 } else {
1473 if (s->window_size - s->strstart <= used) {
1474 /* Slide the window down. */
1475 s->strstart -= s->w_size;
1476 memcpy(dest: s->window, src: s->window + s->w_size, n: s->strstart);
1477 if (s->matches < 2)
1478 s->matches++; /* add a pending slide_hash() */
1479 if (s->insert > s->strstart)
1480 s->insert = s->strstart;
1481 }
1482 memcpy(dest: s->window + s->strstart, src: s->strm->next_in - used, n: used);
1483 s->strstart += used;
1484 s->insert += MIN(used, s->w_size - s->insert);
1485 }
1486 s->block_start = (int)s->strstart;
1487 }
1488 if (s->high_water < s->strstart)
1489 s->high_water = s->strstart;
1490
1491 /* If the last block was written to next_out, then done. */
1492 if (last)
1493 return finish_done;
1494
1495 /* If flushing and all input has been consumed, then done. */
1496 if (flush != Z_NO_FLUSH && flush != Z_FINISH && s->strm->avail_in == 0 && (int)s->strstart == s->block_start)
1497 return block_done;
1498
1499 /* Fill the window with any remaining input. */
1500 have = s->window_size - s->strstart;
1501 if (s->strm->avail_in > have && s->block_start >= (int)s->w_size) {
1502 /* Slide the window down. */
1503 s->block_start -= (int)s->w_size;
1504 s->strstart -= s->w_size;
1505 memcpy(dest: s->window, src: s->window + s->w_size, n: s->strstart);
1506 if (s->matches < 2)
1507 s->matches++; /* add a pending slide_hash() */
1508 have += s->w_size; /* more space now */
1509 if (s->insert > s->strstart)
1510 s->insert = s->strstart;
1511 }
1512 if (have > s->strm->avail_in)
1513 have = s->strm->avail_in;
1514 if (have) {
1515 read_buf(strm: s->strm, buf: s->window + s->strstart, size: have);
1516 s->strstart += have;
1517 s->insert += MIN(have, s->w_size - s->insert);
1518 }
1519 if (s->high_water < s->strstart)
1520 s->high_water = s->strstart;
1521
1522 /* There was not enough avail_out to write a complete worthy or flushed
1523 * stored block to next_out. Write a stored block to pending instead, if we
1524 * have enough input for a worthy block, or if flushing and there is enough
1525 * room for the remaining input as a stored block in the pending buffer.
1526 */
1527 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1528 /* maximum stored block length that will fit in pending: */
1529 have = MIN(s->pending_buf_size - have, MAX_STORED);
1530 min_block = MIN(have, s->w_size);
1531 left = (int)s->strstart - s->block_start;
1532 if (left >= min_block || ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && s->strm->avail_in == 0 && left <= have)) {
1533 len = MIN(left, have);
1534 last = flush == Z_FINISH && s->strm->avail_in == 0 && len == left ? 1 : 0;
1535 zng_tr_stored_block(s, buf: (char *)s->window + s->block_start, stored_len: len, last);
1536 s->block_start += (int)len;
1537 flush_pending(strm: s->strm);
1538 }
1539
1540 /* We've done all we can with the available input and output. */
1541 return last ? finish_started : need_more;
1542}
1543
1544
1545/* ===========================================================================
1546 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1547 * one. Do not maintain a hash table. (It will be regenerated if this run of
1548 * deflate switches away from Z_RLE.)
1549 */
1550static block_state deflate_rle(deflate_state *s, int flush) {
1551 int bflush = 0; /* set if current block must be flushed */
1552 unsigned int prev; /* byte at distance one to match */
1553 unsigned char *scan, *strend; /* scan goes up to strend for length of run */
1554 uint32_t match_len = 0;
1555
1556 for (;;) {
1557 /* Make sure that we always have enough lookahead, except
1558 * at the end of the input file. We need MAX_MATCH bytes
1559 * for the longest run, plus one for the unrolled loop.
1560 */
1561 if (s->lookahead <= MAX_MATCH) {
1562 fill_window(s);
1563 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH)
1564 return need_more;
1565 if (s->lookahead == 0)
1566 break; /* flush the current block */
1567 }
1568
1569 /* See how many times the previous byte repeats */
1570 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1571 scan = s->window + s->strstart - 1;
1572 prev = *scan;
1573 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1574 strend = s->window + s->strstart + MAX_MATCH;
1575 do {
1576 } while (prev == *++scan && prev == *++scan &&
1577 prev == *++scan && prev == *++scan &&
1578 prev == *++scan && prev == *++scan &&
1579 prev == *++scan && prev == *++scan &&
1580 scan < strend);
1581 match_len = MAX_MATCH - (unsigned int)(strend - scan);
1582 if (match_len > s->lookahead)
1583 match_len = s->lookahead;
1584 }
1585 Assert(scan <= s->window + s->window_size - 1, "wild scan");
1586 }
1587
1588 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1589 if (match_len >= MIN_MATCH) {
1590 check_match(s, s->strstart, s->strstart - 1, match_len);
1591
1592 bflush = zng_tr_tally_dist(s, dist: 1, len: match_len - MIN_MATCH);
1593
1594 s->lookahead -= match_len;
1595 s->strstart += match_len;
1596 match_len = 0;
1597 } else {
1598 /* No match, output a literal byte */
1599 bflush = zng_tr_tally_lit(s, c: s->window[s->strstart]);
1600 s->lookahead--;
1601 s->strstart++;
1602 }
1603 if (bflush)
1604 FLUSH_BLOCK(s, 0);
1605 }
1606 s->insert = 0;
1607 if (flush == Z_FINISH) {
1608 FLUSH_BLOCK(s, 1);
1609 return finish_done;
1610 }
1611 if (s->sym_next)
1612 FLUSH_BLOCK(s, 0);
1613 return block_done;
1614}
1615
1616/* ===========================================================================
1617 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
1618 * (It will be regenerated if this run of deflate switches away from Huffman.)
1619 */
1620static block_state deflate_huff(deflate_state *s, int flush) {
1621 int bflush = 0; /* set if current block must be flushed */
1622
1623 for (;;) {
1624 /* Make sure that we have a literal to write. */
1625 if (s->lookahead == 0) {
1626 fill_window(s);
1627 if (s->lookahead == 0) {
1628 if (flush == Z_NO_FLUSH)
1629 return need_more;
1630 break; /* flush the current block */
1631 }
1632 }
1633
1634 /* Output a literal byte */
1635 bflush = zng_tr_tally_lit(s, c: s->window[s->strstart]);
1636 s->lookahead--;
1637 s->strstart++;
1638 if (bflush)
1639 FLUSH_BLOCK(s, 0);
1640 }
1641 s->insert = 0;
1642 if (flush == Z_FINISH) {
1643 FLUSH_BLOCK(s, 1);
1644 return finish_done;
1645 }
1646 if (s->sym_next)
1647 FLUSH_BLOCK(s, 0);
1648 return block_done;
1649}
1650
1651#ifndef ZLIB_COMPAT
1652/* =========================================================================
1653 * Checks whether buffer size is sufficient and whether this parameter is a duplicate.
1654 */
1655static int32_t deflateSetParamPre(zng_deflate_param_value **out, size_t min_size, zng_deflate_param_value *param) {
1656 int32_t buf_error = param->size < min_size;
1657
1658 if (*out != NULL) {
1659 (*out)->status = Z_BUF_ERROR;
1660 buf_error = 1;
1661 }
1662 *out = param;
1663 return buf_error;
1664}
1665
1666/* ========================================================================= */
1667int32_t Z_EXPORT zng_deflateSetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
1668 size_t i;
1669 deflate_state *s;
1670 zng_deflate_param_value *new_level = NULL;
1671 zng_deflate_param_value *new_strategy = NULL;
1672 zng_deflate_param_value *new_reproducible = NULL;
1673 int param_buf_error;
1674 int version_error = 0;
1675 int buf_error = 0;
1676 int stream_error = 0;
1677 int ret;
1678 int val;
1679
1680 /* Initialize the statuses. */
1681 for (i = 0; i < count; i++)
1682 params[i].status = Z_OK;
1683
1684 /* Check whether the stream state is consistent. */
1685 if (deflateStateCheck(strm))
1686 return Z_STREAM_ERROR;
1687 s = strm->state;
1688
1689 /* Check buffer sizes and detect duplicates. */
1690 for (i = 0; i < count; i++) {
1691 switch (params[i].param) {
1692 case Z_DEFLATE_LEVEL:
1693 param_buf_error = deflateSetParamPre(&new_level, sizeof(int), &params[i]);
1694 break;
1695 case Z_DEFLATE_STRATEGY:
1696 param_buf_error = deflateSetParamPre(&new_strategy, sizeof(int), &params[i]);
1697 break;
1698 case Z_DEFLATE_REPRODUCIBLE:
1699 param_buf_error = deflateSetParamPre(&new_reproducible, sizeof(int), &params[i]);
1700 break;
1701 default:
1702 params[i].status = Z_VERSION_ERROR;
1703 version_error = 1;
1704 param_buf_error = 0;
1705 break;
1706 }
1707 if (param_buf_error) {
1708 params[i].status = Z_BUF_ERROR;
1709 buf_error = 1;
1710 }
1711 }
1712 /* Exit early if small buffers or duplicates are detected. */
1713 if (buf_error)
1714 return Z_BUF_ERROR;
1715
1716 /* Apply changes, remember if there were errors. */
1717 if (new_level != NULL || new_strategy != NULL) {
1718 ret = PREFIX(deflateParams)(strm, new_level == NULL ? s->level : *(int *)new_level->buf,
1719 new_strategy == NULL ? s->strategy : *(int *)new_strategy->buf);
1720 if (ret != Z_OK) {
1721 if (new_level != NULL)
1722 new_level->status = Z_STREAM_ERROR;
1723 if (new_strategy != NULL)
1724 new_strategy->status = Z_STREAM_ERROR;
1725 stream_error = 1;
1726 }
1727 }
1728 if (new_reproducible != NULL) {
1729 val = *(int *)new_reproducible->buf;
1730 if (DEFLATE_CAN_SET_REPRODUCIBLE(strm, val)) {
1731 s->reproducible = val;
1732 } else {
1733 new_reproducible->status = Z_STREAM_ERROR;
1734 stream_error = 1;
1735 }
1736 }
1737
1738 /* Report version errors only if there are no real errors. */
1739 return stream_error ? Z_STREAM_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
1740}
1741
1742/* ========================================================================= */
1743int32_t Z_EXPORT zng_deflateGetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
1744 deflate_state *s;
1745 size_t i;
1746 int32_t buf_error = 0;
1747 int32_t version_error = 0;
1748
1749 /* Initialize the statuses. */
1750 for (i = 0; i < count; i++)
1751 params[i].status = Z_OK;
1752
1753 /* Check whether the stream state is consistent. */
1754 if (deflateStateCheck(strm))
1755 return Z_STREAM_ERROR;
1756 s = strm->state;
1757
1758 for (i = 0; i < count; i++) {
1759 switch (params[i].param) {
1760 case Z_DEFLATE_LEVEL:
1761 if (params[i].size < sizeof(int))
1762 params[i].status = Z_BUF_ERROR;
1763 else
1764 *(int *)params[i].buf = s->level;
1765 break;
1766 case Z_DEFLATE_STRATEGY:
1767 if (params[i].size < sizeof(int))
1768 params[i].status = Z_BUF_ERROR;
1769 else
1770 *(int *)params[i].buf = s->strategy;
1771 break;
1772 case Z_DEFLATE_REPRODUCIBLE:
1773 if (params[i].size < sizeof(int))
1774 params[i].status = Z_BUF_ERROR;
1775 else
1776 *(int *)params[i].buf = s->reproducible;
1777 break;
1778 default:
1779 params[i].status = Z_VERSION_ERROR;
1780 version_error = 1;
1781 break;
1782 }
1783 if (params[i].status == Z_BUF_ERROR)
1784 buf_error = 1;
1785 }
1786 return buf_error ? Z_BUF_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
1787}
1788#endif
1789