?? unzip.c
字號:
for (j = 1 << (k - 1); i & j; j >>= 1) i ^= j; i ^= j; /* backup over finished tables */ while ((i & ((1 << w) - 1)) != x[h]) { h--; /* don't need to update q */ w -= l; } } } /* Return true (1) if we were given an incomplete table */ return y != 0 && g != 1;}/* * inflate (decompress) the codes in a deflated (compressed) block. * Return an error code or zero if it all goes ok. * * tl, td: literal/length and distance decoder tables * bl, bd: number of bits decoded by tl[] and td[] */static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd){ register unsigned long e; /* table entry flag/number of extra bits */ unsigned long n, d; /* length and index for copy */ unsigned long w; /* current window position */ huft_t *t; /* pointer to table entry */ unsigned ml, md; /* masks for bl and bd bits */ register unsigned long b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ register int input_char; /* make local copies of globals */ b = bb; /* initialize bit buffer */ k = bk; w = outcnt; /* initialize window position */ /* inflate the coded data */ ml = mask_bits[bl]; /* precompute masks for speed */ md = mask_bits[bd]; for (;;) { /* do until end of block */ while (k < (unsigned) bl) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) do { if (e == 99) { return 1; } b >>= t->b; k -= t->b; e -= 16; while (k < e) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); b >>= t->b; k -= t->b; if (e == 16) { /* then it's a literal */ window[w++] = (unsigned char) t->v.n; if (w == WSIZE) { outcnt=(w), flush_window(); w = 0; } } else { /* it's an EOB or a length */ /* exit if end of block */ if (e == 15) { break; } /* get length of block to copy */ while (k < e) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } n = t->v.n + ((unsigned) b & mask_bits[e]); b >>= e; k -= e; /* decode distance of block to copy */ while (k < (unsigned) bd) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } if ((e = (t = td + ((unsigned) b & md))->e) > 16) do { if (e == 99) return 1; b >>= t->b; k -= t->b; e -= 16; while (k < e) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); b >>= t->b; k -= t->b; while (k < e) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } d = w - t->v.n - ((unsigned) b & mask_bits[e]); b >>= e; k -= e; /* do the copy */ do { n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);#if !defined(NOMEMCPY) && !defined(DEBUG) if (w - d >= e) { /* (this test assumes unsigned comparison) */ memcpy(window + w, window + d, e); w += e; d += e; } else /* do it slow to avoid memcpy() overlap */#endif /* !NOMEMCPY */ do { window[w++] = window[d++]; } while (--e); if (w == WSIZE) { outcnt=(w), flush_window(); w = 0; } } while (n); } } /* restore the globals from the locals */ outcnt = w; /* restore global window pointer */ bb = b; /* restore global bit buffer */ bk = k; /* done */ return 0;}/* * decompress an inflated block * e: last block flag * * GLOBAL VARIABLES: bb, kk, */static int inflate_block(int *e){ unsigned t; /* block type */ register unsigned long b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; /* note: see note #13 above about the 258 in this list. */ static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */ 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, 99, 99 }; /* 99==invalid */ static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }; static unsigned short cpdext[] = { /* Extra bits for distance codes */ 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 }; int input_char; /* make local bit buffer */ b = bb; k = bk; /* read in last block bit */ while (k < 1) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } *e = (int) b & 1; b >>= 1; k -= 1; /* read in block type */ while (k < 2) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b |= ((unsigned long)input_char) << k; k += 8; } t = (unsigned) b & 3; b >>= 2; k -= 2; /* restore the global bit buffer */ bb = b; bk = k; /* inflate that block type */ switch (t) { case 0: /* Inflate stored */ { unsigned long n; /* number of bytes in block */ unsigned long w; /* current window position */ register unsigned long b_stored; /* bit buffer */ register unsigned long k_stored; /* number of bits in bit buffer */ /* make local copies of globals */ b_stored = bb; /* initialize bit buffer */ k_stored = bk; w = outcnt; /* initialize window position */ /* go to byte boundary */ n = k_stored & 7; b_stored >>= n; k_stored -= n; /* get the length and its complement */ while (k_stored < 16) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b_stored |= ((unsigned long)input_char) << k_stored; k_stored += 8; } n = ((unsigned) b_stored & 0xffff); b_stored >>= 16; k_stored -= 16; while (k_stored < 16) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b_stored |= ((unsigned long)input_char) << k_stored; k_stored += 8; } if (n != (unsigned) ((~b_stored) & 0xffff)) { return 1; /* error in compressed data */ } b_stored >>= 16; k_stored -= 16; /* read and output the compressed data */ while (n--) { while (k_stored < 8) { input_char = fgetc(in_file); if (input_char == EOF) return 1; b_stored |= ((unsigned long)input_char) << k_stored; k_stored += 8; } window[w++] = (unsigned char) b_stored; if (w == (unsigned long)WSIZE) { outcnt=(w), flush_window(); w = 0; } b_stored >>= 8; k_stored -= 8; } /* restore the globals from the locals */ outcnt = w; /* restore global window pointer */ bb = b_stored; /* restore global bit buffer */ bk = k_stored; return 0; } case 1: /* Inflate fixed * decompress an inflated type 1 (fixed Huffman codes) block. We should * either replace this with a custom decoder, or at least precompute the * Huffman tables. */ { int i; /* temporary variable */ huft_t *tl; /* literal/length code table */ huft_t *td; /* distance code table */ int bl; /* lookup bits for tl */ int bd; /* lookup bits for td */ unsigned int l[288]; /* length list for huft_build */ /* set up literal table */ for (i = 0; i < 144; i++) { l[i] = 8; } for (; i < 256; i++) { l[i] = 9; } for (; i < 280; i++) { l[i] = 7; } for (; i < 288; i++) { /* make a complete, but wrong code set */ l[i] = 8; } bl = 7; if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { return i; } /* set up distance table */ for (i = 0; i < 30; i++) { /* make an incomplete code set */ l[i] = 5; } bd = 5; if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { huft_free(tl); return i; } /* decompress until an end-of-block code */ if (inflate_codes(tl, td, bl, bd)) return 1; /* free the decoding tables, return */ huft_free(tl); huft_free(td); return 0; } case 2: /* Inflate dynamic */ { /* Tables for deflate from PKZIP's appnote.txt. */ static unsigned border[] = { /* Order of the bit length code lengths */ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; int dbits = 6; /* bits in base distance lookup table */ int lbits = 9; /* bits in base literal/length lookup table */ int i; /* temporary variables */ unsigned j; unsigned l; /* last length */ unsigned m; /* mask for bit lengths table */ unsigned n; /* number of lengths to get */ huft_t *tl; /* literal/length code table */ huft_t *td; /* distance code table */ int bl; /* lookup bits for tl */ int bd; /* lookup bits for td */ unsigned nb; /* number of bit length codes */ unsigned nl; /* number of literal/length codes */ unsigned nd; /* number of distance codes */ unsigned ll[286 + 30]; /* literal/length and distance code lengths */ register unsigned long b_dynamic; /* bit buffer */ register unsigned k_dynamic; /* number of bits in bit buffer */ /* make local bit buffer */ b_dynamic = bb; k_dynamic = bk;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -