OLD | NEW |
1 /* deflate.c -- compress data using the deflation algorithm | 1 /* deflate.c -- compress data using the deflation algorithm |
2 * Copyright (C) 1995-2005 Jean-loup Gailly. | 2 * Copyright (C) 1995-2010 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 |
8 * | 8 * |
9 * The "deflation" process depends on being able to identify portions | 9 * The "deflation" process depends on being able to identify portions |
10 * of the input text which are identical to earlier input (within a | 10 * of the input text which are identical to earlier input (within a |
11 * sliding window trailing behind the input currently being processed). | 11 * sliding window trailing behind the input currently being processed). |
12 * | 12 * |
(...skipping 27 matching lines...) Expand all Loading... |
40 * Available in http://www.ietf.org/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. |
44 * | 44 * |
45 * Fiala,E.R., and Greene,D.H. | 45 * Fiala,E.R., and Greene,D.H. |
46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 | 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 |
47 * | 47 * |
48 */ | 48 */ |
49 | 49 |
50 /* @(#) $Id: deflate.c,v 3.6 2005/08/04 19:14:14 tor%cs.brown.edu Exp $ */ | 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.3 Copyright 1995-2005 Jean-loup Gailly "; | 55 " deflate 1.2.5 Copyright 1995-2010 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 /* =========================================================================== | 63 /* =========================================================================== |
64 * Function prototypes. | 64 * Function prototypes. |
65 */ | 65 */ |
66 typedef enum { | 66 typedef enum { |
67 need_more, /* block not completed, need more input or more output */ | 67 need_more, /* block not completed, need more input or more output */ |
68 block_done, /* block flush performed */ | 68 block_done, /* block flush performed */ |
69 finish_started, /* finish started, need only more output at next deflate */ | 69 finish_started, /* finish started, need only more output at next deflate */ |
70 finish_done /* finish done, accept no more input or output */ | 70 finish_done /* finish done, accept no more input or output */ |
71 } block_state; | 71 } block_state; |
72 | 72 |
73 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); | 73 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); |
74 /* Compression function. Returns the block state after the call. */ | 74 /* Compression function. Returns the block state after the call. */ |
75 | 75 |
76 local void fill_window OF((deflate_state *s)); | 76 local void fill_window OF((deflate_state *s)); |
77 local block_state deflate_stored OF((deflate_state *s, int flush)); | 77 local block_state deflate_stored OF((deflate_state *s, int flush)); |
78 local block_state deflate_fast OF((deflate_state *s, int flush)); | 78 local block_state deflate_fast OF((deflate_state *s, int flush)); |
79 #ifndef FASTEST | 79 #ifndef FASTEST |
80 local block_state deflate_slow OF((deflate_state *s, int flush)); | 80 local block_state deflate_slow OF((deflate_state *s, int flush)); |
81 #endif | 81 #endif |
| 82 local block_state deflate_rle OF((deflate_state *s, int flush)); |
| 83 local block_state deflate_huff OF((deflate_state *s, int flush)); |
82 local void lm_init OF((deflate_state *s)); | 84 local void lm_init OF((deflate_state *s)); |
83 local void putShortMSB OF((deflate_state *s, uInt b)); | 85 local void putShortMSB OF((deflate_state *s, uInt b)); |
84 local void flush_pending OF((z_streamp strm)); | 86 local void flush_pending OF((z_streamp strm)); |
85 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); | 87 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); |
86 #ifndef FASTEST | |
87 #ifdef ASMV | 88 #ifdef ASMV |
88 void match_init OF((void)); /* asm code initialization */ | 89 void match_init OF((void)); /* asm code initialization */ |
89 uInt longest_match OF((deflate_state *s, IPos cur_match)); | 90 uInt longest_match OF((deflate_state *s, IPos cur_match)); |
90 #else | 91 #else |
91 local uInt longest_match OF((deflate_state *s, IPos cur_match)); | 92 local uInt longest_match OF((deflate_state *s, IPos cur_match)); |
92 #endif | 93 #endif |
93 #endif | |
94 local uInt longest_match_fast OF((deflate_state *s, IPos cur_match)); | |
95 | 94 |
96 #ifdef DEBUG | 95 #ifdef DEBUG |
97 local void check_match OF((deflate_state *s, IPos start, IPos match, | 96 local void check_match OF((deflate_state *s, IPos start, IPos match, |
98 int length)); | 97 int length)); |
99 #endif | 98 #endif |
100 | 99 |
101 /* =========================================================================== | 100 /* =========================================================================== |
102 * Local data | 101 * Local data |
103 */ | 102 */ |
104 | 103 |
105 #define NIL 0 | 104 #define NIL 0 |
106 /* Tail of hash chains */ | 105 /* Tail of hash chains */ |
107 | 106 |
108 #ifndef TOO_FAR | 107 #ifndef TOO_FAR |
109 # define TOO_FAR 4096 | 108 # define TOO_FAR 4096 |
110 #endif | 109 #endif |
111 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ | 110 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ |
112 | 111 |
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 | 112 /* 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 | 113 * 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 | 114 * exclude worst case performance for pathological files. Better values may be |
121 * found for specific files. | 115 * found for specific files. |
122 */ | 116 */ |
123 typedef struct config_s { | 117 typedef struct config_s { |
124 ush good_length; /* reduce lazy search above this match length */ | 118 ush good_length; /* reduce lazy search above this match length */ |
125 ush max_lazy; /* do not perform lazy search above this match length */ | 119 ush max_lazy; /* do not perform lazy search above this match length */ |
126 ush nice_length; /* quit search above this match length */ | 120 ush nice_length; /* quit search above this match length */ |
127 ush max_chain; | 121 ush max_chain; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 275 |
282 s->hash_bits = memLevel + 7; | 276 s->hash_bits = memLevel + 7; |
283 s->hash_size = 1 << s->hash_bits; | 277 s->hash_size = 1 << s->hash_bits; |
284 s->hash_mask = s->hash_size - 1; | 278 s->hash_mask = s->hash_size - 1; |
285 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); | 279 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); |
286 | 280 |
287 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); | 281 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); |
288 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); | 282 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
289 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); | 283 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
290 | 284 |
| 285 s->high_water = 0; /* nothing written to s->window yet */ |
| 286 |
291 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | 287 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
292 | 288 |
293 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); | 289 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); |
294 s->pending_buf = (uchf *) overlay; | 290 s->pending_buf = (uchf *) overlay; |
295 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); | 291 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); |
296 | 292 |
297 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || | 293 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
298 s->pending_buf == Z_NULL) { | 294 s->pending_buf == Z_NULL) { |
299 s->status = FINISH_STATE; | 295 s->status = FINISH_STATE; |
300 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); | 296 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); |
(...skipping 24 matching lines...) Expand all Loading... |
325 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || | 321 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || |
326 strm->state->wrap == 2 || | 322 strm->state->wrap == 2 || |
327 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) | 323 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) |
328 return Z_STREAM_ERROR; | 324 return Z_STREAM_ERROR; |
329 | 325 |
330 s = strm->state; | 326 s = strm->state; |
331 if (s->wrap) | 327 if (s->wrap) |
332 strm->adler = adler32(strm->adler, dictionary, dictLength); | 328 strm->adler = adler32(strm->adler, dictionary, dictLength); |
333 | 329 |
334 if (length < MIN_MATCH) return Z_OK; | 330 if (length < MIN_MATCH) return Z_OK; |
335 if (length > MAX_DIST(s)) { | 331 if (length > s->w_size) { |
336 length = MAX_DIST(s); | 332 length = s->w_size; |
337 dictionary += dictLength - length; /* use the tail of the dictionary */ | 333 dictionary += dictLength - length; /* use the tail of the dictionary */ |
338 } | 334 } |
339 zmemcpy(s->window, dictionary, length); | 335 zmemcpy(s->window, dictionary, length); |
340 s->strstart = length; | 336 s->strstart = length; |
341 s->block_start = (long)length; | 337 s->block_start = (long)length; |
342 | 338 |
343 /* Insert all strings in the hash table (except for the last two bytes). | 339 /* Insert all strings in the hash table (except for the last two bytes). |
344 * s->lookahead stays null, so s->ins_h will be recomputed at the next | 340 * s->lookahead stays null, so s->ins_h will be recomputed at the next |
345 * call of fill_window. | 341 * call of fill_window. |
346 */ | 342 */ |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 #ifdef FASTEST | 424 #ifdef FASTEST |
429 if (level != 0) level = 1; | 425 if (level != 0) level = 1; |
430 #else | 426 #else |
431 if (level == Z_DEFAULT_COMPRESSION) level = 6; | 427 if (level == Z_DEFAULT_COMPRESSION) level = 6; |
432 #endif | 428 #endif |
433 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { | 429 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { |
434 return Z_STREAM_ERROR; | 430 return Z_STREAM_ERROR; |
435 } | 431 } |
436 func = configuration_table[s->level].func; | 432 func = configuration_table[s->level].func; |
437 | 433 |
438 if (func != configuration_table[level].func && strm->total_in != 0) { | 434 if ((strategy != s->strategy || func != configuration_table[level].func) && |
| 435 strm->total_in != 0) { |
439 /* Flush the last buffer: */ | 436 /* Flush the last buffer: */ |
440 err = deflate(strm, Z_PARTIAL_FLUSH); | 437 err = deflate(strm, Z_BLOCK); |
441 } | 438 } |
442 if (s->level != level) { | 439 if (s->level != level) { |
443 s->level = level; | 440 s->level = level; |
444 s->max_lazy_match = configuration_table[level].max_lazy; | 441 s->max_lazy_match = configuration_table[level].max_lazy; |
445 s->good_match = configuration_table[level].good_length; | 442 s->good_match = configuration_table[level].good_length; |
446 s->nice_match = configuration_table[level].nice_length; | 443 s->nice_match = configuration_table[level].nice_length; |
447 s->max_chain_length = configuration_table[level].max_chain; | 444 s->max_chain_length = configuration_table[level].max_chain; |
448 } | 445 } |
449 s->strategy = strategy; | 446 s->strategy = strategy; |
450 return err; | 447 return err; |
(...skipping 23 matching lines...) Expand all Loading... |
474 * a close to exact, as well as small, upper bound on the compressed size. | 471 * a close to exact, as well as small, upper bound on the compressed size. |
475 * They are coded as constants here for a reason--if the #define's are | 472 * They are coded as constants here for a reason--if the #define's are |
476 * changed, then this function needs to be changed as well. The return | 473 * changed, then this function needs to be changed as well. The return |
477 * value for 15 and 8 only works for those exact settings. | 474 * value for 15 and 8 only works for those exact settings. |
478 * | 475 * |
479 * For any setting other than those defaults for windowBits and memLevel, | 476 * For any setting other than those defaults for windowBits and memLevel, |
480 * the value returned is a conservative worst case for the maximum expansion | 477 * the value returned is a conservative worst case for the maximum expansion |
481 * resulting from using fixed blocks instead of stored blocks, which deflate | 478 * resulting from using fixed blocks instead of stored blocks, which deflate |
482 * can emit on compressed data for some combinations of the parameters. | 479 * can emit on compressed data for some combinations of the parameters. |
483 * | 480 * |
484 * This function could be more sophisticated to provide closer upper bounds | 481 * This function could be more sophisticated to provide closer upper bounds for |
485 * for every combination of windowBits and memLevel, as well as wrap. | 482 * every combination of windowBits and memLevel. But even the conservative |
486 * But even the conservative upper bound of about 14% expansion does not | 483 * upper bound of about 14% expansion does not seem onerous for output buffer |
487 * seem onerous for output buffer allocation. | 484 * allocation. |
488 */ | 485 */ |
489 uLong ZEXPORT deflateBound(strm, sourceLen) | 486 uLong ZEXPORT deflateBound(strm, sourceLen) |
490 z_streamp strm; | 487 z_streamp strm; |
491 uLong sourceLen; | 488 uLong sourceLen; |
492 { | 489 { |
493 deflate_state *s; | 490 deflate_state *s; |
494 uLong destLen; | 491 uLong complen, wraplen; |
| 492 Bytef *str; |
495 | 493 |
496 /* conservative upper bound */ | 494 /* conservative upper bound for compressed data */ |
497 destLen = sourceLen + | 495 complen = sourceLen + |
498 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11; | 496 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; |
499 | 497 |
500 /* if can't get parameters, return conservative bound */ | 498 /* if can't get parameters, return conservative bound plus zlib wrapper */ |
501 if (strm == Z_NULL || strm->state == Z_NULL) | 499 if (strm == Z_NULL || strm->state == Z_NULL) |
502 return destLen; | 500 return complen + 6; |
| 501 |
| 502 /* compute wrapper length */ |
| 503 s = strm->state; |
| 504 switch (s->wrap) { |
| 505 case 0: /* raw deflate */ |
| 506 wraplen = 0; |
| 507 break; |
| 508 case 1: /* zlib wrapper */ |
| 509 wraplen = 6 + (s->strstart ? 4 : 0); |
| 510 break; |
| 511 case 2: /* gzip wrapper */ |
| 512 wraplen = 18; |
| 513 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ |
| 514 if (s->gzhead->extra != Z_NULL) |
| 515 wraplen += 2 + s->gzhead->extra_len; |
| 516 str = s->gzhead->name; |
| 517 if (str != Z_NULL) |
| 518 do { |
| 519 wraplen++; |
| 520 } while (*str++); |
| 521 str = s->gzhead->comment; |
| 522 if (str != Z_NULL) |
| 523 do { |
| 524 wraplen++; |
| 525 } while (*str++); |
| 526 if (s->gzhead->hcrc) |
| 527 wraplen += 2; |
| 528 } |
| 529 break; |
| 530 default: /* for compiler happiness */ |
| 531 wraplen = 6; |
| 532 } |
503 | 533 |
504 /* if not default parameters, return conservative bound */ | 534 /* if not default parameters, return conservative bound */ |
505 s = strm->state; | |
506 if (s->w_bits != 15 || s->hash_bits != 8 + 7) | 535 if (s->w_bits != 15 || s->hash_bits != 8 + 7) |
507 return destLen; | 536 return complen + wraplen; |
508 | 537 |
509 /* default settings: return tight bound for that case */ | 538 /* default settings: return tight bound for that case */ |
510 return compressBound(sourceLen); | 539 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + |
| 540 (sourceLen >> 25) + 13 - 6 + wraplen; |
511 } | 541 } |
512 | 542 |
513 /* ========================================================================= | 543 /* ========================================================================= |
514 * Put a short in the pending buffer. The 16-bit value is put in MSB order. | 544 * Put a short in the pending buffer. The 16-bit value is put in MSB order. |
515 * IN assertion: the stream state is correct and there is enough room in | 545 * IN assertion: the stream state is correct and there is enough room in |
516 * pending_buf. | 546 * pending_buf. |
517 */ | 547 */ |
518 local void putShortMSB (s, b) | 548 local void putShortMSB (s, b) |
519 deflate_state *s; | 549 deflate_state *s; |
520 uInt b; | 550 uInt b; |
(...skipping 29 matching lines...) Expand all Loading... |
550 | 580 |
551 /* ========================================================================= */ | 581 /* ========================================================================= */ |
552 int ZEXPORT deflate (strm, flush) | 582 int ZEXPORT deflate (strm, flush) |
553 z_streamp strm; | 583 z_streamp strm; |
554 int flush; | 584 int flush; |
555 { | 585 { |
556 int old_flush; /* value of flush param for previous deflate call */ | 586 int old_flush; /* value of flush param for previous deflate call */ |
557 deflate_state *s; | 587 deflate_state *s; |
558 | 588 |
559 if (strm == Z_NULL || strm->state == Z_NULL || | 589 if (strm == Z_NULL || strm->state == Z_NULL || |
560 flush > Z_FINISH || flush < 0) { | 590 flush > Z_BLOCK || flush < 0) { |
561 return Z_STREAM_ERROR; | 591 return Z_STREAM_ERROR; |
562 } | 592 } |
563 s = strm->state; | 593 s = strm->state; |
564 | 594 |
565 if (strm->next_out == Z_NULL || | 595 if (strm->next_out == Z_NULL || |
566 (strm->next_in == Z_NULL && strm->avail_in != 0) || | 596 (strm->next_in == Z_NULL && strm->avail_in != 0) || |
567 (s->status == FINISH_STATE && flush != Z_FINISH)) { | 597 (s->status == FINISH_STATE && flush != Z_FINISH)) { |
568 ERR_RETURN(strm, Z_STREAM_ERROR); | 598 ERR_RETURN(strm, Z_STREAM_ERROR); |
569 } | 599 } |
570 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); | 600 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); |
571 | 601 |
572 s->strm = strm; /* just in case */ | 602 s->strm = strm; /* just in case */ |
573 old_flush = s->last_flush; | 603 old_flush = s->last_flush; |
574 s->last_flush = flush; | 604 s->last_flush = flush; |
575 | 605 |
576 /* Write the header */ | 606 /* Write the header */ |
577 if (s->status == INIT_STATE) { | 607 if (s->status == INIT_STATE) { |
578 #ifdef GZIP | 608 #ifdef GZIP |
579 if (s->wrap == 2) { | 609 if (s->wrap == 2) { |
580 strm->adler = crc32(0L, Z_NULL, 0); | 610 strm->adler = crc32(0L, Z_NULL, 0); |
581 put_byte(s, 31); | 611 put_byte(s, 31); |
582 put_byte(s, 139); | 612 put_byte(s, 139); |
583 put_byte(s, 8); | 613 put_byte(s, 8); |
584 if (s->gzhead == NULL) { | 614 if (s->gzhead == Z_NULL) { |
585 put_byte(s, 0); | 615 put_byte(s, 0); |
586 put_byte(s, 0); | 616 put_byte(s, 0); |
587 put_byte(s, 0); | 617 put_byte(s, 0); |
588 put_byte(s, 0); | 618 put_byte(s, 0); |
589 put_byte(s, 0); | 619 put_byte(s, 0); |
590 put_byte(s, s->level == 9 ? 2 : | 620 put_byte(s, s->level == 9 ? 2 : |
591 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? | 621 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
592 4 : 0)); | 622 4 : 0)); |
593 put_byte(s, OS_CODE); | 623 put_byte(s, OS_CODE); |
594 s->status = BUSY_STATE; | 624 s->status = BUSY_STATE; |
595 } | 625 } |
596 else { | 626 else { |
597 put_byte(s, (s->gzhead->text ? 1 : 0) + | 627 put_byte(s, (s->gzhead->text ? 1 : 0) + |
598 (s->gzhead->hcrc ? 2 : 0) + | 628 (s->gzhead->hcrc ? 2 : 0) + |
599 (s->gzhead->extra == Z_NULL ? 0 : 4) + | 629 (s->gzhead->extra == Z_NULL ? 0 : 4) + |
600 (s->gzhead->name == Z_NULL ? 0 : 8) + | 630 (s->gzhead->name == Z_NULL ? 0 : 8) + |
601 (s->gzhead->comment == Z_NULL ? 0 : 16) | 631 (s->gzhead->comment == Z_NULL ? 0 : 16) |
602 ); | 632 ); |
603 put_byte(s, (Byte)(s->gzhead->time & 0xff)); | 633 put_byte(s, (Byte)(s->gzhead->time & 0xff)); |
604 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); | 634 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); |
605 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); | 635 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); |
606 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); | 636 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); |
607 put_byte(s, s->level == 9 ? 2 : | 637 put_byte(s, s->level == 9 ? 2 : |
608 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? | 638 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? |
609 4 : 0)); | 639 4 : 0)); |
610 put_byte(s, s->gzhead->os & 0xff); | 640 put_byte(s, s->gzhead->os & 0xff); |
611 if (s->gzhead->extra != NULL) { | 641 if (s->gzhead->extra != Z_NULL) { |
612 put_byte(s, s->gzhead->extra_len & 0xff); | 642 put_byte(s, s->gzhead->extra_len & 0xff); |
613 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); | 643 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); |
614 } | 644 } |
615 if (s->gzhead->hcrc) | 645 if (s->gzhead->hcrc) |
616 strm->adler = crc32(strm->adler, s->pending_buf, | 646 strm->adler = crc32(strm->adler, s->pending_buf, |
617 s->pending); | 647 s->pending); |
618 s->gzindex = 0; | 648 s->gzindex = 0; |
619 s->status = EXTRA_STATE; | 649 s->status = EXTRA_STATE; |
620 } | 650 } |
621 } | 651 } |
(...skipping 21 matching lines...) Expand all Loading... |
643 /* Save the adler32 of the preset dictionary: */ | 673 /* Save the adler32 of the preset dictionary: */ |
644 if (s->strstart != 0) { | 674 if (s->strstart != 0) { |
645 putShortMSB(s, (uInt)(strm->adler >> 16)); | 675 putShortMSB(s, (uInt)(strm->adler >> 16)); |
646 putShortMSB(s, (uInt)(strm->adler & 0xffff)); | 676 putShortMSB(s, (uInt)(strm->adler & 0xffff)); |
647 } | 677 } |
648 strm->adler = adler32(0L, Z_NULL, 0); | 678 strm->adler = adler32(0L, Z_NULL, 0); |
649 } | 679 } |
650 } | 680 } |
651 #ifdef GZIP | 681 #ifdef GZIP |
652 if (s->status == EXTRA_STATE) { | 682 if (s->status == EXTRA_STATE) { |
653 if (s->gzhead->extra != NULL) { | 683 if (s->gzhead->extra != Z_NULL) { |
654 uInt beg = s->pending; /* start of bytes to update crc */ | 684 uInt beg = s->pending; /* start of bytes to update crc */ |
655 | 685 |
656 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { | 686 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { |
657 if (s->pending == s->pending_buf_size) { | 687 if (s->pending == s->pending_buf_size) { |
658 if (s->gzhead->hcrc && s->pending > beg) | 688 if (s->gzhead->hcrc && s->pending > beg) |
659 strm->adler = crc32(strm->adler, s->pending_buf + beg, | 689 strm->adler = crc32(strm->adler, s->pending_buf + beg, |
660 s->pending - beg); | 690 s->pending - beg); |
661 flush_pending(strm); | 691 flush_pending(strm); |
662 beg = s->pending; | 692 beg = s->pending; |
663 if (s->pending == s->pending_buf_size) | 693 if (s->pending == s->pending_buf_size) |
664 break; | 694 break; |
665 } | 695 } |
666 put_byte(s, s->gzhead->extra[s->gzindex]); | 696 put_byte(s, s->gzhead->extra[s->gzindex]); |
667 s->gzindex++; | 697 s->gzindex++; |
668 } | 698 } |
669 if (s->gzhead->hcrc && s->pending > beg) | 699 if (s->gzhead->hcrc && s->pending > beg) |
670 strm->adler = crc32(strm->adler, s->pending_buf + beg, | 700 strm->adler = crc32(strm->adler, s->pending_buf + beg, |
671 s->pending - beg); | 701 s->pending - beg); |
672 if (s->gzindex == s->gzhead->extra_len) { | 702 if (s->gzindex == s->gzhead->extra_len) { |
673 s->gzindex = 0; | 703 s->gzindex = 0; |
674 s->status = NAME_STATE; | 704 s->status = NAME_STATE; |
675 } | 705 } |
676 } | 706 } |
677 else | 707 else |
678 s->status = NAME_STATE; | 708 s->status = NAME_STATE; |
679 } | 709 } |
680 if (s->status == NAME_STATE) { | 710 if (s->status == NAME_STATE) { |
681 if (s->gzhead->name != NULL) { | 711 if (s->gzhead->name != Z_NULL) { |
682 uInt beg = s->pending; /* start of bytes to update crc */ | 712 uInt beg = s->pending; /* start of bytes to update crc */ |
683 int val; | 713 int val; |
684 | 714 |
685 do { | 715 do { |
686 if (s->pending == s->pending_buf_size) { | 716 if (s->pending == s->pending_buf_size) { |
687 if (s->gzhead->hcrc && s->pending > beg) | 717 if (s->gzhead->hcrc && s->pending > beg) |
688 strm->adler = crc32(strm->adler, s->pending_buf + beg, | 718 strm->adler = crc32(strm->adler, s->pending_buf + beg, |
689 s->pending - beg); | 719 s->pending - beg); |
690 flush_pending(strm); | 720 flush_pending(strm); |
691 beg = s->pending; | 721 beg = s->pending; |
(...skipping 10 matching lines...) Expand all Loading... |
702 s->pending - beg); | 732 s->pending - beg); |
703 if (val == 0) { | 733 if (val == 0) { |
704 s->gzindex = 0; | 734 s->gzindex = 0; |
705 s->status = COMMENT_STATE; | 735 s->status = COMMENT_STATE; |
706 } | 736 } |
707 } | 737 } |
708 else | 738 else |
709 s->status = COMMENT_STATE; | 739 s->status = COMMENT_STATE; |
710 } | 740 } |
711 if (s->status == COMMENT_STATE) { | 741 if (s->status == COMMENT_STATE) { |
712 if (s->gzhead->comment != NULL) { | 742 if (s->gzhead->comment != Z_NULL) { |
713 uInt beg = s->pending; /* start of bytes to update crc */ | 743 uInt beg = s->pending; /* start of bytes to update crc */ |
714 int val; | 744 int val; |
715 | 745 |
716 do { | 746 do { |
717 if (s->pending == s->pending_buf_size) { | 747 if (s->pending == s->pending_buf_size) { |
718 if (s->gzhead->hcrc && s->pending > beg) | 748 if (s->gzhead->hcrc && s->pending > beg) |
719 strm->adler = crc32(strm->adler, s->pending_buf + beg, | 749 strm->adler = crc32(strm->adler, s->pending_buf + beg, |
720 s->pending - beg); | 750 s->pending - beg); |
721 flush_pending(strm); | 751 flush_pending(strm); |
722 beg = s->pending; | 752 beg = s->pending; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 if (s->status == FINISH_STATE && strm->avail_in != 0) { | 810 if (s->status == FINISH_STATE && strm->avail_in != 0) { |
781 ERR_RETURN(strm, Z_BUF_ERROR); | 811 ERR_RETURN(strm, Z_BUF_ERROR); |
782 } | 812 } |
783 | 813 |
784 /* Start a new block or continue the current one. | 814 /* Start a new block or continue the current one. |
785 */ | 815 */ |
786 if (strm->avail_in != 0 || s->lookahead != 0 || | 816 if (strm->avail_in != 0 || s->lookahead != 0 || |
787 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { | 817 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { |
788 block_state bstate; | 818 block_state bstate; |
789 | 819 |
790 bstate = (*(configuration_table[s->level].func))(s, flush); | 820 bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : |
| 821 (s->strategy == Z_RLE ? deflate_rle(s, flush) : |
| 822 (*(configuration_table[s->level].func))(s, flush)); |
791 | 823 |
792 if (bstate == finish_started || bstate == finish_done) { | 824 if (bstate == finish_started || bstate == finish_done) { |
793 s->status = FINISH_STATE; | 825 s->status = FINISH_STATE; |
794 } | 826 } |
795 if (bstate == need_more || bstate == finish_started) { | 827 if (bstate == need_more || bstate == finish_started) { |
796 if (strm->avail_out == 0) { | 828 if (strm->avail_out == 0) { |
797 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ | 829 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ |
798 } | 830 } |
799 return Z_OK; | 831 return Z_OK; |
800 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call | 832 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call |
801 * of deflate should use the same flush parameter to make sure | 833 * of deflate should use the same flush parameter to make sure |
802 * that the flush is complete. So we don't have to output an | 834 * that the flush is complete. So we don't have to output an |
803 * empty block here, this will be done at next call. This also | 835 * empty block here, this will be done at next call. This also |
804 * ensures that for a very small output buffer, we emit at most | 836 * ensures that for a very small output buffer, we emit at most |
805 * one empty block. | 837 * one empty block. |
806 */ | 838 */ |
807 } | 839 } |
808 if (bstate == block_done) { | 840 if (bstate == block_done) { |
809 if (flush == Z_PARTIAL_FLUSH) { | 841 if (flush == Z_PARTIAL_FLUSH) { |
810 _tr_align(s); | 842 _tr_align(s); |
811 } else { /* FULL_FLUSH or SYNC_FLUSH */ | 843 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ |
812 _tr_stored_block(s, (char*)0, 0L, 0); | 844 _tr_stored_block(s, (char*)0, 0L, 0); |
813 /* For a full flush, this empty block will be recognized | 845 /* For a full flush, this empty block will be recognized |
814 * as a special marker by inflate_sync(). | 846 * as a special marker by inflate_sync(). |
815 */ | 847 */ |
816 if (flush == Z_FULL_FLUSH) { | 848 if (flush == Z_FULL_FLUSH) { |
817 CLEAR_HASH(s); /* forget history */ | 849 CLEAR_HASH(s); /* forget history */ |
| 850 if (s->lookahead == 0) { |
| 851 s->strstart = 0; |
| 852 s->block_start = 0L; |
| 853 } |
818 } | 854 } |
819 } | 855 } |
820 flush_pending(strm); | 856 flush_pending(strm); |
821 if (strm->avail_out == 0) { | 857 if (strm->avail_out == 0) { |
822 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ | 858 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ |
823 return Z_OK; | 859 return Z_OK; |
824 } | 860 } |
825 } | 861 } |
826 } | 862 } |
827 Assert(strm->avail_out > 0, "bug2"); | 863 Assert(strm->avail_out > 0, "bug2"); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 scan_end = scan[best_len]; | 1196 scan_end = scan[best_len]; |
1161 #endif | 1197 #endif |
1162 } | 1198 } |
1163 } while ((cur_match = prev[cur_match & wmask]) > limit | 1199 } while ((cur_match = prev[cur_match & wmask]) > limit |
1164 && --chain_length != 0); | 1200 && --chain_length != 0); |
1165 | 1201 |
1166 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; | 1202 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; |
1167 return s->lookahead; | 1203 return s->lookahead; |
1168 } | 1204 } |
1169 #endif /* ASMV */ | 1205 #endif /* ASMV */ |
1170 #endif /* FASTEST */ | 1206 |
| 1207 #else /* FASTEST */ |
1171 | 1208 |
1172 /* --------------------------------------------------------------------------- | 1209 /* --------------------------------------------------------------------------- |
1173 * Optimized version for level == 1 or strategy == Z_RLE only | 1210 * Optimized version for FASTEST only |
1174 */ | 1211 */ |
1175 local uInt longest_match_fast(s, cur_match) | 1212 local uInt longest_match(s, cur_match) |
1176 deflate_state *s; | 1213 deflate_state *s; |
1177 IPos cur_match; /* current match */ | 1214 IPos cur_match; /* current match */ |
1178 { | 1215 { |
1179 register Bytef *scan = s->window + s->strstart; /* current string */ | 1216 register Bytef *scan = s->window + s->strstart; /* current string */ |
1180 register Bytef *match; /* matched string */ | 1217 register Bytef *match; /* matched string */ |
1181 register int len; /* length of current match */ | 1218 register int len; /* length of current match */ |
1182 register Bytef *strend = s->window + s->strstart + MAX_MATCH; | 1219 register Bytef *strend = s->window + s->strstart + MAX_MATCH; |
1183 | 1220 |
1184 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. | 1221 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. |
1185 * It is easy to get rid of this optimization if necessary. | 1222 * It is easy to get rid of this optimization if necessary. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1218 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); | 1255 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); |
1219 | 1256 |
1220 len = MAX_MATCH - (int)(strend - scan); | 1257 len = MAX_MATCH - (int)(strend - scan); |
1221 | 1258 |
1222 if (len < MIN_MATCH) return MIN_MATCH - 1; | 1259 if (len < MIN_MATCH) return MIN_MATCH - 1; |
1223 | 1260 |
1224 s->match_start = cur_match; | 1261 s->match_start = cur_match; |
1225 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; | 1262 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; |
1226 } | 1263 } |
1227 | 1264 |
| 1265 #endif /* FASTEST */ |
| 1266 |
1228 #ifdef DEBUG | 1267 #ifdef DEBUG |
1229 /* =========================================================================== | 1268 /* =========================================================================== |
1230 * Check that the match at match_start is indeed a match. | 1269 * Check that the match at match_start is indeed a match. |
1231 */ | 1270 */ |
1232 local void check_match(s, start, match, length) | 1271 local void check_match(s, start, match, length) |
1233 deflate_state *s; | 1272 deflate_state *s; |
1234 IPos start, match; | 1273 IPos start, match; |
1235 int length; | 1274 int length; |
1236 { | 1275 { |
1237 /* check that the match is indeed a match */ | 1276 /* check that the match is indeed a match */ |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 s->match_start -= wsize; | 1335 s->match_start -= wsize; |
1297 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ | 1336 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ |
1298 s->block_start -= (long) wsize; | 1337 s->block_start -= (long) wsize; |
1299 | 1338 |
1300 /* Slide the hash table (could be avoided with 32 bit values | 1339 /* Slide the hash table (could be avoided with 32 bit values |
1301 at the expense of memory usage). We slide even when level == 0 | 1340 at the expense of memory usage). We slide even when level == 0 |
1302 to keep the hash table consistent if we switch back to level > 0 | 1341 to keep the hash table consistent if we switch back to level > 0 |
1303 later. (Using level 0 permanently is not an optimal usage of | 1342 later. (Using level 0 permanently is not an optimal usage of |
1304 zlib, so we don't care about this pathological case.) | 1343 zlib, so we don't care about this pathological case.) |
1305 */ | 1344 */ |
1306 /* %%% avoid this when Z_RLE */ | |
1307 n = s->hash_size; | 1345 n = s->hash_size; |
1308 p = &s->head[n]; | 1346 p = &s->head[n]; |
1309 do { | 1347 do { |
1310 m = *--p; | 1348 m = *--p; |
1311 *p = (Pos)(m >= wsize ? m-wsize : NIL); | 1349 *p = (Pos)(m >= wsize ? m-wsize : NIL); |
1312 } while (--n); | 1350 } while (--n); |
1313 | 1351 |
1314 n = wsize; | 1352 n = wsize; |
1315 #ifndef FASTEST | 1353 #ifndef FASTEST |
1316 p = &s->prev[n]; | 1354 p = &s->prev[n]; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1348 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); | 1386 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); |
1349 #if MIN_MATCH != 3 | 1387 #if MIN_MATCH != 3 |
1350 Call UPDATE_HASH() MIN_MATCH-3 more times | 1388 Call UPDATE_HASH() MIN_MATCH-3 more times |
1351 #endif | 1389 #endif |
1352 } | 1390 } |
1353 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, | 1391 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, |
1354 * but this is not important since only literal bytes will be emitted. | 1392 * but this is not important since only literal bytes will be emitted. |
1355 */ | 1393 */ |
1356 | 1394 |
1357 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); | 1395 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 1396 |
| 1397 /* If the WIN_INIT bytes after the end of the current data have never been |
| 1398 * written, then zero those bytes in order to avoid memory check reports of |
| 1399 * the use of uninitialized (or uninitialised as Julian writes) bytes by |
| 1400 * the longest match routines. Update the high water mark for the next |
| 1401 * time through here. WIN_INIT is set to MAX_MATCH since the longest match |
| 1402 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. |
| 1403 */ |
| 1404 if (s->high_water < s->window_size) { |
| 1405 ulg curr = s->strstart + (ulg)(s->lookahead); |
| 1406 ulg init; |
| 1407 |
| 1408 if (s->high_water < curr) { |
| 1409 /* Previous high water mark below current data -- zero WIN_INIT |
| 1410 * bytes or up to end of window, whichever is less. |
| 1411 */ |
| 1412 init = s->window_size - curr; |
| 1413 if (init > WIN_INIT) |
| 1414 init = WIN_INIT; |
| 1415 zmemzero(s->window + curr, (unsigned)init); |
| 1416 s->high_water = curr + init; |
| 1417 } |
| 1418 else if (s->high_water < (ulg)curr + WIN_INIT) { |
| 1419 /* High water mark at or above current data, but below current data |
| 1420 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
| 1421 * to end of window, whichever is less. |
| 1422 */ |
| 1423 init = (ulg)curr + WIN_INIT - s->high_water; |
| 1424 if (init > s->window_size - s->high_water) |
| 1425 init = s->window_size - s->high_water; |
| 1426 zmemzero(s->window + s->high_water, (unsigned)init); |
| 1427 s->high_water += init; |
| 1428 } |
| 1429 } |
1358 } | 1430 } |
1359 | 1431 |
1360 /* =========================================================================== | 1432 /* =========================================================================== |
1361 * Flush the current block, with given end-of-file flag. | 1433 * Flush the current block, with given end-of-file flag. |
1362 * IN assertion: strstart is set to the end of the current match. | 1434 * IN assertion: strstart is set to the end of the current match. |
1363 */ | 1435 */ |
1364 #define FLUSH_BLOCK_ONLY(s, eof) { \ | 1436 #define FLUSH_BLOCK_ONLY(s, last) { \ |
1365 _tr_flush_block(s, (s->block_start >= 0L ? \ | 1437 _tr_flush_block(s, (s->block_start >= 0L ? \ |
1366 (charf *)&s->window[(unsigned)s->block_start] : \ | 1438 (charf *)&s->window[(unsigned)s->block_start] : \ |
1367 (charf *)Z_NULL), \ | 1439 (charf *)Z_NULL), \ |
1368 (ulg)((long)s->strstart - s->block_start), \ | 1440 (ulg)((long)s->strstart - s->block_start), \ |
1369 (eof)); \ | 1441 (last)); \ |
1370 s->block_start = s->strstart; \ | 1442 s->block_start = s->strstart; \ |
1371 flush_pending(s->strm); \ | 1443 flush_pending(s->strm); \ |
1372 Tracev((stderr,"[FLUSH]")); \ | 1444 Tracev((stderr,"[FLUSH]")); \ |
1373 } | 1445 } |
1374 | 1446 |
1375 /* Same but force premature exit if necessary. */ | 1447 /* Same but force premature exit if necessary. */ |
1376 #define FLUSH_BLOCK(s, eof) { \ | 1448 #define FLUSH_BLOCK(s, last) { \ |
1377 FLUSH_BLOCK_ONLY(s, eof); \ | 1449 FLUSH_BLOCK_ONLY(s, last); \ |
1378 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \ | 1450 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ |
1379 } | 1451 } |
1380 | 1452 |
1381 /* =========================================================================== | 1453 /* =========================================================================== |
1382 * Copy without compression as much as possible from the input stream, return | 1454 * Copy without compression as much as possible from the input stream, return |
1383 * the current block state. | 1455 * the current block state. |
1384 * This function does not insert new strings in the dictionary since | 1456 * This function does not insert new strings in the dictionary since |
1385 * uncompressible data is probably not useful. This function is used | 1457 * uncompressible data is probably not useful. This function is used |
1386 * only for the level=0 compression option. | 1458 * only for the level=0 compression option. |
1387 * NOTE: this function should be optimized to avoid extra copying from | 1459 * NOTE: this function should be optimized to avoid extra copying from |
1388 * window to pending_buf. | 1460 * window to pending_buf. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1442 * Compress as much as possible from the input stream, return the current | 1514 * Compress as much as possible from the input stream, return the current |
1443 * block state. | 1515 * block state. |
1444 * This function does not perform lazy evaluation of matches and inserts | 1516 * This function does not perform lazy evaluation of matches and inserts |
1445 * new strings in the dictionary only for unmatched strings or for short | 1517 * new strings in the dictionary only for unmatched strings or for short |
1446 * matches. It is used only for the fast compression options. | 1518 * matches. It is used only for the fast compression options. |
1447 */ | 1519 */ |
1448 local block_state deflate_fast(s, flush) | 1520 local block_state deflate_fast(s, flush) |
1449 deflate_state *s; | 1521 deflate_state *s; |
1450 int flush; | 1522 int flush; |
1451 { | 1523 { |
1452 IPos hash_head = NIL; /* head of the hash chain */ | 1524 IPos hash_head; /* head of the hash chain */ |
1453 int bflush; /* set if current block must be flushed */ | 1525 int bflush; /* set if current block must be flushed */ |
1454 | 1526 |
1455 for (;;) { | 1527 for (;;) { |
1456 /* Make sure that we always have enough lookahead, except | 1528 /* Make sure that we always have enough lookahead, except |
1457 * at the end of the input file. We need MAX_MATCH bytes | 1529 * at the end of the input file. We need MAX_MATCH bytes |
1458 * for the next match, plus MIN_MATCH bytes to insert the | 1530 * for the next match, plus MIN_MATCH bytes to insert the |
1459 * string following the next match. | 1531 * string following the next match. |
1460 */ | 1532 */ |
1461 if (s->lookahead < MIN_LOOKAHEAD) { | 1533 if (s->lookahead < MIN_LOOKAHEAD) { |
1462 fill_window(s); | 1534 fill_window(s); |
1463 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { | 1535 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
1464 return need_more; | 1536 return need_more; |
1465 } | 1537 } |
1466 if (s->lookahead == 0) break; /* flush the current block */ | 1538 if (s->lookahead == 0) break; /* flush the current block */ |
1467 } | 1539 } |
1468 | 1540 |
1469 /* Insert the string window[strstart .. strstart+2] in the | 1541 /* Insert the string window[strstart .. strstart+2] in the |
1470 * dictionary, and set hash_head to the head of the hash chain: | 1542 * dictionary, and set hash_head to the head of the hash chain: |
1471 */ | 1543 */ |
| 1544 hash_head = NIL; |
1472 if (s->lookahead >= MIN_MATCH) { | 1545 if (s->lookahead >= MIN_MATCH) { |
1473 INSERT_STRING(s, s->strstart, hash_head); | 1546 INSERT_STRING(s, s->strstart, hash_head); |
1474 } | 1547 } |
1475 | 1548 |
1476 /* Find the longest match, discarding those <= prev_length. | 1549 /* Find the longest match, discarding those <= prev_length. |
1477 * At this point we have always match_length < MIN_MATCH | 1550 * At this point we have always match_length < MIN_MATCH |
1478 */ | 1551 */ |
1479 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { | 1552 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { |
1480 /* To simplify the code, we prevent matches with the string | 1553 /* To simplify the code, we prevent matches with the string |
1481 * of window index 0 (in particular we have to avoid a match | 1554 * of window index 0 (in particular we have to avoid a match |
1482 * of the string with itself at the start of the input file). | 1555 * of the string with itself at the start of the input file). |
1483 */ | 1556 */ |
1484 #ifdef FASTEST | 1557 s->match_length = longest_match (s, hash_head); |
1485 if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) || | 1558 /* longest_match() sets match_start */ |
1486 (s->strategy == Z_RLE && s->strstart - hash_head == 1)) { | |
1487 s->match_length = longest_match_fast (s, hash_head); | |
1488 } | |
1489 #else | |
1490 if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { | |
1491 s->match_length = longest_match (s, hash_head); | |
1492 } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { | |
1493 s->match_length = longest_match_fast (s, hash_head); | |
1494 } | |
1495 #endif | |
1496 /* longest_match() or longest_match_fast() sets match_start */ | |
1497 } | 1559 } |
1498 if (s->match_length >= MIN_MATCH) { | 1560 if (s->match_length >= MIN_MATCH) { |
1499 check_match(s, s->strstart, s->match_start, s->match_length); | 1561 check_match(s, s->strstart, s->match_start, s->match_length); |
1500 | 1562 |
1501 _tr_tally_dist(s, s->strstart - s->match_start, | 1563 _tr_tally_dist(s, s->strstart - s->match_start, |
1502 s->match_length - MIN_MATCH, bflush); | 1564 s->match_length - MIN_MATCH, bflush); |
1503 | 1565 |
1504 s->lookahead -= s->match_length; | 1566 s->lookahead -= s->match_length; |
1505 | 1567 |
1506 /* Insert new strings in the hash table only if the match length | 1568 /* Insert new strings in the hash table only if the match length |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1548 #ifndef FASTEST | 1610 #ifndef FASTEST |
1549 /* =========================================================================== | 1611 /* =========================================================================== |
1550 * Same as above, but achieves better compression. We use a lazy | 1612 * Same as above, but achieves better compression. We use a lazy |
1551 * evaluation for matches: a match is finally adopted only if there is | 1613 * evaluation for matches: a match is finally adopted only if there is |
1552 * no better match at the next window position. | 1614 * no better match at the next window position. |
1553 */ | 1615 */ |
1554 local block_state deflate_slow(s, flush) | 1616 local block_state deflate_slow(s, flush) |
1555 deflate_state *s; | 1617 deflate_state *s; |
1556 int flush; | 1618 int flush; |
1557 { | 1619 { |
1558 IPos hash_head = NIL; /* head of hash chain */ | 1620 IPos hash_head; /* head of hash chain */ |
1559 int bflush; /* set if current block must be flushed */ | 1621 int bflush; /* set if current block must be flushed */ |
1560 | 1622 |
1561 /* Process the input block. */ | 1623 /* Process the input block. */ |
1562 for (;;) { | 1624 for (;;) { |
1563 /* Make sure that we always have enough lookahead, except | 1625 /* Make sure that we always have enough lookahead, except |
1564 * at the end of the input file. We need MAX_MATCH bytes | 1626 * at the end of the input file. We need MAX_MATCH bytes |
1565 * for the next match, plus MIN_MATCH bytes to insert the | 1627 * for the next match, plus MIN_MATCH bytes to insert the |
1566 * string following the next match. | 1628 * string following the next match. |
1567 */ | 1629 */ |
1568 if (s->lookahead < MIN_LOOKAHEAD) { | 1630 if (s->lookahead < MIN_LOOKAHEAD) { |
1569 fill_window(s); | 1631 fill_window(s); |
1570 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { | 1632 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
1571 return need_more; | 1633 return need_more; |
1572 } | 1634 } |
1573 if (s->lookahead == 0) break; /* flush the current block */ | 1635 if (s->lookahead == 0) break; /* flush the current block */ |
1574 } | 1636 } |
1575 | 1637 |
1576 /* Insert the string window[strstart .. strstart+2] in the | 1638 /* Insert the string window[strstart .. strstart+2] in the |
1577 * dictionary, and set hash_head to the head of the hash chain: | 1639 * dictionary, and set hash_head to the head of the hash chain: |
1578 */ | 1640 */ |
| 1641 hash_head = NIL; |
1579 if (s->lookahead >= MIN_MATCH) { | 1642 if (s->lookahead >= MIN_MATCH) { |
1580 INSERT_STRING(s, s->strstart, hash_head); | 1643 INSERT_STRING(s, s->strstart, hash_head); |
1581 } | 1644 } |
1582 | 1645 |
1583 /* Find the longest match, discarding those <= prev_length. | 1646 /* Find the longest match, discarding those <= prev_length. |
1584 */ | 1647 */ |
1585 s->prev_length = s->match_length, s->prev_match = s->match_start; | 1648 s->prev_length = s->match_length, s->prev_match = s->match_start; |
1586 s->match_length = MIN_MATCH-1; | 1649 s->match_length = MIN_MATCH-1; |
1587 | 1650 |
1588 if (hash_head != NIL && s->prev_length < s->max_lazy_match && | 1651 if (hash_head != NIL && s->prev_length < s->max_lazy_match && |
1589 s->strstart - hash_head <= MAX_DIST(s)) { | 1652 s->strstart - hash_head <= MAX_DIST(s)) { |
1590 /* To simplify the code, we prevent matches with the string | 1653 /* To simplify the code, we prevent matches with the string |
1591 * of window index 0 (in particular we have to avoid a match | 1654 * of window index 0 (in particular we have to avoid a match |
1592 * of the string with itself at the start of the input file). | 1655 * of the string with itself at the start of the input file). |
1593 */ | 1656 */ |
1594 if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) { | 1657 s->match_length = longest_match (s, hash_head); |
1595 s->match_length = longest_match (s, hash_head); | 1658 /* longest_match() sets match_start */ |
1596 } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) { | |
1597 s->match_length = longest_match_fast (s, hash_head); | |
1598 } | |
1599 /* longest_match() or longest_match_fast() sets match_start */ | |
1600 | 1659 |
1601 if (s->match_length <= 5 && (s->strategy == Z_FILTERED | 1660 if (s->match_length <= 5 && (s->strategy == Z_FILTERED |
1602 #if TOO_FAR <= 32767 | 1661 #if TOO_FAR <= 32767 |
1603 || (s->match_length == MIN_MATCH && | 1662 || (s->match_length == MIN_MATCH && |
1604 s->strstart - s->match_start > TOO_FAR) | 1663 s->strstart - s->match_start > TOO_FAR) |
1605 #endif | 1664 #endif |
1606 )) { | 1665 )) { |
1607 | 1666 |
1608 /* If prev_match is also MIN_MATCH, match_start is garbage | 1667 /* If prev_match is also MIN_MATCH, match_start is garbage |
1609 * but we will ignore the current match anyway. | 1668 * but we will ignore the current match anyway. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1667 if (s->match_available) { | 1726 if (s->match_available) { |
1668 Tracevv((stderr,"%c", s->window[s->strstart-1])); | 1727 Tracevv((stderr,"%c", s->window[s->strstart-1])); |
1669 _tr_tally_lit(s, s->window[s->strstart-1], bflush); | 1728 _tr_tally_lit(s, s->window[s->strstart-1], bflush); |
1670 s->match_available = 0; | 1729 s->match_available = 0; |
1671 } | 1730 } |
1672 FLUSH_BLOCK(s, flush == Z_FINISH); | 1731 FLUSH_BLOCK(s, flush == Z_FINISH); |
1673 return flush == Z_FINISH ? finish_done : block_done; | 1732 return flush == Z_FINISH ? finish_done : block_done; |
1674 } | 1733 } |
1675 #endif /* FASTEST */ | 1734 #endif /* FASTEST */ |
1676 | 1735 |
1677 #if 0 | |
1678 /* =========================================================================== | 1736 /* =========================================================================== |
1679 * For Z_RLE, simply look for runs of bytes, generate matches only of distance | 1737 * For Z_RLE, simply look for runs of bytes, generate matches only of distance |
1680 * one. Do not maintain a hash table. (It will be regenerated if this run of | 1738 * one. Do not maintain a hash table. (It will be regenerated if this run of |
1681 * deflate switches away from Z_RLE.) | 1739 * deflate switches away from Z_RLE.) |
1682 */ | 1740 */ |
1683 local block_state deflate_rle(s, flush) | 1741 local block_state deflate_rle(s, flush) |
1684 deflate_state *s; | 1742 deflate_state *s; |
1685 int flush; | 1743 int flush; |
1686 { | 1744 { |
1687 int bflush; /* set if current block must be flushed */ | 1745 int bflush; /* set if current block must be flushed */ |
1688 uInt run; /* length of run */ | 1746 uInt prev; /* byte at distance one to match */ |
1689 uInt max; /* maximum length of run */ | 1747 Bytef *scan, *strend; /* scan goes up to strend for length of run */ |
1690 uInt prev; /* byte at distance one to match */ | |
1691 Bytef *scan; /* scan for end of run */ | |
1692 | 1748 |
1693 for (;;) { | 1749 for (;;) { |
1694 /* Make sure that we always have enough lookahead, except | 1750 /* Make sure that we always have enough lookahead, except |
1695 * at the end of the input file. We need MAX_MATCH bytes | 1751 * at the end of the input file. We need MAX_MATCH bytes |
1696 * for the longest encodable run. | 1752 * for the longest encodable run. |
1697 */ | 1753 */ |
1698 if (s->lookahead < MAX_MATCH) { | 1754 if (s->lookahead < MAX_MATCH) { |
1699 fill_window(s); | 1755 fill_window(s); |
1700 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { | 1756 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { |
1701 return need_more; | 1757 return need_more; |
1702 } | 1758 } |
1703 if (s->lookahead == 0) break; /* flush the current block */ | 1759 if (s->lookahead == 0) break; /* flush the current block */ |
1704 } | 1760 } |
1705 | 1761 |
1706 /* See how many times the previous byte repeats */ | 1762 /* See how many times the previous byte repeats */ |
1707 run = 0; | 1763 s->match_length = 0; |
1708 if (s->strstart > 0) { /* if there is a previous byte, that is */ | 1764 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { |
1709 max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH; | |
1710 scan = s->window + s->strstart - 1; | 1765 scan = s->window + s->strstart - 1; |
1711 prev = *scan++; | 1766 prev = *scan; |
1712 do { | 1767 if (prev == *++scan && prev == *++scan && prev == *++scan) { |
1713 if (*scan++ != prev) | 1768 strend = s->window + s->strstart + MAX_MATCH; |
1714 break; | 1769 do { |
1715 } while (++run < max); | 1770 } while (prev == *++scan && prev == *++scan && |
| 1771 prev == *++scan && prev == *++scan && |
| 1772 prev == *++scan && prev == *++scan && |
| 1773 prev == *++scan && prev == *++scan && |
| 1774 scan < strend); |
| 1775 s->match_length = MAX_MATCH - (int)(strend - scan); |
| 1776 if (s->match_length > s->lookahead) |
| 1777 s->match_length = s->lookahead; |
| 1778 } |
1716 } | 1779 } |
1717 | 1780 |
1718 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ | 1781 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ |
1719 if (run >= MIN_MATCH) { | 1782 if (s->match_length >= MIN_MATCH) { |
1720 check_match(s, s->strstart, s->strstart - 1, run); | 1783 check_match(s, s->strstart, s->strstart - 1, s->match_length); |
1721 _tr_tally_dist(s, 1, run - MIN_MATCH, bflush); | 1784 |
1722 s->lookahead -= run; | 1785 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); |
1723 s->strstart += run; | 1786 |
| 1787 s->lookahead -= s->match_length; |
| 1788 s->strstart += s->match_length; |
| 1789 s->match_length = 0; |
1724 } else { | 1790 } else { |
1725 /* No match, output a literal byte */ | 1791 /* No match, output a literal byte */ |
1726 Tracevv((stderr,"%c", s->window[s->strstart])); | 1792 Tracevv((stderr,"%c", s->window[s->strstart])); |
1727 _tr_tally_lit (s, s->window[s->strstart], bflush); | 1793 _tr_tally_lit (s, s->window[s->strstart], bflush); |
1728 s->lookahead--; | 1794 s->lookahead--; |
1729 s->strstart++; | 1795 s->strstart++; |
1730 } | 1796 } |
1731 if (bflush) FLUSH_BLOCK(s, 0); | 1797 if (bflush) FLUSH_BLOCK(s, 0); |
1732 } | 1798 } |
1733 FLUSH_BLOCK(s, flush == Z_FINISH); | 1799 FLUSH_BLOCK(s, flush == Z_FINISH); |
1734 return flush == Z_FINISH ? finish_done : block_done; | 1800 return flush == Z_FINISH ? finish_done : block_done; |
1735 } | 1801 } |
1736 #endif | 1802 |
| 1803 /* =========================================================================== |
| 1804 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. |
| 1805 * (It will be regenerated if this run of deflate switches away from Huffman.) |
| 1806 */ |
| 1807 local block_state deflate_huff(s, flush) |
| 1808 deflate_state *s; |
| 1809 int flush; |
| 1810 { |
| 1811 int bflush; /* set if current block must be flushed */ |
| 1812 |
| 1813 for (;;) { |
| 1814 /* Make sure that we have a literal to write. */ |
| 1815 if (s->lookahead == 0) { |
| 1816 fill_window(s); |
| 1817 if (s->lookahead == 0) { |
| 1818 if (flush == Z_NO_FLUSH) |
| 1819 return need_more; |
| 1820 break; /* flush the current block */ |
| 1821 } |
| 1822 } |
| 1823 |
| 1824 /* Output a literal byte */ |
| 1825 s->match_length = 0; |
| 1826 Tracevv((stderr,"%c", s->window[s->strstart])); |
| 1827 _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 1828 s->lookahead--; |
| 1829 s->strstart++; |
| 1830 if (bflush) FLUSH_BLOCK(s, 0); |
| 1831 } |
| 1832 FLUSH_BLOCK(s, flush == Z_FINISH); |
| 1833 return flush == Z_FINISH ? finish_done : block_done; |
| 1834 } |
OLD | NEW |