diff options
Diffstat (limited to 'deflate.c')
-rw-r--r-- | deflate.c | 54 |
1 files changed, 35 insertions, 19 deletions
@@ -37,7 +37,7 @@ | |||
37 | * REFERENCES | 37 | * REFERENCES |
38 | * | 38 | * |
39 | * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". | 39 | * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". |
40 | * Available in ftp://ds.internic.net/rfc/rfc1951.txt | 40 | * Available in http://www.ietf.org/rfc/rfc1951.txt |
41 | * | 41 | * |
42 | * A description of the Rabin and Karp algorithm is given in the book | 42 | * A description of the Rabin and Karp algorithm is given in the book |
43 | * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. | 43 | * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. |
@@ -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.0.1 Copyright 1995-2003 Jean-loup Gailly "; | 55 | " deflate 1.2.0.2 Copyright 1995-2003 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 |
@@ -157,7 +157,9 @@ local const config configuration_table[10] = { | |||
157 | #define EQUAL 0 | 157 | #define EQUAL 0 |
158 | /* result of memcmp for equal strings */ | 158 | /* result of memcmp for equal strings */ |
159 | 159 | ||
160 | #ifndef NO_DUMMY_DECL | ||
160 | struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ | 161 | struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ |
162 | #endif | ||
161 | 163 | ||
162 | /* =========================================================================== | 164 | /* =========================================================================== |
163 | * Update a hash value with the given input byte | 165 | * Update a hash value with the given input byte |
@@ -255,10 +257,11 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, | |||
255 | windowBits = -windowBits; | 257 | windowBits = -windowBits; |
256 | } | 258 | } |
257 | if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || | 259 | if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || |
258 | windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || | 260 | windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || |
259 | strategy < 0 || strategy > Z_RLE) { | 261 | strategy < 0 || strategy > Z_RLE) { |
260 | return Z_STREAM_ERROR; | 262 | return Z_STREAM_ERROR; |
261 | } | 263 | } |
264 | if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ | ||
262 | s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); | 265 | s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); |
263 | if (s == Z_NULL) return Z_MEM_ERROR; | 266 | if (s == Z_NULL) return Z_MEM_ERROR; |
264 | strm->state = (struct internal_state FAR *)s; | 267 | strm->state = (struct internal_state FAR *)s; |
@@ -520,9 +523,16 @@ int ZEXPORT deflate (strm, flush) | |||
520 | if (s->status == INIT_STATE) { | 523 | if (s->status == INIT_STATE) { |
521 | 524 | ||
522 | uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; | 525 | uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; |
523 | uInt level_flags = (s->level-1) >> 1; | 526 | uInt level_flags; |
524 | 527 | ||
525 | if (level_flags > 3) level_flags = 3; | 528 | if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) |
529 | level_flags = 0; | ||
530 | else if (s->level < 6) | ||
531 | level_flags = 1; | ||
532 | else if (s->level == 6) | ||
533 | level_flags = 2; | ||
534 | else | ||
535 | level_flags = 3; | ||
526 | header |= (level_flags << 6); | 536 | header |= (level_flags << 6); |
527 | if (s->strstart != 0) header |= PRESET_DICT; | 537 | if (s->strstart != 0) header |= PRESET_DICT; |
528 | header += 31 - (header % 31); | 538 | header += 31 - (header % 31); |
@@ -975,7 +985,7 @@ local uInt longest_match_fast(s, cur_match) | |||
975 | if (len < MIN_MATCH) return MIN_MATCH - 1; | 985 | if (len < MIN_MATCH) return MIN_MATCH - 1; |
976 | 986 | ||
977 | s->match_start = cur_match; | 987 | s->match_start = cur_match; |
978 | return len <= s->lookahead ? len : s->lookahead; | 988 | return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; |
979 | } | 989 | } |
980 | 990 | ||
981 | #ifdef DEBUG | 991 | #ifdef DEBUG |
@@ -1028,19 +1038,22 @@ local void fill_window(s) | |||
1028 | more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | 1038 | more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
1029 | 1039 | ||
1030 | /* Deal with !@#$% 64K limit: */ | 1040 | /* Deal with !@#$% 64K limit: */ |
1031 | if (more == 0 && s->strstart == 0 && s->lookahead == 0) { | 1041 | if (sizeof(int) <= 2) { |
1032 | more = wsize; | 1042 | if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
1033 | 1043 | more = wsize; | |
1034 | } else if (more == (unsigned)(-1)) { | 1044 | |
1035 | /* Very unlikely, but possible on 16 bit machine if strstart == 0 | 1045 | } else if (more == (unsigned)(-1)) { |
1036 | * and lookahead == 1 (input done one byte at time) | 1046 | /* Very unlikely, but possible on 16 bit machine if |
1037 | */ | 1047 | * strstart == 0 && lookahead == 1 (input done one byte at time) |
1038 | more--; | 1048 | */ |
1049 | more--; | ||
1050 | } | ||
1051 | } | ||
1039 | 1052 | ||
1040 | /* If the window is almost full and there is insufficient lookahead, | 1053 | /* If the window is almost full and there is insufficient lookahead, |
1041 | * move the upper half to the lower one to make room in the upper half. | 1054 | * move the upper half to the lower one to make room in the upper half. |
1042 | */ | 1055 | */ |
1043 | } else if (s->strstart >= wsize+MAX_DIST(s)) { | 1056 | if (s->strstart >= wsize+MAX_DIST(s)) { |
1044 | 1057 | ||
1045 | zmemcpy(s->window, s->window+wsize, (unsigned)wsize); | 1058 | zmemcpy(s->window, s->window+wsize, (unsigned)wsize); |
1046 | s->match_start -= wsize; | 1059 | s->match_start -= wsize; |
@@ -1347,9 +1360,12 @@ local block_state deflate_slow(s, flush) | |||
1347 | } | 1360 | } |
1348 | /* longest_match() or longest_match_fast() sets match_start */ | 1361 | /* longest_match() or longest_match_fast() sets match_start */ |
1349 | 1362 | ||
1350 | if (s->match_length <= 5 && (s->strategy == Z_FILTERED || | 1363 | if (s->match_length <= 5 && (s->strategy == Z_FILTERED |
1351 | (s->match_length == MIN_MATCH && | 1364 | #if TOO_FAR < 32768 |
1352 | s->strstart - s->match_start > TOO_FAR))) { | 1365 | || (s->match_length == MIN_MATCH && |
1366 | s->strstart - s->match_start > TOO_FAR) | ||
1367 | #endif | ||
1368 | )) { | ||
1353 | 1369 | ||
1354 | /* If prev_match is also MIN_MATCH, match_start is garbage | 1370 | /* If prev_match is also MIN_MATCH, match_start is garbage |
1355 | * but we will ignore the current match anyway. | 1371 | * but we will ignore the current match anyway. |