📄 inflate.c
字号:
{ int i; /* temporary variable */ struct huft *tl; /* literal/length code table */ struct huft *td; /* distance code table */ int bl; /* lookup bits for tl */ int bd; /* lookup bits for td */ unsigned 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; tl =0; if ((i = huft_build(l, 288, 257, cplens,31, cplext,31, &tl, &bl)) != 0) { if (tl) huft_free(tl); DBGPrintfo(("inflate_fixed(out1)\r\n")); return i; } /* set up distance table */ for (i = 0; i < 30; i++) /* make an incomplete code set */ l[i] = 5; bd = 5; td=0; if ((i = huft_build(l, 30, 0, cpdist,30, cpdext,30, &td, &bd)) > 1) { if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_fixed(out2)\r\n")); return i; } /* decompress until an end-of-block code */ if (inflate_codes(tl, td, bl, bd)) { if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_fixed(out3)\r\n")); return 1; } /* free the decoding tables, return */ if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_fixed(out4)\r\n")); return 0; }}int inflate_dynamic()/* decompress an inflated type 2 (dynamic Huffman codes) block. */{ DBGPrintfi(("inflate_dynamic(In)\r\n")); { int i; /* temporary variables */ unsigned j; unsigned l; /* last length */ unsigned m; /* mask for bit lengths table */ unsigned n; /* number of lengths to get */ struct huft *tl; /* literal/length code table */ struct huft *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 */#ifdef PKZIP_BUG_WORKAROUND unsigned ll[288+32]; /* literal/length and distance code lengths */#else unsigned ll[286+30]; /* literal/length and distance code lengths */#endif register ulg b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ /* make local bit buffer */ b = bb; k = bk; /* read in table lengths */ NEEDBITS(5) nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ DUMPBITS(5) NEEDBITS(5) nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ DUMPBITS(5) NEEDBITS(4) nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ DUMPBITS(4)#ifdef PKZIP_BUG_WORKAROUND if (nl > 288 || nd > 32)#else if (nl > 286 || nd > 30)#endif { DBGPrintfo(("inflate_dynamic(out1)\r\n")); return 1; } /* bad lengths */ /* read in bit-length-code lengths */ for (j = 0; j < nb; j++) { NEEDBITS(3) ll[border[j]] = (unsigned)b & 7; DUMPBITS(3) } for (; j < 19; j++) ll[border[j]] = 0; /* build decoding table for trees--single level, 7 bit lookup */ bl = 7; tl = 0; if ((i = huft_build(ll, 19, 19, NULL,0, NULL,0, &tl, &bl)) != 0) { if (tl) huft_free(tl); DBGPrintfo(("inflate_dynamic(out2)\r\n")); return i; /* incomplete code set */ } /* read in literal and distance code lengths */ n = nl + nd; m = mask_bits[bl]; i = l = 0; while ((unsigned)i < n) { NEEDBITS((unsigned)bl) j = (td = tl + ((unsigned)b & m))->b; DUMPBITS(j) j = td->v.n; if (j < 16) /* length of code in bits (0..15) */ ll[i++] = l = j; /* save last length in l */ else if (j == 16) /* repeat last length 3 to 6 times */ { NEEDBITS(2) j = 3 + ((unsigned)b & 3); DUMPBITS(2) if ((unsigned)i + j > n) { if (tl) huft_free(tl); DBGPrintfo(("inflate_dynamic(out3)\r\n")); return 1; } while (j--) ll[i++] = l; } else if (j == 17) /* 3 to 10 zero length codes */ { NEEDBITS(3) j = 3 + ((unsigned)b & 7); DUMPBITS(3) if ((unsigned)i + j > n) { if (tl) huft_free(tl); DBGPrintfo(("inflate_dynamic(out4)\r\n")); return 1; } while (j--) ll[i++] = 0; l = 0; } else /* j == 18: 11 to 138 zero length codes */ { NEEDBITS(7) j = 11 + ((unsigned)b & 0x7f); DUMPBITS(7) if ((unsigned)i + j > n) { if (tl) huft_free(tl); DBGPrintfo(("inflate_dynamic(out5)\r\n")); return 1; } while (j--) ll[i++] = 0; l = 0; } } /* free decoding table for trees */ if (tl) huft_free(tl); /* restore the global bit buffer */ bb = b; bk = k; /* build the decoding tables for literal/length and distance codes */ bl = lbits; tl = 0; if ((i = huft_build(ll, nl, 257, cplens, 31, cplext, 31,&tl, &bl)) != 0) { if (tl) huft_free(tl); DBGPrintfo(("inflate_dynamic(out6)\r\n")); return i; /* incomplete code set */ } bd = dbits; td = 0; if ((i = huft_build(ll + nl, nd, 0, cpdist, 30, cpdext, 30, &td, &bd)) != 0) { if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_dynamic(out7)\r\n")); return i; /* incomplete code set */ } /* decompress until an end-of-block code */ if (inflate_codes(tl, td, bl, bd)) { if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_dynamic(out8)\r\n")); return 1; } /* free the decoding tables, return */ if (tl) huft_free(tl); if (td) huft_free(td); DBGPrintfo(("inflate_dynamic(out9)\r\n")); return 0; }}int inflate_block(e)int *e; /* last block flag *//* decompress an inflated block */{ DBGPrintfi(("inflate_block(In)\r\n")); { unsigned t; /* block type */ register ulg b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ /* make local bit buffer */ b = bb; k = bk; /* read in last block bit */ NEEDBITS(1) *e = (int)b & 1; DUMPBITS(1) /* read in block type */ NEEDBITS(2) t = (unsigned)b & 3; DUMPBITS(2) /* restore the global bit buffer */ bb = b; bk = k; /* inflate that block type */ if (t == 2) { int retTemp; retTemp=(int)( inflate_dynamic()); DBGPrintfo(("inflate_block(out1)\r\n")); return retTemp; } if (t == 0) { int retTemp; retTemp=(int)( inflate_stored()); DBGPrintfo(("inflate_block(out2)\r\n")); return retTemp; } if (t == 1) { int retTemp; retTemp=(int)( inflate_fixed()); DBGPrintfo(("inflate_block(out3)\r\n")); return retTemp; } /* bad block type */ DBGPrintfo(("inflate_block(out4)\r\n")); return 2; }}int gzip_inflate()/* decompress an inflated entry */{ DBGPrintfi(("inflate(In)\r\n")); { int e; /* last block flag */ int r; /* result code */ unsigned h; /* maximum struct huft's malloc'ed */ /* initialize window, bit buffer */ wp = 0; bk = 0; bb = 0; /* decompress until the last block */ h = 0; do { hufts = 0; if ((r = inflate_block(&e)) != 0) { DBGPrintfo(("inflate(out1)\r\n")); return r; } if (hufts > h) h = hufts; } while (!e); /* Undo too much lookahead. The next read will be byte aligned so we * can discard unused bits in the last meaningful byte. */ while (bk >= 8) { bk -= 8; inptr--; } /* flush out slide */ flush_output(wp); /* return success */#ifdef DEBUG fprintf(stderr, "<%u> ", h);#endif /* DEBUG */ DBGPrintfo(("inflate(out2)\r\n")); return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -