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