2 * Most parts of this file are not covered by:
3 * ----------------------------------------------------------------------------
4 * "THE BEER-WARE LICENSE" (Revision 42):
5 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
6 * can do whatever you want with this stuff. If we meet some day, and you think
7 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
8 * ----------------------------------------------------------------------------
11 #include <sys/cdefs.h>
12 __FBSDID("$FreeBSD$");
14 #include <sys/param.h>
15 #include <sys/inflate.h>
17 #include <sys/systm.h>
18 #include <sys/kernel.h>
20 #include <sys/malloc.h>
23 static MALLOC_DEFINE(M_GZIP
, "gzip_trees", "Gzip trees");
26 /* needed to make inflate() work */
31 /* Stuff to make inflate() work */
33 #define memzero(dest,len) bzero(dest,len)
37 #define FPRINTF printf
39 extern void putstr (char *);
40 #define FPRINTF putstr
43 #define FLUSH(x,y) { \
44 int foo = (*x->gz_output)(x->gz_private,x->gz_slide,y); \
49 static const int qflag
= 0;
51 #ifndef _KERNEL /* want to use this file in kzip also */
52 extern unsigned char *kzipmalloc (int);
53 extern void kzipfree (void*);
54 #define malloc(x, y, z) kzipmalloc((x))
55 #define free(x, y) kzipfree((x))
59 * This came from unzip-5.12. I have changed it the flow to pass
60 * a structure pointer around, thus hopefully making it re-entrant.
64 /* inflate.c -- put in the public domain by Mark Adler
65 version c14o, 23 August 1994 */
67 /* You can do whatever you like with this source file, though I would
68 prefer that if you modify it and redistribute it that you include
69 comments to that effect with your name and the date. Thank you.
73 ---- --------- -------------- ------------------------------------
74 a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
75 b1 21 Mar 92 M. Adler first version with partial lookup tables
76 b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
77 b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
78 b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
79 is the responsibility of unzip.h--also
80 changed name to slide[]), so needs diffs
81 for unzip.c and unzip.h (this allows
82 compiling in the small model on MSDOS);
83 fixed cast of q in huft_build();
84 b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
85 b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
86 bug in inflate_fixed().
87 c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
88 changed BMAX to 16 for explode. Removed
89 OUTB usage, and replaced it with flush()--
90 this was a 20% speed improvement! Added
91 an explode.c (to replace unimplod.c) that
92 uses the huft routines here. Removed
94 c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
95 c3 10 Apr 92 M. Adler reduced memory of code tables made by
96 huft_build significantly (factor of two to
98 c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
99 worked around a Turbo C optimization bug.
100 c5 21 Apr 92 M. Adler added the GZ_WSIZE #define to allow reducing
101 the 32K window size for specialized
103 c6 31 May 92 M. Adler added some typecasts to eliminate warnings
104 c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug).
105 c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug.
106 c9 9 Oct 92 M. Adler removed a memory error message (~line 416).
107 c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch,
108 removed old inflate, renamed inflate_entry
109 to inflate, added Mark's fix to a comment.
110 c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees.
111 c11 2 Jan 93 M. Adler fixed bug in detection of incomplete
112 tables, and removed assumption that EOB is
113 the longest code (bad assumption).
114 c12 3 Jan 93 M. Adler make tables for fixed blocks only once.
115 c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c
116 outputs one zero length code for an empty
118 c14 12 Mar 93 M. Adler made inflate.c standalone with the
119 introduction of inflate.h.
120 c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470.
121 c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays
123 c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing.
124 c14e 8 Oct 93 G. Roelofs changed memset() to memzero().
125 c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace()
126 conditional; added inflate_free().
127 c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug)
128 c14h 7 Dec 93 C. Ghisler huft_build() optimizations.
129 c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing;
130 G. Roelofs check NEXTBYTE macro for GZ_EOF.
131 c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd
133 c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings.
134 c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
135 to avoid bug in Encore compiler.
136 c14m 7 Jul 94 P. Kienitz modified to allow assembler version of
137 inflate_codes() (define ASM_INFLATECODES)
138 c14n 22 Jul 94 G. Roelofs changed fprintf to FPRINTF for DLL versions
139 c14o 23 Aug 94 C. Spieler added a newline to a debug statement;
140 G. Roelofs added another typecast to avoid MSC warning
145 Inflate deflated (PKZIP's method 8 compressed) data. The compression
146 method searches for as much of the current string of bytes (up to a
147 length of 258) in the previous 32K bytes. If it doesn't find any
148 matches (of at least length 3), it codes the next byte. Otherwise, it
149 codes the length of the matched string and its distance backwards from
150 the current position. There is a single Huffman code that codes both
151 single bytes (called "literals") and match lengths. A second Huffman
152 code codes the distance information, which follows a length code. Each
153 length or distance code actually represents a base value and a number
154 of "extra" (sometimes zero) bits to get to add to the base value. At
155 the end of each deflated block is a special end-of-block (EOB) literal/
156 length code. The decoding process is basically: get a literal/length
157 code; if EOB then done; if a literal, emit the decoded byte; if a
158 length then get the distance and emit the referred-to bytes from the
159 sliding window of previously emitted data.
161 There are (currently) three kinds of inflate blocks: stored, fixed, and
162 dynamic. The compressor outputs a chunk of data at a time and decides
163 which method to use on a chunk-by-chunk basis. A chunk might typically
164 be 32K to 64K, uncompressed. If the chunk is uncompressible, then the
165 "stored" method is used. In this case, the bytes are simply stored as
166 is, eight bits per byte, with none of the above coding. The bytes are
167 preceded by a count, since there is no longer an EOB code.
169 If the data is compressible, then either the fixed or dynamic methods
170 are used. In the dynamic method, the compressed data is preceded by
171 an encoding of the literal/length and distance Huffman codes that are
172 to be used to decode this block. The representation is itself Huffman
173 coded, and so is preceded by a description of that code. These code
174 descriptions take up a little space, and so for small blocks, there is
175 a predefined set of codes, called the fixed codes. The fixed method is
176 used if the block ends up smaller that way (usually for quite small
177 chunks); otherwise the dynamic method is used. In the latter case, the
178 codes are customized to the probabilities in the current block and so
179 can code it much better than the pre-determined fixed codes can.
181 The Huffman codes themselves are decoded using a mutli-level table
182 lookup, in order to maximize the speed of decoding plus the speed of
183 building the decoding tables. See the comments below that precede the
184 lbits and dbits tuning parameters.
189 Notes beyond the 1.93a appnote.txt:
191 1. Distance pointers never point before the beginning of the output
193 2. Distance pointers can point back across blocks, up to 32k away.
194 3. There is an implied maximum of 7 bits for the bit length table and
195 15 bits for the actual data.
196 4. If only one code exists, then it is encoded using one bit. (Zero
197 would be more efficient, but perhaps a little confusing.) If two
198 codes exist, they are coded using one bit each (0 and 1).
199 5. There is no way of sending zero distance codes--a dummy must be
200 sent if there are none. (History: a pre 2.0 version of PKZIP would
201 store blocks with no distance codes, but this was discovered to be
202 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
203 zero distance codes, which is sent as one code of zero bits in
205 6. There are up to 286 literal/length codes. Code 256 represents the
206 end-of-block. Note however that the static length tree defines
207 288 codes just to fill out the Huffman codes. Codes 286 and 287
208 cannot be used though, since there is no length base or extra bits
209 defined for them. Similarily, there are up to 30 distance codes.
210 However, static trees define 32 codes (all 5 bits) to fill out the
211 Huffman codes, but the last two had better not show up in the data.
212 7. Unzip can check dynamic Huffman blocks for complete code sets.
213 The exception is that a single code would not be complete (see #4).
214 8. The five bits following the block type is really the number of
215 literal codes sent minus 257.
216 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
217 (1+6+6). Therefore, to output three times the length, you output
218 three codes (1+1+1), whereas to output four times the same length,
219 you only need two codes (1+3). Hmm.
220 10. In the tree reconstruction algorithm, Code = Code + Increment
221 only if BitLength(i) is not zero. (Pretty obvious.)
222 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
223 12. Note: length code 284 can represent 227-258, but length code 285
224 really is 258. The last length deserves its own, short code
225 since it gets used a lot in very redundant files. The length
226 258 is special since 258 - 3 (the min match length) is 255.
227 13. The literal/length and distance code bit lengths are read as a
228 single stream of lengths. It is possible (and advantageous) for
229 a repeat code (16, 17, or 18) to go across the boundary between
230 the two sets of lengths.
234 #define PKZIP_BUG_WORKAROUND /* PKZIP 1.93a problem--live with it */
237 inflate.h must supply the uch slide[GZ_WSIZE] array and the NEXTBYTE,
238 FLUSH() and memzero macros. If the window size is not 32K, it
239 should also define GZ_WSIZE. If INFMOD is defined, it can include
240 compiled functions to support the NEXTBYTE and/or FLUSH() macros.
241 There are defaults for NEXTBYTE and FLUSH() below for use as
242 examples of what those functions need to do. Normally, you would
243 also want FLUSH() to compute a crc on the data. inflate.h also
244 needs to provide these typedefs:
246 typedef unsigned char uch;
247 typedef unsigned short ush;
248 typedef unsigned long ulg;
250 This module uses the external functions malloc() and free() (and
251 probably memset() or bzero() in the memzero() macro). Their
252 prototypes are normally found in <string.h> and <stdlib.h>.
254 #define INFMOD /* tell inflate.h to include code to be
257 /* Huffman code lookup table entry--this entry is four bytes for machines
258 that have 16-bit pointers (e.g. PC's in the small or medium model).
259 Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
260 means that v is a literal, 16 < e < 32 means that v is a pointer to
261 the next table, which codes e - 16 bits, and lastly e == 99 indicates
262 an unused code. If a code with e == 99 is looked up, this implies an
263 error in the data. */
265 uch e
; /* number of extra bits or operation */
266 uch b
; /* number of bits in this code or subcode */
268 ush n
; /* literal, length base, or distance
270 struct huft
*t
; /* pointer to next level of table */
275 /* Function prototypes */
276 static int huft_build(struct inflate
*, unsigned *, unsigned, unsigned, const ush
*, const ush
*, struct huft
**, int *);
277 static int huft_free(struct inflate
*, struct huft
*);
278 static int inflate_codes(struct inflate
*, struct huft
*, struct huft
*, int, int);
279 static int inflate_stored(struct inflate
*);
280 static int xinflate(struct inflate
*);
281 static int inflate_fixed(struct inflate
*);
282 static int inflate_dynamic(struct inflate
*);
283 static int inflate_block(struct inflate
*, int *);
285 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
286 stream to find repeated byte strings. This is implemented here as a
287 circular buffer. The index is updated simply by incrementing and then
288 and'ing with 0x7fff (32K-1). */
289 /* It is left to other modules to supply the 32K area. It is assumed
290 to be usable as if it were declared "uch slide[32768];" or as just
291 "uch *slide;" and then malloc'ed in the latter case. The definition
292 must be in unzip.h, included above. */
295 /* Tables for deflate from PKZIP's appnote.txt. */
297 /* Order of the bit length code lengths */
298 static const unsigned border
[] = {
299 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
301 static const ush cplens
[] = { /* Copy lengths for literal codes 257..285 */
302 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
303 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
304 /* note: see note #13 above about the 258 in this list. */
306 static const ush cplext
[] = { /* Extra bits for literal codes 257..285 */
307 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
308 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
310 static const ush cpdist
[] = { /* Copy offsets for distance codes 0..29 */
311 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
312 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
313 8193, 12289, 16385, 24577};
315 static const ush cpdext
[] = { /* Extra bits for distance codes */
316 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
317 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
320 /* And'ing with mask[n] masks the lower n bits */
321 static const ush mask
[] = {
323 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
324 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
328 /* Macros for inflate() bit peeking and grabbing.
335 where NEEDBITS makes sure that b has at least j bits in it, and
336 DUMPBITS removes the bits from b. The macros use the variable k
337 for the number of bits in b. Normally, b and k are register
338 variables for speed, and are initialized at the begining of a
339 routine that uses these macros from a global bit buffer and count.
341 In order to not ask for more bits than there are in the compressed
342 stream, the Huffman tables are constructed to only ask for just
343 enough bits to make up the end-of-block code (value 256). Then no
344 bytes need to be "returned" to the buffer at the end of the last
345 block. See the huft_build() routine.
349 * The following 2 were global variables.
350 * They are now fields of the inflate structure.
353 #define NEEDBITS(glbl,n) { \
355 int c=(*glbl->gz_input)(glbl->gz_private); \
363 #define DUMPBITS(n) {b>>=(n);k-=(n);}
366 Huffman code decoding is performed using a multi-level table lookup.
367 The fastest way to decode is to simply build a lookup table whose
368 size is determined by the longest code. However, the time it takes
369 to build this table can also be a factor if the data being decoded
370 is not very long. The most common codes are necessarily the
371 shortest codes, so those codes dominate the decoding time, and hence
372 the speed. The idea is you can have a shorter table that decodes the
373 shorter, more probable codes, and then point to subsidiary tables for
374 the longer codes. The time it costs to decode the longer codes is
375 then traded against the time it takes to make longer tables.
377 This results of this trade are in the variables lbits and dbits
378 below. lbits is the number of bits the first level table for literal/
379 length codes can decode in one step, and dbits is the same thing for
380 the distance codes. Subsequent tables are also less than or equal to
381 those sizes. These values may be adjusted either when all of the
382 codes are shorter than that, in which case the longest code length in
383 bits is used, or when the shortest code is *longer* than the requested
384 table size, in which case the length of the shortest code in bits is
387 There are two different values for the two tables, since they code a
388 different number of possibilities each. The literal/length table
389 codes 286 possible values, or in a flat code, a little over eight
390 bits. The distance table codes 30 possible values, or a little less
391 than five bits, flat. The optimum values for speed end up being
392 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
393 The optimum values may differ though from machine to machine, and
394 possibly even between compilers. Your mileage may vary.
397 static const int lbits
= 9; /* bits in base literal/length lookup table */
398 static const int dbits
= 6; /* bits in base distance lookup table */
401 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
402 #define BMAX 16 /* maximum bit length of any code (16 for
404 #define N_MAX 288 /* maximum number of codes in any set */
406 /* Given a list of code lengths and a maximum table size, make a set of
407 tables to decode that set of codes. Return zero on success, one if
408 the given code set is incomplete (the tables are still built in this
409 case), two if the input is invalid (all zero length codes or an
410 oversubscribed set of lengths), and three if not enough memory.
411 The code with value 256 is special, and the tables are constructed
412 so that no bits beyond that code are fetched when that code is
415 huft_build(glbl
, b
, n
, s
, d
, e
, t
, m
)
416 struct inflate
*glbl
;
417 unsigned *b
; /* code lengths in bits (all assumed <= BMAX) */
418 unsigned n
; /* number of codes (assumed <= N_MAX) */
419 unsigned s
; /* number of simple-valued codes (0..s-1) */
420 const ush
*d
; /* list of base values for non-simple codes */
421 const ush
*e
; /* list of extra bits for non-simple codes */
422 struct huft
**t
; /* result: starting table */
423 int *m
; /* maximum lookup bits, returns actual */
425 unsigned a
; /* counter for codes of length k */
426 unsigned c
[BMAX
+ 1]; /* bit length count table */
427 unsigned el
; /* length of EOB code (value 256) */
428 unsigned f
; /* i repeats in table every f entries */
429 int g
; /* maximum code length */
430 int h
; /* table level */
431 register unsigned i
; /* counter, current code */
432 register unsigned j
; /* counter */
433 register int k
; /* number of bits in current code */
434 int lx
[BMAX
+ 1]; /* memory for l[-1..BMAX-1] */
435 int *l
= lx
+ 1; /* stack of bits per table */
436 register unsigned *p
; /* pointer into c[], b[], or v[] */
437 register struct huft
*q
;/* points to current table */
438 struct huft r
; /* table entry for structure assignment */
439 struct huft
*u
[BMAX
];/* table stack */
440 unsigned v
[N_MAX
]; /* values in order of bit length */
441 register int w
; /* bits before this table == (l * h) */
442 unsigned x
[BMAX
+ 1]; /* bit offsets, then code stack */
443 unsigned *xp
; /* pointer into x */
444 int y
; /* number of dummy codes added */
445 unsigned z
; /* number of entries in current table */
447 /* Generate counts for each bit length */
448 el
= n
> 256 ? b
[256] : BMAX
; /* set length of EOB code, if any */
450 memzero((char *) c
, sizeof(c
));
452 for (i
= 0; i
< BMAX
+1; i
++)
459 p
++; /* assume all entries <= BMAX */
461 if (c
[0] == n
) { /* null input--all zero length codes */
462 *t
= (struct huft
*) NULL
;
466 /* Find minimum and maximum length, bound *m by those */
467 for (j
= 1; j
<= BMAX
; j
++)
470 k
= j
; /* minimum code length */
471 if ((unsigned) *m
< j
)
473 for (i
= BMAX
; i
; i
--)
476 g
= i
; /* maximum code length */
477 if ((unsigned) *m
> i
)
480 /* Adjust last length count to fill out codes, if needed */
481 for (y
= 1 << j
; j
< i
; j
++, y
<<= 1)
483 return 2; /* bad input: more codes than bits */
488 /* Generate starting offsets into the value table for each length */
492 while (--i
) { /* note that i == g from above */
496 /* Make a table of values in order of bit lengths */
504 /* Generate the Huffman codes and for each, make the table entries */
505 x
[0] = i
= 0; /* first Huffman code is zero */
506 p
= v
; /* grab values in bit order */
507 h
= -1; /* no tables yet--level -1 */
508 w
= l
[-1] = 0; /* no bits decoded yet */
509 u
[0] = (struct huft
*) NULL
; /* just to keep compilers happy */
510 q
= (struct huft
*) NULL
; /* ditto */
513 /* go through the bit lengths (k already is bits in shortest code) */
514 for (; k
<= g
; k
++) {
518 * here i is the Huffman code of length k bits for
521 /* make tables up to required level */
522 while (k
> w
+ l
[h
]) {
523 w
+= l
[h
++]; /* add bits already decoded */
526 * compute minimum size table less than or
529 z
= (z
= g
- w
) > (unsigned) *m
? *m
: z
; /* upper limit */
530 if ((f
= 1 << (j
= k
- w
)) > a
+ 1) { /* try a k-w bit table *//* t
531 * oo few codes for k-w
533 f
-= a
+ 1; /* deduct codes from
536 while (++j
< z
) { /* try smaller tables up
538 if ((f
<<= 1) <= *++xp
)
539 break; /* enough codes to use
541 f
-= *xp
; /* else deduct codes
545 if ((unsigned) w
+ j
> el
&& (unsigned) w
< el
)
546 j
= el
- w
; /* make EOB code end at
548 z
= 1 << j
; /* table entries for j-bit
550 l
[h
] = j
; /* set table size in stack */
552 /* allocate and link in new table */
553 if ((q
= (struct huft
*) malloc((z
+ 1) * sizeof(struct huft
), M_GZIP
, M_WAITOK
)) ==
554 (struct huft
*) NULL
) {
556 huft_free(glbl
, u
[0]);
557 return 3; /* not enough memory */
559 glbl
->gz_hufts
+= z
+ 1; /* track memory usage */
560 *t
= q
+ 1; /* link to list for
562 *(t
= &(q
->v
.t
)) = (struct huft
*) NULL
;
563 u
[h
] = ++q
; /* table starts after link */
565 /* connect to last table, if there is one */
567 x
[h
] = i
; /* save pattern for
569 r
.b
= (uch
) l
[h
- 1]; /* bits to dump before
571 r
.e
= (uch
) (16 + j
); /* bits in this table */
572 r
.v
.t
= q
; /* pointer to this table */
573 j
= (i
& ((1 << w
) - 1)) >> (w
- l
[h
- 1]);
574 u
[h
- 1][j
] = r
; /* connect to last table */
578 /* set up table entry in r */
581 r
.e
= 99; /* out of values--invalid
584 r
.e
= (uch
) (*p
< 256 ? 16 : 15); /* 256 is end-of-block
586 r
.v
.n
= *p
++; /* simple code is just the
589 r
.e
= (uch
) e
[*p
- s
]; /* non-simple--look up
594 /* fill code-like entries with r */
596 for (j
= i
>> w
; j
< z
; j
+= f
)
599 /* backwards increment the k-bit code i */
600 for (j
= 1 << (k
- 1); i
& j
; j
>>= 1)
604 /* backup over finished tables */
605 while ((i
& ((1 << w
) - 1)) != x
[h
])
606 w
-= l
[--h
]; /* don't need to update q */
610 /* return actual size of base table */
613 /* Return true (1) if we were given an incomplete table */
614 return y
!= 0 && g
!= 1;
619 struct inflate
*glbl
;
620 struct huft
*t
; /* table to free */
621 /* Free the malloc'ed tables built by huft_build(), which makes a linked
622 list of the tables it made, with the links in a dummy first entry of
625 register struct huft
*p
, *q
;
627 /* Go through linked list, freeing from the malloced (t[-1]) address. */
629 while (p
!= (struct huft
*) NULL
) {
637 /* inflate (decompress) the codes in a deflated (compressed) block.
638 Return an error code or zero if it all goes ok. */
640 inflate_codes(glbl
, tl
, td
, bl
, bd
)
641 struct inflate
*glbl
;
642 struct huft
*tl
, *td
;/* literal/length and distance decoder tables */
643 int bl
, bd
; /* number of bits decoded by tl[] and td[] */
645 register unsigned e
; /* table entry flag/number of extra bits */
646 unsigned n
, d
; /* length and index for copy */
647 unsigned w
; /* current window position */
648 struct huft
*t
; /* pointer to table entry */
649 unsigned ml
, md
; /* masks for bl and bd bits */
650 register ulg b
; /* bit buffer */
651 register unsigned k
; /* number of bits in bit buffer */
653 /* make local copies of globals */
654 b
= glbl
->gz_bb
; /* initialize bit buffer */
656 w
= glbl
->gz_wp
; /* initialize window position */
658 /* inflate the coded data */
659 ml
= mask
[bl
]; /* precompute masks for speed */
661 while (1) { /* do until end of block */
662 NEEDBITS(glbl
, (unsigned) bl
)
663 if ((e
= (t
= tl
+ ((unsigned) b
& ml
))->e
) > 16)
670 } while ((e
= (t
= t
->v
.t
+ ((unsigned) b
& mask
[e
]))->e
) > 16);
672 if (e
== 16) { /* then it's a literal */
673 glbl
->gz_slide
[w
++] = (uch
) t
->v
.n
;
678 } else { /* it's an EOB or a length */
679 /* exit if end of block */
683 /* get length of block to copy */
685 n
= t
->v
.n
+ ((unsigned) b
& mask
[e
]);
688 /* decode distance of block to copy */
689 NEEDBITS(glbl
, (unsigned) bd
)
690 if ((e
= (t
= td
+ ((unsigned) b
& md
))->e
) > 16)
697 } while ((e
= (t
= t
->v
.t
+ ((unsigned) b
& mask
[e
]))->e
) > 16);
700 d
= w
- t
->v
.n
- ((unsigned) b
& mask
[e
]);
704 n
-= (e
= (e
= GZ_WSIZE
- ((d
&= GZ_WSIZE
- 1) > w
? d
: w
)) > n
? n
: e
);
706 if (w
- d
>= e
) { /* (this test assumes
707 * unsigned comparison) */
708 memcpy(glbl
->gz_slide
+ w
, glbl
->gz_slide
+ d
, e
);
711 } else /* do it slow to avoid memcpy()
713 #endif /* !NOMEMCPY */
715 glbl
->gz_slide
[w
++] = glbl
->gz_slide
[d
++];
725 /* restore the globals from the locals */
726 glbl
->gz_wp
= w
; /* restore global window pointer */
727 glbl
->gz_bb
= b
; /* restore global bit buffer */
734 /* "decompress" an inflated type 0 (stored) block. */
737 struct inflate
*glbl
;
739 unsigned n
; /* number of bytes in block */
740 unsigned w
; /* current window position */
741 register ulg b
; /* bit buffer */
742 register unsigned k
; /* number of bits in bit buffer */
744 /* make local copies of globals */
745 b
= glbl
->gz_bb
; /* initialize bit buffer */
747 w
= glbl
->gz_wp
; /* initialize window position */
749 /* go to byte boundary */
753 /* get the length and its complement */
755 n
= ((unsigned) b
& 0xffff);
758 if (n
!= (unsigned) ((~b
) & 0xffff))
759 return 1; /* error in compressed data */
761 /* read and output the compressed data */
764 glbl
->gz_slide
[w
++] = (uch
) b
;
772 /* restore the globals from the locals */
773 glbl
->gz_wp
= w
; /* restore global window pointer */
774 glbl
->gz_bb
= b
; /* restore global bit buffer */
779 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
780 either replace this with a custom decoder, or at least precompute the
784 struct inflate
*glbl
;
786 /* if first time, set up tables for fixed blocks */
787 if (glbl
->gz_fixed_tl
== (struct huft
*) NULL
) {
788 int i
; /* temporary variable */
789 static unsigned l
[288]; /* length list for huft_build */
792 for (i
= 0; i
< 144; i
++)
798 for (; i
< 288; i
++) /* make a complete, but wrong code
801 glbl
->gz_fixed_bl
= 7;
802 if ((i
= huft_build(glbl
, l
, 288, 257, cplens
, cplext
,
803 &glbl
->gz_fixed_tl
, &glbl
->gz_fixed_bl
)) != 0) {
804 glbl
->gz_fixed_tl
= (struct huft
*) NULL
;
808 for (i
= 0; i
< 30; i
++) /* make an incomplete code
811 glbl
->gz_fixed_bd
= 5;
812 if ((i
= huft_build(glbl
, l
, 30, 0, cpdist
, cpdext
,
813 &glbl
->gz_fixed_td
, &glbl
->gz_fixed_bd
)) > 1) {
814 huft_free(glbl
, glbl
->gz_fixed_tl
);
815 glbl
->gz_fixed_tl
= (struct huft
*) NULL
;
819 /* decompress until an end-of-block code */
820 return inflate_codes(glbl
, glbl
->gz_fixed_tl
, glbl
->gz_fixed_td
, glbl
->gz_fixed_bl
, glbl
->gz_fixed_bd
) != 0;
823 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
825 inflate_dynamic(glbl
)
826 struct inflate
*glbl
;
828 int i
; /* temporary variables */
830 unsigned l
; /* last length */
831 unsigned m
; /* mask for bit lengths table */
832 unsigned n
; /* number of lengths to get */
833 struct huft
*tl
; /* literal/length code table */
834 struct huft
*td
; /* distance code table */
835 int bl
; /* lookup bits for tl */
836 int bd
; /* lookup bits for td */
837 unsigned nb
; /* number of bit length codes */
838 unsigned nl
; /* number of literal/length codes */
839 unsigned nd
; /* number of distance codes */
840 #ifdef PKZIP_BUG_WORKAROUND
841 unsigned ll
[288 + 32]; /* literal/length and distance code
844 unsigned ll
[286 + 30]; /* literal/length and distance code
847 register ulg b
; /* bit buffer */
848 register unsigned k
; /* number of bits in bit buffer */
850 /* make local bit buffer */
854 /* read in table lengths */
856 nl
= 257 + ((unsigned) b
& 0x1f); /* number of
857 * literal/length codes */
860 nd
= 1 + ((unsigned) b
& 0x1f); /* number of distance codes */
863 nb
= 4 + ((unsigned) b
& 0xf); /* number of bit length codes */
865 #ifdef PKZIP_BUG_WORKAROUND
866 if (nl
> 288 || nd
> 32)
868 if (nl
> 286 || nd
> 30)
870 return 1; /* bad lengths */
871 /* read in bit-length-code lengths */
872 for (j
= 0; j
< nb
; j
++) {
874 ll
[border
[j
]] = (unsigned) b
& 7;
880 /* build decoding table for trees--single level, 7 bit lookup */
882 if ((i
= huft_build(glbl
, ll
, 19, 19, NULL
, NULL
, &tl
, &bl
)) != 0) {
885 return i
; /* incomplete code set */
887 /* read in literal and distance code lengths */
891 while ((unsigned) i
< n
) {
892 NEEDBITS(glbl
, (unsigned) bl
)
893 j
= (td
= tl
+ ((unsigned) b
& m
))->b
;
896 if (j
< 16) /* length of code in bits (0..15) */
897 ll
[i
++] = l
= j
; /* save last length in l */
898 else if (j
== 16) { /* repeat last length 3 to 6 times */
900 j
= 3 + ((unsigned) b
& 3);
902 if ((unsigned) i
+ j
> n
)
906 } else if (j
== 17) { /* 3 to 10 zero length codes */
908 j
= 3 + ((unsigned) b
& 7);
910 if ((unsigned) i
+ j
> n
)
915 } else { /* j == 18: 11 to 138 zero length codes */
917 j
= 11 + ((unsigned) b
& 0x7f);
919 if ((unsigned) i
+ j
> n
)
927 /* free decoding table for trees */
930 /* restore the global bit buffer */
934 /* build the decoding tables for literal/length and distance codes */
936 i
= huft_build(glbl
, ll
, nl
, 257, cplens
, cplext
, &tl
, &bl
);
938 if (i
== 1 && !qflag
) {
939 FPRINTF("(incomplete l-tree) ");
942 return i
; /* incomplete code set */
945 i
= huft_build(glbl
, ll
+ nl
, nd
, 0, cpdist
, cpdext
, &td
, &bd
);
947 if (i
== 1 && !qflag
) {
948 FPRINTF("(incomplete d-tree) ");
949 #ifdef PKZIP_BUG_WORKAROUND
956 return i
; /* incomplete code set */
959 /* decompress until an end-of-block code */
960 if (inflate_codes(glbl
, tl
, td
, bl
, bd
))
963 /* free the decoding tables, return */
969 /* decompress an inflated block */
971 inflate_block(glbl
, e
)
972 struct inflate
*glbl
;
973 int *e
; /* last block flag */
975 unsigned t
; /* block type */
976 register ulg b
; /* bit buffer */
977 register unsigned k
; /* number of bits in bit buffer */
979 /* make local bit buffer */
983 /* read in last block bit */
987 /* read in block type */
989 t
= (unsigned) b
& 3;
991 /* restore the global bit buffer */
995 /* inflate that block type */
997 return inflate_dynamic(glbl
);
999 return inflate_stored(glbl
);
1001 return inflate_fixed(glbl
);
1002 /* bad block type */
1008 /* decompress an inflated entry */
1011 struct inflate
*glbl
;
1013 int e
; /* last block flag */
1014 int r
; /* result code */
1015 unsigned h
; /* maximum struct huft's malloc'ed */
1017 glbl
->gz_fixed_tl
= (struct huft
*) NULL
;
1019 /* initialize window, bit buffer */
1024 /* decompress until the last block */
1028 if ((r
= inflate_block(glbl
, &e
)) != 0)
1030 if (glbl
->gz_hufts
> h
)
1034 /* flush out slide */
1035 FLUSH(glbl
, glbl
->gz_wp
);
1037 /* return success */
1041 /* Nobody uses this - why not? */
1044 struct inflate
*glbl
;
1050 if (!glbl
->gz_slide
)
1051 p
= glbl
->gz_slide
= malloc(GZ_WSIZE
, M_GZIP
, M_WAITOK
);
1053 if (!glbl
->gz_slide
)
1057 return 3; /* kzip expects 3 */
1061 if (glbl
->gz_fixed_td
!= (struct huft
*) NULL
) {
1062 huft_free(glbl
, glbl
->gz_fixed_td
);
1063 glbl
->gz_fixed_td
= (struct huft
*) NULL
;
1065 if (glbl
->gz_fixed_tl
!= (struct huft
*) NULL
) {
1066 huft_free(glbl
, glbl
->gz_fixed_tl
);
1067 glbl
->gz_fixed_tl
= (struct huft
*) NULL
;
1070 if (p
== glbl
->gz_slide
) {
1071 free(glbl
->gz_slide
, M_GZIP
);
1072 glbl
->gz_slide
= NULL
;
1077 /* ----------------------- END INFLATE.C */