1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* deflate.c -- compress data using the deflation algorithm 26 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler 27 * For conditions of distribution and use, see copyright notice in zlib.h 28 */ 29 30 /* 31 * ALGORITHM 32 * 33 * The "deflation" process depends on being able to identify portions 34 * of the input text which are identical to earlier input (within a 35 * sliding window trailing behind the input currently being processed). 36 * 37 * The most straightforward technique turns out to be the fastest for 38 * most input files: try all possible matches and select the longest. 39 * The key feature of this algorithm is that insertions into the string 40 * dictionary are very simple and thus fast, and deletions are avoided 41 * completely. Insertions are performed at each input character, whereas 42 * string matches are performed only when the previous match ends. So it 43 * is preferable to spend more time in matches to allow very fast string 44 * insertions and avoid deletions. The matching algorithm for small 45 * strings is inspired from that of Rabin & Karp. A brute force approach 46 * is used to find longer strings when a small match has been found. 47 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 48 * (by Leonid Broukhis). 49 * A previous version of this file used a more sophisticated algorithm 50 * (by Fiala and Greene) which is guaranteed to run in linear amortized 51 * time, but has a larger average cost, uses more memory and is patented. 52 * However the F&G algorithm may be faster for some highly redundant 53 * files if the parameter max_chain_length (described below) is too large. 54 * 55 * ACKNOWLEDGEMENTS 56 * 57 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 58 * I found it in 'freeze' written by Leonid Broukhis. 59 * Thanks to many people for bug reports and testing. 60 * 61 * REFERENCES 62 * 63 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". 64 * Available in http://tools.ietf.org/html/rfc1951 65 * 66 * A description of the Rabin and Karp algorithm is given in the book 67 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 68 * 69 * Fiala,E.R., and Greene,D.H. 70 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 71 * 72 */ 73 74 /* @(#) $Id$ */ 75 76 #include "deflate.h" 77 78 const char deflate_copyright[] = 79 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; 80 /* 81 If you use the zlib library in a product, an acknowledgment is welcome 82 in the documentation of your product. If for some reason you cannot 83 include such an acknowledgment, I would appreciate that you keep this 84 copyright string in the executable of your product. 85 */ 86 87 /* =========================================================================== 88 * Function prototypes. 89 */ 90 typedef enum { 91 need_more, /* block not completed, need more input or more output */ 92 block_done, /* block flush performed */ 93 finish_started, /* finish started, need only more output at next deflate */ 94 finish_done /* finish done, accept no more input or output */ 95 } block_state; 96 97 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 98 /* Compression function. Returns the block state after the call. */ 99 100 local int deflateStateCheck OF((z_streamp strm)); 101 local void slide_hash OF((deflate_state *s)); 102 local void fill_window OF((deflate_state *s)); 103 local block_state deflate_stored OF((deflate_state *s, int flush)); 104 local block_state deflate_fast OF((deflate_state *s, int flush)); 105 #ifndef FASTEST 106 local block_state deflate_slow OF((deflate_state *s, int flush)); 107 #endif 108 local block_state deflate_rle OF((deflate_state *s, int flush)); 109 local block_state deflate_huff OF((deflate_state *s, int flush)); 110 local void lm_init OF((deflate_state *s)); 111 local void putShortMSB OF((deflate_state *s, uInt b)); 112 local void flush_pending OF((z_streamp strm)); 113 local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); 114 #ifdef ASMV 115 # pragma message("Assembler code may have bugs -- use at your own risk") 116 void match_init OF((void)); /* asm code initialization */ 117 uInt longest_match OF((deflate_state *s, IPos cur_match)); 118 #else 119 local uInt longest_match OF((deflate_state *s, IPos cur_match)); 120 #endif 121 122 #ifdef ZLIB_DEBUG 123 local void check_match OF((deflate_state *s, IPos start, IPos match, 124 int length)); 125 #endif 126 127 /* =========================================================================== 128 * Local data 129 */ 130 131 #define NIL 0 132 /* Tail of hash chains */ 133 134 #ifndef TOO_FAR 135 # define TOO_FAR 4096 136 #endif 137 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 138 139 /* Values for max_lazy_match, good_match and max_chain_length, depending on 140 * the desired pack level (0..9). The values given below have been tuned to 141 * exclude worst case performance for pathological files. Better values may be 142 * found for specific files. 143 */ 144 typedef struct config_s { 145 ush good_length; /* reduce lazy search above this match length */ 146 ush max_lazy; /* do not perform lazy search above this match length */ 147 ush nice_length; /* quit search above this match length */ 148 ush max_chain; 149 compress_func func; 150 } config; 151 152 #ifdef FASTEST 153 local const config configuration_table[2] = { 154 /* good lazy nice chain */ 155 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 156 /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ 157 #else 158 local const config configuration_table[10] = { 159 /* good lazy nice chain */ 160 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ 161 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ 162 /* 2 */ {4, 5, 16, 8, deflate_fast}, 163 /* 3 */ {4, 6, 32, 32, deflate_fast}, 164 165 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ 166 /* 5 */ {8, 16, 32, 32, deflate_slow}, 167 /* 6 */ {8, 16, 128, 128, deflate_slow}, 168 /* 7 */ {8, 32, 128, 256, deflate_slow}, 169 /* 8 */ {32, 128, 258, 1024, deflate_slow}, 170 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ 171 #endif 172 173 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 174 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 175 * meaning. 176 */ 177 178 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ 179 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) 180 181 /* =========================================================================== 182 * Update a hash value with the given input byte 183 * IN assertion: all calls to UPDATE_HASH are made with consecutive input 184 * characters, so that a running hash key can be computed from the previous 185 * key instead of complete recalculation each time. 186 */ 187 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 188 189 190 /* =========================================================================== 191 * Insert string str in the dictionary and set match_head to the previous head 192 * of the hash chain (the most recent string with same hash key). Return 193 * the previous length of the hash chain. 194 * If this file is compiled with -DFASTEST, the compression level is forced 195 * to 1, and no hash chains are maintained. 196 * IN assertion: all calls to INSERT_STRING are made with consecutive input 197 * characters and the first MIN_MATCH bytes of str are valid (except for 198 * the last MIN_MATCH-1 bytes of the input file). 199 */ 200 #ifdef FASTEST 201 #define INSERT_STRING(s, str, match_head) \ 202 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 203 match_head = s->head[s->ins_h], \ 204 s->head[s->ins_h] = (Pos)(str)) 205 #else 206 #define INSERT_STRING(s, str, match_head) \ 207 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 208 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 209 s->head[s->ins_h] = (Pos)(str)) 210 #endif 211 212 /* =========================================================================== 213 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 214 * prev[] will be initialized on the fly. 215 */ 216 #define CLEAR_HASH(s) \ 217 s->head[s->hash_size-1] = NIL; \ 218 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 219 220 /* =========================================================================== 221 * Slide the hash table when sliding the window down (could be avoided with 32 222 * bit values at the expense of memory usage). We slide even when level == 0 to 223 * keep the hash table consistent if we switch back to level > 0 later. 224 */ 225 local void slide_hash(s) 226 deflate_state *s; 227 { 228 unsigned n, m; 229 Posf *p; 230 uInt wsize = s->w_size; 231 232 n = s->hash_size; 233 p = &s->head[n]; 234 do { 235 m = *--p; 236 *p = (Pos)(m >= wsize ? m - wsize : NIL); 237 } while (--n); 238 n = wsize; 239 #ifndef FASTEST 240 p = &s->prev[n]; 241 do { 242 m = *--p; 243 *p = (Pos)(m >= wsize ? m - wsize : NIL); 244 /* If n is not on any hash chain, prev[n] is garbage but 245 * its value will never be used. 246 */ 247 } while (--n); 248 #endif 249 } 250 251 /* ========================================================================= */ 252 int ZEXPORT deflateInit_(strm, level, version, stream_size) 253 z_streamp strm; 254 int level; 255 const char *version; 256 int stream_size; 257 { 258 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 259 Z_DEFAULT_STRATEGY, version, stream_size); 260 /* To do: ignore strm->next_in if we use it as window */ 261 } 262 263 /* ========================================================================= */ 264 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 265 version, stream_size) 266 z_streamp strm; 267 int level; 268 int method; 269 int windowBits; 270 int memLevel; 271 int strategy; 272 const char *version; 273 int stream_size; 274 { 275 deflate_state *s; 276 int wrap = 1; 277 static const char my_version[] = ZLIB_VERSION; 278 279 ushf *overlay; 280 /* We overlay pending_buf and d_buf+l_buf. This works since the average 281 * output size for (length,distance) codes is <= 24 bits. 282 */ 283 284 if (version == Z_NULL || version[0] != my_version[0] || 285 stream_size != sizeof(z_stream)) { 286 return Z_VERSION_ERROR; 287 } 288 if (strm == Z_NULL) return Z_STREAM_ERROR; 289 290 strm->msg = Z_NULL; 291 if (strm->zalloc == (alloc_func)0) { 292 #ifdef Z_SOLO 293 return Z_STREAM_ERROR; 294 #else 295 strm->zalloc = zcalloc; 296 strm->opaque = (voidpf)0; 297 #endif 298 } 299 if (strm->zfree == (free_func)0) 300 #ifdef Z_SOLO 301 return Z_STREAM_ERROR; 302 #else 303 strm->zfree = zcfree; 304 #endif 305 306 #ifdef FASTEST 307 if (level != 0) level = 1; 308 #else 309 if (level == Z_DEFAULT_COMPRESSION) level = 6; 310 #endif 311 312 if (windowBits < 0) { /* suppress zlib wrapper */ 313 wrap = 0; 314 windowBits = -windowBits; 315 } 316 #ifdef GZIP 317 else if (windowBits > 15) { 318 wrap = 2; /* write gzip wrapper instead */ 319 windowBits -= 16; 320 } 321 #endif 322 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || 323 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || 324 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { 325 return Z_STREAM_ERROR; 326 } 327 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 328 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 329 if (s == Z_NULL) return Z_MEM_ERROR; 330 strm->state = (struct internal_state FAR *)s; 331 s->strm = strm; 332 s->status = INIT_STATE; /* to pass state test in deflateReset() */ 333 334 s->wrap = wrap; 335 s->gzhead = Z_NULL; 336 s->w_bits = (uInt)windowBits; 337 s->w_size = 1 << s->w_bits; 338 s->w_mask = s->w_size - 1; 339 340 s->hash_bits = (uInt)memLevel + 7; 341 s->hash_size = 1 << s->hash_bits; 342 s->hash_mask = s->hash_size - 1; 343 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 344 345 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 346 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 347 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 348 349 s->high_water = 0; /* nothing written to s->window yet */ 350 351 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 352 353 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 354 s->pending_buf = (uchf *) overlay; 355 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 356 357 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 358 s->pending_buf == Z_NULL) { 359 s->status = FINISH_STATE; 360 strm->msg = ERR_MSG(Z_MEM_ERROR); 361 deflateEnd (strm); 362 return Z_MEM_ERROR; 363 } 364 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); 365 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; 366 367 s->level = level; 368 s->strategy = strategy; 369 s->method = (Byte)method; 370 371 return deflateReset(strm); 372 } 373 374 /* ========================================================================= 375 * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 376 */ 377 local int deflateStateCheck (strm) 378 z_streamp strm; 379 { 380 deflate_state *s; 381 if (strm == Z_NULL || 382 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 383 return 1; 384 s = strm->state; 385 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && 386 #ifdef GZIP 387 s->status != GZIP_STATE && 388 #endif 389 s->status != EXTRA_STATE && 390 s->status != NAME_STATE && 391 s->status != COMMENT_STATE && 392 s->status != HCRC_STATE && 393 s->status != BUSY_STATE && 394 s->status != FINISH_STATE)) 395 return 1; 396 return 0; 397 } 398 399 /* ========================================================================= */ 400 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 401 z_streamp strm; 402 const Bytef *dictionary; 403 uInt dictLength; 404 { 405 deflate_state *s; 406 uInt str, n; 407 int wrap; 408 unsigned avail; 409 z_const unsigned char *next; 410 411 if (deflateStateCheck(strm) || dictionary == Z_NULL) 412 return Z_STREAM_ERROR; 413 s = strm->state; 414 wrap = s->wrap; 415 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) 416 return Z_STREAM_ERROR; 417 418 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ 419 if (wrap == 1) 420 strm->adler = adler32(strm->adler, dictionary, dictLength); 421 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ 422 423 /* if dictionary would fill window, just replace the history */ 424 if (dictLength >= s->w_size) { 425 if (wrap == 0) { /* already empty otherwise */ 426 CLEAR_HASH(s); 427 s->strstart = 0; 428 s->block_start = 0L; 429 s->insert = 0; 430 } 431 dictionary += dictLength - s->w_size; /* use the tail */ 432 dictLength = s->w_size; 433 } 434 435 /* insert dictionary into window and hash */ 436 avail = strm->avail_in; 437 next = strm->next_in; 438 strm->avail_in = dictLength; 439 strm->next_in = (z_const Bytef *)dictionary; 440 fill_window(s); 441 while (s->lookahead >= MIN_MATCH) { 442 str = s->strstart; 443 n = s->lookahead - (MIN_MATCH-1); 444 do { 445 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 446 #ifndef FASTEST 447 s->prev[str & s->w_mask] = s->head[s->ins_h]; 448 #endif 449 s->head[s->ins_h] = (Pos)str; 450 str++; 451 } while (--n); 452 s->strstart = str; 453 s->lookahead = MIN_MATCH-1; 454 fill_window(s); 455 } 456 s->strstart += s->lookahead; 457 s->block_start = (long)s->strstart; 458 s->insert = s->lookahead; 459 s->lookahead = 0; 460 s->match_length = s->prev_length = MIN_MATCH-1; 461 s->match_available = 0; 462 strm->next_in = next; 463 strm->avail_in = avail; 464 s->wrap = wrap; 465 return Z_OK; 466 } 467 468 /* ========================================================================= */ 469 int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) 470 z_streamp strm; 471 Bytef *dictionary; 472 uInt *dictLength; 473 { 474 deflate_state *s; 475 uInt len; 476 477 if (deflateStateCheck(strm)) 478 return Z_STREAM_ERROR; 479 s = strm->state; 480 len = s->strstart + s->lookahead; 481 if (len > s->w_size) 482 len = s->w_size; 483 if (dictionary != Z_NULL && len) 484 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); 485 if (dictLength != Z_NULL) 486 *dictLength = len; 487 return Z_OK; 488 } 489 490 /* ========================================================================= */ 491 int ZEXPORT deflateResetKeep (strm) 492 z_streamp strm; 493 { 494 deflate_state *s; 495 496 if (deflateStateCheck(strm)) { 497 return Z_STREAM_ERROR; 498 } 499 500 strm->total_in = strm->total_out = 0; 501 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 502 strm->data_type = Z_UNKNOWN; 503 504 s = (deflate_state *)strm->state; 505 s->pending = 0; 506 s->pending_out = s->pending_buf; 507 508 if (s->wrap < 0) { 509 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 510 } 511 s->status = 512 #ifdef GZIP 513 s->wrap == 2 ? GZIP_STATE : 514 #endif 515 s->wrap ? INIT_STATE : BUSY_STATE; 516 strm->adler = 517 #ifdef GZIP 518 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 519 #endif 520 adler32(0L, Z_NULL, 0); 521 s->last_flush = -2; 522 523 _tr_init(s); 524 525 return Z_OK; 526 } 527 528 /* ========================================================================= */ 529 int ZEXPORT deflateReset (strm) 530 z_streamp strm; 531 { 532 int ret; 533 534 ret = deflateResetKeep(strm); 535 if (ret == Z_OK) 536 lm_init(strm->state); 537 return ret; 538 } 539 540 /* ========================================================================= */ 541 int ZEXPORT deflateSetHeader (strm, head) 542 z_streamp strm; 543 gz_headerp head; 544 { 545 if (deflateStateCheck(strm) || strm->state->wrap != 2) 546 return Z_STREAM_ERROR; 547 strm->state->gzhead = head; 548 return Z_OK; 549 } 550 551 /* ========================================================================= */ 552 int ZEXPORT deflatePending (strm, pending, bits) 553 unsigned *pending; 554 int *bits; 555 z_streamp strm; 556 { 557 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 558 if (pending != Z_NULL) 559 *pending = strm->state->pending; 560 if (bits != Z_NULL) 561 *bits = strm->state->bi_valid; 562 return Z_OK; 563 } 564 565 /* ========================================================================= */ 566 int ZEXPORT deflatePrime (strm, bits, value) 567 z_streamp strm; 568 int bits; 569 int value; 570 { 571 deflate_state *s; 572 int put; 573 574 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 575 s = strm->state; 576 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) 577 return Z_BUF_ERROR; 578 do { 579 put = Buf_size - s->bi_valid; 580 if (put > bits) 581 put = bits; 582 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); 583 s->bi_valid += put; 584 _tr_flush_bits(s); 585 value >>= put; 586 bits -= put; 587 } while (bits); 588 return Z_OK; 589 } 590 591 /* ========================================================================= */ 592 int ZEXPORT deflateParams(strm, level, strategy) 593 z_streamp strm; 594 int level; 595 int strategy; 596 { 597 deflate_state *s; 598 compress_func func; 599 600 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 601 s = strm->state; 602 603 #ifdef FASTEST 604 if (level != 0) level = 1; 605 #else 606 if (level == Z_DEFAULT_COMPRESSION) level = 6; 607 #endif 608 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 609 return Z_STREAM_ERROR; 610 } 611 func = configuration_table[s->level].func; 612 613 if ((strategy != s->strategy || func != configuration_table[level].func) && 614 s->last_flush != -2) { 615 /* Flush the last buffer: */ 616 int err = deflate(strm, Z_BLOCK); 617 if (err == Z_STREAM_ERROR) 618 return err; 619 if (strm->avail_out == 0) 620 return Z_BUF_ERROR; 621 } 622 if (s->level != level) { 623 if (s->level == 0 && s->matches != 0) { 624 if (s->matches == 1) 625 slide_hash(s); 626 else 627 CLEAR_HASH(s); 628 s->matches = 0; 629 } 630 s->level = level; 631 s->max_lazy_match = configuration_table[level].max_lazy; 632 s->good_match = configuration_table[level].good_length; 633 s->nice_match = configuration_table[level].nice_length; 634 s->max_chain_length = configuration_table[level].max_chain; 635 } 636 s->strategy = strategy; 637 return Z_OK; 638 } 639 640 /* ========================================================================= */ 641 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) 642 z_streamp strm; 643 int good_length; 644 int max_lazy; 645 int nice_length; 646 int max_chain; 647 { 648 deflate_state *s; 649 650 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 651 s = strm->state; 652 s->good_match = (uInt)good_length; 653 s->max_lazy_match = (uInt)max_lazy; 654 s->nice_match = nice_length; 655 s->max_chain_length = (uInt)max_chain; 656 return Z_OK; 657 } 658 659 /* ========================================================================= 660 * For the default windowBits of 15 and memLevel of 8, this function returns 661 * a close to exact, as well as small, upper bound on the compressed size. 662 * They are coded as constants here for a reason--if the #define's are 663 * changed, then this function needs to be changed as well. The return 664 * value for 15 and 8 only works for those exact settings. 665 * 666 * For any setting other than those defaults for windowBits and memLevel, 667 * the value returned is a conservative worst case for the maximum expansion 668 * resulting from using fixed blocks instead of stored blocks, which deflate 669 * can emit on compressed data for some combinations of the parameters. 670 * 671 * This function could be more sophisticated to provide closer upper bounds for 672 * every combination of windowBits and memLevel. But even the conservative 673 * upper bound of about 14% expansion does not seem onerous for output buffer 674 * allocation. 675 */ 676 uLong ZEXPORT deflateBound(strm, sourceLen) 677 z_streamp strm; 678 uLong sourceLen; 679 { 680 deflate_state *s; 681 uLong complen, wraplen; 682 683 /* conservative upper bound for compressed data */ 684 complen = sourceLen + 685 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; 686 687 /* if can't get parameters, return conservative bound plus zlib wrapper */ 688 if (deflateStateCheck(strm)) 689 return complen + 6; 690 691 /* compute wrapper length */ 692 s = strm->state; 693 switch (s->wrap) { 694 case 0: /* raw deflate */ 695 wraplen = 0; 696 break; 697 case 1: /* zlib wrapper */ 698 wraplen = 6 + (s->strstart ? 4 : 0); 699 break; 700 #ifdef GZIP 701 case 2: /* gzip wrapper */ 702 wraplen = 18; 703 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ 704 Bytef *str; 705 if (s->gzhead->extra != Z_NULL) 706 wraplen += 2 + s->gzhead->extra_len; 707 str = s->gzhead->name; 708 if (str != Z_NULL) 709 do { 710 wraplen++; 711 } while (*str++); 712 str = s->gzhead->comment; 713 if (str != Z_NULL) 714 do { 715 wraplen++; 716 } while (*str++); 717 if (s->gzhead->hcrc) 718 wraplen += 2; 719 } 720 break; 721 #endif 722 default: /* for compiler happiness */ 723 wraplen = 6; 724 } 725 726 /* if not default parameters, return conservative bound */ 727 if (s->w_bits != 15 || s->hash_bits != 8 + 7) 728 return complen + wraplen; 729 730 /* default settings: return tight bound for that case */ 731 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 732 (sourceLen >> 25) + 13 - 6 + wraplen; 733 } 734 735 /* ========================================================================= 736 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 737 * IN assertion: the stream state is correct and there is enough room in 738 * pending_buf. 739 */ 740 local void putShortMSB (s, b) 741 deflate_state *s; 742 uInt b; 743 { 744 put_byte(s, (Byte)(b >> 8)); 745 put_byte(s, (Byte)(b & 0xff)); 746 } 747 748 /* ========================================================================= 749 * Flush as much pending output as possible. All deflate() output, except for 750 * some deflate_stored() output, goes through this function so some 751 * applications may wish to modify it to avoid allocating a large 752 * strm->next_out buffer and copying into it. (See also read_buf()). 753 */ 754 local void flush_pending(strm) 755 z_streamp strm; 756 { 757 unsigned len; 758 deflate_state *s = strm->state; 759 760 _tr_flush_bits(s); 761 len = s->pending; 762 if (len > strm->avail_out) len = strm->avail_out; 763 if (len == 0) return; 764 765 zmemcpy(strm->next_out, s->pending_out, len); 766 strm->next_out += len; 767 s->pending_out += len; 768 strm->total_out += len; 769 strm->avail_out -= len; 770 s->pending -= len; 771 if (s->pending == 0) { 772 s->pending_out = s->pending_buf; 773 } 774 } 775 776 /* =========================================================================== 777 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. 778 */ 779 #define HCRC_UPDATE(beg) \ 780 do { \ 781 if (s->gzhead->hcrc && s->pending > (beg)) \ 782 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ 783 s->pending - (beg)); \ 784 } while (0) 785 786 /* ========================================================================= */ 787 int ZEXPORT deflate (strm, flush) 788 z_streamp strm; 789 int flush; 790 { 791 int old_flush; /* value of flush param for previous deflate call */ 792 deflate_state *s; 793 794 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { 795 return Z_STREAM_ERROR; 796 } 797 s = strm->state; 798 799 if (strm->next_out == Z_NULL || 800 (strm->avail_in != 0 && strm->next_in == Z_NULL) || 801 (s->status == FINISH_STATE && flush != Z_FINISH)) { 802 ERR_RETURN(strm, Z_STREAM_ERROR); 803 } 804 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 805 806 old_flush = s->last_flush; 807 s->last_flush = flush; 808 809 /* Flush as much pending output as possible */ 810 if (s->pending != 0) { 811 flush_pending(strm); 812 if (strm->avail_out == 0) { 813 /* Since avail_out is 0, deflate will be called again with 814 * more output space, but possibly with both pending and 815 * avail_in equal to zero. There won't be anything to do, 816 * but this is not an error situation so make sure we 817 * return OK instead of BUF_ERROR at next call of deflate: 818 */ 819 s->last_flush = -1; 820 return Z_OK; 821 } 822 823 /* Make sure there is something to do and avoid duplicate consecutive 824 * flushes. For repeated and useless calls with Z_FINISH, we keep 825 * returning Z_STREAM_END instead of Z_BUF_ERROR. 826 */ 827 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && 828 flush != Z_FINISH) { 829 ERR_RETURN(strm, Z_BUF_ERROR); 830 } 831 832 /* User must not provide more input after the first FINISH: */ 833 if (s->status == FINISH_STATE && strm->avail_in != 0) { 834 ERR_RETURN(strm, Z_BUF_ERROR); 835 } 836 837 /* Write the header */ 838 if (s->status == INIT_STATE) { 839 /* zlib header */ 840 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; 841 uInt level_flags; 842 843 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) 844 level_flags = 0; 845 else if (s->level < 6) 846 level_flags = 1; 847 else if (s->level == 6) 848 level_flags = 2; 849 else 850 level_flags = 3; 851 header |= (level_flags << 6); 852 if (s->strstart != 0) header |= PRESET_DICT; 853 header += 31 - (header % 31); 854 855 putShortMSB(s, header); 856 857 /* Save the adler32 of the preset dictionary: */ 858 if (s->strstart != 0) { 859 putShortMSB(s, (uInt)(strm->adler >> 16)); 860 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 861 } 862 strm->adler = adler32(0L, Z_NULL, 0); 863 s->status = BUSY_STATE; 864 865 /* Compression must start with an empty pending buffer */ 866 flush_pending(strm); 867 if (s->pending != 0) { 868 s->last_flush = -1; 869 return Z_OK; 870 } 871 } 872 #ifdef GZIP 873 if (s->status == GZIP_STATE) { 874 /* gzip header */ 875 strm->adler = crc32(0L, Z_NULL, 0); 876 put_byte(s, 31); 877 put_byte(s, 139); 878 put_byte(s, 8); 879 if (s->gzhead == Z_NULL) { 880 put_byte(s, 0); 881 put_byte(s, 0); 882 put_byte(s, 0); 883 put_byte(s, 0); 884 put_byte(s, 0); 885 put_byte(s, s->level == 9 ? 2 : 886 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 887 4 : 0)); 888 put_byte(s, OS_CODE); 889 s->status = BUSY_STATE; 890 891 /* Compression must start with an empty pending buffer */ 892 flush_pending(strm); 893 if (s->pending != 0) { 894 s->last_flush = -1; 895 return Z_OK; 896 } 897 } 898 else { 899 put_byte(s, (s->gzhead->text ? 1 : 0) + 900 (s->gzhead->hcrc ? 2 : 0) + 901 (s->gzhead->extra == Z_NULL ? 0 : 4) + 902 (s->gzhead->name == Z_NULL ? 0 : 8) + 903 (s->gzhead->comment == Z_NULL ? 0 : 16) 904 ); 905 put_byte(s, (Byte)(s->gzhead->time & 0xff)); 906 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); 907 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); 908 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); 909 put_byte(s, s->level == 9 ? 2 : 910 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 911 4 : 0)); 912 put_byte(s, s->gzhead->os & 0xff); 913 if (s->gzhead->extra != Z_NULL) { 914 put_byte(s, s->gzhead->extra_len & 0xff); 915 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); 916 } 917 if (s->gzhead->hcrc) 918 strm->adler = crc32(strm->adler, s->pending_buf, 919 s->pending); 920 s->gzindex = 0; 921 s->status = EXTRA_STATE; 922 } 923 } 924 if (s->status == EXTRA_STATE) { 925 if (s->gzhead->extra != Z_NULL) { 926 ulg beg = s->pending; /* start of bytes to update crc */ 927 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; 928 while (s->pending + left > s->pending_buf_size) { 929 uInt copy = s->pending_buf_size - s->pending; 930 zmemcpy(s->pending_buf + s->pending, 931 s->gzhead->extra + s->gzindex, copy); 932 s->pending = s->pending_buf_size; 933 HCRC_UPDATE(beg); 934 s->gzindex += copy; 935 flush_pending(strm); 936 if (s->pending != 0) { 937 s->last_flush = -1; 938 return Z_OK; 939 } 940 beg = 0; 941 left -= copy; 942 } 943 zmemcpy(s->pending_buf + s->pending, 944 s->gzhead->extra + s->gzindex, left); 945 s->pending += left; 946 HCRC_UPDATE(beg); 947 s->gzindex = 0; 948 } 949 s->status = NAME_STATE; 950 } 951 if (s->status == NAME_STATE) { 952 if (s->gzhead->name != Z_NULL) { 953 ulg beg = s->pending; /* start of bytes to update crc */ 954 int val; 955 do { 956 if (s->pending == s->pending_buf_size) { 957 HCRC_UPDATE(beg); 958 flush_pending(strm); 959 if (s->pending != 0) { 960 s->last_flush = -1; 961 return Z_OK; 962 } 963 beg = 0; 964 } 965 val = s->gzhead->name[s->gzindex++]; 966 put_byte(s, val); 967 } while (val != 0); 968 HCRC_UPDATE(beg); 969 s->gzindex = 0; 970 } 971 s->status = COMMENT_STATE; 972 } 973 if (s->status == COMMENT_STATE) { 974 if (s->gzhead->comment != Z_NULL) { 975 ulg beg = s->pending; /* start of bytes to update crc */ 976 int val; 977 do { 978 if (s->pending == s->pending_buf_size) { 979 HCRC_UPDATE(beg); 980 flush_pending(strm); 981 if (s->pending != 0) { 982 s->last_flush = -1; 983 return Z_OK; 984 } 985 beg = 0; 986 } 987 val = s->gzhead->comment[s->gzindex++]; 988 put_byte(s, val); 989 } while (val != 0); 990 HCRC_UPDATE(beg); 991 } 992 s->status = HCRC_STATE; 993 } 994 if (s->status == HCRC_STATE) { 995 if (s->gzhead->hcrc) { 996 if (s->pending + 2 > s->pending_buf_size) { 997 flush_pending(strm); 998 if (s->pending != 0) { 999 s->last_flush = -1; 1000 return Z_OK; 1001 } 1002 } 1003 put_byte(s, (Byte)(strm->adler & 0xff)); 1004 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 1005 strm->adler = crc32(0L, Z_NULL, 0); 1006 } 1007 s->status = BUSY_STATE; 1008 1009 /* Compression must start with an empty pending buffer */ 1010 flush_pending(strm); 1011 if (s->pending != 0) { 1012 s->last_flush = -1; 1013 return Z_OK; 1014 } 1015 } 1016 #endif 1017 1018 /* Start a new block or continue the current one. 1019 */ 1020 if (strm->avail_in != 0 || s->lookahead != 0 || 1021 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 1022 block_state bstate; 1023 1024 bstate = s->level == 0 ? deflate_stored(s, flush) : 1025 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 1026 s->strategy == Z_RLE ? deflate_rle(s, flush) : 1027 (*(configuration_table[s->level].func))(s, flush); 1028 1029 if (bstate == finish_started || bstate == finish_done) { 1030 s->status = FINISH_STATE; 1031 } 1032 if (bstate == need_more || bstate == finish_started) { 1033 if (strm->avail_out == 0) { 1034 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 1035 } 1036 return Z_OK; 1037 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 1038 * of deflate should use the same flush parameter to make sure 1039 * that the flush is complete. So we don't have to output an 1040 * empty block here, this will be done at next call. This also 1041 * ensures that for a very small output buffer, we emit at most 1042 * one empty block. 1043 */ 1044 } 1045 if (bstate == block_done) { 1046 if (flush == Z_PARTIAL_FLUSH) { 1047 _tr_align(s); 1048 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 1049 _tr_stored_block(s, (char*)0, 0L, 0); 1050 /* For a full flush, this empty block will be recognized 1051 * as a special marker by inflate_sync(). 1052 */ 1053 if (flush == Z_FULL_FLUSH) { 1054 CLEAR_HASH(s); /* forget history */ 1055 if (s->lookahead == 0) { 1056 s->strstart = 0; 1057 s->block_start = 0L; 1058 s->insert = 0; 1059 } 1060 } 1061 } 1062 flush_pending(strm); 1063 if (strm->avail_out == 0) { 1064 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 1065 return Z_OK; 1066 } 1067 } 1068 } 1069 1070 if (flush != Z_FINISH) return Z_OK; 1071 if (s->wrap <= 0) return Z_STREAM_END; 1072 1073 /* Write the trailer */ 1074 #ifdef GZIP 1075 if (s->wrap == 2) { 1076 put_byte(s, (Byte)(strm->adler & 0xff)); 1077 put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); 1078 put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); 1079 put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); 1080 put_byte(s, (Byte)(strm->total_in & 0xff)); 1081 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); 1082 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); 1083 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); 1084 } 1085 else 1086 #endif 1087 { 1088 putShortMSB(s, (uInt)(strm->adler >> 16)); 1089 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 1090 } 1091 flush_pending(strm); 1092 /* If avail_out is zero, the application will call deflate again 1093 * to flush the rest. 1094 */ 1095 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ 1096 return s->pending != 0 ? Z_OK : Z_STREAM_END; 1097 } 1098 1099 /* ========================================================================= */ 1100 int ZEXPORT deflateEnd (strm) 1101 z_streamp strm; 1102 { 1103 int status; 1104 1105 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 1106 1107 status = strm->state->status; 1108 1109 /* Deallocate in reverse order of allocations: */ 1110 TRY_FREE(strm, strm->state->pending_buf); 1111 TRY_FREE(strm, strm->state->head); 1112 TRY_FREE(strm, strm->state->prev); 1113 TRY_FREE(strm, strm->state->window); 1114 1115 ZFREE(strm, strm->state); 1116 strm->state = Z_NULL; 1117 1118 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 1119 } 1120 1121 /* ========================================================================= 1122 * Copy the source state to the destination state. 1123 * To simplify the source, this is not supported for 16-bit MSDOS (which 1124 * doesn't have enough memory anyway to duplicate compression states). 1125 */ 1126 int ZEXPORT deflateCopy (dest, source) 1127 z_streamp dest; 1128 z_streamp source; 1129 { 1130 #ifdef MAXSEG_64K 1131 return Z_STREAM_ERROR; 1132 #else 1133 deflate_state *ds; 1134 deflate_state *ss; 1135 ushf *overlay; 1136 1137 1138 if (deflateStateCheck(source) || dest == Z_NULL) { 1139 return Z_STREAM_ERROR; 1140 } 1141 1142 ss = source->state; 1143 1144 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 1145 1146 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 1147 if (ds == Z_NULL) return Z_MEM_ERROR; 1148 dest->state = (struct internal_state FAR *) ds; 1149 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); 1150 ds->strm = dest; 1151 1152 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 1153 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 1154 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1155 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); 1156 ds->pending_buf = (uchf *) overlay; 1157 1158 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 1159 ds->pending_buf == Z_NULL) { 1160 deflateEnd (dest); 1161 return Z_MEM_ERROR; 1162 } 1163 /* following zmemcpy do not work for 16-bit MSDOS */ 1164 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 1165 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 1166 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1167 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); 1168 1169 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1170 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); 1171 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; 1172 1173 ds->l_desc.dyn_tree = ds->dyn_ltree; 1174 ds->d_desc.dyn_tree = ds->dyn_dtree; 1175 ds->bl_desc.dyn_tree = ds->bl_tree; 1176 1177 return Z_OK; 1178 #endif /* MAXSEG_64K */ 1179 } 1180 1181 /* =========================================================================== 1182 * Read a new buffer from the current input stream, update the adler32 1183 * and total number of bytes read. All deflate() input goes through 1184 * this function so some applications may wish to modify it to avoid 1185 * allocating a large strm->next_in buffer and copying from it. 1186 * (See also flush_pending()). 1187 */ 1188 local unsigned read_buf(strm, buf, size) 1189 z_streamp strm; 1190 Bytef *buf; 1191 unsigned size; 1192 { 1193 unsigned len = strm->avail_in; 1194 1195 if (len > size) len = size; 1196 if (len == 0) return 0; 1197 1198 strm->avail_in -= len; 1199 1200 zmemcpy(buf, strm->next_in, len); 1201 if (strm->state->wrap == 1) { 1202 strm->adler = adler32(strm->adler, buf, len); 1203 } 1204 #ifdef GZIP 1205 else if (strm->state->wrap == 2) { 1206 strm->adler = crc32(strm->adler, buf, len); 1207 } 1208 #endif 1209 strm->next_in += len; 1210 strm->total_in += len; 1211 1212 return len; 1213 } 1214 1215 /* =========================================================================== 1216 * Initialize the "longest match" routines for a new zlib stream 1217 */ 1218 local void lm_init (s) 1219 deflate_state *s; 1220 { 1221 s->window_size = (ulg)2L*s->w_size; 1222 1223 CLEAR_HASH(s); 1224 1225 /* Set the default configuration parameters: 1226 */ 1227 s->max_lazy_match = configuration_table[s->level].max_lazy; 1228 s->good_match = configuration_table[s->level].good_length; 1229 s->nice_match = configuration_table[s->level].nice_length; 1230 s->max_chain_length = configuration_table[s->level].max_chain; 1231 1232 s->strstart = 0; 1233 s->block_start = 0L; 1234 s->lookahead = 0; 1235 s->insert = 0; 1236 s->match_length = s->prev_length = MIN_MATCH-1; 1237 s->match_available = 0; 1238 s->ins_h = 0; 1239 #ifndef FASTEST 1240 #ifdef ASMV 1241 match_init(); /* initialize the asm code */ 1242 #endif 1243 #endif 1244 } 1245 1246 #ifndef FASTEST 1247 /* =========================================================================== 1248 * Set match_start to the longest match starting at the given string and 1249 * return its length. Matches shorter or equal to prev_length are discarded, 1250 * in which case the result is equal to prev_length and match_start is 1251 * garbage. 1252 * IN assertions: cur_match is the head of the hash chain for the current 1253 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1254 * OUT assertion: the match length is not greater than s->lookahead. 1255 */ 1256 #ifndef ASMV 1257 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or 1258 * match.S. The code will be functionally equivalent. 1259 */ 1260 local uInt longest_match(s, cur_match) 1261 deflate_state *s; 1262 IPos cur_match; /* current match */ 1263 { 1264 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1265 register Bytef *scan = s->window + s->strstart; /* current string */ 1266 register Bytef *match; /* matched string */ 1267 register int len; /* length of current match */ 1268 int best_len = (int)s->prev_length; /* best match length so far */ 1269 int nice_match = s->nice_match; /* stop if match long enough */ 1270 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 1271 s->strstart - (IPos)MAX_DIST(s) : NIL; 1272 /* Stop when cur_match becomes <= limit. To simplify the code, 1273 * we prevent matches with the string of window index 0. 1274 */ 1275 Posf *prev = s->prev; 1276 uInt wmask = s->w_mask; 1277 1278 #ifdef UNALIGNED_OK 1279 /* Compare two bytes at a time. Note: this is not always beneficial. 1280 * Try with and without -DUNALIGNED_OK to check. 1281 */ 1282 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 1283 register ush scan_start = *(ushf*)scan; 1284 register ush scan_end = *(ushf*)(scan+best_len-1); 1285 #else 1286 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1287 register Byte scan_end1 = scan[best_len-1]; 1288 register Byte scan_end = scan[best_len]; 1289 #endif 1290 1291 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1292 * It is easy to get rid of this optimization if necessary. 1293 */ 1294 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1295 1296 /* Do not waste too much time if we already have a good match: */ 1297 if (s->prev_length >= s->good_match) { 1298 chain_length >>= 2; 1299 } 1300 /* Do not look for matches beyond the end of the input. This is necessary 1301 * to make deflate deterministic. 1302 */ 1303 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; 1304 1305 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1306 1307 do { 1308 Assert(cur_match < s->strstart, "no future"); 1309 match = s->window + cur_match; 1310 1311 /* Skip to next match if the match length cannot increase 1312 * or if the match length is less than 2. Note that the checks below 1313 * for insufficient lookahead only occur occasionally for performance 1314 * reasons. Therefore uninitialized memory will be accessed, and 1315 * conditional jumps will be made that depend on those values. 1316 * However the length of the match is limited to the lookahead, so 1317 * the output of deflate is not affected by the uninitialized values. 1318 */ 1319 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1320 /* This code assumes sizeof(unsigned short) == 2. Do not use 1321 * UNALIGNED_OK if your compiler uses a different size. 1322 */ 1323 if (*(ushf*)(match+best_len-1) != scan_end || 1324 *(ushf*)match != scan_start) continue; 1325 1326 /* It is not necessary to compare scan[2] and match[2] since they are 1327 * always equal when the other bytes match, given that the hash keys 1328 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 1329 * strstart+3, +5, ... up to strstart+257. We check for insufficient 1330 * lookahead only every 4th comparison; the 128th check will be made 1331 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is 1332 * necessary to put more guard bytes at the end of the window, or 1333 * to check more often for insufficient lookahead. 1334 */ 1335 Assert(scan[2] == match[2], "scan[2]?"); 1336 scan++, match++; 1337 do { 1338 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1339 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1340 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1341 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1342 scan < strend); 1343 /* The funny "do {}" generates better code on most compilers */ 1344 1345 /* Here, scan <= window+strstart+257 */ 1346 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1347 if (*scan == *match) scan++; 1348 1349 len = (MAX_MATCH - 1) - (int)(strend-scan); 1350 scan = strend - (MAX_MATCH-1); 1351 1352 #else /* UNALIGNED_OK */ 1353 1354 if (match[best_len] != scan_end || 1355 match[best_len-1] != scan_end1 || 1356 *match != *scan || 1357 *++match != scan[1]) continue; 1358 1359 /* The check at best_len-1 can be removed because it will be made 1360 * again later. (This heuristic is not always a win.) 1361 * It is not necessary to compare scan[2] and match[2] since they 1362 * are always equal when the other bytes match, given that 1363 * the hash keys are equal and that HASH_BITS >= 8. 1364 */ 1365 scan += 2, match++; 1366 Assert(*scan == *match, "match[2]?"); 1367 1368 /* We check for insufficient lookahead only every 8th comparison; 1369 * the 256th check will be made at strstart+258. 1370 */ 1371 do { 1372 } while (*++scan == *++match && *++scan == *++match && 1373 *++scan == *++match && *++scan == *++match && 1374 *++scan == *++match && *++scan == *++match && 1375 *++scan == *++match && *++scan == *++match && 1376 scan < strend); 1377 1378 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1379 1380 len = MAX_MATCH - (int)(strend - scan); 1381 scan = strend - MAX_MATCH; 1382 1383 #endif /* UNALIGNED_OK */ 1384 1385 if (len > best_len) { 1386 s->match_start = cur_match; 1387 best_len = len; 1388 if (len >= nice_match) break; 1389 #ifdef UNALIGNED_OK 1390 scan_end = *(ushf*)(scan+best_len-1); 1391 #else 1392 scan_end1 = scan[best_len-1]; 1393 scan_end = scan[best_len]; 1394 #endif 1395 } 1396 } while ((cur_match = prev[cur_match & wmask]) > limit 1397 && --chain_length != 0); 1398 1399 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 1400 return s->lookahead; 1401 } 1402 #endif /* ASMV */ 1403 1404 #else /* FASTEST */ 1405 1406 /* --------------------------------------------------------------------------- 1407 * Optimized version for FASTEST only 1408 */ 1409 local uInt longest_match(s, cur_match) 1410 deflate_state *s; 1411 IPos cur_match; /* current match */ 1412 { 1413 register Bytef *scan = s->window + s->strstart; /* current string */ 1414 register Bytef *match; /* matched string */ 1415 register int len; /* length of current match */ 1416 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1417 1418 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1419 * It is easy to get rid of this optimization if necessary. 1420 */ 1421 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1422 1423 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1424 1425 Assert(cur_match < s->strstart, "no future"); 1426 1427 match = s->window + cur_match; 1428 1429 /* Return failure if the match length is less than 2: 1430 */ 1431 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; 1432 1433 /* The check at best_len-1 can be removed because it will be made 1434 * again later. (This heuristic is not always a win.) 1435 * It is not necessary to compare scan[2] and match[2] since they 1436 * are always equal when the other bytes match, given that 1437 * the hash keys are equal and that HASH_BITS >= 8. 1438 */ 1439 scan += 2, match += 2; 1440 Assert(*scan == *match, "match[2]?"); 1441 1442 /* We check for insufficient lookahead only every 8th comparison; 1443 * the 256th check will be made at strstart+258. 1444 */ 1445 do { 1446 } while (*++scan == *++match && *++scan == *++match && 1447 *++scan == *++match && *++scan == *++match && 1448 *++scan == *++match && *++scan == *++match && 1449 *++scan == *++match && *++scan == *++match && 1450 scan < strend); 1451 1452 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1453 1454 len = MAX_MATCH - (int)(strend - scan); 1455 1456 if (len < MIN_MATCH) return MIN_MATCH - 1; 1457 1458 s->match_start = cur_match; 1459 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; 1460 } 1461 1462 #endif /* FASTEST */ 1463 1464 #ifdef ZLIB_DEBUG 1465 1466 #define EQUAL 0 1467 /* result of memcmp for equal strings */ 1468 1469 /* =========================================================================== 1470 * Check that the match at match_start is indeed a match. 1471 */ 1472 local void check_match(s, start, match, length) 1473 deflate_state *s; 1474 IPos start, match; 1475 int length; 1476 { 1477 /* check that the match is indeed a match */ 1478 if (zmemcmp(s->window + match, 1479 s->window + start, length) != EQUAL) { 1480 fprintf(stderr, " start %u, match %u, length %d\n", 1481 start, match, length); 1482 do { 1483 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); 1484 } while (--length != 0); 1485 z_error("invalid match"); 1486 } 1487 if (z_verbose > 1) { 1488 fprintf(stderr,"\\[%d,%d]", start-match, length); 1489 do { putc(s->window[start++], stderr); } while (--length != 0); 1490 } 1491 } 1492 #else 1493 # define check_match(s, start, match, length) 1494 #endif /* ZLIB_DEBUG */ 1495 1496 /* =========================================================================== 1497 * Fill the window when the lookahead becomes insufficient. 1498 * Updates strstart and lookahead. 1499 * 1500 * IN assertion: lookahead < MIN_LOOKAHEAD 1501 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 1502 * At least one byte has been read, or avail_in == 0; reads are 1503 * performed for at least two bytes (required for the zip translate_eol 1504 * option -- not supported here). 1505 */ 1506 local void fill_window(s) 1507 deflate_state *s; 1508 { 1509 unsigned n; 1510 unsigned more; /* Amount of free space at the end of the window. */ 1511 uInt wsize = s->w_size; 1512 1513 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); 1514 1515 do { 1516 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 1517 1518 /* Deal with !@#$% 64K limit: */ 1519 if (sizeof(int) <= 2) { 1520 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 1521 more = wsize; 1522 1523 } else if (more == (unsigned)(-1)) { 1524 /* Very unlikely, but possible on 16 bit machine if 1525 * strstart == 0 && lookahead == 1 (input done a byte at time) 1526 */ 1527 more--; 1528 } 1529 } 1530 1531 /* If the window is almost full and there is insufficient lookahead, 1532 * move the upper half to the lower one to make room in the upper half. 1533 */ 1534 if (s->strstart >= wsize+MAX_DIST(s)) { 1535 1536 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); 1537 s->match_start -= wsize; 1538 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 1539 s->block_start -= (long) wsize; 1540 slide_hash(s); 1541 more += wsize; 1542 } 1543 if (s->strm->avail_in == 0) break; 1544 1545 /* If there was no sliding: 1546 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1547 * more == window_size - lookahead - strstart 1548 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1549 * => more >= window_size - 2*WSIZE + 2 1550 * In the BIG_MEM or MMAP case (not yet supported), 1551 * window_size == input_size + MIN_LOOKAHEAD && 1552 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1553 * Otherwise, window_size == 2*WSIZE so more >= 2. 1554 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1555 */ 1556 Assert(more >= 2, "more < 2"); 1557 1558 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 1559 s->lookahead += n; 1560 1561 /* Initialize the hash value now that we have some input: */ 1562 if (s->lookahead + s->insert >= MIN_MATCH) { 1563 uInt str = s->strstart - s->insert; 1564 s->ins_h = s->window[str]; 1565 UPDATE_HASH(s, s->ins_h, s->window[str + 1]); 1566 #if MIN_MATCH != 3 1567 Call UPDATE_HASH() MIN_MATCH-3 more times 1568 #endif 1569 while (s->insert) { 1570 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); 1571 #ifndef FASTEST 1572 s->prev[str & s->w_mask] = s->head[s->ins_h]; 1573 #endif 1574 s->head[s->ins_h] = (Pos)str; 1575 str++; 1576 s->insert--; 1577 if (s->lookahead + s->insert < MIN_MATCH) 1578 break; 1579 } 1580 } 1581 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1582 * but this is not important since only literal bytes will be emitted. 1583 */ 1584 1585 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 1586 1587 /* If the WIN_INIT bytes after the end of the current data have never been 1588 * written, then zero those bytes in order to avoid memory check reports of 1589 * the use of uninitialized (or uninitialised as Julian writes) bytes by 1590 * the longest match routines. Update the high water mark for the next 1591 * time through here. WIN_INIT is set to MAX_MATCH since the longest match 1592 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. 1593 */ 1594 if (s->high_water < s->window_size) { 1595 ulg curr = s->strstart + (ulg)(s->lookahead); 1596 ulg init; 1597 1598 if (s->high_water < curr) { 1599 /* Previous high water mark below current data -- zero WIN_INIT 1600 * bytes or up to end of window, whichever is less. 1601 */ 1602 init = s->window_size - curr; 1603 if (init > WIN_INIT) 1604 init = WIN_INIT; 1605 zmemzero(s->window + curr, (unsigned)init); 1606 s->high_water = curr + init; 1607 } 1608 else if (s->high_water < (ulg)curr + WIN_INIT) { 1609 /* High water mark at or above current data, but below current data 1610 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 1611 * to end of window, whichever is less. 1612 */ 1613 init = (ulg)curr + WIN_INIT - s->high_water; 1614 if (init > s->window_size - s->high_water) 1615 init = s->window_size - s->high_water; 1616 zmemzero(s->window + s->high_water, (unsigned)init); 1617 s->high_water += init; 1618 } 1619 } 1620 1621 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, 1622 "not enough room for search"); 1623 } 1624 1625 /* =========================================================================== 1626 * Flush the current block, with given end-of-file flag. 1627 * IN assertion: strstart is set to the end of the current match. 1628 */ 1629 #define FLUSH_BLOCK_ONLY(s, last) { \ 1630 _tr_flush_block(s, (s->block_start >= 0L ? \ 1631 (charf *)&s->window[(unsigned)s->block_start] : \ 1632 (charf *)Z_NULL), \ 1633 (ulg)((long)s->strstart - s->block_start), \ 1634 (last)); \ 1635 s->block_start = s->strstart; \ 1636 flush_pending(s->strm); \ 1637 Tracev((stderr,"[FLUSH]")); \ 1638 } 1639 1640 /* Same but force premature exit if necessary. */ 1641 #define FLUSH_BLOCK(s, last) { \ 1642 FLUSH_BLOCK_ONLY(s, last); \ 1643 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 1644 } 1645 1646 /* Maximum stored block length in deflate format (not including header). */ 1647 #define MAX_STORED 65535 1648 1649 /* Minimum of a and b. */ 1650 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 1651 1652 /* =========================================================================== 1653 * Copy without compression as much as possible from the input stream, return 1654 * the current block state. 1655 * 1656 * In case deflateParams() is used to later switch to a non-zero compression 1657 * level, s->matches (otherwise unused when storing) keeps track of the number 1658 * of hash table slides to perform. If s->matches is 1, then one hash table 1659 * slide will be done when switching. If s->matches is 2, the maximum value 1660 * allowed here, then the hash table will be cleared, since two or more slides 1661 * is the same as a clear. 1662 * 1663 * deflate_stored() is written to minimize the number of times an input byte is 1664 * copied. It is most efficient with large input and output buffers, which 1665 * maximizes the opportunites to have a single copy from next_in to next_out. 1666 */ 1667 local block_state deflate_stored(s, flush) 1668 deflate_state *s; 1669 int flush; 1670 { 1671 /* Smallest worthy block size when not flushing or finishing. By default 1672 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 1673 * large input and output buffers, the stored block size will be larger. 1674 */ 1675 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); 1676 1677 /* Copy as many min_block or larger stored blocks directly to next_out as 1678 * possible. If flushing, copy the remaining available input to next_out as 1679 * stored blocks, if there is enough space. 1680 */ 1681 unsigned len, left, have, last = 0; 1682 unsigned used = s->strm->avail_in; 1683 do { 1684 /* Set len to the maximum size block that we can copy directly with the 1685 * available input data and output space. Set left to how much of that 1686 * would be copied from what's left in the window. 1687 */ 1688 len = MAX_STORED; /* maximum deflate stored block length */ 1689 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 1690 if (s->strm->avail_out < have) /* need room for header */ 1691 break; 1692 /* maximum stored block length that will fit in avail_out: */ 1693 have = s->strm->avail_out - have; 1694 left = s->strstart - s->block_start; /* bytes left in window */ 1695 if (len > (ulg)left + s->strm->avail_in) 1696 len = left + s->strm->avail_in; /* limit len to the input */ 1697 if (len > have) 1698 len = have; /* limit len to the output */ 1699 1700 /* If the stored block would be less than min_block in length, or if 1701 * unable to copy all of the available input when flushing, then try 1702 * copying to the window and the pending buffer instead. Also don't 1703 * write an empty block when flushing -- deflate() does that. 1704 */ 1705 if (len < min_block && ((len == 0 && flush != Z_FINISH) || 1706 flush == Z_NO_FLUSH || 1707 len != left + s->strm->avail_in)) 1708 break; 1709 1710 /* Make a dummy stored block in pending to get the header bytes, 1711 * including any pending bits. This also updates the debugging counts. 1712 */ 1713 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; 1714 _tr_stored_block(s, (char *)0, 0L, last); 1715 1716 /* Replace the lengths in the dummy stored block with len. */ 1717 s->pending_buf[s->pending - 4] = len; 1718 s->pending_buf[s->pending - 3] = len >> 8; 1719 s->pending_buf[s->pending - 2] = ~len; 1720 s->pending_buf[s->pending - 1] = ~len >> 8; 1721 1722 /* Write the stored block header bytes. */ 1723 flush_pending(s->strm); 1724 1725 #ifdef ZLIB_DEBUG 1726 /* Update debugging counts for the data about to be copied. */ 1727 s->compressed_len += len << 3; 1728 s->bits_sent += len << 3; 1729 #endif 1730 1731 /* Copy uncompressed bytes from the window to next_out. */ 1732 if (left) { 1733 if (left > len) 1734 left = len; 1735 zmemcpy(s->strm->next_out, s->window + s->block_start, left); 1736 s->strm->next_out += left; 1737 s->strm->avail_out -= left; 1738 s->strm->total_out += left; 1739 s->block_start += left; 1740 len -= left; 1741 } 1742 1743 /* Copy uncompressed bytes directly from next_in to next_out, updating 1744 * the check value. 1745 */ 1746 if (len) { 1747 read_buf(s->strm, s->strm->next_out, len); 1748 s->strm->next_out += len; 1749 s->strm->avail_out -= len; 1750 s->strm->total_out += len; 1751 } 1752 } while (last == 0); 1753 1754 /* Update the sliding window with the last s->w_size bytes of the copied 1755 * data, or append all of the copied data to the existing window if less 1756 * than s->w_size bytes were copied. Also update the number of bytes to 1757 * insert in the hash tables, in the event that deflateParams() switches to 1758 * a non-zero compression level. 1759 */ 1760 used -= s->strm->avail_in; /* number of input bytes directly copied */ 1761 if (used) { 1762 /* If any input was used, then no unused input remains in the window, 1763 * therefore s->block_start == s->strstart. 1764 */ 1765 if (used >= s->w_size) { /* supplant the previous history */ 1766 s->matches = 2; /* clear hash */ 1767 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); 1768 s->strstart = s->w_size; 1769 } 1770 else { 1771 if (s->window_size - s->strstart <= used) { 1772 /* Slide the window down. */ 1773 s->strstart -= s->w_size; 1774 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1775 if (s->matches < 2) 1776 s->matches++; /* add a pending slide_hash() */ 1777 } 1778 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); 1779 s->strstart += used; 1780 } 1781 s->block_start = s->strstart; 1782 s->insert += MIN(used, s->w_size - s->insert); 1783 } 1784 if (s->high_water < s->strstart) 1785 s->high_water = s->strstart; 1786 1787 /* If the last block was written to next_out, then done. */ 1788 if (last) 1789 return finish_done; 1790 1791 /* If flushing and all input has been consumed, then done. */ 1792 if (flush != Z_NO_FLUSH && flush != Z_FINISH && 1793 s->strm->avail_in == 0 && (long)s->strstart == s->block_start) 1794 return block_done; 1795 1796 /* Fill the window with any remaining input. */ 1797 have = s->window_size - s->strstart - 1; 1798 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { 1799 /* Slide the window down. */ 1800 s->block_start -= s->w_size; 1801 s->strstart -= s->w_size; 1802 zmemcpy(s->window, s->window + s->w_size, s->strstart); 1803 if (s->matches < 2) 1804 s->matches++; /* add a pending slide_hash() */ 1805 have += s->w_size; /* more space now */ 1806 } 1807 if (have > s->strm->avail_in) 1808 have = s->strm->avail_in; 1809 if (have) { 1810 read_buf(s->strm, s->window + s->strstart, have); 1811 s->strstart += have; 1812 } 1813 if (s->high_water < s->strstart) 1814 s->high_water = s->strstart; 1815 1816 /* There was not enough avail_out to write a complete worthy or flushed 1817 * stored block to next_out. Write a stored block to pending instead, if we 1818 * have enough input for a worthy block, or if flushing and there is enough 1819 * room for the remaining input as a stored block in the pending buffer. 1820 */ 1821 have = (s->bi_valid + 42) >> 3; /* number of header bytes */ 1822 /* maximum stored block length that will fit in pending: */ 1823 have = MIN(s->pending_buf_size - have, MAX_STORED); 1824 min_block = MIN(have, s->w_size); 1825 left = s->strstart - s->block_start; 1826 if (left >= min_block || 1827 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && 1828 s->strm->avail_in == 0 && left <= have)) { 1829 len = MIN(left, have); 1830 last = flush == Z_FINISH && s->strm->avail_in == 0 && 1831 len == left ? 1 : 0; 1832 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); 1833 s->block_start += len; 1834 flush_pending(s->strm); 1835 } 1836 1837 /* We've done all we can with the available input and output. */ 1838 return last ? finish_started : need_more; 1839 } 1840 1841 /* =========================================================================== 1842 * Compress as much as possible from the input stream, return the current 1843 * block state. 1844 * This function does not perform lazy evaluation of matches and inserts 1845 * new strings in the dictionary only for unmatched strings or for short 1846 * matches. It is used only for the fast compression options. 1847 */ 1848 local block_state deflate_fast(s, flush) 1849 deflate_state *s; 1850 int flush; 1851 { 1852 IPos hash_head; /* head of the hash chain */ 1853 int bflush; /* set if current block must be flushed */ 1854 1855 for (;;) { 1856 /* Make sure that we always have enough lookahead, except 1857 * at the end of the input file. We need MAX_MATCH bytes 1858 * for the next match, plus MIN_MATCH bytes to insert the 1859 * string following the next match. 1860 */ 1861 if (s->lookahead < MIN_LOOKAHEAD) { 1862 fill_window(s); 1863 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1864 return need_more; 1865 } 1866 if (s->lookahead == 0) break; /* flush the current block */ 1867 } 1868 1869 /* Insert the string window[strstart .. strstart+2] in the 1870 * dictionary, and set hash_head to the head of the hash chain: 1871 */ 1872 hash_head = NIL; 1873 if (s->lookahead >= MIN_MATCH) { 1874 INSERT_STRING(s, s->strstart, hash_head); 1875 } 1876 1877 /* Find the longest match, discarding those <= prev_length. 1878 * At this point we have always match_length < MIN_MATCH 1879 */ 1880 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1881 /* To simplify the code, we prevent matches with the string 1882 * of window index 0 (in particular we have to avoid a match 1883 * of the string with itself at the start of the input file). 1884 */ 1885 s->match_length = longest_match (s, hash_head); 1886 /* longest_match() sets match_start */ 1887 } 1888 if (s->match_length >= MIN_MATCH) { 1889 check_match(s, s->strstart, s->match_start, s->match_length); 1890 1891 _tr_tally_dist(s, s->strstart - s->match_start, 1892 s->match_length - MIN_MATCH, bflush); 1893 1894 s->lookahead -= s->match_length; 1895 1896 /* Insert new strings in the hash table only if the match length 1897 * is not too large. This saves time but degrades compression. 1898 */ 1899 #ifndef FASTEST 1900 if (s->match_length <= s->max_insert_length && 1901 s->lookahead >= MIN_MATCH) { 1902 s->match_length--; /* string at strstart already in table */ 1903 do { 1904 s->strstart++; 1905 INSERT_STRING(s, s->strstart, hash_head); 1906 /* strstart never exceeds WSIZE-MAX_MATCH, so there are 1907 * always MIN_MATCH bytes ahead. 1908 */ 1909 } while (--s->match_length != 0); 1910 s->strstart++; 1911 } else 1912 #endif 1913 { 1914 s->strstart += s->match_length; 1915 s->match_length = 0; 1916 s->ins_h = s->window[s->strstart]; 1917 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1918 #if MIN_MATCH != 3 1919 Call UPDATE_HASH() MIN_MATCH-3 more times 1920 #endif 1921 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1922 * matter since it will be recomputed at next deflate call. 1923 */ 1924 } 1925 } else { 1926 /* No match, output a literal byte */ 1927 Tracevv((stderr,"%c", s->window[s->strstart])); 1928 _tr_tally_lit (s, s->window[s->strstart], bflush); 1929 s->lookahead--; 1930 s->strstart++; 1931 } 1932 if (bflush) FLUSH_BLOCK(s, 0); 1933 } 1934 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 1935 if (flush == Z_FINISH) { 1936 FLUSH_BLOCK(s, 1); 1937 return finish_done; 1938 } 1939 if (s->last_lit) 1940 FLUSH_BLOCK(s, 0); 1941 return block_done; 1942 } 1943 1944 #ifndef FASTEST 1945 /* =========================================================================== 1946 * Same as above, but achieves better compression. We use a lazy 1947 * evaluation for matches: a match is finally adopted only if there is 1948 * no better match at the next window position. 1949 */ 1950 local block_state deflate_slow(s, flush) 1951 deflate_state *s; 1952 int flush; 1953 { 1954 IPos hash_head; /* head of hash chain */ 1955 int bflush; /* set if current block must be flushed */ 1956 1957 /* Process the input block. */ 1958 for (;;) { 1959 /* Make sure that we always have enough lookahead, except 1960 * at the end of the input file. We need MAX_MATCH bytes 1961 * for the next match, plus MIN_MATCH bytes to insert the 1962 * string following the next match. 1963 */ 1964 if (s->lookahead < MIN_LOOKAHEAD) { 1965 fill_window(s); 1966 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1967 return need_more; 1968 } 1969 if (s->lookahead == 0) break; /* flush the current block */ 1970 } 1971 1972 /* Insert the string window[strstart .. strstart+2] in the 1973 * dictionary, and set hash_head to the head of the hash chain: 1974 */ 1975 hash_head = NIL; 1976 if (s->lookahead >= MIN_MATCH) { 1977 INSERT_STRING(s, s->strstart, hash_head); 1978 } 1979 1980 /* Find the longest match, discarding those <= prev_length. 1981 */ 1982 s->prev_length = s->match_length, s->prev_match = s->match_start; 1983 s->match_length = MIN_MATCH-1; 1984 1985 if (hash_head != NIL && s->prev_length < s->max_lazy_match && 1986 s->strstart - hash_head <= MAX_DIST(s)) { 1987 /* To simplify the code, we prevent matches with the string 1988 * of window index 0 (in particular we have to avoid a match 1989 * of the string with itself at the start of the input file). 1990 */ 1991 s->match_length = longest_match (s, hash_head); 1992 /* longest_match() sets match_start */ 1993 1994 if (s->match_length <= 5 && (s->strategy == Z_FILTERED 1995 #if TOO_FAR <= 32767 1996 || (s->match_length == MIN_MATCH && 1997 s->strstart - s->match_start > TOO_FAR) 1998 #endif 1999 )) { 2000 2001 /* If prev_match is also MIN_MATCH, match_start is garbage 2002 * but we will ignore the current match anyway. 2003 */ 2004 s->match_length = MIN_MATCH-1; 2005 } 2006 } 2007 /* If there was a match at the previous step and the current 2008 * match is not better, output the previous match: 2009 */ 2010 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 2011 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 2012 /* Do not insert strings in hash table beyond this. */ 2013 2014 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 2015 2016 _tr_tally_dist(s, s->strstart -1 - s->prev_match, 2017 s->prev_length - MIN_MATCH, bflush); 2018 2019 /* Insert in hash table all strings up to the end of the match. 2020 * strstart-1 and strstart are already inserted. If there is not 2021 * enough lookahead, the last two strings are not inserted in 2022 * the hash table. 2023 */ 2024 s->lookahead -= s->prev_length-1; 2025 s->prev_length -= 2; 2026 do { 2027 if (++s->strstart <= max_insert) { 2028 INSERT_STRING(s, s->strstart, hash_head); 2029 } 2030 } while (--s->prev_length != 0); 2031 s->match_available = 0; 2032 s->match_length = MIN_MATCH-1; 2033 s->strstart++; 2034 2035 if (bflush) FLUSH_BLOCK(s, 0); 2036 2037 } else if (s->match_available) { 2038 /* If there was no match at the previous position, output a 2039 * single literal. If there was a match but the current match 2040 * is longer, truncate the previous match to a single literal. 2041 */ 2042 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2043 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2044 if (bflush) { 2045 FLUSH_BLOCK_ONLY(s, 0); 2046 } 2047 s->strstart++; 2048 s->lookahead--; 2049 if (s->strm->avail_out == 0) return need_more; 2050 } else { 2051 /* There is no previous match to compare with, wait for 2052 * the next step to decide. 2053 */ 2054 s->match_available = 1; 2055 s->strstart++; 2056 s->lookahead--; 2057 } 2058 } 2059 Assert (flush != Z_NO_FLUSH, "no flush?"); 2060 if (s->match_available) { 2061 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2062 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2063 s->match_available = 0; 2064 } 2065 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; 2066 if (flush == Z_FINISH) { 2067 FLUSH_BLOCK(s, 1); 2068 return finish_done; 2069 } 2070 if (s->last_lit) 2071 FLUSH_BLOCK(s, 0); 2072 return block_done; 2073 } 2074 #endif /* FASTEST */ 2075 2076 /* =========================================================================== 2077 * For Z_RLE, simply look for runs of bytes, generate matches only of distance 2078 * one. Do not maintain a hash table. (It will be regenerated if this run of 2079 * deflate switches away from Z_RLE.) 2080 */ 2081 local block_state deflate_rle(s, flush) 2082 deflate_state *s; 2083 int flush; 2084 { 2085 int bflush; /* set if current block must be flushed */ 2086 uInt prev; /* byte at distance one to match */ 2087 Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2088 2089 for (;;) { 2090 /* Make sure that we always have enough lookahead, except 2091 * at the end of the input file. We need MAX_MATCH bytes 2092 * for the longest run, plus one for the unrolled loop. 2093 */ 2094 if (s->lookahead <= MAX_MATCH) { 2095 fill_window(s); 2096 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { 2097 return need_more; 2098 } 2099 if (s->lookahead == 0) break; /* flush the current block */ 2100 } 2101 2102 /* See how many times the previous byte repeats */ 2103 s->match_length = 0; 2104 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 2105 scan = s->window + s->strstart - 1; 2106 prev = *scan; 2107 if (prev == *++scan && prev == *++scan && prev == *++scan) { 2108 strend = s->window + s->strstart + MAX_MATCH; 2109 do { 2110 } while (prev == *++scan && prev == *++scan && 2111 prev == *++scan && prev == *++scan && 2112 prev == *++scan && prev == *++scan && 2113 prev == *++scan && prev == *++scan && 2114 scan < strend); 2115 s->match_length = MAX_MATCH - (uInt)(strend - scan); 2116 if (s->match_length > s->lookahead) 2117 s->match_length = s->lookahead; 2118 } 2119 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); 2120 } 2121 2122 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 2123 if (s->match_length >= MIN_MATCH) { 2124 check_match(s, s->strstart, s->strstart - 1, s->match_length); 2125 2126 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 2127 2128 s->lookahead -= s->match_length; 2129 s->strstart += s->match_length; 2130 s->match_length = 0; 2131 } else { 2132 /* No match, output a literal byte */ 2133 Tracevv((stderr,"%c", s->window[s->strstart])); 2134 _tr_tally_lit (s, s->window[s->strstart], bflush); 2135 s->lookahead--; 2136 s->strstart++; 2137 } 2138 if (bflush) FLUSH_BLOCK(s, 0); 2139 } 2140 s->insert = 0; 2141 if (flush == Z_FINISH) { 2142 FLUSH_BLOCK(s, 1); 2143 return finish_done; 2144 } 2145 if (s->last_lit) 2146 FLUSH_BLOCK(s, 0); 2147 return block_done; 2148 } 2149 2150 /* =========================================================================== 2151 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 2152 * (It will be regenerated if this run of deflate switches away from Huffman.) 2153 */ 2154 local block_state deflate_huff(s, flush) 2155 deflate_state *s; 2156 int flush; 2157 { 2158 int bflush; /* set if current block must be flushed */ 2159 2160 for (;;) { 2161 /* Make sure that we have a literal to write. */ 2162 if (s->lookahead == 0) { 2163 fill_window(s); 2164 if (s->lookahead == 0) { 2165 if (flush == Z_NO_FLUSH) 2166 return need_more; 2167 break; /* flush the current block */ 2168 } 2169 } 2170 2171 /* Output a literal byte */ 2172 s->match_length = 0; 2173 Tracevv((stderr,"%c", s->window[s->strstart])); 2174 _tr_tally_lit (s, s->window[s->strstart], bflush); 2175 s->lookahead--; 2176 s->strstart++; 2177 if (bflush) FLUSH_BLOCK(s, 0); 2178 } 2179 s->insert = 0; 2180 if (flush == Z_FINISH) { 2181 FLUSH_BLOCK(s, 1); 2182 return finish_done; 2183 } 2184 if (s->last_lit) 2185 FLUSH_BLOCK(s, 0); 2186 return block_done; 2187 }