📄 liteunzip.c
字号:
case IBM_BTREE:
{
while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
{
NEEDBITS(3)
s->sub.trees.blens[Border[s->sub.trees.index++]] = (uInt)b & 7;
DUMPBITS(3)
}
while (s->sub.trees.index < 19)
s->sub.trees.blens[Border[s->sub.trees.index++]] = 0;
s->sub.trees.bb = 7;
t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb, &s->sub.trees.tb, s->hufts, z);
if (t != Z_OK)
{
r = t; if (r == Z_DATA_ERROR) { GlobalFree(s->sub.trees.blens); s->mode = IBM_BAD; }
LEAVE }
s->sub.trees.index = 0;
#ifndef NDEBUG
LuTracev((stderr, "inflate: bits tree ok\n"));
#endif
s->mode = IBM_DTREE;
}
case IBM_DTREE:
{
while (t = s->sub.trees.table, s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
{
INFLATE_HUFT *h;
uInt i, j, c;
t = s->sub.trees.bb;
NEEDBITS(t)
h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
t = h->word.what.Bits;
c = h->base;
if (c < 16)
{
DUMPBITS(t)
s->sub.trees.blens[s->sub.trees.index++] = c;
}
else // c == 16..18
{
i = c == 18 ? 7 : c - 14;
j = c == 18 ? 11 : 3;
NEEDBITS(t + i)
DUMPBITS(t)
j += (uInt)b & inflate_mask[i];
DUMPBITS(i)
i = s->sub.trees.index;
t = s->sub.trees.table;
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1))
{
GlobalFree(s->sub.trees.blens);
s->mode = IBM_BAD;
#ifndef NDEBUG
z->msg = (char*)"invalid bit length repeat";
#endif
r = Z_DATA_ERROR;
LEAVE
}
c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
do
{
s->sub.trees.blens[i++] = c;
} while (--j);
s->sub.trees.index = i;
}
}
s->sub.trees.tb = 0;
{
uInt bl, bd;
INFLATE_HUFT *tl, *td;
INFLATE_CODES_STATE *c;
bl = 9; // must be <= 9 for lookahead assumptions
bd = 6; // must be <= 9 for lookahead assumptions
t = s->sub.trees.table;
t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), s->sub.trees.blens, &bl, &bd, &tl, &td, s->hufts, z);
if (t != Z_OK) { if (t == (uInt)Z_DATA_ERROR) { GlobalFree(s->sub.trees.blens); s->mode = IBM_BAD; } r = t; LEAVE } #ifndef NDEBUG
LuTracev((stderr, "inflate: trees ok\n"));
#endif if ((c = inflate_codes_new(bl, bd, tl, td, z)) == 0)
{
r = Z_MEM_ERROR;
LEAVE
}
s->sub.decode.codes = c;
}
GlobalFree(s->sub.trees.blens); s->mode = IBM_CODES;
}
case IBM_CODES:
{
UPDATE
if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
return inflate_flush(s, z, r);
r = Z_OK;
GlobalFree(s->sub.decode.codes);
LOAD
#ifndef NDEBUG
LuTracev((stderr, "inflate: codes end, %lu total out\n", z->total_out + (q >= s->read ? q - s->read : (s->end - s->read) + (q - s->window))));
#endif
if (!s->last)
{
s->mode = IBM_TYPE;
break;
}
s->mode = IBM_DRY;
}
case IBM_DRY:
{
FLUSH
if (s->read != s->write)
LEAVE
s->mode = IBM_DONE;
}
case IBM_DONE:
{
r = Z_STREAM_END;
LEAVE
}
case IBM_BAD:
{
r = Z_DATA_ERROR;
LEAVE
}
default:
{
r = Z_STREAM_ERROR;
LEAVE
}
}
}
}
// inftrees.c -- generate Huffman trees for efficient decoding
// Copyright (C) 1995-1998 Mark Adler
// For conditions of distribution and use, see copyright notice in zlib.h
/************************* huft_build() ************************
* Huffman code decoding is performed using a multi-level table
* lookup.
// The fastest way to decode is to simply build a lookup table whose
// size is determined by the longest code. However, the time it takes
// to build this table can also be a factor if the data being decoded
// is not very long. The most common codes are necessarily the
// shortest codes, so those codes dominate the decoding time, and hence
// the speed. The idea is you can have a shorter table that decodes the
// shorter, more probable codes, and then point to subsidiary tables for
// the longer codes. The time it costs to decode the longer codes is
// then traded against the time it takes to make longer tables.
//
// This results of this trade are in the variables lbits and dbits
// below. lbits is the number of bits the first level table for literal/
// length codes can decode in one step, and dbits is the same thing for
// the distance codes. Subsequent tables are also less than or equal to
// those sizes. These values may be adjusted either when all of the
// codes are shorter than that, in which case the longest code length in
// bits is used, or when the shortest code is *longer* than the requested
// table size, in which case the length of the shortest code in bits is
// used.
//
// There are two different values for the two tables, since they code a
// different number of possibilities each. The literal/length table
// codes 286 possible values, or in a flat code, a little over eight
// bits. The distance table codes 30 possible values, or a little less
// than five bits, flat. The optimum values for speed end up being
// about one bit more than those, so lbits is 8+1 and dbits is 5+1.
// The optimum values may differ though from machine to machine, and
// possibly even between compilers. Your mileage may vary.
*/
// If BMAX needs to be larger than 16, then h and x[] should be ULG.
#define BMAX 15 // maximum bit length of any code
static int huft_build(
uInt *b, // code lengths in bits (all assumed <= BMAX)
uInt n, // number of codes (assumed <= 288)
uInt s, // number of simple-valued codes (0..s-1)
const uInt *d, // list of base values for non-simple codes
const uInt *e, // list of extra bits for non-simple codes
INFLATE_HUFT * *t, // result: starting table
uInt *m, // maximum lookup bits, returns actual
INFLATE_HUFT *hp, // space for trees
uInt *hn, // hufts used in space
uInt *v) // working area: values in order of bit length
// Given a list of code lengths and a maximum table size, make a set of// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR// if the given code set is incomplete (the tables are still built in this// case), or Z_DATA_ERROR if the input is invalid.{
uInt a; // counter for codes of length k
uInt c[BMAX+1]; // bit length count table
uInt f; // i repeats in table every f entries
int g; // maximum code length
int h; // table level
register uInt i; // counter, current code
register uInt j; // counter
register int k; // number of bits in current code
int l; // bits per table (returned in m)
uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP
register uInt *p; // pointer into c[], b[], or v[]
INFLATE_HUFT *q; // points to current table
INFLATE_HUFT r; // table entry for structure assignment
INFLATE_HUFT *u[BMAX]; // table stack
register int w; // bits before this table == (l * h)
uInt x[BMAX+1]; // bit offsets, then code stack
uInt *xp; // pointer into x
int y; // number of dummy codes added
uInt z; // number of entries in current table
// Generate counts for each bit length
p = c;
#define C0 *p++ = 0;
#define C2 C0 C0 C0 C0
#define C4 C2 C2 C2 C2
C4;
p; // clear c[]--assume BMAX+1 is 16
p = b; i = n;
do
{
c[*p++]++; // assume all entries <= BMAX
} while (--i);
if (c[0] == n) // null input--all zero length codes
{
*t = (INFLATE_HUFT *)0;
*m = 0;
return Z_OK;
}
// Find minimum and maximum length, bound *m by those
l = *m;
for (j = 1; j <= BMAX; j++)
{
if (c[j]) break;
}
k = j; // minimum code length
if ((uInt)l < j) l = j;
for (i = BMAX; i; i--)
{
if (c[i]) break;
}
g = i; // maximum code length
if ((uInt)l > i) l = i;
*m = l;
// Adjust last length count to fill out codes, if needed
for (y = 1 << j; j < i; j++, y <<= 1)
{
if ((y -= c[j]) < 0) return Z_DATA_ERROR;
}
if ((y -= c[i]) < 0) return Z_DATA_ERROR;
c[i] += y;
// Generate starting offsets into the value table for each length
x[1] = j = 0;
p = c + 1; xp = x + 2;
while (--i) // note that i == g from above
*xp++ = (j += *p++);
// Make a table of values in order of bit lengths
p = b; i = 0;
do
{
if ((j = *p++) != 0) v[x[j]++] = i;
} while (++i < n);
n = x[g]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries
x[0] = i = 0; // first Huffman code is zero
p = v; // grab values in bit order
h = -1; // no tables yet--level -1
w = -l; // bits decoded == (l * h)
u[0] = (INFLATE_HUFT *)0; // just to keep compilers happy
q = (INFLATE_HUFT *)0; // ditto
z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code)
for (; k <= g; k++)
{
a = c[k];
while (a--)
{
// here i is the Huffman code of length k bits for value *p
// make tables up to required level
while (k > w + l)
{
h++;
w += l; // previous table always l bits
// compute minimum size table less than or equal to l bits
z = g - w;
z = z > (uInt)l ? l : z; // table size upper limit
if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table
{ // too few codes for k-w bit table
f -= a + 1; // deduct codes from patterns left
xp = c + k;
if (j < z)
{
while (++j < z) // try smaller tables up to z bits
{
if ((f <<= 1) <= *++xp)
break; // enough codes to use up j bits
f -= *xp; // else deduct codes from patterns
}
}
}
z = 1 << j; // table entries for j-bit table
// allocate new table if (*hn + z > MANY) // (note: doesn't matter for fixed) return Z_DATA_ERROR; // overflow of MANY u[h] = q = hp + *hn; *hn += z;
// connect to last table, if there is one
if (h)
{
x[h] = i; // save pattern for backing up
r.word.what.Bits = (UCH)l; // bits to dump before this table
r.word.what.Exop = (UCH)j; // bits in this table
j = i >> (w - l);
r.base = (uInt)(q - u[h-1] - j); // offset to this table
u[h-1][j] = r; // connect to last table
}
else
*t = q; // first table is returned result
}
// set up table entry in r
r.word.what.Bits = (UCH)(k - w);
if (p >= v + n)
r.word.what.Exop = 128 + 64; // out of values--invalid code
else if (*p < s)
{
r.word.what.Exop = (UCH)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block
r.base = *p++; // simple code is just the value
}
else
{
r.word.what.Exop = (UCH)(e[*p - s] + 16 + 64);// non-simple--look up in lists
r.base = d[*p++ - s];
}
// fill code-like entries with r
f = 1 << (k - w);
for (j = i >> w; j < z; j += f) q[j] = r;
// backwards increment the k-bit code i
for (j = 1 << (k - 1); i & j; j >>= 1) i ^= j;
i ^= j;
// backup over finished tables
mask = (1 << w) - 1; // needed on HP, cc -O bug
while ((i & mask) != x[h])
{
h--; // don't need to update q
w -= l;
mask = (1 << w) - 1;
}
}
}
// Return Z_BUF_ERROR if we were given an incomplete table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -