|
|
1.1.1.2 root 1: /* inflate.c -- Not copyrighted 1992 by Mark Adler
2: version c4, 15 April 1992 */
3:
4:
5: /* You can do whatever you like with this source file, though I would
6: prefer that if you modify it and redistribute it that you include
7: comments to that effect with your name and the date. Thank you.
8:
9: History:
10: vers date who what
11: ---- --------- -------------- ------------------------------------
12: a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
13: b1 21 Mar 92 M. Adler first version with partial lookup tables
14: b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
15: b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
16: b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
17: is the responsibility of unzip.h--also
18: changed name to slide[]), so needs diffs
19: for unzip.c and unzip.h (this allows
20: compiling in the small model on MSDOS);
21: fixed cast of q in huft_build();
22: b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
23: b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
24: bug in inflate_fixed().
25: c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
26: changed BMAX to 16 for explode. Removed
27: OUTB usage, and replaced it with flush()--
28: this was a 20% speed improvement! Added
29: an explode.c (to replace unimplode.c) that
30: uses the huft routines here. Removed
31: register union.
32: c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
33: c3 10 Apr 92 M. Adler reduced memory of code tables made by
34: huft_build significantly (factor of two to
35: three).
36: c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
37: worked around a Turbo C optimization bug.
38: c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
39: the 32K window size for specialized
40: applications.
41: c6 27 May 92 J.loup Gailly Adapted for pgp
42: */
43:
44:
45: /*
46: Inflate deflated (PKZIP's method 8 compressed) data. The compression
47: method searches for as much of the current string of bytes (up to a
48: length of 258) in the previous 32K bytes. If it doesn't find any
49: matches (of at least length 3), it codes the next byte. Otherwise, it
50: codes the length of the matched string and its distance backwards from
51: the current position. There is a single Huffman code that codes both
52: single bytes (called "literals") and match lengths. A second Huffman
53: code codes the distance information, which follows a length code. Each
54: length or distance code actually represents a base value and a number
55: of "extra" (sometimes zero) bits to get to add to the base value. At
56: the end of each deflated block is a special end-of-block (EOB) literal/
57: length code. The decoding process is basically: get a literal/length
58: code; if EOB then done; if a literal, emit the decoded byte; if a
59: length then get the distance and emit the referred-to bytes from the
60: sliding window of previously emitted data.
61:
62: There are (currently) three kinds of inflate blocks: stored, fixed, and
63: dynamic. The compressor deals with some chunk of data at a time, and
64: decides which method to use on a chunk-by-chunk basis. A chunk might
65: typically be 32K or 64K. If the chunk is uncompressible, then the
66: "stored" method is used. In this case, the bytes are simply stored as
67: is, eight bits per byte, with none of the above coding. The bytes are
68: preceded by a count, since there is no longer an EOB code.
69:
70: If the data is compressible, then either the fixed or dynamic methods
71: are used. In the dynamic method, the compressed data is preceded by
72: an encoding of the literal/length and distance Huffman codes that are
73: to be used to decode this block. The representation is itself Huffman
74: coded, and so is preceded by a description of that code. These code
75: descriptions take up a little space, and so for small blocks, there is
76: a predefined set of codes, called the fixed codes. The fixed method is
77: used if the block codes up smaller that way (usually for quite small
78: chunks), otherwise the dynamic method is used. In the latter case, the
79: codes are customized to the probabilities in the current block, and so
80: can code it much better than the pre-determined fixed codes.
81:
82: The Huffman codes themselves are decoded using a mutli-level table
83: lookup, in order to maximize the speed of decoding plus the speed of
84: building the decoding tables. See the comments below that precede the
85: lbits and dbits tuning parameters.
86: */
87:
88:
89: /*
90: Notes beyond the 1.93a appnote.txt:
91:
92: 1. Distance pointers never point before the beginning of the output
93: stream.
94: 2. Distance pointers can point back across blocks, up to 32k away.
95: 3. There is an implied maximum of 7 bits for the bit length table and
96: 15 bits for the actual data.
97: 4. If only one code exists, then it is encoded using one bit. (Zero
98: would be more efficient, but perhaps a little confusing.) If two
99: codes exist, they are coded using one bit each (0 and 1).
100: 5. There is no way of sending zero distance codes--a dummy must be
101: sent if there are none. (History: a pre 2.0 version of PKZIP would
102: store blocks with no distance codes, but this was discovered to be
103: too harsh a criterion.)
104: 6. There are up to 286 literal/length codes. Code 256 represents the
105: end-of-block. Note however that the static length tree defines
106: 288 codes just to fill out the Huffman codes. Codes 286 and 287
107: cannot be used though, since there is no length base or extra bits
108: defined for them. Similarily, there are up to 30 distance codes.
109: However, static trees define 32 codes (all 5 bits) to fill out the
110: Huffman codes, but the last two had better not show up in the data.
111: 7. Unzip can check dynamic huffman blocks for complete code sets.
112: The exception is that a single code would not be complete (see #4).
113: 8. The five bits following the block type is really the number of
114: literal codes sent minus 257.
115: 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
116: (1+6+6). Therefore, to output three times the length, you output
117: three codes (1+1+1), whereas to output four times the same length,
118: you only need two codes (1+3). Hmm.
119: 10. In the tree reconstruction algorithm, Code = Code + Increment
120: only if BitLength(i) is not zero. (Pretty obvious.)
121: 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
122: 12. Note: length code 284 can represent 227-258, but length code 285
123: really is 258. The last length deserves its own, short code
124: since it gets used a lot in very redundant files. The length
125: 258 is special since 258 - 3 (the min match length) is 255.
126: 13. The literal/length and distance code bit lengths are read as a
127: single stream of lengths. It is possible (and advantageous) for
128: a repeat code (16, 17, or 18) to go across the boundary between
129: the two sets of lengths.
130: */
131:
132: #include "zunzip.h"
1.1.1.3 ! root 133: #include "exitpgp.h"
1.1.1.2 root 134: #define OF __
135:
136: #ifndef WSIZE
137: # define WSIZE 8192
138: /* window size--must be a power of two, <= 32K, and equal to that of zip.
139: * On 16 bit machines (MSDOS), WSIZE must be <= 16K (32K is possible
140: * with a few hacks, see the zip archiver.
141: */
142: #endif
143:
144: #ifdef DYN_ALLOC
145: extern char *window;
146: #else
147: extern char window[];
148: #endif
149: #define slide window
150:
151: /* Huffman code lookup table entry--this entry is four bytes for machines
152: that have 16-bit pointers (e.g. PC's in the small or medium model).
153: Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
154: means that v is a literal, 16 < e < 32 means that v is a pointer to
155: the next table, which codes e - 16 bits, and lastly e == 99 indicates
156: an unused code. If a code with e == 99 is looked up, this implies an
157: error in the data. */
158: struct huft {
159: byte e; /* number of extra bits or operation */
160: byte b; /* number of bits in this code or subcode */
161: union {
162: UWORD n; /* literal, length base, or distance base */
163: struct huft *t; /* pointer to next level of table */
164: } v;
165: };
166:
167:
168: /* Function prototypes */
169: int huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *,
170: struct huft **, int *));
171: int huft_free OF((struct huft *));
172: void flush OF((unsigned));
173: int inflate_codes OF((struct huft *, struct huft *, int, int));
174: int inflate_stored OF((void));
175: int inflate_fixed OF((void));
176: int inflate_dynamic OF((void));
177: int inflate_block OF((int *));
178: int inflate_entry OF((void));
179: void inflate OF((void));
180:
181:
182: /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
183: stream to find repeated byte strings. This is implemented here as a
184: circular buffer. The index is updated simply by incrementing and then
185: and'ing with 0x7fff (32K-1). */
186: /* It is left to other modules to supply the 32K area. It is assumed
187: to be usable as if it were declared "byte slide[32768];" or as just
188: "byte *slide;" and then malloc'ed in the latter case. The definition
189: must be in unzip.h, included above. */
190: unsigned wp; /* current position in slide */
191:
192:
193: /* Tables for deflate from PKZIP's appnote.txt. */
194: static unsigned border[] = { /* Order of the bit length code lengths */
195: 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
196: static UWORD cplens[] = { /* Copy lengths for literal codes 257..285 */
197: 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
198: 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
199: /* note: see note #13 above about the 258 in this list. */
200: static UWORD cplext[] = { /* Extra bits for literal codes 257..285 */
201: 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
202: 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
203: static UWORD cpdist[] = { /* Copy offsets for distance codes 0..29 */
204: 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
205: 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
206: 8193, 12289, 16385, 24577};
207: static UWORD cpdext[] = { /* Extra bits for distance codes */
208: 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
209: 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
210: 12, 12, 13, 13};
211:
212:
213:
214: /* Macros for inflate() bit peeking and grabbing.
215: The usage is:
216:
217: NEEDBITS(j)
218: x = b & mask_bits[j];
219: DUMPBITS(j)
220:
221: where NEEDBITS makes sure that b has at least j bits in it, and
222: DUMPBITS removes the bits from b. The macros use the variable k
223: for the number of bits in b. Normally, b and k are register
224: variables for speed, and are initialized at the begining of a
225: routine that uses these macros from a global bit buffer and count.
226:
227: If we assume that EOB will be the longest code, then we will never
228: ask for bits with NEEDBITS that are beyond the end of the stream.
229: So, NEEDBITS should not read any more bytes than are needed to
230: meet the request. Then no bytes need to be "returned" to the buffer
231: at the end of the last block.
232:
233: However, this assumption is not true for fixed blocks--the EOB code
234: is 7 bits, but the other literal/length codes can be 8 or 9 bits.
235: (Why PK made the EOB code, which can only occur once in a block,
236: the *shortest* code in the set, I'll never know.) However, by
237: making the first table have a lookup of seven bits, the EOB code
238: will be found in that first lookup, and so will not require that too
239: many bits be pulled from the stream.
240: */
241:
242: ULONG bb; /* bit buffer */
243: unsigned bk; /* bits in bit buffer */
244:
245: UWORD bytebuf;
246: #define NEXTBYTE (ReadByte(&bytebuf), bytebuf)
247: #define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE)<<k;k+=8;}}
248: #define DUMPBITS(n) {b>>=(n);k-=(n);}
249:
250:
251: /*
252: Huffman code decoding is performed using a multi-level table lookup.
253: The fastest way to decode is to simply build a lookup table whose
254: size is determined by the longest code. However, the time it takes
255: to build this table can also be a factor if the data being decoded
256: is not very long. The most common codes are necessarily the
257: shortest codes, so those codes dominate the decoding time, and hence
258: the speed. The idea is you can have a shorter table that decodes the
259: shorter, more probable codes, and then point to subsidiary tables for
260: the longer codes. The time it costs to decode the longer codes is
261: then traded against the time it takes to make longer tables.
262:
263: This results of this trade are in the variables lbits and dbits
264: below. lbits is the number of bits the first level table for literal/
265: length codes can decode in one step, and dbits is the same thing for
266: the distance codes. Subsequent tables are also less than or equal to
267: those sizes. These values may be adjusted either when all of the
268: codes are shorter than that, in which case the longest code length in
269: bits is used, or when the shortest code is *longer* than the requested
270: table size, in which case the length of the shortest code in bits is
271: used.
272:
273: There are two different values for the two tables, since they code a
274: different number of possibilities each. The literal/length table
275: codes 286 possible values, or in a flat code, a little over eight
276: bits. The distance table codes 30 possible values, or a little less
277: than five bits, flat. The optimum values for speed end up being
278: about one bit more than those, so lbits is 8+1 and dbits is 5+1.
279: The optimum values may differ though from machine to machine, and
280: possibly even between compilers. Your mileage may vary.
281: */
282:
283:
284: int lbits = 9; /* bits in base literal/length lookup table */
285: int dbits = 6; /* bits in base distance lookup table */
286:
287:
288: /* If BMAX needs to be larger than 16, then h and x[] should be ULONG. */
289: #define BMAX 16 /* maximum bit length of any code (16 for explode) */
290: #define NMAX 288 /* maximum number of codes in any set */
291:
292:
293: unsigned hufts; /* track memory usage */
294:
295:
296: int huft_build(b, n, s, d, e, t, m)
297: unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
298: unsigned n; /* number of codes (assumed <= NMAX) */
299: unsigned s; /* number of simple-valued codes (0..s-1) */
300: UWORD *d; /* list of base values for non-simple codes */
301: UWORD *e; /* list of extra bits for non-simple codes */
302: struct huft **t; /* result: starting table */
303: int *m; /* maximum lookup bits, returns actual */
304: /* Given a list of code lengths and a maximum table size, make a set of
305: tables to decode that set of codes. Return zero on success, one if
306: the given code set is incomplete (the tables are still built in this
307: case), two if the input is invalid (all zero length codes or an
308: oversubscribed set of lengths), and three if not enough memory. */
309: {
310: unsigned a; /* counter for codes of length k */
311: unsigned c[BMAX+1]; /* bit length count table */
312: unsigned f; /* i repeats in table every f entries */
313: int g; /* maximum code length */
314: int h; /* table level */
315: register unsigned i; /* counter, current code */
316: register unsigned j; /* counter */
317: register int k; /* number of bits in current code */
318: int l; /* bits per table (returned in m) */
319: register unsigned *p; /* pointer into c[], b[], or v[] */
320: register struct huft *q; /* points to current table */
321: struct huft r; /* table entry for structure assignment */
322: struct huft *u[BMAX]; /* table stack */
323: unsigned v[NMAX]; /* values in order of bit length */
324: register int w; /* bits before this table == (l * h) */
325: unsigned x[BMAX+1]; /* bit offsets, then code stack */
326: unsigned *xp; /* pointer into x */
327: int y; /* number of dummy codes added */
328: unsigned z; /* number of entries in current table */
329:
330:
331: /* Generate counts for each bit length */
332: memset(c, 0, sizeof(c));
333: p = b; i = n;
334: do {
335: c[*p++]++; /* assume all entries <= BMAX */
336: } while (--i);
337: if (c[0] == n)
338: return 2; /* bad input--all zero length codes */
339:
340:
341: /* Find minimum and maximum length, bound *m by those */
342: l = *m;
343: for (j = 1; j <= BMAX; j++)
344: if (c[j])
345: break;
346: k = j; /* minimum code length */
347: if (l < j)
348: l = j;
349: for (i = BMAX; i; i--)
350: if (c[i])
351: break;
352: g = i; /* maximum code length */
353: if (l > i)
354: l = i;
355: *m = l;
356:
357:
358: /* Adjust last length count to fill out codes, if needed */
359: for (y = 1 << j; j < i; j++, y <<= 1)
360: if ((y -= c[j]) < 0)
361: return 2; /* bad input: more codes than bits */
362: if ((y -= c[i]) < 0)
363: return 2;
364: c[i] += y;
365:
366:
367: /* Generate starting offsets into the value table for each length */
368: x[1] = j = 0;
369: p = c + 1; xp = x + 2;
370: while (--i) { /* note that i == g from above */
371: *xp++ = (j += *p++);
372: }
373:
374:
375: /* Make a table of values in order of bit lengths */
376: p = b; i = 0;
377: do {
378: if ((j = *p++) != 0)
379: v[x[j]++] = i;
380: } while (++i < n);
381:
382:
383: /* Generate the Huffman codes and for each, make the table entries */
384: x[0] = i = 0; /* first Huffman code is zero */
385: p = v; /* grab values in bit order */
386: h = -1; /* no tables yet--level -1 */
387: w = -l; /* bits decoded == (l * h) */
388: u[0] = NULL; q = NULL; z = 0; /* just to keep compilers happy */
389:
390: /* go through the bit lengths (k already is bits in shortest code) */
391: for (; k <= g; k++)
392: {
393: a = c[k];
394: while (a--)
395: {
396: /* here i is the Huffman code of length k bits for value *p */
397: /* make tables up to required level */
398: while (k > w + l)
399: {
400: h++;
401: w += l; /* previous table always l bits */
402:
403: /* compute minimum size table less than or equal to l bits */
404: z = (z = g - w) > l ? l : z; /* upper limit on table size */
405: if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
406: { /* too few codes for k-w bit table */
407: f -= a + 1; /* deduct codes from patterns left */
408: xp = c + k;
409: while (++j < z) /* try smaller tables up to z bits */
410: {
411: if ((f <<= 1) <= *++xp)
412: break; /* enough codes to use up j bits */
413: f -= *xp; /* else deduct codes from patterns */
414: }
415: }
416: z = 1 << j; /* table entries for j-bit table */
417:
418: /* allocate and link in new table */
419: if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == NULL)
420: {
421: if (h)
422: huft_free(u[0]);
423: fprintf(stderr, "\n*** inflate out of memory *** ");
424: return 3; /* not enough memory */
425: }
426: hufts += z + 1; /* track memory usage */
427: *t = q + 1; /* link to list for huft_free() */
428: *(t = &(q->v.t)) = NULL;
429: u[h] = ++q; /* table starts after link */
430:
431: /* connect to last table, if there is one */
432: if (h)
433: {
434: x[h] = i; /* save pattern for backing up */
435: r.b = l; /* bits to dump before this table */
436: r.e = 16 + j; /* bits in this table */
437: r.v.t = q; /* pointer to this table */
438: j = i >> (w - l); /* (get around Turbo C bug) */
439: u[h-1][j] = r; /* connect to last table */
440: }
441: }
442:
443: /* set up table entry in r */
444: r.b = k - w;
445: if (p >= v + n)
446: r.e = 99; /* out of values--invalid code */
447: else if (*p < s)
448: {
449: r.e = *p < 256 ? 16 : 15; /* 256 is end-of-block code */
450: r.v.n = *p++; /* simple code is just the value */
451: }
452: else
453: {
454: r.e = e[*p - s]; /* non-simple--look up in lists */
455: r.v.n = d[*p++ - s];
456: }
457:
458: /* fill code-like entries with r */
459: f = 1 << (k - w);
460: for (j = i >> w; j < z; j += f)
461: q[j] = r;
462:
463: /* backwards increment the k-bit code i */
464: for (j = 1 << (k - 1); i & j; j >>= 1)
465: i ^= j;
466: i ^= j;
467:
468: /* backup over finished tables */
469: while ((i & ((1 << w) - 1)) != x[h])
470: {
471: h--; /* don't need to update q */
472: w -= l;
473: }
474: }
475: }
476:
477:
478: /* Return true (1) if we were given an incomplete table */
479: return y != 0 && n != 1;
480: }
481:
482:
483:
484: int huft_free(t)
485: struct huft *t; /* table to free */
486: /* Free the malloc'ed tables built by huft_build(), which makes a linked
487: list of the tables it made, with the links in a dummy first entry of
488: each table. */
489: {
490: register struct huft *p, *q;
491:
492:
493: /* Go through linked list, freeing from the malloced (t[-1]) address. */
494: p = t;
495: while (p != NULL)
496: {
497: q = (--p)->v.t;
498: free(p);
499: p = q;
500: }
501: return 0;
502: }
503:
504:
505:
506: void flush(w)
507: unsigned w; /* number of bytes to flush */
508: /* Do the equivalent of OUTB for the bytes slide[0..w-1]. */
509: {
510: unsigned n;
511: byte *p;
512:
513: p = (byte*)slide;
514: while (w)
515: {
516: n = (n = OUTBUFSIZ - outcnt) < w ? n : w;
517: memcpy(outptr, p, n); /* try to fill up buffer */
518: outptr += n;
519: if ((outcnt += n) == OUTBUFSIZ)
520: if (FlushOutput()) /* if full, empty */
521: {
522: fprintf(stderr, "\nWrite error.\n");
523: exitPGP(1);
524: }
525: p += n;
526: w -= n;
527: }
528: }
529:
530:
531:
532: int inflate_codes(tl, td, bl, bd)
533: struct huft *tl, *td; /* literal/length and distance decoder tables */
534: int bl, bd; /* number of bits decoded by tl[] and td[] */
535: /* inflate (decompress) the codes in a deflated (compressed) block.
536: Return an error code or zero if it all goes ok. */
537: {
538: register unsigned e; /* table entry flag/number of extra bits */
539: unsigned n, d; /* length and index for copy */
540: unsigned w; /* current window position */
541: struct huft *t; /* pointer to table entry */
542: ULONG ml, md; /* masks for bl and bd bits */
543: register ULONG b; /* bit buffer */
544: register unsigned k; /* number of bits in bit buffer */
545:
546:
547: /* make local copies of globals */
548: b = bb; /* initialize bit buffer */
549: k = bk;
550: w = wp; /* initialize window position */
551:
552:
553: /* inflate the coded data */
554: ml = mask_bits[bl]; /* precompute masks for speed */
555: md = mask_bits[bd];
556: while (1) /* do until end of block */
557: {
558: NEEDBITS(bl)
559: if ((e = (t = tl + (b & ml))->e) > 16)
560: do {
561: if (e == 99)
562: return 1;
563: DUMPBITS(t->b)
564: e -= 16;
565: NEEDBITS(e)
566: } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16);
567: DUMPBITS(t->b)
568: if (e == 16) /* then it's a literal */
569: {
570: slide[w++] = t->v.n;
571: if (w == WSIZE)
572: {
573: flush(w);
574: w = 0;
575: }
576: }
577: else /* it's an EOB or a length */
578: {
579: /* exit if end of block */
580: if (e == 15)
581: break;
582:
583: /* get length of block to copy */
584: NEEDBITS(e)
585: n = t->v.n + (b & mask_bits[e]);
586: DUMPBITS(e);
587:
588: /* decode distance of block to copy */
589: NEEDBITS(bd)
590: if ((e = (t = td + (b & md))->e) > 16)
591: do {
592: if (e == 99)
593: return 1;
594: DUMPBITS(t->b)
595: e -= 16;
596: NEEDBITS(e)
597: } while ((e = (t = t->v.t + (b & mask_bits[e]))->e) > 16);
598: DUMPBITS(t->b)
599: NEEDBITS(e)
600: d = w - t->v.n - (b & mask_bits[e]);
601: DUMPBITS(e)
602:
603: /* do the copy */
604: do {
605: n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
606: #ifndef NOMEMCPY
607: if (w - d >= e) /* (this test assumes unsigned comparison) */
608: {
609: memcpy(slide + w, slide + d, e);
610: w += e;
611: d += e;
612: }
613: else /* do it slow to avoid memcpy() overlap */
614: #endif /* !NOMEMCPY */
615: do {
616: slide[w++] = slide[d++];
617: } while (--e);
618: if (w == WSIZE)
619: {
620: flush(w);
621: w = 0;
622: }
623: } while (n);
624: }
625: }
626:
627:
628: /* restore the globals from the locals */
629: wp = w; /* restore global window pointer */
630: bb = b; /* restore global bit buffer */
631: bk = k;
632:
633:
634: /* done */
635: return 0;
636: }
637:
638:
639:
640: int inflate_stored()
641: /* "decompress" an inflated type 0 (stored) block. */
642: {
643: unsigned n; /* number of bytes in block */
644: unsigned w; /* current window position */
645: register ULONG b; /* bit buffer */
646: register unsigned k; /* number of bits in bit buffer */
647:
648:
649: /* make local copies of globals */
650: b = bb; /* initialize bit buffer */
651: k = bk;
652: w = wp; /* initialize window position */
653:
654:
655: /* go to byte boundary */
656: n = k & 7;
657: DUMPBITS(n);
658:
659:
660: /* get the length and its complement */
661: NEEDBITS(16)
662: n = b & 0xffff;
663: DUMPBITS(16)
664: NEEDBITS(16)
665: if (n != ((~b) & 0xffff))
666: return 1; /* error in compressed data */
667: DUMPBITS(16)
668:
669:
670: /* read and output the compressed data */
671: while (n--)
672: {
673: NEEDBITS(8)
674: slide[w++] = b;
675: if (w == WSIZE)
676: {
677: flush(w);
678: w = 0;
679: }
680: DUMPBITS(8)
681: }
682:
683:
684: /* restore the globals from the locals */
685: wp = w; /* restore global window pointer */
686: bb = b; /* restore global bit buffer */
687: bk = k;
688: return 0;
689: }
690:
691:
692:
693: int inflate_fixed()
694: /* decompress an inflated type 1 (fixed Huffman codes) block. We should
695: either replace this with a custom decoder, or at least precompute the
696: Huffman tables. */
697: {
698: int i; /* temporary variable */
699: struct huft *tl; /* literal/length code table */
700: struct huft *td; /* distance code table */
701: int bl; /* lookup bits for tl */
702: int bd; /* lookup bits for td */
703: unsigned l[288]; /* length list for huft_build */
704:
705:
706: /* set up literal table */
707: for (i = 0; i < 144; i++)
708: l[i] = 8;
709: for (; i < 256; i++)
710: l[i] = 9;
711: for (; i < 280; i++)
712: l[i] = 7;
713: for (; i < 288; i++) /* make a complete, but wrong code set */
714: l[i] = 8;
715: bl = 7;
716: if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
717: return i;
718:
719:
720: /* set up distance table */
721: for (i = 0; i < 30; i++) /* make an incomplete code set */
722: l[i] = 5;
723: bd = 5;
724: if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
725: {
726: huft_free(tl);
727: return i;
728: }
729:
730:
731: /* decompress until an end-of-block code */
732: if (inflate_codes(tl, td, bl, bd))
733: return 1;
734:
735:
736: /* free the decoding tables, return */
737: huft_free(tl);
738: huft_free(td);
739: return 0;
740: }
741:
742:
743:
744: int inflate_dynamic()
745: /* decompress an inflated type 2 (dynamic Huffman codes) block. */
746: {
747: int i; /* temporary variables */
748: unsigned j;
749: unsigned l; /* last length */
750: unsigned m; /* mask for bit lengths table */
751: unsigned n; /* number of lengths to get */
752: struct huft *tl; /* literal/length code table */
753: struct huft *td; /* distance code table */
754: int bl; /* lookup bits for tl */
755: int bd; /* lookup bits for td */
756: unsigned nb; /* number of bit length codes */
757: unsigned nl; /* number of literal/length codes */
758: unsigned nd; /* number of distance codes */
759: unsigned ll[286+30]; /* literal/length and distance code lengths */
760: register ULONG b; /* bit buffer */
761: register unsigned k; /* number of bits in bit buffer */
762:
763:
764: /* make local bit buffer */
765: b = bb;
766: k = bk;
767:
768:
769: /* read in table lengths */
770: NEEDBITS(5)
771: nl = 257 + (b & 0x1f); /* number of literal/length codes */
772: DUMPBITS(5)
773: NEEDBITS(5)
774: nd = 1 + (b & 0x1f); /* number of distance codes */
775: DUMPBITS(5)
776: NEEDBITS(4)
777: nb = 4 + (b & 0xf); /* number of bit length codes */
778: DUMPBITS(4)
779: if (nl > 286 || nd > 30)
780: return 1; /* bad lengths */
781:
782:
783: /* read in bit-length-code lengths */
784: for (i = 0; i < nb; i++)
785: {
786: NEEDBITS(3)
787: ll[border[i]] = b & 7;
788: DUMPBITS(3)
789: }
790: for (; i < 19; i++)
791: ll[border[i]] = 0;
792:
793:
794: /* build decoding table for trees--single level, 7 bit lookup */
795: bl = 7;
796: if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
797: {
798: if (i == 1)
799: huft_free(tl);
800: return i; /* incomplete code set */
801: }
802:
803:
804: /* read in literal and distance code lengths */
805: n = nl + nd;
806: m = mask_bits[bl];
807: i = l = 0;
808: while (i < n)
809: {
810: NEEDBITS(bl)
811: j = (td = tl + (b & m))->b;
812: DUMPBITS(j)
813: j = td->v.n;
814: if (j < 16) /* length of code in bits (0..15) */
815: ll[i++] = l = j; /* save last length in l */
816: else if (j == 16) /* repeat last length 3 to 6 times */
817: {
818: NEEDBITS(2)
819: j = 3 + (b & 3);
820: DUMPBITS(2)
821: if (i + j > n)
822: return 1;
823: while (j--)
824: ll[i++] = l;
825: }
826: else if (j == 17) /* 3 to 10 zero length codes */
827: {
828: NEEDBITS(3)
829: j = 3 + (b & 7);
830: DUMPBITS(3)
831: if (i + j > n)
832: return 1;
833: while (j--)
834: ll[i++] = 0;
835: l = 0;
836: }
837: else /* j == 18: 11 to 138 zero length codes */
838: {
839: NEEDBITS(7)
840: j = 11 + (b & 0x7f);
841: DUMPBITS(7)
842: if (i + j > n)
843: return 1;
844: while (j--)
845: ll[i++] = 0;
846: l = 0;
847: }
848: }
849:
850:
851: /* free decoding table for trees */
852: huft_free(tl);
853:
854:
855: /* restore the global bit buffer */
856: bb = b;
857: bk = k;
858:
859:
860: /* build the decoding tables for literal/length and distance codes */
861: bl = lbits;
862: if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
863: {
864: if (i == 1)
865: huft_free(tl);
866: return i; /* incomplete code set */
867: }
868: bd = dbits;
869: if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
870: {
871: if (i == 1)
872: huft_free(td);
873: huft_free(tl);
874: return i; /* incomplete code set */
875: }
876:
877:
878: /* decompress until an end-of-block code */
879: if (inflate_codes(tl, td, bl, bd))
880: return 1;
881:
882:
883: /* free the decoding tables, return */
884: huft_free(tl);
885: huft_free(td);
886: return 0;
887: }
888:
889:
890:
891: int inflate_block(e)
892: int *e; /* last block flag */
893: /* decompress an inflated block */
894: {
895: unsigned t; /* block type */
896: register ULONG b; /* bit buffer */
897: register unsigned k; /* number of bits in bit buffer */
898:
899:
900: /* make local bit buffer */
901: b = bb;
902: k = bk;
903:
904:
905: /* read in last block bit */
906: NEEDBITS(1)
907: *e = b & 1;
908: DUMPBITS(1)
909:
910:
911: /* read in block type */
912: NEEDBITS(2)
913: t = b & 3;
914: DUMPBITS(2)
915:
916:
917: /* restore the global bit buffer */
918: bb = b;
919: bk = k;
920:
921:
922: /* inflate that block type */
923: if (t == 2)
924: return inflate_dynamic();
925: if (t == 0)
926: return inflate_stored();
927: if (t == 1)
928: return inflate_fixed();
929:
930:
931: /* bad block type */
932: return 2;
933: }
934:
935:
936:
937: int inflate_entry()
938: /* decompress an inflated entry */
939: {
940: int e; /* last block flag */
941: int r; /* result code */
942: unsigned h; /* maximum struct huft's malloc'ed */
943:
944:
945: /* initialize window, bit buffer */
946: wp = 0;
947: bk = 0;
948: bb = 0;
949:
950:
951: /* decompress until the last block */
952: h = 0;
953: do {
954: hufts = 0;
955: if ((r = inflate_block(&e)) != 0)
956: return r;
957: if (hufts > h)
958: h = hufts;
959: } while (!e);
960:
961:
962: /* flush out slide */
963: flush(wp);
964:
965:
966: /* return success */
967: #ifdef DEBUG
968: fprintf(stderr, "<%u> ", h);
969: #endif /* DEBUG */
970: return 0;
971: }
972:
973:
974: void inflate()
975: /* ignore the return code for now ... */
976: {
977: #ifdef DYN_ALLOC
1.1.1.3 ! root 978: window = (char*) calloc((unsigned)WSIZE, 2*sizeof(char));
1.1.1.2 root 979: /* Note that inflate only needs WSIZE bytes, but the window
980: * array is shared with deflate, which needs 2*WISZE bytes.
981: */
1.1.1.3 ! root 982: if (window==NULL) err(4, "");
1.1.1.2 root 983: #endif
984:
985: inflate_entry();
1.1.1.3 ! root 986:
! 987: #ifdef DYN_ALLOC
! 988: free(window);
! 989: window = NULL;
! 990: #endif
1.1.1.2 root 991: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.