diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:17:02 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:17:02 -0700 |
commit | ff11b0a61f7345572ff2e413173d3179486162f2 (patch) | |
tree | f3c9e2563c4f0ac6684a0012ad48423d4c6aa798 /deflate.c | |
parent | e26a448e9673d67dc2866e11a48d24fc352e5f80 (diff) | |
download | zlib-ff11b0a61f7345572ff2e413173d3179486162f2.tar.gz zlib-ff11b0a61f7345572ff2e413173d3179486162f2.tar.bz2 zlib-ff11b0a61f7345572ff2e413173d3179486162f2.zip |
zlib 1.0.4v1.0.4
Diffstat (limited to 'deflate.c')
-rw-r--r-- | deflate.c | 145 |
1 files changed, 86 insertions, 59 deletions
@@ -47,11 +47,11 @@ | |||
47 | * | 47 | * |
48 | */ | 48 | */ |
49 | 49 | ||
50 | /* $Id: deflate.c,v 1.13 1996/05/22 11:52:21 me Exp $ */ | 50 | /* $Id: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ */ |
51 | 51 | ||
52 | #include "deflate.h" | 52 | #include "deflate.h" |
53 | 53 | ||
54 | char deflate_copyright[] = " deflate 1.0.2 Copyright 1995-1996 Jean-loup Gailly "; | 54 | char deflate_copyright[] = " deflate 1.0.4 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 |
@@ -62,15 +62,25 @@ char deflate_copyright[] = " deflate 1.0.2 Copyright 1995-1996 Jean-loup Gailly | |||
62 | /* =========================================================================== | 62 | /* =========================================================================== |
63 | * Function prototypes. | 63 | * Function prototypes. |
64 | */ | 64 | */ |
65 | typedef enum { | ||
66 | need_more, /* block not completed, need more input or more output */ | ||
67 | block_done, /* block flush performed */ | ||
68 | finish_started, /* finish started, need only more output at next deflate */ | ||
69 | finish_done /* finish done, accept no more input or output */ | ||
70 | } block_state; | ||
71 | |||
72 | typedef block_state (*compress_func) OF((deflate_state *s, int flush)); | ||
73 | /* Compression function. Returns the block state after the call. */ | ||
74 | |||
65 | local void fill_window OF((deflate_state *s)); | 75 | local void fill_window OF((deflate_state *s)); |
66 | local int deflate_stored OF((deflate_state *s, int flush)); | 76 | local block_state deflate_stored OF((deflate_state *s, int flush)); |
67 | local int deflate_fast OF((deflate_state *s, int flush)); | 77 | local block_state deflate_fast OF((deflate_state *s, int flush)); |
68 | local int deflate_slow OF((deflate_state *s, int flush)); | 78 | local block_state deflate_slow OF((deflate_state *s, int flush)); |
69 | local void lm_init OF((deflate_state *s)); | 79 | local void lm_init OF((deflate_state *s)); |
70 | local uInt longest_match OF((deflate_state *s, IPos cur_match)); | 80 | local uInt longest_match OF((deflate_state *s, IPos cur_match)); |
71 | local void putShortMSB OF((deflate_state *s, uInt b)); | 81 | local void putShortMSB OF((deflate_state *s, uInt b)); |
72 | local void flush_pending OF((z_stream *strm)); | 82 | local void flush_pending OF((z_streamp strm)); |
73 | local int read_buf OF((z_stream *strm, charf *buf, unsigned size)); | 83 | local int read_buf OF((z_streamp strm, charf *buf, unsigned size)); |
74 | #ifdef ASMV | 84 | #ifdef ASMV |
75 | void match_init OF((void)); /* asm code initialization */ | 85 | void match_init OF((void)); /* asm code initialization */ |
76 | #endif | 86 | #endif |
@@ -97,9 +107,6 @@ local void check_match OF((deflate_state *s, IPos start, IPos match, | |||
97 | * See deflate.c for comments about the MIN_MATCH+1. | 107 | * See deflate.c for comments about the MIN_MATCH+1. |
98 | */ | 108 | */ |
99 | 109 | ||
100 | typedef int (*compress_func) OF((deflate_state *s, int flush)); | ||
101 | /* Compressing function */ | ||
102 | |||
103 | /* Values for max_lazy_match, good_match and max_chain_length, depending on | 110 | /* Values for max_lazy_match, good_match and max_chain_length, depending on |
104 | * the desired pack level (0..9). The values given below have been tuned to | 111 | * the desired pack level (0..9). The values given below have been tuned to |
105 | * exclude worst case performance for pathological files. Better values may be | 112 | * exclude worst case performance for pathological files. Better values may be |
@@ -169,7 +176,7 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ | |||
169 | 176 | ||
170 | /* ========================================================================= */ | 177 | /* ========================================================================= */ |
171 | int deflateInit_(strm, level, version, stream_size) | 178 | int deflateInit_(strm, level, version, stream_size) |
172 | z_stream *strm; | 179 | z_streamp strm; |
173 | int level; | 180 | int level; |
174 | const char *version; | 181 | const char *version; |
175 | int stream_size; | 182 | int stream_size; |
@@ -182,7 +189,7 @@ int deflateInit_(strm, level, version, stream_size) | |||
182 | /* ========================================================================= */ | 189 | /* ========================================================================= */ |
183 | int deflateInit2_(strm, level, method, windowBits, memLevel, strategy, | 190 | int deflateInit2_(strm, level, method, windowBits, memLevel, strategy, |
184 | version, stream_size) | 191 | version, stream_size) |
185 | z_stream *strm; | 192 | z_streamp strm; |
186 | int level; | 193 | int level; |
187 | int method; | 194 | int method; |
188 | int windowBits; | 195 | int windowBits; |
@@ -249,7 +256,7 @@ int deflateInit2_(strm, level, method, windowBits, memLevel, strategy, | |||
249 | 256 | ||
250 | if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || | 257 | if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
251 | s->pending_buf == Z_NULL) { | 258 | s->pending_buf == Z_NULL) { |
252 | strm->msg = ERR_MSG(Z_MEM_ERROR); | 259 | strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); |
253 | deflateEnd (strm); | 260 | deflateEnd (strm); |
254 | return Z_MEM_ERROR; | 261 | return Z_MEM_ERROR; |
255 | } | 262 | } |
@@ -265,7 +272,7 @@ int deflateInit2_(strm, level, method, windowBits, memLevel, strategy, | |||
265 | 272 | ||
266 | /* ========================================================================= */ | 273 | /* ========================================================================= */ |
267 | int deflateSetDictionary (strm, dictionary, dictLength) | 274 | int deflateSetDictionary (strm, dictionary, dictLength) |
268 | z_stream *strm; | 275 | z_streamp strm; |
269 | const Bytef *dictionary; | 276 | const Bytef *dictionary; |
270 | uInt dictLength; | 277 | uInt dictLength; |
271 | { | 278 | { |
@@ -304,7 +311,7 @@ int deflateSetDictionary (strm, dictionary, dictLength) | |||
304 | 311 | ||
305 | /* ========================================================================= */ | 312 | /* ========================================================================= */ |
306 | int deflateReset (strm) | 313 | int deflateReset (strm) |
307 | z_stream *strm; | 314 | z_streamp strm; |
308 | { | 315 | { |
309 | deflate_state *s; | 316 | deflate_state *s; |
310 | 317 | ||
@@ -334,7 +341,7 @@ int deflateReset (strm) | |||
334 | 341 | ||
335 | /* ========================================================================= */ | 342 | /* ========================================================================= */ |
336 | int deflateParams(strm, level, strategy) | 343 | int deflateParams(strm, level, strategy) |
337 | z_stream *strm; | 344 | z_streamp strm; |
338 | int level; | 345 | int level; |
339 | int strategy; | 346 | int strategy; |
340 | { | 347 | { |
@@ -388,7 +395,7 @@ local void putShortMSB (s, b) | |||
388 | * (See also read_buf()). | 395 | * (See also read_buf()). |
389 | */ | 396 | */ |
390 | local void flush_pending(strm) | 397 | local void flush_pending(strm) |
391 | z_stream *strm; | 398 | z_streamp strm; |
392 | { | 399 | { |
393 | unsigned len = strm->state->pending; | 400 | unsigned len = strm->state->pending; |
394 | 401 | ||
@@ -408,14 +415,16 @@ local void flush_pending(strm) | |||
408 | 415 | ||
409 | /* ========================================================================= */ | 416 | /* ========================================================================= */ |
410 | int deflate (strm, flush) | 417 | int deflate (strm, flush) |
411 | z_stream *strm; | 418 | z_streamp strm; |
412 | int flush; | 419 | int flush; |
413 | { | 420 | { |
414 | int old_flush; /* value of flush param for previous deflate call */ | 421 | int old_flush; /* value of flush param for previous deflate call */ |
415 | deflate_state *s; | 422 | deflate_state *s; |
416 | 423 | ||
417 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 424 | if (strm == Z_NULL || strm->state == Z_NULL || |
418 | 425 | flush > Z_FINISH || flush < 0) { | |
426 | return Z_STREAM_ERROR; | ||
427 | } | ||
419 | s = strm->state; | 428 | s = strm->state; |
420 | 429 | ||
421 | if (strm->next_out == Z_NULL || | 430 | if (strm->next_out == Z_NULL || |
@@ -447,14 +456,23 @@ int deflate (strm, flush) | |||
447 | if (s->strstart != 0) { | 456 | if (s->strstart != 0) { |
448 | putShortMSB(s, (uInt)(strm->adler >> 16)); | 457 | putShortMSB(s, (uInt)(strm->adler >> 16)); |
449 | putShortMSB(s, (uInt)(strm->adler & 0xffff)); | 458 | putShortMSB(s, (uInt)(strm->adler & 0xffff)); |
450 | strm->adler = 1L; | ||
451 | } | 459 | } |
460 | strm->adler = 1L; | ||
452 | } | 461 | } |
453 | 462 | ||
454 | /* Flush as much pending output as possible */ | 463 | /* Flush as much pending output as possible */ |
455 | if (s->pending != 0) { | 464 | if (s->pending != 0) { |
456 | flush_pending(strm); | 465 | flush_pending(strm); |
457 | if (strm->avail_out == 0) return Z_OK; | 466 | if (strm->avail_out == 0) { |
467 | /* Since avail_out is 0, deflate will be called again with | ||
468 | * more output space, but possibly with both pending and | ||
469 | * avail_in equal to zero. There won't be anything to do, | ||
470 | * but this is not an error situation so make sure we | ||
471 | * return OK instead of BUF_ERROR at next call of deflate: | ||
472 | */ | ||
473 | s->last_flush = -1; | ||
474 | return Z_OK; | ||
475 | } | ||
458 | 476 | ||
459 | /* Make sure there is something to do and avoid duplicate consecutive | 477 | /* Make sure there is something to do and avoid duplicate consecutive |
460 | * flushes. For repeated and useless calls with Z_FINISH, we keep | 478 | * flushes. For repeated and useless calls with Z_FINISH, we keep |
@@ -474,22 +492,27 @@ int deflate (strm, flush) | |||
474 | */ | 492 | */ |
475 | if (strm->avail_in != 0 || s->lookahead != 0 || | 493 | if (strm->avail_in != 0 || s->lookahead != 0 || |
476 | (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { | 494 | (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { |
477 | int quit; | 495 | block_state bstate; |
478 | 496 | ||
479 | if (flush == Z_FINISH) { | 497 | bstate = (*(configuration_table[s->level].func))(s, flush); |
498 | |||
499 | if (bstate == finish_started || bstate == finish_done) { | ||
480 | s->status = FINISH_STATE; | 500 | s->status = FINISH_STATE; |
481 | } | 501 | } |
482 | quit = (*(configuration_table[s->level].func))(s, flush); | 502 | if (bstate == need_more || bstate == finish_started) { |
483 | 503 | if (strm->avail_out == 0) { | |
484 | if (quit || strm->avail_out == 0) return Z_OK; | 504 | s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ |
485 | /* If flush != Z_NO_FLUSH && avail_out == 0, the next call | 505 | } |
486 | * of deflate should use the same flush parameter to make sure | 506 | return Z_OK; |
487 | * that the flush is complete. So we don't have to output an | 507 | /* If flush != Z_NO_FLUSH && avail_out == 0, the next call |
488 | * empty block here, this will be done at next call. This also | 508 | * of deflate should use the same flush parameter to make sure |
489 | * ensures that for a very small output buffer, we emit at most | 509 | * that the flush is complete. So we don't have to output an |
490 | * one empty block. | 510 | * empty block here, this will be done at next call. This also |
491 | */ | 511 | * ensures that for a very small output buffer, we emit at most |
492 | if (flush != Z_NO_FLUSH && flush != Z_FINISH) { | 512 | * one empty block. |
513 | */ | ||
514 | } | ||
515 | if (bstate == block_done) { | ||
493 | if (flush == Z_PARTIAL_FLUSH) { | 516 | if (flush == Z_PARTIAL_FLUSH) { |
494 | _tr_align(s); | 517 | _tr_align(s); |
495 | } else { /* FULL_FLUSH or SYNC_FLUSH */ | 518 | } else { /* FULL_FLUSH or SYNC_FLUSH */ |
@@ -502,7 +525,10 @@ int deflate (strm, flush) | |||
502 | } | 525 | } |
503 | } | 526 | } |
504 | flush_pending(strm); | 527 | flush_pending(strm); |
505 | if (strm->avail_out == 0) return Z_OK; | 528 | if (strm->avail_out == 0) { |
529 | s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ | ||
530 | return Z_OK; | ||
531 | } | ||
506 | } | 532 | } |
507 | } | 533 | } |
508 | Assert(strm->avail_out > 0, "bug2"); | 534 | Assert(strm->avail_out > 0, "bug2"); |
@@ -523,7 +549,7 @@ int deflate (strm, flush) | |||
523 | 549 | ||
524 | /* ========================================================================= */ | 550 | /* ========================================================================= */ |
525 | int deflateEnd (strm) | 551 | int deflateEnd (strm) |
526 | z_stream *strm; | 552 | z_streamp strm; |
527 | { | 553 | { |
528 | int status; | 554 | int status; |
529 | 555 | ||
@@ -544,8 +570,8 @@ int deflateEnd (strm) | |||
544 | 570 | ||
545 | /* ========================================================================= */ | 571 | /* ========================================================================= */ |
546 | int deflateCopy (dest, source) | 572 | int deflateCopy (dest, source) |
547 | z_stream *dest; | 573 | z_streamp dest; |
548 | z_stream *source; | 574 | z_streamp source; |
549 | { | 575 | { |
550 | if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { | 576 | if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { |
551 | return Z_STREAM_ERROR; | 577 | return Z_STREAM_ERROR; |
@@ -570,7 +596,7 @@ int deflateCopy (dest, source) | |||
570 | * (See also flush_pending()). | 596 | * (See also flush_pending()). |
571 | */ | 597 | */ |
572 | local int read_buf(strm, buf, size) | 598 | local int read_buf(strm, buf, size) |
573 | z_stream *strm; | 599 | z_streamp strm; |
574 | charf *buf; | 600 | charf *buf; |
575 | unsigned size; | 601 | unsigned size; |
576 | { | 602 | { |
@@ -916,18 +942,18 @@ local void fill_window(s) | |||
916 | /* Same but force premature exit if necessary. */ | 942 | /* Same but force premature exit if necessary. */ |
917 | #define FLUSH_BLOCK(s, eof) { \ | 943 | #define FLUSH_BLOCK(s, eof) { \ |
918 | FLUSH_BLOCK_ONLY(s, eof); \ | 944 | FLUSH_BLOCK_ONLY(s, eof); \ |
919 | if (s->strm->avail_out == 0) return 1; \ | 945 | if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \ |
920 | } | 946 | } |
921 | 947 | ||
922 | /* =========================================================================== | 948 | /* =========================================================================== |
923 | * Copy without compression as much as possible from the input stream, return | 949 | * Copy without compression as much as possible from the input stream, return |
924 | * true if processing was terminated prematurely (no more input or output | 950 | * the current block state. |
925 | * space). This function does not insert new strings in the dictionary | 951 | * This function does not insert new strings in the dictionary since |
926 | * since uncompressible data is probably not useful. This function is used | 952 | * uncompressible data is probably not useful. This function is used |
927 | * only for the level=0 compression option. | 953 | * only for the level=0 compression option. |
928 | * NOTE: this function should be optimized to avoid extra copying. | 954 | * NOTE: this function should be optimized to avoid extra copying. |
929 | */ | 955 | */ |
930 | local int deflate_stored(s, flush) | 956 | local block_state deflate_stored(s, flush) |
931 | deflate_state *s; | 957 | deflate_state *s; |
932 | int flush; | 958 | int flush; |
933 | { | 959 | { |
@@ -939,7 +965,7 @@ local int deflate_stored(s, flush) | |||
939 | s->block_start >= (long)s->w_size, "slide too late"); | 965 | s->block_start >= (long)s->w_size, "slide too late"); |
940 | 966 | ||
941 | fill_window(s); | 967 | fill_window(s); |
942 | if (s->lookahead == 0 && flush == Z_NO_FLUSH) return 1; | 968 | if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; |
943 | 969 | ||
944 | if (s->lookahead == 0) break; /* flush the current block */ | 970 | if (s->lookahead == 0) break; /* flush the current block */ |
945 | } | 971 | } |
@@ -961,17 +987,17 @@ local int deflate_stored(s, flush) | |||
961 | } | 987 | } |
962 | } | 988 | } |
963 | FLUSH_BLOCK(s, flush == Z_FINISH); | 989 | FLUSH_BLOCK(s, flush == Z_FINISH); |
964 | return 0; /* normal exit */ | 990 | return flush == Z_FINISH ? finish_done : block_done; |
965 | } | 991 | } |
966 | 992 | ||
967 | /* =========================================================================== | 993 | /* =========================================================================== |
968 | * Compress as much as possible from the input stream, return true if | 994 | * Compress as much as possible from the input stream, return the current |
969 | * processing was terminated prematurely (no more input or output space). | 995 | * block state. |
970 | * This function does not perform lazy evaluation of matches and inserts | 996 | * This function does not perform lazy evaluation of matches and inserts |
971 | * new strings in the dictionary only for unmatched strings or for short | 997 | * new strings in the dictionary only for unmatched strings or for short |
972 | * matches. It is used only for the fast compression options. | 998 | * matches. It is used only for the fast compression options. |
973 | */ | 999 | */ |
974 | local int deflate_fast(s, flush) | 1000 | local block_state deflate_fast(s, flush) |
975 | deflate_state *s; | 1001 | deflate_state *s; |
976 | int flush; | 1002 | int flush; |
977 | { | 1003 | { |
@@ -986,8 +1012,9 @@ local int deflate_fast(s, flush) | |||
986 | */ | 1012 | */ |
987 | if (s->lookahead < MIN_LOOKAHEAD) { | 1013 | if (s->lookahead < MIN_LOOKAHEAD) { |
988 | fill_window(s); | 1014 | fill_window(s); |
989 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; | 1015 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
990 | 1016 | return need_more; | |
1017 | } | ||
991 | if (s->lookahead == 0) break; /* flush the current block */ | 1018 | if (s->lookahead == 0) break; /* flush the current block */ |
992 | } | 1019 | } |
993 | 1020 | ||
@@ -1055,7 +1082,7 @@ local int deflate_fast(s, flush) | |||
1055 | if (bflush) FLUSH_BLOCK(s, 0); | 1082 | if (bflush) FLUSH_BLOCK(s, 0); |
1056 | } | 1083 | } |
1057 | FLUSH_BLOCK(s, flush == Z_FINISH); | 1084 | FLUSH_BLOCK(s, flush == Z_FINISH); |
1058 | return 0; /* normal exit */ | 1085 | return flush == Z_FINISH ? finish_done : block_done; |
1059 | } | 1086 | } |
1060 | 1087 | ||
1061 | /* =========================================================================== | 1088 | /* =========================================================================== |
@@ -1063,7 +1090,7 @@ local int deflate_fast(s, flush) | |||
1063 | * evaluation for matches: a match is finally adopted only if there is | 1090 | * evaluation for matches: a match is finally adopted only if there is |
1064 | * no better match at the next window position. | 1091 | * no better match at the next window position. |
1065 | */ | 1092 | */ |
1066 | local int deflate_slow(s, flush) | 1093 | local block_state deflate_slow(s, flush) |
1067 | deflate_state *s; | 1094 | deflate_state *s; |
1068 | int flush; | 1095 | int flush; |
1069 | { | 1096 | { |
@@ -1079,8 +1106,9 @@ local int deflate_slow(s, flush) | |||
1079 | */ | 1106 | */ |
1080 | if (s->lookahead < MIN_LOOKAHEAD) { | 1107 | if (s->lookahead < MIN_LOOKAHEAD) { |
1081 | fill_window(s); | 1108 | fill_window(s); |
1082 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; | 1109 | if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
1083 | 1110 | return need_more; | |
1111 | } | ||
1084 | if (s->lookahead == 0) break; /* flush the current block */ | 1112 | if (s->lookahead == 0) break; /* flush the current block */ |
1085 | } | 1113 | } |
1086 | 1114 | ||
@@ -1158,7 +1186,7 @@ local int deflate_slow(s, flush) | |||
1158 | } | 1186 | } |
1159 | s->strstart++; | 1187 | s->strstart++; |
1160 | s->lookahead--; | 1188 | s->lookahead--; |
1161 | if (s->strm->avail_out == 0) return 1; | 1189 | if (s->strm->avail_out == 0) return need_more; |
1162 | } else { | 1190 | } else { |
1163 | /* There is no previous match to compare with, wait for | 1191 | /* There is no previous match to compare with, wait for |
1164 | * the next step to decide. | 1192 | * the next step to decide. |
@@ -1175,6 +1203,5 @@ local int deflate_slow(s, flush) | |||
1175 | s->match_available = 0; | 1203 | s->match_available = 0; |
1176 | } | 1204 | } |
1177 | FLUSH_BLOCK(s, flush == Z_FINISH); | 1205 | FLUSH_BLOCK(s, flush == Z_FINISH); |
1178 | return 0; | 1206 | return flush == Z_FINISH ? finish_done : block_done; |
1179 | } | 1207 | } |
1180 | |||