comparison zlib/deflate.c @ 2690:9ef72ee5c0b0

Update vendored zlib to 1.3.1
author Michael Pavone <pavone@retrodev.com>
date Sun, 15 Jun 2025 15:39:33 -0700
parents 00d788dac91a
children
comparison
equal deleted inserted replaced
2689:bd6e33de0972 2690:9ef72ee5c0b0
1 /* deflate.c -- compress data using the deflation algorithm 1 /* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler 2 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6 /* 6 /*
7 * ALGORITHM 7 * ALGORITHM
50 /* @(#) $Id$ */ 50 /* @(#) $Id$ */
51 51
52 #include "deflate.h" 52 #include "deflate.h"
53 53
54 const char deflate_copyright[] = 54 const char deflate_copyright[] =
55 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; 55 " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler ";
56 /* 56 /*
57 If you use the zlib library in a product, an acknowledgment is welcome 57 If you use the zlib library in a product, an acknowledgment is welcome
58 in the documentation of your product. If for some reason you cannot 58 in the documentation of your product. If for some reason you cannot
59 include such an acknowledgment, I would appreciate that you keep this 59 include such an acknowledgment, I would appreciate that you keep this
60 copyright string in the executable of your product. 60 copyright string in the executable of your product.
61 */ 61 */
62 62
63 /* ===========================================================================
64 * Function prototypes.
65 */
66 typedef enum { 63 typedef enum {
67 need_more, /* block not completed, need more input or more output */ 64 need_more, /* block not completed, need more input or more output */
68 block_done, /* block flush performed */ 65 block_done, /* block flush performed */
69 finish_started, /* finish started, need only more output at next deflate */ 66 finish_started, /* finish started, need only more output at next deflate */
70 finish_done /* finish done, accept no more input or output */ 67 finish_done /* finish done, accept no more input or output */
71 } block_state; 68 } block_state;
72 69
73 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 70 typedef block_state (*compress_func)(deflate_state *s, int flush);
74 /* Compression function. Returns the block state after the call. */ 71 /* Compression function. Returns the block state after the call. */
75 72
76 local int deflateStateCheck OF((z_streamp strm)); 73 local block_state deflate_stored(deflate_state *s, int flush);
77 local void slide_hash OF((deflate_state *s)); 74 local block_state deflate_fast(deflate_state *s, int flush);
78 local void fill_window OF((deflate_state *s));
79 local block_state deflate_stored OF((deflate_state *s, int flush));
80 local block_state deflate_fast OF((deflate_state *s, int flush));
81 #ifndef FASTEST 75 #ifndef FASTEST
82 local block_state deflate_slow OF((deflate_state *s, int flush)); 76 local block_state deflate_slow(deflate_state *s, int flush);
83 #endif 77 #endif
84 local block_state deflate_rle OF((deflate_state *s, int flush)); 78 local block_state deflate_rle(deflate_state *s, int flush);
85 local block_state deflate_huff OF((deflate_state *s, int flush)); 79 local block_state deflate_huff(deflate_state *s, int flush);
86 local void lm_init OF((deflate_state *s));
87 local void putShortMSB OF((deflate_state *s, uInt b));
88 local void flush_pending OF((z_streamp strm));
89 local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
90 #ifdef ASMV
91 # pragma message("Assembler code may have bugs -- use at your own risk")
92 void match_init OF((void)); /* asm code initialization */
93 uInt longest_match OF((deflate_state *s, IPos cur_match));
94 #else
95 local uInt longest_match OF((deflate_state *s, IPos cur_match));
96 #endif
97
98 #ifdef ZLIB_DEBUG
99 local void check_match OF((deflate_state *s, IPos start, IPos match,
100 int length));
101 #endif
102 80
103 /* =========================================================================== 81 /* ===========================================================================
104 * Local data 82 * Local data
105 */ 83 */
106 84
158 * Update a hash value with the given input byte 136 * Update a hash value with the given input byte
159 * IN assertion: all calls to UPDATE_HASH are made with consecutive input 137 * IN assertion: all calls to UPDATE_HASH are made with consecutive input
160 * characters, so that a running hash key can be computed from the previous 138 * characters, so that a running hash key can be computed from the previous
161 * key instead of complete recalculation each time. 139 * key instead of complete recalculation each time.
162 */ 140 */
163 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 141 #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
164 142
165 143
166 /* =========================================================================== 144 /* ===========================================================================
167 * Insert string str in the dictionary and set match_head to the previous head 145 * Insert string str in the dictionary and set match_head to the previous head
168 * of the hash chain (the most recent string with same hash key). Return 146 * of the hash chain (the most recent string with same hash key). Return
188 /* =========================================================================== 166 /* ===========================================================================
189 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 167 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
190 * prev[] will be initialized on the fly. 168 * prev[] will be initialized on the fly.
191 */ 169 */
192 #define CLEAR_HASH(s) \ 170 #define CLEAR_HASH(s) \
193 s->head[s->hash_size-1] = NIL; \ 171 do { \
194 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 172 s->head[s->hash_size - 1] = NIL; \
173 zmemzero((Bytef *)s->head, \
174 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
175 } while (0)
195 176
196 /* =========================================================================== 177 /* ===========================================================================
197 * Slide the hash table when sliding the window down (could be avoided with 32 178 * Slide the hash table when sliding the window down (could be avoided with 32
198 * bit values at the expense of memory usage). We slide even when level == 0 to 179 * bit values at the expense of memory usage). We slide even when level == 0 to
199 * keep the hash table consistent if we switch back to level > 0 later. 180 * keep the hash table consistent if we switch back to level > 0 later.
200 */ 181 */
201 local void slide_hash(s) 182 #if defined(__has_feature)
202 deflate_state *s; 183 # if __has_feature(memory_sanitizer)
203 { 184 __attribute__((no_sanitize("memory")))
185 # endif
186 #endif
187 local void slide_hash(deflate_state *s) {
204 unsigned n, m; 188 unsigned n, m;
205 Posf *p; 189 Posf *p;
206 uInt wsize = s->w_size; 190 uInt wsize = s->w_size;
207 191
208 n = s->hash_size; 192 n = s->hash_size;
222 */ 206 */
223 } while (--n); 207 } while (--n);
224 #endif 208 #endif
225 } 209 }
226 210
211 /* ===========================================================================
212 * Read a new buffer from the current input stream, update the adler32
213 * and total number of bytes read. All deflate() input goes through
214 * this function so some applications may wish to modify it to avoid
215 * allocating a large strm->next_in buffer and copying from it.
216 * (See also flush_pending()).
217 */
218 local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) {
219 unsigned len = strm->avail_in;
220
221 if (len > size) len = size;
222 if (len == 0) return 0;
223
224 strm->avail_in -= len;
225
226 zmemcpy(buf, strm->next_in, len);
227 if (strm->state->wrap == 1) {
228 strm->adler = adler32(strm->adler, buf, len);
229 }
230 #ifdef GZIP
231 else if (strm->state->wrap == 2) {
232 strm->adler = crc32(strm->adler, buf, len);
233 }
234 #endif
235 strm->next_in += len;
236 strm->total_in += len;
237
238 return len;
239 }
240
241 /* ===========================================================================
242 * Fill the window when the lookahead becomes insufficient.
243 * Updates strstart and lookahead.
244 *
245 * IN assertion: lookahead < MIN_LOOKAHEAD
246 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
247 * At least one byte has been read, or avail_in == 0; reads are
248 * performed for at least two bytes (required for the zip translate_eol
249 * option -- not supported here).
250 */
251 local void fill_window(deflate_state *s) {
252 unsigned n;
253 unsigned more; /* Amount of free space at the end of the window. */
254 uInt wsize = s->w_size;
255
256 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
257
258 do {
259 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
260
261 /* Deal with !@#$% 64K limit: */
262 if (sizeof(int) <= 2) {
263 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
264 more = wsize;
265
266 } else if (more == (unsigned)(-1)) {
267 /* Very unlikely, but possible on 16 bit machine if
268 * strstart == 0 && lookahead == 1 (input done a byte at time)
269 */
270 more--;
271 }
272 }
273
274 /* If the window is almost full and there is insufficient lookahead,
275 * move the upper half to the lower one to make room in the upper half.
276 */
277 if (s->strstart >= wsize + MAX_DIST(s)) {
278
279 zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
280 s->match_start -= wsize;
281 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
282 s->block_start -= (long) wsize;
283 if (s->insert > s->strstart)
284 s->insert = s->strstart;
285 slide_hash(s);
286 more += wsize;
287 }
288 if (s->strm->avail_in == 0) break;
289
290 /* If there was no sliding:
291 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
292 * more == window_size - lookahead - strstart
293 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
294 * => more >= window_size - 2*WSIZE + 2
295 * In the BIG_MEM or MMAP case (not yet supported),
296 * window_size == input_size + MIN_LOOKAHEAD &&
297 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
298 * Otherwise, window_size == 2*WSIZE so more >= 2.
299 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
300 */
301 Assert(more >= 2, "more < 2");
302
303 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
304 s->lookahead += n;
305
306 /* Initialize the hash value now that we have some input: */
307 if (s->lookahead + s->insert >= MIN_MATCH) {
308 uInt str = s->strstart - s->insert;
309 s->ins_h = s->window[str];
310 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
311 #if MIN_MATCH != 3
312 Call UPDATE_HASH() MIN_MATCH-3 more times
313 #endif
314 while (s->insert) {
315 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
316 #ifndef FASTEST
317 s->prev[str & s->w_mask] = s->head[s->ins_h];
318 #endif
319 s->head[s->ins_h] = (Pos)str;
320 str++;
321 s->insert--;
322 if (s->lookahead + s->insert < MIN_MATCH)
323 break;
324 }
325 }
326 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
327 * but this is not important since only literal bytes will be emitted.
328 */
329
330 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
331
332 /* If the WIN_INIT bytes after the end of the current data have never been
333 * written, then zero those bytes in order to avoid memory check reports of
334 * the use of uninitialized (or uninitialised as Julian writes) bytes by
335 * the longest match routines. Update the high water mark for the next
336 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
337 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
338 */
339 if (s->high_water < s->window_size) {
340 ulg curr = s->strstart + (ulg)(s->lookahead);
341 ulg init;
342
343 if (s->high_water < curr) {
344 /* Previous high water mark below current data -- zero WIN_INIT
345 * bytes or up to end of window, whichever is less.
346 */
347 init = s->window_size - curr;
348 if (init > WIN_INIT)
349 init = WIN_INIT;
350 zmemzero(s->window + curr, (unsigned)init);
351 s->high_water = curr + init;
352 }
353 else if (s->high_water < (ulg)curr + WIN_INIT) {
354 /* High water mark at or above current data, but below current data
355 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
356 * to end of window, whichever is less.
357 */
358 init = (ulg)curr + WIN_INIT - s->high_water;
359 if (init > s->window_size - s->high_water)
360 init = s->window_size - s->high_water;
361 zmemzero(s->window + s->high_water, (unsigned)init);
362 s->high_water += init;
363 }
364 }
365
366 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
367 "not enough room for search");
368 }
369
227 /* ========================================================================= */ 370 /* ========================================================================= */
228 int ZEXPORT deflateInit_(strm, level, version, stream_size) 371 int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version,
229 z_streamp strm; 372 int stream_size) {
230 int level;
231 const char *version;
232 int stream_size;
233 {
234 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 373 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
235 Z_DEFAULT_STRATEGY, version, stream_size); 374 Z_DEFAULT_STRATEGY, version, stream_size);
236 /* To do: ignore strm->next_in if we use it as window */ 375 /* To do: ignore strm->next_in if we use it as window */
237 } 376 }
238 377
239 /* ========================================================================= */ 378 /* ========================================================================= */
240 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 379 int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
241 version, stream_size) 380 int windowBits, int memLevel, int strategy,
242 z_streamp strm; 381 const char *version, int stream_size) {
243 int level;
244 int method;
245 int windowBits;
246 int memLevel;
247 int strategy;
248 const char *version;
249 int stream_size;
250 {
251 deflate_state *s; 382 deflate_state *s;
252 int wrap = 1; 383 int wrap = 1;
253 static const char my_version[] = ZLIB_VERSION; 384 static const char my_version[] = ZLIB_VERSION;
254
255 ushf *overlay;
256 /* We overlay pending_buf and d_buf+l_buf. This works since the average
257 * output size for (length,distance) codes is <= 24 bits.
258 */
259 385
260 if (version == Z_NULL || version[0] != my_version[0] || 386 if (version == Z_NULL || version[0] != my_version[0] ||
261 stream_size != sizeof(z_stream)) { 387 stream_size != sizeof(z_stream)) {
262 return Z_VERSION_ERROR; 388 return Z_VERSION_ERROR;
263 } 389 }
285 if (level == Z_DEFAULT_COMPRESSION) level = 6; 411 if (level == Z_DEFAULT_COMPRESSION) level = 6;
286 #endif 412 #endif
287 413
288 if (windowBits < 0) { /* suppress zlib wrapper */ 414 if (windowBits < 0) { /* suppress zlib wrapper */
289 wrap = 0; 415 wrap = 0;
416 if (windowBits < -15)
417 return Z_STREAM_ERROR;
290 windowBits = -windowBits; 418 windowBits = -windowBits;
291 } 419 }
292 #ifdef GZIP 420 #ifdef GZIP
293 else if (windowBits > 15) { 421 else if (windowBits > 15) {
294 wrap = 2; /* write gzip wrapper instead */ 422 wrap = 2; /* write gzip wrapper instead */
314 s->w_mask = s->w_size - 1; 442 s->w_mask = s->w_size - 1;
315 443
316 s->hash_bits = (uInt)memLevel + 7; 444 s->hash_bits = (uInt)memLevel + 7;
317 s->hash_size = 1 << s->hash_bits; 445 s->hash_size = 1 << s->hash_bits;
318 s->hash_mask = s->hash_size - 1; 446 s->hash_mask = s->hash_size - 1;
319 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 447 s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
320 448
321 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 449 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
322 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 450 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
323 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 451 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
324 452
325 s->high_water = 0; /* nothing written to s->window yet */ 453 s->high_water = 0; /* nothing written to s->window yet */
326 454
327 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 455 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
328 456
329 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 457 /* We overlay pending_buf and sym_buf. This works since the average size
330 s->pending_buf = (uchf *) overlay; 458 * for length/distance pairs over any compressed block is assured to be 31
331 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 459 * bits or less.
460 *
461 * Analysis: The longest fixed codes are a length code of 8 bits plus 5
462 * extra bits, for lengths 131 to 257. The longest fixed distance codes are
463 * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
464 * possible fixed-codes length/distance pair is then 31 bits total.
465 *
466 * sym_buf starts one-fourth of the way into pending_buf. So there are
467 * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
468 * in sym_buf is three bytes -- two for the distance and one for the
469 * literal/length. As each symbol is consumed, the pointer to the next
470 * sym_buf value to read moves forward three bytes. From that symbol, up to
471 * 31 bits are written to pending_buf. The closest the written pending_buf
472 * bits gets to the next sym_buf symbol to read is just before the last
473 * code is written. At that time, 31*(n - 2) bits have been written, just
474 * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
475 * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
476 * symbols are written.) The closest the writing gets to what is unread is
477 * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
478 * can range from 128 to 32768.
479 *
480 * Therefore, at a minimum, there are 142 bits of space between what is
481 * written and what is read in the overlain buffers, so the symbols cannot
482 * be overwritten by the compressed data. That space is actually 139 bits,
483 * due to the three-bit fixed-code block header.
484 *
485 * That covers the case where either Z_FIXED is specified, forcing fixed
486 * codes, or when the use of fixed codes is chosen, because that choice
487 * results in a smaller compressed block than dynamic codes. That latter
488 * condition then assures that the above analysis also covers all dynamic
489 * blocks. A dynamic-code block will only be chosen to be emitted if it has
490 * fewer bits than a fixed-code block would for the same set of symbols.
491 * Therefore its average symbol length is assured to be less than 31. So
492 * the compressed data for a dynamic block also cannot overwrite the
493 * symbols from which it is being constructed.
494 */
495
496 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS);
497 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
332 498
333 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 499 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
334 s->pending_buf == Z_NULL) { 500 s->pending_buf == Z_NULL) {
335 s->status = FINISH_STATE; 501 s->status = FINISH_STATE;
336 strm->msg = ERR_MSG(Z_MEM_ERROR); 502 strm->msg = ERR_MSG(Z_MEM_ERROR);
337 deflateEnd (strm); 503 deflateEnd (strm);
338 return Z_MEM_ERROR; 504 return Z_MEM_ERROR;
339 } 505 }
340 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); 506 #ifdef LIT_MEM
341 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; 507 s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
508 s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
509 s->sym_end = s->lit_bufsize - 1;
510 #else
511 s->sym_buf = s->pending_buf + s->lit_bufsize;
512 s->sym_end = (s->lit_bufsize - 1) * 3;
513 #endif
514 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
515 * on 16 bit machines and because stored blocks are restricted to
516 * 64K-1 bytes.
517 */
342 518
343 s->level = level; 519 s->level = level;
344 s->strategy = strategy; 520 s->strategy = strategy;
345 s->method = (Byte)method; 521 s->method = (Byte)method;
346 522
348 } 524 }
349 525
350 /* ========================================================================= 526 /* =========================================================================
351 * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 527 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
352 */ 528 */
353 local int deflateStateCheck (strm) 529 local int deflateStateCheck(z_streamp strm) {
354 z_streamp strm;
355 {
356 deflate_state *s; 530 deflate_state *s;
357 if (strm == Z_NULL || 531 if (strm == Z_NULL ||
358 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 532 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
359 return 1; 533 return 1;
360 s = strm->state; 534 s = strm->state;
371 return 1; 545 return 1;
372 return 0; 546 return 0;
373 } 547 }
374 548
375 /* ========================================================================= */ 549 /* ========================================================================= */
376 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 550 int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
377 z_streamp strm; 551 uInt dictLength) {
378 const Bytef *dictionary;
379 uInt dictLength;
380 {
381 deflate_state *s; 552 deflate_state *s;
382 uInt str, n; 553 uInt str, n;
383 int wrap; 554 int wrap;
384 unsigned avail; 555 unsigned avail;
385 z_const unsigned char *next; 556 z_const unsigned char *next;
440 s->wrap = wrap; 611 s->wrap = wrap;
441 return Z_OK; 612 return Z_OK;
442 } 613 }
443 614
444 /* ========================================================================= */ 615 /* ========================================================================= */
445 int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) 616 int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary,
446 z_streamp strm; 617 uInt *dictLength) {
447 Bytef *dictionary;
448 uInt *dictLength;
449 {
450 deflate_state *s; 618 deflate_state *s;
451 uInt len; 619 uInt len;
452 620
453 if (deflateStateCheck(strm)) 621 if (deflateStateCheck(strm))
454 return Z_STREAM_ERROR; 622 return Z_STREAM_ERROR;
462 *dictLength = len; 630 *dictLength = len;
463 return Z_OK; 631 return Z_OK;
464 } 632 }
465 633
466 /* ========================================================================= */ 634 /* ========================================================================= */
467 int ZEXPORT deflateResetKeep (strm) 635 int ZEXPORT deflateResetKeep(z_streamp strm) {
468 z_streamp strm;
469 {
470 deflate_state *s; 636 deflate_state *s;
471 637
472 if (deflateStateCheck(strm)) { 638 if (deflateStateCheck(strm)) {
473 return Z_STREAM_ERROR; 639 return Z_STREAM_ERROR;
474 } 640 }
486 } 652 }
487 s->status = 653 s->status =
488 #ifdef GZIP 654 #ifdef GZIP
489 s->wrap == 2 ? GZIP_STATE : 655 s->wrap == 2 ? GZIP_STATE :
490 #endif 656 #endif
491 s->wrap ? INIT_STATE : BUSY_STATE; 657 INIT_STATE;
492 strm->adler = 658 strm->adler =
493 #ifdef GZIP 659 #ifdef GZIP
494 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 660 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
495 #endif 661 #endif
496 adler32(0L, Z_NULL, 0); 662 adler32(0L, Z_NULL, 0);
497 s->last_flush = Z_NO_FLUSH; 663 s->last_flush = -2;
498 664
499 _tr_init(s); 665 _tr_init(s);
500 666
501 return Z_OK; 667 return Z_OK;
502 } 668 }
503 669
670 /* ===========================================================================
671 * Initialize the "longest match" routines for a new zlib stream
672 */
673 local void lm_init(deflate_state *s) {
674 s->window_size = (ulg)2L*s->w_size;
675
676 CLEAR_HASH(s);
677
678 /* Set the default configuration parameters:
679 */
680 s->max_lazy_match = configuration_table[s->level].max_lazy;
681 s->good_match = configuration_table[s->level].good_length;
682 s->nice_match = configuration_table[s->level].nice_length;
683 s->max_chain_length = configuration_table[s->level].max_chain;
684
685 s->strstart = 0;
686 s->block_start = 0L;
687 s->lookahead = 0;
688 s->insert = 0;
689 s->match_length = s->prev_length = MIN_MATCH-1;
690 s->match_available = 0;
691 s->ins_h = 0;
692 }
693
504 /* ========================================================================= */ 694 /* ========================================================================= */
505 int ZEXPORT deflateReset (strm) 695 int ZEXPORT deflateReset(z_streamp strm) {
506 z_streamp strm;
507 {
508 int ret; 696 int ret;
509 697
510 ret = deflateResetKeep(strm); 698 ret = deflateResetKeep(strm);
511 if (ret == Z_OK) 699 if (ret == Z_OK)
512 lm_init(strm->state); 700 lm_init(strm->state);
513 return ret; 701 return ret;
514 } 702 }
515 703
516 /* ========================================================================= */ 704 /* ========================================================================= */
517 int ZEXPORT deflateSetHeader (strm, head) 705 int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) {
518 z_streamp strm;
519 gz_headerp head;
520 {
521 if (deflateStateCheck(strm) || strm->state->wrap != 2) 706 if (deflateStateCheck(strm) || strm->state->wrap != 2)
522 return Z_STREAM_ERROR; 707 return Z_STREAM_ERROR;
523 strm->state->gzhead = head; 708 strm->state->gzhead = head;
524 return Z_OK; 709 return Z_OK;
525 } 710 }
526 711
527 /* ========================================================================= */ 712 /* ========================================================================= */
528 int ZEXPORT deflatePending (strm, pending, bits) 713 int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
529 unsigned *pending;
530 int *bits;
531 z_streamp strm;
532 {
533 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 714 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
534 if (pending != Z_NULL) 715 if (pending != Z_NULL)
535 *pending = strm->state->pending; 716 *pending = strm->state->pending;
536 if (bits != Z_NULL) 717 if (bits != Z_NULL)
537 *bits = strm->state->bi_valid; 718 *bits = strm->state->bi_valid;
538 return Z_OK; 719 return Z_OK;
539 } 720 }
540 721
541 /* ========================================================================= */ 722 /* ========================================================================= */
542 int ZEXPORT deflatePrime (strm, bits, value) 723 int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
543 z_streamp strm;
544 int bits;
545 int value;
546 {
547 deflate_state *s; 724 deflate_state *s;
548 int put; 725 int put;
549 726
550 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 727 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
551 s = strm->state; 728 s = strm->state;
552 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) 729 #ifdef LIT_MEM
730 if (bits < 0 || bits > 16 ||
731 (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
553 return Z_BUF_ERROR; 732 return Z_BUF_ERROR;
733 #else
734 if (bits < 0 || bits > 16 ||
735 s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
736 return Z_BUF_ERROR;
737 #endif
554 do { 738 do {
555 put = Buf_size - s->bi_valid; 739 put = Buf_size - s->bi_valid;
556 if (put > bits) 740 if (put > bits)
557 put = bits; 741 put = bits;
558 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); 742 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
563 } while (bits); 747 } while (bits);
564 return Z_OK; 748 return Z_OK;
565 } 749 }
566 750
567 /* ========================================================================= */ 751 /* ========================================================================= */
568 int ZEXPORT deflateParams(strm, level, strategy) 752 int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) {
569 z_streamp strm;
570 int level;
571 int strategy;
572 {
573 deflate_state *s; 753 deflate_state *s;
574 compress_func func; 754 compress_func func;
575 755
576 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 756 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
577 s = strm->state; 757 s = strm->state;
585 return Z_STREAM_ERROR; 765 return Z_STREAM_ERROR;
586 } 766 }
587 func = configuration_table[s->level].func; 767 func = configuration_table[s->level].func;
588 768
589 if ((strategy != s->strategy || func != configuration_table[level].func) && 769 if ((strategy != s->strategy || func != configuration_table[level].func) &&
590 s->high_water) { 770 s->last_flush != -2) {
591 /* Flush the last buffer: */ 771 /* Flush the last buffer: */
592 int err = deflate(strm, Z_BLOCK); 772 int err = deflate(strm, Z_BLOCK);
593 if (err == Z_STREAM_ERROR) 773 if (err == Z_STREAM_ERROR)
594 return err; 774 return err;
595 if (strm->avail_out == 0) 775 if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
596 return Z_BUF_ERROR; 776 return Z_BUF_ERROR;
597 } 777 }
598 if (s->level != level) { 778 if (s->level != level) {
599 if (s->level == 0 && s->matches != 0) { 779 if (s->level == 0 && s->matches != 0) {
600 if (s->matches == 1) 780 if (s->matches == 1)
612 s->strategy = strategy; 792 s->strategy = strategy;
613 return Z_OK; 793 return Z_OK;
614 } 794 }
615 795
616 /* ========================================================================= */ 796 /* ========================================================================= */
617 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) 797 int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy,
618 z_streamp strm; 798 int nice_length, int max_chain) {
619 int good_length;
620 int max_lazy;
621 int nice_length;
622 int max_chain;
623 {
624 deflate_state *s; 799 deflate_state *s;
625 800
626 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 801 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
627 s = strm->state; 802 s = strm->state;
628 s->good_match = (uInt)good_length; 803 s->good_match = (uInt)good_length;
631 s->max_chain_length = (uInt)max_chain; 806 s->max_chain_length = (uInt)max_chain;
632 return Z_OK; 807 return Z_OK;
633 } 808 }
634 809
635 /* ========================================================================= 810 /* =========================================================================
636 * For the default windowBits of 15 and memLevel of 8, this function returns 811 * For the default windowBits of 15 and memLevel of 8, this function returns a
637 * a close to exact, as well as small, upper bound on the compressed size. 812 * close to exact, as well as small, upper bound on the compressed size. This
638 * They are coded as constants here for a reason--if the #define's are 813 * is an expansion of ~0.03%, plus a small constant.
639 * changed, then this function needs to be changed as well. The return
640 * value for 15 and 8 only works for those exact settings.
641 * 814 *
642 * For any setting other than those defaults for windowBits and memLevel, 815 * For any setting other than those defaults for windowBits and memLevel, one
643 * the value returned is a conservative worst case for the maximum expansion 816 * of two worst case bounds is returned. This is at most an expansion of ~4% or
644 * resulting from using fixed blocks instead of stored blocks, which deflate 817 * ~13%, plus a small constant.
645 * can emit on compressed data for some combinations of the parameters.
646 * 818 *
647 * This function could be more sophisticated to provide closer upper bounds for 819 * Both the 0.03% and 4% derive from the overhead of stored blocks. The first
648 * every combination of windowBits and memLevel. But even the conservative 820 * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second
649 * upper bound of about 14% expansion does not seem onerous for output buffer 821 * is for stored blocks of 127 bytes (the worst case memLevel == 1). The
650 * allocation. 822 * expansion results from five bytes of header for each stored block.
651 */ 823 *
652 uLong ZEXPORT deflateBound(strm, sourceLen) 824 * The larger expansion of 13% results from a window size less than or equal to
653 z_streamp strm; 825 * the symbols buffer size (windowBits <= memLevel + 7). In that case some of
654 uLong sourceLen; 826 * the data being compressed may have slid out of the sliding window, impeding
655 { 827 * a stored block from being emitted. Then the only choice is a fixed or
828 * dynamic block, where a fixed block limits the maximum expansion to 9 bits
829 * per 8-bit byte, plus 10 bits for every block. The smallest block size for
830 * which this can occur is 255 (memLevel == 2).
831 *
832 * Shifts are used to approximate divisions, for speed.
833 */
834 uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
656 deflate_state *s; 835 deflate_state *s;
657 uLong complen, wraplen; 836 uLong fixedlen, storelen, wraplen;
658 837
659 /* conservative upper bound for compressed data */ 838 /* upper bound for fixed blocks with 9-bit literals and length 255
660 complen = sourceLen + 839 (memLevel == 2, which is the lowest that may not use stored blocks) --
661 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; 840 ~13% overhead plus a small constant */
662 841 fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
663 /* if can't get parameters, return conservative bound plus zlib wrapper */ 842 (sourceLen >> 9) + 4;
843
844 /* upper bound for stored blocks with length 127 (memLevel == 1) --
845 ~4% overhead plus a small constant */
846 storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
847 (sourceLen >> 11) + 7;
848
849 /* if can't get parameters, return larger bound plus a zlib wrapper */
664 if (deflateStateCheck(strm)) 850 if (deflateStateCheck(strm))
665 return complen + 6; 851 return (fixedlen > storelen ? fixedlen : storelen) + 6;
666 852
667 /* compute wrapper length */ 853 /* compute wrapper length */
668 s = strm->state; 854 s = strm->state;
669 switch (s->wrap) { 855 switch (s->wrap) {
670 case 0: /* raw deflate */ 856 case 0: /* raw deflate */
697 #endif 883 #endif
698 default: /* for compiler happiness */ 884 default: /* for compiler happiness */
699 wraplen = 6; 885 wraplen = 6;
700 } 886 }
701 887
702 /* if not default parameters, return conservative bound */ 888 /* if not default parameters, return one of the conservative bounds */
703 if (s->w_bits != 15 || s->hash_bits != 8 + 7) 889 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
704 return complen + wraplen; 890 return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) +
705 891 wraplen;
706 /* default settings: return tight bound for that case */ 892
893 /* default settings: return tight bound for that case -- ~0.03% overhead
894 plus a small constant */
707 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 895 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
708 (sourceLen >> 25) + 13 - 6 + wraplen; 896 (sourceLen >> 25) + 13 - 6 + wraplen;
709 } 897 }
710 898
711 /* ========================================================================= 899 /* =========================================================================
712 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 900 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
713 * IN assertion: the stream state is correct and there is enough room in 901 * IN assertion: the stream state is correct and there is enough room in
714 * pending_buf. 902 * pending_buf.
715 */ 903 */
716 local void putShortMSB (s, b) 904 local void putShortMSB(deflate_state *s, uInt b) {
717 deflate_state *s;
718 uInt b;
719 {
720 put_byte(s, (Byte)(b >> 8)); 905 put_byte(s, (Byte)(b >> 8));
721 put_byte(s, (Byte)(b & 0xff)); 906 put_byte(s, (Byte)(b & 0xff));
722 } 907 }
723 908
724 /* ========================================================================= 909 /* =========================================================================
725 * Flush as much pending output as possible. All deflate() output, except for 910 * Flush as much pending output as possible. All deflate() output, except for
726 * some deflate_stored() output, goes through this function so some 911 * some deflate_stored() output, goes through this function so some
727 * applications may wish to modify it to avoid allocating a large 912 * applications may wish to modify it to avoid allocating a large
728 * strm->next_out buffer and copying into it. (See also read_buf()). 913 * strm->next_out buffer and copying into it. (See also read_buf()).
729 */ 914 */
730 local void flush_pending(strm) 915 local void flush_pending(z_streamp strm) {
731 z_streamp strm;
732 {
733 unsigned len; 916 unsigned len;
734 deflate_state *s = strm->state; 917 deflate_state *s = strm->state;
735 918
736 _tr_flush_bits(s); 919 _tr_flush_bits(s);
737 len = s->pending; 920 len = s->pending;
758 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ 941 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
759 s->pending - (beg)); \ 942 s->pending - (beg)); \
760 } while (0) 943 } while (0)
761 944
762 /* ========================================================================= */ 945 /* ========================================================================= */
763 int ZEXPORT deflate (strm, flush) 946 int ZEXPORT deflate(z_streamp strm, int flush) {
764 z_streamp strm;
765 int flush;
766 {
767 int old_flush; /* value of flush param for previous deflate call */ 947 int old_flush; /* value of flush param for previous deflate call */
768 deflate_state *s; 948 deflate_state *s;
769 949
770 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { 950 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
771 return Z_STREAM_ERROR; 951 return Z_STREAM_ERROR;
809 if (s->status == FINISH_STATE && strm->avail_in != 0) { 989 if (s->status == FINISH_STATE && strm->avail_in != 0) {
810 ERR_RETURN(strm, Z_BUF_ERROR); 990 ERR_RETURN(strm, Z_BUF_ERROR);
811 } 991 }
812 992
813 /* Write the header */ 993 /* Write the header */
994 if (s->status == INIT_STATE && s->wrap == 0)
995 s->status = BUSY_STATE;
814 if (s->status == INIT_STATE) { 996 if (s->status == INIT_STATE) {
815 /* zlib header */ 997 /* zlib header */
816 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; 998 uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
817 uInt level_flags; 999 uInt level_flags;
818 1000
819 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 1001 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
820 level_flags = 0; 1002 level_flags = 0;
821 else if (s->level < 6) 1003 else if (s->level < 6)
1071 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 1253 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1072 return s->pending != 0 ? Z_OK : Z_STREAM_END; 1254 return s->pending != 0 ? Z_OK : Z_STREAM_END;
1073 } 1255 }
1074 1256
1075 /* ========================================================================= */ 1257 /* ========================================================================= */
1076 int ZEXPORT deflateEnd (strm) 1258 int ZEXPORT deflateEnd(z_streamp strm) {
1077 z_streamp strm;
1078 {
1079 int status; 1259 int status;
1080 1260
1081 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 1261 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1082 1262
1083 status = strm->state->status; 1263 status = strm->state->status;
1097 /* ========================================================================= 1277 /* =========================================================================
1098 * Copy the source state to the destination state. 1278 * Copy the source state to the destination state.
1099 * To simplify the source, this is not supported for 16-bit MSDOS (which 1279 * To simplify the source, this is not supported for 16-bit MSDOS (which
1100 * doesn't have enough memory anyway to duplicate compression states). 1280 * doesn't have enough memory anyway to duplicate compression states).
1101 */ 1281 */
1102 int ZEXPORT deflateCopy (dest, source) 1282 int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1103 z_streamp dest;
1104 z_streamp source;
1105 {
1106 #ifdef MAXSEG_64K 1283 #ifdef MAXSEG_64K
1284 (void)dest;
1285 (void)source;
1107 return Z_STREAM_ERROR; 1286 return Z_STREAM_ERROR;
1108 #else 1287 #else
1109 deflate_state *ds; 1288 deflate_state *ds;
1110 deflate_state *ss; 1289 deflate_state *ss;
1111 ushf *overlay;
1112 1290
1113 1291
1114 if (deflateStateCheck(source) || dest == Z_NULL) { 1292 if (deflateStateCheck(source) || dest == Z_NULL) {
1115 return Z_STREAM_ERROR; 1293 return Z_STREAM_ERROR;
1116 } 1294 }
1126 ds->strm = dest; 1304 ds->strm = dest;
1127 1305
1128 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 1306 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1129 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 1307 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1130 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1308 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1131 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); 1309 ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS);
1132 ds->pending_buf = (uchf *) overlay;
1133 1310
1134 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 1311 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1135 ds->pending_buf == Z_NULL) { 1312 ds->pending_buf == Z_NULL) {
1136 deflateEnd (dest); 1313 deflateEnd (dest);
1137 return Z_MEM_ERROR; 1314 return Z_MEM_ERROR;
1138 } 1315 }
1139 /* following zmemcpy do not work for 16-bit MSDOS */ 1316 /* following zmemcpy do not work for 16-bit MSDOS */
1140 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 1317 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1141 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 1318 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1142 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1319 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1143 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); 1320 zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
1144 1321
1145 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1322 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1146 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); 1323 #ifdef LIT_MEM
1147 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; 1324 ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1325 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1326 #else
1327 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1328 #endif
1148 1329
1149 ds->l_desc.dyn_tree = ds->dyn_ltree; 1330 ds->l_desc.dyn_tree = ds->dyn_ltree;
1150 ds->d_desc.dyn_tree = ds->dyn_dtree; 1331 ds->d_desc.dyn_tree = ds->dyn_dtree;
1151 ds->bl_desc.dyn_tree = ds->bl_tree; 1332 ds->bl_desc.dyn_tree = ds->bl_tree;
1152 1333
1153 return Z_OK; 1334 return Z_OK;
1154 #endif /* MAXSEG_64K */ 1335 #endif /* MAXSEG_64K */
1155 }
1156
1157 /* ===========================================================================
1158 * Read a new buffer from the current input stream, update the adler32
1159 * and total number of bytes read. All deflate() input goes through
1160 * this function so some applications may wish to modify it to avoid
1161 * allocating a large strm->next_in buffer and copying from it.
1162 * (See also flush_pending()).
1163 */
1164 local unsigned read_buf(strm, buf, size)
1165 z_streamp strm;
1166 Bytef *buf;
1167 unsigned size;
1168 {
1169 unsigned len = strm->avail_in;
1170
1171 if (len > size) len = size;
1172 if (len == 0) return 0;
1173
1174 strm->avail_in -= len;
1175
1176 zmemcpy(buf, strm->next_in, len);
1177 if (strm->state->wrap == 1) {
1178 strm->adler = adler32(strm->adler, buf, len);
1179 }
1180 #ifdef GZIP
1181 else if (strm->state->wrap == 2) {
1182 strm->adler = crc32(strm->adler, buf, len);
1183 }
1184 #endif
1185 strm->next_in += len;
1186 strm->total_in += len;
1187
1188 return len;
1189 }
1190
1191 /* ===========================================================================
1192 * Initialize the "longest match" routines for a new zlib stream
1193 */
1194 local void lm_init (s)
1195 deflate_state *s;
1196 {
1197 s->window_size = (ulg)2L*s->w_size;
1198
1199 CLEAR_HASH(s);
1200
1201 /* Set the default configuration parameters:
1202 */
1203 s->max_lazy_match = configuration_table[s->level].max_lazy;
1204 s->good_match = configuration_table[s->level].good_length;
1205 s->nice_match = configuration_table[s->level].nice_length;
1206 s->max_chain_length = configuration_table[s->level].max_chain;
1207
1208 s->strstart = 0;
1209 s->block_start = 0L;
1210 s->lookahead = 0;
1211 s->insert = 0;
1212 s->match_length = s->prev_length = MIN_MATCH-1;
1213 s->match_available = 0;
1214 s->ins_h = 0;
1215 #ifndef FASTEST
1216 #ifdef ASMV
1217 match_init(); /* initialize the asm code */
1218 #endif
1219 #endif
1220 } 1336 }
1221 1337
1222 #ifndef FASTEST 1338 #ifndef FASTEST
1223 /* =========================================================================== 1339 /* ===========================================================================
1224 * Set match_start to the longest match starting at the given string and 1340 * Set match_start to the longest match starting at the given string and
1227 * garbage. 1343 * garbage.
1228 * IN assertions: cur_match is the head of the hash chain for the current 1344 * IN assertions: cur_match is the head of the hash chain for the current
1229 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1345 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1230 * OUT assertion: the match length is not greater than s->lookahead. 1346 * OUT assertion: the match length is not greater than s->lookahead.
1231 */ 1347 */
1232 #ifndef ASMV 1348 local uInt longest_match(deflate_state *s, IPos cur_match) {
1233 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1234 * match.S. The code will be functionally equivalent.
1235 */
1236 local uInt longest_match(s, cur_match)
1237 deflate_state *s;
1238 IPos cur_match; /* current match */
1239 {
1240 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1349 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1241 register Bytef *scan = s->window + s->strstart; /* current string */ 1350 register Bytef *scan = s->window + s->strstart; /* current string */
1242 register Bytef *match; /* matched string */ 1351 register Bytef *match; /* matched string */
1243 register int len; /* length of current match */ 1352 register int len; /* length of current match */
1244 int best_len = (int)s->prev_length; /* best match length so far */ 1353 int best_len = (int)s->prev_length; /* best match length so far */
1255 /* Compare two bytes at a time. Note: this is not always beneficial. 1364 /* Compare two bytes at a time. Note: this is not always beneficial.
1256 * Try with and without -DUNALIGNED_OK to check. 1365 * Try with and without -DUNALIGNED_OK to check.
1257 */ 1366 */
1258 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 1367 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1259 register ush scan_start = *(ushf*)scan; 1368 register ush scan_start = *(ushf*)scan;
1260 register ush scan_end = *(ushf*)(scan+best_len-1); 1369 register ush scan_end = *(ushf*)(scan + best_len - 1);
1261 #else 1370 #else
1262 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1371 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1263 register Byte scan_end1 = scan[best_len-1]; 1372 register Byte scan_end1 = scan[best_len - 1];
1264 register Byte scan_end = scan[best_len]; 1373 register Byte scan_end = scan[best_len];
1265 #endif 1374 #endif
1266 1375
1267 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1376 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1268 * It is easy to get rid of this optimization if necessary. 1377 * It is easy to get rid of this optimization if necessary.
1276 /* Do not look for matches beyond the end of the input. This is necessary 1385 /* Do not look for matches beyond the end of the input. This is necessary
1277 * to make deflate deterministic. 1386 * to make deflate deterministic.
1278 */ 1387 */
1279 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; 1388 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1280 1389
1281 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1390 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1391 "need lookahead");
1282 1392
1283 do { 1393 do {
1284 Assert(cur_match < s->strstart, "no future"); 1394 Assert(cur_match < s->strstart, "no future");
1285 match = s->window + cur_match; 1395 match = s->window + cur_match;
1286 1396
1294 */ 1404 */
1295 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1405 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1296 /* This code assumes sizeof(unsigned short) == 2. Do not use 1406 /* This code assumes sizeof(unsigned short) == 2. Do not use
1297 * UNALIGNED_OK if your compiler uses a different size. 1407 * UNALIGNED_OK if your compiler uses a different size.
1298 */ 1408 */
1299 if (*(ushf*)(match+best_len-1) != scan_end || 1409 if (*(ushf*)(match + best_len - 1) != scan_end ||
1300 *(ushf*)match != scan_start) continue; 1410 *(ushf*)match != scan_start) continue;
1301 1411
1302 /* It is not necessary to compare scan[2] and match[2] since they are 1412 /* It is not necessary to compare scan[2] and match[2] since they are
1303 * always equal when the other bytes match, given that the hash keys 1413 * always equal when the other bytes match, given that the hash keys
1304 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 1414 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1305 * strstart+3, +5, ... up to strstart+257. We check for insufficient 1415 * strstart + 3, + 5, up to strstart + 257. We check for insufficient
1306 * lookahead only every 4th comparison; the 128th check will be made 1416 * lookahead only every 4th comparison; the 128th check will be made
1307 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is 1417 * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
1308 * necessary to put more guard bytes at the end of the window, or 1418 * necessary to put more guard bytes at the end of the window, or
1309 * to check more often for insufficient lookahead. 1419 * to check more often for insufficient lookahead.
1310 */ 1420 */
1311 Assert(scan[2] == match[2], "scan[2]?"); 1421 Assert(scan[2] == match[2], "scan[2]?");
1312 scan++, match++; 1422 scan++, match++;
1313 do { 1423 do {
1314 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1424 } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1315 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1425 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1316 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1426 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1317 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1427 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1318 scan < strend); 1428 scan < strend);
1319 /* The funny "do {}" generates better code on most compilers */ 1429 /* The funny "do {}" generates better code on most compilers */
1320 1430
1321 /* Here, scan <= window+strstart+257 */ 1431 /* Here, scan <= window + strstart + 257 */
1322 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1432 Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1433 "wild scan");
1323 if (*scan == *match) scan++; 1434 if (*scan == *match) scan++;
1324 1435
1325 len = (MAX_MATCH - 1) - (int)(strend-scan); 1436 len = (MAX_MATCH - 1) - (int)(strend - scan);
1326 scan = strend - (MAX_MATCH-1); 1437 scan = strend - (MAX_MATCH-1);
1327 1438
1328 #else /* UNALIGNED_OK */ 1439 #else /* UNALIGNED_OK */
1329 1440
1330 if (match[best_len] != scan_end || 1441 if (match[best_len] != scan_end ||
1331 match[best_len-1] != scan_end1 || 1442 match[best_len - 1] != scan_end1 ||
1332 *match != *scan || 1443 *match != *scan ||
1333 *++match != scan[1]) continue; 1444 *++match != scan[1]) continue;
1334 1445
1335 /* The check at best_len-1 can be removed because it will be made 1446 /* The check at best_len - 1 can be removed because it will be made
1336 * again later. (This heuristic is not always a win.) 1447 * again later. (This heuristic is not always a win.)
1337 * It is not necessary to compare scan[2] and match[2] since they 1448 * It is not necessary to compare scan[2] and match[2] since they
1338 * are always equal when the other bytes match, given that 1449 * are always equal when the other bytes match, given that
1339 * the hash keys are equal and that HASH_BITS >= 8. 1450 * the hash keys are equal and that HASH_BITS >= 8.
1340 */ 1451 */
1341 scan += 2, match++; 1452 scan += 2, match++;
1342 Assert(*scan == *match, "match[2]?"); 1453 Assert(*scan == *match, "match[2]?");
1343 1454
1344 /* We check for insufficient lookahead only every 8th comparison; 1455 /* We check for insufficient lookahead only every 8th comparison;
1345 * the 256th check will be made at strstart+258. 1456 * the 256th check will be made at strstart + 258.
1346 */ 1457 */
1347 do { 1458 do {
1348 } while (*++scan == *++match && *++scan == *++match && 1459 } while (*++scan == *++match && *++scan == *++match &&
1349 *++scan == *++match && *++scan == *++match && 1460 *++scan == *++match && *++scan == *++match &&
1350 *++scan == *++match && *++scan == *++match && 1461 *++scan == *++match && *++scan == *++match &&
1351 *++scan == *++match && *++scan == *++match && 1462 *++scan == *++match && *++scan == *++match &&
1352 scan < strend); 1463 scan < strend);
1353 1464
1354 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1465 Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1466 "wild scan");
1355 1467
1356 len = MAX_MATCH - (int)(strend - scan); 1468 len = MAX_MATCH - (int)(strend - scan);
1357 scan = strend - MAX_MATCH; 1469 scan = strend - MAX_MATCH;
1358 1470
1359 #endif /* UNALIGNED_OK */ 1471 #endif /* UNALIGNED_OK */
1361 if (len > best_len) { 1473 if (len > best_len) {
1362 s->match_start = cur_match; 1474 s->match_start = cur_match;
1363 best_len = len; 1475 best_len = len;
1364 if (len >= nice_match) break; 1476 if (len >= nice_match) break;
1365 #ifdef UNALIGNED_OK 1477 #ifdef UNALIGNED_OK
1366 scan_end = *(ushf*)(scan+best_len-1); 1478 scan_end = *(ushf*)(scan + best_len - 1);
1367 #else 1479 #else
1368 scan_end1 = scan[best_len-1]; 1480 scan_end1 = scan[best_len - 1];
1369 scan_end = scan[best_len]; 1481 scan_end = scan[best_len];
1370 #endif 1482 #endif
1371 } 1483 }
1372 } while ((cur_match = prev[cur_match & wmask]) > limit 1484 } while ((cur_match = prev[cur_match & wmask]) > limit
1373 && --chain_length != 0); 1485 && --chain_length != 0);
1374 1486
1375 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 1487 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1376 return s->lookahead; 1488 return s->lookahead;
1377 } 1489 }
1378 #endif /* ASMV */
1379 1490
1380 #else /* FASTEST */ 1491 #else /* FASTEST */
1381 1492
1382 /* --------------------------------------------------------------------------- 1493 /* ---------------------------------------------------------------------------
1383 * Optimized version for FASTEST only 1494 * Optimized version for FASTEST only
1384 */ 1495 */
1385 local uInt longest_match(s, cur_match) 1496 local uInt longest_match(deflate_state *s, IPos cur_match) {
1386 deflate_state *s;
1387 IPos cur_match; /* current match */
1388 {
1389 register Bytef *scan = s->window + s->strstart; /* current string */ 1497 register Bytef *scan = s->window + s->strstart; /* current string */
1390 register Bytef *match; /* matched string */ 1498 register Bytef *match; /* matched string */
1391 register int len; /* length of current match */ 1499 register int len; /* length of current match */
1392 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1500 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1393 1501
1394 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1502 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1395 * It is easy to get rid of this optimization if necessary. 1503 * It is easy to get rid of this optimization if necessary.
1396 */ 1504 */
1397 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1505 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1398 1506
1399 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1507 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1508 "need lookahead");
1400 1509
1401 Assert(cur_match < s->strstart, "no future"); 1510 Assert(cur_match < s->strstart, "no future");
1402 1511
1403 match = s->window + cur_match; 1512 match = s->window + cur_match;
1404 1513
1405 /* Return failure if the match length is less than 2: 1514 /* Return failure if the match length is less than 2:
1406 */ 1515 */
1407 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 1516 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1408 1517
1409 /* The check at best_len-1 can be removed because it will be made 1518 /* The check at best_len - 1 can be removed because it will be made
1410 * again later. (This heuristic is not always a win.) 1519 * again later. (This heuristic is not always a win.)
1411 * It is not necessary to compare scan[2] and match[2] since they 1520 * It is not necessary to compare scan[2] and match[2] since they
1412 * are always equal when the other bytes match, given that 1521 * are always equal when the other bytes match, given that
1413 * the hash keys are equal and that HASH_BITS >= 8. 1522 * the hash keys are equal and that HASH_BITS >= 8.
1414 */ 1523 */
1415 scan += 2, match += 2; 1524 scan += 2, match += 2;
1416 Assert(*scan == *match, "match[2]?"); 1525 Assert(*scan == *match, "match[2]?");
1417 1526
1418 /* We check for insufficient lookahead only every 8th comparison; 1527 /* We check for insufficient lookahead only every 8th comparison;
1419 * the 256th check will be made at strstart+258. 1528 * the 256th check will be made at strstart + 258.
1420 */ 1529 */
1421 do { 1530 do {
1422 } while (*++scan == *++match && *++scan == *++match && 1531 } while (*++scan == *++match && *++scan == *++match &&
1423 *++scan == *++match && *++scan == *++match && 1532 *++scan == *++match && *++scan == *++match &&
1424 *++scan == *++match && *++scan == *++match && 1533 *++scan == *++match && *++scan == *++match &&
1425 *++scan == *++match && *++scan == *++match && 1534 *++scan == *++match && *++scan == *++match &&
1426 scan < strend); 1535 scan < strend);
1427 1536
1428 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1537 Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan");
1429 1538
1430 len = MAX_MATCH - (int)(strend - scan); 1539 len = MAX_MATCH - (int)(strend - scan);
1431 1540
1432 if (len < MIN_MATCH) return MIN_MATCH - 1; 1541 if (len < MIN_MATCH) return MIN_MATCH - 1;
1433 1542
1443 /* result of memcmp for equal strings */ 1552 /* result of memcmp for equal strings */
1444 1553
1445 /* =========================================================================== 1554 /* ===========================================================================
1446 * Check that the match at match_start is indeed a match. 1555 * Check that the match at match_start is indeed a match.
1447 */ 1556 */
1448 local void check_match(s, start, match, length) 1557 local void check_match(deflate_state *s, IPos start, IPos match, int length) {
1449 deflate_state *s;
1450 IPos start, match;
1451 int length;
1452 {
1453 /* check that the match is indeed a match */ 1558 /* check that the match is indeed a match */
1454 if (zmemcmp(s->window + match, 1559 Bytef *back = s->window + (int)match, *here = s->window + start;
1455 s->window + start, length) != EQUAL) { 1560 IPos len = length;
1456 fprintf(stderr, " start %u, match %u, length %d\n", 1561 if (match == (IPos)-1) {
1457 start, match, length); 1562 /* match starts one byte before the current window -- just compare the
1563 subsequent length-1 bytes */
1564 back++;
1565 here++;
1566 len--;
1567 }
1568 if (zmemcmp(back, here, len) != EQUAL) {
1569 fprintf(stderr, " start %u, match %d, length %d\n",
1570 start, (int)match, length);
1458 do { 1571 do {
1459 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); 1572 fprintf(stderr, "(%02x %02x)", *back++, *here++);
1460 } while (--length != 0); 1573 } while (--len != 0);
1461 z_error("invalid match"); 1574 z_error("invalid match");
1462 } 1575 }
1463 if (z_verbose > 1) { 1576 if (z_verbose > 1) {
1464 fprintf(stderr,"\\[%d,%d]", start-match, length); 1577 fprintf(stderr,"\\[%d,%d]", start - match, length);
1465 do { putc(s->window[start++], stderr); } while (--length != 0); 1578 do { putc(s->window[start++], stderr); } while (--length != 0);
1466 } 1579 }
1467 } 1580 }
1468 #else 1581 #else
1469 # define check_match(s, start, match, length) 1582 # define check_match(s, start, match, length)
1470 #endif /* ZLIB_DEBUG */ 1583 #endif /* ZLIB_DEBUG */
1471
1472 /* ===========================================================================
1473 * Fill the window when the lookahead becomes insufficient.
1474 * Updates strstart and lookahead.
1475 *
1476 * IN assertion: lookahead < MIN_LOOKAHEAD
1477 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1478 * At least one byte has been read, or avail_in == 0; reads are
1479 * performed for at least two bytes (required for the zip translate_eol
1480 * option -- not supported here).
1481 */
1482 local void fill_window(s)
1483 deflate_state *s;
1484 {
1485 unsigned n;
1486 unsigned more; /* Amount of free space at the end of the window. */
1487 uInt wsize = s->w_size;
1488
1489 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1490
1491 do {
1492 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1493
1494 /* Deal with !@#$% 64K limit: */
1495 if (sizeof(int) <= 2) {
1496 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1497 more = wsize;
1498
1499 } else if (more == (unsigned)(-1)) {
1500 /* Very unlikely, but possible on 16 bit machine if
1501 * strstart == 0 && lookahead == 1 (input done a byte at time)
1502 */
1503 more--;
1504 }
1505 }
1506
1507 /* If the window is almost full and there is insufficient lookahead,
1508 * move the upper half to the lower one to make room in the upper half.
1509 */
1510 if (s->strstart >= wsize+MAX_DIST(s)) {
1511
1512 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
1513 s->match_start -= wsize;
1514 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1515 s->block_start -= (long) wsize;
1516 slide_hash(s);
1517 more += wsize;
1518 }
1519 if (s->strm->avail_in == 0) break;
1520
1521 /* If there was no sliding:
1522 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1523 * more == window_size - lookahead - strstart
1524 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1525 * => more >= window_size - 2*WSIZE + 2
1526 * In the BIG_MEM or MMAP case (not yet supported),
1527 * window_size == input_size + MIN_LOOKAHEAD &&
1528 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1529 * Otherwise, window_size == 2*WSIZE so more >= 2.
1530 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1531 */
1532 Assert(more >= 2, "more < 2");
1533
1534 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1535 s->lookahead += n;
1536
1537 /* Initialize the hash value now that we have some input: */
1538 if (s->lookahead + s->insert >= MIN_MATCH) {
1539 uInt str = s->strstart - s->insert;
1540 s->ins_h = s->window[str];
1541 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1542 #if MIN_MATCH != 3
1543 Call UPDATE_HASH() MIN_MATCH-3 more times
1544 #endif
1545 while (s->insert) {
1546 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1547 #ifndef FASTEST
1548 s->prev[str & s->w_mask] = s->head[s->ins_h];
1549 #endif
1550 s->head[s->ins_h] = (Pos)str;
1551 str++;
1552 s->insert--;
1553 if (s->lookahead + s->insert < MIN_MATCH)
1554 break;
1555 }
1556 }
1557 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1558 * but this is not important since only literal bytes will be emitted.
1559 */
1560
1561 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1562
1563 /* If the WIN_INIT bytes after the end of the current data have never been
1564 * written, then zero those bytes in order to avoid memory check reports of
1565 * the use of uninitialized (or uninitialised as Julian writes) bytes by
1566 * the longest match routines. Update the high water mark for the next
1567 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1568 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1569 */
1570 if (s->high_water < s->window_size) {
1571 ulg curr = s->strstart + (ulg)(s->lookahead);
1572 ulg init;
1573
1574 if (s->high_water < curr) {
1575 /* Previous high water mark below current data -- zero WIN_INIT
1576 * bytes or up to end of window, whichever is less.
1577 */
1578 init = s->window_size - curr;
1579 if (init > WIN_INIT)
1580 init = WIN_INIT;
1581 zmemzero(s->window + curr, (unsigned)init);
1582 s->high_water = curr + init;
1583 }
1584 else if (s->high_water < (ulg)curr + WIN_INIT) {
1585 /* High water mark at or above current data, but below current data
1586 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1587 * to end of window, whichever is less.
1588 */
1589 init = (ulg)curr + WIN_INIT - s->high_water;
1590 if (init > s->window_size - s->high_water)
1591 init = s->window_size - s->high_water;
1592 zmemzero(s->window + s->high_water, (unsigned)init);
1593 s->high_water += init;
1594 }
1595 }
1596
1597 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1598 "not enough room for search");
1599 }
1600 1584
1601 /* =========================================================================== 1585 /* ===========================================================================
1602 * Flush the current block, with given end-of-file flag. 1586 * Flush the current block, with given end-of-file flag.
1603 * IN assertion: strstart is set to the end of the current match. 1587 * IN assertion: strstart is set to the end of the current match.
1604 */ 1588 */
1636 * allowed here, then the hash table will be cleared, since two or more slides 1620 * allowed here, then the hash table will be cleared, since two or more slides
1637 * is the same as a clear. 1621 * is the same as a clear.
1638 * 1622 *
1639 * deflate_stored() is written to minimize the number of times an input byte is 1623 * deflate_stored() is written to minimize the number of times an input byte is
1640 * copied. It is most efficient with large input and output buffers, which 1624 * copied. It is most efficient with large input and output buffers, which
1641 * maximizes the opportunites to have a single copy from next_in to next_out. 1625 * maximizes the opportunities to have a single copy from next_in to next_out.
1642 */ 1626 */
1643 local block_state deflate_stored(s, flush) 1627 local block_state deflate_stored(deflate_state *s, int flush) {
1644 deflate_state *s;
1645 int flush;
1646 {
1647 /* Smallest worthy block size when not flushing or finishing. By default 1628 /* Smallest worthy block size when not flushing or finishing. By default
1648 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 1629 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1649 * large input and output buffers, the stored block size will be larger. 1630 * large input and output buffers, the stored block size will be larger.
1650 */ 1631 */
1651 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); 1632 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1740 */ 1721 */
1741 if (used >= s->w_size) { /* supplant the previous history */ 1722 if (used >= s->w_size) { /* supplant the previous history */
1742 s->matches = 2; /* clear hash */ 1723 s->matches = 2; /* clear hash */
1743 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); 1724 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1744 s->strstart = s->w_size; 1725 s->strstart = s->w_size;
1726 s->insert = s->strstart;
1745 } 1727 }
1746 else { 1728 else {
1747 if (s->window_size - s->strstart <= used) { 1729 if (s->window_size - s->strstart <= used) {
1748 /* Slide the window down. */ 1730 /* Slide the window down. */
1749 s->strstart -= s->w_size; 1731 s->strstart -= s->w_size;
1750 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1732 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1751 if (s->matches < 2) 1733 if (s->matches < 2)
1752 s->matches++; /* add a pending slide_hash() */ 1734 s->matches++; /* add a pending slide_hash() */
1735 if (s->insert > s->strstart)
1736 s->insert = s->strstart;
1753 } 1737 }
1754 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); 1738 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1755 s->strstart += used; 1739 s->strstart += used;
1740 s->insert += MIN(used, s->w_size - s->insert);
1756 } 1741 }
1757 s->block_start = s->strstart; 1742 s->block_start = s->strstart;
1758 s->insert += MIN(used, s->w_size - s->insert);
1759 } 1743 }
1760 if (s->high_water < s->strstart) 1744 if (s->high_water < s->strstart)
1761 s->high_water = s->strstart; 1745 s->high_water = s->strstart;
1762 1746
1763 /* If the last block was written to next_out, then done. */ 1747 /* If the last block was written to next_out, then done. */
1768 if (flush != Z_NO_FLUSH && flush != Z_FINISH && 1752 if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1769 s->strm->avail_in == 0 && (long)s->strstart == s->block_start) 1753 s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1770 return block_done; 1754 return block_done;
1771 1755
1772 /* Fill the window with any remaining input. */ 1756 /* Fill the window with any remaining input. */
1773 have = s->window_size - s->strstart - 1; 1757 have = s->window_size - s->strstart;
1774 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { 1758 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1775 /* Slide the window down. */ 1759 /* Slide the window down. */
1776 s->block_start -= s->w_size; 1760 s->block_start -= s->w_size;
1777 s->strstart -= s->w_size; 1761 s->strstart -= s->w_size;
1778 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1762 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1779 if (s->matches < 2) 1763 if (s->matches < 2)
1780 s->matches++; /* add a pending slide_hash() */ 1764 s->matches++; /* add a pending slide_hash() */
1781 have += s->w_size; /* more space now */ 1765 have += s->w_size; /* more space now */
1766 if (s->insert > s->strstart)
1767 s->insert = s->strstart;
1782 } 1768 }
1783 if (have > s->strm->avail_in) 1769 if (have > s->strm->avail_in)
1784 have = s->strm->avail_in; 1770 have = s->strm->avail_in;
1785 if (have) { 1771 if (have) {
1786 read_buf(s->strm, s->window + s->strstart, have); 1772 read_buf(s->strm, s->window + s->strstart, have);
1787 s->strstart += have; 1773 s->strstart += have;
1774 s->insert += MIN(have, s->w_size - s->insert);
1788 } 1775 }
1789 if (s->high_water < s->strstart) 1776 if (s->high_water < s->strstart)
1790 s->high_water = s->strstart; 1777 s->high_water = s->strstart;
1791 1778
1792 /* There was not enough avail_out to write a complete worthy or flushed 1779 /* There was not enough avail_out to write a complete worthy or flushed
1819 * block state. 1806 * block state.
1820 * This function does not perform lazy evaluation of matches and inserts 1807 * This function does not perform lazy evaluation of matches and inserts
1821 * new strings in the dictionary only for unmatched strings or for short 1808 * new strings in the dictionary only for unmatched strings or for short
1822 * matches. It is used only for the fast compression options. 1809 * matches. It is used only for the fast compression options.
1823 */ 1810 */
1824 local block_state deflate_fast(s, flush) 1811 local block_state deflate_fast(deflate_state *s, int flush) {
1825 deflate_state *s;
1826 int flush;
1827 {
1828 IPos hash_head; /* head of the hash chain */ 1812 IPos hash_head; /* head of the hash chain */
1829 int bflush; /* set if current block must be flushed */ 1813 int bflush; /* set if current block must be flushed */
1830 1814
1831 for (;;) { 1815 for (;;) {
1832 /* Make sure that we always have enough lookahead, except 1816 /* Make sure that we always have enough lookahead, except
1840 return need_more; 1824 return need_more;
1841 } 1825 }
1842 if (s->lookahead == 0) break; /* flush the current block */ 1826 if (s->lookahead == 0) break; /* flush the current block */
1843 } 1827 }
1844 1828
1845 /* Insert the string window[strstart .. strstart+2] in the 1829 /* Insert the string window[strstart .. strstart + 2] in the
1846 * dictionary, and set hash_head to the head of the hash chain: 1830 * dictionary, and set hash_head to the head of the hash chain:
1847 */ 1831 */
1848 hash_head = NIL; 1832 hash_head = NIL;
1849 if (s->lookahead >= MIN_MATCH) { 1833 if (s->lookahead >= MIN_MATCH) {
1850 INSERT_STRING(s, s->strstart, hash_head); 1834 INSERT_STRING(s, s->strstart, hash_head);
1888 #endif 1872 #endif
1889 { 1873 {
1890 s->strstart += s->match_length; 1874 s->strstart += s->match_length;
1891 s->match_length = 0; 1875 s->match_length = 0;
1892 s->ins_h = s->window[s->strstart]; 1876 s->ins_h = s->window[s->strstart];
1893 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1877 UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
1894 #if MIN_MATCH != 3 1878 #if MIN_MATCH != 3
1895 Call UPDATE_HASH() MIN_MATCH-3 more times 1879 Call UPDATE_HASH() MIN_MATCH-3 more times
1896 #endif 1880 #endif
1897 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1881 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1898 * matter since it will be recomputed at next deflate call. 1882 * matter since it will be recomputed at next deflate call.
1899 */ 1883 */
1900 } 1884 }
1901 } else { 1885 } else {
1902 /* No match, output a literal byte */ 1886 /* No match, output a literal byte */
1903 Tracevv((stderr,"%c", s->window[s->strstart])); 1887 Tracevv((stderr,"%c", s->window[s->strstart]));
1904 _tr_tally_lit (s, s->window[s->strstart], bflush); 1888 _tr_tally_lit(s, s->window[s->strstart], bflush);
1905 s->lookahead--; 1889 s->lookahead--;
1906 s->strstart++; 1890 s->strstart++;
1907 } 1891 }
1908 if (bflush) FLUSH_BLOCK(s, 0); 1892 if (bflush) FLUSH_BLOCK(s, 0);
1909 } 1893 }
1910 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 1894 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1911 if (flush == Z_FINISH) { 1895 if (flush == Z_FINISH) {
1912 FLUSH_BLOCK(s, 1); 1896 FLUSH_BLOCK(s, 1);
1913 return finish_done; 1897 return finish_done;
1914 } 1898 }
1915 if (s->last_lit) 1899 if (s->sym_next)
1916 FLUSH_BLOCK(s, 0); 1900 FLUSH_BLOCK(s, 0);
1917 return block_done; 1901 return block_done;
1918 } 1902 }
1919 1903
1920 #ifndef FASTEST 1904 #ifndef FASTEST
1921 /* =========================================================================== 1905 /* ===========================================================================
1922 * Same as above, but achieves better compression. We use a lazy 1906 * Same as above, but achieves better compression. We use a lazy
1923 * evaluation for matches: a match is finally adopted only if there is 1907 * evaluation for matches: a match is finally adopted only if there is
1924 * no better match at the next window position. 1908 * no better match at the next window position.
1925 */ 1909 */
1926 local block_state deflate_slow(s, flush) 1910 local block_state deflate_slow(deflate_state *s, int flush) {
1927 deflate_state *s;
1928 int flush;
1929 {
1930 IPos hash_head; /* head of hash chain */ 1911 IPos hash_head; /* head of hash chain */
1931 int bflush; /* set if current block must be flushed */ 1912 int bflush; /* set if current block must be flushed */
1932 1913
1933 /* Process the input block. */ 1914 /* Process the input block. */
1934 for (;;) { 1915 for (;;) {
1943 return need_more; 1924 return need_more;
1944 } 1925 }
1945 if (s->lookahead == 0) break; /* flush the current block */ 1926 if (s->lookahead == 0) break; /* flush the current block */
1946 } 1927 }
1947 1928
1948 /* Insert the string window[strstart .. strstart+2] in the 1929 /* Insert the string window[strstart .. strstart + 2] in the
1949 * dictionary, and set hash_head to the head of the hash chain: 1930 * dictionary, and set hash_head to the head of the hash chain:
1950 */ 1931 */
1951 hash_head = NIL; 1932 hash_head = NIL;
1952 if (s->lookahead >= MIN_MATCH) { 1933 if (s->lookahead >= MIN_MATCH) {
1953 INSERT_STRING(s, s->strstart, hash_head); 1934 INSERT_STRING(s, s->strstart, hash_head);
1985 */ 1966 */
1986 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 1967 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1987 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 1968 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1988 /* Do not insert strings in hash table beyond this. */ 1969 /* Do not insert strings in hash table beyond this. */
1989 1970
1990 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1971 check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
1991 1972
1992 _tr_tally_dist(s, s->strstart -1 - s->prev_match, 1973 _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
1993 s->prev_length - MIN_MATCH, bflush); 1974 s->prev_length - MIN_MATCH, bflush);
1994 1975
1995 /* Insert in hash table all strings up to the end of the match. 1976 /* Insert in hash table all strings up to the end of the match.
1996 * strstart-1 and strstart are already inserted. If there is not 1977 * strstart - 1 and strstart are already inserted. If there is not
1997 * enough lookahead, the last two strings are not inserted in 1978 * enough lookahead, the last two strings are not inserted in
1998 * the hash table. 1979 * the hash table.
1999 */ 1980 */
2000 s->lookahead -= s->prev_length-1; 1981 s->lookahead -= s->prev_length - 1;
2001 s->prev_length -= 2; 1982 s->prev_length -= 2;
2002 do { 1983 do {
2003 if (++s->strstart <= max_insert) { 1984 if (++s->strstart <= max_insert) {
2004 INSERT_STRING(s, s->strstart, hash_head); 1985 INSERT_STRING(s, s->strstart, hash_head);
2005 } 1986 }
2013 } else if (s->match_available) { 1994 } else if (s->match_available) {
2014 /* If there was no match at the previous position, output a 1995 /* If there was no match at the previous position, output a
2015 * single literal. If there was a match but the current match 1996 * single literal. If there was a match but the current match
2016 * is longer, truncate the previous match to a single literal. 1997 * is longer, truncate the previous match to a single literal.
2017 */ 1998 */
2018 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1999 Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2019 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2000 _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2020 if (bflush) { 2001 if (bflush) {
2021 FLUSH_BLOCK_ONLY(s, 0); 2002 FLUSH_BLOCK_ONLY(s, 0);
2022 } 2003 }
2023 s->strstart++; 2004 s->strstart++;
2024 s->lookahead--; 2005 s->lookahead--;
2032 s->lookahead--; 2013 s->lookahead--;
2033 } 2014 }
2034 } 2015 }
2035 Assert (flush != Z_NO_FLUSH, "no flush?"); 2016 Assert (flush != Z_NO_FLUSH, "no flush?");
2036 if (s->match_available) { 2017 if (s->match_available) {
2037 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2018 Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2038 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2019 _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2039 s->match_available = 0; 2020 s->match_available = 0;
2040 } 2021 }
2041 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 2022 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2042 if (flush == Z_FINISH) { 2023 if (flush == Z_FINISH) {
2043 FLUSH_BLOCK(s, 1); 2024 FLUSH_BLOCK(s, 1);
2044 return finish_done; 2025 return finish_done;
2045 } 2026 }
2046 if (s->last_lit) 2027 if (s->sym_next)
2047 FLUSH_BLOCK(s, 0); 2028 FLUSH_BLOCK(s, 0);
2048 return block_done; 2029 return block_done;
2049 } 2030 }
2050 #endif /* FASTEST */ 2031 #endif /* FASTEST */
2051 2032
2052 /* =========================================================================== 2033 /* ===========================================================================
2053 * For Z_RLE, simply look for runs of bytes, generate matches only of distance 2034 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2054 * one. Do not maintain a hash table. (It will be regenerated if this run of 2035 * one. Do not maintain a hash table. (It will be regenerated if this run of
2055 * deflate switches away from Z_RLE.) 2036 * deflate switches away from Z_RLE.)
2056 */ 2037 */
2057 local block_state deflate_rle(s, flush) 2038 local block_state deflate_rle(deflate_state *s, int flush) {
2058 deflate_state *s;
2059 int flush;
2060 {
2061 int bflush; /* set if current block must be flushed */ 2039 int bflush; /* set if current block must be flushed */
2062 uInt prev; /* byte at distance one to match */ 2040 uInt prev; /* byte at distance one to match */
2063 Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2041 Bytef *scan, *strend; /* scan goes up to strend for length of run */
2064 2042
2065 for (;;) { 2043 for (;;) {
2090 scan < strend); 2068 scan < strend);
2091 s->match_length = MAX_MATCH - (uInt)(strend - scan); 2069 s->match_length = MAX_MATCH - (uInt)(strend - scan);
2092 if (s->match_length > s->lookahead) 2070 if (s->match_length > s->lookahead)
2093 s->match_length = s->lookahead; 2071 s->match_length = s->lookahead;
2094 } 2072 }
2095 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); 2073 Assert(scan <= s->window + (uInt)(s->window_size - 1),
2074 "wild scan");
2096 } 2075 }
2097 2076
2098 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 2077 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2099 if (s->match_length >= MIN_MATCH) { 2078 if (s->match_length >= MIN_MATCH) {
2100 check_match(s, s->strstart, s->strstart - 1, s->match_length); 2079 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2105 s->strstart += s->match_length; 2084 s->strstart += s->match_length;
2106 s->match_length = 0; 2085 s->match_length = 0;
2107 } else { 2086 } else {
2108 /* No match, output a literal byte */ 2087 /* No match, output a literal byte */
2109 Tracevv((stderr,"%c", s->window[s->strstart])); 2088 Tracevv((stderr,"%c", s->window[s->strstart]));
2110 _tr_tally_lit (s, s->window[s->strstart], bflush); 2089 _tr_tally_lit(s, s->window[s->strstart], bflush);
2111 s->lookahead--; 2090 s->lookahead--;
2112 s->strstart++; 2091 s->strstart++;
2113 } 2092 }
2114 if (bflush) FLUSH_BLOCK(s, 0); 2093 if (bflush) FLUSH_BLOCK(s, 0);
2115 } 2094 }
2116 s->insert = 0; 2095 s->insert = 0;
2117 if (flush == Z_FINISH) { 2096 if (flush == Z_FINISH) {
2118 FLUSH_BLOCK(s, 1); 2097 FLUSH_BLOCK(s, 1);
2119 return finish_done; 2098 return finish_done;
2120 } 2099 }
2121 if (s->last_lit) 2100 if (s->sym_next)
2122 FLUSH_BLOCK(s, 0); 2101 FLUSH_BLOCK(s, 0);
2123 return block_done; 2102 return block_done;
2124 } 2103 }
2125 2104
2126 /* =========================================================================== 2105 /* ===========================================================================
2127 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 2106 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2128 * (It will be regenerated if this run of deflate switches away from Huffman.) 2107 * (It will be regenerated if this run of deflate switches away from Huffman.)
2129 */ 2108 */
2130 local block_state deflate_huff(s, flush) 2109 local block_state deflate_huff(deflate_state *s, int flush) {
2131 deflate_state *s;
2132 int flush;
2133 {
2134 int bflush; /* set if current block must be flushed */ 2110 int bflush; /* set if current block must be flushed */
2135 2111
2136 for (;;) { 2112 for (;;) {
2137 /* Make sure that we have a literal to write. */ 2113 /* Make sure that we have a literal to write. */
2138 if (s->lookahead == 0) { 2114 if (s->lookahead == 0) {
2145 } 2121 }
2146 2122
2147 /* Output a literal byte */ 2123 /* Output a literal byte */
2148 s->match_length = 0; 2124 s->match_length = 0;
2149 Tracevv((stderr,"%c", s->window[s->strstart])); 2125 Tracevv((stderr,"%c", s->window[s->strstart]));
2150 _tr_tally_lit (s, s->window[s->strstart], bflush); 2126 _tr_tally_lit(s, s->window[s->strstart], bflush);
2151 s->lookahead--; 2127 s->lookahead--;
2152 s->strstart++; 2128 s->strstart++;
2153 if (bflush) FLUSH_BLOCK(s, 0); 2129 if (bflush) FLUSH_BLOCK(s, 0);
2154 } 2130 }
2155 s->insert = 0; 2131 s->insert = 0;
2156 if (flush == Z_FINISH) { 2132 if (flush == Z_FINISH) {
2157 FLUSH_BLOCK(s, 1); 2133 FLUSH_BLOCK(s, 1);
2158 return finish_done; 2134 return finish_done;
2159 } 2135 }
2160 if (s->last_lit) 2136 if (s->sym_next)
2161 FLUSH_BLOCK(s, 0); 2137 FLUSH_BLOCK(s, 0);
2162 return block_done; 2138 return block_done;
2163 } 2139 }