2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* inftrees.c -- generate Huffman trees for efficient decoding
20 * Copyright (C) 1995-1998 Mark Adler
21 * For conditions of distribution and use, see copyright notice in zlib.h
27 #if !defined(BUILDFIXED) && !defined(STDC)
28 # define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
31 const char inflate_copyright
[] =
32 " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
34 If you use the zlib library in a product, an acknowledgment is welcome
35 in the documentation of your product. If for some reason you cannot
36 include such an acknowledgment, I would appreciate that you keep this
37 copyright string in the executable of your product.
39 struct internal_state
{int dummy
;}; /* for buggy compilers */
41 /* simplify the use of the inflate_huft type with some defines */
42 #define exop word.what.Exop
43 #define bits word.what.Bits
46 local
int huft_build
OF((
47 uIntf
*, /* code lengths in bits */
48 uInt
, /* number of codes */
49 uInt
, /* number of "simple" codes */
50 const uIntf
*, /* list of base values for non-simple codes */
51 const uIntf
*, /* list of extra bits for non-simple codes */
52 inflate_huft
* FAR
*,/* result: starting table */
53 uIntf
*, /* maximum lookup bits (returns actual) */
54 inflate_huft
*, /* space for trees */
55 uInt
*, /* hufts used in space */
56 uIntf
* )); /* space for values */
58 /* Tables for deflate from PKZIP's appnote.txt. */
59 local
const uInt cplens
[31] = { /* Copy lengths for literal codes 257..285 */
60 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
61 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
62 /* see note #13 above about 258 */
63 local
const uInt cplext
[31] = { /* Extra bits for literal codes 257..285 */
64 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
65 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
66 local
const uInt cpdist
[30] = { /* Copy offsets for distance codes 0..29 */
67 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
68 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
69 8193, 12289, 16385, 24577};
70 local
const uInt cpdext
[30] = { /* Extra bits for distance codes */
71 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
72 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
76 Huffman code decoding is performed using a multi-level table lookup.
77 The fastest way to decode is to simply build a lookup table whose
78 size is determined by the longest code. However, the time it takes
79 to build this table can also be a factor if the data being decoded
80 is not very long. The most common codes are necessarily the
81 shortest codes, so those codes dominate the decoding time, and hence
82 the speed. The idea is you can have a shorter table that decodes the
83 shorter, more probable codes, and then point to subsidiary tables for
84 the longer codes. The time it costs to decode the longer codes is
85 then traded against the time it takes to make longer tables.
87 This results of this trade are in the variables lbits and dbits
88 below. lbits is the number of bits the first level table for literal/
89 length codes can decode in one step, and dbits is the same thing for
90 the distance codes. Subsequent tables are also less than or equal to
91 those sizes. These values may be adjusted either when all of the
92 codes are shorter than that, in which case the longest code length in
93 bits is used, or when the shortest code is *longer* than the requested
94 table size, in which case the length of the shortest code in bits is
97 There are two different values for the two tables, since they code a
98 different number of possibilities each. The literal/length table
99 codes 286 possible values, or in a flat code, a little over eight
100 bits. The distance table codes 30 possible values, or a little less
101 than five bits, flat. The optimum values for speed end up being
102 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
103 The optimum values may differ though from machine to machine, and
104 possibly even between compilers. Your mileage may vary.
108 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
109 #define BMAX 15 /* maximum bit length of any code */
111 local
int huft_build(b
, n
, s
, d
, e
, t
, m
, hp
, hn
, v
)
112 uIntf
*b
; /* code lengths in bits (all assumed <= BMAX) */
113 uInt n
; /* number of codes (assumed <= 288) */
114 uInt s
; /* number of simple-valued codes (0..s-1) */
115 const uIntf
*d
; /* list of base values for non-simple codes */
116 const uIntf
*e
; /* list of extra bits for non-simple codes */
117 inflate_huft
* FAR
*t
; /* result: starting table */
118 uIntf
*m
; /* maximum lookup bits, returns actual */
119 inflate_huft
*hp
; /* space for trees */
120 uInt
*hn
; /* hufts used in space */
121 uIntf
*v
; /* working area: values in order of bit length */
122 /* Given a list of code lengths and a maximum table size, make a set of
123 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
124 if the given code set is incomplete (the tables are still built in this
125 case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
126 lengths), or Z_MEM_ERROR if not enough memory. */
129 uInt a
; /* counter for codes of length k */
130 uInt c
[BMAX
+1]; /* bit length count table */
131 uInt f
; /* i repeats in table every f entries */
132 int g
; /* maximum code length */
133 int h
; /* table level */
134 register uInt i
; /* counter, current code */
135 register uInt j
; /* counter */
136 register int k
; /* number of bits in current code */
137 int l
; /* bits per table (returned in m) */
138 uInt mask
; /* (1 << w) - 1, to avoid cc -O bug on HP */
139 register uIntf
*p
; /* pointer into c[], b[], or v[] */
140 inflate_huft
*q
; /* points to current table */
141 struct inflate_huft_s r
; /* table entry for structure assignment */
142 inflate_huft
*u
[BMAX
]; /* table stack */
143 register int w
; /* bits before this table == (l * h) */
144 uInt x
[BMAX
+1]; /* bit offsets, then code stack */
145 uIntf
*xp
; /* pointer into x */
146 int y
; /* number of dummy codes added */
147 uInt z
; /* number of entries in current table */
150 /* Generate counts for each bit length */
153 #define C2 C0 C0 C0 C0
154 #define C4 C2 C2 C2 C2
155 C4
/* clear c[]--assume BMAX+1 is 16 */
158 c
[*p
++]++; /* assume all entries <= BMAX */
160 if (c
[0] == n
) /* null input--all zero length codes */
162 *t
= (inflate_huft
*)Z_NULL
;
168 /* Find minimum and maximum length, bound *m by those */
170 for (j
= 1; j
<= BMAX
; j
++)
173 k
= j
; /* minimum code length */
176 for (i
= BMAX
; i
; i
--)
179 g
= i
; /* maximum code length */
185 /* Adjust last length count to fill out codes, if needed */
186 for (y
= 1 << j
; j
< i
; j
++, y
<<= 1)
194 /* Generate starting offsets into the value table for each length */
196 p
= c
+ 1; xp
= x
+ 2;
197 while (--i
) { /* note that i == g from above */
202 /* Make a table of values in order of bit lengths */
208 n
= x
[g
]; /* set n to length of v */
211 /* Generate the Huffman codes and for each, make the table entries */
212 x
[0] = i
= 0; /* first Huffman code is zero */
213 p
= v
; /* grab values in bit order */
214 h
= -1; /* no tables yet--level -1 */
215 w
= -l
; /* bits decoded == (l * h) */
216 u
[0] = (inflate_huft
*)Z_NULL
; /* just to keep compilers happy */
217 q
= (inflate_huft
*)Z_NULL
; /* ditto */
220 /* go through the bit lengths (k already is bits in shortest code) */
226 /* here i is the Huffman code of length k bits for value *p */
227 /* make tables up to required level */
231 w
+= l
; /* previous table always l bits */
233 /* compute minimum size table less than or equal to l bits */
235 z
= z
> (uInt
)l
? l
: z
; /* table size upper limit */
236 if ((f
= 1 << (j
= k
- w
)) > a
+ 1) /* try a k-w bit table */
237 { /* too few codes for k-w bit table */
238 f
-= a
+ 1; /* deduct codes from patterns left */
241 while (++j
< z
) /* try smaller tables up to z bits */
243 if ((f
<<= 1) <= *++xp
)
244 break; /* enough codes to use up j bits */
245 f
-= *xp
; /* else deduct codes from patterns */
248 z
= 1 << j
; /* table entries for j-bit table */
250 /* allocate new table */
251 if (*hn
+ z
> MANY
) /* (note: doesn't matter for fixed) */
252 return Z_MEM_ERROR
; /* not enough memory */
256 /* connect to last table, if there is one */
259 x
[h
] = i
; /* save pattern for backing up */
260 r
.bits
= (Byte
)l
; /* bits to dump before this table */
261 r
.exop
= (Byte
)j
; /* bits in this table */
263 r
.base
= (uInt
)(q
- u
[h
-1] - j
); /* offset to this table */
264 u
[h
-1][j
] = r
; /* connect to last table */
267 *t
= q
; /* first table is returned result */
270 /* set up table entry in r */
271 r
.bits
= (Byte
)(k
- w
);
273 r
.exop
= 128 + 64; /* out of values--invalid code */
276 r
.exop
= (Byte
)(*p
< 256 ? 0 : 32 + 64); /* 256 is end-of-block */
277 r
.base
= *p
++; /* simple code is just the value */
281 r
.exop
= (Byte
)(e
[*p
- s
] + 16 + 64);/* non-simple--look up in lists */
282 r
.base
= d
[*p
++ - s
];
285 /* fill code-like entries with r */
287 for (j
= i
>> w
; j
< z
; j
+= f
)
290 /* backwards increment the k-bit code i */
291 for (j
= 1 << (k
- 1); i
& j
; j
>>= 1)
295 /* backup over finished tables */
296 mask
= (1 << w
) - 1; /* needed on HP, cc -O bug */
297 while ((i
& mask
) != x
[h
])
299 h
--; /* don't need to update q */
307 /* Return Z_BUF_ERROR if we were given an incomplete table */
308 return y
!= 0 && g
!= 1 ? Z_BUF_ERROR
: Z_OK
;
312 int inflate_trees_bits(c
, bb
, tb
, hp
, z
)
313 uIntf
*c
; /* 19 code lengths */
314 uIntf
*bb
; /* bits tree desired/actual depth */
315 inflate_huft
* FAR
*tb
; /* bits tree result */
316 inflate_huft
*hp
; /* space for trees */
317 z_streamp z
; /* for messages */
320 uInt hn
= 0; /* hufts used in space */
321 uIntf
*v
; /* work area for huft_build */
323 if ((v
= (uIntf
*)ZALLOC(z
, 19, sizeof(uInt
))) == Z_NULL
)
325 r
= huft_build(c
, 19, 19, (uIntf
*)Z_NULL
, (uIntf
*)Z_NULL
,
327 if (r
== Z_DATA_ERROR
)
328 z
->msg
= (char*)"oversubscribed dynamic bit lengths tree";
329 else if (r
== Z_BUF_ERROR
|| *bb
== 0)
331 z
->msg
= (char*)"incomplete dynamic bit lengths tree";
339 int inflate_trees_dynamic(nl
, nd
, c
, bl
, bd
, tl
, td
, hp
, z
)
340 uInt nl
; /* number of literal/length codes */
341 uInt nd
; /* number of distance codes */
342 uIntf
*c
; /* that many (total) code lengths */
343 uIntf
*bl
; /* literal desired/actual bit depth */
344 uIntf
*bd
; /* distance desired/actual bit depth */
345 inflate_huft
* FAR
*tl
; /* literal/length tree result */
346 inflate_huft
* FAR
*td
; /* distance tree result */
347 inflate_huft
*hp
; /* space for trees */
348 z_streamp z
; /* for messages */
351 uInt hn
= 0; /* hufts used in space */
352 uIntf
*v
; /* work area for huft_build */
354 /* allocate work area */
355 if ((v
= (uIntf
*)ZALLOC(z
, 288, sizeof(uInt
))) == Z_NULL
)
358 /* build literal/length tree */
359 r
= huft_build(c
, nl
, 257, cplens
, cplext
, tl
, bl
, hp
, &hn
, v
);
360 if (r
!= Z_OK
|| *bl
== 0)
362 if (r
== Z_DATA_ERROR
)
363 z
->msg
= (char*)"oversubscribed literal/length tree";
364 else if (r
!= Z_MEM_ERROR
)
366 z
->msg
= (char*)"incomplete literal/length tree";
373 /* build distance tree */
374 r
= huft_build(c
+ nl
, nd
, 0, cpdist
, cpdext
, td
, bd
, hp
, &hn
, v
);
375 if (r
!= Z_OK
|| (*bd
== 0 && nl
> 257))
377 if (r
== Z_DATA_ERROR
)
378 z
->msg
= (char*)"oversubscribed distance tree";
379 else if (r
== Z_BUF_ERROR
) {
380 #ifdef PKZIP_BUG_WORKAROUND
384 z
->msg
= (char*)"incomplete distance tree";
387 else if (r
!= Z_MEM_ERROR
)
389 z
->msg
= (char*)"empty distance tree with lengths";
403 /* build fixed tables only once--keep them here */
405 local
int fixed_built
= 0;
406 #define FIXEDH 544 /* number of hufts used by fixed tables */
407 local inflate_huft fixed_mem
[FIXEDH
];
410 local inflate_huft
*fixed_tl
;
411 local inflate_huft
*fixed_td
;
413 #include "inffixed.h"
417 int inflate_trees_fixed(bl
, bd
, tl
, td
, z
)
418 uIntf
*bl
; /* literal desired/actual bit depth */
419 uIntf
*bd
; /* distance desired/actual bit depth */
420 inflate_huft
* FAR
*tl
; /* literal/length tree result */
421 inflate_huft
* FAR
*td
; /* distance tree result */
422 z_streamp z
; /* for memory allocation */
425 /* build fixed tables if not already */
428 int k
; /* temporary variable */
429 uInt f
= 0; /* number of hufts used in fixed_mem */
430 uIntf
*c
; /* length list for huft_build */
431 uIntf
*v
; /* work area for huft_build */
433 /* allocate memory */
434 if ((c
= (uIntf
*)ZALLOC(z
, 288, sizeof(uInt
))) == Z_NULL
)
436 if ((v
= (uIntf
*)ZALLOC(z
, 288, sizeof(uInt
))) == Z_NULL
)
443 for (k
= 0; k
< 144; k
++)
452 huft_build(c
, 288, 257, cplens
, cplext
, &fixed_tl
, &fixed_bl
,
456 for (k
= 0; k
< 30; k
++)
459 huft_build(c
, 30, 0, cpdist
, cpdext
, &fixed_td
, &fixed_bd
,