|
|
1.1 ! root 1: /* inflate.c -- put in the public domain by Mark Adler */ ! 2: ! 3: /* Decompresses raw data compressed using the DEFLATE algorithm (RFC 1951) */ ! 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 unimplod.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: c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees. ! 49: c11 2 Jan 93 M. Adler fixed bug in detection of incomplete ! 50: tables, and removed assumption that EOB is ! 51: the longest code (bad assumption). ! 52: c12 3 Jan 93 M. Adler make tables for fixed blocks only once. ! 53: c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c ! 54: outputs one zero length code for an empty ! 55: distance tree). ! 56: c14 12 Mar 93 M. Adler made inflate.c standalone with the ! 57: introduction of inflate.h. ! 58: c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470. ! 59: c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays ! 60: to static for Amiga. ! 61: c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing. ! 62: c14e 8 Oct 93 G. Roelofs changed memset() to memzero(). ! 63: c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace() ! 64: conditional; added inflate_free(). ! 65: c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug) ! 66: c14h 7 Dec 93 C. Ghisler huft_build() optimizations. ! 67: c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing; ! 68: G. Roelofs check NEXTBYTE macro for EOF. ! 69: c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd ! 70: EOF check. ! 71: c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings. ! 72: c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines ! 73: to avoid bug in Encore compiler. ! 74: c14m 7 Jul 94 P. Kienitz modified to allow assembler version of ! 75: inflate_codes() (define ASM_INFLATECODES) ! 76: c14n 22 Jul 94 G. Roelofs changed fprintf to macro for DLL versions ! 77: c14o 23 Aug 94 C. Spieler added a newline to a debug statement; ! 78: G. Roelofs added another typecast to avoid MSC warning ! 79: c14p 4 Oct 94 G. Roelofs added (voidp *) cast to free() argument ! 80: c14q 30 Oct 94 G. Roelofs changed fprintf macro to MESSAGE() ! 81: c14r 1 Nov 94 G. Roelofs fixed possible redefinition of CHECK_EOF ! 82: c14s 7 May 95 S. Maxwell OS/2 DLL globals stuff incorporated; ! 83: P. Kienitz "fixed" ASM_INFLATECODES macro/prototype ! 84: c14t 18 Aug 95 G. Roelofs added inflate() to use zlib functions; ! 85: changed voidp to zvoid; moved huft_build() ! 86: and huft_free() to end of file ! 87: c14u 1 Oct 95 G. Roelofs moved G into definition of MESSAGE macro ! 88: c14v 8 Nov 95 P. Kienitz changed ASM_INFLATECODES to use a regular ! 89: call with __G__ instead of a macro ! 90: c15 3 Aug 96 M. Adler fixed bomb-bug on random input data (Adobe) ! 91: c15b 24 Aug 96 M. Adler more fixes for random input data ! 92: c15c 28 Mar 97 G. Roelofs changed USE_ZLIB fatal exit code from ! 93: PK_MEM2 to PK_MEM3 ! 94: c16 20 Apr 97 J. Altman added memzero(v[]) in huft_build() ! 95: c16b 29 Mar 98 C. Spieler modified DLL code for slide redirection ! 96: ! 97: fork 12 Dec 07 TrueCrypt Foundation Adapted for TrueCrypt ! 98: */ ! 99: ! 100: ! 101: /* ! 102: Inflate deflated (PKZIP's method 8 compressed) data. The compression ! 103: method searches for as much of the current string of bytes (up to a ! 104: length of 258) in the previous 32K bytes. If it doesn't find any ! 105: matches (of at least length 3), it codes the next byte. Otherwise, it ! 106: codes the length of the matched string and its distance backwards from ! 107: the current position. There is a single Huffman code that codes both ! 108: single bytes (called "literals") and match lengths. A second Huffman ! 109: code codes the distance information, which follows a length code. Each ! 110: length or distance code actually represents a base value and a number ! 111: of "extra" (sometimes zero) bits to get to add to the base value. At ! 112: the end of each deflated block is a special end-of-block (EOB) literal/ ! 113: length code. The decoding process is basically: get a literal/length ! 114: code; if EOB then done; if a literal, emit the decoded byte; if a ! 115: length then get the distance and emit the referred-to bytes from the ! 116: sliding window of previously emitted data. ! 117: ! 118: There are (currently) three kinds of inflate blocks: stored, fixed, and ! 119: dynamic. The compressor outputs a chunk of data at a time and decides ! 120: which method to use on a chunk-by-chunk basis. A chunk might typically ! 121: be 32K to 64K, uncompressed. If the chunk is uncompressible, then the ! 122: "stored" method is used. In this case, the bytes are simply stored as ! 123: is, eight bits per byte, with none of the above coding. The bytes are ! 124: preceded by a count, since there is no longer an EOB code. ! 125: ! 126: If the data are compressible, then either the fixed or dynamic methods ! 127: are used. In the dynamic method, the compressed data are preceded by ! 128: an encoding of the literal/length and distance Huffman codes that are ! 129: to be used to decode this block. The representation is itself Huffman ! 130: coded, and so is preceded by a description of that code. These code ! 131: descriptions take up a little space, and so for small blocks, there is ! 132: a predefined set of codes, called the fixed codes. The fixed method is ! 133: used if the block ends up smaller that way (usually for quite small ! 134: chunks); otherwise the dynamic method is used. In the latter case, the ! 135: codes are customized to the probabilities in the current block and so ! 136: can code it much better than the pre-determined fixed codes can. ! 137: ! 138: The Huffman codes themselves are decoded using a multi-level table ! 139: lookup, in order to maximize the speed of decoding plus the speed of ! 140: building the decoding tables. See the comments below that precede the ! 141: lbits and dbits tuning parameters. ! 142: ! 143: GRR: return values(?) ! 144: 0 OK ! 145: 1 incomplete table ! 146: 2 bad input ! 147: 3 not enough memory ! 148: */ ! 149: ! 150: ! 151: /* ! 152: Notes beyond the 1.93a appnote.txt: ! 153: ! 154: 1. Distance pointers never point before the beginning of the output ! 155: stream. ! 156: 2. Distance pointers can point back across blocks, up to 32k away. ! 157: 3. There is an implied maximum of 7 bits for the bit length table and ! 158: 15 bits for the actual data. ! 159: 4. If only one code exists, then it is encoded using one bit. (Zero ! 160: would be more efficient, but perhaps a little confusing.) If two ! 161: codes exist, they are coded using one bit each (0 and 1). ! 162: 5. There is no way of sending zero distance codes--a dummy must be ! 163: sent if there are none. (History: a pre 2.0 version of PKZIP would ! 164: store blocks with no distance codes, but this was discovered to be ! 165: too harsh a criterion.) Valid only for 1.93a. 2.04c does allow ! 166: zero distance codes, which is sent as one code of zero bits in ! 167: length. ! 168: 6. There are up to 286 literal/length codes. Code 256 represents the ! 169: end-of-block. Note however that the static length tree defines ! 170: 288 codes just to fill out the Huffman codes. Codes 286 and 287 ! 171: cannot be used though, since there is no length base or extra bits ! 172: defined for them. Similarily, there are up to 30 distance codes. ! 173: However, static trees define 32 codes (all 5 bits) to fill out the ! 174: Huffman codes, but the last two had better not show up in the data. ! 175: 7. Unzip can check dynamic Huffman blocks for complete code sets. ! 176: The exception is that a single code would not be complete (see #4). ! 177: 8. The five bits following the block type is really the number of ! 178: literal codes sent minus 257. ! 179: 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits ! 180: (1+6+6). Therefore, to output three times the length, you output ! 181: three codes (1+1+1), whereas to output four times the same length, ! 182: you only need two codes (1+3). Hmm. ! 183: 10. In the tree reconstruction algorithm, Code = Code + Increment ! 184: only if BitLength(i) is not zero. (Pretty obvious.) ! 185: 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) ! 186: 12. Note: length code 284 can represent 227-258, but length code 285 ! 187: really is 258. The last length deserves its own, short code ! 188: since it gets used a lot in very redundant files. The length ! 189: 258 is special since 258 - 3 (the min match length) is 255. ! 190: 13. The literal/length and distance code bit lengths are read as a ! 191: single stream of lengths. It is possible (and advantageous) for ! 192: a repeat code (16, 17, or 18) to go across the boundary between ! 193: the two sets of lengths. ! 194: */ ! 195: ! 196: ! 197: /* #define DEBUG */ ! 198: #define INFMOD /* tell inflate.h to include code to be compiled */ ! 199: #include "inflate.h" ! 200: ! 201: ! 202: #ifndef WSIZE /* default is 32K */ ! 203: # define WSIZE 0x8000 /* window size--must be a power of two, and at least */ ! 204: #endif /* 32K for zip's deflate method */ ! 205: ! 206: #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) ! 207: # define wsize G._wsize /* wsize is a variable */ ! 208: #else ! 209: # define wsize WSIZE /* wsize is a constant */ ! 210: #endif ! 211: ! 212: ! 213: #ifndef NEXTBYTE /* default is to simply get a byte from stdin */ ! 214: # define NEXTBYTE getchar() ! 215: #endif ! 216: ! 217: #ifndef MESSAGE /* only used twice, for fixed strings--NOT general-purpose */ ! 218: # define MESSAGE(str,len,flag) fprintf(stderr,(char *)(str)) ! 219: #endif ! 220: ! 221: #ifndef FLUSH /* default is to simply write the buffer to stdout */ ! 222: # define FLUSH(n) fwrite(redirSlide, 1, n, stdout) /* return value not used */ ! 223: #endif ! 224: /* Warning: the fwrite above might not work on 16-bit compilers, since ! 225: 0x8000 might be interpreted as -32,768 by the library function. */ ! 226: ! 227: #ifndef Trace ! 228: # ifdef DEBUG ! 229: # define Trace(x) fprintf x ! 230: # else ! 231: # define Trace(x) ! 232: # endif ! 233: #endif ! 234: ! 235: G_struct G; ! 236: uch redirSlide [WSIZE]; ! 237: ! 238: /*---------------------------------------------------------------------------*/ ! 239: #ifdef USE_ZLIB ! 240: ! 241: ! 242: /* ! 243: GRR: return values for both original inflate() and inflate() ! 244: 0 OK ! 245: 1 incomplete table(?) ! 246: 2 bad input ! 247: 3 not enough memory ! 248: */ ! 249: ! 250: /**************************/ ! 251: /* Function inflate() */ ! 252: /**************************/ ! 253: ! 254: int inflate(__G) /* decompress an inflated entry using the zlib routines */ ! 255: __GDEF ! 256: { ! 257: int err=Z_OK; ! 258: ! 259: #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) ! 260: if (G.redirect_slide) ! 261: wsize = G.redirect_size, redirSlide = G.redirect_buffer; ! 262: else ! 263: wsize = WSIZE, redirSlide = slide; ! 264: #endif ! 265: ! 266: G.dstrm.next_out = redirSlide; ! 267: G.dstrm.avail_out = wsize; ! 268: ! 269: G.dstrm.next_in = G.inptr; ! 270: G.dstrm.avail_in = G.incnt; ! 271: ! 272: if (!G.inflInit) { ! 273: unsigned i; ! 274: int windowBits; ! 275: ! 276: /* only need to test this stuff once */ ! 277: if (zlib_version[0] != ZLIB_VERSION[0]) { ! 278: Info(slide, 0x21, ((char *)slide, ! 279: "error: incompatible zlib version (expected %s, found %s)\n", ! 280: ZLIB_VERSION, zlib_version)); ! 281: return 3; ! 282: } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) ! 283: Info(slide, 0x21, ((char *)slide, ! 284: "warning: different zlib version (expected %s, using %s)\n", ! 285: ZLIB_VERSION, zlib_version)); ! 286: ! 287: /* windowBits = log2(wsize) */ ! 288: for (i = ((unsigned)wsize * 2 - 1), windowBits = 0; ! 289: !(i & 1); i >>= 1, ++windowBits); ! 290: if ((unsigned)windowBits > (unsigned)15) ! 291: windowBits = 15; ! 292: else if (windowBits < 8) ! 293: windowBits = 8; ! 294: ! 295: G.dstrm.zalloc = (alloc_func)Z_NULL; ! 296: G.dstrm.zfree = (free_func)Z_NULL; ! 297: ! 298: Trace((stderr, "initializing inflate()\n")); ! 299: err = inflateInit2(&G.dstrm, -windowBits); ! 300: ! 301: if (err == Z_MEM_ERROR) ! 302: return 3; ! 303: else if (err != Z_OK) ! 304: Trace((stderr, "oops! (inflateInit2() err = %d)\n", err)); ! 305: G.inflInit = 1; ! 306: } ! 307: ! 308: #ifdef FUNZIP ! 309: while (err != Z_STREAM_END) { ! 310: #else /* !FUNZIP */ ! 311: while (G.csize > 0) { ! 312: Trace((stderr, "first loop: G.csize = %ld\n", G.csize)); ! 313: #endif /* ?FUNZIP */ ! 314: while (G.dstrm.avail_out > 0) { ! 315: err = inflate(&G.dstrm, Z_PARTIAL_FLUSH); ! 316: ! 317: if (err == Z_DATA_ERROR) ! 318: return 2; ! 319: else if (err == Z_MEM_ERROR) ! 320: return 3; ! 321: else if (err != Z_OK && err != Z_STREAM_END) ! 322: Trace((stderr, "oops! (inflate(first loop) err = %d)\n", err)); ! 323: ! 324: #ifdef FUNZIP ! 325: if (err == Z_STREAM_END) /* "END-of-entry-condition" ? */ ! 326: #else /* !FUNZIP */ ! 327: if (G.csize <= 0L) /* "END-of-entry-condition" ? */ ! 328: #endif /* ?FUNZIP */ ! 329: break; ! 330: ! 331: if (G.dstrm.avail_in <= 0) { ! 332: if (fillinbuf(__G) == 0) ! 333: return 2; /* no "END-condition" yet, but no more data */ ! 334: ! 335: G.dstrm.next_in = G.inptr; ! 336: G.dstrm.avail_in = G.incnt; ! 337: } ! 338: Trace((stderr, " avail_in = %d\n", G.dstrm.avail_in)); ! 339: } ! 340: FLUSH(wsize - G.dstrm.avail_out); /* flush slide[] */ ! 341: Trace((stderr, "inside loop: flushing %ld bytes (ptr diff = %ld)\n", ! 342: (long)(wsize - G.dstrm.avail_out), ! 343: (long)(G.dstrm.next_out-(Bytef *)redirSlide))); ! 344: G.dstrm.next_out = redirSlide; ! 345: G.dstrm.avail_out = wsize; ! 346: } ! 347: ! 348: /* no more input, so loop until we have all output */ ! 349: Trace((stderr, "beginning final loop: err = %d\n", err)); ! 350: while (err != Z_STREAM_END) { ! 351: err = inflate(&G.dstrm, Z_PARTIAL_FLUSH); ! 352: if (err == Z_DATA_ERROR) ! 353: return 2; ! 354: else if (err == Z_MEM_ERROR) ! 355: return 3; ! 356: else if (err == Z_BUF_ERROR) { /* DEBUG */ ! 357: Trace((stderr, "zlib inflate() did not detect stream end (%s, %s)\n" ! 358: , G.zipfn, G.filename)); ! 359: break; ! 360: } else if (err != Z_OK && err != Z_STREAM_END) { ! 361: Trace((stderr, "oops! (inflate(final loop) err = %d)\n", err)); ! 362: DESTROYGLOBALS() ! 363: EXIT(PK_MEM3); ! 364: } ! 365: FLUSH(wsize - G.dstrm.avail_out); /* final flush of slide[] */ ! 366: Trace((stderr, "final loop: flushing %ld bytes (ptr diff = %ld)\n", ! 367: (long)(wsize - G.dstrm.avail_out), ! 368: (long)(G.dstrm.next_out-(Bytef *)redirSlide))); ! 369: G.dstrm.next_out = redirSlide; ! 370: G.dstrm.avail_out = wsize; ! 371: } ! 372: Trace((stderr, "total in = %ld, total out = %ld\n", G.dstrm.total_in, ! 373: G.dstrm.total_out)); ! 374: ! 375: G.inptr = (uch *)G.dstrm.next_in; ! 376: G.incnt = (G.inbuf + INBUFSIZ) - G.inptr; /* reset for other routines */ ! 377: ! 378: err = inflateReset(&G.dstrm); ! 379: if (err != Z_OK) ! 380: Trace((stderr, "oops! (inflateReset() err = %d)\n", err)); ! 381: ! 382: return 0; ! 383: } ! 384: ! 385: ! 386: /*---------------------------------------------------------------------------*/ ! 387: #else /* !USE_ZLIB */ ! 388: ! 389: ! 390: /* Function prototypes */ ! 391: #ifndef OF ! 392: # ifdef __STDC__ ! 393: # define OF(a) a ! 394: # else ! 395: # define OF(a) () ! 396: # endif ! 397: #endif /* !OF */ ! 398: int inflate_codes OF((__GPRO__ struct huft *tl, struct huft *td, ! 399: int bl, int bd)); ! 400: static int inflate_stored OF((__GPRO)); ! 401: static int inflate_fixed OF((__GPRO)); ! 402: static int inflate_dynamic OF((__GPRO)); ! 403: static int inflate_block OF((__GPRO__ int *e)); ! 404: ! 405: ! 406: /* The inflate algorithm uses a sliding 32K byte window on the uncompressed ! 407: stream to find repeated byte strings. This is implemented here as a ! 408: circular buffer. The index is updated simply by incrementing and then ! 409: and'ing with 0x7fff (32K-1). */ ! 410: /* It is left to other modules to supply the 32K area. It is assumed ! 411: to be usable as if it were declared "uch slide[32768];" or as just ! 412: "uch *slide;" and then malloc'ed in the latter case. The definition ! 413: must be in unzip.h, included above. */ ! 414: ! 415: ! 416: /* unsigned wp; moved to globals.h */ /* current position in slide */ ! 417: ! 418: ! 419: /* Tables for deflate from PKZIP's appnote.txt. */ ! 420: static ZCONST unsigned border[] = { /* Order of the bit length code lengths */ ! 421: 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; ! 422: static ZCONST ush cplens[] = { /* Copy lengths for literal codes 257..285 */ ! 423: 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, ! 424: 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; ! 425: /* note: see note #13 above about the 258 in this list. */ ! 426: static ZCONST ush cplext[] = { /* Extra bits for literal codes 257..285 */ ! 427: 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, ! 428: 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ ! 429: static ZCONST ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ ! 430: 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, ! 431: 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, ! 432: 8193, 12289, 16385, 24577}; ! 433: static ZCONST ush cpdext[] = { /* Extra bits for distance codes */ ! 434: 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, ! 435: 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, ! 436: 12, 12, 13, 13}; ! 437: ! 438: ! 439: /* moved to consts.h (included in unzip.c), resp. funzip.c */ ! 440: #if 1 ! 441: /* And'ing with mask_bits[n] masks the lower n bits */ ! 442: ZCONST ush near mask_bits[] = { ! 443: 0x0000, ! 444: 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, ! 445: 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff ! 446: }; ! 447: #endif /* 0 */ ! 448: ! 449: ! 450: /* Macros for inflate() bit peeking and grabbing. ! 451: The usage is: ! 452: ! 453: NEEDBITS(j) ! 454: x = b & mask_bits[j]; ! 455: DUMPBITS(j) ! 456: ! 457: where NEEDBITS makes sure that b has at least j bits in it, and ! 458: DUMPBITS removes the bits from b. The macros use the variable k ! 459: for the number of bits in b. Normally, b and k are register ! 460: variables for speed and are initialized at the begining of a ! 461: routine that uses these macros from a global bit buffer and count. ! 462: ! 463: In order to not ask for more bits than there are in the compressed ! 464: stream, the Huffman tables are constructed to only ask for just ! 465: enough bits to make up the end-of-block code (value 256). Then no ! 466: bytes need to be "returned" to the buffer at the end of the last ! 467: block. See the huft_build() routine. ! 468: */ ! 469: ! 470: /* These have been moved to globals.h */ ! 471: #if 0 ! 472: ulg bb; /* bit buffer */ ! 473: unsigned bk; /* bits in bit buffer */ ! 474: #endif ! 475: ! 476: #ifndef CHECK_EOF ! 477: # define CHECK_EOF /* default as of 5.13/5.2 */ ! 478: #endif ! 479: ! 480: #ifndef CHECK_EOF ! 481: # define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} ! 482: #else ! 483: # define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\ ! 484: b|=((ulg)c)<<k;k+=8;}} ! 485: #endif /* Piet Plomp: change "return 1" to "break" */ ! 486: ! 487: #define DUMPBITS(n) {b>>=(n);k-=(n);} ! 488: ! 489: ! 490: /* ! 491: Huffman code decoding is performed using a multi-level table lookup. ! 492: The fastest way to decode is to simply build a lookup table whose ! 493: size is determined by the longest code. However, the time it takes ! 494: to build this table can also be a factor if the data being decoded ! 495: are not very long. The most common codes are necessarily the ! 496: shortest codes, so those codes dominate the decoding time, and hence ! 497: the speed. The idea is you can have a shorter table that decodes the ! 498: shorter, more probable codes, and then point to subsidiary tables for ! 499: the longer codes. The time it costs to decode the longer codes is ! 500: then traded against the time it takes to make longer tables. ! 501: ! 502: This results of this trade are in the variables lbits and dbits ! 503: below. lbits is the number of bits the first level table for literal/ ! 504: length codes can decode in one step, and dbits is the same thing for ! 505: the distance codes. Subsequent tables are also less than or equal to ! 506: those sizes. These values may be adjusted either when all of the ! 507: codes are shorter than that, in which case the longest code length in ! 508: bits is used, or when the shortest code is *longer* than the requested ! 509: table size, in which case the length of the shortest code in bits is ! 510: used. ! 511: ! 512: There are two different values for the two tables, since they code a ! 513: different number of possibilities each. The literal/length table ! 514: codes 286 possible values, or in a flat code, a little over eight ! 515: bits. The distance table codes 30 possible values, or a little less ! 516: than five bits, flat. The optimum values for speed end up being ! 517: about one bit more than those, so lbits is 8+1 and dbits is 5+1. ! 518: The optimum values may differ though from machine to machine, and ! 519: possibly even between compilers. Your mileage may vary. ! 520: */ ! 521: ! 522: static ZCONST int lbits = 9; /* bits in base literal/length lookup table */ ! 523: static ZCONST int dbits = 6; /* bits in base distance lookup table */ ! 524: ! 525: ! 526: #ifndef ASM_INFLATECODES ! 527: ! 528: int inflate_codes(__G__ tl, td, bl, bd) ! 529: __GDEF ! 530: struct huft *tl, *td; /* literal/length and distance decoder tables */ ! 531: int bl, bd; /* number of bits decoded by tl[] and td[] */ ! 532: /* inflate (decompress) the codes in a deflated (compressed) block. ! 533: Return an error code or zero if it all goes ok. */ ! 534: { ! 535: register unsigned e; /* table entry flag/number of extra bits */ ! 536: unsigned n, d; /* length and index for copy */ ! 537: unsigned w; /* current window position */ ! 538: struct huft *t; /* pointer to table entry */ ! 539: unsigned ml, md; /* masks for bl and bd bits */ ! 540: register ulg b; /* bit buffer */ ! 541: register unsigned k; /* number of bits in bit buffer */ ! 542: ! 543: ! 544: /* make local copies of globals */ ! 545: b = G.bb; /* initialize bit buffer */ ! 546: k = G.bk; ! 547: w = G.wp; /* initialize window position */ ! 548: ! 549: ! 550: /* inflate the coded data */ ! 551: ml = mask_bits[bl]; /* precompute masks for speed */ ! 552: md = mask_bits[bd]; ! 553: while (1) /* do until end of block */ ! 554: { ! 555: NEEDBITS((unsigned)bl) ! 556: if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) ! 557: do { ! 558: if (e == 99) ! 559: return 1; ! 560: DUMPBITS(t->b) ! 561: e -= 16; ! 562: NEEDBITS(e) ! 563: } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); ! 564: DUMPBITS(t->b) ! 565: if (e == 16) /* then it's a literal */ ! 566: { ! 567: redirSlide[w++] = (uch)t->v.n; ! 568: if (w == wsize) ! 569: { ! 570: FLUSH(w); ! 571: w = 0; ! 572: } ! 573: } ! 574: else /* it's an EOB or a length */ ! 575: { ! 576: /* exit if end of block */ ! 577: if (e == 15) ! 578: break; ! 579: ! 580: /* get length of block to copy */ ! 581: NEEDBITS(e) ! 582: n = t->v.n + ((unsigned)b & mask_bits[e]); ! 583: DUMPBITS(e); ! 584: ! 585: /* decode distance of block to copy */ ! 586: NEEDBITS((unsigned)bd) ! 587: if ((e = (t = td + ((unsigned)b & md))->e) > 16) ! 588: do { ! 589: if (e == 99) ! 590: return 1; ! 591: DUMPBITS(t->b) ! 592: e -= 16; ! 593: NEEDBITS(e) ! 594: } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); ! 595: DUMPBITS(t->b) ! 596: NEEDBITS(e) ! 597: d = w - t->v.n - ((unsigned)b & mask_bits[e]); ! 598: DUMPBITS(e) ! 599: ! 600: /* do the copy */ ! 601: do { ! 602: #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) ! 603: if (G.redirect_slide) {/* &= w/ wsize unnecessary & wrong if redirect */ ! 604: if (d >= wsize) ! 605: return 1; /* invalid compressed data */ ! 606: n -= (e = (e = wsize - (d > w ? d : w)) > n ? n : e); ! 607: } ! 608: else ! 609: #endif ! 610: n -= (e = (e = wsize - ((d &= wsize-1) > w ? d : w)) > n ? n : e); ! 611: #ifndef NOMEMCPY ! 612: if (w - d >= e) /* (this test assumes unsigned comparison) */ ! 613: { ! 614: memcpy(redirSlide + w, redirSlide + d, e); ! 615: w += e; ! 616: d += e; ! 617: } ! 618: else /* do it slowly to avoid memcpy() overlap */ ! 619: #endif /* !NOMEMCPY */ ! 620: do { ! 621: redirSlide[w++] = redirSlide[d++]; ! 622: } while (--e); ! 623: if (w == wsize) ! 624: { ! 625: FLUSH(w); ! 626: w = 0; ! 627: } ! 628: } while (n); ! 629: } ! 630: } ! 631: ! 632: ! 633: /* restore the globals from the locals */ ! 634: G.wp = w; /* restore global window pointer */ ! 635: G.bb = b; /* restore global bit buffer */ ! 636: G.bk = k; ! 637: ! 638: ! 639: /* done */ ! 640: return 0; ! 641: } ! 642: ! 643: #endif /* ASM_INFLATECODES */ ! 644: ! 645: ! 646: ! 647: static int inflate_stored(__G) ! 648: __GDEF ! 649: /* "decompress" an inflated type 0 (stored) block. */ ! 650: { ! 651: unsigned n; /* number of bytes in block */ ! 652: unsigned w; /* current window position */ ! 653: register ulg b; /* bit buffer */ ! 654: register unsigned k; /* number of bits in bit buffer */ ! 655: ! 656: ! 657: /* make local copies of globals */ ! 658: Trace((stderr, "\nstored block")); ! 659: b = G.bb; /* initialize bit buffer */ ! 660: k = G.bk; ! 661: w = G.wp; /* initialize window position */ ! 662: ! 663: ! 664: /* go to byte boundary */ ! 665: n = k & 7; ! 666: DUMPBITS(n); ! 667: ! 668: ! 669: /* get the length and its complement */ ! 670: NEEDBITS(16) ! 671: n = ((unsigned)b & 0xffff); ! 672: DUMPBITS(16) ! 673: NEEDBITS(16) ! 674: if (n != (unsigned)((~b) & 0xffff)) ! 675: return 1; /* error in compressed data */ ! 676: DUMPBITS(16) ! 677: ! 678: ! 679: /* read and output the compressed data */ ! 680: while (n--) ! 681: { ! 682: NEEDBITS(8) ! 683: redirSlide[w++] = (uch)b; ! 684: if (w == wsize) ! 685: { ! 686: FLUSH(w); ! 687: w = 0; ! 688: } ! 689: DUMPBITS(8) ! 690: } ! 691: ! 692: ! 693: /* restore the globals from the locals */ ! 694: G.wp = w; /* restore global window pointer */ ! 695: G.bb = b; /* restore global bit buffer */ ! 696: G.bk = k; ! 697: return 0; ! 698: } ! 699: ! 700: ! 701: /* Globals for literal tables (built once) */ ! 702: /* Moved to globals.h */ ! 703: #if 0 ! 704: struct huft *fixed_tl = (struct huft *)NULL; ! 705: struct huft *fixed_td; ! 706: int fixed_bl, fixed_bd; ! 707: #endif ! 708: ! 709: static int inflate_fixed(__G) ! 710: __GDEF ! 711: /* decompress an inflated type 1 (fixed Huffman codes) block. We should ! 712: either replace this with a custom decoder, or at least precompute the ! 713: Huffman tables. */ ! 714: { ! 715: /* if first time, set up tables for fixed blocks */ ! 716: Trace((stderr, "\nliteral block")); ! 717: if (G.fixed_tl == (struct huft *)NULL) ! 718: { ! 719: int i; /* temporary variable */ ! 720: unsigned l[288]; /* length list for huft_build */ ! 721: ! 722: /* literal table */ ! 723: for (i = 0; i < 144; i++) ! 724: l[i] = 8; ! 725: for (; i < 256; i++) ! 726: l[i] = 9; ! 727: for (; i < 280; i++) ! 728: l[i] = 7; ! 729: for (; i < 288; i++) /* make a complete, but wrong code set */ ! 730: l[i] = 8; ! 731: G.fixed_bl = 7; ! 732: if ((i = huft_build(__G__ l, 288, 257, cplens, cplext, ! 733: &G.fixed_tl, &G.fixed_bl)) != 0) ! 734: { ! 735: G.fixed_tl = (struct huft *)NULL; ! 736: return i; ! 737: } ! 738: ! 739: /* distance table */ ! 740: for (i = 0; i < 30; i++) /* make an incomplete code set */ ! 741: l[i] = 5; ! 742: G.fixed_bd = 5; ! 743: if ((i = huft_build(__G__ l, 30, 0, cpdist, cpdext, ! 744: &G.fixed_td, &G.fixed_bd)) > 1) ! 745: { ! 746: huft_free(G.fixed_tl); ! 747: G.fixed_tl = (struct huft *)NULL; ! 748: return i; ! 749: } ! 750: } ! 751: ! 752: /* decompress until an end-of-block code */ ! 753: return inflate_codes(__G__ G.fixed_tl, G.fixed_td, ! 754: G.fixed_bl, G.fixed_bd) != 0; ! 755: } ! 756: ! 757: ! 758: ! 759: static int inflate_dynamic(__G) ! 760: __GDEF ! 761: /* decompress an inflated type 2 (dynamic Huffman codes) block. */ ! 762: { ! 763: int i; /* temporary variables */ ! 764: unsigned j; ! 765: unsigned l; /* last length */ ! 766: unsigned m; /* mask for bit lengths table */ ! 767: unsigned n; /* number of lengths to get */ ! 768: struct huft *tl; /* literal/length code table */ ! 769: struct huft *td; /* distance code table */ ! 770: int bl; /* lookup bits for tl */ ! 771: int bd; /* lookup bits for td */ ! 772: unsigned nb; /* number of bit length codes */ ! 773: unsigned nl; /* number of literal/length codes */ ! 774: unsigned nd; /* number of distance codes */ ! 775: #ifdef PKZIP_BUG_WORKAROUND ! 776: unsigned ll[288+32]; /* literal/length and distance code lengths */ ! 777: #else ! 778: unsigned ll[286+30]; /* literal/length and distance code lengths */ ! 779: #endif ! 780: register ulg b; /* bit buffer */ ! 781: register unsigned k; /* number of bits in bit buffer */ ! 782: ! 783: ! 784: /* make local bit buffer */ ! 785: Trace((stderr, "\ndynamic block")); ! 786: b = G.bb; ! 787: k = G.bk; ! 788: ! 789: ! 790: /* read in table lengths */ ! 791: NEEDBITS(5) ! 792: nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ ! 793: DUMPBITS(5) ! 794: NEEDBITS(5) ! 795: nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ ! 796: DUMPBITS(5) ! 797: NEEDBITS(4) ! 798: nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ ! 799: DUMPBITS(4) ! 800: #ifdef PKZIP_BUG_WORKAROUND ! 801: if (nl > 288 || nd > 32) ! 802: #else ! 803: if (nl > 286 || nd > 30) ! 804: #endif ! 805: return 1; /* bad lengths */ ! 806: ! 807: ! 808: /* read in bit-length-code lengths */ ! 809: for (j = 0; j < nb; j++) ! 810: { ! 811: NEEDBITS(3) ! 812: ll[border[j]] = (unsigned)b & 7; ! 813: DUMPBITS(3) ! 814: } ! 815: for (; j < 19; j++) ! 816: ll[border[j]] = 0; ! 817: ! 818: ! 819: /* build decoding table for trees--single level, 7 bit lookup */ ! 820: bl = 7; ! 821: i = huft_build(__G__ ll, 19, 19, NULL, NULL, &tl, &bl); ! 822: if (bl == 0) /* no bit lengths */ ! 823: i = 1; ! 824: if (i) ! 825: { ! 826: if (i == 1) ! 827: huft_free(tl); ! 828: return i; /* incomplete code set */ ! 829: } ! 830: ! 831: ! 832: /* read in literal and distance code lengths */ ! 833: n = nl + nd; ! 834: m = mask_bits[bl]; ! 835: i = l = 0; ! 836: while ((unsigned)i < n) ! 837: { ! 838: NEEDBITS((unsigned)bl) ! 839: j = (td = tl + ((unsigned)b & m))->b; ! 840: DUMPBITS(j) ! 841: j = td->v.n; ! 842: if (j < 16) /* length of code in bits (0..15) */ ! 843: ll[i++] = l = j; /* save last length in l */ ! 844: else if (j == 16) /* repeat last length 3 to 6 times */ ! 845: { ! 846: NEEDBITS(2) ! 847: j = 3 + ((unsigned)b & 3); ! 848: DUMPBITS(2) ! 849: if ((unsigned)i + j > n) ! 850: return 1; ! 851: while (j--) ! 852: ll[i++] = l; ! 853: } ! 854: else if (j == 17) /* 3 to 10 zero length codes */ ! 855: { ! 856: NEEDBITS(3) ! 857: j = 3 + ((unsigned)b & 7); ! 858: DUMPBITS(3) ! 859: if ((unsigned)i + j > n) ! 860: return 1; ! 861: while (j--) ! 862: ll[i++] = 0; ! 863: l = 0; ! 864: } ! 865: else /* j == 18: 11 to 138 zero length codes */ ! 866: { ! 867: NEEDBITS(7) ! 868: j = 11 + ((unsigned)b & 0x7f); ! 869: DUMPBITS(7) ! 870: if ((unsigned)i + j > n) ! 871: return 1; ! 872: while (j--) ! 873: ll[i++] = 0; ! 874: l = 0; ! 875: } ! 876: } ! 877: ! 878: ! 879: /* free decoding table for trees */ ! 880: huft_free(tl); ! 881: ! 882: ! 883: /* restore the global bit buffer */ ! 884: G.bb = b; ! 885: G.bk = k; ! 886: ! 887: ! 888: /* build the decoding tables for literal/length and distance codes */ ! 889: bl = lbits; ! 890: i = huft_build(__G__ ll, nl, 257, cplens, cplext, &tl, &bl); ! 891: if (bl == 0) /* no literals or lengths */ ! 892: i = 1; ! 893: if (i) ! 894: { ! 895: if (i == 1) { ! 896: //if (!uO.qflag) ! 897: MESSAGE((uch *)"(incomplete l-tree) ", 21L, 1); ! 898: huft_free(tl); ! 899: } ! 900: return i; /* incomplete code set */ ! 901: } ! 902: bd = dbits; ! 903: i = huft_build(__G__ ll + nl, nd, 0, cpdist, cpdext, &td, &bd); ! 904: if (bd == 0 && nl > 257) /* lengths but no distances */ ! 905: { ! 906: //if (!uO.qflag) ! 907: MESSAGE((uch *)"(incomplete d-tree) ", 21L, 1); ! 908: huft_free(tl); ! 909: return 1; ! 910: } ! 911: if (i == 1) { ! 912: #ifdef PKZIP_BUG_WORKAROUND ! 913: i = 0; ! 914: #else ! 915: //if (!uO.qflag) ! 916: MESSAGE((uch *)"(incomplete d-tree) ", 21L, 1); ! 917: huft_free(td); ! 918: #endif ! 919: } ! 920: if (i) ! 921: { ! 922: huft_free(tl); ! 923: return i; ! 924: } ! 925: ! 926: ! 927: /* decompress until an end-of-block code */ ! 928: if (inflate_codes(__G__ tl, td, bl, bd)) ! 929: return 1; ! 930: ! 931: ! 932: /* free the decoding tables, return */ ! 933: huft_free(tl); ! 934: huft_free(td); ! 935: return 0; ! 936: } ! 937: ! 938: ! 939: ! 940: static int inflate_block(__G__ e) ! 941: __GDEF ! 942: int *e; /* last block flag */ ! 943: /* decompress an inflated block */ ! 944: { ! 945: unsigned t; /* block type */ ! 946: register ulg b; /* bit buffer */ ! 947: register unsigned k; /* number of bits in bit buffer */ ! 948: ! 949: ! 950: /* make local bit buffer */ ! 951: b = G.bb; ! 952: k = G.bk; ! 953: ! 954: ! 955: /* read in last block bit */ ! 956: NEEDBITS(1) ! 957: *e = (int)b & 1; ! 958: DUMPBITS(1) ! 959: ! 960: ! 961: /* read in block type */ ! 962: NEEDBITS(2) ! 963: t = (unsigned)b & 3; ! 964: DUMPBITS(2) ! 965: ! 966: ! 967: /* restore the global bit buffer */ ! 968: G.bb = b; ! 969: G.bk = k; ! 970: ! 971: ! 972: /* inflate that block type */ ! 973: if (t == 2) ! 974: return inflate_dynamic(__G); ! 975: if (t == 0) ! 976: return inflate_stored(__G); ! 977: if (t == 1) ! 978: return inflate_fixed(__G); ! 979: ! 980: ! 981: /* bad block type */ ! 982: return 2; ! 983: } ! 984: ! 985: ! 986: ! 987: int inflate(__G) ! 988: __GDEF ! 989: /* decompress an inflated entry */ ! 990: { ! 991: int e; /* last block flag */ ! 992: int r; /* result code */ ! 993: #ifdef DEBUG ! 994: unsigned h = 0; /* maximum struct huft's malloc'ed */ ! 995: #endif ! 996: ! 997: #if (defined(DLL) && !defined(NO_SLIDE_REDIR)) ! 998: if (G.redirect_slide) ! 999: wsize = G.redirect_size, redirSlide = G.redirect_buffer; ! 1000: else ! 1001: wsize = WSIZE, redirSlide = slide; /* how they're #defined if !DLL */ ! 1002: #endif ! 1003: ! 1004: /* initialize window, bit buffer */ ! 1005: G.wp = 0; ! 1006: G.bk = 0; ! 1007: G.bb = 0; ! 1008: ! 1009: ! 1010: /* decompress until the last block */ ! 1011: do { ! 1012: #ifdef DEBUG ! 1013: G.hufts = 0; ! 1014: #endif ! 1015: if ((r = inflate_block(__G__ &e)) != 0) ! 1016: return r; ! 1017: #ifdef DEBUG ! 1018: if (G.hufts > h) ! 1019: h = G.hufts; ! 1020: #endif ! 1021: } while (!e); ! 1022: ! 1023: ! 1024: /* flush out redirSlide */ ! 1025: FLUSH(G.wp); ! 1026: ! 1027: ! 1028: /* return success */ ! 1029: Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n", ! 1030: h * sizeof(struct huft), sizeof(struct huft))); ! 1031: return 0; ! 1032: } ! 1033: ! 1034: ! 1035: ! 1036: int inflate_free(__G) ! 1037: __GDEF ! 1038: { ! 1039: if (G.fixed_tl != (struct huft *)NULL) ! 1040: { ! 1041: huft_free(G.fixed_td); ! 1042: huft_free(G.fixed_tl); ! 1043: G.fixed_td = G.fixed_tl = (struct huft *)NULL; ! 1044: } ! 1045: return 0; ! 1046: } ! 1047: ! 1048: #endif /* ?USE_ZLIB */ ! 1049: ! 1050: ! 1051: /* ! 1052: * GRR: moved huft_build() and huft_free() down here; used by explode() ! 1053: * and fUnZip regardless of whether USE_ZLIB defined or not ! 1054: */ ! 1055: ! 1056: ! 1057: /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ ! 1058: #define BMAX 16 /* maximum bit length of any code (16 for explode) */ ! 1059: #define N_MAX 288 /* maximum number of codes in any set */ ! 1060: ! 1061: ! 1062: int huft_build( ! 1063: __GDEF ! 1064: ZCONST unsigned *b, /* code lengths in bits (all assumed <= BMAX) */ ! 1065: unsigned n, /* number of codes (assumed <= N_MAX) */ ! 1066: unsigned s, /* number of simple-valued codes (0..s-1) */ ! 1067: ZCONST ush *d, /* list of base values for non-simple codes */ ! 1068: ZCONST ush *e, /* list of extra bits for non-simple codes */ ! 1069: struct huft **t, /* result: starting table */ ! 1070: int *m /* maximum lookup bits, returns actual */ ! 1071: ) ! 1072: /* Given a list of code lengths and a maximum table size, make a set of ! 1073: tables to decode that set of codes. Return zero on success, one if ! 1074: the given code set is incomplete (the tables are still built in this ! 1075: case), two if the input is invalid (all zero length codes or an ! 1076: oversubscribed set of lengths), and three if not enough memory. ! 1077: The code with value 256 is special, and the tables are constructed ! 1078: so that no bits beyond that code are fetched when that code is ! 1079: decoded. */ ! 1080: { ! 1081: unsigned a; /* counter for codes of length k */ ! 1082: unsigned c[BMAX+1]; /* bit length count table */ ! 1083: unsigned el; /* length of EOB code (value 256) */ ! 1084: unsigned f; /* i repeats in table every f entries */ ! 1085: int g; /* maximum code length */ ! 1086: int h; /* table level */ ! 1087: register unsigned i; /* counter, current code */ ! 1088: register unsigned j; /* counter */ ! 1089: register int k; /* number of bits in current code */ ! 1090: int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ ! 1091: int *l = lx+1; /* stack of bits per table */ ! 1092: register unsigned *p; /* pointer into c[], b[], or v[] */ ! 1093: register struct huft *q; /* points to current table */ ! 1094: struct huft r; /* table entry for structure assignment */ ! 1095: struct huft *u[BMAX]; /* table stack */ ! 1096: unsigned v[N_MAX]; /* values in order of bit length */ ! 1097: register int w; /* bits before this table == (l * h) */ ! 1098: unsigned x[BMAX+1]; /* bit offsets, then code stack */ ! 1099: unsigned *xp; /* pointer into x */ ! 1100: int y; /* number of dummy codes added */ ! 1101: unsigned z; /* number of entries in current table */ ! 1102: ! 1103: ! 1104: /* Generate counts for each bit length */ ! 1105: el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */ ! 1106: memset(c, 0, sizeof(c)); ! 1107: p = (unsigned *)b; i = n; ! 1108: do { ! 1109: c[*p]++; p++; /* assume all entries <= BMAX */ ! 1110: } while (--i); ! 1111: if (c[0] == n) /* null input--all zero length codes */ ! 1112: { ! 1113: *t = (struct huft *)NULL; ! 1114: *m = 0; ! 1115: return 0; ! 1116: } ! 1117: ! 1118: ! 1119: /* Find minimum and maximum length, bound *m by those */ ! 1120: for (j = 1; j <= BMAX; j++) ! 1121: if (c[j]) ! 1122: break; ! 1123: k = j; /* minimum code length */ ! 1124: if ((unsigned)*m < j) ! 1125: *m = j; ! 1126: for (i = BMAX; i; i--) ! 1127: if (c[i]) ! 1128: break; ! 1129: g = i; /* maximum code length */ ! 1130: if ((unsigned)*m > i) ! 1131: *m = i; ! 1132: ! 1133: ! 1134: /* Adjust last length count to fill out codes, if needed */ ! 1135: for (y = 1 << j; j < i; j++, y <<= 1) ! 1136: if ((y -= c[j]) < 0) ! 1137: return 2; /* bad input: more codes than bits */ ! 1138: if ((y -= c[i]) < 0) ! 1139: return 2; ! 1140: c[i] += y; ! 1141: ! 1142: ! 1143: /* Generate starting offsets into the value table for each length */ ! 1144: x[1] = j = 0; ! 1145: p = c + 1; xp = x + 2; ! 1146: while (--i) { /* note that i == g from above */ ! 1147: *xp++ = (j += *p++); ! 1148: } ! 1149: ! 1150: ! 1151: /* Make a table of values in order of bit lengths */ ! 1152: memset(v, 0, sizeof(v)); ! 1153: p = (unsigned *)b; i = 0; ! 1154: do { ! 1155: if ((j = *p++) != 0) ! 1156: v[x[j]++] = i; ! 1157: } while (++i < n); ! 1158: n = x[g]; /* set n to length of v */ ! 1159: ! 1160: ! 1161: /* Generate the Huffman codes and for each, make the table entries */ ! 1162: x[0] = i = 0; /* first Huffman code is zero */ ! 1163: p = v; /* grab values in bit order */ ! 1164: h = -1; /* no tables yet--level -1 */ ! 1165: w = l[-1] = 0; /* no bits decoded yet */ ! 1166: u[0] = (struct huft *)NULL; /* just to keep compilers happy */ ! 1167: q = (struct huft *)NULL; /* ditto */ ! 1168: z = 0; /* ditto */ ! 1169: ! 1170: /* go through the bit lengths (k already is bits in shortest code) */ ! 1171: for (; k <= g; k++) ! 1172: { ! 1173: a = c[k]; ! 1174: while (a--) ! 1175: { ! 1176: /* here i is the Huffman code of length k bits for value *p */ ! 1177: /* make tables up to required level */ ! 1178: while (k > w + l[h]) ! 1179: { ! 1180: w += l[h++]; /* add bits already decoded */ ! 1181: ! 1182: /* compute minimum size table less than or equal to *m bits */ ! 1183: z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */ ! 1184: if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ ! 1185: { /* too few codes for k-w bit table */ ! 1186: f -= a + 1; /* deduct codes from patterns left */ ! 1187: xp = c + k; ! 1188: while (++j < z) /* try smaller tables up to z bits */ ! 1189: { ! 1190: if ((f <<= 1) <= *++xp) ! 1191: break; /* enough codes to use up j bits */ ! 1192: f -= *xp; /* else deduct codes from patterns */ ! 1193: } ! 1194: } ! 1195: if ((unsigned)w + j > el && (unsigned)w < el) ! 1196: j = el - w; /* make EOB code end at table */ ! 1197: z = 1 << j; /* table entries for j-bit table */ ! 1198: l[h] = j; /* set table size in stack */ ! 1199: ! 1200: /* allocate and link in new table */ ! 1201: if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == ! 1202: (struct huft *)NULL) ! 1203: { ! 1204: if (h) ! 1205: huft_free(u[0]); ! 1206: return 3; /* not enough memory */ ! 1207: } ! 1208: #ifdef DEBUG ! 1209: G.hufts += z + 1; /* track memory usage */ ! 1210: #endif ! 1211: *t = q + 1; /* link to list for huft_free() */ ! 1212: *(t = &(q->v.t)) = (struct huft *)NULL; ! 1213: u[h] = ++q; /* table starts after link */ ! 1214: ! 1215: /* connect to last table, if there is one */ ! 1216: if (h) ! 1217: { ! 1218: x[h] = i; /* save pattern for backing up */ ! 1219: r.b = (uch)l[h-1]; /* bits to dump before this table */ ! 1220: r.e = (uch)(16 + j); /* bits in this table */ ! 1221: r.v.t = q; /* pointer to this table */ ! 1222: j = (i & ((1 << w) - 1)) >> (w - l[h-1]); ! 1223: u[h-1][j] = r; /* connect to last table */ ! 1224: } ! 1225: } ! 1226: ! 1227: /* set up table entry in r */ ! 1228: r.b = (uch)(k - w); ! 1229: if (p >= v + n) ! 1230: r.e = 99; /* out of values--invalid code */ ! 1231: else if (*p < s) ! 1232: { ! 1233: r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ ! 1234: r.v.n = (ush)*p++; /* simple code is just the value */ ! 1235: } ! 1236: else ! 1237: { ! 1238: r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ ! 1239: r.v.n = d[*p++ - s]; ! 1240: } ! 1241: ! 1242: /* fill code-like entries with r */ ! 1243: f = 1 << (k - w); ! 1244: for (j = i >> w; j < z; j += f) ! 1245: q[j] = r; ! 1246: ! 1247: /* backwards increment the k-bit code i */ ! 1248: for (j = 1 << (k - 1); i & j; j >>= 1) ! 1249: i ^= j; ! 1250: i ^= j; ! 1251: ! 1252: /* backup over finished tables */ ! 1253: while ((i & ((1 << w) - 1)) != x[h]) ! 1254: w -= l[--h]; /* don't need to update q */ ! 1255: } ! 1256: } ! 1257: ! 1258: ! 1259: /* return actual size of base table */ ! 1260: *m = l[0]; ! 1261: ! 1262: ! 1263: /* Return true (1) if we were given an incomplete table */ ! 1264: return y != 0 && g != 1; ! 1265: } ! 1266: ! 1267: ! 1268: ! 1269: int huft_free (struct huft *t) ! 1270: /* table to free */ ! 1271: /* Free the malloc'ed tables built by huft_build(), which makes a linked ! 1272: list of the tables it made, with the links in a dummy first entry of ! 1273: each table. */ ! 1274: { ! 1275: register struct huft *p, *q; ! 1276: ! 1277: ! 1278: /* Go through linked list, freeing from the malloced (t[-1]) address. */ ! 1279: p = t; ! 1280: while (p != (struct huft *)NULL) ! 1281: { ! 1282: q = (--p)->v.t; ! 1283: free((zvoid *)p); ! 1284: p = q; ! 1285: } ! 1286: return 0; ! 1287: } ! 1288: ! 1289: ! 1290: // Main public function. Decompresses raw data compressed using the DEFLATE algorithm (RFC 1951 - e.g. zlib, gzip). ! 1291: // Returns 0 if decompression fails or, if successful, returns the size of the decompressed data. ! 1292: int DecompressDeflatedData (char *out, char *in, int inLength) ! 1293: { ! 1294: G.outbufptr = out; ! 1295: G.inptr = in; ! 1296: G.incnt = inLength; ! 1297: G.outCounter = 0; ! 1298: ! 1299: if (inflate(__G) != 0) ! 1300: { ! 1301: // Error decompressing ! 1302: return 0; ! 1303: } ! 1304: return G.outCounter; ! 1305: } ! 1306:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.