1 /* $NetBSD: zlib.c,v 1.6 1997/05/17 21:12:14 christos Exp $ */ 2 3 /* 4 * This file is derived from various .h and .c files from the zlib-0.95 5 * distribution by Jean-loup Gailly and Mark Adler, with some additions 6 * by Paul Mackerras to aid in implementing Deflate compression and 7 * decompression for PPP packets. See zlib.h for conditions of 8 * distribution and use. 9 * 10 * Changes that have been made include: 11 * - changed functions not used outside this file to "local" 12 * - added minCompression parameter to deflateInit2 13 * - added Z_PACKET_FLUSH (see zlib.h for details) 14 * - added inflateIncomp 15 * 16 * Id: zlib.c,v 1.6 1997/04/30 05:41:19 paulus Exp 17 */ 18 19 /* 20 * ==FILEVERSION 970421== 21 * 22 * This marker is used by the Linux installation script to determine 23 * whether an up-to-date version of this file is already installed. 24 */ 25 26 /*+++++*/ 27 /* zutil.h -- internal interface and configuration of the compression library 28 * Copyright (C) 1995 Jean-loup Gailly. 29 * For conditions of distribution and use, see copyright notice in zlib.h 30 */ 31 32 /* WARNING: this file should *not* be used by applications. It is 33 part of the implementation of the compression library and is 34 subject to change. Applications should only use zlib.h. 35 */ 36 37 /* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */ 38 39 #define _Z_UTIL_H 40 41 #include "zlib.h" 42 43 #ifndef local 44 # define local static 45 #endif 46 /* compile with -Dlocal if your debugger can't find static symbols */ 47 48 #define FAR 49 50 typedef unsigned char uch; 51 typedef uch FAR uchf; 52 typedef unsigned short ush; 53 typedef ush FAR ushf; 54 typedef unsigned long ulg; 55 56 extern char *z_errmsg[]; /* indexed by 1-zlib_error */ 57 58 #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err) 59 /* To be used only when the state is known to be valid */ 60 61 #ifndef NULL 62 #define NULL ((void *) 0) 63 #endif 64 65 /* common constants */ 66 67 #define DEFLATED 8 68 69 #ifndef DEF_WBITS 70 # define DEF_WBITS MAX_WBITS 71 #endif 72 /* default windowBits for decompression. MAX_WBITS is for compression only */ 73 74 #if MAX_MEM_LEVEL >= 8 75 # define DEF_MEM_LEVEL 8 76 #else 77 # define DEF_MEM_LEVEL MAX_MEM_LEVEL 78 #endif 79 /* default memLevel */ 80 81 #define STORED_BLOCK 0 82 #define STATIC_TREES 1 83 #define DYN_TREES 2 84 /* The three kinds of block type */ 85 86 #define MIN_MATCH 3 87 #define MAX_MATCH 258 88 /* The minimum and maximum match lengths */ 89 90 /* functions */ 91 92 #if defined(KERNEL) || defined(_KERNEL) 93 #include <sys/types.h> 94 #include <sys/time.h> 95 #include <sys/systm.h> 96 # define zmemcpy(d, s, n) bcopy((s), (d), (n)) 97 # define zmemzero bzero 98 99 #else 100 #if defined(__KERNEL__) 101 /* Assume this is Linux */ 102 #include <linux/string.h> 103 #define zmemcpy memcpy 104 #define zmemzero(dest, len) memset(dest, 0, len) 105 106 #else /* not kernel */ 107 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 108 # define HAVE_MEMCPY 109 #endif 110 #ifdef HAVE_MEMCPY 111 # define zmemcpy memcpy 112 # define zmemzero(dest, len) memset(dest, 0, len) 113 #else 114 extern void zmemcpy OF((Bytef* dest, Bytef* source, uInt len)); 115 extern void zmemzero OF((Bytef* dest, uInt len)); 116 #endif 117 #endif /* __KERNEL__ */ 118 #endif /* KERNEL */ 119 120 /* Diagnostic functions */ 121 #ifdef DEBUG_ZLIB 122 # include <stdio.h> 123 # ifndef verbose 124 # define verbose 0 125 # endif 126 # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 127 # define Trace(x) fprintf x 128 # define Tracev(x) {if (verbose) fprintf x ;} 129 # define Tracevv(x) {if (verbose>1) fprintf x ;} 130 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} 131 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} 132 #else 133 # define Assert(cond,msg) 134 # define Trace(x) 135 # define Tracev(x) 136 # define Tracevv(x) 137 # define Tracec(c,x) 138 # define Tracecv(c,x) 139 #endif 140 141 142 typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len)); 143 144 /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */ 145 /* void zcfree OF((voidpf opaque, voidpf ptr)); */ 146 147 #define ZALLOC(strm, items, size) \ 148 (*((strm)->zalloc))((strm)->opaque, (items), (size)) 149 #define ZALLOC_INIT(strm, items, size) \ 150 (*((strm)->zalloc_init))((strm)->opaque, (items), (size)) 151 #define ZFREE(strm, addr, size) \ 152 (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size)) 153 #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);} 154 155 /* deflate.h -- internal compression state 156 * Copyright (C) 1995 Jean-loup Gailly 157 * For conditions of distribution and use, see copyright notice in zlib.h 158 */ 159 160 /* WARNING: this file should *not* be used by applications. It is 161 part of the implementation of the compression library and is 162 subject to change. Applications should only use zlib.h. 163 */ 164 165 166 /*+++++*/ 167 /* From: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp */ 168 169 /* =========================================================================== 170 * Internal compression state. 171 */ 172 173 /* Data type */ 174 #define Z_BINARY 0 175 #define ASCII 1 176 #define UNKNOWN 2 177 178 #define LENGTH_CODES 29 179 /* number of length codes, not counting the special END_BLOCK code */ 180 181 #define LITERALS 256 182 /* number of literal bytes 0..255 */ 183 184 #define L_CODES (LITERALS+1+LENGTH_CODES) 185 /* number of Literal or Length codes, including the END_BLOCK code */ 186 187 #define D_CODES 30 188 /* number of distance codes */ 189 190 #define BL_CODES 19 191 /* number of codes used to transfer the bit lengths */ 192 193 #define HEAP_SIZE (2*L_CODES+1) 194 /* maximum heap size */ 195 196 #define MAX_BITS 15 197 /* All codes must not exceed MAX_BITS bits */ 198 199 #define INIT_STATE 42 200 #define BUSY_STATE 113 201 #define FLUSH_STATE 124 202 #define FINISH_STATE 666 203 /* Stream status */ 204 205 206 /* Data structure describing a single value and its code string. */ 207 typedef struct ct_data_s { 208 union { 209 ush freq; /* frequency count */ 210 ush code; /* bit string */ 211 } fc; 212 union { 213 ush dad; /* father node in Huffman tree */ 214 ush len; /* length of bit string */ 215 } dl; 216 } FAR ct_data; 217 218 #define Freq fc.freq 219 #define Code fc.code 220 #define Dad dl.dad 221 #define Len dl.len 222 223 typedef struct static_tree_desc_s static_tree_desc; 224 225 typedef struct tree_desc_s { 226 ct_data *dyn_tree; /* the dynamic tree */ 227 int max_code; /* largest code with non zero frequency */ 228 static_tree_desc *stat_desc; /* the corresponding static tree */ 229 } FAR tree_desc; 230 231 typedef ush Pos; 232 typedef Pos FAR Posf; 233 typedef unsigned IPos; 234 235 /* A Pos is an index in the character window. We use short instead of int to 236 * save space in the various tables. IPos is used only for parameter passing. 237 */ 238 239 typedef struct deflate_state { 240 z_stream *strm; /* pointer back to this zlib stream */ 241 int status; /* as the name implies */ 242 Bytef *pending_buf; /* output still pending */ 243 Bytef *pending_out; /* next pending byte to output to the stream */ 244 int pending; /* nb of bytes in the pending buffer */ 245 uLong adler; /* adler32 of uncompressed data */ 246 int noheader; /* suppress zlib header and adler32 */ 247 Byte data_type; /* UNKNOWN, Z_BINARY or ASCII */ 248 Byte method; /* STORED (for zip only) or DEFLATED */ 249 int minCompr; /* min size decrease for Z_FLUSH_NOSTORE */ 250 251 /* used by deflate.c: */ 252 253 uInt w_size; /* LZ77 window size (32K by default) */ 254 uInt w_bits; /* log2(w_size) (8..16) */ 255 uInt w_mask; /* w_size - 1 */ 256 257 Bytef *window; 258 /* Sliding window. Input bytes are read into the second half of the window, 259 * and move to the first half later to keep a dictionary of at least wSize 260 * bytes. With this organization, matches are limited to a distance of 261 * wSize-MAX_MATCH bytes, but this ensures that IO is always 262 * performed with a length multiple of the block size. Also, it limits 263 * the window size to 64K, which is quite useful on MSDOS. 264 * To do: use the user input buffer as sliding window. 265 */ 266 267 ulg window_size; 268 /* Actual size of window: 2*wSize, except when the user input buffer 269 * is directly used as sliding window. 270 */ 271 272 Posf *prev; 273 /* Link to older string with same hash index. To limit the size of this 274 * array to 64K, this link is maintained only for the last 32K strings. 275 * An index in this array is thus a window index modulo 32K. 276 */ 277 278 Posf *head; /* Heads of the hash chains or NIL. */ 279 280 uInt ins_h; /* hash index of string to be inserted */ 281 uInt hash_size; /* number of elements in hash table */ 282 uInt hash_bits; /* log2(hash_size) */ 283 uInt hash_mask; /* hash_size-1 */ 284 285 uInt hash_shift; 286 /* Number of bits by which ins_h must be shifted at each input 287 * step. It must be such that after MIN_MATCH steps, the oldest 288 * byte no longer takes part in the hash key, that is: 289 * hash_shift * MIN_MATCH >= hash_bits 290 */ 291 292 long block_start; 293 /* Window position at the beginning of the current output block. Gets 294 * negative when the window is moved backwards. 295 */ 296 297 uInt match_length; /* length of best match */ 298 IPos prev_match; /* previous match */ 299 int match_available; /* set if previous match exists */ 300 uInt strstart; /* start of string to insert */ 301 uInt match_start; /* start of matching string */ 302 uInt lookahead; /* number of valid bytes ahead in window */ 303 304 uInt prev_length; 305 /* Length of the best match at previous step. Matches not greater than this 306 * are discarded. This is used in the lazy match evaluation. 307 */ 308 309 uInt max_chain_length; 310 /* To speed up deflation, hash chains are never searched beyond this 311 * length. A higher limit improves compression ratio but degrades the 312 * speed. 313 */ 314 315 uInt max_lazy_match; 316 /* Attempt to find a better match only when the current match is strictly 317 * smaller than this value. This mechanism is used only for compression 318 * levels >= 4. 319 */ 320 # define max_insert_length max_lazy_match 321 /* Insert new strings in the hash table only if the match length is not 322 * greater than this length. This saves time but degrades compression. 323 * max_insert_length is used only for compression levels <= 3. 324 */ 325 326 int level; /* compression level (1..9) */ 327 int strategy; /* favor or force Huffman coding*/ 328 329 uInt good_match; 330 /* Use a faster search when the previous match is longer than this */ 331 332 int nice_match; /* Stop searching when current match exceeds this */ 333 334 /* used by trees.c: */ 335 /* Didn't use ct_data typedef below to supress compiler warning */ 336 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 337 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 338 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 339 340 struct tree_desc_s l_desc; /* desc. for literal tree */ 341 struct tree_desc_s d_desc; /* desc. for distance tree */ 342 struct tree_desc_s bl_desc; /* desc. for bit length tree */ 343 344 ush bl_count[MAX_BITS+1]; 345 /* number of codes at each bit length for an optimal tree */ 346 347 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 348 int heap_len; /* number of elements in the heap */ 349 int heap_max; /* element of largest frequency */ 350 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 351 * The same heap array is used to build all trees. 352 */ 353 354 uch depth[2*L_CODES+1]; 355 /* Depth of each subtree used as tie breaker for trees of equal frequency 356 */ 357 358 uchf *l_buf; /* buffer for literals or lengths */ 359 360 uInt lit_bufsize; 361 /* Size of match buffer for literals/lengths. There are 4 reasons for 362 * limiting lit_bufsize to 64K: 363 * - frequencies can be kept in 16 bit counters 364 * - if compression is not successful for the first block, all input 365 * data is still in the window so we can still emit a stored block even 366 * when input comes from standard input. (This can also be done for 367 * all blocks if lit_bufsize is not greater than 32K.) 368 * - if compression is not successful for a file smaller than 64K, we can 369 * even emit a stored file instead of a stored block (saving 5 bytes). 370 * This is applicable only for zip (not gzip or zlib). 371 * - creating new Huffman trees less frequently may not provide fast 372 * adaptation to changes in the input data statistics. (Take for 373 * example a binary file with poorly compressible code followed by 374 * a highly compressible string table.) Smaller buffer sizes give 375 * fast adaptation but have of course the overhead of transmitting 376 * trees more frequently. 377 * - I can't count above 4 378 */ 379 380 uInt last_lit; /* running index in l_buf */ 381 382 ushf *d_buf; 383 /* Buffer for distances. To simplify the code, d_buf and l_buf have 384 * the same number of elements. To use different lengths, an extra flag 385 * array would be necessary. 386 */ 387 388 ulg opt_len; /* bit length of current block with optimal trees */ 389 ulg static_len; /* bit length of current block with static trees */ 390 ulg compressed_len; /* total bit length of compressed file */ 391 uInt matches; /* number of string matches in current block */ 392 int last_eob_len; /* bit length of EOB code for last block */ 393 394 #ifdef DEBUG_ZLIB 395 ulg bits_sent; /* bit length of the compressed data */ 396 #endif 397 398 ush bi_buf; 399 /* Output buffer. bits are inserted starting at the bottom (least 400 * significant bits). 401 */ 402 int bi_valid; 403 /* Number of valid bits in bi_buf. All bits above the last valid bit 404 * are always zero. 405 */ 406 407 uInt blocks_in_packet; 408 /* Number of blocks produced since the last time Z_PACKET_FLUSH 409 * was used. 410 */ 411 412 } FAR deflate_state; 413 414 /* Output a byte on the stream. 415 * IN assertion: there is enough room in pending_buf. 416 */ 417 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} 418 419 420 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 421 /* Minimum amount of lookahead, except at the end of the input file. 422 * See deflate.c for comments about the MIN_MATCH+1. 423 */ 424 425 #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 426 /* In order to simplify the code, particularly on 16 bit machines, match 427 * distances are limited to MAX_DIST instead of WSIZE. 428 */ 429 430 /* in trees.c */ 431 local void ct_init OF((deflate_state *s)); 432 local int ct_tally OF((deflate_state *s, int dist, int lc)); 433 local ulg ct_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, 434 int flush)); 435 local void ct_align OF((deflate_state *s)); 436 local void ct_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, 437 int eof)); 438 local void ct_stored_type_only OF((deflate_state *s)); 439 440 441 /*+++++*/ 442 /* deflate.c -- compress data using the deflation algorithm 443 * Copyright (C) 1995 Jean-loup Gailly. 444 * For conditions of distribution and use, see copyright notice in zlib.h 445 */ 446 447 /* 448 * ALGORITHM 449 * 450 * The "deflation" process depends on being able to identify portions 451 * of the input text which are identical to earlier input (within a 452 * sliding window trailing behind the input currently being processed). 453 * 454 * The most straightforward technique turns out to be the fastest for 455 * most input files: try all possible matches and select the longest. 456 * The key feature of this algorithm is that insertions into the string 457 * dictionary are very simple and thus fast, and deletions are avoided 458 * completely. Insertions are performed at each input character, whereas 459 * string matches are performed only when the previous match ends. So it 460 * is preferable to spend more time in matches to allow very fast string 461 * insertions and avoid deletions. The matching algorithm for small 462 * strings is inspired from that of Rabin & Karp. A brute force approach 463 * is used to find longer strings when a small match has been found. 464 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze 465 * (by Leonid Broukhis). 466 * A previous version of this file used a more sophisticated algorithm 467 * (by Fiala and Greene) which is guaranteed to run in linear amortized 468 * time, but has a larger average cost, uses more memory and is patented. 469 * However the F&G algorithm may be faster for some highly redundant 470 * files if the parameter max_chain_length (described below) is too large. 471 * 472 * ACKNOWLEDGEMENTS 473 * 474 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and 475 * I found it in 'freeze' written by Leonid Broukhis. 476 * Thanks to many people for bug reports and testing. 477 * 478 * REFERENCES 479 * 480 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". 481 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc 482 * 483 * A description of the Rabin and Karp algorithm is given in the book 484 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. 485 * 486 * Fiala,E.R., and Greene,D.H. 487 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 488 * 489 */ 490 491 /* From: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp */ 492 493 #if 0 494 local char zlib_copyright[] = " deflate Copyright 1995 Jean-loup Gailly "; 495 #endif 496 /* 497 If you use the zlib library in a product, an acknowledgment is welcome 498 in the documentation of your product. If for some reason you cannot 499 include such an acknowledgment, I would appreciate that you keep this 500 copyright string in the executable of your product. 501 */ 502 503 #define NIL 0 504 /* Tail of hash chains */ 505 506 #ifndef TOO_FAR 507 # define TOO_FAR 4096 508 #endif 509 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ 510 511 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 512 /* Minimum amount of lookahead, except at the end of the input file. 513 * See deflate.c for comments about the MIN_MATCH+1. 514 */ 515 516 /* Values for max_lazy_match, good_match and max_chain_length, depending on 517 * the desired pack level (0..9). The values given below have been tuned to 518 * exclude worst case performance for pathological files. Better values may be 519 * found for specific files. 520 */ 521 522 typedef struct config_s { 523 ush good_length; /* reduce lazy search above this match length */ 524 ush max_lazy; /* do not perform lazy search above this match length */ 525 ush nice_length; /* quit search above this match length */ 526 ush max_chain; 527 } config; 528 529 local config configuration_table[10] = { 530 /* good lazy nice chain */ 531 /* 0 */ {0, 0, 0, 0}, /* store only */ 532 /* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */ 533 /* 2 */ {4, 5, 16, 8}, 534 /* 3 */ {4, 6, 32, 32}, 535 536 /* 4 */ {4, 4, 16, 16}, /* lazy matches */ 537 /* 5 */ {8, 16, 32, 32}, 538 /* 6 */ {8, 16, 128, 128}, 539 /* 7 */ {8, 32, 128, 256}, 540 /* 8 */ {32, 128, 258, 1024}, 541 /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */ 542 543 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 544 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 545 * meaning. 546 */ 547 548 #define EQUAL 0 549 /* result of memcmp for equal strings */ 550 551 /* =========================================================================== 552 * Prototypes for local functions. 553 */ 554 555 local void fill_window OF((deflate_state *s)); 556 local int deflate_fast OF((deflate_state *s, int flush)); 557 local int deflate_slow OF((deflate_state *s, int flush)); 558 local void lm_init OF((deflate_state *s)); 559 local int longest_match OF((deflate_state *s, IPos cur_match)); 560 local void putShortMSB OF((deflate_state *s, uInt b)); 561 local void flush_pending OF((z_stream *strm)); 562 local int read_buf OF((z_stream *strm, charf *buf, unsigned size)); 563 #ifdef ASMV 564 void match_init OF((void)); /* asm code initialization */ 565 #endif 566 567 #ifdef DEBUG_ZLIB 568 local void check_match OF((deflate_state *s, IPos start, IPos match, 569 int length)); 570 #endif 571 572 573 /* =========================================================================== 574 * Update a hash value with the given input byte 575 * IN assertion: all calls to to UPDATE_HASH are made with consecutive 576 * input characters, so that a running hash key can be computed from the 577 * previous key instead of complete recalculation each time. 578 */ 579 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 580 581 582 /* =========================================================================== 583 * Insert string str in the dictionary and set match_head to the previous head 584 * of the hash chain (the most recent string with same hash key). Return 585 * the previous length of the hash chain. 586 * IN assertion: all calls to to INSERT_STRING are made with consecutive 587 * input characters and the first MIN_MATCH bytes of str are valid 588 * (except for the last MIN_MATCH-1 bytes of the input file). 589 */ 590 #define INSERT_STRING(s, str, match_head) \ 591 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 592 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \ 593 s->head[s->ins_h] = (str)) 594 595 /* =========================================================================== 596 * Initialize the hash table (avoiding 64K overflow for 16 bit systems). 597 * prev[] will be initialized on the fly. 598 */ 599 #define CLEAR_HASH(s) \ 600 s->head[s->hash_size-1] = NIL; \ 601 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 602 603 /* ========================================================================= */ 604 int deflateInit (strm, level) 605 z_stream *strm; 606 int level; 607 { 608 return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 609 0, 0); 610 /* To do: ignore strm->next_in if we use it as window */ 611 } 612 613 /* ========================================================================= */ 614 int deflateInit2 (strm, level, method, windowBits, memLevel, 615 strategy, minCompression) 616 z_stream *strm; 617 int level; 618 int method; 619 int windowBits; 620 int memLevel; 621 int strategy; 622 int minCompression; 623 { 624 deflate_state *s; 625 int noheader = 0; 626 627 if (strm == Z_NULL) return Z_STREAM_ERROR; 628 629 strm->msg = Z_NULL; 630 /* if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; */ 631 /* if (strm->zfree == Z_NULL) strm->zfree = zcfree; */ 632 633 if (level == Z_DEFAULT_COMPRESSION) level = 6; 634 635 if (windowBits < 0) { /* undocumented feature: suppress zlib header */ 636 noheader = 1; 637 windowBits = -windowBits; 638 } 639 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED || 640 windowBits < 8 || windowBits > 15 || level < 1 || level > 9) { 641 return Z_STREAM_ERROR; 642 } 643 s = (deflate_state *) ZALLOC_INIT(strm, 1, sizeof(deflate_state)); 644 if (s == Z_NULL) return Z_MEM_ERROR; 645 strm->state = (struct internal_state FAR *)s; 646 s->strm = strm; 647 648 s->noheader = noheader; 649 s->w_bits = windowBits; 650 s->w_size = 1 << s->w_bits; 651 s->w_mask = s->w_size - 1; 652 653 s->hash_bits = memLevel + 7; 654 s->hash_size = 1 << s->hash_bits; 655 s->hash_mask = s->hash_size - 1; 656 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 657 658 s->window = (Bytef *) ZALLOC_INIT(strm, s->w_size, 2*sizeof(Byte)); 659 s->prev = (Posf *) ZALLOC_INIT(strm, s->w_size, sizeof(Pos)); 660 s->head = (Posf *) ZALLOC_INIT(strm, s->hash_size, sizeof(Pos)); 661 662 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 663 664 s->pending_buf = (uchf *) ZALLOC_INIT(strm, s->lit_bufsize, 2*sizeof(ush)); 665 666 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 667 s->pending_buf == Z_NULL) { 668 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 669 deflateEnd (strm); 670 return Z_MEM_ERROR; 671 } 672 s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]); 673 s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]); 674 /* We overlay pending_buf and d_buf+l_buf. This works since the average 675 * output size for (length,distance) codes is <= 32 bits (worst case 676 * is 15+15+13=33). 677 */ 678 679 s->level = level; 680 s->strategy = strategy; 681 s->method = (Byte)method; 682 s->minCompr = minCompression; 683 s->blocks_in_packet = 0; 684 685 return deflateReset(strm); 686 } 687 688 /* ========================================================================= */ 689 int deflateReset (strm) 690 z_stream *strm; 691 { 692 deflate_state *s; 693 694 if (strm == Z_NULL || strm->state == Z_NULL || 695 strm->zalloc == Z_NULL || strm->zfree == Z_NULL || 696 strm->zalloc_init == Z_NULL) return Z_STREAM_ERROR; 697 698 strm->total_in = strm->total_out = 0; 699 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 700 strm->data_type = Z_UNKNOWN; 701 702 s = (deflate_state *)strm->state; 703 s->pending = 0; 704 s->pending_out = s->pending_buf; 705 706 if (s->noheader < 0) { 707 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */ 708 } 709 s->status = s->noheader ? BUSY_STATE : INIT_STATE; 710 s->adler = 1; 711 712 ct_init(s); 713 lm_init(s); 714 715 return Z_OK; 716 } 717 718 /* ========================================================================= 719 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 720 * IN assertion: the stream state is correct and there is enough room in 721 * pending_buf. 722 */ 723 local void putShortMSB (s, b) 724 deflate_state *s; 725 uInt b; 726 { 727 put_byte(s, (Byte)(b >> 8)); 728 put_byte(s, (Byte)(b & 0xff)); 729 } 730 731 /* ========================================================================= 732 * Flush as much pending output as possible. 733 */ 734 local void flush_pending(strm) 735 z_stream *strm; 736 { 737 deflate_state *state = (deflate_state *) strm->state; 738 unsigned len = state->pending; 739 740 if (len > strm->avail_out) len = strm->avail_out; 741 if (len == 0) return; 742 743 if (strm->next_out != NULL) { 744 zmemcpy(strm->next_out, state->pending_out, len); 745 strm->next_out += len; 746 } 747 state->pending_out += len; 748 strm->total_out += len; 749 strm->avail_out -= len; 750 state->pending -= len; 751 if (state->pending == 0) { 752 state->pending_out = state->pending_buf; 753 } 754 } 755 756 /* ========================================================================= */ 757 int deflate (strm, flush) 758 z_stream *strm; 759 int flush; 760 { 761 deflate_state *state = (deflate_state *) strm->state; 762 763 if (strm == Z_NULL || state == Z_NULL) return Z_STREAM_ERROR; 764 765 if (strm->next_in == Z_NULL && strm->avail_in != 0) { 766 ERR_RETURN(strm, Z_STREAM_ERROR); 767 } 768 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 769 770 state->strm = strm; /* just in case */ 771 772 /* Write the zlib header */ 773 if (state->status == INIT_STATE) { 774 775 uInt header = (DEFLATED + ((state->w_bits-8)<<4)) << 8; 776 uInt level_flags = (state->level-1) >> 1; 777 778 if (level_flags > 3) level_flags = 3; 779 header |= (level_flags << 6); 780 header += 31 - (header % 31); 781 782 state->status = BUSY_STATE; 783 putShortMSB(state, header); 784 } 785 786 /* Flush as much pending output as possible */ 787 if (state->pending != 0) { 788 flush_pending(strm); 789 if (strm->avail_out == 0) return Z_OK; 790 } 791 792 /* If we came back in here to get the last output from 793 * a previous flush, we're done for now. 794 */ 795 if (state->status == FLUSH_STATE) { 796 state->status = BUSY_STATE; 797 if (flush != Z_NO_FLUSH && flush != Z_FINISH) 798 return Z_OK; 799 } 800 801 /* User must not provide more input after the first FINISH: */ 802 if (state->status == FINISH_STATE && strm->avail_in != 0) { 803 ERR_RETURN(strm, Z_BUF_ERROR); 804 } 805 806 /* Start a new block or continue the current one. 807 */ 808 if (strm->avail_in != 0 || state->lookahead != 0 || 809 (flush == Z_FINISH && state->status != FINISH_STATE)) { 810 int quit; 811 812 if (flush == Z_FINISH) { 813 state->status = FINISH_STATE; 814 } 815 if (state->level <= 3) { 816 quit = deflate_fast(state, flush); 817 } else { 818 quit = deflate_slow(state, flush); 819 } 820 if (quit || strm->avail_out == 0) 821 return Z_OK; 822 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 823 * of deflate should use the same flush parameter to make sure 824 * that the flush is complete. So we don't have to output an 825 * empty block here, this will be done at next call. This also 826 * ensures that for a very small output buffer, we emit at most 827 * one empty block. 828 */ 829 } 830 831 /* If a flush was requested, we have a little more to output now. */ 832 if (flush != Z_NO_FLUSH && flush != Z_FINISH 833 && state->status != FINISH_STATE) { 834 switch (flush) { 835 case Z_PARTIAL_FLUSH: 836 ct_align(state); 837 break; 838 case Z_PACKET_FLUSH: 839 /* Output just the 3-bit `stored' block type value, 840 but not a zero length. */ 841 ct_stored_type_only(state); 842 break; 843 default: 844 ct_stored_block(state, (char*)0, 0L, 0); 845 /* For a full flush, this empty block will be recognized 846 * as a special marker by inflate_sync(). 847 */ 848 if (flush == Z_FULL_FLUSH) { 849 CLEAR_HASH(state); /* forget history */ 850 } 851 } 852 flush_pending(strm); 853 if (strm->avail_out == 0) { 854 /* We'll have to come back to get the rest of the output; 855 * this ensures we don't output a second zero-length stored 856 * block (or whatever). 857 */ 858 state->status = FLUSH_STATE; 859 return Z_OK; 860 } 861 } 862 863 Assert(strm->avail_out > 0, "bug2"); 864 865 if (flush != Z_FINISH) return Z_OK; 866 if (state->noheader) return Z_STREAM_END; 867 868 /* Write the zlib trailer (adler32) */ 869 putShortMSB(state, (uInt)(state->adler >> 16)); 870 putShortMSB(state, (uInt)(state->adler & 0xffff)); 871 flush_pending(strm); 872 /* If avail_out is zero, the application will call deflate again 873 * to flush the rest. 874 */ 875 state->noheader = -1; /* write the trailer only once! */ 876 return state->pending != 0 ? Z_OK : Z_STREAM_END; 877 } 878 879 /* ========================================================================= */ 880 int deflateEnd (strm) 881 z_stream *strm; 882 { 883 deflate_state *state = (deflate_state *) strm->state; 884 885 if (strm == Z_NULL || state == Z_NULL) return Z_STREAM_ERROR; 886 887 TRY_FREE(strm, state->window, state->w_size * 2 * sizeof(Byte)); 888 TRY_FREE(strm, state->prev, state->w_size * sizeof(Pos)); 889 TRY_FREE(strm, state->head, state->hash_size * sizeof(Pos)); 890 TRY_FREE(strm, state->pending_buf, state->lit_bufsize * 2 * sizeof(ush)); 891 892 ZFREE(strm, state, sizeof(deflate_state)); 893 strm->state = Z_NULL; 894 895 return Z_OK; 896 } 897 898 /* =========================================================================== 899 * Read a new buffer from the current input stream, update the adler32 900 * and total number of bytes read. 901 */ 902 local int read_buf(strm, buf, size) 903 z_stream *strm; 904 charf *buf; 905 unsigned size; 906 { 907 unsigned len = strm->avail_in; 908 deflate_state *state = (deflate_state *) strm->state; 909 910 if (len > size) len = size; 911 if (len == 0) return 0; 912 913 strm->avail_in -= len; 914 915 if (!state->noheader) { 916 state->adler = adler32(state->adler, strm->next_in, len); 917 } 918 zmemcpy(buf, strm->next_in, len); 919 strm->next_in += len; 920 strm->total_in += len; 921 922 return (int)len; 923 } 924 925 /* =========================================================================== 926 * Initialize the "longest match" routines for a new zlib stream 927 */ 928 local void lm_init (s) 929 deflate_state *s; 930 { 931 s->window_size = (ulg)2L*s->w_size; 932 933 CLEAR_HASH(s); 934 935 /* Set the default configuration parameters: 936 */ 937 s->max_lazy_match = configuration_table[s->level].max_lazy; 938 s->good_match = configuration_table[s->level].good_length; 939 s->nice_match = configuration_table[s->level].nice_length; 940 s->max_chain_length = configuration_table[s->level].max_chain; 941 942 s->strstart = 0; 943 s->block_start = 0L; 944 s->lookahead = 0; 945 s->match_length = MIN_MATCH-1; 946 s->match_available = 0; 947 s->ins_h = 0; 948 #ifdef ASMV 949 match_init(); /* initialize the asm code */ 950 #endif 951 } 952 953 /* =========================================================================== 954 * Set match_start to the longest match starting at the given string and 955 * return its length. Matches shorter or equal to prev_length are discarded, 956 * in which case the result is equal to prev_length and match_start is 957 * garbage. 958 * IN assertions: cur_match is the head of the hash chain for the current 959 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 960 */ 961 #ifndef ASMV 962 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or 963 * match.S. The code will be functionally equivalent. 964 */ 965 local int longest_match(s, cur_match) 966 deflate_state *s; 967 IPos cur_match; /* current match */ 968 { 969 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 970 register Bytef *scan = s->window + s->strstart; /* current string */ 971 register Bytef *match; /* matched string */ 972 register int len; /* length of current match */ 973 int best_len = s->prev_length; /* best match length so far */ 974 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 975 s->strstart - (IPos)MAX_DIST(s) : NIL; 976 /* Stop when cur_match becomes <= limit. To simplify the code, 977 * we prevent matches with the string of window index 0. 978 */ 979 Posf *prev = s->prev; 980 uInt wmask = s->w_mask; 981 982 #ifdef UNALIGNED_OK 983 /* Compare two bytes at a time. Note: this is not always beneficial. 984 * Try with and without -DUNALIGNED_OK to check. 985 */ 986 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; 987 register ush scan_start = *(ushf*)scan; 988 register ush scan_end = *(ushf*)(scan+best_len-1); 989 #else 990 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 991 register Byte scan_end1 = scan[best_len-1]; 992 register Byte scan_end = scan[best_len]; 993 #endif 994 995 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 996 * It is easy to get rid of this optimization if necessary. 997 */ 998 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 999 1000 /* Do not waste too much time if we already have a good match: */ 1001 if (s->prev_length >= s->good_match) { 1002 chain_length >>= 2; 1003 } 1004 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1005 1006 do { 1007 Assert(cur_match < s->strstart, "no future"); 1008 match = s->window + cur_match; 1009 1010 /* Skip to next match if the match length cannot increase 1011 * or if the match length is less than 2: 1012 */ 1013 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1014 /* This code assumes sizeof(unsigned short) == 2. Do not use 1015 * UNALIGNED_OK if your compiler uses a different size. 1016 */ 1017 if (*(ushf*)(match+best_len-1) != scan_end || 1018 *(ushf*)match != scan_start) continue; 1019 1020 /* It is not necessary to compare scan[2] and match[2] since they are 1021 * always equal when the other bytes match, given that the hash keys 1022 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at 1023 * strstart+3, +5, ... up to strstart+257. We check for insufficient 1024 * lookahead only every 4th comparison; the 128th check will be made 1025 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is 1026 * necessary to put more guard bytes at the end of the window, or 1027 * to check more often for insufficient lookahead. 1028 */ 1029 Assert(scan[2] == match[2], "scan[2]?"); 1030 scan++, match++; 1031 do { 1032 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1033 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1034 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1035 *(ushf*)(scan+=2) == *(ushf*)(match+=2) && 1036 scan < strend); 1037 /* The funny "do {}" generates better code on most compilers */ 1038 1039 /* Here, scan <= window+strstart+257 */ 1040 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1041 if (*scan == *match) scan++; 1042 1043 len = (MAX_MATCH - 1) - (int)(strend-scan); 1044 scan = strend - (MAX_MATCH-1); 1045 1046 #else /* UNALIGNED_OK */ 1047 1048 if (match[best_len] != scan_end || 1049 match[best_len-1] != scan_end1 || 1050 *match != *scan || 1051 *++match != scan[1]) continue; 1052 1053 /* The check at best_len-1 can be removed because it will be made 1054 * again later. (This heuristic is not always a win.) 1055 * It is not necessary to compare scan[2] and match[2] since they 1056 * are always equal when the other bytes match, given that 1057 * the hash keys are equal and that HASH_BITS >= 8. 1058 */ 1059 scan += 2, match++; 1060 Assert(*scan == *match, "match[2]?"); 1061 1062 /* We check for insufficient lookahead only every 8th comparison; 1063 * the 256th check will be made at strstart+258. 1064 */ 1065 do { 1066 } while (*++scan == *++match && *++scan == *++match && 1067 *++scan == *++match && *++scan == *++match && 1068 *++scan == *++match && *++scan == *++match && 1069 *++scan == *++match && *++scan == *++match && 1070 scan < strend); 1071 1072 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1073 1074 len = MAX_MATCH - (int)(strend - scan); 1075 scan = strend - MAX_MATCH; 1076 1077 #endif /* UNALIGNED_OK */ 1078 1079 if (len > best_len) { 1080 s->match_start = cur_match; 1081 best_len = len; 1082 if (len >= s->nice_match) break; 1083 #ifdef UNALIGNED_OK 1084 scan_end = *(ushf*)(scan+best_len-1); 1085 #else 1086 scan_end1 = scan[best_len-1]; 1087 scan_end = scan[best_len]; 1088 #endif 1089 } 1090 } while ((cur_match = prev[cur_match & wmask]) > limit 1091 && --chain_length != 0); 1092 1093 return best_len; 1094 } 1095 #endif /* ASMV */ 1096 1097 #ifdef DEBUG_ZLIB 1098 /* =========================================================================== 1099 * Check that the match at match_start is indeed a match. 1100 */ 1101 local void check_match(s, start, match, length) 1102 deflate_state *s; 1103 IPos start, match; 1104 int length; 1105 { 1106 /* check that the match is indeed a match */ 1107 if (memcmp((charf *)s->window + match, 1108 (charf *)s->window + start, length) != EQUAL) { 1109 fprintf(stderr, 1110 " start %u, match %u, length %d\n", 1111 start, match, length); 1112 do { fprintf(stderr, "%c%c", s->window[match++], 1113 s->window[start++]); } while (--length != 0); 1114 z_error("invalid match"); 1115 } 1116 if (verbose > 1) { 1117 fprintf(stderr,"\\[%d,%d]", start-match, length); 1118 do { putc(s->window[start++], stderr); } while (--length != 0); 1119 } 1120 } 1121 #else 1122 # define check_match(s, start, match, length) 1123 #endif 1124 1125 /* =========================================================================== 1126 * Fill the window when the lookahead becomes insufficient. 1127 * Updates strstart and lookahead. 1128 * 1129 * IN assertion: lookahead < MIN_LOOKAHEAD 1130 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 1131 * At least one byte has been read, or avail_in == 0; reads are 1132 * performed for at least two bytes (required for the zip translate_eol 1133 * option -- not supported here). 1134 */ 1135 local void fill_window(s) 1136 deflate_state *s; 1137 { 1138 register unsigned n, m; 1139 register Posf *p; 1140 unsigned more; /* Amount of free space at the end of the window. */ 1141 uInt wsize = s->w_size; 1142 1143 do { 1144 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 1145 1146 /* Deal with !@#$% 64K limit: */ 1147 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 1148 more = wsize; 1149 } else if (more == (unsigned)(-1)) { 1150 /* Very unlikely, but possible on 16 bit machine if strstart == 0 1151 * and lookahead == 1 (input done one byte at time) 1152 */ 1153 more--; 1154 1155 /* If the window is almost full and there is insufficient lookahead, 1156 * move the upper half to the lower one to make room in the upper half. 1157 */ 1158 } else if (s->strstart >= wsize+MAX_DIST(s)) { 1159 1160 /* By the IN assertion, the window is not empty so we can't confuse 1161 * more == 0 with more == 64K on a 16 bit machine. 1162 */ 1163 zmemcpy((charf *)s->window, (charf *)s->window+wsize, 1164 (unsigned)wsize); 1165 s->match_start -= wsize; 1166 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ 1167 1168 s->block_start -= (long) wsize; 1169 1170 /* Slide the hash table (could be avoided with 32 bit values 1171 at the expense of memory usage): 1172 */ 1173 n = s->hash_size; 1174 p = &s->head[n]; 1175 do { 1176 m = *--p; 1177 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1178 } while (--n); 1179 1180 n = wsize; 1181 p = &s->prev[n]; 1182 do { 1183 m = *--p; 1184 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1185 /* If n is not on any hash chain, prev[n] is garbage but 1186 * its value will never be used. 1187 */ 1188 } while (--n); 1189 1190 more += wsize; 1191 } 1192 if (s->strm->avail_in == 0) return; 1193 1194 /* If there was no sliding: 1195 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1196 * more == window_size - lookahead - strstart 1197 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1198 * => more >= window_size - 2*WSIZE + 2 1199 * In the BIG_MEM or MMAP case (not yet supported), 1200 * window_size == input_size + MIN_LOOKAHEAD && 1201 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1202 * Otherwise, window_size == 2*WSIZE so more >= 2. 1203 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1204 */ 1205 Assert(more >= 2, "more < 2"); 1206 1207 n = read_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead, 1208 more); 1209 s->lookahead += n; 1210 1211 /* Initialize the hash value now that we have some input: */ 1212 if (s->lookahead >= MIN_MATCH) { 1213 s->ins_h = s->window[s->strstart]; 1214 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1215 #if MIN_MATCH != 3 1216 Call UPDATE_HASH() MIN_MATCH-3 more times 1217 #endif 1218 } 1219 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1220 * but this is not important since only literal bytes will be emitted. 1221 */ 1222 1223 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 1224 } 1225 1226 /* =========================================================================== 1227 * Flush the current block, with given end-of-file flag. 1228 * IN assertion: strstart is set to the end of the current match. 1229 */ 1230 #define FLUSH_BLOCK_ONLY(s, flush) { \ 1231 ct_flush_block(s, (s->block_start >= 0L ? \ 1232 (charf *)&s->window[(unsigned)s->block_start] : \ 1233 (charf *)Z_NULL), (long)s->strstart - s->block_start, (flush)); \ 1234 s->block_start = s->strstart; \ 1235 flush_pending(s->strm); \ 1236 Tracev((stderr,"[FLUSH]")); \ 1237 } 1238 1239 /* Same but force premature exit if necessary. */ 1240 #define FLUSH_BLOCK(s, flush) { \ 1241 FLUSH_BLOCK_ONLY(s, flush); \ 1242 if (s->strm->avail_out == 0) return 1; \ 1243 } 1244 1245 /* =========================================================================== 1246 * Compress as much as possible from the input stream, return true if 1247 * processing was terminated prematurely (no more input or output space). 1248 * This function does not perform lazy evaluationof matches and inserts 1249 * new strings in the dictionary only for unmatched strings or for short 1250 * matches. It is used only for the fast compression options. 1251 */ 1252 local int deflate_fast(s, flush) 1253 deflate_state *s; 1254 int flush; 1255 { 1256 IPos hash_head = NIL; /* head of the hash chain */ 1257 int bflush; /* set if current block must be flushed */ 1258 1259 s->prev_length = MIN_MATCH-1; 1260 1261 for (;;) { 1262 /* Make sure that we always have enough lookahead, except 1263 * at the end of the input file. We need MAX_MATCH bytes 1264 * for the next match, plus MIN_MATCH bytes to insert the 1265 * string following the next match. 1266 */ 1267 if (s->lookahead < MIN_LOOKAHEAD) { 1268 fill_window(s); 1269 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 1270 1271 if (s->lookahead == 0) break; /* flush the current block */ 1272 } 1273 1274 /* Insert the string window[strstart .. strstart+2] in the 1275 * dictionary, and set hash_head to the head of the hash chain: 1276 */ 1277 if (s->lookahead >= MIN_MATCH) { 1278 INSERT_STRING(s, s->strstart, hash_head); 1279 } 1280 1281 /* Find the longest match, discarding those <= prev_length. 1282 * At this point we have always match_length < MIN_MATCH 1283 */ 1284 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1285 /* To simplify the code, we prevent matches with the string 1286 * of window index 0 (in particular we have to avoid a match 1287 * of the string with itself at the start of the input file). 1288 */ 1289 if (s->strategy != Z_HUFFMAN_ONLY) { 1290 s->match_length = longest_match (s, hash_head); 1291 } 1292 /* longest_match() sets match_start */ 1293 1294 if (s->match_length > s->lookahead) s->match_length = s->lookahead; 1295 } 1296 if (s->match_length >= MIN_MATCH) { 1297 check_match(s, s->strstart, s->match_start, s->match_length); 1298 1299 bflush = ct_tally(s, s->strstart - s->match_start, 1300 s->match_length - MIN_MATCH); 1301 1302 s->lookahead -= s->match_length; 1303 1304 /* Insert new strings in the hash table only if the match length 1305 * is not too large. This saves time but degrades compression. 1306 */ 1307 if (s->match_length <= s->max_insert_length && 1308 s->lookahead >= MIN_MATCH) { 1309 s->match_length--; /* string at strstart already in hash table */ 1310 do { 1311 s->strstart++; 1312 INSERT_STRING(s, s->strstart, hash_head); 1313 /* strstart never exceeds WSIZE-MAX_MATCH, so there are 1314 * always MIN_MATCH bytes ahead. 1315 */ 1316 } while (--s->match_length != 0); 1317 s->strstart++; 1318 } else { 1319 s->strstart += s->match_length; 1320 s->match_length = 0; 1321 s->ins_h = s->window[s->strstart]; 1322 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1323 #if MIN_MATCH != 3 1324 Call UPDATE_HASH() MIN_MATCH-3 more times 1325 #endif 1326 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1327 * matter since it will be recomputed at next deflate call. 1328 */ 1329 } 1330 } else { 1331 /* No match, output a literal byte */ 1332 Tracevv((stderr,"%c", s->window[s->strstart])); 1333 bflush = ct_tally (s, 0, s->window[s->strstart]); 1334 s->lookahead--; 1335 s->strstart++; 1336 } 1337 if (bflush) FLUSH_BLOCK(s, Z_NO_FLUSH); 1338 } 1339 FLUSH_BLOCK(s, flush); 1340 return 0; /* normal exit */ 1341 } 1342 1343 /* =========================================================================== 1344 * Same as above, but achieves better compression. We use a lazy 1345 * evaluation for matches: a match is finally adopted only if there is 1346 * no better match at the next window position. 1347 */ 1348 local int deflate_slow(s, flush) 1349 deflate_state *s; 1350 int flush; 1351 { 1352 IPos hash_head = NIL; /* head of hash chain */ 1353 int bflush; /* set if current block must be flushed */ 1354 1355 /* Process the input block. */ 1356 for (;;) { 1357 /* Make sure that we always have enough lookahead, except 1358 * at the end of the input file. We need MAX_MATCH bytes 1359 * for the next match, plus MIN_MATCH bytes to insert the 1360 * string following the next match. 1361 */ 1362 if (s->lookahead < MIN_LOOKAHEAD) { 1363 fill_window(s); 1364 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 1365 1366 if (s->lookahead == 0) break; /* flush the current block */ 1367 } 1368 1369 /* Insert the string window[strstart .. strstart+2] in the 1370 * dictionary, and set hash_head to the head of the hash chain: 1371 */ 1372 if (s->lookahead >= MIN_MATCH) { 1373 INSERT_STRING(s, s->strstart, hash_head); 1374 } 1375 1376 /* Find the longest match, discarding those <= prev_length. 1377 */ 1378 s->prev_length = s->match_length, s->prev_match = s->match_start; 1379 s->match_length = MIN_MATCH-1; 1380 1381 if (hash_head != NIL && s->prev_length < s->max_lazy_match && 1382 s->strstart - hash_head <= MAX_DIST(s)) { 1383 /* To simplify the code, we prevent matches with the string 1384 * of window index 0 (in particular we have to avoid a match 1385 * of the string with itself at the start of the input file). 1386 */ 1387 if (s->strategy != Z_HUFFMAN_ONLY) { 1388 s->match_length = longest_match (s, hash_head); 1389 } 1390 /* longest_match() sets match_start */ 1391 if (s->match_length > s->lookahead) s->match_length = s->lookahead; 1392 1393 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 1394 (s->match_length == MIN_MATCH && 1395 s->strstart - s->match_start > TOO_FAR))) { 1396 1397 /* If prev_match is also MIN_MATCH, match_start is garbage 1398 * but we will ignore the current match anyway. 1399 */ 1400 s->match_length = MIN_MATCH-1; 1401 } 1402 } 1403 /* If there was a match at the previous step and the current 1404 * match is not better, output the previous match: 1405 */ 1406 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 1407 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 1408 /* Do not insert strings in hash table beyond this. */ 1409 1410 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1411 1412 bflush = ct_tally(s, s->strstart -1 - s->prev_match, 1413 s->prev_length - MIN_MATCH); 1414 1415 /* Insert in hash table all strings up to the end of the match. 1416 * strstart-1 and strstart are already inserted. If there is not 1417 * enough lookahead, the last two strings are not inserted in 1418 * the hash table. 1419 */ 1420 s->lookahead -= s->prev_length-1; 1421 s->prev_length -= 2; 1422 do { 1423 if (++s->strstart <= max_insert) { 1424 INSERT_STRING(s, s->strstart, hash_head); 1425 } 1426 } while (--s->prev_length != 0); 1427 s->match_available = 0; 1428 s->match_length = MIN_MATCH-1; 1429 s->strstart++; 1430 1431 if (bflush) FLUSH_BLOCK(s, Z_NO_FLUSH); 1432 1433 } else if (s->match_available) { 1434 /* If there was no match at the previous position, output a 1435 * single literal. If there was a match but the current match 1436 * is longer, truncate the previous match to a single literal. 1437 */ 1438 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1439 if (ct_tally (s, 0, s->window[s->strstart-1])) { 1440 FLUSH_BLOCK_ONLY(s, Z_NO_FLUSH); 1441 } 1442 s->strstart++; 1443 s->lookahead--; 1444 if (s->strm->avail_out == 0) return 1; 1445 } else { 1446 /* There is no previous match to compare with, wait for 1447 * the next step to decide. 1448 */ 1449 s->match_available = 1; 1450 s->strstart++; 1451 s->lookahead--; 1452 } 1453 } 1454 Assert (flush != Z_NO_FLUSH, "no flush?"); 1455 if (s->match_available) { 1456 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1457 ct_tally (s, 0, s->window[s->strstart-1]); 1458 s->match_available = 0; 1459 } 1460 FLUSH_BLOCK(s, flush); 1461 return 0; 1462 } 1463 1464 1465 /*+++++*/ 1466 /* trees.c -- output deflated data using Huffman coding 1467 * Copyright (C) 1995 Jean-loup Gailly 1468 * For conditions of distribution and use, see copyright notice in zlib.h 1469 */ 1470 1471 /* 1472 * ALGORITHM 1473 * 1474 * The "deflation" process uses several Huffman trees. The more 1475 * common source values are represented by shorter bit sequences. 1476 * 1477 * Each code tree is stored in a compressed form which is itself 1478 * a Huffman encoding of the lengths of all the code strings (in 1479 * ascending order by source values). The actual code strings are 1480 * reconstructed from the lengths in the inflate process, as described 1481 * in the deflate specification. 1482 * 1483 * REFERENCES 1484 * 1485 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". 1486 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc 1487 * 1488 * Storer, James A. 1489 * Data Compression: Methods and Theory, pp. 49-50. 1490 * Computer Science Press, 1988. ISBN 0-7167-8156-5. 1491 * 1492 * Sedgewick, R. 1493 * Algorithms, p290. 1494 * Addison-Wesley, 1983. ISBN 0-201-06672-6. 1495 */ 1496 1497 /* From: trees.c,v 1.5 1995/05/03 17:27:12 jloup Exp */ 1498 1499 #ifdef DEBUG_ZLIB 1500 # include <ctype.h> 1501 #endif 1502 1503 /* =========================================================================== 1504 * Constants 1505 */ 1506 1507 #define MAX_BL_BITS 7 1508 /* Bit length codes must not exceed MAX_BL_BITS bits */ 1509 1510 #define END_BLOCK 256 1511 /* end of block literal code */ 1512 1513 #define REP_3_6 16 1514 /* repeat previous bit length 3-6 times (2 bits of repeat count) */ 1515 1516 #define REPZ_3_10 17 1517 /* repeat a zero length 3-10 times (3 bits of repeat count) */ 1518 1519 #define REPZ_11_138 18 1520 /* repeat a zero length 11-138 times (7 bits of repeat count) */ 1521 1522 local int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 1523 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; 1524 1525 local int extra_dbits[D_CODES] /* extra bits for each distance code */ 1526 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; 1527 1528 local int extra_blbits[BL_CODES]/* extra bits for each bit length code */ 1529 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; 1530 1531 local uch bl_order[BL_CODES] 1532 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; 1533 /* The lengths of the bit length codes are sent in order of decreasing 1534 * probability, to avoid transmitting the lengths for unused bit length codes. 1535 */ 1536 1537 #define Buf_size (8 * 2*sizeof(char)) 1538 /* Number of bits used within bi_buf. (bi_buf might be implemented on 1539 * more than 16 bits on some systems.) 1540 */ 1541 1542 /* =========================================================================== 1543 * Local data. These are initialized only once. 1544 * To do: initialize at compile time to be completely reentrant. ??? 1545 */ 1546 1547 local ct_data static_ltree[L_CODES+2]; 1548 /* The static literal tree. Since the bit lengths are imposed, there is no 1549 * need for the L_CODES extra codes used during heap construction. However 1550 * The codes 286 and 287 are needed to build a canonical tree (see ct_init 1551 * below). 1552 */ 1553 1554 local ct_data static_dtree[D_CODES]; 1555 /* The static distance tree. (Actually a trivial tree since all codes use 1556 * 5 bits.) 1557 */ 1558 1559 local uch dist_code[512]; 1560 /* distance codes. The first 256 values correspond to the distances 1561 * 3 .. 258, the last 256 values correspond to the top 8 bits of 1562 * the 15 bit distances. 1563 */ 1564 1565 local uch length_code[MAX_MATCH-MIN_MATCH+1]; 1566 /* length code for each normalized match length (0 == MIN_MATCH) */ 1567 1568 local int base_length[LENGTH_CODES]; 1569 /* First normalized length for each code (0 = MIN_MATCH) */ 1570 1571 local int base_dist[D_CODES]; 1572 /* First normalized distance for each code (0 = distance of 1) */ 1573 1574 struct static_tree_desc_s { 1575 ct_data *static_tree; /* static tree or NULL */ 1576 intf *extra_bits; /* extra bits for each code or NULL */ 1577 int extra_base; /* base index for extra_bits */ 1578 int elems; /* max number of elements in the tree */ 1579 int max_length; /* max bit length for the codes */ 1580 }; 1581 1582 local static_tree_desc static_l_desc = 1583 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 1584 1585 local static_tree_desc static_d_desc = 1586 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 1587 1588 local static_tree_desc static_bl_desc = 1589 {(ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 1590 1591 /* =========================================================================== 1592 * Local (static) routines in this file. 1593 */ 1594 1595 local void ct_static_init OF((void)); 1596 local void init_block OF((deflate_state *s)); 1597 local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); 1598 local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); 1599 local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); 1600 local void build_tree OF((deflate_state *s, tree_desc *desc)); 1601 local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); 1602 local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); 1603 local int build_bl_tree OF((deflate_state *s)); 1604 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, 1605 int blcodes)); 1606 local void compress_block OF((deflate_state *s, ct_data *ltree, 1607 ct_data *dtree)); 1608 local void set_data_type OF((deflate_state *s)); 1609 local unsigned bi_reverse OF((unsigned value, int length)); 1610 local void bi_windup OF((deflate_state *s)); 1611 local void bi_flush OF((deflate_state *s)); 1612 local void copy_block OF((deflate_state *s, charf *buf, unsigned len, 1613 int header)); 1614 1615 #ifndef DEBUG_ZLIB 1616 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 1617 /* Send a code of the given tree. c and tree must not have side effects */ 1618 1619 #else /* DEBUG_ZLIB */ 1620 # define send_code(s, c, tree) \ 1621 { if (verbose>1) fprintf(stderr,"\ncd %3d ",(c)); \ 1622 send_bits(s, tree[c].Code, tree[c].Len); } 1623 #endif 1624 1625 #define d_code(dist) \ 1626 ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)]) 1627 /* Mapping from a distance to a distance code. dist is the distance - 1 and 1628 * must not have side effects. dist_code[256] and dist_code[257] are never 1629 * used. 1630 */ 1631 1632 /* =========================================================================== 1633 * Output a short LSB first on the stream. 1634 * IN assertion: there is enough room in pendingBuf. 1635 */ 1636 #define put_short(s, w) { \ 1637 put_byte(s, (uch)((w) & 0xff)); \ 1638 put_byte(s, (uch)((ush)(w) >> 8)); \ 1639 } 1640 1641 /* =========================================================================== 1642 * Send a value on a given number of bits. 1643 * IN assertion: length <= 16 and value fits in length bits. 1644 */ 1645 #ifdef DEBUG_ZLIB 1646 local void send_bits OF((deflate_state *s, int value, int length)); 1647 1648 local void send_bits(s, value, length) 1649 deflate_state *s; 1650 int value; /* value to send */ 1651 int length; /* number of bits */ 1652 { 1653 Tracev((stderr," l %2d v %4x ", length, value)); 1654 Assert(length > 0 && length <= 15, "invalid length"); 1655 s->bits_sent += (ulg)length; 1656 1657 /* If not enough room in bi_buf, use (valid) bits from bi_buf and 1658 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) 1659 * unused bits in value. 1660 */ 1661 if (s->bi_valid > (int)Buf_size - length) { 1662 s->bi_buf |= (value << s->bi_valid); 1663 put_short(s, s->bi_buf); 1664 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); 1665 s->bi_valid += length - Buf_size; 1666 } else { 1667 s->bi_buf |= value << s->bi_valid; 1668 s->bi_valid += length; 1669 } 1670 } 1671 #else /* !DEBUG_ZLIB */ 1672 1673 #define send_bits(s, value, length) \ 1674 { int len = length;\ 1675 if (s->bi_valid > (int)Buf_size - len) {\ 1676 int val = value;\ 1677 s->bi_buf |= (val << s->bi_valid);\ 1678 put_short(s, s->bi_buf);\ 1679 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ 1680 s->bi_valid += len - Buf_size;\ 1681 } else {\ 1682 s->bi_buf |= (value) << s->bi_valid;\ 1683 s->bi_valid += len;\ 1684 }\ 1685 } 1686 #endif /* DEBUG_ZLIB */ 1687 1688 1689 #define MAX(a,b) (a >= b ? a : b) 1690 /* the arguments must not have side effects */ 1691 1692 /* =========================================================================== 1693 * Initialize the various 'constant' tables. 1694 * To do: do this at compile time. 1695 */ 1696 local void ct_static_init() 1697 { 1698 int n; /* iterates over tree elements */ 1699 int bits; /* bit counter */ 1700 int length; /* length value */ 1701 int code; /* code value */ 1702 int dist; /* distance index */ 1703 ush bl_count[MAX_BITS+1]; 1704 /* number of codes at each bit length for an optimal tree */ 1705 1706 /* Initialize the mapping length (0..255) -> length code (0..28) */ 1707 length = 0; 1708 for (code = 0; code < LENGTH_CODES-1; code++) { 1709 base_length[code] = length; 1710 for (n = 0; n < (1<<extra_lbits[code]); n++) { 1711 length_code[length++] = (uch)code; 1712 } 1713 } 1714 Assert (length == 256, "ct_static_init: length != 256"); 1715 /* Note that the length 255 (match length 258) can be represented 1716 * in two different ways: code 284 + 5 bits or code 285, so we 1717 * overwrite length_code[255] to use the best encoding: 1718 */ 1719 length_code[length-1] = (uch)code; 1720 1721 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ 1722 dist = 0; 1723 for (code = 0 ; code < 16; code++) { 1724 base_dist[code] = dist; 1725 for (n = 0; n < (1<<extra_dbits[code]); n++) { 1726 dist_code[dist++] = (uch)code; 1727 } 1728 } 1729 Assert (dist == 256, "ct_static_init: dist != 256"); 1730 dist >>= 7; /* from now on, all distances are divided by 128 */ 1731 for ( ; code < D_CODES; code++) { 1732 base_dist[code] = dist << 7; 1733 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { 1734 dist_code[256 + dist++] = (uch)code; 1735 } 1736 } 1737 Assert (dist == 256, "ct_static_init: 256+dist != 512"); 1738 1739 /* Construct the codes of the static literal tree */ 1740 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 1741 n = 0; 1742 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; 1743 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; 1744 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; 1745 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; 1746 /* Codes 286 and 287 do not exist, but we must include them in the 1747 * tree construction to get a canonical Huffman tree (longest code 1748 * all ones) 1749 */ 1750 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); 1751 1752 /* The static distance tree is trivial: */ 1753 for (n = 0; n < D_CODES; n++) { 1754 static_dtree[n].Len = 5; 1755 static_dtree[n].Code = bi_reverse(n, 5); 1756 } 1757 } 1758 1759 /* =========================================================================== 1760 * Initialize the tree data structures for a new zlib stream. 1761 */ 1762 local void ct_init(s) 1763 deflate_state *s; 1764 { 1765 if (static_dtree[0].Len == 0) { 1766 ct_static_init(); /* To do: at compile time */ 1767 } 1768 1769 s->compressed_len = 0L; 1770 1771 s->l_desc.dyn_tree = s->dyn_ltree; 1772 s->l_desc.stat_desc = &static_l_desc; 1773 1774 s->d_desc.dyn_tree = s->dyn_dtree; 1775 s->d_desc.stat_desc = &static_d_desc; 1776 1777 s->bl_desc.dyn_tree = s->bl_tree; 1778 s->bl_desc.stat_desc = &static_bl_desc; 1779 1780 s->bi_buf = 0; 1781 s->bi_valid = 0; 1782 s->last_eob_len = 8; /* enough lookahead for inflate */ 1783 #ifdef DEBUG_ZLIB 1784 s->bits_sent = 0L; 1785 #endif 1786 s->blocks_in_packet = 0; 1787 1788 /* Initialize the first block of the first file: */ 1789 init_block(s); 1790 } 1791 1792 /* =========================================================================== 1793 * Initialize a new block. 1794 */ 1795 local void init_block(s) 1796 deflate_state *s; 1797 { 1798 int n; /* iterates over tree elements */ 1799 1800 /* Initialize the trees. */ 1801 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 1802 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 1803 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 1804 1805 s->dyn_ltree[END_BLOCK].Freq = 1; 1806 s->opt_len = s->static_len = 0L; 1807 s->last_lit = s->matches = 0; 1808 } 1809 1810 #define SMALLEST 1 1811 /* Index within the heap array of least frequent node in the Huffman tree */ 1812 1813 1814 /* =========================================================================== 1815 * Remove the smallest element from the heap and recreate the heap with 1816 * one less element. Updates heap and heap_len. 1817 */ 1818 #define pqremove(s, tree, top) \ 1819 {\ 1820 top = s->heap[SMALLEST]; \ 1821 s->heap[SMALLEST] = s->heap[s->heap_len--]; \ 1822 pqdownheap(s, tree, SMALLEST); \ 1823 } 1824 1825 /* =========================================================================== 1826 * Compares to subtrees, using the tree depth as tie breaker when 1827 * the subtrees have equal frequency. This minimizes the worst case length. 1828 */ 1829 #define smaller(tree, n, m, depth) \ 1830 (tree[n].Freq < tree[m].Freq || \ 1831 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 1832 1833 /* =========================================================================== 1834 * Restore the heap property by moving down the tree starting at node k, 1835 * exchanging a node with the smallest of its two sons if necessary, stopping 1836 * when the heap property is re-established (each father smaller than its 1837 * two sons). 1838 */ 1839 local void pqdownheap(s, tree, k) 1840 deflate_state *s; 1841 ct_data *tree; /* the tree to restore */ 1842 int k; /* node to move down */ 1843 { 1844 int v = s->heap[k]; 1845 int j = k << 1; /* left son of k */ 1846 while (j <= s->heap_len) { 1847 /* Set j to the smallest of the two sons: */ 1848 if (j < s->heap_len && 1849 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { 1850 j++; 1851 } 1852 /* Exit if v is smaller than both sons */ 1853 if (smaller(tree, v, s->heap[j], s->depth)) break; 1854 1855 /* Exchange v with the smallest son */ 1856 s->heap[k] = s->heap[j]; k = j; 1857 1858 /* And continue down the tree, setting j to the left son of k */ 1859 j <<= 1; 1860 } 1861 s->heap[k] = v; 1862 } 1863 1864 /* =========================================================================== 1865 * Compute the optimal bit lengths for a tree and update the total bit length 1866 * for the current block. 1867 * IN assertion: the fields freq and dad are set, heap[heap_max] and 1868 * above are the tree nodes sorted by increasing frequency. 1869 * OUT assertions: the field len is set to the optimal bit length, the 1870 * array bl_count contains the frequencies for each bit length. 1871 * The length opt_len is updated; static_len is also updated if stree is 1872 * not null. 1873 */ 1874 local void gen_bitlen(s, desc) 1875 deflate_state *s; 1876 tree_desc *desc; /* the tree descriptor */ 1877 { 1878 ct_data *tree = desc->dyn_tree; 1879 int max_code = desc->max_code; 1880 ct_data *stree = desc->stat_desc->static_tree; 1881 intf *extra = desc->stat_desc->extra_bits; 1882 int base = desc->stat_desc->extra_base; 1883 int max_length = desc->stat_desc->max_length; 1884 int h; /* heap index */ 1885 int n, m; /* iterate over the tree elements */ 1886 int bits; /* bit length */ 1887 int xbits; /* extra bits */ 1888 ush f; /* frequency */ 1889 int overflow = 0; /* number of elements with bit length too large */ 1890 1891 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; 1892 1893 /* In a first pass, compute the optimal bit lengths (which may 1894 * overflow in the case of the bit length tree). 1895 */ 1896 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ 1897 1898 for (h = s->heap_max+1; h < HEAP_SIZE; h++) { 1899 n = s->heap[h]; 1900 bits = tree[tree[n].Dad].Len + 1; 1901 if (bits > max_length) bits = max_length, overflow++; 1902 tree[n].Len = (ush)bits; 1903 /* We overwrite tree[n].Dad which is no longer needed */ 1904 1905 if (n > max_code) continue; /* not a leaf node */ 1906 1907 s->bl_count[bits]++; 1908 xbits = 0; 1909 if (n >= base) xbits = extra[n-base]; 1910 f = tree[n].Freq; 1911 s->opt_len += (ulg)f * (bits + xbits); 1912 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); 1913 } 1914 if (overflow == 0) return; 1915 1916 Trace((stderr,"\nbit length overflow\n")); 1917 /* This happens for example on obj2 and pic of the Calgary corpus */ 1918 1919 /* Find the first bit length which could increase: */ 1920 do { 1921 bits = max_length-1; 1922 while (s->bl_count[bits] == 0) bits--; 1923 s->bl_count[bits]--; /* move one leaf down the tree */ 1924 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ 1925 s->bl_count[max_length]--; 1926 /* The brother of the overflow item also moves one step up, 1927 * but this does not affect bl_count[max_length] 1928 */ 1929 overflow -= 2; 1930 } while (overflow > 0); 1931 1932 /* Now recompute all bit lengths, scanning in increasing frequency. 1933 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all 1934 * lengths instead of fixing only the wrong ones. This idea is taken 1935 * from 'ar' written by Haruhiko Okumura.) 1936 */ 1937 for (bits = max_length; bits != 0; bits--) { 1938 n = s->bl_count[bits]; 1939 while (n != 0) { 1940 m = s->heap[--h]; 1941 if (m > max_code) continue; 1942 if (tree[m].Len != (unsigned) bits) { 1943 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); 1944 s->opt_len += ((long)bits - (long)tree[m].Len) 1945 *(long)tree[m].Freq; 1946 tree[m].Len = (ush)bits; 1947 } 1948 n--; 1949 } 1950 } 1951 } 1952 1953 /* =========================================================================== 1954 * Generate the codes for a given tree and bit counts (which need not be 1955 * optimal). 1956 * IN assertion: the array bl_count contains the bit length statistics for 1957 * the given tree and the field len is set for all tree elements. 1958 * OUT assertion: the field code is set for all tree elements of non 1959 * zero code length. 1960 */ 1961 local void gen_codes (tree, max_code, bl_count) 1962 ct_data *tree; /* the tree to decorate */ 1963 int max_code; /* largest code with non zero frequency */ 1964 ushf *bl_count; /* number of codes at each bit length */ 1965 { 1966 ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 1967 ush code = 0; /* running code value */ 1968 int bits; /* bit index */ 1969 int n; /* code index */ 1970 1971 /* The distribution counts are first used to generate the code values 1972 * without bit reversal. 1973 */ 1974 for (bits = 1; bits <= MAX_BITS; bits++) { 1975 next_code[bits] = code = (code + bl_count[bits-1]) << 1; 1976 } 1977 /* Check that the bit counts in bl_count are consistent. The last code 1978 * must be all ones. 1979 */ 1980 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, 1981 "inconsistent bit counts"); 1982 Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 1983 1984 for (n = 0; n <= max_code; n++) { 1985 int len = tree[n].Len; 1986 if (len == 0) continue; 1987 /* Now reverse the bits */ 1988 tree[n].Code = bi_reverse(next_code[len]++, len); 1989 1990 Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 1991 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); 1992 } 1993 } 1994 1995 /* =========================================================================== 1996 * Construct one Huffman tree and assigns the code bit strings and lengths. 1997 * Update the total bit length for the current block. 1998 * IN assertion: the field freq is set for all tree elements. 1999 * OUT assertions: the fields len and code are set to the optimal bit length 2000 * and corresponding code. The length opt_len is updated; static_len is 2001 * also updated if stree is not null. The field max_code is set. 2002 */ 2003 local void build_tree(s, desc) 2004 deflate_state *s; 2005 tree_desc *desc; /* the tree descriptor */ 2006 { 2007 ct_data *tree = desc->dyn_tree; 2008 ct_data *stree = desc->stat_desc->static_tree; 2009 int elems = desc->stat_desc->elems; 2010 int n, m; /* iterate over heap elements */ 2011 int max_code = -1; /* largest code with non zero frequency */ 2012 int node; /* new node being created */ 2013 2014 /* Construct the initial heap, with least frequent element in 2015 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. 2016 * heap[0] is not used. 2017 */ 2018 s->heap_len = 0, s->heap_max = HEAP_SIZE; 2019 2020 for (n = 0; n < elems; n++) { 2021 if (tree[n].Freq != 0) { 2022 s->heap[++(s->heap_len)] = max_code = n; 2023 s->depth[n] = 0; 2024 } else { 2025 tree[n].Len = 0; 2026 } 2027 } 2028 2029 /* The pkzip format requires that at least one distance code exists, 2030 * and that at least one bit should be sent even if there is only one 2031 * possible code. So to avoid special checks later on we force at least 2032 * two codes of non zero frequency. 2033 */ 2034 while (s->heap_len < 2) { 2035 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); 2036 tree[node].Freq = 1; 2037 s->depth[node] = 0; 2038 s->opt_len--; if (stree) s->static_len -= stree[node].Len; 2039 /* node is 0 or 1 so it does not have extra bits */ 2040 } 2041 desc->max_code = max_code; 2042 2043 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, 2044 * establish sub-heaps of increasing lengths: 2045 */ 2046 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); 2047 2048 /* Construct the Huffman tree by repeatedly combining the least two 2049 * frequent nodes. 2050 */ 2051 node = elems; /* next internal node of the tree */ 2052 do { 2053 pqremove(s, tree, n); /* n = node of least frequency */ 2054 m = s->heap[SMALLEST]; /* m = node of next least frequency */ 2055 2056 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ 2057 s->heap[--(s->heap_max)] = m; 2058 2059 /* Create a new node father of n and m */ 2060 tree[node].Freq = tree[n].Freq + tree[m].Freq; 2061 s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1); 2062 tree[n].Dad = tree[m].Dad = (ush)node; 2063 #ifdef DUMP_BL_TREE 2064 if (tree == s->bl_tree) { 2065 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", 2066 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 2067 } 2068 #endif 2069 /* and insert the new node in the heap */ 2070 s->heap[SMALLEST] = node++; 2071 pqdownheap(s, tree, SMALLEST); 2072 2073 } while (s->heap_len >= 2); 2074 2075 s->heap[--(s->heap_max)] = s->heap[SMALLEST]; 2076 2077 /* At this point, the fields freq and dad are set. We can now 2078 * generate the bit lengths. 2079 */ 2080 gen_bitlen(s, (tree_desc *)desc); 2081 2082 /* The field len is now set, we can generate the bit codes */ 2083 gen_codes ((ct_data *)tree, max_code, s->bl_count); 2084 } 2085 2086 /* =========================================================================== 2087 * Scan a literal or distance tree to determine the frequencies of the codes 2088 * in the bit length tree. 2089 */ 2090 local void scan_tree (s, tree, max_code) 2091 deflate_state *s; 2092 ct_data *tree; /* the tree to be scanned */ 2093 int max_code; /* and its largest code of non zero frequency */ 2094 { 2095 int n; /* iterates over all tree elements */ 2096 int prevlen = -1; /* last emitted length */ 2097 int curlen; /* length of current code */ 2098 int nextlen = tree[0].Len; /* length of next code */ 2099 int count = 0; /* repeat count of the current code */ 2100 int max_count = 7; /* max repeat count */ 2101 int min_count = 4; /* min repeat count */ 2102 2103 if (nextlen == 0) max_count = 138, min_count = 3; 2104 tree[max_code+1].Len = (ush)0xffff; /* guard */ 2105 2106 for (n = 0; n <= max_code; n++) { 2107 curlen = nextlen; nextlen = tree[n+1].Len; 2108 if (++count < max_count && curlen == nextlen) { 2109 continue; 2110 } else if (count < min_count) { 2111 s->bl_tree[curlen].Freq += count; 2112 } else if (curlen != 0) { 2113 if (curlen != prevlen) s->bl_tree[curlen].Freq++; 2114 s->bl_tree[REP_3_6].Freq++; 2115 } else if (count <= 10) { 2116 s->bl_tree[REPZ_3_10].Freq++; 2117 } else { 2118 s->bl_tree[REPZ_11_138].Freq++; 2119 } 2120 count = 0; prevlen = curlen; 2121 if (nextlen == 0) { 2122 max_count = 138, min_count = 3; 2123 } else if (curlen == nextlen) { 2124 max_count = 6, min_count = 3; 2125 } else { 2126 max_count = 7, min_count = 4; 2127 } 2128 } 2129 } 2130 2131 /* =========================================================================== 2132 * Send a literal or distance tree in compressed form, using the codes in 2133 * bl_tree. 2134 */ 2135 local void send_tree (s, tree, max_code) 2136 deflate_state *s; 2137 ct_data *tree; /* the tree to be scanned */ 2138 int max_code; /* and its largest code of non zero frequency */ 2139 { 2140 int n; /* iterates over all tree elements */ 2141 int prevlen = -1; /* last emitted length */ 2142 int curlen; /* length of current code */ 2143 int nextlen = tree[0].Len; /* length of next code */ 2144 int count = 0; /* repeat count of the current code */ 2145 int max_count = 7; /* max repeat count */ 2146 int min_count = 4; /* min repeat count */ 2147 2148 /* tree[max_code+1].Len = -1; */ /* guard already set */ 2149 if (nextlen == 0) max_count = 138, min_count = 3; 2150 2151 for (n = 0; n <= max_code; n++) { 2152 curlen = nextlen; nextlen = tree[n+1].Len; 2153 if (++count < max_count && curlen == nextlen) { 2154 continue; 2155 } else if (count < min_count) { 2156 do { send_code(s, curlen, s->bl_tree); } while (--count != 0); 2157 2158 } else if (curlen != 0) { 2159 if (curlen != prevlen) { 2160 send_code(s, curlen, s->bl_tree); count--; 2161 } 2162 Assert(count >= 3 && count <= 6, " 3_6?"); 2163 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); 2164 2165 } else if (count <= 10) { 2166 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); 2167 2168 } else { 2169 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); 2170 } 2171 count = 0; prevlen = curlen; 2172 if (nextlen == 0) { 2173 max_count = 138, min_count = 3; 2174 } else if (curlen == nextlen) { 2175 max_count = 6, min_count = 3; 2176 } else { 2177 max_count = 7, min_count = 4; 2178 } 2179 } 2180 } 2181 2182 /* =========================================================================== 2183 * Construct the Huffman tree for the bit lengths and return the index in 2184 * bl_order of the last bit length code to send. 2185 */ 2186 local int build_bl_tree(s) 2187 deflate_state *s; 2188 { 2189 int max_blindex; /* index of last bit length code of non zero freq */ 2190 2191 /* Determine the bit length frequencies for literal and distance trees */ 2192 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 2193 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 2194 2195 /* Build the bit length tree: */ 2196 build_tree(s, (tree_desc *)(&(s->bl_desc))); 2197 /* opt_len now includes the length of the tree representations, except 2198 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 2199 */ 2200 2201 /* Determine the number of bit length codes to send. The pkzip format 2202 * requires that at least 4 bit length codes be sent. (appnote.txt says 2203 * 3 but the actual value used is 4.) 2204 */ 2205 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 2206 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; 2207 } 2208 /* Update opt_len to include the bit length tree and counts */ 2209 s->opt_len += 3*(max_blindex+1) + 5+5+4; 2210 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 2211 s->opt_len, s->static_len)); 2212 2213 return max_blindex; 2214 } 2215 2216 /* =========================================================================== 2217 * Send the header for a block using dynamic Huffman trees: the counts, the 2218 * lengths of the bit length codes, the literal tree and the distance tree. 2219 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 2220 */ 2221 local void send_all_trees(s, lcodes, dcodes, blcodes) 2222 deflate_state *s; 2223 int lcodes, dcodes, blcodes; /* number of codes for each tree */ 2224 { 2225 int rank; /* index in bl_order */ 2226 2227 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 2228 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 2229 "too many codes"); 2230 Tracev((stderr, "\nbl counts: ")); 2231 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ 2232 send_bits(s, dcodes-1, 5); 2233 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ 2234 for (rank = 0; rank < blcodes; rank++) { 2235 Tracev((stderr, "\nbl code %2d ", bl_order[rank])); 2236 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); 2237 } 2238 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); 2239 2240 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ 2241 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); 2242 2243 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ 2244 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 2245 } 2246 2247 /* =========================================================================== 2248 * Send a stored block 2249 */ 2250 local void ct_stored_block(s, buf, stored_len, eof) 2251 deflate_state *s; 2252 charf *buf; /* input block */ 2253 ulg stored_len; /* length of input block */ 2254 int eof; /* true if this is the last block for a file */ 2255 { 2256 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */ 2257 s->compressed_len = (s->compressed_len + 3 + 7) & ~7L; 2258 s->compressed_len += (stored_len + 4) << 3; 2259 2260 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 2261 } 2262 2263 /* Send just the `stored block' type code without any length bytes or data. 2264 */ 2265 local void ct_stored_type_only(s) 2266 deflate_state *s; 2267 { 2268 send_bits(s, (STORED_BLOCK << 1), 3); 2269 bi_windup(s); 2270 s->compressed_len = (s->compressed_len + 3) & ~7L; 2271 } 2272 2273 2274 /* =========================================================================== 2275 * Send one empty static block to give enough lookahead for inflate. 2276 * This takes 10 bits, of which 7 may remain in the bit buffer. 2277 * The current inflate code requires 9 bits of lookahead. If the EOB 2278 * code for the previous block was coded on 5 bits or less, inflate 2279 * may have only 5+3 bits of lookahead to decode this EOB. 2280 * (There are no problems if the previous block is stored or fixed.) 2281 */ 2282 local void ct_align(s) 2283 deflate_state *s; 2284 { 2285 send_bits(s, STATIC_TREES<<1, 3); 2286 send_code(s, END_BLOCK, static_ltree); 2287 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 2288 bi_flush(s); 2289 /* Of the 10 bits for the empty block, we have already sent 2290 * (10 - bi_valid) bits. The lookahead for the EOB of the previous 2291 * block was thus its length plus what we have just sent. 2292 */ 2293 if (s->last_eob_len + 10 - s->bi_valid < 9) { 2294 send_bits(s, STATIC_TREES<<1, 3); 2295 send_code(s, END_BLOCK, static_ltree); 2296 s->compressed_len += 10L; 2297 bi_flush(s); 2298 } 2299 s->last_eob_len = 7; 2300 } 2301 2302 /* =========================================================================== 2303 * Determine the best encoding for the current block: dynamic trees, static 2304 * trees or store, and output the encoded block to the zip file. This function 2305 * returns the total compressed length for the file so far. 2306 */ 2307 local ulg ct_flush_block(s, buf, stored_len, flush) 2308 deflate_state *s; 2309 charf *buf; /* input block, or NULL if too old */ 2310 ulg stored_len; /* length of input block */ 2311 int flush; /* Z_FINISH if this is the last block for a file */ 2312 { 2313 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 2314 int max_blindex; /* index of last bit length code of non zero freq */ 2315 int eof = flush == Z_FINISH; 2316 2317 ++s->blocks_in_packet; 2318 2319 /* Check if the file is ascii or binary */ 2320 if (s->data_type == UNKNOWN) set_data_type(s); 2321 2322 /* Construct the literal and distance trees */ 2323 build_tree(s, (tree_desc *)(&(s->l_desc))); 2324 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, 2325 s->static_len)); 2326 2327 build_tree(s, (tree_desc *)(&(s->d_desc))); 2328 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 2329 s->static_len)); 2330 /* At this point, opt_len and static_len are the total bit lengths of 2331 * the compressed block data, excluding the tree representations. 2332 */ 2333 2334 /* Build the bit length tree for the above two trees, and get the index 2335 * in bl_order of the last bit length code to send. 2336 */ 2337 max_blindex = build_bl_tree(s); 2338 2339 /* Determine the best encoding. Compute first the block length in bytes */ 2340 opt_lenb = (s->opt_len+3+7)>>3; 2341 static_lenb = (s->static_len+3+7)>>3; 2342 2343 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 2344 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 2345 s->last_lit)); 2346 2347 if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 2348 2349 /* If compression failed and this is the first and last block, 2350 * and if the .zip file can be seeked (to rewrite the local header), 2351 * the whole file is transformed into a stored file: 2352 */ 2353 #ifdef STORED_FILE_OK 2354 # ifdef FORCE_STORED_FILE 2355 if (eof && compressed_len == 0L) /* force stored file */ 2356 # else 2357 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) 2358 # endif 2359 { 2360 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ 2361 if (buf == (charf*)0) error ("block vanished"); 2362 2363 copy_block(buf, (unsigned)stored_len, 0); /* without header */ 2364 s->compressed_len = stored_len << 3; 2365 s->method = STORED; 2366 } else 2367 #endif /* STORED_FILE_OK */ 2368 2369 /* For Z_PACKET_FLUSH, if we don't achieve the required minimum 2370 * compression, and this block contains all the data since the last 2371 * time we used Z_PACKET_FLUSH, then just omit this block completely 2372 * from the output. 2373 */ 2374 if (flush == Z_PACKET_FLUSH && s->blocks_in_packet == 1 2375 && opt_lenb > stored_len - s->minCompr) { 2376 s->blocks_in_packet = 0; 2377 /* output nothing */ 2378 } else 2379 2380 #ifdef FORCE_STORED 2381 if (buf != (char*)0) /* force stored block */ 2382 #else 2383 if (stored_len+4 <= opt_lenb && buf != (char*)0) 2384 /* 4: two words for the lengths */ 2385 #endif 2386 { 2387 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 2388 * Otherwise we can't have processed more than WSIZE input bytes since 2389 * the last block flush, because compression would have been 2390 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 2391 * transform a block into a stored block. 2392 */ 2393 ct_stored_block(s, buf, stored_len, eof); 2394 } else 2395 2396 #ifdef FORCE_STATIC 2397 if (static_lenb >= 0) /* force static trees */ 2398 #else 2399 if (static_lenb == opt_lenb) 2400 #endif 2401 { 2402 send_bits(s, (STATIC_TREES<<1)+eof, 3); 2403 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree); 2404 s->compressed_len += 3 + s->static_len; 2405 } else { 2406 send_bits(s, (DYN_TREES<<1)+eof, 3); 2407 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, 2408 max_blindex+1); 2409 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree); 2410 s->compressed_len += 3 + s->opt_len; 2411 } 2412 Assert (s->compressed_len == s->bits_sent, "bad compressed size"); 2413 init_block(s); 2414 2415 if (eof) { 2416 bi_windup(s); 2417 s->compressed_len += 7; /* align on byte boundary */ 2418 } 2419 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, 2420 s->compressed_len-7*eof)); 2421 2422 return s->compressed_len >> 3; 2423 } 2424 2425 /* =========================================================================== 2426 * Save the match info and tally the frequency counts. Return true if 2427 * the current block must be flushed. 2428 */ 2429 local int ct_tally (s, dist, lc) 2430 deflate_state *s; 2431 int dist; /* distance of matched string */ 2432 int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ 2433 { 2434 s->d_buf[s->last_lit] = (ush)dist; 2435 s->l_buf[s->last_lit++] = (uch)lc; 2436 if (dist == 0) { 2437 /* lc is the unmatched char */ 2438 s->dyn_ltree[lc].Freq++; 2439 } else { 2440 s->matches++; 2441 /* Here, lc is the match length - MIN_MATCH */ 2442 dist--; /* dist = match distance - 1 */ 2443 Assert((ush)dist < (ush)MAX_DIST(s) && 2444 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 2445 (ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match"); 2446 2447 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++; 2448 s->dyn_dtree[d_code(dist)].Freq++; 2449 } 2450 2451 /* Try to guess if it is profitable to stop the current block here */ 2452 if (s->level > 2 && (s->last_lit & 0xfff) == 0) { 2453 /* Compute an upper bound for the compressed length */ 2454 ulg out_length = (ulg)s->last_lit*8L; 2455 ulg in_length = (ulg)s->strstart - s->block_start; 2456 int dcode; 2457 for (dcode = 0; dcode < D_CODES; dcode++) { 2458 out_length += (ulg)s->dyn_dtree[dcode].Freq * 2459 (5L+extra_dbits[dcode]); 2460 } 2461 out_length >>= 3; 2462 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", 2463 s->last_lit, in_length, out_length, 2464 100L - out_length*100L/in_length)); 2465 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; 2466 } 2467 return (s->last_lit == s->lit_bufsize-1); 2468 /* We avoid equality with lit_bufsize because of wraparound at 64K 2469 * on 16 bit machines and because stored blocks are restricted to 2470 * 64K-1 bytes. 2471 */ 2472 } 2473 2474 /* =========================================================================== 2475 * Send the block data compressed using the given Huffman trees 2476 */ 2477 local void compress_block(s, ltree, dtree) 2478 deflate_state *s; 2479 ct_data *ltree; /* literal tree */ 2480 ct_data *dtree; /* distance tree */ 2481 { 2482 unsigned dist; /* distance of matched string */ 2483 int lc; /* match length or unmatched char (if dist == 0) */ 2484 unsigned lx = 0; /* running index in l_buf */ 2485 unsigned code; /* the code to send */ 2486 int extra; /* number of extra bits to send */ 2487 2488 if (s->last_lit != 0) do { 2489 dist = s->d_buf[lx]; 2490 lc = s->l_buf[lx++]; 2491 if (dist == 0) { 2492 send_code(s, lc, ltree); /* send a literal byte */ 2493 Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 2494 } else { 2495 /* Here, lc is the match length - MIN_MATCH */ 2496 code = length_code[lc]; 2497 send_code(s, code+LITERALS+1, ltree); /* send the length code */ 2498 extra = extra_lbits[code]; 2499 if (extra != 0) { 2500 lc -= base_length[code]; 2501 send_bits(s, lc, extra); /* send the extra length bits */ 2502 } 2503 dist--; /* dist is now the match distance - 1 */ 2504 code = d_code(dist); 2505 Assert (code < D_CODES, "bad d_code"); 2506 2507 send_code(s, code, dtree); /* send the distance code */ 2508 extra = extra_dbits[code]; 2509 if (extra != 0) { 2510 dist -= base_dist[code]; 2511 send_bits(s, dist, extra); /* send the extra distance bits */ 2512 } 2513 } /* literal or match pair ? */ 2514 2515 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 2516 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow"); 2517 2518 } while (lx < s->last_lit); 2519 2520 send_code(s, END_BLOCK, ltree); 2521 s->last_eob_len = ltree[END_BLOCK].Len; 2522 } 2523 2524 /* =========================================================================== 2525 * Set the data type to ASCII or Z_BINARY, using a crude approximation: 2526 * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise. 2527 * IN assertion: the fields freq of dyn_ltree are set and the total of all 2528 * frequencies does not exceed 64K (to fit in an int on 16 bit machines). 2529 */ 2530 local void set_data_type(s) 2531 deflate_state *s; 2532 { 2533 int n = 0; 2534 unsigned ascii_freq = 0; 2535 unsigned bin_freq = 0; 2536 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq; 2537 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq; 2538 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq; 2539 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : ASCII); 2540 } 2541 2542 /* =========================================================================== 2543 * Reverse the first len bits of a code, using straightforward code (a faster 2544 * method would use a table) 2545 * IN assertion: 1 <= len <= 15 2546 */ 2547 local unsigned bi_reverse(code, len) 2548 unsigned code; /* the value to invert */ 2549 int len; /* its bit length */ 2550 { 2551 register unsigned res = 0; 2552 do { 2553 res |= code & 1; 2554 code >>= 1, res <<= 1; 2555 } while (--len > 0); 2556 return res >> 1; 2557 } 2558 2559 /* =========================================================================== 2560 * Flush the bit buffer, keeping at most 7 bits in it. 2561 */ 2562 local void bi_flush(s) 2563 deflate_state *s; 2564 { 2565 if (s->bi_valid == 16) { 2566 put_short(s, s->bi_buf); 2567 s->bi_buf = 0; 2568 s->bi_valid = 0; 2569 } else if (s->bi_valid >= 8) { 2570 put_byte(s, (Byte)s->bi_buf); 2571 s->bi_buf >>= 8; 2572 s->bi_valid -= 8; 2573 } 2574 } 2575 2576 /* =========================================================================== 2577 * Flush the bit buffer and align the output on a byte boundary 2578 */ 2579 local void bi_windup(s) 2580 deflate_state *s; 2581 { 2582 if (s->bi_valid > 8) { 2583 put_short(s, s->bi_buf); 2584 } else if (s->bi_valid > 0) { 2585 put_byte(s, (Byte)s->bi_buf); 2586 } 2587 s->bi_buf = 0; 2588 s->bi_valid = 0; 2589 #ifdef DEBUG_ZLIB 2590 s->bits_sent = (s->bits_sent+7) & ~7; 2591 #endif 2592 } 2593 2594 /* =========================================================================== 2595 * Copy a stored block, storing first the length and its 2596 * one's complement if requested. 2597 */ 2598 local void copy_block(s, buf, len, header) 2599 deflate_state *s; 2600 charf *buf; /* the input data */ 2601 unsigned len; /* its length */ 2602 int header; /* true if block header must be written */ 2603 { 2604 bi_windup(s); /* align on byte boundary */ 2605 s->last_eob_len = 8; /* enough lookahead for inflate */ 2606 2607 if (header) { 2608 put_short(s, (ush)len); 2609 put_short(s, (ush)~len); 2610 #ifdef DEBUG_ZLIB 2611 s->bits_sent += 2*16; 2612 #endif 2613 } 2614 #ifdef DEBUG_ZLIB 2615 s->bits_sent += (ulg)len<<3; 2616 #endif 2617 while (len--) { 2618 put_byte(s, *buf++); 2619 } 2620 } 2621 2622 2623 /*+++++*/ 2624 /* infblock.h -- header to use infblock.c 2625 * Copyright (C) 1995 Mark Adler 2626 * For conditions of distribution and use, see copyright notice in zlib.h 2627 */ 2628 2629 /* WARNING: this file should *not* be used by applications. It is 2630 part of the implementation of the compression library and is 2631 subject to change. Applications should only use zlib.h. 2632 */ 2633 2634 struct inflate_blocks_state; 2635 typedef struct inflate_blocks_state FAR inflate_blocks_statef; 2636 2637 local inflate_blocks_statef * inflate_blocks_new OF(( 2638 z_stream *z, 2639 check_func c, /* check function */ 2640 uInt w)); /* window size */ 2641 2642 local int inflate_blocks OF(( 2643 inflate_blocks_statef *, 2644 z_stream *, 2645 int)); /* initial return code */ 2646 2647 local void inflate_blocks_reset OF(( 2648 inflate_blocks_statef *, 2649 z_stream *, 2650 uLongf *)); /* check value on output */ 2651 2652 local int inflate_blocks_free OF(( 2653 inflate_blocks_statef *, 2654 z_stream *, 2655 uLongf *)); /* check value on output */ 2656 2657 local int inflate_addhistory OF(( 2658 inflate_blocks_statef *, 2659 z_stream *)); 2660 2661 local int inflate_packet_flush OF(( 2662 inflate_blocks_statef *)); 2663 2664 /*+++++*/ 2665 /* inftrees.h -- header to use inftrees.c 2666 * Copyright (C) 1995 Mark Adler 2667 * For conditions of distribution and use, see copyright notice in zlib.h 2668 */ 2669 2670 /* WARNING: this file should *not* be used by applications. It is 2671 part of the implementation of the compression library and is 2672 subject to change. Applications should only use zlib.h. 2673 */ 2674 2675 /* Huffman code lookup table entry--this entry is four bytes for machines 2676 that have 16-bit pointers (e.g. PC's in the small or medium model). */ 2677 2678 typedef struct inflate_huft_s FAR inflate_huft; 2679 2680 struct inflate_huft_s { 2681 union { 2682 struct { 2683 Byte Exop; /* number of extra bits or operation */ 2684 Byte Bits; /* number of bits in this code or subcode */ 2685 } what; 2686 uInt Nalloc; /* number of these allocated here */ 2687 Bytef *pad; /* pad structure to a power of 2 (4 bytes for */ 2688 } word; /* 16-bit, 8 bytes for 32-bit machines) */ 2689 union { 2690 uInt Base; /* literal, length base, or distance base */ 2691 inflate_huft *Next; /* pointer to next level of table */ 2692 } more; 2693 }; 2694 2695 #ifdef DEBUG_ZLIB 2696 local uInt inflate_hufts; 2697 #endif 2698 2699 local int inflate_trees_bits OF(( 2700 uIntf *, /* 19 code lengths */ 2701 uIntf *, /* bits tree desired/actual depth */ 2702 inflate_huft * FAR *, /* bits tree result */ 2703 z_stream *)); /* for zalloc, zfree functions */ 2704 2705 local int inflate_trees_dynamic OF(( 2706 uInt, /* number of literal/length codes */ 2707 uInt, /* number of distance codes */ 2708 uIntf *, /* that many (total) code lengths */ 2709 uIntf *, /* literal desired/actual bit depth */ 2710 uIntf *, /* distance desired/actual bit depth */ 2711 inflate_huft * FAR *, /* literal/length tree result */ 2712 inflate_huft * FAR *, /* distance tree result */ 2713 z_stream *)); /* for zalloc, zfree functions */ 2714 2715 local int inflate_trees_fixed OF(( 2716 uIntf *, /* literal desired/actual bit depth */ 2717 uIntf *, /* distance desired/actual bit depth */ 2718 inflate_huft * FAR *, /* literal/length tree result */ 2719 inflate_huft * FAR *)); /* distance tree result */ 2720 2721 local int inflate_trees_free OF(( 2722 inflate_huft *, /* tables to free */ 2723 z_stream *)); /* for zfree function */ 2724 2725 2726 /*+++++*/ 2727 /* infcodes.h -- header to use infcodes.c 2728 * Copyright (C) 1995 Mark Adler 2729 * For conditions of distribution and use, see copyright notice in zlib.h 2730 */ 2731 2732 /* WARNING: this file should *not* be used by applications. It is 2733 part of the implementation of the compression library and is 2734 subject to change. Applications should only use zlib.h. 2735 */ 2736 2737 struct inflate_codes_state; 2738 typedef struct inflate_codes_state FAR inflate_codes_statef; 2739 2740 local inflate_codes_statef *inflate_codes_new OF(( 2741 uInt, uInt, 2742 inflate_huft *, inflate_huft *, 2743 z_stream *)); 2744 2745 local int inflate_codes OF(( 2746 inflate_blocks_statef *, 2747 z_stream *, 2748 int)); 2749 2750 local void inflate_codes_free OF(( 2751 inflate_codes_statef *, 2752 z_stream *)); 2753 2754 2755 /*+++++*/ 2756 /* inflate.c -- zlib interface to inflate modules 2757 * Copyright (C) 1995 Mark Adler 2758 * For conditions of distribution and use, see copyright notice in zlib.h 2759 */ 2760 2761 /* inflate private state */ 2762 struct internal_state { 2763 2764 /* mode */ 2765 enum { 2766 METHOD, /* waiting for method byte */ 2767 FLAG, /* waiting for flag byte */ 2768 BLOCKS, /* decompressing blocks */ 2769 CHECK4, /* four check bytes to go */ 2770 CHECK3, /* three check bytes to go */ 2771 CHECK2, /* two check bytes to go */ 2772 CHECK1, /* one check byte to go */ 2773 DONE, /* finished check, done */ 2774 BAD} /* got an error--stay here */ 2775 mode; /* current inflate mode */ 2776 2777 /* mode dependent information */ 2778 union { 2779 uInt method; /* if FLAGS, method byte */ 2780 struct { 2781 uLong was; /* computed check value */ 2782 uLong need; /* stream check value */ 2783 } check; /* if CHECK, check values to compare */ 2784 uInt marker; /* if BAD, inflateSync's marker bytes count */ 2785 } sub; /* submode */ 2786 2787 /* mode independent information */ 2788 int nowrap; /* flag for no wrapper */ 2789 uInt wbits; /* log2(window size) (8..15, defaults to 15) */ 2790 inflate_blocks_statef 2791 *blocks; /* current inflate_blocks state */ 2792 2793 }; 2794 2795 2796 int inflateReset(z) 2797 z_stream *z; 2798 { 2799 uLong c; 2800 2801 if (z == Z_NULL || z->state == Z_NULL) 2802 return Z_STREAM_ERROR; 2803 z->total_in = z->total_out = 0; 2804 z->msg = Z_NULL; 2805 z->state->mode = z->state->nowrap ? BLOCKS : METHOD; 2806 inflate_blocks_reset(z->state->blocks, z, &c); 2807 Trace((stderr, "inflate: reset\n")); 2808 return Z_OK; 2809 } 2810 2811 2812 int inflateEnd(z) 2813 z_stream *z; 2814 { 2815 uLong c; 2816 2817 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) 2818 return Z_STREAM_ERROR; 2819 if (z->state->blocks != Z_NULL) 2820 inflate_blocks_free(z->state->blocks, z, &c); 2821 ZFREE(z, z->state, sizeof(struct internal_state)); 2822 z->state = Z_NULL; 2823 Trace((stderr, "inflate: end\n")); 2824 return Z_OK; 2825 } 2826 2827 2828 int inflateInit2(z, w) 2829 z_stream *z; 2830 int w; 2831 { 2832 /* initialize state */ 2833 if (z == Z_NULL) 2834 return Z_STREAM_ERROR; 2835 /* if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */ 2836 /* if (z->zfree == Z_NULL) z->zfree = zcfree; */ 2837 if ((z->state = (struct internal_state FAR *) 2838 ZALLOC_INIT(z,1,sizeof(struct internal_state))) == Z_NULL) 2839 return Z_MEM_ERROR; 2840 z->state->blocks = Z_NULL; 2841 2842 /* handle undocumented nowrap option (no zlib header or check) */ 2843 z->state->nowrap = 0; 2844 if (w < 0) 2845 { 2846 w = - w; 2847 z->state->nowrap = 1; 2848 } 2849 2850 /* set window size */ 2851 if (w < 8 || w > 15) 2852 { 2853 inflateEnd(z); 2854 return Z_STREAM_ERROR; 2855 } 2856 z->state->wbits = (uInt)w; 2857 2858 /* create inflate_blocks state */ 2859 if ((z->state->blocks = 2860 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w)) 2861 == Z_NULL) 2862 { 2863 inflateEnd(z); 2864 return Z_MEM_ERROR; 2865 } 2866 Trace((stderr, "inflate: allocated\n")); 2867 2868 /* reset state */ 2869 inflateReset(z); 2870 return Z_OK; 2871 } 2872 2873 2874 int inflateInit(z) 2875 z_stream *z; 2876 { 2877 return inflateInit2(z, DEF_WBITS); 2878 } 2879 2880 2881 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;} 2882 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) 2883 2884 int inflate(z, f) 2885 z_stream *z; 2886 int f; 2887 { 2888 int r; 2889 uInt b; 2890 2891 if (z == Z_NULL || z->next_in == Z_NULL) 2892 return Z_STREAM_ERROR; 2893 r = Z_BUF_ERROR; 2894 while (1) switch (z->state->mode) 2895 { 2896 case METHOD: 2897 NEEDBYTE 2898 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED) 2899 { 2900 z->state->mode = BAD; 2901 z->msg = "unknown compression method"; 2902 z->state->sub.marker = 5; /* can't try inflateSync */ 2903 break; 2904 } 2905 if ((z->state->sub.method >> 4) + 8 > z->state->wbits) 2906 { 2907 z->state->mode = BAD; 2908 z->msg = "invalid window size"; 2909 z->state->sub.marker = 5; /* can't try inflateSync */ 2910 break; 2911 } 2912 z->state->mode = FLAG; 2913 case FLAG: 2914 NEEDBYTE 2915 if ((b = NEXTBYTE) & 0x20) 2916 { 2917 z->state->mode = BAD; 2918 z->msg = "invalid reserved bit"; 2919 z->state->sub.marker = 5; /* can't try inflateSync */ 2920 break; 2921 } 2922 if (((z->state->sub.method << 8) + b) % 31) 2923 { 2924 z->state->mode = BAD; 2925 z->msg = "incorrect header check"; 2926 z->state->sub.marker = 5; /* can't try inflateSync */ 2927 break; 2928 } 2929 Trace((stderr, "inflate: zlib header ok\n")); 2930 z->state->mode = BLOCKS; 2931 case BLOCKS: 2932 r = inflate_blocks(z->state->blocks, z, r); 2933 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0) 2934 r = inflate_packet_flush(z->state->blocks); 2935 if (r == Z_DATA_ERROR) 2936 { 2937 z->state->mode = BAD; 2938 z->state->sub.marker = 0; /* can try inflateSync */ 2939 break; 2940 } 2941 if (r != Z_STREAM_END) 2942 return r; 2943 r = Z_OK; 2944 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); 2945 if (z->state->nowrap) 2946 { 2947 z->state->mode = DONE; 2948 break; 2949 } 2950 z->state->mode = CHECK4; 2951 case CHECK4: 2952 NEEDBYTE 2953 z->state->sub.check.need = (uLong)NEXTBYTE << 24; 2954 z->state->mode = CHECK3; 2955 case CHECK3: 2956 NEEDBYTE 2957 z->state->sub.check.need += (uLong)NEXTBYTE << 16; 2958 z->state->mode = CHECK2; 2959 case CHECK2: 2960 NEEDBYTE 2961 z->state->sub.check.need += (uLong)NEXTBYTE << 8; 2962 z->state->mode = CHECK1; 2963 case CHECK1: 2964 NEEDBYTE 2965 z->state->sub.check.need += (uLong)NEXTBYTE; 2966 2967 if (z->state->sub.check.was != z->state->sub.check.need) 2968 { 2969 z->state->mode = BAD; 2970 z->msg = "incorrect data check"; 2971 z->state->sub.marker = 5; /* can't try inflateSync */ 2972 break; 2973 } 2974 Trace((stderr, "inflate: zlib check ok\n")); 2975 z->state->mode = DONE; 2976 case DONE: 2977 return Z_STREAM_END; 2978 case BAD: 2979 return Z_DATA_ERROR; 2980 default: 2981 return Z_STREAM_ERROR; 2982 } 2983 2984 empty: 2985 if (f != Z_PACKET_FLUSH) 2986 return r; 2987 z->state->mode = BAD; 2988 z->state->sub.marker = 0; /* can try inflateSync */ 2989 return Z_DATA_ERROR; 2990 } 2991 2992 /* 2993 * This subroutine adds the data at next_in/avail_in to the output history 2994 * without performing any output. The output buffer must be "caught up"; 2995 * i.e. no pending output (hence s->read equals s->write), and the state must 2996 * be BLOCKS (i.e. we should be willing to see the start of a series of 2997 * BLOCKS). On exit, the output will also be caught up, and the checksum 2998 * will have been updated if need be. 2999 */ 3000 3001 int inflateIncomp(z) 3002 z_stream *z; 3003 { 3004 if (z->state->mode != BLOCKS) 3005 return Z_DATA_ERROR; 3006 return inflate_addhistory(z->state->blocks, z); 3007 } 3008 3009 3010 int inflateSync(z) 3011 z_stream *z; 3012 { 3013 uInt n; /* number of bytes to look at */ 3014 Bytef *p; /* pointer to bytes */ 3015 uInt m; /* number of marker bytes found in a row */ 3016 uLong r, w; /* temporaries to save total_in and total_out */ 3017 3018 /* set up */ 3019 if (z == Z_NULL || z->state == Z_NULL) 3020 return Z_STREAM_ERROR; 3021 if (z->state->mode != BAD) 3022 { 3023 z->state->mode = BAD; 3024 z->state->sub.marker = 0; 3025 } 3026 if ((n = z->avail_in) == 0) 3027 return Z_BUF_ERROR; 3028 p = z->next_in; 3029 m = z->state->sub.marker; 3030 3031 /* search */ 3032 while (n && m < 4) 3033 { 3034 if (*p == (Byte)(m < 2 ? 0 : 0xff)) 3035 m++; 3036 else if (*p) 3037 m = 0; 3038 else 3039 m = 4 - m; 3040 p++, n--; 3041 } 3042 3043 /* restore */ 3044 z->total_in += p - z->next_in; 3045 z->next_in = p; 3046 z->avail_in = n; 3047 z->state->sub.marker = m; 3048 3049 /* return no joy or set up to restart on a new block */ 3050 if (m != 4) 3051 return Z_DATA_ERROR; 3052 r = z->total_in; w = z->total_out; 3053 inflateReset(z); 3054 z->total_in = r; z->total_out = w; 3055 z->state->mode = BLOCKS; 3056 return Z_OK; 3057 } 3058 3059 #undef NEEDBYTE 3060 #undef NEXTBYTE 3061 3062 /*+++++*/ 3063 /* infutil.h -- types and macros common to blocks and codes 3064 * Copyright (C) 1995 Mark Adler 3065 * For conditions of distribution and use, see copyright notice in zlib.h 3066 */ 3067 3068 /* WARNING: this file should *not* be used by applications. It is 3069 part of the implementation of the compression library and is 3070 subject to change. Applications should only use zlib.h. 3071 */ 3072 3073 /* inflate blocks semi-private state */ 3074 struct inflate_blocks_state { 3075 3076 /* mode */ 3077 enum { 3078 TYPE, /* get type bits (3, including end bit) */ 3079 LENS, /* get lengths for stored */ 3080 STORED, /* processing stored block */ 3081 TABLE, /* get table lengths */ 3082 BTREE, /* get bit lengths tree for a dynamic block */ 3083 DTREE, /* get length, distance trees for a dynamic block */ 3084 CODES, /* processing fixed or dynamic block */ 3085 DRY, /* output remaining window bytes */ 3086 DONEB, /* finished last block, done */ 3087 BADB} /* got a data error--stuck here */ 3088 mode; /* current inflate_block mode */ 3089 3090 /* mode dependent information */ 3091 union { 3092 uInt left; /* if STORED, bytes left to copy */ 3093 struct { 3094 uInt table; /* table lengths (14 bits) */ 3095 uInt index; /* index into blens (or border) */ 3096 uIntf *blens; /* bit lengths of codes */ 3097 uInt bb; /* bit length tree depth */ 3098 inflate_huft *tb; /* bit length decoding tree */ 3099 int nblens; /* # elements allocated at blens */ 3100 } trees; /* if DTREE, decoding info for trees */ 3101 struct { 3102 inflate_huft *tl, *td; /* trees to free */ 3103 inflate_codes_statef 3104 *codes; 3105 } decode; /* if CODES, current state */ 3106 } sub; /* submode */ 3107 uInt last; /* true if this block is the last block */ 3108 3109 /* mode independent information */ 3110 uInt bitk; /* bits in bit buffer */ 3111 uLong bitb; /* bit buffer */ 3112 Bytef *window; /* sliding window */ 3113 Bytef *end; /* one byte after sliding window */ 3114 Bytef *read; /* window read pointer */ 3115 Bytef *write; /* window write pointer */ 3116 check_func checkfn; /* check function */ 3117 uLong check; /* check on output */ 3118 3119 }; 3120 3121 3122 /* defines for inflate input/output */ 3123 /* update pointers and return */ 3124 #define UPDBITS {s->bitb=b;s->bitk=k;} 3125 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;} 3126 #define UPDOUT {s->write=q;} 3127 #define UPDATE {UPDBITS UPDIN UPDOUT} 3128 #define LEAVE {UPDATE return inflate_flush(s,z,r);} 3129 /* get bytes and bits */ 3130 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} 3131 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE} 3132 #define NEXTBYTE (n--,*p++) 3133 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} 3134 #define DUMPBITS(j) {b>>=(j);k-=(j);} 3135 /* output bytes */ 3136 #define WAVAIL (q<s->read?s->read-q-1:s->end-q) 3137 #define LOADOUT {q=s->write;m=WAVAIL;} 3138 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}} 3139 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT} 3140 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} 3141 #define OUTBYTE(a) {*q++=(Byte)(a);m--;} 3142 /* load local pointers */ 3143 #define LOAD {LOADIN LOADOUT} 3144 3145 /* And'ing with mask[n] masks the lower n bits */ 3146 local uInt inflate_mask[] = { 3147 0x0000, 3148 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 3149 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 3150 }; 3151 3152 /* copy as much as possible from the sliding window to the output area */ 3153 local int inflate_flush OF(( 3154 inflate_blocks_statef *, 3155 z_stream *, 3156 int)); 3157 3158 /*+++++*/ 3159 /* inffast.h -- header to use inffast.c 3160 * Copyright (C) 1995 Mark Adler 3161 * For conditions of distribution and use, see copyright notice in zlib.h 3162 */ 3163 3164 /* WARNING: this file should *not* be used by applications. It is 3165 part of the implementation of the compression library and is 3166 subject to change. Applications should only use zlib.h. 3167 */ 3168 3169 local int inflate_fast OF(( 3170 uInt, 3171 uInt, 3172 inflate_huft *, 3173 inflate_huft *, 3174 inflate_blocks_statef *, 3175 z_stream *)); 3176 3177 3178 /*+++++*/ 3179 /* infblock.c -- interpret and process block types to last block 3180 * Copyright (C) 1995 Mark Adler 3181 * For conditions of distribution and use, see copyright notice in zlib.h 3182 */ 3183 3184 /* Table for deflate from PKZIP's appnote.txt. */ 3185 local uInt border[] = { /* Order of the bit length code lengths */ 3186 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 3187 3188 /* 3189 Notes beyond the 1.93a appnote.txt: 3190 3191 1. Distance pointers never point before the beginning of the output 3192 stream. 3193 2. Distance pointers can point back across blocks, up to 32k away. 3194 3. There is an implied maximum of 7 bits for the bit length table and 3195 15 bits for the actual data. 3196 4. If only one code exists, then it is encoded using one bit. (Zero 3197 would be more efficient, but perhaps a little confusing.) If two 3198 codes exist, they are coded using one bit each (0 and 1). 3199 5. There is no way of sending zero distance codes--a dummy must be 3200 sent if there are none. (History: a pre 2.0 version of PKZIP would 3201 store blocks with no distance codes, but this was discovered to be 3202 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow 3203 zero distance codes, which is sent as one code of zero bits in 3204 length. 3205 6. There are up to 286 literal/length codes. Code 256 represents the 3206 end-of-block. Note however that the static length tree defines 3207 288 codes just to fill out the Huffman codes. Codes 286 and 287 3208 cannot be used though, since there is no length base or extra bits 3209 defined for them. Similarily, there are up to 30 distance codes. 3210 However, static trees define 32 codes (all 5 bits) to fill out the 3211 Huffman codes, but the last two had better not show up in the data. 3212 7. Unzip can check dynamic Huffman blocks for complete code sets. 3213 The exception is that a single code would not be complete (see #4). 3214 8. The five bits following the block type is really the number of 3215 literal codes sent minus 257. 3216 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits 3217 (1+6+6). Therefore, to output three times the length, you output 3218 three codes (1+1+1), whereas to output four times the same length, 3219 you only need two codes (1+3). Hmm. 3220 10. In the tree reconstruction algorithm, Code = Code + Increment 3221 only if BitLength(i) is not zero. (Pretty obvious.) 3222 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) 3223 12. Note: length code 284 can represent 227-258, but length code 285 3224 really is 258. The last length deserves its own, short code 3225 since it gets used a lot in very redundant files. The length 3226 258 is special since 258 - 3 (the min match length) is 255. 3227 13. The literal/length and distance code bit lengths are read as a 3228 single stream of lengths. It is possible (and advantageous) for 3229 a repeat code (16, 17, or 18) to go across the boundary between 3230 the two sets of lengths. 3231 */ 3232 3233 3234 local void inflate_blocks_reset(s, z, c) 3235 inflate_blocks_statef *s; 3236 z_stream *z; 3237 uLongf *c; 3238 { 3239 if (s->checkfn != Z_NULL) 3240 *c = s->check; 3241 if (s->mode == BTREE || s->mode == DTREE) 3242 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt)); 3243 if (s->mode == CODES) 3244 { 3245 inflate_codes_free(s->sub.decode.codes, z); 3246 inflate_trees_free(s->sub.decode.td, z); 3247 inflate_trees_free(s->sub.decode.tl, z); 3248 } 3249 s->mode = TYPE; 3250 s->bitk = 0; 3251 s->bitb = 0; 3252 s->read = s->write = s->window; 3253 if (s->checkfn != Z_NULL) 3254 s->check = (*s->checkfn)(0L, Z_NULL, 0); 3255 Trace((stderr, "inflate: blocks reset\n")); 3256 } 3257 3258 3259 local inflate_blocks_statef *inflate_blocks_new(z, c, w) 3260 z_stream *z; 3261 check_func c; 3262 uInt w; 3263 { 3264 inflate_blocks_statef *s; 3265 3266 if ((s = (inflate_blocks_statef *)ZALLOC_INIT 3267 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL) 3268 return s; 3269 if ((s->window = (Bytef *)ZALLOC_INIT(z, 1, w)) == Z_NULL) 3270 { 3271 ZFREE(z, s, sizeof(struct inflate_blocks_state)); 3272 return Z_NULL; 3273 } 3274 s->end = s->window + w; 3275 s->checkfn = c; 3276 s->mode = TYPE; 3277 Trace((stderr, "inflate: blocks allocated\n")); 3278 inflate_blocks_reset(s, z, &s->check); 3279 return s; 3280 } 3281 3282 3283 local int inflate_blocks(s, z, r) 3284 inflate_blocks_statef *s; 3285 z_stream *z; 3286 int r; 3287 { 3288 uInt t; /* temporary storage */ 3289 uLong b; /* bit buffer */ 3290 uInt k; /* bits in bit buffer */ 3291 Bytef *p; /* input data pointer */ 3292 uInt n; /* bytes available there */ 3293 Bytef *q; /* output window write pointer */ 3294 uInt m; /* bytes to end of window or read pointer */ 3295 3296 /* copy input/output information to locals (UPDATE macro restores) */ 3297 LOAD 3298 3299 /* process input based on current state */ 3300 while (1) switch (s->mode) 3301 { 3302 case TYPE: 3303 NEEDBITS(3) 3304 t = (uInt)b & 7; 3305 s->last = t & 1; 3306 switch (t >> 1) 3307 { 3308 case 0: /* stored */ 3309 Trace((stderr, "inflate: stored block%s\n", 3310 s->last ? " (last)" : "")); 3311 DUMPBITS(3) 3312 t = k & 7; /* go to byte boundary */ 3313 DUMPBITS(t) 3314 s->mode = LENS; /* get length of stored block */ 3315 break; 3316 case 1: /* fixed */ 3317 Trace((stderr, "inflate: fixed codes block%s\n", 3318 s->last ? " (last)" : "")); 3319 { 3320 uInt bl, bd; 3321 inflate_huft *tl, *td; 3322 3323 inflate_trees_fixed(&bl, &bd, &tl, &td); 3324 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z); 3325 if (s->sub.decode.codes == Z_NULL) 3326 { 3327 r = Z_MEM_ERROR; 3328 LEAVE 3329 } 3330 s->sub.decode.tl = Z_NULL; /* don't try to free these */ 3331 s->sub.decode.td = Z_NULL; 3332 } 3333 DUMPBITS(3) 3334 s->mode = CODES; 3335 break; 3336 case 2: /* dynamic */ 3337 Trace((stderr, "inflate: dynamic codes block%s\n", 3338 s->last ? " (last)" : "")); 3339 DUMPBITS(3) 3340 s->mode = TABLE; 3341 break; 3342 case 3: /* illegal */ 3343 DUMPBITS(3) 3344 s->mode = BADB; 3345 z->msg = "invalid block type"; 3346 r = Z_DATA_ERROR; 3347 LEAVE 3348 } 3349 break; 3350 case LENS: 3351 NEEDBITS(32) 3352 if (((~b) >> 16) != (b & 0xffff)) 3353 { 3354 s->mode = BADB; 3355 z->msg = "invalid stored block lengths"; 3356 r = Z_DATA_ERROR; 3357 LEAVE 3358 } 3359 s->sub.left = (uInt)b & 0xffff; 3360 b = k = 0; /* dump bits */ 3361 Tracev((stderr, "inflate: stored length %u\n", s->sub.left)); 3362 s->mode = s->sub.left ? STORED : TYPE; 3363 break; 3364 case STORED: 3365 if (n == 0) 3366 LEAVE 3367 NEEDOUT 3368 t = s->sub.left; 3369 if (t > n) t = n; 3370 if (t > m) t = m; 3371 zmemcpy(q, p, t); 3372 p += t; n -= t; 3373 q += t; m -= t; 3374 if ((s->sub.left -= t) != 0) 3375 break; 3376 Tracev((stderr, "inflate: stored end, %lu total out\n", 3377 z->total_out + (q >= s->read ? q - s->read : 3378 (s->end - s->read) + (q - s->window)))); 3379 s->mode = s->last ? DRY : TYPE; 3380 break; 3381 case TABLE: 3382 NEEDBITS(14) 3383 s->sub.trees.table = t = (uInt)b & 0x3fff; 3384 #ifndef PKZIP_BUG_WORKAROUND 3385 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) 3386 { 3387 s->mode = BADB; 3388 z->msg = "too many length or distance symbols"; 3389 r = Z_DATA_ERROR; 3390 LEAVE 3391 } 3392 #endif 3393 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); 3394 if (t < 19) 3395 t = 19; 3396 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL) 3397 { 3398 r = Z_MEM_ERROR; 3399 LEAVE 3400 } 3401 s->sub.trees.nblens = t; 3402 DUMPBITS(14) 3403 s->sub.trees.index = 0; 3404 Tracev((stderr, "inflate: table sizes ok\n")); 3405 s->mode = BTREE; 3406 case BTREE: 3407 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10)) 3408 { 3409 NEEDBITS(3) 3410 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7; 3411 DUMPBITS(3) 3412 } 3413 while (s->sub.trees.index < 19) 3414 s->sub.trees.blens[border[s->sub.trees.index++]] = 0; 3415 s->sub.trees.bb = 7; 3416 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb, 3417 &s->sub.trees.tb, z); 3418 if (t != Z_OK) 3419 { 3420 r = t; 3421 if (r == Z_DATA_ERROR) 3422 s->mode = BADB; 3423 LEAVE 3424 } 3425 s->sub.trees.index = 0; 3426 Tracev((stderr, "inflate: bits tree ok\n")); 3427 s->mode = DTREE; 3428 case DTREE: 3429 while (t = s->sub.trees.table, 3430 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) 3431 { 3432 inflate_huft *h; 3433 uInt i, j, c; 3434 3435 t = s->sub.trees.bb; 3436 NEEDBITS(t) 3437 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]); 3438 t = h->word.what.Bits; 3439 c = h->more.Base; 3440 if (c < 16) 3441 { 3442 DUMPBITS(t) 3443 s->sub.trees.blens[s->sub.trees.index++] = c; 3444 } 3445 else /* c == 16..18 */ 3446 { 3447 i = c == 18 ? 7 : c - 14; 3448 j = c == 18 ? 11 : 3; 3449 NEEDBITS(t + i) 3450 DUMPBITS(t) 3451 j += (uInt)b & inflate_mask[i]; 3452 DUMPBITS(i) 3453 i = s->sub.trees.index; 3454 t = s->sub.trees.table; 3455 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || 3456 (c == 16 && i < 1)) 3457 { 3458 s->mode = BADB; 3459 z->msg = "invalid bit length repeat"; 3460 r = Z_DATA_ERROR; 3461 LEAVE 3462 } 3463 c = c == 16 ? s->sub.trees.blens[i - 1] : 0; 3464 do { 3465 s->sub.trees.blens[i++] = c; 3466 } while (--j); 3467 s->sub.trees.index = i; 3468 } 3469 } 3470 inflate_trees_free(s->sub.trees.tb, z); 3471 s->sub.trees.tb = Z_NULL; 3472 { 3473 uInt bl, bd; 3474 inflate_huft *tl, *td; 3475 inflate_codes_statef *c; 3476 3477 bl = 9; /* must be <= 9 for lookahead assumptions */ 3478 bd = 6; /* must be <= 9 for lookahead assumptions */ 3479 t = s->sub.trees.table; 3480 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), 3481 s->sub.trees.blens, &bl, &bd, &tl, &td, z); 3482 if (t != Z_OK) 3483 { 3484 if (t == (uInt)Z_DATA_ERROR) 3485 s->mode = BADB; 3486 r = t; 3487 LEAVE 3488 } 3489 Tracev((stderr, "inflate: trees ok\n")); 3490 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL) 3491 { 3492 inflate_trees_free(td, z); 3493 inflate_trees_free(tl, z); 3494 r = Z_MEM_ERROR; 3495 LEAVE 3496 } 3497 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt)); 3498 s->sub.decode.codes = c; 3499 s->sub.decode.tl = tl; 3500 s->sub.decode.td = td; 3501 } 3502 s->mode = CODES; 3503 case CODES: 3504 UPDATE 3505 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END) 3506 return inflate_flush(s, z, r); 3507 r = Z_OK; 3508 inflate_codes_free(s->sub.decode.codes, z); 3509 inflate_trees_free(s->sub.decode.td, z); 3510 inflate_trees_free(s->sub.decode.tl, z); 3511 LOAD 3512 Tracev((stderr, "inflate: codes end, %lu total out\n", 3513 z->total_out + (q >= s->read ? q - s->read : 3514 (s->end - s->read) + (q - s->window)))); 3515 if (!s->last) 3516 { 3517 s->mode = TYPE; 3518 break; 3519 } 3520 if (k > 7) /* return unused byte, if any */ 3521 { 3522 Assert(k < 16, "inflate_codes grabbed too many bytes") 3523 k -= 8; 3524 n++; 3525 p--; /* can always return one */ 3526 } 3527 s->mode = DRY; 3528 case DRY: 3529 FLUSH 3530 if (s->read != s->write) 3531 LEAVE 3532 s->mode = DONEB; 3533 case DONEB: 3534 r = Z_STREAM_END; 3535 LEAVE 3536 case BADB: 3537 r = Z_DATA_ERROR; 3538 LEAVE 3539 default: 3540 r = Z_STREAM_ERROR; 3541 LEAVE 3542 } 3543 } 3544 3545 3546 local int inflate_blocks_free(s, z, c) 3547 inflate_blocks_statef *s; 3548 z_stream *z; 3549 uLongf *c; 3550 { 3551 inflate_blocks_reset(s, z, c); 3552 ZFREE(z, s->window, s->end - s->window); 3553 ZFREE(z, s, sizeof(struct inflate_blocks_state)); 3554 Trace((stderr, "inflate: blocks freed\n")); 3555 return Z_OK; 3556 } 3557 3558 /* 3559 * This subroutine adds the data at next_in/avail_in to the output history 3560 * without performing any output. The output buffer must be "caught up"; 3561 * i.e. no pending output (hence s->read equals s->write), and the state must 3562 * be BLOCKS (i.e. we should be willing to see the start of a series of 3563 * BLOCKS). On exit, the output will also be caught up, and the checksum 3564 * will have been updated if need be. 3565 */ 3566 local int inflate_addhistory(s, z) 3567 inflate_blocks_statef *s; 3568 z_stream *z; 3569 { 3570 uLong b; /* bit buffer */ /* NOT USED HERE */ 3571 uInt k; /* bits in bit buffer */ /* NOT USED HERE */ 3572 uInt t; /* temporary storage */ 3573 Bytef *p; /* input data pointer */ 3574 uInt n; /* bytes available there */ 3575 Bytef *q; /* output window write pointer */ 3576 uInt m; /* bytes to end of window or read pointer */ 3577 3578 if (s->read != s->write) 3579 return Z_STREAM_ERROR; 3580 if (s->mode != TYPE) 3581 return Z_DATA_ERROR; 3582 3583 /* we're ready to rock */ 3584 LOAD 3585 /* while there is input ready, copy to output buffer, moving 3586 * pointers as needed. 3587 */ 3588 while (n) { 3589 t = n; /* how many to do */ 3590 /* is there room until end of buffer? */ 3591 if (t > m) t = m; 3592 /* update check information */ 3593 if (s->checkfn != Z_NULL) 3594 s->check = (*s->checkfn)(s->check, q, t); 3595 zmemcpy(q, p, t); 3596 q += t; 3597 p += t; 3598 n -= t; 3599 z->total_out += t; 3600 s->read = q; /* drag read pointer forward */ 3601 /* WRAP */ /* expand WRAP macro by hand to handle s->read */ 3602 if (q == s->end) { 3603 s->read = q = s->window; 3604 m = WAVAIL; 3605 } 3606 } 3607 UPDATE 3608 return Z_OK; 3609 } 3610 3611 3612 /* 3613 * At the end of a Deflate-compressed PPP packet, we expect to have seen 3614 * a `stored' block type value but not the (zero) length bytes. 3615 */ 3616 local int inflate_packet_flush(s) 3617 inflate_blocks_statef *s; 3618 { 3619 if (s->mode != LENS) 3620 return Z_DATA_ERROR; 3621 s->mode = TYPE; 3622 return Z_OK; 3623 } 3624 3625 3626 /*+++++*/ 3627 /* inftrees.c -- generate Huffman trees for efficient decoding 3628 * Copyright (C) 1995 Mark Adler 3629 * For conditions of distribution and use, see copyright notice in zlib.h 3630 */ 3631 3632 /* simplify the use of the inflate_huft type with some defines */ 3633 #define base more.Base 3634 #define next more.Next 3635 #define exop word.what.Exop 3636 #define bits word.what.Bits 3637 3638 3639 local int huft_build OF(( 3640 uIntf *, /* code lengths in bits */ 3641 uInt, /* number of codes */ 3642 uInt, /* number of "simple" codes */ 3643 uIntf *, /* list of base values for non-simple codes */ 3644 uIntf *, /* list of extra bits for non-simple codes */ 3645 inflate_huft * FAR*,/* result: starting table */ 3646 uIntf *, /* maximum lookup bits (returns actual) */ 3647 z_stream *)); /* for zalloc function */ 3648 3649 local voidpf falloc OF(( 3650 voidpf, /* opaque pointer (not used) */ 3651 uInt, /* number of items */ 3652 uInt)); /* size of item */ 3653 3654 local void ffree OF(( 3655 voidpf q, /* opaque pointer (not used) */ 3656 voidpf p, /* what to free (not used) */ 3657 uInt n)); /* number of bytes (not used) */ 3658 3659 /* Tables for deflate from PKZIP's appnote.txt. */ 3660 local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */ 3661 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 3662 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 3663 /* actually lengths - 2; also see note #13 above about 258 */ 3664 local uInt cplext[] = { /* Extra bits for literal codes 257..285 */ 3665 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3666 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */ 3667 local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */ 3668 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 3669 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 3670 8193, 12289, 16385, 24577}; 3671 local uInt cpdext[] = { /* Extra bits for distance codes */ 3672 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 3673 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 3674 12, 12, 13, 13}; 3675 3676 /* 3677 Huffman code decoding is performed using a multi-level table lookup. 3678 The fastest way to decode is to simply build a lookup table whose 3679 size is determined by the longest code. However, the time it takes 3680 to build this table can also be a factor if the data being decoded 3681 is not very long. The most common codes are necessarily the 3682 shortest codes, so those codes dominate the decoding time, and hence 3683 the speed. The idea is you can have a shorter table that decodes the 3684 shorter, more probable codes, and then point to subsidiary tables for 3685 the longer codes. The time it costs to decode the longer codes is 3686 then traded against the time it takes to make longer tables. 3687 3688 This results of this trade are in the variables lbits and dbits 3689 below. lbits is the number of bits the first level table for literal/ 3690 length codes can decode in one step, and dbits is the same thing for 3691 the distance codes. Subsequent tables are also less than or equal to 3692 those sizes. These values may be adjusted either when all of the 3693 codes are shorter than that, in which case the longest code length in 3694 bits is used, or when the shortest code is *longer* than the requested 3695 table size, in which case the length of the shortest code in bits is 3696 used. 3697 3698 There are two different values for the two tables, since they code a 3699 different number of possibilities each. The literal/length table 3700 codes 286 possible values, or in a flat code, a little over eight 3701 bits. The distance table codes 30 possible values, or a little less 3702 than five bits, flat. The optimum values for speed end up being 3703 about one bit more than those, so lbits is 8+1 and dbits is 5+1. 3704 The optimum values may differ though from machine to machine, and 3705 possibly even between compilers. Your mileage may vary. 3706 */ 3707 3708 3709 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ 3710 #define BMAX 15 /* maximum bit length of any code */ 3711 #define N_MAX 288 /* maximum number of codes in any set */ 3712 3713 #ifdef DEBUG_ZLIB 3714 uInt inflate_hufts; 3715 #endif 3716 3717 local int huft_build(b, n, s, d, e, t, m, zs) 3718 uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ 3719 uInt n; /* number of codes (assumed <= N_MAX) */ 3720 uInt s; /* number of simple-valued codes (0..s-1) */ 3721 uIntf *d; /* list of base values for non-simple codes */ 3722 uIntf *e; /* list of extra bits for non-simple codes */ 3723 inflate_huft * FAR *t; /* result: starting table */ 3724 uIntf *m; /* maximum lookup bits, returns actual */ 3725 z_stream *zs; /* for zalloc function */ 3726 /* Given a list of code lengths and a maximum table size, make a set of 3727 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR 3728 if the given code set is incomplete (the tables are still built in this 3729 case), Z_DATA_ERROR if the input is invalid (all zero length codes or an 3730 over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */ 3731 { 3732 3733 uInt a; /* counter for codes of length k */ 3734 uInt c[BMAX+1]; /* bit length count table */ 3735 uInt f; /* i repeats in table every f entries */ 3736 int g; /* maximum code length */ 3737 int h; /* table level */ 3738 register uInt i; /* counter, current code */ 3739 register uInt j; /* counter */ 3740 register int k; /* number of bits in current code */ 3741 int l; /* bits per table (returned in m) */ 3742 register uIntf *p; /* pointer into c[], b[], or v[] */ 3743 inflate_huft *q; /* points to current table */ 3744 struct inflate_huft_s r; /* table entry for structure assignment */ 3745 inflate_huft *u[BMAX]; /* table stack */ 3746 uInt v[N_MAX]; /* values in order of bit length */ 3747 register int w; /* bits before this table == (l * h) */ 3748 uInt x[BMAX+1]; /* bit offsets, then code stack */ 3749 uIntf *xp; /* pointer into x */ 3750 int y; /* number of dummy codes added */ 3751 uInt z; /* number of entries in current table */ 3752 3753 3754 /* Generate counts for each bit length */ 3755 p = c; 3756 #define C0 *p++ = 0; 3757 #define C2 C0 C0 C0 C0 3758 #define C4 C2 C2 C2 C2 3759 C4 /* clear c[]--assume BMAX+1 is 16 */ 3760 p = b; i = n; 3761 do { 3762 c[*p++]++; /* assume all entries <= BMAX */ 3763 } while (--i); 3764 if (c[0] == n) /* null input--all zero length codes */ 3765 { 3766 *t = (inflate_huft *)Z_NULL; 3767 *m = 0; 3768 return Z_OK; 3769 } 3770 3771 3772 /* Find minimum and maximum length, bound *m by those */ 3773 l = *m; 3774 for (j = 1; j <= BMAX; j++) 3775 if (c[j]) 3776 break; 3777 k = j; /* minimum code length */ 3778 if ((uInt)l < j) 3779 l = j; 3780 for (i = BMAX; i; i--) 3781 if (c[i]) 3782 break; 3783 g = i; /* maximum code length */ 3784 if ((uInt)l > i) 3785 l = i; 3786 *m = l; 3787 3788 3789 /* Adjust last length count to fill out codes, if needed */ 3790 for (y = 1 << j; j < i; j++, y <<= 1) 3791 if ((y -= c[j]) < 0) 3792 return Z_DATA_ERROR; 3793 if ((y -= c[i]) < 0) 3794 return Z_DATA_ERROR; 3795 c[i] += y; 3796 3797 3798 /* Generate starting offsets into the value table for each length */ 3799 x[1] = j = 0; 3800 p = c + 1; xp = x + 2; 3801 while (--i) { /* note that i == g from above */ 3802 *xp++ = (j += *p++); 3803 } 3804 3805 3806 /* Make a table of values in order of bit lengths */ 3807 p = b; i = 0; 3808 do { 3809 if ((j = *p++) != 0) 3810 v[x[j]++] = i; 3811 } while (++i < n); 3812 3813 3814 /* Generate the Huffman codes and for each, make the table entries */ 3815 x[0] = i = 0; /* first Huffman code is zero */ 3816 p = v; /* grab values in bit order */ 3817 h = -1; /* no tables yet--level -1 */ 3818 w = -l; /* bits decoded == (l * h) */ 3819 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */ 3820 q = (inflate_huft *)Z_NULL; /* ditto */ 3821 z = 0; /* ditto */ 3822 3823 /* go through the bit lengths (k already is bits in shortest code) */ 3824 for (; k <= g; k++) 3825 { 3826 a = c[k]; 3827 while (a--) 3828 { 3829 /* here i is the Huffman code of length k bits for value *p */ 3830 /* make tables up to required level */ 3831 while (k > w + l) 3832 { 3833 h++; 3834 w += l; /* previous table always l bits */ 3835 3836 /* compute minimum size table less than or equal to l bits */ 3837 z = (z = g - w) > (uInt)l ? l : z; /* table size upper limit */ 3838 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ 3839 { /* too few codes for k-w bit table */ 3840 f -= a + 1; /* deduct codes from patterns left */ 3841 xp = c + k; 3842 if (j < z) 3843 while (++j < z) /* try smaller tables up to z bits */ 3844 { 3845 if ((f <<= 1) <= *++xp) 3846 break; /* enough codes to use up j bits */ 3847 f -= *xp; /* else deduct codes from patterns */ 3848 } 3849 } 3850 z = 1 << j; /* table entries for j-bit table */ 3851 3852 /* allocate and link in new table */ 3853 if ((q = (inflate_huft *)ZALLOC 3854 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL) 3855 { 3856 if (h) 3857 inflate_trees_free(u[0], zs); 3858 return Z_MEM_ERROR; /* not enough memory */ 3859 } 3860 q->word.Nalloc = z + 1; 3861 #ifdef DEBUG_ZLIB 3862 inflate_hufts += z + 1; 3863 #endif 3864 *t = q + 1; /* link to list for huft_free() */ 3865 *(t = &(q->next)) = Z_NULL; 3866 u[h] = ++q; /* table starts after link */ 3867 3868 /* connect to last table, if there is one */ 3869 if (h) 3870 { 3871 x[h] = i; /* save pattern for backing up */ 3872 r.bits = (Byte)l; /* bits to dump before this table */ 3873 r.exop = (Byte)j; /* bits in this table */ 3874 r.next = q; /* pointer to this table */ 3875 j = i >> (w - l); /* (get around Turbo C bug) */ 3876 u[h-1][j] = r; /* connect to last table */ 3877 } 3878 } 3879 3880 /* set up table entry in r */ 3881 r.bits = (Byte)(k - w); 3882 if (p >= v + n) 3883 r.exop = 128 + 64; /* out of values--invalid code */ 3884 else if (*p < s) 3885 { 3886 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ 3887 r.base = *p++; /* simple code is just the value */ 3888 } 3889 else 3890 { 3891 r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */ 3892 r.base = d[*p++ - s]; 3893 } 3894 3895 /* fill code-like entries with r */ 3896 f = 1 << (k - w); 3897 for (j = i >> w; j < z; j += f) 3898 q[j] = r; 3899 3900 /* backwards increment the k-bit code i */ 3901 for (j = 1 << (k - 1); i & j; j >>= 1) 3902 i ^= j; 3903 i ^= j; 3904 3905 /* backup over finished tables */ 3906 while ((i & ((1 << w) - 1)) != x[h]) 3907 { 3908 h--; /* don't need to update q */ 3909 w -= l; 3910 } 3911 } 3912 } 3913 3914 3915 /* Return Z_BUF_ERROR if we were given an incomplete table */ 3916 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; 3917 } 3918 3919 3920 local int inflate_trees_bits(c, bb, tb, z) 3921 uIntf *c; /* 19 code lengths */ 3922 uIntf *bb; /* bits tree desired/actual depth */ 3923 inflate_huft * FAR *tb; /* bits tree result */ 3924 z_stream *z; /* for zfree function */ 3925 { 3926 int r; 3927 3928 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); 3929 if (r == Z_DATA_ERROR) 3930 z->msg = "oversubscribed dynamic bit lengths tree"; 3931 else if (r == Z_BUF_ERROR) 3932 { 3933 inflate_trees_free(*tb, z); 3934 z->msg = "incomplete dynamic bit lengths tree"; 3935 r = Z_DATA_ERROR; 3936 } 3937 return r; 3938 } 3939 3940 3941 local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z) 3942 uInt nl; /* number of literal/length codes */ 3943 uInt nd; /* number of distance codes */ 3944 uIntf *c; /* that many (total) code lengths */ 3945 uIntf *bl; /* literal desired/actual bit depth */ 3946 uIntf *bd; /* distance desired/actual bit depth */ 3947 inflate_huft * FAR *tl; /* literal/length tree result */ 3948 inflate_huft * FAR *td; /* distance tree result */ 3949 z_stream *z; /* for zfree function */ 3950 { 3951 int r; 3952 3953 /* build literal/length tree */ 3954 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) 3955 { 3956 if (r == Z_DATA_ERROR) 3957 z->msg = "oversubscribed literal/length tree"; 3958 else if (r == Z_BUF_ERROR) 3959 { 3960 inflate_trees_free(*tl, z); 3961 z->msg = "incomplete literal/length tree"; 3962 r = Z_DATA_ERROR; 3963 } 3964 return r; 3965 } 3966 3967 /* build distance tree */ 3968 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) 3969 { 3970 if (r == Z_DATA_ERROR) 3971 z->msg = "oversubscribed literal/length tree"; 3972 else if (r == Z_BUF_ERROR) { 3973 #ifdef PKZIP_BUG_WORKAROUND 3974 r = Z_OK; 3975 } 3976 #else 3977 inflate_trees_free(*td, z); 3978 z->msg = "incomplete literal/length tree"; 3979 r = Z_DATA_ERROR; 3980 } 3981 inflate_trees_free(*tl, z); 3982 return r; 3983 #endif 3984 } 3985 3986 /* done */ 3987 return Z_OK; 3988 } 3989 3990 3991 /* build fixed tables only once--keep them here */ 3992 local int fixed_lock = 0; 3993 local int fixed_built = 0; 3994 #define FIXEDH 530 /* number of hufts used by fixed tables */ 3995 local uInt fixed_left = FIXEDH; 3996 local inflate_huft fixed_mem[FIXEDH]; 3997 local uInt fixed_bl; 3998 local uInt fixed_bd; 3999 local inflate_huft *fixed_tl; 4000 local inflate_huft *fixed_td; 4001 4002 4003 local voidpf falloc(q, n, s) 4004 voidpf q; /* opaque pointer (not used) */ 4005 uInt n; /* number of items */ 4006 uInt s; /* size of item */ 4007 { 4008 Assert(s == sizeof(inflate_huft) && n <= fixed_left, 4009 "inflate_trees falloc overflow"); 4010 if (q) s++; /* to make some compilers happy */ 4011 fixed_left -= n; 4012 return (voidpf)(fixed_mem + fixed_left); 4013 } 4014 4015 4016 local void ffree(q, p, n) 4017 voidpf q; 4018 voidpf p; 4019 uInt n; 4020 { 4021 Assert(0, "inflate_trees ffree called!"); 4022 if (q) q = p; /* to make some compilers happy */ 4023 } 4024 4025 4026 local int inflate_trees_fixed(bl, bd, tl, td) 4027 uIntf *bl; /* literal desired/actual bit depth */ 4028 uIntf *bd; /* distance desired/actual bit depth */ 4029 inflate_huft * FAR *tl; /* literal/length tree result */ 4030 inflate_huft * FAR *td; /* distance tree result */ 4031 { 4032 /* build fixed tables if not built already--lock out other instances */ 4033 while (++fixed_lock > 1) 4034 fixed_lock--; 4035 if (!fixed_built) 4036 { 4037 int k; /* temporary variable */ 4038 unsigned c[288]; /* length list for huft_build */ 4039 z_stream z; /* for falloc function */ 4040 4041 /* set up fake z_stream for memory routines */ 4042 z.zalloc = falloc; 4043 z.zfree = ffree; 4044 z.opaque = Z_NULL; 4045 4046 /* literal table */ 4047 for (k = 0; k < 144; k++) 4048 c[k] = 8; 4049 for (; k < 256; k++) 4050 c[k] = 9; 4051 for (; k < 280; k++) 4052 c[k] = 7; 4053 for (; k < 288; k++) 4054 c[k] = 8; 4055 fixed_bl = 7; 4056 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z); 4057 4058 /* distance table */ 4059 for (k = 0; k < 30; k++) 4060 c[k] = 5; 4061 fixed_bd = 5; 4062 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z); 4063 4064 /* done */ 4065 fixed_built = 1; 4066 } 4067 fixed_lock--; 4068 *bl = fixed_bl; 4069 *bd = fixed_bd; 4070 *tl = fixed_tl; 4071 *td = fixed_td; 4072 return Z_OK; 4073 } 4074 4075 4076 local int inflate_trees_free(t, z) 4077 inflate_huft *t; /* table to free */ 4078 z_stream *z; /* for zfree function */ 4079 /* Free the malloc'ed tables built by huft_build(), which makes a linked 4080 list of the tables it made, with the links in a dummy first entry of 4081 each table. */ 4082 { 4083 register inflate_huft *p, *q; 4084 4085 /* Go through linked list, freeing from the malloced (t[-1]) address. */ 4086 p = t; 4087 while (p != Z_NULL) 4088 { 4089 q = (--p)->next; 4090 ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft)); 4091 p = q; 4092 } 4093 return Z_OK; 4094 } 4095 4096 /*+++++*/ 4097 /* infcodes.c -- process literals and length/distance pairs 4098 * Copyright (C) 1995 Mark Adler 4099 * For conditions of distribution and use, see copyright notice in zlib.h 4100 */ 4101 4102 /* simplify the use of the inflate_huft type with some defines */ 4103 #define base more.Base 4104 #define next more.Next 4105 #define exop word.what.Exop 4106 #define bits word.what.Bits 4107 4108 /* inflate codes private state */ 4109 struct inflate_codes_state { 4110 4111 /* mode */ 4112 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ 4113 START, /* x: set up for LEN */ 4114 LEN, /* i: get length/literal/eob next */ 4115 LENEXT, /* i: getting length extra (have base) */ 4116 DIST, /* i: get distance next */ 4117 DISTEXT, /* i: getting distance extra */ 4118 COPY, /* o: copying bytes in window, waiting for space */ 4119 LIT, /* o: got literal, waiting for output space */ 4120 WASH, /* o: got eob, possibly still output waiting */ 4121 END, /* x: got eob and all data flushed */ 4122 BADCODE} /* x: got error */ 4123 mode; /* current inflate_codes mode */ 4124 4125 /* mode dependent information */ 4126 uInt len; 4127 union { 4128 struct { 4129 inflate_huft *tree; /* pointer into tree */ 4130 uInt need; /* bits needed */ 4131 } code; /* if LEN or DIST, where in tree */ 4132 uInt lit; /* if LIT, literal */ 4133 struct { 4134 uInt get; /* bits to get for extra */ 4135 uInt dist; /* distance back to copy from */ 4136 } copy; /* if EXT or COPY, where and how much */ 4137 } sub; /* submode */ 4138 4139 /* mode independent information */ 4140 Byte lbits; /* ltree bits decoded per branch */ 4141 Byte dbits; /* dtree bits decoder per branch */ 4142 inflate_huft *ltree; /* literal/length/eob tree */ 4143 inflate_huft *dtree; /* distance tree */ 4144 4145 }; 4146 4147 4148 local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z) 4149 uInt bl, bd; 4150 inflate_huft *tl, *td; 4151 z_stream *z; 4152 { 4153 inflate_codes_statef *c; 4154 4155 if ((c = (inflate_codes_statef *) 4156 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) 4157 { 4158 c->mode = START; 4159 c->lbits = (Byte)bl; 4160 c->dbits = (Byte)bd; 4161 c->ltree = tl; 4162 c->dtree = td; 4163 Tracev((stderr, "inflate: codes new\n")); 4164 } 4165 return c; 4166 } 4167 4168 4169 local int inflate_codes(s, z, r) 4170 inflate_blocks_statef *s; 4171 z_stream *z; 4172 int r; 4173 { 4174 uInt j; /* temporary storage */ 4175 inflate_huft *t; /* temporary pointer */ 4176 uInt e; /* extra bits or operation */ 4177 uLong b; /* bit buffer */ 4178 uInt k; /* bits in bit buffer */ 4179 Bytef *p; /* input data pointer */ 4180 uInt n; /* bytes available there */ 4181 Bytef *q; /* output window write pointer */ 4182 uInt m; /* bytes to end of window or read pointer */ 4183 Bytef *f; /* pointer to copy strings from */ 4184 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ 4185 4186 /* copy input/output information to locals (UPDATE macro restores) */ 4187 LOAD 4188 4189 /* process input and output based on current state */ 4190 while (1) switch (c->mode) 4191 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ 4192 case START: /* x: set up for LEN */ 4193 #ifndef SLOW 4194 if (m >= 258 && n >= 10) 4195 { 4196 UPDATE 4197 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); 4198 LOAD 4199 if (r != Z_OK) 4200 { 4201 c->mode = r == Z_STREAM_END ? WASH : BADCODE; 4202 break; 4203 } 4204 } 4205 #endif /* !SLOW */ 4206 c->sub.code.need = c->lbits; 4207 c->sub.code.tree = c->ltree; 4208 c->mode = LEN; 4209 case LEN: /* i: get length/literal/eob next */ 4210 j = c->sub.code.need; 4211 NEEDBITS(j) 4212 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); 4213 DUMPBITS(t->bits) 4214 e = (uInt)(t->exop); 4215 if (e == 0) /* literal */ 4216 { 4217 c->sub.lit = t->base; 4218 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 4219 "inflate: literal '%c'\n" : 4220 "inflate: literal 0x%02x\n", t->base)); 4221 c->mode = LIT; 4222 break; 4223 } 4224 if (e & 16) /* length */ 4225 { 4226 c->sub.copy.get = e & 15; 4227 c->len = t->base; 4228 c->mode = LENEXT; 4229 break; 4230 } 4231 if ((e & 64) == 0) /* next table */ 4232 { 4233 c->sub.code.need = e; 4234 c->sub.code.tree = t->next; 4235 break; 4236 } 4237 if (e & 32) /* end of block */ 4238 { 4239 Tracevv((stderr, "inflate: end of block\n")); 4240 c->mode = WASH; 4241 break; 4242 } 4243 c->mode = BADCODE; /* invalid code */ 4244 z->msg = "invalid literal/length code"; 4245 r = Z_DATA_ERROR; 4246 LEAVE 4247 case LENEXT: /* i: getting length extra (have base) */ 4248 j = c->sub.copy.get; 4249 NEEDBITS(j) 4250 c->len += (uInt)b & inflate_mask[j]; 4251 DUMPBITS(j) 4252 c->sub.code.need = c->dbits; 4253 c->sub.code.tree = c->dtree; 4254 Tracevv((stderr, "inflate: length %u\n", c->len)); 4255 c->mode = DIST; 4256 case DIST: /* i: get distance next */ 4257 j = c->sub.code.need; 4258 NEEDBITS(j) 4259 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); 4260 DUMPBITS(t->bits) 4261 e = (uInt)(t->exop); 4262 if (e & 16) /* distance */ 4263 { 4264 c->sub.copy.get = e & 15; 4265 c->sub.copy.dist = t->base; 4266 c->mode = DISTEXT; 4267 break; 4268 } 4269 if ((e & 64) == 0) /* next table */ 4270 { 4271 c->sub.code.need = e; 4272 c->sub.code.tree = t->next; 4273 break; 4274 } 4275 c->mode = BADCODE; /* invalid code */ 4276 z->msg = "invalid distance code"; 4277 r = Z_DATA_ERROR; 4278 LEAVE 4279 case DISTEXT: /* i: getting distance extra */ 4280 j = c->sub.copy.get; 4281 NEEDBITS(j) 4282 c->sub.copy.dist += (uInt)b & inflate_mask[j]; 4283 DUMPBITS(j) 4284 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); 4285 c->mode = COPY; 4286 case COPY: /* o: copying bytes in window, waiting for space */ 4287 #ifndef __TURBOC__ /* Turbo C bug for following expression */ 4288 f = (uInt)(q - s->window) < c->sub.copy.dist ? 4289 s->end - (c->sub.copy.dist - (q - s->window)) : 4290 q - c->sub.copy.dist; 4291 #else 4292 f = q - c->sub.copy.dist; 4293 if ((uInt)(q - s->window) < c->sub.copy.dist) 4294 f = s->end - (c->sub.copy.dist - (q - s->window)); 4295 #endif 4296 while (c->len) 4297 { 4298 NEEDOUT 4299 OUTBYTE(*f++) 4300 if (f == s->end) 4301 f = s->window; 4302 c->len--; 4303 } 4304 c->mode = START; 4305 break; 4306 case LIT: /* o: got literal, waiting for output space */ 4307 NEEDOUT 4308 OUTBYTE(c->sub.lit) 4309 c->mode = START; 4310 break; 4311 case WASH: /* o: got eob, possibly more output */ 4312 FLUSH 4313 if (s->read != s->write) 4314 LEAVE 4315 c->mode = END; 4316 case END: 4317 r = Z_STREAM_END; 4318 LEAVE 4319 case BADCODE: /* x: got error */ 4320 r = Z_DATA_ERROR; 4321 LEAVE 4322 default: 4323 r = Z_STREAM_ERROR; 4324 LEAVE 4325 } 4326 } 4327 4328 4329 local void inflate_codes_free(c, z) 4330 inflate_codes_statef *c; 4331 z_stream *z; 4332 { 4333 ZFREE(z, c, sizeof(struct inflate_codes_state)); 4334 Tracev((stderr, "inflate: codes free\n")); 4335 } 4336 4337 /*+++++*/ 4338 /* inflate_util.c -- data and routines common to blocks and codes 4339 * Copyright (C) 1995 Mark Adler 4340 * For conditions of distribution and use, see copyright notice in zlib.h 4341 */ 4342 4343 /* copy as much as possible from the sliding window to the output area */ 4344 local int inflate_flush(s, z, r) 4345 inflate_blocks_statef *s; 4346 z_stream *z; 4347 int r; 4348 { 4349 uInt n; 4350 Bytef *p, *q; 4351 4352 /* local copies of source and destination pointers */ 4353 p = z->next_out; 4354 q = s->read; 4355 4356 /* compute number of bytes to copy as far as end of window */ 4357 n = (uInt)((q <= s->write ? s->write : s->end) - q); 4358 if (n > z->avail_out) n = z->avail_out; 4359 if (n && r == Z_BUF_ERROR) r = Z_OK; 4360 4361 /* update counters */ 4362 z->avail_out -= n; 4363 z->total_out += n; 4364 4365 /* update check information */ 4366 if (s->checkfn != Z_NULL) 4367 s->check = (*s->checkfn)(s->check, q, n); 4368 4369 /* copy as far as end of window */ 4370 if (p != NULL) { 4371 zmemcpy(p, q, n); 4372 p += n; 4373 } 4374 q += n; 4375 4376 /* see if more to copy at beginning of window */ 4377 if (q == s->end) 4378 { 4379 /* wrap pointers */ 4380 q = s->window; 4381 if (s->write == s->end) 4382 s->write = s->window; 4383 4384 /* compute bytes to copy */ 4385 n = (uInt)(s->write - q); 4386 if (n > z->avail_out) n = z->avail_out; 4387 if (n && r == Z_BUF_ERROR) r = Z_OK; 4388 4389 /* update counters */ 4390 z->avail_out -= n; 4391 z->total_out += n; 4392 4393 /* update check information */ 4394 if (s->checkfn != Z_NULL) 4395 s->check = (*s->checkfn)(s->check, q, n); 4396 4397 /* copy */ 4398 if (p != NULL) { 4399 zmemcpy(p, q, n); 4400 p += n; 4401 } 4402 q += n; 4403 } 4404 4405 /* update pointers */ 4406 z->next_out = p; 4407 s->read = q; 4408 4409 /* done */ 4410 return r; 4411 } 4412 4413 4414 /*+++++*/ 4415 /* inffast.c -- process literals and length/distance pairs fast 4416 * Copyright (C) 1995 Mark Adler 4417 * For conditions of distribution and use, see copyright notice in zlib.h 4418 */ 4419 4420 /* simplify the use of the inflate_huft type with some defines */ 4421 #define base more.Base 4422 #define next more.Next 4423 #define exop word.what.Exop 4424 #define bits word.what.Bits 4425 4426 /* macros for bit input with no checking and for returning unused bytes */ 4427 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}} 4428 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;} 4429 4430 /* Called with number of bytes left to write in window at least 258 4431 (the maximum string length) and number of input bytes available 4432 at least ten. The ten bytes are six bytes for the longest length/ 4433 distance pair plus four bytes for overloading the bit buffer. */ 4434 4435 local int inflate_fast(bl, bd, tl, td, s, z) 4436 uInt bl, bd; 4437 inflate_huft *tl, *td; 4438 inflate_blocks_statef *s; 4439 z_stream *z; 4440 { 4441 inflate_huft *t; /* temporary pointer */ 4442 uInt e; /* extra bits or operation */ 4443 uLong b; /* bit buffer */ 4444 uInt k; /* bits in bit buffer */ 4445 Bytef *p; /* input data pointer */ 4446 uInt n; /* bytes available there */ 4447 Bytef *q; /* output window write pointer */ 4448 uInt m; /* bytes to end of window or read pointer */ 4449 uInt ml; /* mask for literal/length tree */ 4450 uInt md; /* mask for distance tree */ 4451 uInt c; /* bytes to copy */ 4452 uInt d; /* distance back to copy from */ 4453 Bytef *r; /* copy source pointer */ 4454 4455 /* load input, output, bit values */ 4456 LOAD 4457 4458 /* initialize masks */ 4459 ml = inflate_mask[bl]; 4460 md = inflate_mask[bd]; 4461 4462 /* do until not enough input or output space for fast loop */ 4463 do { /* assume called with m >= 258 && n >= 10 */ 4464 /* get literal/length code */ 4465 GRABBITS(20) /* max bits for literal/length code */ 4466 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) 4467 { 4468 DUMPBITS(t->bits) 4469 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 4470 "inflate: * literal '%c'\n" : 4471 "inflate: * literal 0x%02x\n", t->base)); 4472 *q++ = (Byte)t->base; 4473 m--; 4474 continue; 4475 } 4476 do { 4477 DUMPBITS(t->bits) 4478 if (e & 16) 4479 { 4480 /* get extra bits for length */ 4481 e &= 15; 4482 c = t->base + ((uInt)b & inflate_mask[e]); 4483 DUMPBITS(e) 4484 Tracevv((stderr, "inflate: * length %u\n", c)); 4485 4486 /* decode distance base of block to copy */ 4487 GRABBITS(15); /* max bits for distance code */ 4488 e = (t = td + ((uInt)b & md))->exop; 4489 do { 4490 DUMPBITS(t->bits) 4491 if (e & 16) 4492 { 4493 /* get extra bits to add to distance base */ 4494 e &= 15; 4495 GRABBITS(e) /* get extra bits (up to 13) */ 4496 d = t->base + ((uInt)b & inflate_mask[e]); 4497 DUMPBITS(e) 4498 Tracevv((stderr, "inflate: * distance %u\n", d)); 4499 4500 /* do the copy */ 4501 m -= c; 4502 if ((uInt)(q - s->window) >= d) /* offset before dest */ 4503 { /* just copy */ 4504 r = q - d; 4505 *q++ = *r++; c--; /* minimum count is three, */ 4506 *q++ = *r++; c--; /* so unroll loop a little */ 4507 } 4508 else /* else offset after destination */ 4509 { 4510 e = d - (q - s->window); /* bytes from offset to end */ 4511 r = s->end - e; /* pointer to offset */ 4512 if (c > e) /* if source crosses, */ 4513 { 4514 c -= e; /* copy to end of window */ 4515 do { 4516 *q++ = *r++; 4517 } while (--e); 4518 r = s->window; /* copy rest from start of window */ 4519 } 4520 } 4521 do { /* copy all or what's left */ 4522 *q++ = *r++; 4523 } while (--c); 4524 break; 4525 } 4526 else if ((e & 64) == 0) 4527 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop; 4528 else 4529 { 4530 z->msg = "invalid distance code"; 4531 UNGRAB 4532 UPDATE 4533 return Z_DATA_ERROR; 4534 } 4535 } while (1); 4536 break; 4537 } 4538 if ((e & 64) == 0) 4539 { 4540 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0) 4541 { 4542 DUMPBITS(t->bits) 4543 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? 4544 "inflate: * literal '%c'\n" : 4545 "inflate: * literal 0x%02x\n", t->base)); 4546 *q++ = (Byte)t->base; 4547 m--; 4548 break; 4549 } 4550 } 4551 else if (e & 32) 4552 { 4553 Tracevv((stderr, "inflate: * end of block\n")); 4554 UNGRAB 4555 UPDATE 4556 return Z_STREAM_END; 4557 } 4558 else 4559 { 4560 z->msg = "invalid literal/length code"; 4561 UNGRAB 4562 UPDATE 4563 return Z_DATA_ERROR; 4564 } 4565 } while (1); 4566 } while (m >= 258 && n >= 10); 4567 4568 /* not enough input or output--restore pointers and return */ 4569 UNGRAB 4570 UPDATE 4571 return Z_OK; 4572 } 4573 4574 4575 /*+++++*/ 4576 /* zutil.c -- target dependent utility functions for the compression library 4577 * Copyright (C) 1995 Jean-loup Gailly. 4578 * For conditions of distribution and use, see copyright notice in zlib.h 4579 */ 4580 4581 /* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */ 4582 4583 char *zlib_version = ZLIB_VERSION; 4584 4585 char *z_errmsg[] = { 4586 "stream end", /* Z_STREAM_END 1 */ 4587 "", /* Z_OK 0 */ 4588 "file error", /* Z_ERRNO (-1) */ 4589 "stream error", /* Z_STREAM_ERROR (-2) */ 4590 "data error", /* Z_DATA_ERROR (-3) */ 4591 "insufficient memory", /* Z_MEM_ERROR (-4) */ 4592 "buffer error", /* Z_BUF_ERROR (-5) */ 4593 ""}; 4594 4595 4596 /*+++++*/ 4597 /* adler32.c -- compute the Adler-32 checksum of a data stream 4598 * Copyright (C) 1995 Mark Adler 4599 * For conditions of distribution and use, see copyright notice in zlib.h 4600 */ 4601 4602 /* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */ 4603 4604 #define BASE 65521L /* largest prime smaller than 65536 */ 4605 #define NMAX 5552 4606 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 4607 4608 #define DO1(buf) {s1 += *buf++; s2 += s1;} 4609 #define DO2(buf) DO1(buf); DO1(buf); 4610 #define DO4(buf) DO2(buf); DO2(buf); 4611 #define DO8(buf) DO4(buf); DO4(buf); 4612 #define DO16(buf) DO8(buf); DO8(buf); 4613 4614 /* ========================================================================= */ 4615 uLong adler32(adler, buf, len) 4616 uLong adler; 4617 Bytef *buf; 4618 uInt len; 4619 { 4620 unsigned long s1 = adler & 0xffff; 4621 unsigned long s2 = (adler >> 16) & 0xffff; 4622 int k; 4623 4624 if (buf == Z_NULL) return 1L; 4625 4626 while (len > 0) { 4627 k = len < NMAX ? len : NMAX; 4628 len -= k; 4629 while (k >= 16) { 4630 DO16(buf); 4631 k -= 16; 4632 } 4633 if (k != 0) do { 4634 DO1(buf); 4635 } while (--k); 4636 s1 %= BASE; 4637 s2 %= BASE; 4638 } 4639 return (s2 << 16) | s1; 4640 } 4641