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