1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/misc/inflate.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,916 @@
1.4 +/* inflate.c -- Not copyrighted 1992 by Mark Adler
1.5 + version c10p1, 10 January 1993 */
1.6 +
1.7 +/* You can do whatever you like with this source file, though I would
1.8 + prefer that if you modify it and redistribute it that you include
1.9 + comments to that effect with your name and the date. Thank you.
1.10 + [The history has been moved to the file ChangeLog.]
1.11 + */
1.12 +
1.13 +/*
1.14 + Inflate deflated (PKZIP's method 8 compressed) data. The compression
1.15 + method searches for as much of the current string of bytes (up to a
1.16 + length of 258) in the previous 32K bytes. If it doesn't find any
1.17 + matches (of at least length 3), it codes the next byte. Otherwise, it
1.18 + codes the length of the matched string and its distance backwards from
1.19 + the current position. There is a single Huffman code that codes both
1.20 + single bytes (called "literals") and match lengths. A second Huffman
1.21 + code codes the distance information, which follows a length code. Each
1.22 + length or distance code actually represents a base value and a number
1.23 + of "extra" (sometimes zero) bits to get to add to the base value. At
1.24 + the end of each deflated block is a special end-of-block (EOB) literal/
1.25 + length code. The decoding process is basically: get a literal/length
1.26 + code; if EOB then done; if a literal, emit the decoded byte; if a
1.27 + length then get the distance and emit the referred-to bytes from the
1.28 + sliding window of previously emitted data.
1.29 +
1.30 + There are (currently) three kinds of inflate blocks: stored, fixed, and
1.31 + dynamic. The compressor deals with some chunk of data at a time, and
1.32 + decides which method to use on a chunk-by-chunk basis. A chunk might
1.33 + typically be 32K or 64K. If the chunk is uncompressible, then the
1.34 + "stored" method is used. In this case, the bytes are simply stored as
1.35 + is, eight bits per byte, with none of the above coding. The bytes are
1.36 + preceded by a count, since there is no longer an EOB code.
1.37 +
1.38 + If the data is compressible, then either the fixed or dynamic methods
1.39 + are used. In the dynamic method, the compressed data is preceded by
1.40 + an encoding of the literal/length and distance Huffman codes that are
1.41 + to be used to decode this block. The representation is itself Huffman
1.42 + coded, and so is preceded by a description of that code. These code
1.43 + descriptions take up a little space, and so for small blocks, there is
1.44 + a predefined set of codes, called the fixed codes. The fixed method is
1.45 + used if the block codes up smaller that way (usually for quite small
1.46 + chunks), otherwise the dynamic method is used. In the latter case, the
1.47 + codes are customized to the probabilities in the current block, and so
1.48 + can code it much better than the pre-determined fixed codes.
1.49 +
1.50 + The Huffman codes themselves are decoded using a mutli-level table
1.51 + lookup, in order to maximize the speed of decoding plus the speed of
1.52 + building the decoding tables. See the comments below that precede the
1.53 + lbits and dbits tuning parameters.
1.54 + */
1.55 +
1.56 +
1.57 +/*
1.58 + Notes beyond the 1.93a appnote.txt:
1.59 +
1.60 + 1. Distance pointers never point before the beginning of the output
1.61 + stream.
1.62 + 2. Distance pointers can point back across blocks, up to 32k away.
1.63 + 3. There is an implied maximum of 7 bits for the bit length table and
1.64 + 15 bits for the actual data.
1.65 + 4. If only one code exists, then it is encoded using one bit. (Zero
1.66 + would be more efficient, but perhaps a little confusing.) If two
1.67 + codes exist, they are coded using one bit each (0 and 1).
1.68 + 5. There is no way of sending zero distance codes--a dummy must be
1.69 + sent if there are none. (History: a pre 2.0 version of PKZIP would
1.70 + store blocks with no distance codes, but this was discovered to be
1.71 + too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
1.72 + zero distance codes, which is sent as one code of zero bits in
1.73 + length.
1.74 + 6. There are up to 286 literal/length codes. Code 256 represents the
1.75 + end-of-block. Note however that the static length tree defines
1.76 + 288 codes just to fill out the Huffman codes. Codes 286 and 287
1.77 + cannot be used though, since there is no length base or extra bits
1.78 + defined for them. Similarly, there are up to 30 distance codes.
1.79 + However, static trees define 32 codes (all 5 bits) to fill out the
1.80 + Huffman codes, but the last two had better not show up in the data.
1.81 + 7. Unzip can check dynamic Huffman blocks for complete code sets.
1.82 + The exception is that a single code would not be complete (see #4).
1.83 + 8. The five bits following the block type is really the number of
1.84 + literal codes sent minus 257.
1.85 + 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
1.86 + (1+6+6). Therefore, to output three times the length, you output
1.87 + three codes (1+1+1), whereas to output four times the same length,
1.88 + you only need two codes (1+3). Hmm.
1.89 + 10. In the tree reconstruction algorithm, Code = Code + Increment
1.90 + only if BitLength(i) is not zero. (Pretty obvious.)
1.91 + 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
1.92 + 12. Note: length code 284 can represent 227-258, but length code 285
1.93 + really is 258. The last length deserves its own, short code
1.94 + since it gets used a lot in very redundant files. The length
1.95 + 258 is special since 258 - 3 (the min match length) is 255.
1.96 + 13. The literal/length and distance code bit lengths are read as a
1.97 + single stream of lengths. It is possible (and advantageous) for
1.98 + a repeat code (16, 17, or 18) to go across the boundary between
1.99 + the two sets of lengths.
1.100 + */
1.101 +
1.102 +#include "inflate.h"
1.103 +
1.104 +extern void* memcpy(void*, const void*, unsigned);
1.105 +extern void* memset(void*, int, unsigned);
1.106 +
1.107 +/* Huffman code lookup table entry--this entry is four bytes for machines
1.108 + that have 16-bit pointers (e.g. PC's in the small or medium model).
1.109 + Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
1.110 + means that v is a literal, 16 < e < 32 means that v is a pointer to
1.111 + the next table, which codes e - 16 bits, and lastly e == 99 indicates
1.112 + an unused code. If a code with e == 99 is looked up, this implies an
1.113 + error in the data. */
1.114 +struct huft {
1.115 + uch e; /* number of extra bits or operation */
1.116 + uch b; /* number of bits in this code or subcode */
1.117 + union {
1.118 + ush n; /* literal, length base, or distance base */
1.119 + struct huft *t; /* pointer to next level of table */
1.120 + } v;
1.121 +};
1.122 +
1.123 +
1.124 +/* Function prototypes */
1.125 +int huft_build(unsigned *, unsigned, unsigned, const ush *, const ush *,
1.126 + struct huft **, int *);
1.127 +int huft_free(struct huft *);
1.128 +int inflate_codes(struct huft *, struct huft *, int, int);
1.129 +int inflate_stored(void);
1.130 +int inflate_fixed(void);
1.131 +int inflate_dynamic(void);
1.132 +int inflate_block(int *);
1.133 +int inflate(void);
1.134 +
1.135 +
1.136 +/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
1.137 + stream to find repeated byte strings. This is implemented here as a
1.138 + circular buffer. The index is updated simply by incrementing and then
1.139 + and'ing with 0x7fff (32K-1). */
1.140 +/* It is left to other modules to supply the 32K area. It is assumed
1.141 + to be usable as if it were declared "uch slide[32768];" or as just
1.142 + "uch *slide;" and then malloc'ed in the latter case. The definition
1.143 + must be in unzip.h, included above. */
1.144 +/* unsigned wp; current position in slide */
1.145 +/*#define wp outcnt*/
1.146 +/*#define flush_output(w) (wp=(w),flush_window())*/
1.147 +
1.148 +/* Tables for deflate from PKZIP's appnote.txt. */
1.149 +static const unsigned border[] = { /* Order of the bit length code lengths */
1.150 + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1.151 +static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
1.152 + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1.153 + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1.154 + /* note: see note #13 above about the 258 in this list. */
1.155 +static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
1.156 + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1.157 + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
1.158 +static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
1.159 + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1.160 + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1.161 + 8193, 12289, 16385, 24577};
1.162 +static const ush cpdext[] = { /* Extra bits for distance codes */
1.163 + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1.164 + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1.165 + 12, 12, 13, 13};
1.166 +
1.167 +
1.168 +
1.169 +/* Macros for inflate() bit peeking and grabbing.
1.170 + The usage is:
1.171 +
1.172 + NEEDBITS(j)
1.173 + x = b & mask_bits[j];
1.174 + DUMPBITS(j)
1.175 +
1.176 + where NEEDBITS makes sure that b has at least j bits in it, and
1.177 + DUMPBITS removes the bits from b. The macros use the variable k
1.178 + for the number of bits in b. Normally, b and k are register
1.179 + variables for speed, and are initialized at the beginning of a
1.180 + routine that uses these macros from a global bit buffer and count.
1.181 +
1.182 + If we assume that EOB will be the longest code, then we will never
1.183 + ask for bits with NEEDBITS that are beyond the end of the stream.
1.184 + So, NEEDBITS should not read any more bytes than are needed to
1.185 + meet the request. Then no bytes need to be "returned" to the buffer
1.186 + at the end of the last block.
1.187 +
1.188 + However, this assumption is not true for fixed blocks--the EOB code
1.189 + is 7 bits, but the other literal/length codes can be 8 or 9 bits.
1.190 + (The EOB code is shorter than other codes because fixed blocks are
1.191 + generally short. So, while a block always has an EOB, many other
1.192 + literal/length codes have a significantly lower probability of
1.193 + showing up at all.) However, by making the first table have a
1.194 + lookup of seven bits, the EOB code will be found in that first
1.195 + lookup, and so will not require that too many bits be pulled from
1.196 + the stream.
1.197 + */
1.198 +
1.199 +ulg bb; /* bit buffer */
1.200 +unsigned bk; /* bits in bit buffer */
1.201 +
1.202 +static const ush mask_bits[] = {
1.203 + 0x0000,
1.204 + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
1.205 + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
1.206 +};
1.207 +
1.208 +#define get_byte() (inptr < inbuf_end ? *inptr++ : fill_inbuf())
1.209 +#define NEXTBYTE() (uch)get_byte()
1.210 +#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
1.211 +#define DUMPBITS(n) {b>>=(n);k-=(n);}
1.212 +
1.213 +
1.214 +/*
1.215 + Huffman code decoding is performed using a multi-level table lookup.
1.216 + The fastest way to decode is to simply build a lookup table whose
1.217 + size is determined by the longest code. However, the time it takes
1.218 + to build this table can also be a factor if the data being decoded
1.219 + is not very long. The most common codes are necessarily the
1.220 + shortest codes, so those codes dominate the decoding time, and hence
1.221 + the speed. The idea is you can have a shorter table that decodes the
1.222 + shorter, more probable codes, and then point to subsidiary tables for
1.223 + the longer codes. The time it costs to decode the longer codes is
1.224 + then traded against the time it takes to make longer tables.
1.225 +
1.226 + This results of this trade are in the variables lbits and dbits
1.227 + below. lbits is the number of bits the first level table for literal/
1.228 + length codes can decode in one step, and dbits is the same thing for
1.229 + the distance codes. Subsequent tables are also less than or equal to
1.230 + those sizes. These values may be adjusted either when all of the
1.231 + codes are shorter than that, in which case the longest code length in
1.232 + bits is used, or when the shortest code is *longer* than the requested
1.233 + table size, in which case the length of the shortest code in bits is
1.234 + used.
1.235 +
1.236 + There are two different values for the two tables, since they code a
1.237 + different number of possibilities each. The literal/length table
1.238 + codes 286 possible values, or in a flat code, a little over eight
1.239 + bits. The distance table codes 30 possible values, or a little less
1.240 + than five bits, flat. The optimum values for speed end up being
1.241 + about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1.242 + The optimum values may differ though from machine to machine, and
1.243 + possibly even between compilers. Your mileage may vary.
1.244 + */
1.245 +
1.246 +
1.247 +static const int lbits = 9; /* bits in base literal/length lookup table */
1.248 +static const int dbits = 6; /* bits in base distance lookup table */
1.249 +
1.250 +
1.251 +/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
1.252 +#define BMAX 16 /* maximum bit length of any code (16 for explode) */
1.253 +#define N_MAX 288 /* maximum number of codes in any set */
1.254 +
1.255 +
1.256 +unsigned hufts; /* track memory usage */
1.257 +
1.258 +
1.259 +int huft_build(
1.260 +unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
1.261 +unsigned n, /* number of codes (assumed <= N_MAX) */
1.262 +unsigned s, /* number of simple-valued codes (0..s-1) */
1.263 +const ush *d, /* list of base values for non-simple codes */
1.264 +const ush *e, /* list of extra bits for non-simple codes */
1.265 +struct huft **t, /* result: starting table */
1.266 +int *m /* maximum lookup bits, returns actual */
1.267 +)
1.268 +/* Given a list of code lengths and a maximum table size, make a set of
1.269 + tables to decode that set of codes. Return zero on success, one if
1.270 + the given code set is incomplete (the tables are still built in this
1.271 + case), two if the input is invalid (all zero length codes or an
1.272 + oversubscribed set of lengths), and three if not enough memory. */
1.273 +{
1.274 + unsigned a; /* counter for codes of length k */
1.275 + unsigned c[BMAX+1]; /* bit length count table */
1.276 + unsigned f; /* i repeats in table every f entries */
1.277 + int g; /* maximum code length */
1.278 + int h; /* table level */
1.279 + register unsigned i; /* counter, current code */
1.280 + register unsigned j; /* counter */
1.281 + register int k; /* number of bits in current code */
1.282 + int l; /* bits per table (returned in m) */
1.283 + register unsigned *p; /* pointer into c[], b[], or v[] */
1.284 + register struct huft *q; /* points to current table */
1.285 + struct huft r; /* table entry for structure assignment */
1.286 + struct huft *u[BMAX]; /* table stack */
1.287 + unsigned v[N_MAX]; /* values in order of bit length */
1.288 + register int w; /* bits before this table == (l * h) */
1.289 + unsigned x[BMAX+1]; /* bit offsets, then code stack */
1.290 + unsigned *xp; /* pointer into x */
1.291 + int y; /* number of dummy codes added */
1.292 + unsigned z; /* number of entries in current table */
1.293 +
1.294 +
1.295 + /* Generate counts for each bit length */
1.296 + memset(c, 0, sizeof(c));
1.297 + p = b; i = n;
1.298 + do {
1.299 +/* Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"),
1.300 + n-i, *p));*/
1.301 + c[*p]++; /* assume all entries <= BMAX */
1.302 + p++; /* Can't combine with above line (Solaris bug) */
1.303 + } while (--i);
1.304 + if (c[0] == n) /* null input--all zero length codes */
1.305 + {
1.306 + *t = (struct huft *)NULL;
1.307 + *m = 0;
1.308 + return 0;
1.309 + }
1.310 +
1.311 +
1.312 + /* Find minimum and maximum length, bound *m by those */
1.313 + l = *m;
1.314 + for (j = 1; j <= BMAX; j++)
1.315 + if (c[j])
1.316 + break;
1.317 + k = j; /* minimum code length */
1.318 + if ((unsigned)l < j)
1.319 + l = j;
1.320 + for (i = BMAX; i; i--)
1.321 + if (c[i])
1.322 + break;
1.323 + g = i; /* maximum code length */
1.324 + if ((unsigned)l > i)
1.325 + l = i;
1.326 + *m = l;
1.327 +
1.328 +
1.329 + /* Adjust last length count to fill out codes, if needed */
1.330 + for (y = 1 << j; j < i; j++, y <<= 1)
1.331 + if ((y -= c[j]) < 0)
1.332 + return 2; /* bad input: more codes than bits */
1.333 + if ((y -= c[i]) < 0)
1.334 + return 2;
1.335 + c[i] += y;
1.336 +
1.337 +
1.338 + /* Generate starting offsets into the value table for each length */
1.339 + x[1] = j = 0;
1.340 + p = c + 1; xp = x + 2;
1.341 + while (--i) { /* note that i == g from above */
1.342 + *xp++ = (j += *p++);
1.343 + }
1.344 +
1.345 +
1.346 + /* Make a table of values in order of bit lengths */
1.347 + p = b; i = 0;
1.348 + do {
1.349 + if ((j = *p++) != 0)
1.350 + v[x[j]++] = i;
1.351 + } while (++i < n);
1.352 +
1.353 +
1.354 + /* Generate the Huffman codes and for each, make the table entries */
1.355 + x[0] = i = 0; /* first Huffman code is zero */
1.356 + p = v; /* grab values in bit order */
1.357 + h = -1; /* no tables yet--level -1 */
1.358 + w = -l; /* bits decoded == (l * h) */
1.359 + u[0] = (struct huft *)NULL; /* just to keep compilers happy */
1.360 + q = (struct huft *)NULL; /* ditto */
1.361 + z = 0; /* ditto */
1.362 +
1.363 + /* go through the bit lengths (k already is bits in shortest code) */
1.364 + for (; k <= g; k++)
1.365 + {
1.366 + a = c[k];
1.367 + while (a--)
1.368 + {
1.369 + /* here i is the Huffman code of length k bits for value *p */
1.370 + /* make tables up to required level */
1.371 + while (k > w + l)
1.372 + {
1.373 + h++;
1.374 + w += l; /* previous table always l bits */
1.375 +
1.376 + /* compute minimum size table less than or equal to l bits */
1.377 + z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */
1.378 + if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
1.379 + { /* too few codes for k-w bit table */
1.380 + f -= a + 1; /* deduct codes from patterns left */
1.381 + xp = c + k;
1.382 + while (++j < z) /* try smaller tables up to z bits */
1.383 + {
1.384 + if ((f <<= 1) <= *++xp)
1.385 + break; /* enough codes to use up j bits */
1.386 + f -= *xp; /* else deduct codes from patterns */
1.387 + }
1.388 + }
1.389 + z = 1 << j; /* table entries for j-bit table */
1.390 +
1.391 + /* allocate and link in new table */
1.392 + if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
1.393 + (struct huft *)NULL)
1.394 + {
1.395 + if (h)
1.396 + huft_free(u[0]);
1.397 + return 3; /* not enough memory */
1.398 + }
1.399 + hufts += z + 1; /* track memory usage */
1.400 + *t = q + 1; /* link to list for huft_free() */
1.401 + *(t = &(q->v.t)) = (struct huft *)NULL;
1.402 + u[h] = ++q; /* table starts after link */
1.403 +
1.404 + /* connect to last table, if there is one */
1.405 + if (h)
1.406 + {
1.407 + x[h] = i; /* save pattern for backing up */
1.408 + r.b = (uch)l; /* bits to dump before this table */
1.409 + r.e = (uch)(16 + j); /* bits in this table */
1.410 + r.v.t = q; /* pointer to this table */
1.411 + j = i >> (w - l); /* (get around Turbo C bug) */
1.412 + u[h-1][j] = r; /* connect to last table */
1.413 + }
1.414 + }
1.415 +
1.416 + /* set up table entry in r */
1.417 + r.b = (uch)(k - w);
1.418 + if (p >= v + n)
1.419 + r.e = 99; /* out of values--invalid code */
1.420 + else if (*p < s)
1.421 + {
1.422 + r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
1.423 + r.v.n = (ush)(*p); /* simple code is just the value */
1.424 + p++; /* one compiler does not like *p++ */
1.425 + }
1.426 + else
1.427 + {
1.428 + r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
1.429 + r.v.n = d[*p++ - s];
1.430 + }
1.431 +
1.432 + /* fill code-like entries with r */
1.433 + f = 1 << (k - w);
1.434 + for (j = i >> w; j < z; j += f)
1.435 + q[j] = r;
1.436 +
1.437 + /* backwards increment the k-bit code i */
1.438 + for (j = 1 << (k - 1); i & j; j >>= 1)
1.439 + i ^= j;
1.440 + i ^= j;
1.441 +
1.442 + /* backup over finished tables */
1.443 + while ((i & ((1 << w) - 1)) != x[h])
1.444 + {
1.445 + h--; /* don't need to update q */
1.446 + w -= l;
1.447 + }
1.448 + }
1.449 + }
1.450 +
1.451 +
1.452 + /* Return true (1) if we were given an incomplete table */
1.453 + return y != 0 && g != 1;
1.454 +}
1.455 +
1.456 +
1.457 +
1.458 +int huft_free(struct huft *t)
1.459 +/* Free the malloc'ed tables built by huft_build(), which makes a linked
1.460 + list of the tables it made, with the links in a dummy first entry of
1.461 + each table. */
1.462 +{
1.463 + register struct huft *p, *q;
1.464 +
1.465 +
1.466 + /* Go through linked list, freeing from the malloced (t[-1]) address. */
1.467 + p = t;
1.468 + while (p != (struct huft *)NULL)
1.469 + {
1.470 + q = (--p)->v.t;
1.471 + free((char*)p);
1.472 + p = q;
1.473 + }
1.474 + return 0;
1.475 +}
1.476 +
1.477 +
1.478 +int inflate_codes(
1.479 +struct huft *tl,
1.480 +struct huft *td, /* literal/length and distance decoder tables */
1.481 +int bl,
1.482 +int bd /* number of bits decoded by tl[] and td[] */
1.483 +)
1.484 +/* inflate (decompress) the codes in a deflated (compressed) block.
1.485 + Return an error code or zero if it all goes ok. */
1.486 +{
1.487 + register unsigned e; /* table entry flag/number of extra bits */
1.488 + unsigned n, d; /* length and index for copy */
1.489 + struct huft *t; /* pointer to table entry */
1.490 + unsigned ml, md; /* masks for bl and bd bits */
1.491 + register ulg b=bb; /* bit buffer */
1.492 + register unsigned k=bk; /* number of bits in bit buffer */
1.493 + register uch* p=(uch*)outptr;
1.494 +
1.495 + /* inflate the coded data */
1.496 + ml = mask_bits[bl]; /* precompute masks for speed */
1.497 + md = mask_bits[bd];
1.498 + for (;;) /* do until end of block */
1.499 + {
1.500 + NEEDBITS((unsigned)bl)
1.501 + if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
1.502 + do {
1.503 + if (e == 99)
1.504 + return 1;
1.505 + DUMPBITS(t->b)
1.506 + e -= 16;
1.507 + NEEDBITS(e)
1.508 + } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
1.509 + DUMPBITS(t->b)
1.510 + if (e == 16) /* then it's a literal */
1.511 + {
1.512 + *p++ = (uch)t->v.n;
1.513 + }
1.514 + else /* it's an EOB or a length */
1.515 + {
1.516 + /* exit if end of block */
1.517 + if (e == 15)
1.518 + break;
1.519 +
1.520 + /* get length of block to copy */
1.521 + NEEDBITS(e)
1.522 + n = t->v.n + ((unsigned)b & mask_bits[e]);
1.523 + DUMPBITS(e);
1.524 +
1.525 + /* decode distance of block to copy */
1.526 + NEEDBITS((unsigned)bd)
1.527 + if ((e = (t = td + ((unsigned)b & md))->e) > 16)
1.528 + do {
1.529 + if (e == 99)
1.530 + return 1;
1.531 + DUMPBITS(t->b)
1.532 + e -= 16;
1.533 + NEEDBITS(e)
1.534 + } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
1.535 + DUMPBITS(t->b)
1.536 + NEEDBITS(e)
1.537 + d = t->v.n + ((unsigned)b & mask_bits[e]);
1.538 + d &= ZIP_WINDOW_SIZE-1;
1.539 + DUMPBITS(e)
1.540 +
1.541 + /* do the copy */
1.542 + if (d>=n)
1.543 + {
1.544 + memcpy(p, p-d, n);
1.545 + p+=n;
1.546 + }
1.547 + else
1.548 + {
1.549 + uch* q=p-d;
1.550 + while(n--) *p++=*q++;
1.551 + }
1.552 + }
1.553 + }
1.554 +
1.555 +
1.556 + /* restore the globals from the locals */
1.557 + outptr=p;
1.558 + bb = b; /* restore global bit buffer */
1.559 + bk = k;
1.560 +
1.561 + /* done */
1.562 + return 0;
1.563 +}
1.564 +
1.565 +
1.566 +
1.567 +int inflate_stored()
1.568 +/* "decompress" an inflated type 0 (stored) block. */
1.569 +{
1.570 + unsigned n; /* number of bytes in block */
1.571 + register ulg b; /* bit buffer */
1.572 + register unsigned k; /* number of bits in bit buffer */
1.573 +
1.574 + register uch* p=(uch*)outptr;
1.575 +
1.576 +
1.577 + /* make local copies of globals */
1.578 + b = bb; /* initialize bit buffer */
1.579 + k = bk;
1.580 +
1.581 +
1.582 + /* go to byte boundary */
1.583 + n = k & 7;
1.584 + DUMPBITS(n);
1.585 +
1.586 +
1.587 + /* get the length and its complement */
1.588 + NEEDBITS(16)
1.589 + n = ((unsigned)b & 0xffff);
1.590 + DUMPBITS(16)
1.591 + NEEDBITS(16)
1.592 + if (n != (unsigned)((~b) & 0xffff))
1.593 + return 1; /* error in compressed data */
1.594 + DUMPBITS(16)
1.595 +
1.596 +
1.597 + /* read and output the compressed data */
1.598 + while (n--)
1.599 + {
1.600 + NEEDBITS(8)
1.601 + *p++=(uch)b;
1.602 + DUMPBITS(8)
1.603 + }
1.604 +
1.605 +
1.606 + /* restore the globals from the locals */
1.607 + outptr=p;
1.608 + bb = b; /* restore global bit buffer */
1.609 + bk = k;
1.610 + return 0;
1.611 +}
1.612 +
1.613 +
1.614 +
1.615 +int inflate_fixed()
1.616 +/* decompress an inflated type 1 (fixed Huffman codes) block. We should
1.617 + either replace this with a custom decoder, or at least precompute the
1.618 + Huffman tables. */
1.619 +{
1.620 + int i; /* temporary variable */
1.621 + struct huft *tl; /* literal/length code table */
1.622 + struct huft *td; /* distance code table */
1.623 + int bl; /* lookup bits for tl */
1.624 + int bd; /* lookup bits for td */
1.625 + unsigned l[288]; /* length list for huft_build */
1.626 +
1.627 +
1.628 + /* set up literal table */
1.629 + for (i = 0; i < 144; i++)
1.630 + l[i] = 8;
1.631 + for (; i < 256; i++)
1.632 + l[i] = 9;
1.633 + for (; i < 280; i++)
1.634 + l[i] = 7;
1.635 + for (; i < 288; i++) /* make a complete, but wrong code set */
1.636 + l[i] = 8;
1.637 + bl = 7;
1.638 + if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
1.639 + return i;
1.640 +
1.641 +
1.642 + /* set up distance table */
1.643 + for (i = 0; i < 30; i++) /* make an incomplete code set */
1.644 + l[i] = 5;
1.645 + bd = 5;
1.646 + if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
1.647 + {
1.648 + huft_free(tl);
1.649 + return i;
1.650 + }
1.651 +
1.652 +
1.653 + /* decompress until an end-of-block code */
1.654 + if (inflate_codes(tl, td, bl, bd))
1.655 + return 1;
1.656 +
1.657 +
1.658 + /* free the decoding tables, return */
1.659 + huft_free(tl);
1.660 + huft_free(td);
1.661 + return 0;
1.662 +}
1.663 +
1.664 +
1.665 +
1.666 +int inflate_dynamic()
1.667 +/* decompress an inflated type 2 (dynamic Huffman codes) block. */
1.668 +{
1.669 + int i; /* temporary variables */
1.670 + unsigned j;
1.671 + unsigned l; /* last length */
1.672 + unsigned m; /* mask for bit lengths table */
1.673 + unsigned n; /* number of lengths to get */
1.674 + struct huft *tl; /* literal/length code table */
1.675 + struct huft *td; /* distance code table */
1.676 + int bl; /* lookup bits for tl */
1.677 + int bd; /* lookup bits for td */
1.678 + unsigned nb; /* number of bit length codes */
1.679 + unsigned nl; /* number of literal/length codes */
1.680 + unsigned nd; /* number of distance codes */
1.681 +#ifdef PKZIP_BUG_WORKAROUND
1.682 + unsigned ll[288+32]; /* literal/length and distance code lengths */
1.683 +#else
1.684 + unsigned ll[286+30]; /* literal/length and distance code lengths */
1.685 +#endif
1.686 + register ulg b; /* bit buffer */
1.687 + register unsigned k; /* number of bits in bit buffer */
1.688 +
1.689 +
1.690 + /* make local bit buffer */
1.691 + b = bb;
1.692 + k = bk;
1.693 +
1.694 +
1.695 + /* read in table lengths */
1.696 + NEEDBITS(5)
1.697 + nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
1.698 + DUMPBITS(5)
1.699 + NEEDBITS(5)
1.700 + nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
1.701 + DUMPBITS(5)
1.702 + NEEDBITS(4)
1.703 + nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
1.704 + DUMPBITS(4)
1.705 +#ifdef PKZIP_BUG_WORKAROUND
1.706 + if (nl > 288 || nd > 32)
1.707 +#else
1.708 + if (nl > 286 || nd > 30)
1.709 +#endif
1.710 + return 1; /* bad lengths */
1.711 +
1.712 +
1.713 + /* read in bit-length-code lengths */
1.714 + for (j = 0; j < nb; j++)
1.715 + {
1.716 + NEEDBITS(3)
1.717 + ll[border[j]] = (unsigned)b & 7;
1.718 + DUMPBITS(3)
1.719 + }
1.720 + for (; j < 19; j++)
1.721 + ll[border[j]] = 0;
1.722 +
1.723 +
1.724 + /* build decoding table for trees--single level, 7 bit lookup */
1.725 + bl = 7;
1.726 + if ((i = huft_build(ll, 19, 19, (ush*)NULL, (ush*)NULL, &tl, &bl)) != 0)
1.727 + {
1.728 + if (i == 1)
1.729 + huft_free(tl);
1.730 + return i; /* incomplete code set */
1.731 + }
1.732 +
1.733 +
1.734 + /* read in literal and distance code lengths */
1.735 + n = nl + nd;
1.736 + m = mask_bits[bl];
1.737 + i = l = 0;
1.738 + while ((unsigned)i < n)
1.739 + {
1.740 + NEEDBITS((unsigned)bl)
1.741 + j = (td = tl + ((unsigned)b & m))->b;
1.742 + DUMPBITS(j)
1.743 + j = td->v.n;
1.744 + if (j < 16) /* length of code in bits (0..15) */
1.745 + ll[i++] = l = j; /* save last length in l */
1.746 + else if (j == 16) /* repeat last length 3 to 6 times */
1.747 + {
1.748 + NEEDBITS(2)
1.749 + j = 3 + ((unsigned)b & 3);
1.750 + DUMPBITS(2)
1.751 + if ((unsigned)i + j > n)
1.752 + return 1;
1.753 + while (j--)
1.754 + ll[i++] = l;
1.755 + }
1.756 + else if (j == 17) /* 3 to 10 zero length codes */
1.757 + {
1.758 + NEEDBITS(3)
1.759 + j = 3 + ((unsigned)b & 7);
1.760 + DUMPBITS(3)
1.761 + if ((unsigned)i + j > n)
1.762 + return 1;
1.763 + while (j--)
1.764 + ll[i++] = 0;
1.765 + l = 0;
1.766 + }
1.767 + else /* j == 18: 11 to 138 zero length codes */
1.768 + {
1.769 + NEEDBITS(7)
1.770 + j = 11 + ((unsigned)b & 0x7f);
1.771 + DUMPBITS(7)
1.772 + if ((unsigned)i + j > n)
1.773 + return 1;
1.774 + while (j--)
1.775 + ll[i++] = 0;
1.776 + l = 0;
1.777 + }
1.778 + }
1.779 +
1.780 +
1.781 + /* free decoding table for trees */
1.782 + huft_free(tl);
1.783 +
1.784 +
1.785 + /* restore the global bit buffer */
1.786 + bb = b;
1.787 + bk = k;
1.788 +
1.789 +
1.790 + /* build the decoding tables for literal/length and distance codes */
1.791 + bl = lbits;
1.792 + if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
1.793 + {
1.794 + if (i == 1) {
1.795 +/* fprintf(stderr, " incomplete literal tree\n");*/
1.796 + huft_free(tl);
1.797 + }
1.798 + return i; /* incomplete code set */
1.799 + }
1.800 + bd = dbits;
1.801 + if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
1.802 + {
1.803 + if (i == 1) {
1.804 +/* fprintf(stderr, " incomplete distance tree\n");*/
1.805 +#ifdef PKZIP_BUG_WORKAROUND
1.806 + i = 0;
1.807 + }
1.808 +#else
1.809 + huft_free(td);
1.810 + }
1.811 + huft_free(tl);
1.812 + return i; /* incomplete code set */
1.813 +#endif
1.814 + }
1.815 +
1.816 +
1.817 + /* decompress until an end-of-block code */
1.818 + if (inflate_codes(tl, td, bl, bd))
1.819 + return 1;
1.820 +
1.821 +
1.822 + /* free the decoding tables, return */
1.823 + huft_free(tl);
1.824 + huft_free(td);
1.825 + return 0;
1.826 +}
1.827 +
1.828 +
1.829 +
1.830 +int inflate_block(int* e)
1.831 +/* decompress an inflated block */
1.832 +{
1.833 + unsigned t; /* block type */
1.834 + register ulg b; /* bit buffer */
1.835 + register unsigned k; /* number of bits in bit buffer */
1.836 +
1.837 +
1.838 + /* make local bit buffer */
1.839 + b = bb;
1.840 + k = bk;
1.841 +
1.842 +
1.843 + /* read in last block bit */
1.844 + NEEDBITS(1)
1.845 + *e = (int)b & 1;
1.846 + DUMPBITS(1)
1.847 +
1.848 +
1.849 + /* read in block type */
1.850 + NEEDBITS(2)
1.851 + t = (unsigned)b & 3;
1.852 + DUMPBITS(2)
1.853 +
1.854 +
1.855 + /* restore the global bit buffer */
1.856 + bb = b;
1.857 + bk = k;
1.858 +
1.859 +
1.860 + /* inflate that block type */
1.861 + if (t == 2)
1.862 + return inflate_dynamic();
1.863 + if (t == 0)
1.864 + return inflate_stored();
1.865 + if (t == 1)
1.866 + return inflate_fixed();
1.867 +
1.868 +
1.869 + /* bad block type */
1.870 + return 2;
1.871 +}
1.872 +
1.873 +
1.874 +
1.875 +int inflate()
1.876 +/* decompress an inflated entry */
1.877 +{
1.878 + int e; /* last block flag */
1.879 + int r; /* result code */
1.880 + unsigned h; /* maximum struct huft's malloc'ed */
1.881 +
1.882 +
1.883 + /* initialize window, bit buffer */
1.884 +/* wp = 0;*/
1.885 + bk = 0;
1.886 + bb = 0;
1.887 +
1.888 +
1.889 + /* decompress until the last block */
1.890 + h = 0;
1.891 + do {
1.892 + hufts = 0;
1.893 + r=inflate_block(&e);
1.894 + process_block(r);
1.895 + if (r!=0)
1.896 + return r;
1.897 + if (hufts > h)
1.898 + h = hufts;
1.899 + } while (!e);
1.900 +
1.901 + /* Undo too much lookahead. The next read will be byte aligned so we
1.902 + * can discard unused bits in the last meaningful byte.
1.903 + */
1.904 +/* while (bk >= 8) {
1.905 + bk -= 8;
1.906 + inptr--;
1.907 + }*/
1.908 +
1.909 + /* flush out slide */
1.910 +/* flush_output(wp);*/
1.911 +
1.912 +
1.913 + /* return success */
1.914 +#ifdef DEBUG
1.915 +/* fprintf(stderr, "<%u> ", h);*/
1.916 +#endif /* DEBUG */
1.917 +
1.918 + return 0;
1.919 +}