?? inflate.c
字號:
/* 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 */ G.bb = b; G.bk = k; /* inflate that block type */ if (t == 2) return inflate_dynamic(__G); if (t == 0) return inflate_stored(__G); if (t == 1) return inflate_fixed(__G); /* bad block type */ retval = 2;cleanup_and_exit: return retval;}int inflate(__G__ is_defl64) __GDEF int is_defl64;/* decompress an inflated entry */{ int e; /* last block flag */ int r; /* result code */#ifdef DEBUG unsigned h = 0; /* maximum struct huft's malloc'ed */#endif#if (defined(DLL) && !defined(NO_SLIDE_REDIR)) if (G.redirect_slide) wsize = G.redirect_size, redirSlide = G.redirect_buffer; else wsize = WSIZE, redirSlide = slide; /* how they're #defined if !DLL */#endif /* initialize window, bit buffer */ G.wp = 0; G.bk = 0; G.bb = 0;#ifdef USE_DEFLATE64 if (is_defl64) { G.cplens = cplens64; G.cplext = cplext64; G.cpdext = cpdext64; G.fixed_tl = G.fixed_tl64; G.fixed_bl = G.fixed_bl64; G.fixed_td = G.fixed_td64; G.fixed_bd = G.fixed_bd64; } else { G.cplens = cplens32; G.cplext = cplext32; G.cpdext = cpdext32; G.fixed_tl = G.fixed_tl32; G.fixed_bl = G.fixed_bl32; G.fixed_td = G.fixed_td32; G.fixed_bd = G.fixed_bd32; }#else /* !USE_DEFLATE64 */ if (is_defl64) { /* This should not happen unless UnZip is built from object files * compiled with inconsistent option setting. Handle this by * returning with "bad input" error code. */ Trace((stderr, "\nThis inflate() cannot handle Deflate64!\n")); return 2; }#endif /* ?USE_DEFLATE64 */ /* decompress until the last block */ do {#ifdef DEBUG G.hufts = 0;#endif if ((r = inflate_block(__G__ &e)) != 0) return r;#ifdef DEBUG if (G.hufts > h) h = G.hufts;#endif } while (!e); Trace((stderr, "\n%u bytes in Huffman tables (%u/entry)\n", h * (unsigned)sizeof(struct huft), (unsigned)sizeof(struct huft)));#ifdef USE_DEFLATE64 if (is_defl64) { G.fixed_tl64 = G.fixed_tl; G.fixed_bl64 = G.fixed_bl; G.fixed_td64 = G.fixed_td; G.fixed_bd64 = G.fixed_bd; } else { G.fixed_tl32 = G.fixed_tl; G.fixed_bl32 = G.fixed_bl; G.fixed_td32 = G.fixed_td; G.fixed_bd32 = G.fixed_bd; }#endif /* flush out redirSlide and return (success, unless final FLUSH failed) */ return (FLUSH(G.wp));}int inflate_free(__G) __GDEF{ if (G.fixed_tl != (struct huft *)NULL) { huft_free(G.fixed_td); huft_free(G.fixed_tl); G.fixed_td = G.fixed_tl = (struct huft *)NULL; } return 0;}#endif /* ?USE_ZLIB *//* * GRR: moved huft_build() and huft_free() down here; used by explode() * and fUnZip regardless of whether USE_ZLIB defined or not *//* If BMAX needs to be larger than 16, then h and x[] should be ulg. */#define BMAX 16 /* maximum bit length of any code (16 for explode) */#define N_MAX 288 /* maximum number of codes in any set */int huft_build(__G__ b, n, s, d, e, t, m) __GDEF ZCONST unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ unsigned n; /* number of codes (assumed <= N_MAX) */ unsigned s; /* number of simple-valued codes (0..s-1) */ ZCONST ush *d; /* list of base values for non-simple codes */ ZCONST uch *e; /* list of extra bits for non-simple codes */ struct huft **t; /* result: starting table */ int *m; /* maximum lookup bits, returns actual *//* Given a list of code lengths and a maximum table size, make a set of tables to decode that set of codes. Return zero on success, one if the given code set is incomplete (the tables are still built in this case), two if the input is invalid (all zero length codes or an oversubscribed set of lengths), and three if not enough memory. The code with value 256 is special, and the tables are constructed so that no bits beyond that code are fetched when that code is decoded. */{ unsigned a; /* counter for codes of length k */ unsigned c[BMAX+1]; /* bit length count table */ unsigned el; /* length of EOB code (value 256) */ unsigned f; /* i repeats in table every f entries */ int g; /* maximum code length */ int h; /* table level */ register unsigned i; /* counter, current code */ register unsigned j; /* counter */ register int k; /* number of bits in current code */ int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ int *l = lx+1; /* stack of bits per table */ register unsigned *p; /* pointer into c[], b[], or v[] */ register struct huft *q; /* points to current table */ struct huft r; /* table entry for structure assignment */ struct huft *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ register int w; /* bits before this table == (l * h) */ unsigned x[BMAX+1]; /* bit offsets, then code stack */ unsigned *xp; /* pointer into x */ int y; /* number of dummy codes added */ unsigned z; /* number of entries in current table */ /* Generate counts for each bit length */ el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */ memzero((char *)c, sizeof(c)); p = (unsigned *)b; i = n; do { c[*p]++; p++; /* assume all entries <= BMAX */ } while (--i); if (c[0] == n) /* null input--all zero length codes */ { *t = (struct huft *)NULL; *m = 0; return 0; } /* Find minimum and maximum length, bound *m by those */ for (j = 1; j <= BMAX; j++) if (c[j]) break; k = j; /* minimum code length */ if ((unsigned)*m < j) *m = j; for (i = BMAX; i; i--) if (c[i]) break; g = i; /* maximum code length */ if ((unsigned)*m > i) *m = i; /* 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 2; /* bad input: more codes than bits */ if ((y -= c[i]) < 0) return 2; 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 */ memzero((char *)v, sizeof(v)); p = (unsigned *)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[-1] = 0; /* no bits decoded yet */ u[0] = (struct huft *)NULL; /* just to keep compilers happy */ q = (struct huft *)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[h++]; /* add bits already decoded */ /* compute minimum size table less than or equal to *m bits */ z = (z = g - w) > (unsigned)*m ? *m : z; /* 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; 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 */ } } if ((unsigned)w + j > el && (unsigned)w < el) j = el - w; /* make EOB code end at table */ z = 1 << j; /* table entries for j-bit table */ l[h] = j; /* set table size in stack */ /* allocate and link in new table */ if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == (struct huft *)NULL) { if (h) huft_free(u[0]); return 3; /* not enough memory */ }#ifdef DEBUG G.hufts += z + 1; /* track memory usage */#endif *t = q + 1; /* link to list for huft_free() */ *(t = &(q->v.t)) = (struct huft *)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.b = (uch)l[h-1]; /* bits to dump before this table */ r.e = (uch)(32 + j); /* bits in this table */ r.v.t = q; /* pointer to this table */ j = (i & ((1 << w) - 1)) >> (w - l[h-1]); u[h-1][j] = r; /* connect to last table */ } } /* set up table entry in r */ r.b = (uch)(k - w); if (p >= v + n) r.e = INVALID_CODE; /* out of values--invalid code */ else if (*p < s) { r.e = (uch)(*p < 256 ? 32 : 31); /* 256 is end-of-block code */ r.v.n = (ush)*p++; /* simple code is just the value */ } else { r.e = e[*p - s]; /* non-simple--look up in lists */ r.v.n = 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]) w -= l[--h]; /* don't need to update q */ } } /* return actual size of base table */ *m = l[0]; /* Return true (1) if we were given an incomplete table */ return y != 0 && g != 1;}int huft_free(t)struct huft *t; /* table to free *//* 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 struct huft *p, *q; /* Go through linked list, freeing from the malloced (t[-1]) address. */ p = t; while (p != (struct huft *)NULL) { q = (--p)->v.t; free((zvoid *)p); p = q; } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -