diff options
Diffstat (limited to 'deflate.c')
-rw-r--r-- | deflate.c | 75 |
1 files changed, 55 insertions, 20 deletions
@@ -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-2006 Jean-loup Gailly. | 2 | * Copyright (C) 1995-2009 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 | ||
@@ -52,7 +52,7 @@ | |||
52 | #include "deflate.h" | 52 | #include "deflate.h" |
53 | 53 | ||
54 | const char deflate_copyright[] = | 54 | const char deflate_copyright[] = |
55 | " deflate 1.2.3.3 Copyright 1995-2006 Jean-loup Gailly "; | 55 | " deflate 1.2.3.4 Copyright 1995-2009 Jean-loup Gailly "; |
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 |
@@ -110,11 +110,6 @@ local void check_match OF((deflate_state *s, IPos start, IPos match, | |||
110 | #endif | 110 | #endif |
111 | /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ | 111 | /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ |
112 | 112 | ||
113 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) | ||
114 | /* Minimum amount of lookahead, except at the end of the input file. | ||
115 | * See deflate.c for comments about the MIN_MATCH+1. | ||
116 | */ | ||
117 | |||
118 | /* Values for max_lazy_match, good_match and max_chain_length, depending on | 113 | /* Values for max_lazy_match, good_match and max_chain_length, depending on |
119 | * the desired pack level (0..9). The values given below have been tuned to | 114 | * the desired pack level (0..9). The values given below have been tuned to |
120 | * exclude worst case performance for pathological files. Better values may be | 115 | * exclude worst case performance for pathological files. Better values may be |
@@ -288,6 +283,8 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, | |||
288 | s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); | 283 | s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
289 | s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); | 284 | s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
290 | 285 | ||
286 | s->high_water = 0; /* nothing written to s->window yet */ | ||
287 | |||
291 | s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | 288 | s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
292 | 289 | ||
293 | overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); | 290 | overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); |
@@ -332,8 +329,8 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) | |||
332 | strm->adler = adler32(strm->adler, dictionary, dictLength); | 329 | strm->adler = adler32(strm->adler, dictionary, dictLength); |
333 | 330 | ||
334 | if (length < MIN_MATCH) return Z_OK; | 331 | if (length < MIN_MATCH) return Z_OK; |
335 | if (length > MAX_DIST(s)) { | 332 | if (length > s->w_size) { |
336 | length = MAX_DIST(s); | 333 | length = s->w_size; |
337 | dictionary += dictLength - length; /* use the tail of the dictionary */ | 334 | dictionary += dictLength - length; /* use the tail of the dictionary */ |
338 | } | 335 | } |
339 | zmemcpy(s->window, dictionary, length); | 336 | zmemcpy(s->window, dictionary, length); |
@@ -513,16 +510,16 @@ uLong ZEXPORT deflateBound(strm, sourceLen) | |||
513 | break; | 510 | break; |
514 | case 2: /* gzip wrapper */ | 511 | case 2: /* gzip wrapper */ |
515 | wraplen = 18; | 512 | wraplen = 18; |
516 | if (s->gzhead != NULL) { /* user-supplied gzip header */ | 513 | if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ |
517 | if (s->gzhead->extra != NULL) | 514 | if (s->gzhead->extra != Z_NULL) |
518 | wraplen += 2 + s->gzhead->extra_len; | 515 | wraplen += 2 + s->gzhead->extra_len; |
519 | str = s->gzhead->name; | 516 | str = s->gzhead->name; |
520 | if (str != NULL) | 517 | if (str != Z_NULL) |
521 | do { | 518 | do { |
522 | wraplen++; | 519 | wraplen++; |
523 | } while (*str++); | 520 | } while (*str++); |
524 | str = s->gzhead->comment; | 521 | str = s->gzhead->comment; |
525 | if (str != NULL) | 522 | if (str != Z_NULL) |
526 | do { | 523 | do { |
527 | wraplen++; | 524 | wraplen++; |
528 | } while (*str++); | 525 | } while (*str++); |
@@ -589,7 +586,7 @@ int ZEXPORT deflate (strm, flush) | |||
589 | deflate_state *s; | 586 | deflate_state *s; |
590 | 587 | ||
591 | if (strm == Z_NULL || strm->state == Z_NULL || | 588 | if (strm == Z_NULL || strm->state == Z_NULL || |
592 | flush > Z_FINISH || flush < 0) { | 589 | flush > Z_BLOCK || flush < 0) { |
593 | return Z_STREAM_ERROR; | 590 | return Z_STREAM_ERROR; |
594 | } | 591 | } |
595 | s = strm->state; | 592 | s = strm->state; |
@@ -613,7 +610,7 @@ int ZEXPORT deflate (strm, flush) | |||
613 | put_byte(s, 31); | 610 | put_byte(s, 31); |
614 | put_byte(s, 139); | 611 | put_byte(s, 139); |
615 | put_byte(s, 8); | 612 | put_byte(s, 8); |
616 | if (s->gzhead == NULL) { | 613 | if (s->gzhead == Z_NULL) { |
617 | put_byte(s, 0); | 614 | put_byte(s, 0); |
618 | put_byte(s, 0); | 615 | put_byte(s, 0); |
619 | put_byte(s, 0); | 616 | put_byte(s, 0); |
@@ -640,7 +637,7 @@ int ZEXPORT deflate (strm, flush) | |||
640 | (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? | 637 | (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
641 | 4 : 0)); | 638 | 4 : 0)); |
642 | put_byte(s, s->gzhead->os & 0xff); | 639 | put_byte(s, s->gzhead->os & 0xff); |
643 | if (s->gzhead->extra != NULL) { | 640 | if (s->gzhead->extra != Z_NULL) { |
644 | put_byte(s, s->gzhead->extra_len & 0xff); | 641 | put_byte(s, s->gzhead->extra_len & 0xff); |
645 | put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); | 642 | put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); |
646 | } | 643 | } |
@@ -682,7 +679,7 @@ int ZEXPORT deflate (strm, flush) | |||
682 | } | 679 | } |
683 | #ifdef GZIP | 680 | #ifdef GZIP |
684 | if (s->status == EXTRA_STATE) { | 681 | if (s->status == EXTRA_STATE) { |
685 | if (s->gzhead->extra != NULL) { | 682 | if (s->gzhead->extra != Z_NULL) { |
686 | uInt beg = s->pending; /* start of bytes to update crc */ | 683 | uInt beg = s->pending; /* start of bytes to update crc */ |
687 | 684 | ||
688 | while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { | 685 | while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { |
@@ -710,7 +707,7 @@ int ZEXPORT deflate (strm, flush) | |||
710 | s->status = NAME_STATE; | 707 | s->status = NAME_STATE; |
711 | } | 708 | } |
712 | if (s->status == NAME_STATE) { | 709 | if (s->status == NAME_STATE) { |
713 | if (s->gzhead->name != NULL) { | 710 | if (s->gzhead->name != Z_NULL) { |
714 | uInt beg = s->pending; /* start of bytes to update crc */ | 711 | uInt beg = s->pending; /* start of bytes to update crc */ |
715 | int val; | 712 | int val; |
716 | 713 | ||
@@ -741,7 +738,7 @@ int ZEXPORT deflate (strm, flush) | |||
741 | s->status = COMMENT_STATE; | 738 | s->status = COMMENT_STATE; |
742 | } | 739 | } |
743 | if (s->status == COMMENT_STATE) { | 740 | if (s->status == COMMENT_STATE) { |
744 | if (s->gzhead->comment != NULL) { | 741 | if (s->gzhead->comment != Z_NULL) { |
745 | uInt beg = s->pending; /* start of bytes to update crc */ | 742 | uInt beg = s->pending; /* start of bytes to update crc */ |
746 | int val; | 743 | int val; |
747 | 744 | ||
@@ -840,13 +837,17 @@ int ZEXPORT deflate (strm, flush) | |||
840 | if (bstate == block_done) { | 837 | if (bstate == block_done) { |
841 | if (flush == Z_PARTIAL_FLUSH) { | 838 | if (flush == Z_PARTIAL_FLUSH) { |
842 | _tr_align(s); | 839 | _tr_align(s); |
843 | } else { /* FULL_FLUSH or SYNC_FLUSH */ | 840 | } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ |
844 | _tr_stored_block(s, (char*)0, 0L, 0); | 841 | _tr_stored_block(s, (char*)0, 0L, 0); |
845 | /* For a full flush, this empty block will be recognized | 842 | /* For a full flush, this empty block will be recognized |
846 | * as a special marker by inflate_sync(). | 843 | * as a special marker by inflate_sync(). |
847 | */ | 844 | */ |
848 | if (flush == Z_FULL_FLUSH) { | 845 | if (flush == Z_FULL_FLUSH) { |
849 | CLEAR_HASH(s); /* forget history */ | 846 | CLEAR_HASH(s); /* forget history */ |
847 | if (s->lookahead == 0) { | ||
848 | s->strstart = 0; | ||
849 | s->block_start = 0L; | ||
850 | } | ||
850 | } | 851 | } |
851 | } | 852 | } |
852 | flush_pending(strm); | 853 | flush_pending(strm); |
@@ -1387,6 +1388,40 @@ local void fill_window(s) | |||
1387 | */ | 1388 | */ |
1388 | 1389 | ||
1389 | } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); | 1390 | } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
1391 | |||
1392 | /* If the WIN_INIT bytes after the end of the current data have never been | ||
1393 | * written, then zero those bytes in order to avoid memory check reports of | ||
1394 | * the use of uninitialized (or uninitialised as Julian writes) bytes by | ||
1395 | * the longest match routines. Update the high water mark for the next | ||
1396 | * time through here. WIN_INIT is set to MAX_MATCH since the longest match | ||
1397 | * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. | ||
1398 | */ | ||
1399 | if (s->high_water < s->window_size) { | ||
1400 | ulg curr = s->strstart + (ulg)(s->lookahead); | ||
1401 | ulg init; | ||
1402 | |||
1403 | if (s->high_water < curr) { | ||
1404 | /* Previous high water mark below current data -- zero WIN_INIT | ||
1405 | * bytes or up to end of window, whichever is less. | ||
1406 | */ | ||
1407 | init = s->window_size - curr; | ||
1408 | if (init > WIN_INIT) | ||
1409 | init = WIN_INIT; | ||
1410 | zmemzero(s->window + curr, (unsigned)init); | ||
1411 | s->high_water = curr + init; | ||
1412 | } | ||
1413 | else if (s->high_water < (ulg)curr + WIN_INIT) { | ||
1414 | /* High water mark at or above current data, but below current data | ||
1415 | * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up | ||
1416 | * to end of window, whichever is less. | ||
1417 | */ | ||
1418 | init = (ulg)curr + WIN_INIT - s->high_water; | ||
1419 | if (init > s->window_size - s->high_water) | ||
1420 | init = s->window_size - s->high_water; | ||
1421 | zmemzero(s->window + s->high_water, (unsigned)init); | ||
1422 | s->high_water += init; | ||
1423 | } | ||
1424 | } | ||
1390 | } | 1425 | } |
1391 | 1426 | ||
1392 | /* =========================================================================== | 1427 | /* =========================================================================== |