📄 explode.c
字号:
NEEDBITS(8)
n += (unsigned)b & 0xff;
DUMPBITS(8)
}
/* do the copy */
s -= n;
do {
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
if (u && w <= d)
{
memset(slide + w, 0, e);
w += e;
d += e;
}
else
#ifndef NOMEMCPY
if (w - d >= e) /* (this test assumes unsigned comparison) */
{
memcpy(slide + w, slide + d, e);
w += e;
d += e;
}
else /* do it slow to avoid memcpy() overlap */
#endif /* !NOMEMCPY */
do {
slide[w++] = slide[d++];
} while (--e);
if (w == WSIZE)
{
flush(w);
w = u = 0;
}
} while (n);
}
}
/* flush out slide */
flush(w);
return csize ? 5 : 0; /* should have read csize bytes */
}
int explode_nolit8(tl, td, bl, bd)
struct 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 an 8K sliding
window. */
{
longint 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 ULONG 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 = ucsize;
while (s > 0) /* do until ucsize bytes uncompressed */
{
NEEDBITS(1)
if (b & 1) /* then literal--get eight bits */
{
DUMPBITS(1)
s--;
NEEDBITS(8)
slide[w++] = (byte)b;
if (w == WSIZE)
{
flush(w);
w = u = 0;
}
DUMPBITS(8)
}
else /* else distance/length */
{
DUMPBITS(1)
NEEDBITS(7) /* get distance low bits */
d = (unsigned)b & 0x7f;
DUMPBITS(7)
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 {
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
if (u && w <= d)
{
memset(slide + w, 0, e);
w += e;
d += e;
}
else
#ifndef NOMEMCPY
if (w - d >= e) /* (this test assumes unsigned comparison) */
{
memcpy(slide + w, slide + d, e);
w += e;
d += e;
}
else /* do it slow to avoid memcpy() overlap */
#endif /* !NOMEMCPY */
do {
slide[w++] = slide[d++];
} while (--e);
if (w == WSIZE)
{
flush(w);
w = u = 0;
}
} while (n);
}
}
/* flush out slide */
flush(w);
return csize ? 5 : 0; /* should have read csize bytes */
}
int explode_nolit4(tl, td, bl, bd)
struct 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. */
{
longint 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 ULONG 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 = ucsize;
while (s > 0) /* do until ucsize bytes uncompressed */
{
NEEDBITS(1)
if (b & 1) /* then literal--get eight bits */
{
DUMPBITS(1)
s--;
NEEDBITS(8)
slide[w++] = (byte)b;
if (w == WSIZE)
{
flush(w);
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 {
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
if (u && w <= d)
{
memset(slide + w, 0, e);
w += e;
d += e;
}
else
#ifndef NOMEMCPY
if (w - d >= e) /* (this test assumes unsigned comparison) */
{
memcpy(slide + w, slide + d, e);
w += e;
d += e;
}
else /* do it slow to avoid memcpy() overlap */
#endif /* !NOMEMCPY */
do {
slide[w++] = slide[d++];
} while (--e);
if (w == WSIZE)
{
flush(w);
w = u = 0;
}
} while (n);
}
}
/* flush out slide */
flush(w);
return csize ? 5 : 0; /* should have read csize bytes */
}
int explode()
/* 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 */
/* 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 suprised to find out the 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 = csize > 200000L ? 8 : 7;
/* With literal tree--minimum match length is 3 */
hufts = 0; /* initialze huft's malloc'ed */
if (lrec.general_purpose_bit_flag & 4)
{
bb = 9; /* base table size for literals */
if ((r = get_tree(l, 256)) != 0)
return r;
if ((r = huft_build(l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
{
if (r == 1)
huft_free(tb);
return r;
}
if ((r = get_tree(l, 64)) != 0)
return r;
if ((r = huft_build(l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
{
if (r == 1)
huft_free(tl);
huft_free(tb);
return r;
}
if ((r = get_tree(l, 64)) != 0)
return r;
if (lrec.general_purpose_bit_flag & 2) /* true if 8K */
{
if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
{
if (r == 1)
huft_free(td);
huft_free(tl);
huft_free(tb);
return r;
}
r = explode_lit8(tb, tl, td, bb, bl, bd);
}
else /* else 4K */
{
if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
{
if (r == 1)
huft_free(td);
huft_free(tl);
huft_free(tb);
return r;
}
r = explode_lit4(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(l, 64)) != 0)
return r;
if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
{
if (r == 1)
huft_free(tl);
return r;
}
if ((r = get_tree(l, 64)) != 0)
return r;
if (lrec.general_purpose_bit_flag & 2) /* true if 8K */
{
if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
{
if (r == 1)
huft_free(td);
huft_free(tl);
return r;
}
r = explode_nolit8(tl, td, bl, bd);
}
else /* else 4K */
{
if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
{
if (r == 1)
huft_free(td);
huft_free(tl);
return r;
}
r = explode_nolit4(tl, td, bl, bd);
}
huft_free(td);
huft_free(tl);
}
#ifdef DEBUG
fprintf(stderr, "<%u > ", hufts);
#endif /* DEBUG */
return r;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -