summaryrefslogtreecommitdiff
path: root/deflate.c
diff options
context:
space:
mode:
Diffstat (limited to 'deflate.c')
-rw-r--r--deflate.c230
1 files changed, 135 insertions, 95 deletions
diff --git a/deflate.c b/deflate.c
index 46ddc55..64710e6 100644
--- a/deflate.c
+++ b/deflate.c
@@ -1,5 +1,5 @@
1/* deflate.c -- compress data using the deflation algorithm 1/* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
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
@@ -47,11 +47,11 @@
47 * 47 *
48 */ 48 */
49 49
50/* $Id: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp $ */ 50/* $Id: deflate.c,v 1.12 1996/01/30 21:59:11 me Exp $ */
51 51
52#include "deflate.h" 52#include "deflate.h"
53 53
54char deflate_copyright[] = " deflate 1.0 Copyright 1995 Jean-loup Gailly "; 54char deflate_copyright[] = " deflate 1.0.1 Copyright 1995-1996 Jean-loup Gailly ";
55/* 55/*
56 If you use the zlib library in a product, an acknowledgment is welcome 56 If you use the zlib library in a product, an acknowledgment is welcome
57 in the documentation of your product. If for some reason you cannot 57 in the documentation of your product. If for some reason you cannot
@@ -60,15 +60,14 @@ char deflate_copyright[] = " deflate 1.0 Copyright 1995 Jean-loup Gailly ";
60 */ 60 */
61 61
62/* =========================================================================== 62/* ===========================================================================
63 * Prototypes for local functions. 63 * Function prototypes.
64 */ 64 */
65
66local void fill_window OF((deflate_state *s)); 65local void fill_window OF((deflate_state *s));
67local int deflate_stored OF((deflate_state *s, int flush)); 66local int deflate_stored OF((deflate_state *s, int flush));
68local int deflate_fast OF((deflate_state *s, int flush)); 67local int deflate_fast OF((deflate_state *s, int flush));
69local int deflate_slow OF((deflate_state *s, int flush)); 68local int deflate_slow OF((deflate_state *s, int flush));
70local void lm_init OF((deflate_state *s)); 69local void lm_init OF((deflate_state *s));
71local int longest_match OF((deflate_state *s, IPos cur_match)); 70local uInt longest_match OF((deflate_state *s, IPos cur_match));
72local void putShortMSB OF((deflate_state *s, uInt b)); 71local void putShortMSB OF((deflate_state *s, uInt b));
73local void flush_pending OF((z_stream *strm)); 72local void flush_pending OF((z_stream *strm));
74local int read_buf OF((z_stream *strm, charf *buf, unsigned size)); 73local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
@@ -158,7 +157,7 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
158#define INSERT_STRING(s, str, match_head) \ 157#define INSERT_STRING(s, str, match_head) \
159 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 158 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
160 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \ 159 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
161 s->head[s->ins_h] = (str)) 160 s->head[s->ins_h] = (Pos)(str))
162 161
163/* =========================================================================== 162/* ===========================================================================
164 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 163 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
@@ -169,26 +168,41 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
169 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 168 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
170 169
171/* ========================================================================= */ 170/* ========================================================================= */
172int deflateInit (strm, level) 171int deflateInit_(strm, level, version, stream_size)
173 z_stream *strm; 172 z_stream *strm;
174 int level; 173 int level;
174 const char *version;
175 int stream_size;
175{ 176{
176 return deflateInit2 (strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0); 177 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
178 Z_DEFAULT_STRATEGY, version, stream_size);
177 /* To do: ignore strm->next_in if we use it as window */ 179 /* To do: ignore strm->next_in if we use it as window */
178} 180}
179 181
180/* ========================================================================= */ 182/* ========================================================================= */
181int deflateInit2 (strm, level, method, windowBits, memLevel, strategy) 183int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
184 version, stream_size)
182 z_stream *strm; 185 z_stream *strm;
183 int level; 186 int level;
184 int method; 187 int method;
185 int windowBits; 188 int windowBits;
186 int memLevel; 189 int memLevel;
187 int strategy; 190 int strategy;
191 const char *version;
192 int stream_size;
188{ 193{
189 deflate_state *s; 194 deflate_state *s;
190 int noheader = 0; 195 int noheader = 0;
191 196
197 ushf *overlay;
198 /* We overlay pending_buf and d_buf+l_buf. This works since the average
199 * output size for (length,distance) codes is <= 24 bits.
200 */
201
202 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
203 stream_size != sizeof(z_stream)) {
204 return Z_VERSION_ERROR;
205 }
192 if (strm == Z_NULL) return Z_STREAM_ERROR; 206 if (strm == Z_NULL) return Z_STREAM_ERROR;
193 207
194 strm->msg = Z_NULL; 208 strm->msg = Z_NULL;
@@ -230,20 +244,17 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
230 244
231 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 245 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
232 246
233 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush)); 247 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
248 s->pending_buf = (uchf *) overlay;
234 249
235 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 250 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
236 s->pending_buf == Z_NULL) { 251 s->pending_buf == Z_NULL) {
237 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 252 strm->msg = ERR_MSG(Z_MEM_ERROR);
238 deflateEnd (strm); 253 deflateEnd (strm);
239 return Z_MEM_ERROR; 254 return Z_MEM_ERROR;
240 } 255 }
241 s->l_buf = (uchf *) &(s->pending_buf[s->lit_bufsize]); 256 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
242 s->d_buf = (ushf *) &(s->pending_buf[2*s->lit_bufsize]); 257 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
243 /* We overlay pending_buf and d_buf+l_buf. This works since the average
244 * output size for (length,distance) codes is <= 32 bits (worst case
245 * is 15+15+13=33). d_buf is put last in case sizeof(short)>2.
246 */
247 258
248 s->level = level; 259 s->level = level;
249 s->strategy = strategy; 260 s->strategy = strategy;
@@ -252,32 +263,42 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
252 return deflateReset(strm); 263 return deflateReset(strm);
253} 264}
254 265
255/* ========================================================================= 266/* ========================================================================= */
256 * Undocumented function to return the Adler32 of a stream. It can be 267int deflateSetDictionary (strm, dictionary, dictLength)
257 * called immediately after a flush with Z_SYNC_FLUSH, to allow later
258 * resumption of the compressor with deflateSetAdler32.
259 */
260uLong deflateGetAdler32 (strm)
261 z_stream *strm; 268 z_stream *strm;
269 const Bytef *dictionary;
270 uInt dictLength;
262{ 271{
263 if (strm == Z_NULL || strm->state == Z_NULL) return (uLong)Z_STREAM_ERROR; 272 deflate_state *s;
264 /* Z_STREAM_ERROR happens to be an invalid Adler32 value. */ 273 uInt length = dictLength;
274 uInt n;
275 IPos hash_head = 0;
265 276
266 return strm->state->adler; 277 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
267} 278 strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
268 279
269/* ========================================================================= 280 s = strm->state;
270 * Undocumented function to set the Adler32 of a stream. It can be called 281 strm->adler = adler32(strm->adler, dictionary, dictLength);
271 * immediately after deflateInit to reset the Adler32 of a compression
272 * stream which had been interrupted.
273 */
274int deflateSetAdler32 (strm, adler)
275 z_stream *strm;
276 uLong adler;
277{
278 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
279 282
280 strm->state->adler = adler; 283 if (length < MIN_MATCH) return Z_OK;
284 if (length > MAX_DIST(s)) {
285 length = MAX_DIST(s);
286 dictionary += dictLength - length;
287 }
288 zmemcpy((charf *)s->window, dictionary, length);
289 s->strstart = length;
290 s->block_start = (long)length;
291
292 /* Insert all strings in the hash table (except for the last two bytes).
293 * s->lookahead stays null, so s->ins_h will be recomputed at the next
294 * call of fill_window.
295 */
296 s->ins_h = s->window[0];
297 UPDATE_HASH(s, s->ins_h, s->window[1]);
298 for (n = 0; n <= length - MIN_MATCH; n++) {
299 INSERT_STRING(s, n, hash_head);
300 }
301 if (hash_head) hash_head = 0; /* to make compiler happy */
281 return Z_OK; 302 return Z_OK;
282} 303}
283 304
@@ -302,10 +323,10 @@ int deflateReset (strm)
302 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */ 323 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
303 } 324 }
304 s->status = s->noheader ? BUSY_STATE : INIT_STATE; 325 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
305 s->adler = 1; 326 strm->adler = 1;
306 s->last_flush = Z_NO_FLUSH; 327 s->last_flush = Z_NO_FLUSH;
307 328
308 tr_init(s); 329 _tr_init(s);
309 lm_init(s); 330 lm_init(s);
310 331
311 return Z_OK; 332 return Z_OK;
@@ -319,6 +340,7 @@ int deflateParams(strm, level, strategy)
319{ 340{
320 deflate_state *s; 341 deflate_state *s;
321 compress_func func; 342 compress_func func;
343 int err = Z_OK;
322 344
323 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 345 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
324 s = strm->state; 346 s = strm->state;
@@ -331,11 +353,9 @@ int deflateParams(strm, level, strategy)
331 } 353 }
332 func = configuration_table[s->level].func; 354 func = configuration_table[s->level].func;
333 355
334 if (func != configuration_table[level].func 356 if (func != configuration_table[level].func && strm->total_in != 0) {
335 && strm->state->lookahead != 0) {
336
337 /* Flush the last buffer: */ 357 /* Flush the last buffer: */
338 (void)(*func)(strm->state, Z_PARTIAL_FLUSH); 358 err = deflate(strm, Z_PARTIAL_FLUSH);
339 } 359 }
340 if (s->level != level) { 360 if (s->level != level) {
341 s->level = level; 361 s->level = level;
@@ -345,7 +365,7 @@ int deflateParams(strm, level, strategy)
345 s->max_chain_length = configuration_table[level].max_chain; 365 s->max_chain_length = configuration_table[level].max_chain;
346 } 366 }
347 s->strategy = strategy; 367 s->strategy = strategy;
348 return Z_OK; 368 return err;
349} 369}
350 370
351/* ========================================================================= 371/* =========================================================================
@@ -392,36 +412,47 @@ int deflate (strm, flush)
392 int flush; 412 int flush;
393{ 413{
394 int old_flush; /* value of flush param for previous deflate call */ 414 int old_flush; /* value of flush param for previous deflate call */
415 deflate_state *s;
395 416
396 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 417 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
397 418
419 s = strm->state;
420
398 if (strm->next_out == Z_NULL || 421 if (strm->next_out == Z_NULL ||
399 (strm->next_in == Z_NULL && strm->avail_in != 0) || 422 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
400 (strm->state->status == FINISH_STATE && flush != Z_FINISH)) { 423 (s->status == FINISH_STATE && flush != Z_FINISH)) {
401 ERR_RETURN(strm, Z_STREAM_ERROR); 424 ERR_RETURN(strm, Z_STREAM_ERROR);
402 } 425 }
403 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 426 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
404 427
405 strm->state->strm = strm; /* just in case */ 428 s->strm = strm; /* just in case */
406 old_flush = strm->state->last_flush; 429 old_flush = s->last_flush;
407 strm->state->last_flush = flush; 430 s->last_flush = flush;
408 431
409 /* Write the zlib header */ 432 /* Write the zlib header */
410 if (strm->state->status == INIT_STATE) { 433 if (s->status == INIT_STATE) {
411 434
412 uInt header = (Z_DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; 435 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
413 uInt level_flags = (strm->state->level-1) >> 1; 436 uInt level_flags = (s->level-1) >> 1;
414 437
415 if (level_flags > 3) level_flags = 3; 438 if (level_flags > 3) level_flags = 3;
416 header |= (level_flags << 6); 439 header |= (level_flags << 6);
440 if (s->strstart != 0) header |= PRESET_DICT;
417 header += 31 - (header % 31); 441 header += 31 - (header % 31);
418 442
419 strm->state->status = BUSY_STATE; 443 s->status = BUSY_STATE;
420 putShortMSB(strm->state, header); 444 putShortMSB(s, header);
445
446 /* Save the adler32 of the preset dictionary: */
447 if (s->strstart != 0) {
448 putShortMSB(s, (uInt)(strm->adler >> 16));
449 putShortMSB(s, (uInt)(strm->adler & 0xffff));
450 strm->adler = 1L;
451 }
421 } 452 }
422 453
423 /* Flush as much pending output as possible */ 454 /* Flush as much pending output as possible */
424 if (strm->state->pending != 0) { 455 if (s->pending != 0) {
425 flush_pending(strm); 456 flush_pending(strm);
426 if (strm->avail_out == 0) return Z_OK; 457 if (strm->avail_out == 0) return Z_OK;
427 458
@@ -435,21 +466,20 @@ int deflate (strm, flush)
435 } 466 }
436 467
437 /* User must not provide more input after the first FINISH: */ 468 /* User must not provide more input after the first FINISH: */
438 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { 469 if (s->status == FINISH_STATE && strm->avail_in != 0) {
439 ERR_RETURN(strm, Z_BUF_ERROR); 470 ERR_RETURN(strm, Z_BUF_ERROR);
440 } 471 }
441 472
442 /* Start a new block or continue the current one. 473 /* Start a new block or continue the current one.
443 */ 474 */
444 if (strm->avail_in != 0 || strm->state->lookahead != 0 || 475 if (strm->avail_in != 0 || s->lookahead != 0 ||
445 (flush != Z_NO_FLUSH && strm->state->status != FINISH_STATE)) { 476 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
446 int quit; 477 int quit;
447 478
448 if (flush == Z_FINISH) { 479 if (flush == Z_FINISH) {
449 strm->state->status = FINISH_STATE; 480 s->status = FINISH_STATE;
450 } 481 }
451 quit = (*(configuration_table[strm->state->level].func)) 482 quit = (*(configuration_table[s->level].func))(s, flush);
452 (strm->state, flush);
453 483
454 if (quit || strm->avail_out == 0) return Z_OK; 484 if (quit || strm->avail_out == 0) return Z_OK;
455 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 485 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
@@ -461,14 +491,14 @@ int deflate (strm, flush)
461 */ 491 */
462 if (flush != Z_NO_FLUSH && flush != Z_FINISH) { 492 if (flush != Z_NO_FLUSH && flush != Z_FINISH) {
463 if (flush == Z_PARTIAL_FLUSH) { 493 if (flush == Z_PARTIAL_FLUSH) {
464 tr_align(strm->state); 494 _tr_align(s);
465 } else { /* FULL_FLUSH or SYNC_FLUSH */ 495 } else { /* FULL_FLUSH or SYNC_FLUSH */
466 tr_stored_block(strm->state, (char*)0, 0L, 0); 496 _tr_stored_block(s, (char*)0, 0L, 0);
467 /* For a full flush, this empty block will be recognized 497 /* For a full flush, this empty block will be recognized
468 * as a special marker by inflate_sync(). 498 * as a special marker by inflate_sync().
469 */ 499 */
470 if (flush == Z_FULL_FLUSH) { 500 if (flush == Z_FULL_FLUSH) {
471 CLEAR_HASH(strm->state); /* forget history */ 501 CLEAR_HASH(s); /* forget history */
472 } 502 }
473 } 503 }
474 flush_pending(strm); 504 flush_pending(strm);
@@ -478,23 +508,25 @@ int deflate (strm, flush)
478 Assert(strm->avail_out > 0, "bug2"); 508 Assert(strm->avail_out > 0, "bug2");
479 509
480 if (flush != Z_FINISH) return Z_OK; 510 if (flush != Z_FINISH) return Z_OK;
481 if (strm->state->noheader) return Z_STREAM_END; 511 if (s->noheader) return Z_STREAM_END;
482 512
483 /* Write the zlib trailer (adler32) */ 513 /* Write the zlib trailer (adler32) */
484 putShortMSB(strm->state, (uInt)(strm->state->adler >> 16)); 514 putShortMSB(s, (uInt)(strm->adler >> 16));
485 putShortMSB(strm->state, (uInt)(strm->state->adler & 0xffff)); 515 putShortMSB(s, (uInt)(strm->adler & 0xffff));
486 flush_pending(strm); 516 flush_pending(strm);
487 /* If avail_out is zero, the application will call deflate again 517 /* If avail_out is zero, the application will call deflate again
488 * to flush the rest. 518 * to flush the rest.
489 */ 519 */
490 strm->state->noheader = -1; /* write the trailer only once! */ 520 s->noheader = -1; /* write the trailer only once! */
491 return strm->state->pending != 0 ? Z_OK : Z_STREAM_END; 521 return s->pending != 0 ? Z_OK : Z_STREAM_END;
492} 522}
493 523
494/* ========================================================================= */ 524/* ========================================================================= */
495int deflateEnd (strm) 525int deflateEnd (strm)
496 z_stream *strm; 526 z_stream *strm;
497{ 527{
528 int status;
529
498 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 530 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
499 531
500 /* Deallocate in reverse order of allocations: */ 532 /* Deallocate in reverse order of allocations: */
@@ -503,10 +535,11 @@ int deflateEnd (strm)
503 TRY_FREE(strm, strm->state->prev); 535 TRY_FREE(strm, strm->state->prev);
504 TRY_FREE(strm, strm->state->window); 536 TRY_FREE(strm, strm->state->window);
505 537
538 status = strm->state->status;
506 ZFREE(strm, strm->state); 539 ZFREE(strm, strm->state);
507 strm->state = Z_NULL; 540 strm->state = Z_NULL;
508 541
509 return Z_OK; 542 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
510} 543}
511 544
512/* ========================================================================= */ 545/* ========================================================================= */
@@ -549,7 +582,7 @@ local int read_buf(strm, buf, size)
549 strm->avail_in -= len; 582 strm->avail_in -= len;
550 583
551 if (!strm->state->noheader) { 584 if (!strm->state->noheader) {
552 strm->state->adler = adler32(strm->state->adler, strm->next_in, len); 585 strm->adler = adler32(strm->adler, strm->next_in, len);
553 } 586 }
554 zmemcpy(buf, strm->next_in, len); 587 zmemcpy(buf, strm->next_in, len);
555 strm->next_in += len; 588 strm->next_in += len;
@@ -593,12 +626,13 @@ local void lm_init (s)
593 * garbage. 626 * garbage.
594 * IN assertions: cur_match is the head of the hash chain for the current 627 * IN assertions: cur_match is the head of the hash chain for the current
595 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 628 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
629 * OUT assertion: the match length is not greater than s->lookahead.
596 */ 630 */
597#ifndef ASMV 631#ifndef ASMV
598/* For 80x86 and 680x0, an optimized version will be provided in match.asm or 632/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
599 * match.S. The code will be functionally equivalent. 633 * match.S. The code will be functionally equivalent.
600 */ 634 */
601local int longest_match(s, cur_match) 635local uInt longest_match(s, cur_match)
602 deflate_state *s; 636 deflate_state *s;
603 IPos cur_match; /* current match */ 637 IPos cur_match; /* current match */
604{ 638{
@@ -607,6 +641,7 @@ local int longest_match(s, cur_match)
607 register Bytef *match; /* matched string */ 641 register Bytef *match; /* matched string */
608 register int len; /* length of current match */ 642 register int len; /* length of current match */
609 int best_len = s->prev_length; /* best match length so far */ 643 int best_len = s->prev_length; /* best match length so far */
644 int nice_match = s->nice_match; /* stop if match long enough */
610 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 645 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
611 s->strstart - (IPos)MAX_DIST(s) : NIL; 646 s->strstart - (IPos)MAX_DIST(s) : NIL;
612 /* Stop when cur_match becomes <= limit. To simplify the code, 647 /* Stop when cur_match becomes <= limit. To simplify the code,
@@ -637,6 +672,11 @@ local int longest_match(s, cur_match)
637 if (s->prev_length >= s->good_match) { 672 if (s->prev_length >= s->good_match) {
638 chain_length >>= 2; 673 chain_length >>= 2;
639 } 674 }
675 /* Do not look for matches beyond the end of the input. This is necessary
676 * to make deflate deterministic.
677 */
678 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
679
640 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 680 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
641 681
642 do { 682 do {
@@ -715,7 +755,7 @@ local int longest_match(s, cur_match)
715 if (len > best_len) { 755 if (len > best_len) {
716 s->match_start = cur_match; 756 s->match_start = cur_match;
717 best_len = len; 757 best_len = len;
718 if (len >= s->nice_match) break; 758 if (len >= nice_match) break;
719#ifdef UNALIGNED_OK 759#ifdef UNALIGNED_OK
720 scan_end = *(ushf*)(scan+best_len-1); 760 scan_end = *(ushf*)(scan+best_len-1);
721#else 761#else
@@ -726,7 +766,8 @@ local int longest_match(s, cur_match)
726 } while ((cur_match = prev[cur_match & wmask]) > limit 766 } while ((cur_match = prev[cur_match & wmask]) > limit
727 && --chain_length != 0); 767 && --chain_length != 0);
728 768
729 return best_len; 769 if ((uInt)best_len <= s->lookahead) return best_len;
770 return s->lookahead;
730} 771}
731#endif /* ASMV */ 772#endif /* ASMV */
732 773
@@ -740,13 +781,13 @@ local void check_match(s, start, match, length)
740 int length; 781 int length;
741{ 782{
742 /* check that the match is indeed a match */ 783 /* check that the match is indeed a match */
743 if (memcmp((charf *)s->window + match, 784 if (zmemcmp((charf *)s->window + match,
744 (charf *)s->window + start, length) != EQUAL) { 785 (charf *)s->window + start, length) != EQUAL) {
745 fprintf(stderr, 786 fprintf(stderr, " start %u, match %u, length %d\n",
746 " start %u, match %u, length %d\n", 787 start, match, length);
747 start, match, length); 788 do {
748 do { fprintf(stderr, "%c%c", s->window[match++], 789 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
749 s->window[start++]); } while (--length != 0); 790 } while (--length != 0);
750 z_error("invalid match"); 791 z_error("invalid match");
751 } 792 }
752 if (verbose > 1) { 793 if (verbose > 1) {
@@ -862,9 +903,11 @@ local void fill_window(s)
862 * IN assertion: strstart is set to the end of the current match. 903 * IN assertion: strstart is set to the end of the current match.
863 */ 904 */
864#define FLUSH_BLOCK_ONLY(s, eof) { \ 905#define FLUSH_BLOCK_ONLY(s, eof) { \
865 tr_flush_block(s, (s->block_start >= 0L ? \ 906 _tr_flush_block(s, (s->block_start >= 0L ? \
866 (charf *)&s->window[(unsigned)s->block_start] : \ 907 (charf *)&s->window[(unsigned)s->block_start] : \
867 (charf *)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ 908 (charf *)Z_NULL), \
909 (ulg)((long)s->strstart - s->block_start), \
910 (eof)); \
868 s->block_start = s->strstart; \ 911 s->block_start = s->strstart; \
869 flush_pending(s->strm); \ 912 flush_pending(s->strm); \
870 Tracev((stderr,"[FLUSH]")); \ 913 Tracev((stderr,"[FLUSH]")); \
@@ -906,7 +949,7 @@ local int deflate_stored(s, flush)
906 s->lookahead = 0; 949 s->lookahead = 0;
907 950
908 /* Stored blocks are limited to 0xffff bytes: */ 951 /* Stored blocks are limited to 0xffff bytes: */
909 if (s->strstart == 0 || s->strstart > 0xffff) { 952 if (s->strstart == 0 || s->strstart > 0xfffe) {
910 /* strstart == 0 is possible when wraparound on 16-bit machine */ 953 /* strstart == 0 is possible when wraparound on 16-bit machine */
911 s->lookahead = s->strstart - 0xffff; 954 s->lookahead = s->strstart - 0xffff;
912 s->strstart = 0xffff; 955 s->strstart = 0xffff;
@@ -967,14 +1010,12 @@ local int deflate_fast(s, flush)
967 s->match_length = longest_match (s, hash_head); 1010 s->match_length = longest_match (s, hash_head);
968 } 1011 }
969 /* longest_match() sets match_start */ 1012 /* longest_match() sets match_start */
970
971 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
972 } 1013 }
973 if (s->match_length >= MIN_MATCH) { 1014 if (s->match_length >= MIN_MATCH) {
974 check_match(s, s->strstart, s->match_start, s->match_length); 1015 check_match(s, s->strstart, s->match_start, s->match_length);
975 1016
976 bflush = tr_tally(s, s->strstart - s->match_start, 1017 bflush = _tr_tally(s, s->strstart - s->match_start,
977 s->match_length - MIN_MATCH); 1018 s->match_length - MIN_MATCH);
978 1019
979 s->lookahead -= s->match_length; 1020 s->lookahead -= s->match_length;
980 1021
@@ -1007,7 +1048,7 @@ local int deflate_fast(s, flush)
1007 } else { 1048 } else {
1008 /* No match, output a literal byte */ 1049 /* No match, output a literal byte */
1009 Tracevv((stderr,"%c", s->window[s->strstart])); 1050 Tracevv((stderr,"%c", s->window[s->strstart]));
1010 bflush = tr_tally (s, 0, s->window[s->strstart]); 1051 bflush = _tr_tally (s, 0, s->window[s->strstart]);
1011 s->lookahead--; 1052 s->lookahead--;
1012 s->strstart++; 1053 s->strstart++;
1013 } 1054 }
@@ -1065,7 +1106,6 @@ local int deflate_slow(s, flush)
1065 s->match_length = longest_match (s, hash_head); 1106 s->match_length = longest_match (s, hash_head);
1066 } 1107 }
1067 /* longest_match() sets match_start */ 1108 /* longest_match() sets match_start */
1068 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
1069 1109
1070 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 1110 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1071 (s->match_length == MIN_MATCH && 1111 (s->match_length == MIN_MATCH &&
@@ -1086,8 +1126,8 @@ local int deflate_slow(s, flush)
1086 1126
1087 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1127 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1088 1128
1089 bflush = tr_tally(s, s->strstart -1 - s->prev_match, 1129 bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
1090 s->prev_length - MIN_MATCH); 1130 s->prev_length - MIN_MATCH);
1091 1131
1092 /* Insert in hash table all strings up to the end of the match. 1132 /* Insert in hash table all strings up to the end of the match.
1093 * strstart-1 and strstart are already inserted. If there is not 1133 * strstart-1 and strstart are already inserted. If there is not
@@ -1113,7 +1153,7 @@ local int deflate_slow(s, flush)
1113 * is longer, truncate the previous match to a single literal. 1153 * is longer, truncate the previous match to a single literal.
1114 */ 1154 */
1115 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1155 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1116 if (tr_tally (s, 0, s->window[s->strstart-1])) { 1156 if (_tr_tally (s, 0, s->window[s->strstart-1])) {
1117 FLUSH_BLOCK_ONLY(s, 0); 1157 FLUSH_BLOCK_ONLY(s, 0);
1118 } 1158 }
1119 s->strstart++; 1159 s->strstart++;
@@ -1131,7 +1171,7 @@ local int deflate_slow(s, flush)
1131 Assert (flush != Z_NO_FLUSH, "no flush?"); 1171 Assert (flush != Z_NO_FLUSH, "no flush?");
1132 if (s->match_available) { 1172 if (s->match_available) {
1133 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1173 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1134 tr_tally (s, 0, s->window[s->strstart-1]); 1174 _tr_tally (s, 0, s->window[s->strstart-1]);
1135 s->match_available = 0; 1175 s->match_available = 0;
1136 } 1176 }
1137 FLUSH_BLOCK(s, flush == Z_FINISH); 1177 FLUSH_BLOCK(s, flush == Z_FINISH);