📄 explode.c
字号:
do { redirSlide[w++] = redirSlide[d++]; } while (--e); if (w == wsize) { flush(__G__ redirSlide, (ulg)w, 0); w = u = 0; } } while (n); } } /* flush out redirSlide */ flush(__G__ redirSlide, (ulg)w, 0); if (G.csize + G.incnt + (k >> 3)) /* should have read csize bytes, but */ { /* sometimes read one too many: k>>3 compensates */ G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3); return 5; } return 0;}static int explode_nolit4(__G__ tl, td, bl, bd) __GDEFstruct huft *tl, *td; /* length and distance decoder tables */int bl, bd; /* number of bits decoded by tl[] and td[] *//* Decompress the imploded data using uncoded literals and a 4K sliding window. */{ long s; /* bytes to decompress */ register unsigned e; /* table entry flag/number of extra bits */ unsigned n, d; /* length and index for copy */ unsigned w; /* current window position */ struct huft *t; /* pointer to table entry */ unsigned ml, md; /* masks for bl and bd bits */ register ulg b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ unsigned u; /* true if unflushed */ /* explode the coded data */ b = k = w = 0; /* initialize bit buffer, window */ u = 1; /* buffer unflushed */ ml = mask_bits[bl]; /* precompute masks for speed */ md = mask_bits[bd]; s = G.ucsize; while (s > 0) /* do until ucsize bytes uncompressed */ { NEEDBITS(1) if (b & 1) /* then literal--get eight bits */ { DUMPBITS(1) s--; NEEDBITS(8) redirSlide[w++] = (uch)b; if (w == wsize) { flush(__G__ redirSlide, (ulg)w, 0); w = u = 0; } DUMPBITS(8) } else /* else distance/length */ { DUMPBITS(1) NEEDBITS(6) /* get distance low bits */ d = (unsigned)b & 0x3f; DUMPBITS(6) NEEDBITS((unsigned)bd) /* get coded distance high bits */ if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16) do { if (e == 99) return 1; DUMPBITS(t->b) e -= 16; NEEDBITS(e) } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); DUMPBITS(t->b) d = w - d - t->v.n; /* construct offset */ NEEDBITS((unsigned)bl) /* get coded length */ if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16) do { if (e == 99) return 1; DUMPBITS(t->b) e -= 16; NEEDBITS(e) } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); DUMPBITS(t->b) n = t->v.n; if (e) /* get length extra bits */ { NEEDBITS(8) n += (unsigned)b & 0xff; DUMPBITS(8) } /* do the copy */ s -= n; do {#if (defined(DLL) && !defined(NO_SLIDE_REDIR)) if (G.redirect_slide) { /* &= w/ wsize not needed and wrong if redirect */ if (d >= wsize) return 1; n -= (e = (e = wsize - (d > w ? d : w)) > n ? n : e); } else#endif n -= (e = (e = wsize - ((d &= wsize-1) > w ? d : w)) > n ? n : e); if (u && w <= d) { memzero(redirSlide + w, e); w += e; d += e; } else#ifndef NOMEMCPY if (w - d >= e) /* (this test assumes unsigned comparison) */ { memcpy(redirSlide + w, redirSlide + d, e); w += e; d += e; } else /* do it slow to avoid memcpy() overlap */#endif /* !NOMEMCPY */ do { redirSlide[w++] = redirSlide[d++]; } while (--e); if (w == wsize) { flush(__G__ redirSlide, (ulg)w, 0); w = u = 0; } } while (n); } } /* flush out redirSlide */ flush(__G__ redirSlide, (ulg)w, 0); if (G.csize + G.incnt + (k >> 3)) /* should have read csize bytes, but */ { /* sometimes read one too many: k>>3 compensates */ G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3); return 5; } return 0;}int explode(__G) __GDEF/* Explode an imploded compressed stream. Based on the general purpose bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding window. Construct the literal (if any), length, and distance codes and the tables needed to decode them (using huft_build() from inflate.c), and call the appropriate routine for the type of data in the remainder of the stream. The four routines are nearly identical, differing only in whether the literal is decoded or simply read in, and in how many bits are read in, uncoded, for the low distance bits. */{ unsigned r; /* return codes */ struct huft *tb; /* literal code table */ struct huft *tl; /* length code table */ struct huft *td; /* distance code table */ int bb; /* bits for tb */ int bl; /* bits for tl */ int bd; /* bits for td */ unsigned l[256]; /* bit lengths for codes */#if (defined(DLL) && !defined(NO_SLIDE_REDIR)) if (G.redirect_slide) wsize = G.redirect_size, redirSlide = G.redirect_buffer; else wsize = WSIZE, redirSlide = slide;#endif /* Tune base table sizes. Note: I thought that to truly optimize speed, I would have to select different bl, bd, and bb values for different compressed file sizes. I was surprised to find out that the values of 7, 7, and 9 worked best over a very wide range of sizes, except that bd = 8 worked marginally better for large compressed sizes. */ bl = 7; bd = (G.csize + G.incnt) > 200000L ? 8 : 7; /* With literal tree--minimum match length is 3 */#ifdef DEBUG G.hufts = 0; /* initialize huft's malloc'ed */#endif if (G.lrec.general_purpose_bit_flag & 4) { bb = 9; /* base table size for literals */ if ((r = get_tree(__G__ l, 256)) != 0) return (int)r; if ((r = huft_build(__G__ l, 256, 256, NULL, NULL, &tb, &bb)) != 0) { if (r == 1) huft_free(tb); return (int)r; } if ((r = get_tree(__G__ l, 64)) != 0) return (int)r; if ((r = huft_build(__G__ l, 64, 0, cplen3, extra, &tl, &bl)) != 0) { if (r == 1) huft_free(tl); huft_free(tb); return (int)r; } if ((r = get_tree(__G__ l, 64)) != 0) return (int)r; if (G.lrec.general_purpose_bit_flag & 2) /* true if 8K */ { if ((r = huft_build(__G__ l, 64, 0, cpdist8, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); huft_free(tb); return (int)r; } r = explode_lit8(__G__ tb, tl, td, bb, bl, bd); } else /* else 4K */ { if ((r = huft_build(__G__ l, 64, 0, cpdist4, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); huft_free(tb); return (int)r; } r = explode_lit4(__G__ tb, tl, td, bb, bl, bd); } huft_free(td); huft_free(tl); huft_free(tb); } else /* No literal tree--minimum match length is 2 */ { if ((r = get_tree(__G__ l, 64)) != 0) return (int)r; if ((r = huft_build(__G__ l, 64, 0, cplen2, extra, &tl, &bl)) != 0) { if (r == 1) huft_free(tl); return (int)r; } if ((r = get_tree(__G__ l, 64)) != 0) return (int)r; if (G.lrec.general_purpose_bit_flag & 2) /* true if 8K */ { if ((r = huft_build(__G__ l, 64, 0, cpdist8, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); return (int)r; } r = explode_nolit8(__G__ tl, td, bl, bd); } else /* else 4K */ { if ((r = huft_build(__G__ l, 64, 0, cpdist4, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); return (int)r; } r = explode_nolit4(__G__ tl, td, bl, bd); } huft_free(td); huft_free(tl); } Trace((stderr, "<%u > ", G.hufts)); return (int)r;}/* so explode.c and inflate.c can be compiled together into one object: */#undef NEXTBYTE#undef NEEDBITS#undef DUMPBITS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -