|
|
1.1 ! root 1: /* inflate.c -- Not copyrighted 1992 by Mark Adler ! 2: version c10p1, 10 January 1993 */ ! 3: ! 4: ! 5: /* You can do whatever you like with this source file, though I would ! 6: prefer that if you modify it and redistribute it that you include ! 7: comments to that effect with your name and the date. Thank you. ! 8: ! 9: History: ! 10: vers date who what ! 11: ---- --------- -------------- ------------------------------------ ! 12: a ~~ Feb 92 M. Adler used full (large, one-step) lookup table ! 13: b1 21 Mar 92 M. Adler first version with partial lookup tables ! 14: b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks ! 15: b3 22 Mar 92 M. Adler sped up match copies, cleaned up some ! 16: b4 25 Mar 92 M. Adler added prototypes; removed window[] (now ! 17: is the responsibility of unzip.h--also ! 18: changed name to slide[]), so needs diffs ! 19: for unzip.c and unzip.h (this allows ! 20: compiling in the small model on MSDOS); ! 21: fixed cast of q in huft_build(); ! 22: b5 26 Mar 92 M. Adler got rid of unintended macro recursion. ! 23: b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed ! 24: bug in inflate_fixed(). ! 25: c1 30 Mar 92 M. Adler removed lbits, dbits environment variables. ! 26: changed BMAX to 16 for explode. Removed ! 27: OUTB usage, and replaced it with flush()-- ! 28: this was a 20% speed improvement! Added ! 29: an explode.c (to replace unimplode.c) that ! 30: uses the huft routines here. Removed ! 31: register union. ! 32: c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k. ! 33: c3 10 Apr 92 M. Adler reduced memory of code tables made by ! 34: huft_build significantly (factor of two to ! 35: three). ! 36: c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy(). ! 37: worked around a Turbo C optimization bug. ! 38: c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing ! 39: the 32K window size for specialized ! 40: applications. ! 41: c6 31 May 92 M. Adler added some typecasts to eliminate warnings ! 42: c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug). ! 43: c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug. ! 44: c9 9 Oct 92 M. Adler removed a memory error message (~line 416). ! 45: c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch, ! 46: removed old inflate, renamed inflate_entry ! 47: to inflate, added Mark's fix to a comment. ! 48: c10p1 10 Jan 93 G. Roelofs version c10 plus Mark's c13 patch: ! 49: [c13] M. Adler allow empty code sets in huft_build (the ! 50: new pkz204c.exe file has a null distance ! 51: tree for the file pkzip.exe) ! 52: */ ! 53: ! 54: ! 55: /* ! 56: Inflate deflated (PKZIP's method 8 compressed) data. The compression ! 57: method searches for as much of the current string of bytes (up to a ! 58: length of 258) in the previous 32K bytes. If it doesn't find any ! 59: matches (of at least length 3), it codes the next byte. Otherwise, it ! 60: codes the length of the matched string and its distance backwards from ! 61: the current position. There is a single Huffman code that codes both ! 62: single bytes (called "literals") and match lengths. A second Huffman ! 63: code codes the distance information, which follows a length code. Each ! 64: length or distance code actually represents a base value and a number ! 65: of "extra" (sometimes zero) bits to get to add to the base value. At ! 66: the end of each deflated block is a special end-of-block (EOB) literal/ ! 67: length code. The decoding process is basically: get a literal/length ! 68: code; if EOB then done; if a literal, emit the decoded byte; if a ! 69: length then get the distance and emit the referred-to bytes from the ! 70: sliding window of previously emitted data. ! 71: ! 72: There are (currently) three kinds of inflate blocks: stored, fixed, and ! 73: dynamic. The compressor outputs a chunk of data at a time, and decides ! 74: which method to use on a chunk-by-chunk basis. A chunk might typically ! 75: be 32K to 64K, uncompressed. If the chunk is uncompressible, then the ! 76: "stored" method is used. In this case, the bytes are simply stored as ! 77: is, eight bits per byte, with none of the above coding. The bytes are ! 78: preceded by a count, since there is no longer an EOB code. ! 79: ! 80: If the data is compressible, then either the fixed or dynamic methods ! 81: are used. In the dynamic method, the compressed data is preceded by ! 82: an encoding of the literal/length and distance Huffman codes that are ! 83: to be used to decode this block. The representation is itself Huffman ! 84: coded, and so is preceded by a description of that code. These code ! 85: descriptions take up a little space, and so for small blocks, there is ! 86: a predefined set of codes, called the fixed codes. The fixed method is ! 87: used if the block ends up smaller that way (usually for quite small ! 88: chunks), otherwise the dynamic method is used. In the latter case, the ! 89: codes are customized to the probabilities in the current block, and so ! 90: can code it much better than the pre-determined fixed codes can. ! 91: ! 92: The Huffman codes themselves are decoded using a mutli-level table ! 93: lookup, in order to maximize the speed of decoding plus the speed of ! 94: building the decoding tables. See the comments below that precede the ! 95: lbits and dbits tuning parameters. ! 96: */ ! 97: ! 98: ! 99: /* ! 100: Notes beyond the 1.93a appnote.txt: ! 101: ! 102: 1. Distance pointers never point before the beginning of the output ! 103: stream. ! 104: 2. Distance pointers can point back across blocks, up to 32k away. ! 105: 3. There is an implied maximum of 7 bits for the bit length table and ! 106: 15 bits for the actual data. ! 107: 4. If only one code exists, then it is encoded using one bit. (Zero ! 108: would be more efficient, but perhaps a little confusing.) If two ! 109: codes exist, they are coded using one bit each (0 and 1). ! 110: 5. There is no way of sending zero distance codes--a dummy must be ! 111: sent if there are none. (History: a pre 2.0 version of PKZIP would ! 112: store blocks with no distance codes, but this was discovered to be ! 113: too harsh a criterion.) Valid only for 1.93a. 2.04c does allow ! 114: zero distance codes, which is sent as one code of zero bits in ! 115: length. ! 116: 6. There are up to 286 literal/length codes. Code 256 represents the ! 117: end-of-block. Note however that the static length tree defines ! 118: 288 codes just to fill out the Huffman codes. Codes 286 and 287 ! 119: cannot be used though, since there is no length base or extra bits ! 120: defined for them. Similarily, there are up to 30 distance codes. ! 121: However, static trees define 32 codes (all 5 bits) to fill out the ! 122: Huffman codes, but the last two had better not show up in the data. ! 123: 7. Unzip can check dynamic Huffman blocks for complete code sets. ! 124: The exception is that a single code would not be complete (see #4). ! 125: 8. The five bits following the block type is really the number of ! 126: literal codes sent minus 257. ! 127: 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits ! 128: (1+6+6). Therefore, to output three times the length, you output ! 129: three codes (1+1+1), whereas to output four times the same length, ! 130: you only need two codes (1+3). Hmm. ! 131: 10. In the tree reconstruction algorithm, Code = Code + Increment ! 132: only if BitLength(i) is not zero. (Pretty obvious.) ! 133: 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) ! 134: 12. Note: length code 284 can represent 227-258, but length code 285 ! 135: really is 258. The last length deserves its own, short code ! 136: since it gets used a lot in very redundant files. The length ! 137: 258 is special since 258 - 3 (the min match length) is 255. ! 138: 13. The literal/length and distance code bit lengths are read as a ! 139: single stream of lengths. It is possible (and advantageous) for ! 140: a repeat code (16, 17, or 18) to go across the boundary between ! 141: the two sets of lengths. ! 142: */ ! 143: ! 144: #include "unzip.h" /* this must supply the slide[] (uch) array */ ! 145: ! 146: #ifndef WSIZE ! 147: # define WSIZE 0x8000 /* window size--must be a power of two, and at least ! 148: 32K for zip's deflate method */ ! 149: #endif /* !WSIZE */ ! 150: ! 151: #ifdef DEBUG ! 152: # define Trace(x) fprintf x ! 153: #else ! 154: # define Trace(x) ! 155: #endif ! 156: ! 157: ! 158: /* Huffman code lookup table entry--this entry is four bytes for machines ! 159: that have 16-bit pointers (e.g. PC's in the small or medium model). ! 160: Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 ! 161: means that v is a literal, 16 < e < 32 means that v is a pointer to ! 162: the next table, which codes e - 16 bits, and lastly e == 99 indicates ! 163: an unused code. If a code with e == 99 is looked up, this implies an ! 164: error in the data. */ ! 165: struct huft { ! 166: uch e; /* number of extra bits or operation */ ! 167: uch b; /* number of bits in this code or subcode */ ! 168: union { ! 169: ush n; /* literal, length base, or distance base */ ! 170: struct huft *t; /* pointer to next level of table */ ! 171: } v; ! 172: }; ! 173: ! 174: ! 175: /* Function prototypes */ ! 176: int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *, ! 177: struct huft **, int *)); ! 178: int huft_free OF((struct huft *)); ! 179: void flush OF((unsigned)); ! 180: int inflate_codes OF((struct huft *, struct huft *, int, int)); ! 181: int inflate_stored OF((void)); ! 182: int inflate_fixed OF((void)); ! 183: int inflate_dynamic OF((void)); ! 184: int inflate_block OF((int *)); ! 185: int inflate OF((void)); ! 186: ! 187: ! 188: /* The inflate algorithm uses a sliding 32K byte window on the uncompressed ! 189: stream to find repeated byte strings. This is implemented here as a ! 190: circular buffer. The index is updated simply by incrementing and then ! 191: and'ing with 0x7fff (32K-1). */ ! 192: /* It is left to other modules to supply the 32K area. It is assumed ! 193: to be usable as if it were declared "uch slide[32768];" or as just ! 194: "uch *slide;" and then malloc'ed in the latter case. The definition ! 195: must be in unzip.h, included above. */ ! 196: unsigned wp; /* current position in slide */ ! 197: ! 198: ! 199: /* Tables for deflate from PKZIP's appnote.txt. */ ! 200: static unsigned border[] = { /* Order of the bit length code lengths */ ! 201: 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; ! 202: static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ ! 203: 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, ! 204: 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; ! 205: /* note: see note #13 above about the 258 in this list. */ ! 206: static ush cplext[] = { /* Extra bits for literal codes 257..285 */ ! 207: 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, ! 208: 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ ! 209: static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ ! 210: 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, ! 211: 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, ! 212: 8193, 12289, 16385, 24577}; ! 213: static ush cpdext[] = { /* Extra bits for distance codes */ ! 214: 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, ! 215: 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, ! 216: 12, 12, 13, 13}; ! 217: ! 218: ! 219: ! 220: /* Macros for inflate() bit peeking and grabbing. ! 221: The usage is: ! 222: ! 223: NEEDBITS(j) ! 224: x = b & mask_bits[j]; ! 225: DUMPBITS(j) ! 226: ! 227: where NEEDBITS makes sure that b has at least j bits in it, and ! 228: DUMPBITS removes the bits from b. The macros use the variable k ! 229: for the number of bits in b. Normally, b and k are register ! 230: variables for speed, and are initialized at the begining of a ! 231: routine that uses these macros from a global bit buffer and count. ! 232: ! 233: If we assume that EOB will be the longest code, then we will never ! 234: ask for bits with NEEDBITS that are beyond the end of the stream. ! 235: So, NEEDBITS should not read any more bytes than are needed to ! 236: meet the request. Then no bytes need to be "returned" to the buffer ! 237: at the end of the last block. ! 238: ! 239: However, this assumption is not true for fixed blocks--the EOB code ! 240: is 7 bits, but the other literal/length codes can be 8 or 9 bits. ! 241: (The EOB code is shorter than other codes becuase fixed blocks are ! 242: generally short. So, while a block always has an EOB, many other ! 243: literal/length codes have a significantly lower probability of ! 244: showing up at all.) However, by making the first table have a ! 245: lookup of seven bits, the EOB code will be found in that first ! 246: lookup, and so will not require that too many bits be pulled from ! 247: the stream. ! 248: */ ! 249: ! 250: ulg bb; /* bit buffer */ ! 251: unsigned bk; /* bits in bit buffer */ ! 252: ! 253: ush bytebuf; ! 254: #define NEXTBYTE (ReadByte(&bytebuf), bytebuf) ! 255: #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} ! 256: #define DUMPBITS(n) {b>>=(n);k-=(n);} ! 257: ! 258: ! 259: /* ! 260: Huffman code decoding is performed using a multi-level table lookup. ! 261: The fastest way to decode is to simply build a lookup table whose ! 262: size is determined by the longest code. However, the time it takes ! 263: to build this table can also be a factor if the data being decoded ! 264: is not very long. The most common codes are necessarily the ! 265: shortest codes, so those codes dominate the decoding time, and hence ! 266: the speed. The idea is you can have a shorter table that decodes the ! 267: shorter, more probable codes, and then point to subsidiary tables for ! 268: the longer codes. The time it costs to decode the longer codes is ! 269: then traded against the time it takes to make longer tables. ! 270: ! 271: This results of this trade are in the variables lbits and dbits ! 272: below. lbits is the number of bits the first level table for literal/ ! 273: length codes can decode in one step, and dbits is the same thing for ! 274: the distance codes. Subsequent tables are also less than or equal to ! 275: those sizes. These values may be adjusted either when all of the ! 276: codes are shorter than that, in which case the longest code length in ! 277: bits is used, or when the shortest code is *longer* than the requested ! 278: table size, in which case the length of the shortest code in bits is ! 279: used. ! 280: ! 281: There are two different values for the two tables, since they code a ! 282: different number of possibilities each. The literal/length table ! 283: codes 286 possible values, or in a flat code, a little over eight ! 284: bits. The distance table codes 30 possible values, or a little less ! 285: than five bits, flat. The optimum values for speed end up being ! 286: about one bit more than those, so lbits is 8+1 and dbits is 5+1. ! 287: The optimum values may differ though from machine to machine, and ! 288: possibly even between compilers. Your mileage may vary. ! 289: */ ! 290: ! 291: ! 292: int lbits = 9; /* bits in base literal/length lookup table */ ! 293: int dbits = 6; /* bits in base distance lookup table */ ! 294: ! 295: ! 296: /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ ! 297: #define BMAX 16 /* maximum bit length of any code (16 for explode) */ ! 298: #define N_MAX 288 /* maximum number of codes in any set */ ! 299: ! 300: ! 301: unsigned hufts; /* track memory usage */ ! 302: ! 303: ! 304: int huft_build(b, n, s, d, e, t, m) ! 305: unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ ! 306: unsigned n; /* number of codes (assumed <= N_MAX) */ ! 307: unsigned s; /* number of simple-valued codes (0..s-1) */ ! 308: ush *d; /* list of base values for non-simple codes */ ! 309: ush *e; /* list of extra bits for non-simple codes */ ! 310: struct huft **t; /* result: starting table */ ! 311: int *m; /* maximum lookup bits, returns actual */ ! 312: /* Given a list of code lengths and a maximum table size, make a set of ! 313: tables to decode that set of codes. Return zero on success, one if ! 314: the given code set is incomplete (the tables are still built in this ! 315: case), two if the input is invalid (all zero length codes or an ! 316: oversubscribed set of lengths), and three if not enough memory. */ ! 317: { ! 318: unsigned a; /* counter for codes of length k */ ! 319: unsigned c[BMAX+1]; /* bit length count table */ ! 320: unsigned f; /* i repeats in table every f entries */ ! 321: int g; /* maximum code length */ ! 322: int h; /* table level */ ! 323: register unsigned i; /* counter, current code */ ! 324: register unsigned j; /* counter */ ! 325: register int k; /* number of bits in current code */ ! 326: int l; /* bits per table (returned in m) */ ! 327: register unsigned *p; /* pointer into c[], b[], or v[] */ ! 328: register struct huft *q; /* points to current table */ ! 329: struct huft r; /* table entry for structure assignment */ ! 330: struct huft *u[BMAX]; /* table stack */ ! 331: unsigned v[N_MAX]; /* values in order of bit length */ ! 332: register int w; /* bits before this table == (l * h) */ ! 333: unsigned x[BMAX+1]; /* bit offsets, then code stack */ ! 334: unsigned *xp; /* pointer into x */ ! 335: int y; /* number of dummy codes added */ ! 336: unsigned z; /* number of entries in current table */ ! 337: ! 338: ! 339: /* Generate counts for each bit length */ ! 340: memset(c, 0, sizeof(c)); ! 341: p = b; i = n; ! 342: do { ! 343: c[*p++]++; /* assume all entries <= BMAX */ ! 344: } while (--i); ! 345: if (c[0] == n) /* null input--all zero length codes */ ! 346: { ! 347: *t = (struct huft *)NULL; ! 348: *m = 0; ! 349: return 0; ! 350: } ! 351: ! 352: ! 353: /* Find minimum and maximum length, bound *m by those */ ! 354: l = *m; ! 355: for (j = 1; j <= BMAX; j++) ! 356: if (c[j]) ! 357: break; ! 358: k = j; /* minimum code length */ ! 359: if ((unsigned)l < j) ! 360: l = j; ! 361: for (i = BMAX; i; i--) ! 362: if (c[i]) ! 363: break; ! 364: g = i; /* maximum code length */ ! 365: if ((unsigned)l > i) ! 366: l = i; ! 367: *m = l; ! 368: ! 369: ! 370: /* Adjust last length count to fill out codes, if needed */ ! 371: for (y = 1 << j; j < i; j++, y <<= 1) ! 372: if ((y -= c[j]) < 0) ! 373: return 2; /* bad input: more codes than bits */ ! 374: if ((y -= c[i]) < 0) ! 375: return 2; ! 376: c[i] += y; ! 377: ! 378: ! 379: /* Generate starting offsets into the value table for each length */ ! 380: x[1] = j = 0; ! 381: p = c + 1; xp = x + 2; ! 382: while (--i) { /* note that i == g from above */ ! 383: *xp++ = (j += *p++); ! 384: } ! 385: ! 386: ! 387: /* Make a table of values in order of bit lengths */ ! 388: p = b; i = 0; ! 389: do { ! 390: if ((j = *p++) != 0) ! 391: v[x[j]++] = i; ! 392: } while (++i < n); ! 393: ! 394: ! 395: /* Generate the Huffman codes and for each, make the table entries */ ! 396: x[0] = i = 0; /* first Huffman code is zero */ ! 397: p = v; /* grab values in bit order */ ! 398: h = -1; /* no tables yet--level -1 */ ! 399: w = -l; /* bits decoded == (l * h) */ ! 400: u[0] = (struct huft *)NULL; /* just to keep compilers happy */ ! 401: q = (struct huft *)NULL; /* ditto */ ! 402: z = 0; /* ditto */ ! 403: ! 404: /* go through the bit lengths (k already is bits in shortest code) */ ! 405: for (; k <= g; k++) ! 406: { ! 407: a = c[k]; ! 408: while (a--) ! 409: { ! 410: /* here i is the Huffman code of length k bits for value *p */ ! 411: /* make tables up to required level */ ! 412: while (k > w + l) ! 413: { ! 414: h++; ! 415: w += l; /* previous table always l bits */ ! 416: ! 417: /* compute minimum size table less than or equal to l bits */ ! 418: z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ ! 419: if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ ! 420: { /* too few codes for k-w bit table */ ! 421: f -= a + 1; /* deduct codes from patterns left */ ! 422: xp = c + k; ! 423: while (++j < z) /* try smaller tables up to z bits */ ! 424: { ! 425: if ((f <<= 1) <= *++xp) ! 426: break; /* enough codes to use up j bits */ ! 427: f -= *xp; /* else deduct codes from patterns */ ! 428: } ! 429: } ! 430: z = 1 << j; /* table entries for j-bit table */ ! 431: ! 432: /* allocate and link in new table */ ! 433: if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == ! 434: (struct huft *)NULL) ! 435: { ! 436: if (h) ! 437: huft_free(u[0]); ! 438: return 3; /* not enough memory */ ! 439: } ! 440: hufts += z + 1; /* track memory usage */ ! 441: *t = q + 1; /* link to list for huft_free() */ ! 442: *(t = &(q->v.t)) = (struct huft *)NULL; ! 443: u[h] = ++q; /* table starts after link */ ! 444: ! 445: /* connect to last table, if there is one */ ! 446: if (h) ! 447: { ! 448: x[h] = i; /* save pattern for backing up */ ! 449: r.b = (uch)l; /* bits to dump before this table */ ! 450: r.e = (uch)(16 + j); /* bits in this table */ ! 451: r.v.t = q; /* pointer to this table */ ! 452: j = i >> (w - l); /* (get around Turbo C bug) */ ! 453: u[h-1][j] = r; /* connect to last table */ ! 454: } ! 455: } ! 456: ! 457: /* set up table entry in r */ ! 458: r.b = (uch)(k - w); ! 459: if (p >= v + n) ! 460: r.e = 99; /* out of values--invalid code */ ! 461: else if (*p < s) ! 462: { ! 463: r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ ! 464: r.v.n = *p++; /* simple code is just the value */ ! 465: } ! 466: else ! 467: { ! 468: r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ ! 469: r.v.n = d[*p++ - s]; ! 470: } ! 471: ! 472: /* fill code-like entries with r */ ! 473: f = 1 << (k - w); ! 474: for (j = i >> w; j < z; j += f) ! 475: q[j] = r; ! 476: ! 477: /* backwards increment the k-bit code i */ ! 478: for (j = 1 << (k - 1); i & j; j >>= 1) ! 479: i ^= j; ! 480: i ^= j; ! 481: ! 482: /* backup over finished tables */ ! 483: while ((i & ((1 << w) - 1)) != x[h]) ! 484: { ! 485: h--; /* don't need to update q */ ! 486: w -= l; ! 487: } ! 488: } ! 489: } ! 490: ! 491: ! 492: /* Return true (1) if we were given an incomplete table */ ! 493: return y != 0 && g != 1; ! 494: } ! 495: ! 496: ! 497: ! 498: int huft_free(t) ! 499: struct huft *t; /* table to free */ ! 500: /* Free the malloc'ed tables built by huft_build(), which makes a linked ! 501: list of the tables it made, with the links in a dummy first entry of ! 502: each table. */ ! 503: { ! 504: register struct huft *p, *q; ! 505: ! 506: ! 507: /* Go through linked list, freeing from the malloced (t[-1]) address. */ ! 508: p = t; ! 509: while (p != (struct huft *)NULL) ! 510: { ! 511: q = (--p)->v.t; ! 512: free(p); ! 513: p = q; ! 514: } ! 515: return 0; ! 516: } ! 517: ! 518: ! 519: ! 520: void flush(w) ! 521: unsigned w; /* number of bytes to flush */ ! 522: /* Do the equivalent of OUTB for the bytes slide[0..w-1]. */ ! 523: { ! 524: unsigned n; ! 525: uch *p; ! 526: ! 527: p = slide; ! 528: while (w) ! 529: { ! 530: n = (n = OUTBUFSIZ - outcnt) < w ? n : w; ! 531: memcpy(outptr, p, n); /* try to fill up buffer */ ! 532: outptr += n; ! 533: if ((outcnt += n) == OUTBUFSIZ) ! 534: FlushOutput(); /* if full, empty */ ! 535: p += n; ! 536: w -= n; ! 537: } ! 538: } ! 539: ! 540: ! 541: ! 542: int inflate_codes(tl, td, bl, bd) ! 543: struct huft *tl, *td; /* literal/length and distance decoder tables */ ! 544: int bl, bd; /* number of bits decoded by tl[] and td[] */ ! 545: /* inflate (decompress) the codes in a deflated (compressed) block. ! 546: Return an error code or zero if it all goes ok. */ ! 547: { ! 548: register unsigned e; /* table entry flag/number of extra bits */ ! 549: unsigned n, d; /* length and index for copy */ ! 550: unsigned w; /* current window position */ ! 551: struct huft *t; /* pointer to table entry */ ! 552: unsigned ml, md; /* masks for bl and bd bits */ ! 553: register ulg b; /* bit buffer */ ! 554: register unsigned k; /* number of bits in bit buffer */ ! 555: ! 556: ! 557: /* make local copies of globals */ ! 558: b = bb; /* initialize bit buffer */ ! 559: k = bk; ! 560: w = wp; /* initialize window position */ ! 561: ! 562: ! 563: /* inflate the coded data */ ! 564: ml = mask_bits[bl]; /* precompute masks for speed */ ! 565: md = mask_bits[bd]; ! 566: while (1) /* do until end of block */ ! 567: { ! 568: NEEDBITS((unsigned)bl) ! 569: if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) ! 570: do { ! 571: if (e == 99) ! 572: return 1; ! 573: DUMPBITS(t->b) ! 574: e -= 16; ! 575: NEEDBITS(e) ! 576: } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); ! 577: DUMPBITS(t->b) ! 578: if (e == 16) /* then it's a literal */ ! 579: { ! 580: slide[w++] = (uch)t->v.n; ! 581: if (w == WSIZE) ! 582: { ! 583: flush(w); ! 584: w = 0; ! 585: } ! 586: } ! 587: else /* it's an EOB or a length */ ! 588: { ! 589: /* exit if end of block */ ! 590: if (e == 15) ! 591: break; ! 592: ! 593: /* get length of block to copy */ ! 594: NEEDBITS(e) ! 595: n = t->v.n + ((unsigned)b & mask_bits[e]); ! 596: DUMPBITS(e); ! 597: ! 598: /* decode distance of block to copy */ ! 599: NEEDBITS((unsigned)bd) ! 600: if ((e = (t = td + ((unsigned)b & md))->e) > 16) ! 601: do { ! 602: if (e == 99) ! 603: return 1; ! 604: DUMPBITS(t->b) ! 605: e -= 16; ! 606: NEEDBITS(e) ! 607: } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); ! 608: DUMPBITS(t->b) ! 609: NEEDBITS(e) ! 610: d = w - t->v.n - ((unsigned)b & mask_bits[e]); ! 611: DUMPBITS(e) ! 612: ! 613: /* do the copy */ ! 614: do { ! 615: n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); ! 616: #ifndef NOMEMCPY ! 617: if (w - d >= e) /* (this test assumes unsigned comparison) */ ! 618: { ! 619: memcpy(slide + w, slide + d, e); ! 620: w += e; ! 621: d += e; ! 622: } ! 623: else /* do it slow to avoid memcpy() overlap */ ! 624: #endif /* !NOMEMCPY */ ! 625: do { ! 626: slide[w++] = slide[d++]; ! 627: } while (--e); ! 628: if (w == WSIZE) ! 629: { ! 630: flush(w); ! 631: w = 0; ! 632: } ! 633: } while (n); ! 634: } ! 635: } ! 636: ! 637: ! 638: /* restore the globals from the locals */ ! 639: wp = w; /* restore global window pointer */ ! 640: bb = b; /* restore global bit buffer */ ! 641: bk = k; ! 642: ! 643: ! 644: /* done */ ! 645: return 0; ! 646: } ! 647: ! 648: ! 649: ! 650: int inflate_stored() ! 651: /* "decompress" an inflated type 0 (stored) block. */ ! 652: { ! 653: unsigned n; /* number of bytes in block */ ! 654: unsigned w; /* current window position */ ! 655: register ulg b; /* bit buffer */ ! 656: register unsigned k; /* number of bits in bit buffer */ ! 657: ! 658: ! 659: /* make local copies of globals */ ! 660: b = bb; /* initialize bit buffer */ ! 661: k = bk; ! 662: w = wp; /* initialize window position */ ! 663: ! 664: ! 665: /* go to byte boundary */ ! 666: n = k & 7; ! 667: DUMPBITS(n); ! 668: ! 669: ! 670: /* get the length and its complement */ ! 671: NEEDBITS(16) ! 672: n = ((unsigned)b & 0xffff); ! 673: DUMPBITS(16) ! 674: NEEDBITS(16) ! 675: if (n != (unsigned)((~b) & 0xffff)) ! 676: return 1; /* error in compressed data */ ! 677: DUMPBITS(16) ! 678: ! 679: ! 680: /* read and output the compressed data */ ! 681: while (n--) ! 682: { ! 683: NEEDBITS(8) ! 684: slide[w++] = (uch)b; ! 685: if (w == WSIZE) ! 686: { ! 687: flush(w); ! 688: w = 0; ! 689: } ! 690: DUMPBITS(8) ! 691: } ! 692: ! 693: ! 694: /* restore the globals from the locals */ ! 695: wp = w; /* restore global window pointer */ ! 696: bb = b; /* restore global bit buffer */ ! 697: bk = k; ! 698: return 0; ! 699: } ! 700: ! 701: ! 702: ! 703: int inflate_fixed() ! 704: /* decompress an inflated type 1 (fixed Huffman codes) block. We should ! 705: either replace this with a custom decoder, or at least precompute the ! 706: Huffman tables. */ ! 707: { ! 708: int i; /* temporary variable */ ! 709: struct huft *tl; /* literal/length code table */ ! 710: struct huft *td; /* distance code table */ ! 711: int bl; /* lookup bits for tl */ ! 712: int bd; /* lookup bits for td */ ! 713: unsigned l[288]; /* length list for huft_build */ ! 714: ! 715: ! 716: /* set up literal table */ ! 717: for (i = 0; i < 144; i++) ! 718: l[i] = 8; ! 719: for (; i < 256; i++) ! 720: l[i] = 9; ! 721: for (; i < 280; i++) ! 722: l[i] = 7; ! 723: for (; i < 288; i++) /* make a complete, but wrong code set */ ! 724: l[i] = 8; ! 725: bl = 7; ! 726: if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) ! 727: return i; ! 728: ! 729: ! 730: /* set up distance table */ ! 731: for (i = 0; i < 30; i++) /* make an incomplete code set */ ! 732: l[i] = 5; ! 733: bd = 5; ! 734: if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) ! 735: { ! 736: huft_free(tl); ! 737: return i; ! 738: } ! 739: ! 740: ! 741: /* decompress until an end-of-block code */ ! 742: if (inflate_codes(tl, td, bl, bd)) ! 743: return 1; ! 744: ! 745: ! 746: /* free the decoding tables, return */ ! 747: huft_free(tl); ! 748: huft_free(td); ! 749: return 0; ! 750: } ! 751: ! 752: ! 753: ! 754: int inflate_dynamic() ! 755: /* decompress an inflated type 2 (dynamic Huffman codes) block. */ ! 756: { ! 757: int i; /* temporary variables */ ! 758: unsigned j; ! 759: unsigned l; /* last length */ ! 760: unsigned m; /* mask for bit lengths table */ ! 761: unsigned n; /* number of lengths to get */ ! 762: struct huft *tl; /* literal/length code table */ ! 763: struct huft *td; /* distance code table */ ! 764: int bl; /* lookup bits for tl */ ! 765: int bd; /* lookup bits for td */ ! 766: unsigned nb; /* number of bit length codes */ ! 767: unsigned nl; /* number of literal/length codes */ ! 768: unsigned nd; /* number of distance codes */ ! 769: #ifdef PKZIP_BUG_WORKAROUND ! 770: unsigned ll[288+32]; /* literal/length and distance code lengths */ ! 771: #else ! 772: unsigned ll[286+30]; /* literal/length and distance code lengths */ ! 773: #endif ! 774: register ulg b; /* bit buffer */ ! 775: register unsigned k; /* number of bits in bit buffer */ ! 776: ! 777: ! 778: /* make local bit buffer */ ! 779: b = bb; ! 780: k = bk; ! 781: ! 782: ! 783: /* read in table lengths */ ! 784: NEEDBITS(5) ! 785: nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ ! 786: DUMPBITS(5) ! 787: NEEDBITS(5) ! 788: nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ ! 789: DUMPBITS(5) ! 790: NEEDBITS(4) ! 791: nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ ! 792: DUMPBITS(4) ! 793: #ifdef PKZIP_BUG_WORKAROUND ! 794: if (nl > 288 || nd > 32) ! 795: #else ! 796: if (nl > 286 || nd > 30) ! 797: #endif ! 798: return 1; /* bad lengths */ ! 799: ! 800: ! 801: /* read in bit-length-code lengths */ ! 802: for (j = 0; j < nb; j++) ! 803: { ! 804: NEEDBITS(3) ! 805: ll[border[j]] = (unsigned)b & 7; ! 806: DUMPBITS(3) ! 807: } ! 808: for (; j < 19; j++) ! 809: ll[border[j]] = 0; ! 810: ! 811: ! 812: /* build decoding table for trees--single level, 7 bit lookup */ ! 813: bl = 7; ! 814: if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) ! 815: { ! 816: if (i == 1) ! 817: huft_free(tl); ! 818: return i; /* incomplete code set */ ! 819: } ! 820: ! 821: ! 822: /* read in literal and distance code lengths */ ! 823: n = nl + nd; ! 824: m = mask_bits[bl]; ! 825: i = l = 0; ! 826: while ((unsigned)i < n) ! 827: { ! 828: NEEDBITS((unsigned)bl) ! 829: j = (td = tl + ((unsigned)b & m))->b; ! 830: DUMPBITS(j) ! 831: j = td->v.n; ! 832: if (j < 16) /* length of code in bits (0..15) */ ! 833: ll[i++] = l = j; /* save last length in l */ ! 834: else if (j == 16) /* repeat last length 3 to 6 times */ ! 835: { ! 836: NEEDBITS(2) ! 837: j = 3 + ((unsigned)b & 3); ! 838: DUMPBITS(2) ! 839: if ((unsigned)i + j > n) ! 840: return 1; ! 841: while (j--) ! 842: ll[i++] = l; ! 843: } ! 844: else if (j == 17) /* 3 to 10 zero length codes */ ! 845: { ! 846: NEEDBITS(3) ! 847: j = 3 + ((unsigned)b & 7); ! 848: DUMPBITS(3) ! 849: if ((unsigned)i + j > n) ! 850: return 1; ! 851: while (j--) ! 852: ll[i++] = 0; ! 853: l = 0; ! 854: } ! 855: else /* j == 18: 11 to 138 zero length codes */ ! 856: { ! 857: NEEDBITS(7) ! 858: j = 11 + ((unsigned)b & 0x7f); ! 859: DUMPBITS(7) ! 860: if ((unsigned)i + j > n) ! 861: return 1; ! 862: while (j--) ! 863: ll[i++] = 0; ! 864: l = 0; ! 865: } ! 866: } ! 867: ! 868: ! 869: /* free decoding table for trees */ ! 870: huft_free(tl); ! 871: ! 872: ! 873: /* restore the global bit buffer */ ! 874: bb = b; ! 875: bk = k; ! 876: ! 877: ! 878: /* build the decoding tables for literal/length and distance codes */ ! 879: bl = lbits; ! 880: if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) ! 881: { ! 882: if (i == 1) { ! 883: fprintf(stderr, " incomplete literal tree\n"); ! 884: huft_free(tl); ! 885: } ! 886: return i; /* incomplete code set */ ! 887: } ! 888: bd = dbits; ! 889: if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) ! 890: { ! 891: if (i == 1) { ! 892: fprintf(stderr, " incomplete distance tree\n"); ! 893: #ifdef PKZIP_BUG_WORKAROUND ! 894: i = 0; ! 895: } ! 896: #else ! 897: huft_free(td); ! 898: } ! 899: huft_free(tl); ! 900: return i; /* incomplete code set */ ! 901: #endif ! 902: } ! 903: ! 904: ! 905: /* decompress until an end-of-block code */ ! 906: if (inflate_codes(tl, td, bl, bd)) ! 907: return 1; ! 908: ! 909: ! 910: /* free the decoding tables, return */ ! 911: huft_free(tl); ! 912: huft_free(td); ! 913: return 0; ! 914: } ! 915: ! 916: ! 917: ! 918: int inflate_block(e) ! 919: int *e; /* last block flag */ ! 920: /* decompress an inflated block */ ! 921: { ! 922: unsigned t; /* block type */ ! 923: register ulg b; /* bit buffer */ ! 924: register unsigned k; /* number of bits in bit buffer */ ! 925: ! 926: ! 927: /* make local bit buffer */ ! 928: b = bb; ! 929: k = bk; ! 930: ! 931: ! 932: /* read in last block bit */ ! 933: NEEDBITS(1) ! 934: *e = (int)b & 1; ! 935: DUMPBITS(1) ! 936: ! 937: ! 938: /* read in block type */ ! 939: NEEDBITS(2) ! 940: t = (unsigned)b & 3; ! 941: DUMPBITS(2) ! 942: ! 943: ! 944: /* restore the global bit buffer */ ! 945: bb = b; ! 946: bk = k; ! 947: ! 948: ! 949: /* inflate that block type */ ! 950: if (t == 2) ! 951: return inflate_dynamic(); ! 952: if (t == 0) ! 953: return inflate_stored(); ! 954: if (t == 1) ! 955: return inflate_fixed(); ! 956: ! 957: ! 958: /* bad block type */ ! 959: return 2; ! 960: } ! 961: ! 962: ! 963: ! 964: int inflate() ! 965: /* decompress an inflated entry */ ! 966: { ! 967: int e; /* last block flag */ ! 968: int r; /* result code */ ! 969: unsigned h; /* maximum struct huft's malloc'ed */ ! 970: ! 971: ! 972: /* initialize window, bit buffer */ ! 973: wp = 0; ! 974: bk = 0; ! 975: bb = 0; ! 976: ! 977: ! 978: /* decompress until the last block */ ! 979: h = 0; ! 980: do { ! 981: hufts = 0; ! 982: if ((r = inflate_block(&e)) != 0) ! 983: { ! 984: Trace((stderr, "\ninflate_block returned %d", r)); ! 985: return r; ! 986: } ! 987: if (hufts > h) ! 988: h = hufts; ! 989: } while (!e); ! 990: ! 991: ! 992: /* flush out slide */ ! 993: flush(wp); ! 994: ! 995: ! 996: /* return success */ ! 997: #ifdef DEBUG ! 998: fprintf(stderr, "<%u> ", h); ! 999: #endif /* DEBUG */ ! 1000: return 0; ! 1001: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.