blob: e5a69ddaedfcbe088e146828795b8b702ee07a59 [file] [log] [blame]
initial.commit3d533e02008-07-27 00:38:33 +00001/* deflate.c -- compress data using the deflation algorithm
mark13dc2462017-02-14 22:15:29 -08002 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
initial.commit3d533e02008-07-27 00:38:33 +00003 * 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".
jiadong.zhu6c142162016-06-22 21:22:18 -070040 * Available in http://tools.ietf.org/html/rfc1951
initial.commit3d533e02008-07-27 00:38:33 +000041 *
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
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +000050/* @(#) $Id$ */
robert.bradford10dd6862014-11-05 06:59:34 -080051#include <assert.h>
initial.commit3d533e02008-07-27 00:38:33 +000052#include "deflate.h"
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +000053#include "cpu_features.h"
Adenilson Cavalcantid7f3ca92019-12-12 20:49:49 +000054#include "contrib/optimizations/insert_string.h"
Chris Blumef262c1b2019-12-04 11:18:49 +000055
Adenilson Cavalcantid6d19612018-08-07 16:08:59 +000056#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
57#include "contrib/optimizations/slide_hash_neon.h"
58#endif
Adenilson Cavalcanti21cc38f2018-08-15 01:06:05 +000059#if defined(CRC32_ARMV8_CRC32)
Adenilson Cavalcanti21cc38f2018-08-15 01:06:05 +000060#include "crc32_simd.h"
61#endif
initial.commit3d533e02008-07-27 00:38:33 +000062
63const char deflate_copyright[] =
mark13dc2462017-02-14 22:15:29 -080064 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
initial.commit3d533e02008-07-27 00:38:33 +000065/*
66 If you use the zlib library in a product, an acknowledgment is welcome
67 in the documentation of your product. If for some reason you cannot
68 include such an acknowledgment, I would appreciate that you keep this
69 copyright string in the executable of your product.
70 */
71
72/* ===========================================================================
73 * Function prototypes.
74 */
75typedef enum {
76 need_more, /* block not completed, need more input or more output */
77 block_done, /* block flush performed */
78 finish_started, /* finish started, need only more output at next deflate */
79 finish_done /* finish done, accept no more input or output */
80} block_state;
81
davidben709ed022017-02-02 13:58:58 -080082typedef block_state (*compress_func) OF((deflate_state *s, int flush));
initial.commit3d533e02008-07-27 00:38:33 +000083/* Compression function. Returns the block state after the call. */
84
mark13dc2462017-02-14 22:15:29 -080085local int deflateStateCheck OF((z_streamp strm));
86local void slide_hash OF((deflate_state *s));
initial.commit3d533e02008-07-27 00:38:33 +000087local void fill_window OF((deflate_state *s));
davidben709ed022017-02-02 13:58:58 -080088local block_state deflate_stored OF((deflate_state *s, int flush));
89local block_state deflate_fast OF((deflate_state *s, int flush));
initial.commit3d533e02008-07-27 00:38:33 +000090#ifndef FASTEST
davidben709ed022017-02-02 13:58:58 -080091local block_state deflate_slow OF((deflate_state *s, int flush));
initial.commit3d533e02008-07-27 00:38:33 +000092#endif
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +000093local block_state deflate_rle OF((deflate_state *s, int flush));
94local block_state deflate_huff OF((deflate_state *s, int flush));
initial.commit3d533e02008-07-27 00:38:33 +000095local void lm_init OF((deflate_state *s));
96local void putShortMSB OF((deflate_state *s, uInt b));
97local void flush_pending OF((z_streamp strm));
Daniel Bratell2eb38892018-01-11 01:01:17 +000098unsigned ZLIB_INTERNAL deflate_read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
initial.commit3d533e02008-07-27 00:38:33 +000099#ifdef ASMV
mark13dc2462017-02-14 22:15:29 -0800100# pragma message("Assembler code may have bugs -- use at your own risk")
initial.commit3d533e02008-07-27 00:38:33 +0000101 void match_init OF((void)); /* asm code initialization */
davidben709ed022017-02-02 13:58:58 -0800102 uInt longest_match OF((deflate_state *s, IPos cur_match));
initial.commit3d533e02008-07-27 00:38:33 +0000103#else
davidben709ed022017-02-02 13:58:58 -0800104local uInt longest_match OF((deflate_state *s, IPos cur_match));
initial.commit3d533e02008-07-27 00:38:33 +0000105#endif
initial.commit3d533e02008-07-27 00:38:33 +0000106
mark13dc2462017-02-14 22:15:29 -0800107#ifdef ZLIB_DEBUG
initial.commit3d533e02008-07-27 00:38:33 +0000108local void check_match OF((deflate_state *s, IPos start, IPos match,
109 int length));
110#endif
111
robert.bradford10dd6862014-11-05 06:59:34 -0800112/* From crc32.c */
113extern void ZLIB_INTERNAL crc_reset(deflate_state *const s);
114extern void ZLIB_INTERNAL crc_finalize(deflate_state *const s);
115extern void ZLIB_INTERNAL copy_with_crc(z_streamp strm, Bytef *dst, long size);
116
initial.commit3d533e02008-07-27 00:38:33 +0000117/* ===========================================================================
118 * Local data
119 */
120
121#define NIL 0
122/* Tail of hash chains */
123
124#ifndef TOO_FAR
125# define TOO_FAR 4096
126#endif
127/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
128
initial.commit3d533e02008-07-27 00:38:33 +0000129/* 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 ush good_length; /* reduce lazy search above this match length */
136 ush max_lazy; /* do not perform lazy search above this match length */
137 ush nice_length; /* quit search above this match length */
138 ush max_chain;
139 compress_func func;
140} config;
141
142#ifdef FASTEST
143local const config configuration_table[2] = {
144/* good lazy nice chain */
145/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
146/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
147#else
148local const config configuration_table[10] = {
149/* good lazy nice chain */
150/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
151/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
152/* 2 */ {4, 5, 16, 8, deflate_fast},
153/* 3 */ {4, 6, 32, 32, deflate_fast},
154
155/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
156/* 5 */ {8, 16, 32, 32, deflate_slow},
157/* 6 */ {8, 16, 128, 128, deflate_slow},
158/* 7 */ {8, 32, 128, 256, deflate_slow},
159/* 8 */ {32, 128, 258, 1024, deflate_slow},
160/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
161#endif
162
163/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
164 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
165 * meaning.
166 */
167
jiadong.zhu6c142162016-06-22 21:22:18 -0700168/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
mark13dc2462017-02-14 22:15:29 -0800169#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
jiadong.zhu6c142162016-06-22 21:22:18 -0700170
initial.commit3d533e02008-07-27 00:38:33 +0000171/* ===========================================================================
initial.commit3d533e02008-07-27 00:38:33 +0000172 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
173 * prev[] will be initialized on the fly.
174 */
175#define CLEAR_HASH(s) \
176 s->head[s->hash_size-1] = NIL; \
177 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
178
mark13dc2462017-02-14 22:15:29 -0800179/* ===========================================================================
180 * Slide the hash table when sliding the window down (could be avoided with 32
181 * bit values at the expense of memory usage). We slide even when level == 0 to
182 * keep the hash table consistent if we switch back to level > 0 later.
183 */
184local void slide_hash(s)
185 deflate_state *s;
186{
Adenilson Cavalcantid6d19612018-08-07 16:08:59 +0000187#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
188 /* NEON based hash table rebase. */
189 return neon_slide_hash(s->head, s->prev, s->w_size, s->hash_size);
190#endif
mark13dc2462017-02-14 22:15:29 -0800191 unsigned n, m;
192 Posf *p;
193 uInt wsize = s->w_size;
194
195 n = s->hash_size;
196 p = &s->head[n];
197 do {
198 m = *--p;
199 *p = (Pos)(m >= wsize ? m - wsize : NIL);
200 } while (--n);
201 n = wsize;
202#ifndef FASTEST
203 p = &s->prev[n];
204 do {
205 m = *--p;
206 *p = (Pos)(m >= wsize ? m - wsize : NIL);
207 /* If n is not on any hash chain, prev[n] is garbage but
208 * its value will never be used.
209 */
210 } while (--n);
211#endif
212}
213
initial.commit3d533e02008-07-27 00:38:33 +0000214/* ========================================================================= */
215int ZEXPORT deflateInit_(strm, level, version, stream_size)
216 z_streamp strm;
217 int level;
218 const char *version;
219 int stream_size;
220{
221 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
222 Z_DEFAULT_STRATEGY, version, stream_size);
223 /* To do: ignore strm->next_in if we use it as window */
224}
225
226/* ========================================================================= */
227int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
228 version, stream_size)
229 z_streamp strm;
230 int level;
231 int method;
232 int windowBits;
233 int memLevel;
234 int strategy;
235 const char *version;
236 int stream_size;
237{
robert.bradford10dd6862014-11-05 06:59:34 -0800238 unsigned window_padding = 8;
initial.commit3d533e02008-07-27 00:38:33 +0000239 deflate_state *s;
240 int wrap = 1;
241 static const char my_version[] = ZLIB_VERSION;
242
Adenilson Cavalcanti7c4128a2019-11-21 23:04:38 +0000243 // Needed to activate optimized insert_string() that helps compression
244 // for all wrapper formats (e.g. RAW, ZLIB, GZIP).
245 // Feature detection is not triggered while using RAW mode (i.e. we never
246 // call crc32() with a NULL buffer).
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +0000247#if defined(CRC32_ARMV8_CRC32) || defined(CRC32_SIMD_SSE42_PCLMUL)
248 cpu_check_features();
Adenilson Cavalcanti7c4128a2019-11-21 23:04:38 +0000249#endif
robert.bradford10dd6862014-11-05 06:59:34 -0800250
initial.commit3d533e02008-07-27 00:38:33 +0000251 if (version == Z_NULL || version[0] != my_version[0] ||
252 stream_size != sizeof(z_stream)) {
253 return Z_VERSION_ERROR;
254 }
255 if (strm == Z_NULL) return Z_STREAM_ERROR;
256
257 strm->msg = Z_NULL;
258 if (strm->zalloc == (alloc_func)0) {
jiadong.zhu6c142162016-06-22 21:22:18 -0700259#ifdef Z_SOLO
260 return Z_STREAM_ERROR;
261#else
initial.commit3d533e02008-07-27 00:38:33 +0000262 strm->zalloc = zcalloc;
263 strm->opaque = (voidpf)0;
jiadong.zhu6c142162016-06-22 21:22:18 -0700264#endif
initial.commit3d533e02008-07-27 00:38:33 +0000265 }
jiadong.zhu6c142162016-06-22 21:22:18 -0700266 if (strm->zfree == (free_func)0)
267#ifdef Z_SOLO
268 return Z_STREAM_ERROR;
269#else
270 strm->zfree = zcfree;
271#endif
initial.commit3d533e02008-07-27 00:38:33 +0000272
273#ifdef FASTEST
274 if (level != 0) level = 1;
275#else
276 if (level == Z_DEFAULT_COMPRESSION) level = 6;
277#endif
278
279 if (windowBits < 0) { /* suppress zlib wrapper */
280 wrap = 0;
281 windowBits = -windowBits;
282 }
283#ifdef GZIP
284 else if (windowBits > 15) {
285 wrap = 2; /* write gzip wrapper instead */
286 windowBits -= 16;
287 }
288#endif
289 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
290 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
mark13dc2462017-02-14 22:15:29 -0800291 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
initial.commit3d533e02008-07-27 00:38:33 +0000292 return Z_STREAM_ERROR;
293 }
294 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
295 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
296 if (s == Z_NULL) return Z_MEM_ERROR;
297 strm->state = (struct internal_state FAR *)s;
298 s->strm = strm;
mark13dc2462017-02-14 22:15:29 -0800299 s->status = INIT_STATE; /* to pass state test in deflateReset() */
initial.commit3d533e02008-07-27 00:38:33 +0000300
301 s->wrap = wrap;
302 s->gzhead = Z_NULL;
mark13dc2462017-02-14 22:15:29 -0800303 s->w_bits = (uInt)windowBits;
initial.commit3d533e02008-07-27 00:38:33 +0000304 s->w_size = 1 << s->w_bits;
305 s->w_mask = s->w_size - 1;
306
Hans Wennborg898c6c02020-09-08 21:06:23 +0000307 s->hash_bits = memLevel + 7;
308 if ((x86_cpu_enable_simd || arm_cpu_enable_crc32) && s->hash_bits < 15) {
robert.bradford10dd6862014-11-05 06:59:34 -0800309 s->hash_bits = 15;
robert.bradford10dd6862014-11-05 06:59:34 -0800310 }
311
initial.commit3d533e02008-07-27 00:38:33 +0000312 s->hash_size = 1 << s->hash_bits;
313 s->hash_mask = s->hash_size - 1;
314 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
315
mark13dc2462017-02-14 22:15:29 -0800316 s->window = (Bytef *) ZALLOC(strm,
317 s->w_size + window_padding,
318 2*sizeof(Byte));
initial.commit3d533e02008-07-27 00:38:33 +0000319 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
Adenilson Cavalcanti2326c6c2020-01-23 00:49:29 +0000320 /* Avoid use of uninitialized value, see:
321 * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360
322 */
Adenilson Cavalcantia0f08d62020-01-23 22:58:07 +0000323 zmemzero(s->prev, s->w_size * sizeof(Pos));
initial.commit3d533e02008-07-27 00:38:33 +0000324 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
325
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000326 s->high_water = 0; /* nothing written to s->window yet */
327
initial.commit3d533e02008-07-27 00:38:33 +0000328 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
329
Chris Blume28ea0a92018-08-17 06:16:32 +0000330 /* We overlay pending_buf and sym_buf. This works since the average size
331 * for length/distance pairs over any compressed block is assured to be 31
332 * bits or less.
333 *
334 * Analysis: The longest fixed codes are a length code of 8 bits plus 5
335 * extra bits, for lengths 131 to 257. The longest fixed distance codes are
336 * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
337 * possible fixed-codes length/distance pair is then 31 bits total.
338 *
339 * sym_buf starts one-fourth of the way into pending_buf. So there are
340 * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
341 * in sym_buf is three bytes -- two for the distance and one for the
342 * literal/length. As each symbol is consumed, the pointer to the next
343 * sym_buf value to read moves forward three bytes. From that symbol, up to
344 * 31 bits are written to pending_buf. The closest the written pending_buf
345 * bits gets to the next sym_buf symbol to read is just before the last
346 * code is written. At that time, 31*(n-2) bits have been written, just
347 * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
348 * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
349 * symbols are written.) The closest the writing gets to what is unread is
350 * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
351 * can range from 128 to 32768.
352 *
353 * Therefore, at a minimum, there are 142 bits of space between what is
354 * written and what is read in the overlain buffers, so the symbols cannot
355 * be overwritten by the compressed data. That space is actually 139 bits,
356 * due to the three-bit fixed-code block header.
357 *
358 * That covers the case where either Z_FIXED is specified, forcing fixed
359 * codes, or when the use of fixed codes is chosen, because that choice
360 * results in a smaller compressed block than dynamic codes. That latter
361 * condition then assures that the above analysis also covers all dynamic
362 * blocks. A dynamic-code block will only be chosen to be emitted if it has
363 * fewer bits than a fixed-code block would for the same set of symbols.
364 * Therefore its average symbol length is assured to be less than 31. So
365 * the compressed data for a dynamic block also cannot overwrite the
366 * symbols from which it is being constructed.
367 */
368 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
369 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
initial.commit3d533e02008-07-27 00:38:33 +0000370
371 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
372 s->pending_buf == Z_NULL) {
373 s->status = FINISH_STATE;
jiadong.zhu6c142162016-06-22 21:22:18 -0700374 strm->msg = ERR_MSG(Z_MEM_ERROR);
initial.commit3d533e02008-07-27 00:38:33 +0000375 deflateEnd (strm);
376 return Z_MEM_ERROR;
377 }
Chris Blume28ea0a92018-08-17 06:16:32 +0000378 s->sym_buf = s->pending_buf + s->lit_bufsize;
379 s->sym_end = (s->lit_bufsize - 1) * 3;
380 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
381 * on 16 bit machines and because stored blocks are restricted to
382 * 64K-1 bytes.
383 */
initial.commit3d533e02008-07-27 00:38:33 +0000384
385 s->level = level;
386 s->strategy = strategy;
387 s->method = (Byte)method;
388
389 return deflateReset(strm);
390}
391
mark13dc2462017-02-14 22:15:29 -0800392/* =========================================================================
393 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
394 */
395local int deflateStateCheck (strm)
396 z_streamp strm;
397{
398 deflate_state *s;
399 if (strm == Z_NULL ||
400 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
401 return 1;
402 s = strm->state;
403 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
404#ifdef GZIP
405 s->status != GZIP_STATE &&
406#endif
407 s->status != EXTRA_STATE &&
408 s->status != NAME_STATE &&
409 s->status != COMMENT_STATE &&
410 s->status != HCRC_STATE &&
411 s->status != BUSY_STATE &&
412 s->status != FINISH_STATE))
413 return 1;
414 return 0;
415}
416
initial.commit3d533e02008-07-27 00:38:33 +0000417/* ========================================================================= */
418int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
419 z_streamp strm;
420 const Bytef *dictionary;
421 uInt dictLength;
422{
423 deflate_state *s;
jiadong.zhu6c142162016-06-22 21:22:18 -0700424 uInt str, n;
425 int wrap;
426 unsigned avail;
427 z_const unsigned char *next;
initial.commit3d533e02008-07-27 00:38:33 +0000428
mark13dc2462017-02-14 22:15:29 -0800429 if (deflateStateCheck(strm) || dictionary == Z_NULL)
jiadong.zhu6c142162016-06-22 21:22:18 -0700430 return Z_STREAM_ERROR;
431 s = strm->state;
432 wrap = s->wrap;
433 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
jiadong.zhu90f7dad2016-06-20 04:09:43 -0700434 return Z_STREAM_ERROR;
jmadillbf2aebe2016-06-20 06:58:52 -0700435
jiadong.zhu6c142162016-06-22 21:22:18 -0700436 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
437 if (wrap == 1)
initial.commit3d533e02008-07-27 00:38:33 +0000438 strm->adler = adler32(strm->adler, dictionary, dictLength);
Daniel Bratell2eb38892018-01-11 01:01:17 +0000439 s->wrap = 0; /* avoid computing Adler-32 in deflate_read_buf */
initial.commit3d533e02008-07-27 00:38:33 +0000440
jiadong.zhu6c142162016-06-22 21:22:18 -0700441 /* if dictionary would fill window, just replace the history */
442 if (dictLength >= s->w_size) {
443 if (wrap == 0) { /* already empty otherwise */
444 CLEAR_HASH(s);
445 s->strstart = 0;
446 s->block_start = 0L;
447 s->insert = 0;
448 }
449 dictionary += dictLength - s->w_size; /* use the tail */
450 dictLength = s->w_size;
initial.commit3d533e02008-07-27 00:38:33 +0000451 }
initial.commit3d533e02008-07-27 00:38:33 +0000452
jiadong.zhu6c142162016-06-22 21:22:18 -0700453 /* insert dictionary into window and hash */
454 avail = strm->avail_in;
455 next = strm->next_in;
456 strm->avail_in = dictLength;
457 strm->next_in = (z_const Bytef *)dictionary;
458 fill_window(s);
459 while (s->lookahead >= MIN_MATCH) {
460 str = s->strstart;
461 n = s->lookahead - (MIN_MATCH-1);
462 do {
463 insert_string(s, str);
464 str++;
465 } while (--n);
466 s->strstart = str;
467 s->lookahead = MIN_MATCH-1;
468 fill_window(s);
initial.commit3d533e02008-07-27 00:38:33 +0000469 }
jiadong.zhu6c142162016-06-22 21:22:18 -0700470 s->strstart += s->lookahead;
471 s->block_start = (long)s->strstart;
472 s->insert = s->lookahead;
473 s->lookahead = 0;
474 s->match_length = s->prev_length = MIN_MATCH-1;
475 s->match_available = 0;
476 strm->next_in = next;
477 strm->avail_in = avail;
478 s->wrap = wrap;
initial.commit3d533e02008-07-27 00:38:33 +0000479 return Z_OK;
480}
481
482/* ========================================================================= */
mark13dc2462017-02-14 22:15:29 -0800483int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
484 z_streamp strm;
485 Bytef *dictionary;
486 uInt *dictLength;
487{
488 deflate_state *s;
489 uInt len;
490
491 if (deflateStateCheck(strm))
492 return Z_STREAM_ERROR;
493 s = strm->state;
494 len = s->strstart + s->lookahead;
495 if (len > s->w_size)
496 len = s->w_size;
497 if (dictionary != Z_NULL && len)
498 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
499 if (dictLength != Z_NULL)
500 *dictLength = len;
501 return Z_OK;
502}
503
504/* ========================================================================= */
jiadong.zhu6c142162016-06-22 21:22:18 -0700505int ZEXPORT deflateResetKeep (strm)
initial.commit3d533e02008-07-27 00:38:33 +0000506 z_streamp strm;
507{
508 deflate_state *s;
509
mark13dc2462017-02-14 22:15:29 -0800510 if (deflateStateCheck(strm)) {
initial.commit3d533e02008-07-27 00:38:33 +0000511 return Z_STREAM_ERROR;
512 }
513
514 strm->total_in = strm->total_out = 0;
515 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
516 strm->data_type = Z_UNKNOWN;
517
518 s = (deflate_state *)strm->state;
519 s->pending = 0;
520 s->pending_out = s->pending_buf;
521
522 if (s->wrap < 0) {
523 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
524 }
mark13dc2462017-02-14 22:15:29 -0800525 s->status =
526#ifdef GZIP
527 s->wrap == 2 ? GZIP_STATE :
528#endif
529 s->wrap ? INIT_STATE : BUSY_STATE;
initial.commit3d533e02008-07-27 00:38:33 +0000530 strm->adler =
531#ifdef GZIP
532 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
533#endif
534 adler32(0L, Z_NULL, 0);
535 s->last_flush = Z_NO_FLUSH;
536
537 _tr_init(s);
initial.commit3d533e02008-07-27 00:38:33 +0000538
539 return Z_OK;
540}
541
542/* ========================================================================= */
jiadong.zhu6c142162016-06-22 21:22:18 -0700543int ZEXPORT deflateReset (strm)
544 z_streamp strm;
545{
546 int ret;
547
548 ret = deflateResetKeep(strm);
549 if (ret == Z_OK)
550 lm_init(strm->state);
551 return ret;
552}
553
554/* ========================================================================= */
initial.commit3d533e02008-07-27 00:38:33 +0000555int ZEXPORT deflateSetHeader (strm, head)
556 z_streamp strm;
557 gz_headerp head;
558{
mark13dc2462017-02-14 22:15:29 -0800559 if (deflateStateCheck(strm) || strm->state->wrap != 2)
560 return Z_STREAM_ERROR;
initial.commit3d533e02008-07-27 00:38:33 +0000561 strm->state->gzhead = head;
562 return Z_OK;
563}
564
565/* ========================================================================= */
jiadong.zhu6c142162016-06-22 21:22:18 -0700566int ZEXPORT deflatePending (strm, pending, bits)
567 unsigned *pending;
568 int *bits;
569 z_streamp strm;
570{
mark13dc2462017-02-14 22:15:29 -0800571 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
jiadong.zhu6c142162016-06-22 21:22:18 -0700572 if (pending != Z_NULL)
573 *pending = strm->state->pending;
574 if (bits != Z_NULL)
575 *bits = strm->state->bi_valid;
576 return Z_OK;
577}
578
579/* ========================================================================= */
initial.commit3d533e02008-07-27 00:38:33 +0000580int ZEXPORT deflatePrime (strm, bits, value)
581 z_streamp strm;
582 int bits;
583 int value;
584{
jiadong.zhu6c142162016-06-22 21:22:18 -0700585 deflate_state *s;
586 int put;
587
mark13dc2462017-02-14 22:15:29 -0800588 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
jiadong.zhu6c142162016-06-22 21:22:18 -0700589 s = strm->state;
Chris Blume28ea0a92018-08-17 06:16:32 +0000590 if (s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
jiadong.zhu6c142162016-06-22 21:22:18 -0700591 return Z_BUF_ERROR;
592 do {
593 put = Buf_size - s->bi_valid;
594 if (put > bits)
595 put = bits;
596 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
597 s->bi_valid += put;
598 _tr_flush_bits(s);
599 value >>= put;
600 bits -= put;
601 } while (bits);
initial.commit3d533e02008-07-27 00:38:33 +0000602 return Z_OK;
603}
604
605/* ========================================================================= */
606int ZEXPORT deflateParams(strm, level, strategy)
607 z_streamp strm;
608 int level;
609 int strategy;
610{
611 deflate_state *s;
612 compress_func func;
initial.commit3d533e02008-07-27 00:38:33 +0000613
mark13dc2462017-02-14 22:15:29 -0800614 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
initial.commit3d533e02008-07-27 00:38:33 +0000615 s = strm->state;
616
617#ifdef FASTEST
618 if (level != 0) level = 1;
619#else
620 if (level == Z_DEFAULT_COMPRESSION) level = 6;
621#endif
622 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
623 return Z_STREAM_ERROR;
624 }
625 func = configuration_table[s->level].func;
626
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000627 if ((strategy != s->strategy || func != configuration_table[level].func) &&
mark13dc2462017-02-14 22:15:29 -0800628 s->high_water) {
initial.commit3d533e02008-07-27 00:38:33 +0000629 /* Flush the last buffer: */
mark13dc2462017-02-14 22:15:29 -0800630 int err = deflate(strm, Z_BLOCK);
631 if (err == Z_STREAM_ERROR)
632 return err;
633 if (strm->avail_out == 0)
634 return Z_BUF_ERROR;
initial.commit3d533e02008-07-27 00:38:33 +0000635 }
636 if (s->level != level) {
mark13dc2462017-02-14 22:15:29 -0800637 if (s->level == 0 && s->matches != 0) {
638 if (s->matches == 1)
639 slide_hash(s);
640 else
641 CLEAR_HASH(s);
642 s->matches = 0;
643 }
initial.commit3d533e02008-07-27 00:38:33 +0000644 s->level = level;
645 s->max_lazy_match = configuration_table[level].max_lazy;
646 s->good_match = configuration_table[level].good_length;
647 s->nice_match = configuration_table[level].nice_length;
648 s->max_chain_length = configuration_table[level].max_chain;
649 }
650 s->strategy = strategy;
mark13dc2462017-02-14 22:15:29 -0800651 return Z_OK;
initial.commit3d533e02008-07-27 00:38:33 +0000652}
653
654/* ========================================================================= */
655int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
656 z_streamp strm;
657 int good_length;
658 int max_lazy;
659 int nice_length;
660 int max_chain;
661{
662 deflate_state *s;
663
mark13dc2462017-02-14 22:15:29 -0800664 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
initial.commit3d533e02008-07-27 00:38:33 +0000665 s = strm->state;
mark13dc2462017-02-14 22:15:29 -0800666 s->good_match = (uInt)good_length;
667 s->max_lazy_match = (uInt)max_lazy;
initial.commit3d533e02008-07-27 00:38:33 +0000668 s->nice_match = nice_length;
mark13dc2462017-02-14 22:15:29 -0800669 s->max_chain_length = (uInt)max_chain;
initial.commit3d533e02008-07-27 00:38:33 +0000670 return Z_OK;
671}
672
673/* =========================================================================
674 * For the default windowBits of 15 and memLevel of 8, this function returns
675 * a close to exact, as well as small, upper bound on the compressed size.
676 * They are coded as constants here for a reason--if the #define's are
677 * changed, then this function needs to be changed as well. The return
678 * value for 15 and 8 only works for those exact settings.
679 *
680 * For any setting other than those defaults for windowBits and memLevel,
681 * the value returned is a conservative worst case for the maximum expansion
682 * resulting from using fixed blocks instead of stored blocks, which deflate
683 * can emit on compressed data for some combinations of the parameters.
684 *
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000685 * This function could be more sophisticated to provide closer upper bounds for
686 * every combination of windowBits and memLevel. But even the conservative
687 * upper bound of about 14% expansion does not seem onerous for output buffer
688 * allocation.
initial.commit3d533e02008-07-27 00:38:33 +0000689 */
690uLong ZEXPORT deflateBound(strm, sourceLen)
691 z_streamp strm;
692 uLong sourceLen;
693{
694 deflate_state *s;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000695 uLong complen, wraplen;
initial.commit3d533e02008-07-27 00:38:33 +0000696
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000697 /* conservative upper bound for compressed data */
698 complen = sourceLen +
699 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
initial.commit3d533e02008-07-27 00:38:33 +0000700
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000701 /* if can't get parameters, return conservative bound plus zlib wrapper */
mark13dc2462017-02-14 22:15:29 -0800702 if (deflateStateCheck(strm))
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000703 return complen + 6;
704
705 /* compute wrapper length */
706 s = strm->state;
707 switch (s->wrap) {
708 case 0: /* raw deflate */
709 wraplen = 0;
710 break;
711 case 1: /* zlib wrapper */
712 wraplen = 6 + (s->strstart ? 4 : 0);
713 break;
mark13dc2462017-02-14 22:15:29 -0800714#ifdef GZIP
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000715 case 2: /* gzip wrapper */
716 wraplen = 18;
717 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
mark13dc2462017-02-14 22:15:29 -0800718 Bytef *str;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000719 if (s->gzhead->extra != Z_NULL)
720 wraplen += 2 + s->gzhead->extra_len;
721 str = s->gzhead->name;
722 if (str != Z_NULL)
723 do {
724 wraplen++;
725 } while (*str++);
726 str = s->gzhead->comment;
727 if (str != Z_NULL)
728 do {
729 wraplen++;
730 } while (*str++);
731 if (s->gzhead->hcrc)
732 wraplen += 2;
733 }
734 break;
mark13dc2462017-02-14 22:15:29 -0800735#endif
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000736 default: /* for compiler happiness */
737 wraplen = 6;
738 }
initial.commit3d533e02008-07-27 00:38:33 +0000739
740 /* if not default parameters, return conservative bound */
initial.commit3d533e02008-07-27 00:38:33 +0000741 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000742 return complen + wraplen;
initial.commit3d533e02008-07-27 00:38:33 +0000743
744 /* default settings: return tight bound for that case */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +0000745 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
746 (sourceLen >> 25) + 13 - 6 + wraplen;
initial.commit3d533e02008-07-27 00:38:33 +0000747}
748
749/* =========================================================================
750 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
751 * IN assertion: the stream state is correct and there is enough room in
752 * pending_buf.
753 */
754local void putShortMSB (s, b)
755 deflate_state *s;
756 uInt b;
757{
758 put_byte(s, (Byte)(b >> 8));
759 put_byte(s, (Byte)(b & 0xff));
760}
761
762/* =========================================================================
mark13dc2462017-02-14 22:15:29 -0800763 * Flush as much pending output as possible. All deflate() output, except for
764 * some deflate_stored() output, goes through this function so some
765 * applications may wish to modify it to avoid allocating a large
Daniel Bratell2eb38892018-01-11 01:01:17 +0000766 * strm->next_out buffer and copying into it. (See also deflate_read_buf()).
initial.commit3d533e02008-07-27 00:38:33 +0000767 */
768local void flush_pending(strm)
769 z_streamp strm;
770{
jiadong.zhu6c142162016-06-22 21:22:18 -0700771 unsigned len;
772 deflate_state *s = strm->state;
initial.commit3d533e02008-07-27 00:38:33 +0000773
jiadong.zhu6c142162016-06-22 21:22:18 -0700774 _tr_flush_bits(s);
775 len = s->pending;
initial.commit3d533e02008-07-27 00:38:33 +0000776 if (len > strm->avail_out) len = strm->avail_out;
777 if (len == 0) return;
778
jiadong.zhu6c142162016-06-22 21:22:18 -0700779 zmemcpy(strm->next_out, s->pending_out, len);
initial.commit3d533e02008-07-27 00:38:33 +0000780 strm->next_out += len;
jiadong.zhu6c142162016-06-22 21:22:18 -0700781 s->pending_out += len;
initial.commit3d533e02008-07-27 00:38:33 +0000782 strm->total_out += len;
mark13dc2462017-02-14 22:15:29 -0800783 strm->avail_out -= len;
784 s->pending -= len;
jiadong.zhu6c142162016-06-22 21:22:18 -0700785 if (s->pending == 0) {
786 s->pending_out = s->pending_buf;
initial.commit3d533e02008-07-27 00:38:33 +0000787 }
788}
789
mark13dc2462017-02-14 22:15:29 -0800790/* ===========================================================================
791 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
792 */
793#define HCRC_UPDATE(beg) \
794 do { \
795 if (s->gzhead->hcrc && s->pending > (beg)) \
796 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
797 s->pending - (beg)); \
798 } while (0)
799
initial.commit3d533e02008-07-27 00:38:33 +0000800/* ========================================================================= */
801int ZEXPORT deflate (strm, flush)
802 z_streamp strm;
803 int flush;
804{
805 int old_flush; /* value of flush param for previous deflate call */
806 deflate_state *s;
807
mark13dc2462017-02-14 22:15:29 -0800808 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
initial.commit3d533e02008-07-27 00:38:33 +0000809 return Z_STREAM_ERROR;
810 }
811 s = strm->state;
812
813 if (strm->next_out == Z_NULL ||
mark13dc2462017-02-14 22:15:29 -0800814 (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
initial.commit3d533e02008-07-27 00:38:33 +0000815 (s->status == FINISH_STATE && flush != Z_FINISH)) {
816 ERR_RETURN(strm, Z_STREAM_ERROR);
817 }
818 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
819
initial.commit3d533e02008-07-27 00:38:33 +0000820 old_flush = s->last_flush;
821 s->last_flush = flush;
822
initial.commit3d533e02008-07-27 00:38:33 +0000823 /* Flush as much pending output as possible */
824 if (s->pending != 0) {
825 flush_pending(strm);
826 if (strm->avail_out == 0) {
827 /* Since avail_out is 0, deflate will be called again with
828 * more output space, but possibly with both pending and
829 * avail_in equal to zero. There won't be anything to do,
830 * but this is not an error situation so make sure we
831 * return OK instead of BUF_ERROR at next call of deflate:
832 */
833 s->last_flush = -1;
834 return Z_OK;
835 }
836
837 /* Make sure there is something to do and avoid duplicate consecutive
838 * flushes. For repeated and useless calls with Z_FINISH, we keep
839 * returning Z_STREAM_END instead of Z_BUF_ERROR.
840 */
jiadong.zhu6c142162016-06-22 21:22:18 -0700841 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
initial.commit3d533e02008-07-27 00:38:33 +0000842 flush != Z_FINISH) {
843 ERR_RETURN(strm, Z_BUF_ERROR);
844 }
845
846 /* User must not provide more input after the first FINISH: */
847 if (s->status == FINISH_STATE && strm->avail_in != 0) {
848 ERR_RETURN(strm, Z_BUF_ERROR);
849 }
850
mark13dc2462017-02-14 22:15:29 -0800851 /* Write the header */
852 if (s->status == INIT_STATE) {
853 /* zlib header */
854 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
855 uInt level_flags;
856
857 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
858 level_flags = 0;
859 else if (s->level < 6)
860 level_flags = 1;
861 else if (s->level == 6)
862 level_flags = 2;
863 else
864 level_flags = 3;
865 header |= (level_flags << 6);
866 if (s->strstart != 0) header |= PRESET_DICT;
867 header += 31 - (header % 31);
868
869 putShortMSB(s, header);
870
871 /* Save the adler32 of the preset dictionary: */
872 if (s->strstart != 0) {
873 putShortMSB(s, (uInt)(strm->adler >> 16));
874 putShortMSB(s, (uInt)(strm->adler & 0xffff));
875 }
876 strm->adler = adler32(0L, Z_NULL, 0);
877 s->status = BUSY_STATE;
878
879 /* Compression must start with an empty pending buffer */
880 flush_pending(strm);
881 if (s->pending != 0) {
882 s->last_flush = -1;
883 return Z_OK;
884 }
885 }
886#ifdef GZIP
887 if (s->status == GZIP_STATE) {
888 /* gzip header */
889 crc_reset(s);
890 put_byte(s, 31);
891 put_byte(s, 139);
892 put_byte(s, 8);
893 if (s->gzhead == Z_NULL) {
894 put_byte(s, 0);
895 put_byte(s, 0);
896 put_byte(s, 0);
897 put_byte(s, 0);
898 put_byte(s, 0);
899 put_byte(s, s->level == 9 ? 2 :
900 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
901 4 : 0));
902 put_byte(s, OS_CODE);
903 s->status = BUSY_STATE;
904
905 /* Compression must start with an empty pending buffer */
906 flush_pending(strm);
907 if (s->pending != 0) {
908 s->last_flush = -1;
909 return Z_OK;
910 }
911 }
912 else {
913 put_byte(s, (s->gzhead->text ? 1 : 0) +
914 (s->gzhead->hcrc ? 2 : 0) +
915 (s->gzhead->extra == Z_NULL ? 0 : 4) +
916 (s->gzhead->name == Z_NULL ? 0 : 8) +
917 (s->gzhead->comment == Z_NULL ? 0 : 16)
918 );
919 put_byte(s, (Byte)(s->gzhead->time & 0xff));
920 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
921 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
922 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
923 put_byte(s, s->level == 9 ? 2 :
924 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
925 4 : 0));
926 put_byte(s, s->gzhead->os & 0xff);
927 if (s->gzhead->extra != Z_NULL) {
928 put_byte(s, s->gzhead->extra_len & 0xff);
929 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
930 }
931 if (s->gzhead->hcrc)
932 strm->adler = crc32(strm->adler, s->pending_buf,
933 s->pending);
934 s->gzindex = 0;
935 s->status = EXTRA_STATE;
936 }
937 }
938 if (s->status == EXTRA_STATE) {
939 if (s->gzhead->extra != Z_NULL) {
940 ulg beg = s->pending; /* start of bytes to update crc */
941 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
942 while (s->pending + left > s->pending_buf_size) {
943 uInt copy = s->pending_buf_size - s->pending;
944 zmemcpy(s->pending_buf + s->pending,
945 s->gzhead->extra + s->gzindex, copy);
946 s->pending = s->pending_buf_size;
947 HCRC_UPDATE(beg);
948 s->gzindex += copy;
949 flush_pending(strm);
950 if (s->pending != 0) {
951 s->last_flush = -1;
952 return Z_OK;
953 }
954 beg = 0;
955 left -= copy;
956 }
957 zmemcpy(s->pending_buf + s->pending,
958 s->gzhead->extra + s->gzindex, left);
959 s->pending += left;
960 HCRC_UPDATE(beg);
961 s->gzindex = 0;
962 }
963 s->status = NAME_STATE;
964 }
965 if (s->status == NAME_STATE) {
966 if (s->gzhead->name != Z_NULL) {
967 ulg beg = s->pending; /* start of bytes to update crc */
968 int val;
969 do {
970 if (s->pending == s->pending_buf_size) {
971 HCRC_UPDATE(beg);
972 flush_pending(strm);
973 if (s->pending != 0) {
974 s->last_flush = -1;
975 return Z_OK;
976 }
977 beg = 0;
978 }
979 val = s->gzhead->name[s->gzindex++];
980 put_byte(s, val);
981 } while (val != 0);
982 HCRC_UPDATE(beg);
983 s->gzindex = 0;
984 }
985 s->status = COMMENT_STATE;
986 }
987 if (s->status == COMMENT_STATE) {
988 if (s->gzhead->comment != Z_NULL) {
989 ulg beg = s->pending; /* start of bytes to update crc */
990 int val;
991 do {
992 if (s->pending == s->pending_buf_size) {
993 HCRC_UPDATE(beg);
994 flush_pending(strm);
995 if (s->pending != 0) {
996 s->last_flush = -1;
997 return Z_OK;
998 }
999 beg = 0;
1000 }
1001 val = s->gzhead->comment[s->gzindex++];
1002 put_byte(s, val);
1003 } while (val != 0);
1004 HCRC_UPDATE(beg);
1005 }
1006 s->status = HCRC_STATE;
1007 }
1008 if (s->status == HCRC_STATE) {
1009 if (s->gzhead->hcrc) {
1010 if (s->pending + 2 > s->pending_buf_size) {
1011 flush_pending(strm);
1012 if (s->pending != 0) {
1013 s->last_flush = -1;
1014 return Z_OK;
1015 }
1016 }
1017 put_byte(s, (Byte)(strm->adler & 0xff));
1018 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1019 strm->adler = crc32(0L, Z_NULL, 0);
1020 }
1021 s->status = BUSY_STATE;
1022
1023 /* Compression must start with an empty pending buffer */
1024 flush_pending(strm);
1025 if (s->pending != 0) {
1026 s->last_flush = -1;
1027 return Z_OK;
1028 }
1029 }
1030#endif
1031
initial.commit3d533e02008-07-27 00:38:33 +00001032 /* Start a new block or continue the current one.
1033 */
1034 if (strm->avail_in != 0 || s->lookahead != 0 ||
1035 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1036 block_state bstate;
1037
mark13dc2462017-02-14 22:15:29 -08001038 bstate = s->level == 0 ? deflate_stored(s, flush) :
1039 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1040 s->strategy == Z_RLE ? deflate_rle(s, flush) :
1041 (*(configuration_table[s->level].func))(s, flush);
initial.commit3d533e02008-07-27 00:38:33 +00001042
1043 if (bstate == finish_started || bstate == finish_done) {
1044 s->status = FINISH_STATE;
1045 }
1046 if (bstate == need_more || bstate == finish_started) {
1047 if (strm->avail_out == 0) {
1048 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1049 }
1050 return Z_OK;
1051 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1052 * of deflate should use the same flush parameter to make sure
1053 * that the flush is complete. So we don't have to output an
1054 * empty block here, this will be done at next call. This also
1055 * ensures that for a very small output buffer, we emit at most
1056 * one empty block.
1057 */
1058 }
1059 if (bstate == block_done) {
1060 if (flush == Z_PARTIAL_FLUSH) {
1061 _tr_align(s);
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001062 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
initial.commit3d533e02008-07-27 00:38:33 +00001063 _tr_stored_block(s, (char*)0, 0L, 0);
1064 /* For a full flush, this empty block will be recognized
1065 * as a special marker by inflate_sync().
1066 */
1067 if (flush == Z_FULL_FLUSH) {
1068 CLEAR_HASH(s); /* forget history */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001069 if (s->lookahead == 0) {
1070 s->strstart = 0;
1071 s->block_start = 0L;
jiadong.zhu6c142162016-06-22 21:22:18 -07001072 s->insert = 0;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001073 }
initial.commit3d533e02008-07-27 00:38:33 +00001074 }
1075 }
1076 flush_pending(strm);
1077 if (strm->avail_out == 0) {
1078 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1079 return Z_OK;
1080 }
1081 }
1082 }
initial.commit3d533e02008-07-27 00:38:33 +00001083
1084 if (flush != Z_FINISH) return Z_OK;
1085 if (s->wrap <= 0) return Z_STREAM_END;
1086
1087 /* Write the trailer */
1088#ifdef GZIP
1089 if (s->wrap == 2) {
robert.bradford10dd6862014-11-05 06:59:34 -08001090 crc_finalize(s);
initial.commit3d533e02008-07-27 00:38:33 +00001091 put_byte(s, (Byte)(strm->adler & 0xff));
1092 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1093 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1094 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1095 put_byte(s, (Byte)(strm->total_in & 0xff));
1096 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1097 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1098 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1099 }
1100 else
1101#endif
1102 {
1103 putShortMSB(s, (uInt)(strm->adler >> 16));
1104 putShortMSB(s, (uInt)(strm->adler & 0xffff));
1105 }
1106 flush_pending(strm);
1107 /* If avail_out is zero, the application will call deflate again
1108 * to flush the rest.
1109 */
1110 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1111 return s->pending != 0 ? Z_OK : Z_STREAM_END;
1112}
1113
1114/* ========================================================================= */
1115int ZEXPORT deflateEnd (strm)
1116 z_streamp strm;
1117{
1118 int status;
1119
mark13dc2462017-02-14 22:15:29 -08001120 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
initial.commit3d533e02008-07-27 00:38:33 +00001121
1122 status = strm->state->status;
initial.commit3d533e02008-07-27 00:38:33 +00001123
1124 /* Deallocate in reverse order of allocations: */
1125 TRY_FREE(strm, strm->state->pending_buf);
1126 TRY_FREE(strm, strm->state->head);
1127 TRY_FREE(strm, strm->state->prev);
1128 TRY_FREE(strm, strm->state->window);
1129
1130 ZFREE(strm, strm->state);
1131 strm->state = Z_NULL;
1132
1133 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1134}
1135
1136/* =========================================================================
1137 * Copy the source state to the destination state.
1138 * To simplify the source, this is not supported for 16-bit MSDOS (which
1139 * doesn't have enough memory anyway to duplicate compression states).
1140 */
1141int ZEXPORT deflateCopy (dest, source)
1142 z_streamp dest;
1143 z_streamp source;
1144{
1145#ifdef MAXSEG_64K
1146 return Z_STREAM_ERROR;
1147#else
1148 deflate_state *ds;
1149 deflate_state *ss;
initial.commit3d533e02008-07-27 00:38:33 +00001150
1151
mark13dc2462017-02-14 22:15:29 -08001152 if (deflateStateCheck(source) || dest == Z_NULL) {
initial.commit3d533e02008-07-27 00:38:33 +00001153 return Z_STREAM_ERROR;
1154 }
1155
1156 ss = source->state;
1157
jiadong.zhu6c142162016-06-22 21:22:18 -07001158 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
initial.commit3d533e02008-07-27 00:38:33 +00001159
1160 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1161 if (ds == Z_NULL) return Z_MEM_ERROR;
1162 dest->state = (struct internal_state FAR *) ds;
jiadong.zhu6c142162016-06-22 21:22:18 -07001163 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
initial.commit3d533e02008-07-27 00:38:33 +00001164 ds->strm = dest;
1165
1166 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1167 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1168 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
Chris Blume28ea0a92018-08-17 06:16:32 +00001169 ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
initial.commit3d533e02008-07-27 00:38:33 +00001170
1171 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1172 ds->pending_buf == Z_NULL) {
1173 deflateEnd (dest);
1174 return Z_MEM_ERROR;
1175 }
1176 /* following zmemcpy do not work for 16-bit MSDOS */
1177 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
jiadong.zhu6c142162016-06-22 21:22:18 -07001178 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1179 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
initial.commit3d533e02008-07-27 00:38:33 +00001180 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1181
1182 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
Chris Blume28ea0a92018-08-17 06:16:32 +00001183 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
initial.commit3d533e02008-07-27 00:38:33 +00001184
1185 ds->l_desc.dyn_tree = ds->dyn_ltree;
1186 ds->d_desc.dyn_tree = ds->dyn_dtree;
1187 ds->bl_desc.dyn_tree = ds->bl_tree;
1188
1189 return Z_OK;
1190#endif /* MAXSEG_64K */
1191}
1192
1193/* ===========================================================================
1194 * Read a new buffer from the current input stream, update the adler32
1195 * and total number of bytes read. All deflate() input goes through
1196 * this function so some applications may wish to modify it to avoid
1197 * allocating a large strm->next_in buffer and copying from it.
1198 * (See also flush_pending()).
1199 */
Daniel Bratell2eb38892018-01-11 01:01:17 +00001200ZLIB_INTERNAL unsigned deflate_read_buf(strm, buf, size)
initial.commit3d533e02008-07-27 00:38:33 +00001201 z_streamp strm;
1202 Bytef *buf;
1203 unsigned size;
1204{
1205 unsigned len = strm->avail_in;
1206
1207 if (len > size) len = size;
1208 if (len == 0) return 0;
1209
1210 strm->avail_in -= len;
1211
initial.commit3d533e02008-07-27 00:38:33 +00001212#ifdef GZIP
jiadong.zhu6c142162016-06-22 21:22:18 -07001213 if (strm->state->wrap == 2)
robert.bradford10dd6862014-11-05 06:59:34 -08001214 copy_with_crc(strm, buf, len);
Noel Gordon3e0ea902020-04-23 13:04:21 +00001215 else
initial.commit3d533e02008-07-27 00:38:33 +00001216#endif
robert.bradford10dd6862014-11-05 06:59:34 -08001217 {
1218 zmemcpy(buf, strm->next_in, len);
1219 if (strm->state->wrap == 1)
1220 strm->adler = adler32(strm->adler, buf, len);
1221 }
initial.commit3d533e02008-07-27 00:38:33 +00001222 strm->next_in += len;
1223 strm->total_in += len;
1224
mark13dc2462017-02-14 22:15:29 -08001225 return len;
initial.commit3d533e02008-07-27 00:38:33 +00001226}
1227
1228/* ===========================================================================
1229 * Initialize the "longest match" routines for a new zlib stream
1230 */
1231local void lm_init (s)
1232 deflate_state *s;
1233{
1234 s->window_size = (ulg)2L*s->w_size;
1235
1236 CLEAR_HASH(s);
1237
1238 /* Set the default configuration parameters:
1239 */
1240 s->max_lazy_match = configuration_table[s->level].max_lazy;
1241 s->good_match = configuration_table[s->level].good_length;
1242 s->nice_match = configuration_table[s->level].nice_length;
1243 s->max_chain_length = configuration_table[s->level].max_chain;
1244
1245 s->strstart = 0;
1246 s->block_start = 0L;
1247 s->lookahead = 0;
jiadong.zhu6c142162016-06-22 21:22:18 -07001248 s->insert = 0;
initial.commit3d533e02008-07-27 00:38:33 +00001249 s->match_length = s->prev_length = MIN_MATCH-1;
1250 s->match_available = 0;
1251 s->ins_h = 0;
1252#ifndef FASTEST
1253#ifdef ASMV
1254 match_init(); /* initialize the asm code */
1255#endif
1256#endif
1257}
1258
1259#ifndef FASTEST
1260/* ===========================================================================
1261 * Set match_start to the longest match starting at the given string and
1262 * return its length. Matches shorter or equal to prev_length are discarded,
1263 * in which case the result is equal to prev_length and match_start is
1264 * garbage.
1265 * IN assertions: cur_match is the head of the hash chain for the current
1266 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1267 * OUT assertion: the match length is not greater than s->lookahead.
1268 */
1269#ifndef ASMV
1270/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1271 * match.S. The code will be functionally equivalent.
1272 */
davidben709ed022017-02-02 13:58:58 -08001273local uInt longest_match(s, cur_match)
initial.commit3d533e02008-07-27 00:38:33 +00001274 deflate_state *s;
1275 IPos cur_match; /* current match */
1276{
1277 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1278 register Bytef *scan = s->window + s->strstart; /* current string */
mark13dc2462017-02-14 22:15:29 -08001279 register Bytef *match; /* matched string */
initial.commit3d533e02008-07-27 00:38:33 +00001280 register int len; /* length of current match */
mark13dc2462017-02-14 22:15:29 -08001281 int best_len = (int)s->prev_length; /* best match length so far */
initial.commit3d533e02008-07-27 00:38:33 +00001282 int nice_match = s->nice_match; /* stop if match long enough */
1283 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1284 s->strstart - (IPos)MAX_DIST(s) : NIL;
1285 /* Stop when cur_match becomes <= limit. To simplify the code,
1286 * we prevent matches with the string of window index 0.
1287 */
1288 Posf *prev = s->prev;
1289 uInt wmask = s->w_mask;
1290
1291#ifdef UNALIGNED_OK
1292 /* Compare two bytes at a time. Note: this is not always beneficial.
1293 * Try with and without -DUNALIGNED_OK to check.
1294 */
1295 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1296 register ush scan_start = *(ushf*)scan;
1297 register ush scan_end = *(ushf*)(scan+best_len-1);
1298#else
1299 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1300 register Byte scan_end1 = scan[best_len-1];
1301 register Byte scan_end = scan[best_len];
1302#endif
1303
1304 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1305 * It is easy to get rid of this optimization if necessary.
1306 */
1307 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1308
1309 /* Do not waste too much time if we already have a good match: */
1310 if (s->prev_length >= s->good_match) {
1311 chain_length >>= 2;
1312 }
1313 /* Do not look for matches beyond the end of the input. This is necessary
1314 * to make deflate deterministic.
1315 */
mark13dc2462017-02-14 22:15:29 -08001316 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
initial.commit3d533e02008-07-27 00:38:33 +00001317
1318 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1319
1320 do {
1321 Assert(cur_match < s->strstart, "no future");
1322 match = s->window + cur_match;
1323
1324 /* Skip to next match if the match length cannot increase
1325 * or if the match length is less than 2. Note that the checks below
1326 * for insufficient lookahead only occur occasionally for performance
1327 * reasons. Therefore uninitialized memory will be accessed, and
1328 * conditional jumps will be made that depend on those values.
1329 * However the length of the match is limited to the lookahead, so
1330 * the output of deflate is not affected by the uninitialized values.
1331 */
1332#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1333 /* This code assumes sizeof(unsigned short) == 2. Do not use
1334 * UNALIGNED_OK if your compiler uses a different size.
1335 */
1336 if (*(ushf*)(match+best_len-1) != scan_end ||
1337 *(ushf*)match != scan_start) continue;
1338
1339 /* It is not necessary to compare scan[2] and match[2] since they are
1340 * always equal when the other bytes match, given that the hash keys
1341 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1342 * strstart+3, +5, ... up to strstart+257. We check for insufficient
1343 * lookahead only every 4th comparison; the 128th check will be made
1344 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1345 * necessary to put more guard bytes at the end of the window, or
1346 * to check more often for insufficient lookahead.
1347 */
1348 Assert(scan[2] == match[2], "scan[2]?");
1349 scan++, match++;
1350 do {
1351 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1352 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1353 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1354 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1355 scan < strend);
1356 /* The funny "do {}" generates better code on most compilers */
1357
1358 /* Here, scan <= window+strstart+257 */
1359 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1360 if (*scan == *match) scan++;
1361
1362 len = (MAX_MATCH - 1) - (int)(strend-scan);
1363 scan = strend - (MAX_MATCH-1);
1364
1365#else /* UNALIGNED_OK */
1366
1367 if (match[best_len] != scan_end ||
1368 match[best_len-1] != scan_end1 ||
1369 *match != *scan ||
1370 *++match != scan[1]) continue;
1371
1372 /* The check at best_len-1 can be removed because it will be made
1373 * again later. (This heuristic is not always a win.)
1374 * It is not necessary to compare scan[2] and match[2] since they
1375 * are always equal when the other bytes match, given that
1376 * the hash keys are equal and that HASH_BITS >= 8.
1377 */
1378 scan += 2, match++;
1379 Assert(*scan == *match, "match[2]?");
1380
davidben709ed022017-02-02 13:58:58 -08001381 /* We check for insufficient lookahead only every 8th comparison;
1382 * the 256th check will be made at strstart+258.
1383 */
1384 do {
1385 } while (*++scan == *++match && *++scan == *++match &&
1386 *++scan == *++match && *++scan == *++match &&
1387 *++scan == *++match && *++scan == *++match &&
1388 *++scan == *++match && *++scan == *++match &&
1389 scan < strend);
initial.commit3d533e02008-07-27 00:38:33 +00001390
1391 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1392
1393 len = MAX_MATCH - (int)(strend - scan);
1394 scan = strend - MAX_MATCH;
1395
1396#endif /* UNALIGNED_OK */
1397
1398 if (len > best_len) {
1399 s->match_start = cur_match;
1400 best_len = len;
1401 if (len >= nice_match) break;
1402#ifdef UNALIGNED_OK
1403 scan_end = *(ushf*)(scan+best_len-1);
1404#else
1405 scan_end1 = scan[best_len-1];
1406 scan_end = scan[best_len];
1407#endif
1408 }
1409 } while ((cur_match = prev[cur_match & wmask]) > limit
1410 && --chain_length != 0);
1411
1412 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1413 return s->lookahead;
1414}
1415#endif /* ASMV */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001416
1417#else /* FASTEST */
initial.commit3d533e02008-07-27 00:38:33 +00001418
1419/* ---------------------------------------------------------------------------
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001420 * Optimized version for FASTEST only
initial.commit3d533e02008-07-27 00:38:33 +00001421 */
davidben709ed022017-02-02 13:58:58 -08001422local uInt longest_match(s, cur_match)
initial.commit3d533e02008-07-27 00:38:33 +00001423 deflate_state *s;
1424 IPos cur_match; /* current match */
1425{
1426 register Bytef *scan = s->window + s->strstart; /* current string */
1427 register Bytef *match; /* matched string */
1428 register int len; /* length of current match */
1429 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1430
1431 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1432 * It is easy to get rid of this optimization if necessary.
1433 */
1434 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1435
1436 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1437
1438 Assert(cur_match < s->strstart, "no future");
1439
1440 match = s->window + cur_match;
1441
1442 /* Return failure if the match length is less than 2:
1443 */
1444 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1445
1446 /* The check at best_len-1 can be removed because it will be made
1447 * again later. (This heuristic is not always a win.)
1448 * It is not necessary to compare scan[2] and match[2] since they
1449 * are always equal when the other bytes match, given that
1450 * the hash keys are equal and that HASH_BITS >= 8.
1451 */
1452 scan += 2, match += 2;
1453 Assert(*scan == *match, "match[2]?");
1454
1455 /* We check for insufficient lookahead only every 8th comparison;
1456 * the 256th check will be made at strstart+258.
1457 */
1458 do {
1459 } while (*++scan == *++match && *++scan == *++match &&
1460 *++scan == *++match && *++scan == *++match &&
1461 *++scan == *++match && *++scan == *++match &&
1462 *++scan == *++match && *++scan == *++match &&
1463 scan < strend);
1464
1465 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1466
1467 len = MAX_MATCH - (int)(strend - scan);
1468
1469 if (len < MIN_MATCH) return MIN_MATCH - 1;
1470
1471 s->match_start = cur_match;
1472 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1473}
1474
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001475#endif /* FASTEST */
1476
mark13dc2462017-02-14 22:15:29 -08001477#ifdef ZLIB_DEBUG
1478
1479#define EQUAL 0
1480/* result of memcmp for equal strings */
1481
initial.commit3d533e02008-07-27 00:38:33 +00001482/* ===========================================================================
1483 * Check that the match at match_start is indeed a match.
1484 */
1485local void check_match(s, start, match, length)
1486 deflate_state *s;
1487 IPos start, match;
1488 int length;
1489{
1490 /* check that the match is indeed a match */
1491 if (zmemcmp(s->window + match,
1492 s->window + start, length) != EQUAL) {
1493 fprintf(stderr, " start %u, match %u, length %d\n",
1494 start, match, length);
1495 do {
1496 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1497 } while (--length != 0);
1498 z_error("invalid match");
1499 }
1500 if (z_verbose > 1) {
1501 fprintf(stderr,"\\[%d,%d]", start-match, length);
1502 do { putc(s->window[start++], stderr); } while (--length != 0);
1503 }
1504}
1505#else
1506# define check_match(s, start, match, length)
mark13dc2462017-02-14 22:15:29 -08001507#endif /* ZLIB_DEBUG */
initial.commit3d533e02008-07-27 00:38:33 +00001508
1509/* ===========================================================================
1510 * Fill the window when the lookahead becomes insufficient.
1511 * Updates strstart and lookahead.
1512 *
1513 * IN assertion: lookahead < MIN_LOOKAHEAD
1514 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1515 * At least one byte has been read, or avail_in == 0; reads are
1516 * performed for at least two bytes (required for the zip translate_eol
1517 * option -- not supported here).
1518 */
robert.bradford10dd6862014-11-05 06:59:34 -08001519local void fill_window_c(deflate_state *s);
1520
1521local void fill_window(deflate_state *s)
1522{
Noel Gordonf36fe822020-04-22 02:50:27 +00001523#ifdef DEFLATE_FILL_WINDOW_SSE2
Noel Gordon3e0ea902020-04-23 13:04:21 +00001524 if (x86_cpu_enable_simd) {
robert.bradford10dd6862014-11-05 06:59:34 -08001525 fill_window_sse(s);
1526 return;
1527 }
Adenilson Cavalcanti5de00af2020-01-08 22:12:31 +00001528#endif
robert.bradford10dd6862014-11-05 06:59:34 -08001529 fill_window_c(s);
1530}
1531
1532local void fill_window_c(s)
initial.commit3d533e02008-07-27 00:38:33 +00001533 deflate_state *s;
1534{
mark13dc2462017-02-14 22:15:29 -08001535 unsigned n;
initial.commit3d533e02008-07-27 00:38:33 +00001536 unsigned more; /* Amount of free space at the end of the window. */
1537 uInt wsize = s->w_size;
1538
jiadong.zhu6c142162016-06-22 21:22:18 -07001539 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1540
initial.commit3d533e02008-07-27 00:38:33 +00001541 do {
1542 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1543
1544 /* Deal with !@#$% 64K limit: */
1545 if (sizeof(int) <= 2) {
1546 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1547 more = wsize;
1548
1549 } else if (more == (unsigned)(-1)) {
1550 /* Very unlikely, but possible on 16 bit machine if
1551 * strstart == 0 && lookahead == 1 (input done a byte at time)
1552 */
1553 more--;
1554 }
1555 }
1556
1557 /* If the window is almost full and there is insufficient lookahead,
1558 * move the upper half to the lower one to make room in the upper half.
1559 */
1560 if (s->strstart >= wsize+MAX_DIST(s)) {
1561
mark13dc2462017-02-14 22:15:29 -08001562 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
initial.commit3d533e02008-07-27 00:38:33 +00001563 s->match_start -= wsize;
1564 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1565 s->block_start -= (long) wsize;
mark13dc2462017-02-14 22:15:29 -08001566 slide_hash(s);
initial.commit3d533e02008-07-27 00:38:33 +00001567 more += wsize;
1568 }
jiadong.zhu6c142162016-06-22 21:22:18 -07001569 if (s->strm->avail_in == 0) break;
initial.commit3d533e02008-07-27 00:38:33 +00001570
1571 /* If there was no sliding:
1572 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1573 * more == window_size - lookahead - strstart
1574 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1575 * => more >= window_size - 2*WSIZE + 2
1576 * In the BIG_MEM or MMAP case (not yet supported),
1577 * window_size == input_size + MIN_LOOKAHEAD &&
1578 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1579 * Otherwise, window_size == 2*WSIZE so more >= 2.
1580 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1581 */
1582 Assert(more >= 2, "more < 2");
1583
Daniel Bratell2eb38892018-01-11 01:01:17 +00001584 n = deflate_read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
initial.commit3d533e02008-07-27 00:38:33 +00001585 s->lookahead += n;
1586
1587 /* Initialize the hash value now that we have some input: */
jiadong.zhu6c142162016-06-22 21:22:18 -07001588 if (s->lookahead + s->insert >= MIN_MATCH) {
1589 uInt str = s->strstart - s->insert;
1590 s->ins_h = s->window[str];
1591 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
initial.commit3d533e02008-07-27 00:38:33 +00001592#if MIN_MATCH != 3
1593 Call UPDATE_HASH() MIN_MATCH-3 more times
1594#endif
jiadong.zhu6c142162016-06-22 21:22:18 -07001595 while (s->insert) {
1596 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1597#ifndef FASTEST
1598 s->prev[str & s->w_mask] = s->head[s->ins_h];
1599#endif
1600 s->head[s->ins_h] = (Pos)str;
1601 str++;
1602 s->insert--;
1603 if (s->lookahead + s->insert < MIN_MATCH)
1604 break;
1605 }
initial.commit3d533e02008-07-27 00:38:33 +00001606 }
1607 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1608 * but this is not important since only literal bytes will be emitted.
1609 */
1610
1611 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001612
1613 /* If the WIN_INIT bytes after the end of the current data have never been
1614 * written, then zero those bytes in order to avoid memory check reports of
1615 * the use of uninitialized (or uninitialised as Julian writes) bytes by
1616 * the longest match routines. Update the high water mark for the next
1617 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1618 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1619 */
1620 if (s->high_water < s->window_size) {
1621 ulg curr = s->strstart + (ulg)(s->lookahead);
1622 ulg init;
1623
1624 if (s->high_water < curr) {
1625 /* Previous high water mark below current data -- zero WIN_INIT
1626 * bytes or up to end of window, whichever is less.
1627 */
1628 init = s->window_size - curr;
1629 if (init > WIN_INIT)
1630 init = WIN_INIT;
1631 zmemzero(s->window + curr, (unsigned)init);
1632 s->high_water = curr + init;
1633 }
1634 else if (s->high_water < (ulg)curr + WIN_INIT) {
1635 /* High water mark at or above current data, but below current data
1636 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1637 * to end of window, whichever is less.
1638 */
1639 init = (ulg)curr + WIN_INIT - s->high_water;
1640 if (init > s->window_size - s->high_water)
1641 init = s->window_size - s->high_water;
1642 zmemzero(s->window + s->high_water, (unsigned)init);
1643 s->high_water += init;
1644 }
1645 }
jiadong.zhu6c142162016-06-22 21:22:18 -07001646
1647 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1648 "not enough room for search");
initial.commit3d533e02008-07-27 00:38:33 +00001649}
1650
1651/* ===========================================================================
1652 * Flush the current block, with given end-of-file flag.
1653 * IN assertion: strstart is set to the end of the current match.
1654 */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001655#define FLUSH_BLOCK_ONLY(s, last) { \
initial.commit3d533e02008-07-27 00:38:33 +00001656 _tr_flush_block(s, (s->block_start >= 0L ? \
1657 (charf *)&s->window[(unsigned)s->block_start] : \
1658 (charf *)Z_NULL), \
1659 (ulg)((long)s->strstart - s->block_start), \
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001660 (last)); \
initial.commit3d533e02008-07-27 00:38:33 +00001661 s->block_start = s->strstart; \
1662 flush_pending(s->strm); \
1663 Tracev((stderr,"[FLUSH]")); \
1664}
1665
1666/* Same but force premature exit if necessary. */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001667#define FLUSH_BLOCK(s, last) { \
1668 FLUSH_BLOCK_ONLY(s, last); \
1669 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
initial.commit3d533e02008-07-27 00:38:33 +00001670}
1671
mark13dc2462017-02-14 22:15:29 -08001672/* Maximum stored block length in deflate format (not including header). */
1673#define MAX_STORED 65535
1674
1675/* Minimum of a and b. */
1676#define MIN(a, b) ((a) > (b) ? (b) : (a))
1677
initial.commit3d533e02008-07-27 00:38:33 +00001678/* ===========================================================================
1679 * Copy without compression as much as possible from the input stream, return
1680 * the current block state.
mark13dc2462017-02-14 22:15:29 -08001681 *
1682 * In case deflateParams() is used to later switch to a non-zero compression
1683 * level, s->matches (otherwise unused when storing) keeps track of the number
1684 * of hash table slides to perform. If s->matches is 1, then one hash table
1685 * slide will be done when switching. If s->matches is 2, the maximum value
1686 * allowed here, then the hash table will be cleared, since two or more slides
1687 * is the same as a clear.
1688 *
1689 * deflate_stored() is written to minimize the number of times an input byte is
1690 * copied. It is most efficient with large input and output buffers, which
1691 * maximizes the opportunites to have a single copy from next_in to next_out.
initial.commit3d533e02008-07-27 00:38:33 +00001692 */
davidben709ed022017-02-02 13:58:58 -08001693local block_state deflate_stored(s, flush)
initial.commit3d533e02008-07-27 00:38:33 +00001694 deflate_state *s;
1695 int flush;
1696{
mark13dc2462017-02-14 22:15:29 -08001697 /* Smallest worthy block size when not flushing or finishing. By default
1698 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1699 * large input and output buffers, the stored block size will be larger.
initial.commit3d533e02008-07-27 00:38:33 +00001700 */
mark13dc2462017-02-14 22:15:29 -08001701 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
initial.commit3d533e02008-07-27 00:38:33 +00001702
mark13dc2462017-02-14 22:15:29 -08001703 /* Copy as many min_block or larger stored blocks directly to next_out as
1704 * possible. If flushing, copy the remaining available input to next_out as
1705 * stored blocks, if there is enough space.
1706 */
1707 unsigned len, left, have, last = 0;
1708 unsigned used = s->strm->avail_in;
1709 do {
1710 /* Set len to the maximum size block that we can copy directly with the
1711 * available input data and output space. Set left to how much of that
1712 * would be copied from what's left in the window.
initial.commit3d533e02008-07-27 00:38:33 +00001713 */
mark13dc2462017-02-14 22:15:29 -08001714 len = MAX_STORED; /* maximum deflate stored block length */
1715 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1716 if (s->strm->avail_out < have) /* need room for header */
1717 break;
1718 /* maximum stored block length that will fit in avail_out: */
1719 have = s->strm->avail_out - have;
1720 left = s->strstart - s->block_start; /* bytes left in window */
1721 if (len > (ulg)left + s->strm->avail_in)
1722 len = left + s->strm->avail_in; /* limit len to the input */
1723 if (len > have)
1724 len = have; /* limit len to the output */
1725
1726 /* If the stored block would be less than min_block in length, or if
1727 * unable to copy all of the available input when flushing, then try
1728 * copying to the window and the pending buffer instead. Also don't
1729 * write an empty block when flushing -- deflate() does that.
1730 */
1731 if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1732 flush == Z_NO_FLUSH ||
1733 len != left + s->strm->avail_in))
1734 break;
1735
1736 /* Make a dummy stored block in pending to get the header bytes,
1737 * including any pending bits. This also updates the debugging counts.
1738 */
1739 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1740 _tr_stored_block(s, (char *)0, 0L, last);
1741
1742 /* Replace the lengths in the dummy stored block with len. */
1743 s->pending_buf[s->pending - 4] = len;
1744 s->pending_buf[s->pending - 3] = len >> 8;
1745 s->pending_buf[s->pending - 2] = ~len;
1746 s->pending_buf[s->pending - 1] = ~len >> 8;
1747
1748 /* Write the stored block header bytes. */
1749 flush_pending(s->strm);
1750
1751#ifdef ZLIB_DEBUG
1752 /* Update debugging counts for the data about to be copied. */
1753 s->compressed_len += len << 3;
1754 s->bits_sent += len << 3;
1755#endif
1756
1757 /* Copy uncompressed bytes from the window to next_out. */
1758 if (left) {
1759 if (left > len)
1760 left = len;
1761 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1762 s->strm->next_out += left;
1763 s->strm->avail_out -= left;
1764 s->strm->total_out += left;
1765 s->block_start += left;
1766 len -= left;
initial.commit3d533e02008-07-27 00:38:33 +00001767 }
mark13dc2462017-02-14 22:15:29 -08001768
1769 /* Copy uncompressed bytes directly from next_in to next_out, updating
1770 * the check value.
1771 */
1772 if (len) {
Daniel Bratell2eb38892018-01-11 01:01:17 +00001773 deflate_read_buf(s->strm, s->strm->next_out, len);
mark13dc2462017-02-14 22:15:29 -08001774 s->strm->next_out += len;
1775 s->strm->avail_out -= len;
1776 s->strm->total_out += len;
1777 }
1778 } while (last == 0);
1779
1780 /* Update the sliding window with the last s->w_size bytes of the copied
1781 * data, or append all of the copied data to the existing window if less
1782 * than s->w_size bytes were copied. Also update the number of bytes to
1783 * insert in the hash tables, in the event that deflateParams() switches to
1784 * a non-zero compression level.
1785 */
1786 used -= s->strm->avail_in; /* number of input bytes directly copied */
1787 if (used) {
1788 /* If any input was used, then no unused input remains in the window,
1789 * therefore s->block_start == s->strstart.
1790 */
1791 if (used >= s->w_size) { /* supplant the previous history */
1792 s->matches = 2; /* clear hash */
1793 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1794 s->strstart = s->w_size;
1795 }
1796 else {
1797 if (s->window_size - s->strstart <= used) {
1798 /* Slide the window down. */
1799 s->strstart -= s->w_size;
1800 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1801 if (s->matches < 2)
1802 s->matches++; /* add a pending slide_hash() */
1803 }
1804 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1805 s->strstart += used;
1806 }
1807 s->block_start = s->strstart;
1808 s->insert += MIN(used, s->w_size - s->insert);
initial.commit3d533e02008-07-27 00:38:33 +00001809 }
mark13dc2462017-02-14 22:15:29 -08001810 if (s->high_water < s->strstart)
1811 s->high_water = s->strstart;
1812
1813 /* If the last block was written to next_out, then done. */
1814 if (last)
jiadong.zhu6c142162016-06-22 21:22:18 -07001815 return finish_done;
mark13dc2462017-02-14 22:15:29 -08001816
1817 /* If flushing and all input has been consumed, then done. */
1818 if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1819 s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1820 return block_done;
1821
1822 /* Fill the window with any remaining input. */
1823 have = s->window_size - s->strstart - 1;
1824 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1825 /* Slide the window down. */
1826 s->block_start -= s->w_size;
1827 s->strstart -= s->w_size;
1828 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1829 if (s->matches < 2)
1830 s->matches++; /* add a pending slide_hash() */
1831 have += s->w_size; /* more space now */
jiadong.zhu6c142162016-06-22 21:22:18 -07001832 }
mark13dc2462017-02-14 22:15:29 -08001833 if (have > s->strm->avail_in)
1834 have = s->strm->avail_in;
1835 if (have) {
Daniel Bratell2eb38892018-01-11 01:01:17 +00001836 deflate_read_buf(s->strm, s->window + s->strstart, have);
mark13dc2462017-02-14 22:15:29 -08001837 s->strstart += have;
1838 }
1839 if (s->high_water < s->strstart)
1840 s->high_water = s->strstart;
1841
1842 /* There was not enough avail_out to write a complete worthy or flushed
1843 * stored block to next_out. Write a stored block to pending instead, if we
1844 * have enough input for a worthy block, or if flushing and there is enough
1845 * room for the remaining input as a stored block in the pending buffer.
1846 */
1847 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1848 /* maximum stored block length that will fit in pending: */
1849 have = MIN(s->pending_buf_size - have, MAX_STORED);
1850 min_block = MIN(have, s->w_size);
1851 left = s->strstart - s->block_start;
1852 if (left >= min_block ||
1853 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1854 s->strm->avail_in == 0 && left <= have)) {
1855 len = MIN(left, have);
1856 last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1857 len == left ? 1 : 0;
1858 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1859 s->block_start += len;
1860 flush_pending(s->strm);
1861 }
1862
1863 /* We've done all we can with the available input and output. */
1864 return last ? finish_started : need_more;
initial.commit3d533e02008-07-27 00:38:33 +00001865}
1866
1867/* ===========================================================================
1868 * Compress as much as possible from the input stream, return the current
1869 * block state.
1870 * This function does not perform lazy evaluation of matches and inserts
1871 * new strings in the dictionary only for unmatched strings or for short
1872 * matches. It is used only for the fast compression options.
1873 */
davidben709ed022017-02-02 13:58:58 -08001874local block_state deflate_fast(s, flush)
initial.commit3d533e02008-07-27 00:38:33 +00001875 deflate_state *s;
1876 int flush;
1877{
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001878 IPos hash_head; /* head of the hash chain */
initial.commit3d533e02008-07-27 00:38:33 +00001879 int bflush; /* set if current block must be flushed */
1880
1881 for (;;) {
1882 /* Make sure that we always have enough lookahead, except
1883 * at the end of the input file. We need MAX_MATCH bytes
1884 * for the next match, plus MIN_MATCH bytes to insert the
1885 * string following the next match.
1886 */
1887 if (s->lookahead < MIN_LOOKAHEAD) {
1888 fill_window(s);
1889 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1890 return need_more;
1891 }
1892 if (s->lookahead == 0) break; /* flush the current block */
1893 }
1894
1895 /* Insert the string window[strstart .. strstart+2] in the
1896 * dictionary, and set hash_head to the head of the hash chain:
1897 */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001898 hash_head = NIL;
initial.commit3d533e02008-07-27 00:38:33 +00001899 if (s->lookahead >= MIN_MATCH) {
robert.bradford10dd6862014-11-05 06:59:34 -08001900 hash_head = insert_string(s, s->strstart);
initial.commit3d533e02008-07-27 00:38:33 +00001901 }
1902
1903 /* Find the longest match, discarding those <= prev_length.
1904 * At this point we have always match_length < MIN_MATCH
1905 */
1906 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1907 /* To simplify the code, we prevent matches with the string
1908 * of window index 0 (in particular we have to avoid a match
1909 * of the string with itself at the start of the input file).
1910 */
davidben709ed022017-02-02 13:58:58 -08001911 s->match_length = longest_match (s, hash_head);
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001912 /* longest_match() sets match_start */
initial.commit3d533e02008-07-27 00:38:33 +00001913 }
1914 if (s->match_length >= MIN_MATCH) {
1915 check_match(s, s->strstart, s->match_start, s->match_length);
1916
1917 _tr_tally_dist(s, s->strstart - s->match_start,
1918 s->match_length - MIN_MATCH, bflush);
1919
1920 s->lookahead -= s->match_length;
1921
1922 /* Insert new strings in the hash table only if the match length
1923 * is not too large. This saves time but degrades compression.
1924 */
1925#ifndef FASTEST
1926 if (s->match_length <= s->max_insert_length &&
1927 s->lookahead >= MIN_MATCH) {
1928 s->match_length--; /* string at strstart already in table */
1929 do {
1930 s->strstart++;
robert.bradford10dd6862014-11-05 06:59:34 -08001931 hash_head = insert_string(s, s->strstart);
initial.commit3d533e02008-07-27 00:38:33 +00001932 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1933 * always MIN_MATCH bytes ahead.
1934 */
1935 } while (--s->match_length != 0);
1936 s->strstart++;
1937 } else
1938#endif
1939 {
1940 s->strstart += s->match_length;
1941 s->match_length = 0;
1942 s->ins_h = s->window[s->strstart];
1943 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1944#if MIN_MATCH != 3
1945 Call UPDATE_HASH() MIN_MATCH-3 more times
1946#endif
1947 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1948 * matter since it will be recomputed at next deflate call.
1949 */
1950 }
1951 } else {
1952 /* No match, output a literal byte */
1953 Tracevv((stderr,"%c", s->window[s->strstart]));
1954 _tr_tally_lit (s, s->window[s->strstart], bflush);
1955 s->lookahead--;
1956 s->strstart++;
1957 }
1958 if (bflush) FLUSH_BLOCK(s, 0);
1959 }
jiadong.zhu6c142162016-06-22 21:22:18 -07001960 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1961 if (flush == Z_FINISH) {
1962 FLUSH_BLOCK(s, 1);
1963 return finish_done;
1964 }
Chris Blume28ea0a92018-08-17 06:16:32 +00001965 if (s->sym_next)
jiadong.zhu6c142162016-06-22 21:22:18 -07001966 FLUSH_BLOCK(s, 0);
1967 return block_done;
initial.commit3d533e02008-07-27 00:38:33 +00001968}
1969
1970#ifndef FASTEST
1971/* ===========================================================================
1972 * Same as above, but achieves better compression. We use a lazy
1973 * evaluation for matches: a match is finally adopted only if there is
1974 * no better match at the next window position.
1975 */
davidben709ed022017-02-02 13:58:58 -08001976local block_state deflate_slow(s, flush)
initial.commit3d533e02008-07-27 00:38:33 +00001977 deflate_state *s;
1978 int flush;
1979{
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00001980 IPos hash_head; /* head of hash chain */
initial.commit3d533e02008-07-27 00:38:33 +00001981 int bflush; /* set if current block must be flushed */
1982
1983 /* Process the input block. */
1984 for (;;) {
1985 /* Make sure that we always have enough lookahead, except
1986 * at the end of the input file. We need MAX_MATCH bytes
1987 * for the next match, plus MIN_MATCH bytes to insert the
1988 * string following the next match.
1989 */
1990 if (s->lookahead < MIN_LOOKAHEAD) {
1991 fill_window(s);
1992 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1993 return need_more;
1994 }
1995 if (s->lookahead == 0) break; /* flush the current block */
1996 }
1997
1998 /* Insert the string window[strstart .. strstart+2] in the
1999 * dictionary, and set hash_head to the head of the hash chain:
2000 */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002001 hash_head = NIL;
initial.commit3d533e02008-07-27 00:38:33 +00002002 if (s->lookahead >= MIN_MATCH) {
robert.bradford10dd6862014-11-05 06:59:34 -08002003 hash_head = insert_string(s, s->strstart);
initial.commit3d533e02008-07-27 00:38:33 +00002004 }
2005
2006 /* Find the longest match, discarding those <= prev_length.
2007 */
2008 s->prev_length = s->match_length, s->prev_match = s->match_start;
2009 s->match_length = MIN_MATCH-1;
2010
davidben709ed022017-02-02 13:58:58 -08002011 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
2012 s->strstart - hash_head <= MAX_DIST(s)) {
initial.commit3d533e02008-07-27 00:38:33 +00002013 /* To simplify the code, we prevent matches with the string
2014 * of window index 0 (in particular we have to avoid a match
2015 * of the string with itself at the start of the input file).
2016 */
davidben709ed022017-02-02 13:58:58 -08002017 s->match_length = longest_match (s, hash_head);
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002018 /* longest_match() sets match_start */
initial.commit3d533e02008-07-27 00:38:33 +00002019
2020 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
2021#if TOO_FAR <= 32767
2022 || (s->match_length == MIN_MATCH &&
2023 s->strstart - s->match_start > TOO_FAR)
2024#endif
2025 )) {
2026
2027 /* If prev_match is also MIN_MATCH, match_start is garbage
2028 * but we will ignore the current match anyway.
2029 */
2030 s->match_length = MIN_MATCH-1;
2031 }
2032 }
2033 /* If there was a match at the previous step and the current
2034 * match is not better, output the previous match:
2035 */
davidben709ed022017-02-02 13:58:58 -08002036 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
initial.commit3d533e02008-07-27 00:38:33 +00002037 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
2038 /* Do not insert strings in hash table beyond this. */
2039
2040 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
2041
2042 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
2043 s->prev_length - MIN_MATCH, bflush);
2044
2045 /* Insert in hash table all strings up to the end of the match.
2046 * strstart-1 and strstart are already inserted. If there is not
2047 * enough lookahead, the last two strings are not inserted in
2048 * the hash table.
2049 */
2050 s->lookahead -= s->prev_length-1;
2051 s->prev_length -= 2;
2052 do {
2053 if (++s->strstart <= max_insert) {
robert.bradford10dd6862014-11-05 06:59:34 -08002054 hash_head = insert_string(s, s->strstart);
initial.commit3d533e02008-07-27 00:38:33 +00002055 }
2056 } while (--s->prev_length != 0);
2057 s->match_available = 0;
2058 s->match_length = MIN_MATCH-1;
2059 s->strstart++;
2060
2061 if (bflush) FLUSH_BLOCK(s, 0);
2062
2063 } else if (s->match_available) {
2064 /* If there was no match at the previous position, output a
2065 * single literal. If there was a match but the current match
2066 * is longer, truncate the previous match to a single literal.
2067 */
2068 Tracevv((stderr,"%c", s->window[s->strstart-1]));
2069 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
2070 if (bflush) {
2071 FLUSH_BLOCK_ONLY(s, 0);
2072 }
2073 s->strstart++;
2074 s->lookahead--;
2075 if (s->strm->avail_out == 0) return need_more;
2076 } else {
2077 /* There is no previous match to compare with, wait for
2078 * the next step to decide.
2079 */
2080 s->match_available = 1;
2081 s->strstart++;
2082 s->lookahead--;
2083 }
2084 }
2085 Assert (flush != Z_NO_FLUSH, "no flush?");
2086 if (s->match_available) {
2087 Tracevv((stderr,"%c", s->window[s->strstart-1]));
2088 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
2089 s->match_available = 0;
2090 }
jiadong.zhu6c142162016-06-22 21:22:18 -07002091 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2092 if (flush == Z_FINISH) {
2093 FLUSH_BLOCK(s, 1);
2094 return finish_done;
2095 }
Chris Blume28ea0a92018-08-17 06:16:32 +00002096 if (s->sym_next)
jiadong.zhu6c142162016-06-22 21:22:18 -07002097 FLUSH_BLOCK(s, 0);
2098 return block_done;
initial.commit3d533e02008-07-27 00:38:33 +00002099}
2100#endif /* FASTEST */
2101
initial.commit3d533e02008-07-27 00:38:33 +00002102/* ===========================================================================
2103 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2104 * one. Do not maintain a hash table. (It will be regenerated if this run of
2105 * deflate switches away from Z_RLE.)
2106 */
2107local block_state deflate_rle(s, flush)
2108 deflate_state *s;
2109 int flush;
2110{
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002111 int bflush; /* set if current block must be flushed */
2112 uInt prev; /* byte at distance one to match */
2113 Bytef *scan, *strend; /* scan goes up to strend for length of run */
initial.commit3d533e02008-07-27 00:38:33 +00002114
2115 for (;;) {
2116 /* Make sure that we always have enough lookahead, except
2117 * at the end of the input file. We need MAX_MATCH bytes
jiadong.zhu6c142162016-06-22 21:22:18 -07002118 * for the longest run, plus one for the unrolled loop.
initial.commit3d533e02008-07-27 00:38:33 +00002119 */
jiadong.zhu6c142162016-06-22 21:22:18 -07002120 if (s->lookahead <= MAX_MATCH) {
initial.commit3d533e02008-07-27 00:38:33 +00002121 fill_window(s);
jiadong.zhu6c142162016-06-22 21:22:18 -07002122 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
initial.commit3d533e02008-07-27 00:38:33 +00002123 return need_more;
2124 }
2125 if (s->lookahead == 0) break; /* flush the current block */
2126 }
2127
2128 /* See how many times the previous byte repeats */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002129 s->match_length = 0;
2130 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
initial.commit3d533e02008-07-27 00:38:33 +00002131 scan = s->window + s->strstart - 1;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002132 prev = *scan;
2133 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2134 strend = s->window + s->strstart + MAX_MATCH;
2135 do {
2136 } while (prev == *++scan && prev == *++scan &&
2137 prev == *++scan && prev == *++scan &&
2138 prev == *++scan && prev == *++scan &&
2139 prev == *++scan && prev == *++scan &&
2140 scan < strend);
mark13dc2462017-02-14 22:15:29 -08002141 s->match_length = MAX_MATCH - (uInt)(strend - scan);
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002142 if (s->match_length > s->lookahead)
2143 s->match_length = s->lookahead;
2144 }
jiadong.zhu6c142162016-06-22 21:22:18 -07002145 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
initial.commit3d533e02008-07-27 00:38:33 +00002146 }
2147
2148 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002149 if (s->match_length >= MIN_MATCH) {
2150 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2151
2152 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2153
2154 s->lookahead -= s->match_length;
2155 s->strstart += s->match_length;
2156 s->match_length = 0;
initial.commit3d533e02008-07-27 00:38:33 +00002157 } else {
2158 /* No match, output a literal byte */
2159 Tracevv((stderr,"%c", s->window[s->strstart]));
2160 _tr_tally_lit (s, s->window[s->strstart], bflush);
2161 s->lookahead--;
2162 s->strstart++;
2163 }
2164 if (bflush) FLUSH_BLOCK(s, 0);
2165 }
jiadong.zhu6c142162016-06-22 21:22:18 -07002166 s->insert = 0;
2167 if (flush == Z_FINISH) {
2168 FLUSH_BLOCK(s, 1);
2169 return finish_done;
2170 }
Chris Blume28ea0a92018-08-17 06:16:32 +00002171 if (s->sym_next)
jiadong.zhu6c142162016-06-22 21:22:18 -07002172 FLUSH_BLOCK(s, 0);
2173 return block_done;
initial.commit3d533e02008-07-27 00:38:33 +00002174}
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002175
2176/* ===========================================================================
2177 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2178 * (It will be regenerated if this run of deflate switches away from Huffman.)
2179 */
2180local block_state deflate_huff(s, flush)
2181 deflate_state *s;
2182 int flush;
2183{
2184 int bflush; /* set if current block must be flushed */
2185
2186 for (;;) {
2187 /* Make sure that we have a literal to write. */
2188 if (s->lookahead == 0) {
2189 fill_window(s);
2190 if (s->lookahead == 0) {
2191 if (flush == Z_NO_FLUSH)
2192 return need_more;
2193 break; /* flush the current block */
2194 }
2195 }
2196
2197 /* Output a literal byte */
2198 s->match_length = 0;
2199 Tracevv((stderr,"%c", s->window[s->strstart]));
2200 _tr_tally_lit (s, s->window[s->strstart], bflush);
2201 s->lookahead--;
2202 s->strstart++;
2203 if (bflush) FLUSH_BLOCK(s, 0);
2204 }
jiadong.zhu6c142162016-06-22 21:22:18 -07002205 s->insert = 0;
2206 if (flush == Z_FINISH) {
2207 FLUSH_BLOCK(s, 1);
2208 return finish_done;
2209 }
Chris Blume28ea0a92018-08-17 06:16:32 +00002210 if (s->sym_next)
jiadong.zhu6c142162016-06-22 21:22:18 -07002211 FLUSH_BLOCK(s, 0);
2212 return block_done;
hbono@chromium.orgd2dc2092011-12-12 08:48:38 +00002213}