📄 zlib.c
字号:
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); /* 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 *)Z_NULL; /* just to keep compilers happy */ q = (inflate_huft *)Z_NULL; /* 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 = (z = g - w) > (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 and link in new table */ if ((q = (inflate_huft *)ZALLOC (zs,z + 1,sizeof(inflate_huft))) == Z_NULL) { if (h) inflate_trees_free(u[0], zs); return Z_MEM_ERROR; /* not enough memory */ } q->word.Nalloc = z + 1;#ifdef DEBUG_ZLIB inflate_hufts += z + 1;#endif *t = q + 1; /* link to list for huft_free() */ *(t = &(q->next)) = Z_NULL; u[h] = ++q; /* table starts after link */ /* connect to last table, if there is one */ if (h) { x[h] = i; /* save pattern for backing up */ r.bits = (Byte)l; /* bits to dump before this table */ r.exop = (Byte)j; /* bits in this table */ r.next = q; /* pointer to this table */ j = i >> (w - l); /* (get around Turbo C bug) */ u[h-1][j] = r; /* connect to last table */ } } /* set up table entry in r */ r.bits = (Byte)(k - w); if (p >= v + n) r.exop = 128 + 64; /* out of values--invalid code */ else if (*p < s) { r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ r.base = *p++; /* simple code is just the value */ } else { r.exop = (Byte)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 */ while ((i & ((1 << w) - 1)) != x[h]) { h--; /* don't need to update q */ w -= l; } } } /* Return Z_BUF_ERROR if we were given an incomplete table */ return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;}local int inflate_trees_bits(c, bb, tb, z)uIntf *c; /* 19 code lengths */uIntf *bb; /* bits tree desired/actual depth */inflate_huft * FAR *tb; /* bits tree result */z_stream *z; /* for zfree function */{ int r; r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); if (r == Z_DATA_ERROR) z->msg = "oversubscribed dynamic bit lengths tree"; else if (r == Z_BUF_ERROR) { inflate_trees_free(*tb, z); z->msg = "incomplete dynamic bit lengths tree"; r = Z_DATA_ERROR; } return r;}local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)uInt nl; /* number of literal/length codes */uInt nd; /* number of distance codes */uIntf *c; /* that many (total) code lengths */uIntf *bl; /* literal desired/actual bit depth */uIntf *bd; /* distance desired/actual bit depth */inflate_huft * FAR *tl; /* literal/length tree result */inflate_huft * FAR *td; /* distance tree result */z_stream *z; /* for zfree function */{ int r; /* build literal/length tree */ if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) { if (r == Z_DATA_ERROR) z->msg = "oversubscribed literal/length tree"; else if (r == Z_BUF_ERROR) { inflate_trees_free(*tl, z); z->msg = "incomplete literal/length tree"; r = Z_DATA_ERROR; } return r; } /* build distance tree */ if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) { if (r == Z_DATA_ERROR) z->msg = "oversubscribed literal/length tree"; else if (r == Z_BUF_ERROR) {#ifdef PKZIP_BUG_WORKAROUND r = Z_OK; }#else inflate_trees_free(*td, z); z->msg = "incomplete literal/length tree"; r = Z_DATA_ERROR; } inflate_trees_free(*tl, z); return r;#endif } /* done */ return Z_OK;}/* build fixed tables only once--keep them here */local int fixed_lock = 0;local int fixed_built = 0;#define FIXEDH 530 /* number of hufts used by fixed tables */local uInt fixed_left = FIXEDH;local inflate_huft fixed_mem[FIXEDH];local uInt fixed_bl;local uInt fixed_bd;local inflate_huft *fixed_tl;local inflate_huft *fixed_td;local voidpf falloc(q, n, s)voidpf q; /* opaque pointer (not used) */uInt n; /* number of items */uInt s; /* size of item */{ Assert(s == sizeof(inflate_huft) && n <= fixed_left, "inflate_trees falloc overflow"); if (q) s++; /* to make some compilers happy */ fixed_left -= n; return (voidpf)(fixed_mem + fixed_left);}local void ffree(q, p, n)voidpf q;voidpf p;uInt n;{ Assert(0, "inflate_trees ffree called!"); if (q) q = p; /* to make some compilers happy */}local int inflate_trees_fixed(bl, bd, tl, td)uIntf *bl; /* literal desired/actual bit depth */uIntf *bd; /* distance desired/actual bit depth */inflate_huft * FAR *tl; /* literal/length tree result */inflate_huft * FAR *td; /* distance tree result */{ /* build fixed tables if not built already--lock out other instances */ while (++fixed_lock > 1) fixed_lock--; if (!fixed_built) { int k; /* temporary variable */ unsigned c[288]; /* length list for huft_build */ z_stream z; /* for falloc function */ /* set up fake z_stream for memory routines */ z.zalloc = falloc; z.zfree = ffree; z.opaque = Z_NULL; /* literal table */ for (k = 0; k < 144; k++) c[k] = 8; for (; k < 256; k++) c[k] = 9; for (; k < 280; k++) c[k] = 7; for (; k < 288; k++) c[k] = 8; fixed_bl = 7; huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z); /* distance table */ for (k = 0; k < 30; k++) c[k] = 5; fixed_bd = 5; huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z); /* done */ fixed_built = 1; } fixed_lock--; *bl = fixed_bl; *bd = fixed_bd; *tl = fixed_tl; *td = fixed_td; return Z_OK;}local int inflate_trees_free(t, z)inflate_huft *t; /* table to free */z_stream *z; /* for zfree function *//* Free the malloc'ed tables built by huft_build(), which makes a linked list of the tables it made, with the links in a dummy first entry of each table. */{ register inflate_huft *p, *q; /* Go through linked list, freeing from the malloced (t[-1]) address. */ p = t; while (p != Z_NULL) { q = (--p)->next; ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft)); p = q; } return Z_OK;}/*+++++*//* infcodes.c -- process literals and length/distance pairs * Copyright (C) 1995 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h *//* simplify the use of the inflate_huft type with some defines */#define base more.Base#define next more.Next#define exop word.what.Exop#define bits word.what.Bits/* inflate codes private state */struct inflate_codes_state { /* mode */ enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ START, /* x: set up for LEN */ LEN, /* i: get length/literal/eob next */ LENEXT, /* i: getting length extra (have base) */ DIST, /* i: get distance next */ DISTEXT, /* i: getting distance extra */ COPY, /* o: copying bytes in window, waiting for space */ LIT, /* o: got literal, waiting for output space */ WASH, /* o: got eob, possibly still output waiting */ END, /* x: got eob and all data flushed */ BADCODE} /* x: got error */ mode; /* current inflate_codes mode */ /* mode dependent information */ uInt len; union { struct { inflate_huft *tree; /* pointer into tree */ uInt need; /* bits needed */ } code; /* if LEN or DIST, where in tree */ uInt lit; /* if LIT, literal */ struct { uInt get; /* bits to get for extra */ uInt dist; /* distance back to copy from */ } copy; /* if EXT or COPY, where and how much */ } sub; /* submode */ /* mode independent information */ Byte lbits; /* ltree bits decoded per branch */ Byte dbits; /* dtree bits decoder per branch */ inflate_huft *ltree; /* literal/length/eob tree */ inflate_huft *dtree; /* distance tree */};local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)uInt bl, bd;inflate_huft *tl, *td;z_stream *z;{ inflate_codes_statef *c; if ((c = (inflate_codes_statef *) ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) { c->mode = START; c->lbits = (Byte)bl; c->dbits = (Byte)bd; c->ltree = tl; c->dtree = td; Tracev((stderr, "inflate: codes new\n")); } return c;}local int inflate_codes(s, z, r)inflate_blocks_statef *s;z_stream *z;int r;{ uInt j; /* temporary storage */ inflate_huft *t; /* temporary pointer */ uInt e; /* extra bits or operation */ uLong b; /* bit buffer */ uInt k; /* bits in bit buffer */ Bytef *p; /* input data pointer */ uInt n; /* bytes available there */ Bytef *q; /* output window write pointer */ uInt m; /* bytes to end of window or read pointer */ Bytef *f; /* pointer to copy strings from */ inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ /* copy input/output information to locals (UPDATE macro restores) */ LOAD /* process input and output based on current state */ while (1) switch (c->mode) { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ case START: /* x: set up for LEN */#ifndef SLOW if (m >= 258 && n >= 10) { UPDATE r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); LOAD if (r != Z_OK) { c->mode = r == Z_STREAM_END ? WASH : BADCODE; break; } }#endif /* !SLOW */ c->sub.code.need = c->lbits; c->sub.code.tree = c->ltree; c->mode = LEN; case LEN: /* i: get length/literal/eob next */ j = c->sub.code.need; NEEDBITS(j) t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); DUMPBITS(t->bits) e = (uInt)(t->exop); if (e == 0) /* literal */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -