📄 gunzip.c
字号:
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. */static unsigned long lbits = 9; /* bits in base literal/length lookup table */static unsigned long dbits = 6; /* bits in base distance lookup table *//* 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 */static unsigned long hufts; /* track memory usage *//* Macros for inflate() bit peeking and grabbing. The usage is: NEEDBITS(j) x = b & mask_bits[j]; DUMPBITS(j) where NEEDBITS makes sure that b has at least j bits in it, and DUMPBITS removes the bits from b. The macros use the variable k for the number of bits in b. Normally, b and k are register variables for speed, and are initialized at the beginning of a routine that uses these macros from a global bit buffer and count. If we assume that EOB will be the longest code, then we will never ask for bits with NEEDBITS that are beyond the end of the stream. So, NEEDBITS should not read any more bytes than are needed to meet the request. Then no bytes need to be "returned" to the buffer at the end of the last block. However, this assumption is not true for fixed blocks--the EOB code is 7 bits, but the other literal/length codes can be 8 or 9 bits. (The EOB code is shorter than other codes because fixed blocks are generally short. So, while a block always has an EOB, many other literal/length codes have a significantly lower probability of showing up at all.) However, by making the first table have a lookup of seven bits, the EOB code will be found in that first lookup, and so will not require that too many bits be pulled from the stream. */static unsigned long bb; /* bit buffer */static unsigned long bk; /* bits in bit buffer */static unsigned short mask_bits[] ={ 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff};#define NEEDBITS(n) do {while(k<(n)){b|=get_byte()<<k;k+=8;}} while (0)#define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)#define INBUFSIZ 0x2000static unsigned char inbuf[INBUFSIZ];static unsigned long bufloc = 0;static unsigned longget_byte (void){ if (filepos == gzip_data_offset || bufloc == INBUFSIZ) { bufloc = 0; grub_read ((char *)inbuf, INBUFSIZ); } return inbuf[bufloc++];}/* decompression global pointers */static struct huft *tl; /* literal/length code table */static struct huft *td; /* distance code table */static unsigned long bl; /* lookup bits for tl */static unsigned long bd; /* lookup bits for td */static unsigned long c[BMAX + 1]; /* bit length count table */static struct huft *u[BMAX]; /* table stack */static unsigned long v[N_MAX]; /* values in order of bit length */static unsigned long x[BMAX + 1]; /* bit offsets, then code stack */static unsigned long lh[288]; /* length list for huft_build */static unsigned long ll[286 + 30]; /* literal/length and distance code lengths *//* more function prototypes */static unsigned long huft_build (unsigned long *, unsigned long, unsigned long, unsigned short *, unsigned short *, struct huft **, unsigned long *);static int inflate_codes_in_window (void);/* 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. */static unsigned longhuft_build (unsigned long *b, /* code lengths in bits (all assumed <= BMAX) */ unsigned long n, /* number of codes (assumed <= N_MAX) */ unsigned long s, /* number of simple-valued codes (0..s-1) */ unsigned short * d, /* list of base values for non-simple codes */ unsigned short * e, /* list of extra bits for non-simple codes */ struct huft **t, /* result: starting table */ unsigned long *m) /* maximum lookup bits, returns actual */{ unsigned long a; /* counter for codes of length k *///unsigned long c[BMAX + 1]; /* bit length count table */ unsigned long f; /* i repeats in table every f entries */ int g; /* maximum code length */ int h; /* table level */ register unsigned long i; /* counter, current code */ register unsigned long j; /* counter */ register int k; /* number of bits in current code */ unsigned long l; /* bits per table (returned in m) */ register unsigned long *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 long v[N_MAX]; /* values in order of bit length */ register int w; /* bits before this table == (l * h) *///unsigned long x[BMAX + 1]; /* bit offsets, then code stack */ unsigned long *xp; /* pointer into x */ int y; /* number of dummy codes added */ unsigned long z; /* number of entries in current table */ /* Generate counts for each bit length */ memset ((char *) c, 0, sizeof (c)); p = b; i = n; do { c[*p]++; /* assume all entries <= BMAX */ p++; /* Can't combine with above line (Solaris bug) */ } 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 */ l = *m; for (j = 1; j <= BMAX; j++) if (c[j]) break; k = j; /* minimum code length */ if (l < j) l = j; for (i = BMAX; i; i--) if (c[i]) break; g = i; /* maximum code length */ if (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 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 */ 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] = (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; /* previous table always l bits */ /* compute minimum size table less than or equal to l bits */ z = (z = g - w) > l ? l : z; /* upper limit on table size */ 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 */ } } z = 1 << j; /* table entries for j-bit table */ /* allocate and link in new table */ linalloc_topaddr -= (z + 1) * sizeof (struct huft); linalloc_topaddr &= ~3; q = (struct huft *) linalloc_topaddr; hufts += z + 1; /* track memory usage */ *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 = (unsigned char) l; /* bits to dump before this table */ r.e = (unsigned char) (16 + j); /* bits in this table */ r.v.t = 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.b = (unsigned char) (k - w); if (p >= v + n) r.e = 99; /* out of values--invalid code */ else if (*p < s) { r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ r.v.n = (unsigned short) (*p); /* simple code is just the value */ p++; /* one compiler does not like *p++ */ } else { r.e = (unsigned char) 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]) { h--; /* don't need to update q */ w -= l; } } } /* Return true (1) if we were given an incomplete table */ return y != 0 && g != 1;}/* * inflate (decompress) the codes in a deflated (compressed) block. * Return an error code or zero if it all goes ok. */static unsigned long inflate_n, inflate_d;static intinflate_codes_in_window (void){ register unsigned long e; /* table entry flag/number of extra bits */ unsigned long n, d; /* length and index for copy */ unsigned long w; /* current window position */ struct huft *t; /* pointer to table entry */ unsigned long ml, md; /* masks for bl and bd bits */ register unsigned long b; /* bit buffer */ register unsigned long k; /* number of bits in bit buffer */ /* make local copies of globals */ d = inflate_d; n = inflate_n; b = bb; /* initialize bit buffer */ k = bk; w = wp; /* initialize window position */ /* inflate the coded data */ ml = mask_bits[bl]; /* precompute masks for speed */ md = mask_bits[bd]; for (;;) /* do until end of block */ { if (!code_state) { NEEDBITS (bl); if ((e = (t = tl + (b & ml))->e) > 16) do { if (e == 99) { errnum = ERR_BAD_GZIP_DATA; return 0; } DUMPBITS (t->b); e -= 16; NEEDBITS (e); } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16); DUMPBITS (t->b); if (e == 16) /* then it's a literal */ { slide[w++] = (unsigned char)(t->v.n); if (w == WSIZE) break; } else /* it's an EOB or a length */ { /* exit if end of block */ if (e == 15) { block_len = 0; break; } /* get length of block to copy */ NEEDBITS (e); n = t->v.n + (b & mask_bits[e]); DUMPBITS (e); /* decode distance of block to copy */ NEEDBITS (bd); if ((e = (t = td + (b & md))->e) > 16) do { if (e == 99) { errnum = ERR_BAD_GZIP_DATA; return 0; } DUMPBITS (t->b); e -= 16; NEEDBITS (e); } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16); DUMPBITS (t->b); NEEDBITS (e); d = w - t->v.n - (b & mask_bits[e]); DUMPBITS (e); code_state++; } } if (code_state) { /* do the copy */ do { n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); if (w - d >= e) { memmove (slide + w, slide + d, e); w += e; d += e; } else /* purposefully use the overlap for extra copies here!! */ { while (e--) slide[w++] = slide[d++]; } if (w == WSIZE) break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -