1 /* inftrees.c -- generate Huffman trees for efficient decoding
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 #include "zutil.h"
7 #include "inftrees.h"
8
9 #if !defined(BUILDFIXED) && !defined(STDC)
10 # define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
11 #endif
12
13 local const char inflate_copyright[] =
14 " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
15 /*
16 If you use the zlib library in a product, an acknowledgment is welcome
17 in the documentation of your product. If for some reason you cannot
18 include such an acknowledgment, I would appreciate that you keep this
19 copyright string in the executable of your product.
20 */
21
22 /* simplify the use of the inflate_huft type with some defines */
23 #define exop word.what.Exop
24 #define bits word.what.Bits
25
26
27 local int huft_build OF((
28 uIntf *, /* code lengths in bits */
29 uInt, /* number of codes */
30 uInt, /* number of "simple" codes */
31 const uIntf *, /* list of base values for non-simple codes */
32 const uIntf *, /* list of extra bits for non-simple codes */
33 inflate_huft * FAR*,/* result: starting table */
34 uIntf *, /* maximum lookup bits (returns actual) */
35 inflate_huft *, /* space for trees */
36 uInt *, /* hufts used in space */
37 uIntf * )); /* space for values */
38
39 /* Tables for deflate from PKZIP's appnote.txt. */
40 local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
41 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
42 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
43 /* see note #13 above about 258 */
44 local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
45 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
46 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
47 local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
48 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
49 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
50 8193, 12289, 16385, 24577};
51 local const uInt cpdext[30] = { /* Extra bits for distance codes */
52 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
53 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
54 12, 12, 13, 13};
55
56 /*
57 Huffman code decoding is performed using a multi-level table lookup.
58 The fastest way to decode is to simply build a lookup table whose
59 size is determined by the longest code. However, the time it takes
60 to build this table can also be a factor if the data being decoded
61 is not very long. The most common codes are necessarily the
62 shortest codes, so those codes dominate the decoding time, and hence
63 the speed. The idea is you can have a shorter table that decodes the
64 shorter, more probable codes, and then point to subsidiary tables for
65 the longer codes. The time it costs to decode the longer codes is
66 then traded against the time it takes to make longer tables.
67
68 This results of this trade are in the variables lbits and dbits
69 below. lbits is the number of bits the first level table for literal/
70 length codes can decode in one step, and dbits is the same thing for
71 the distance codes. Subsequent tables are also less than or equal to
72 those sizes. These values may be adjusted either when all of the
73 codes are shorter than that, in which case the longest code length in
74 bits is used, or when the shortest code is *longer* than the requested
75 table size, in which case the length of the shortest code in bits is
76 used.
77
78 There are two different values for the two tables, since they code a
79 different number of possibilities each. The literal/length table
80 codes 286 possible values, or in a flat code, a little over eight
81 bits. The distance table codes 30 possible values, or a little less
82 than five bits, flat. The optimum values for speed end up being
83 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
84 The optimum values may differ though from machine to machine, and
85 possibly even between compilers. Your mileage may vary.
86 */
87
88
89 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
90 #define BMAX 15 /* maximum bit length of any code */
91
huft_build(b,n,s,d,e,t,m,hp,hn,v)92 local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
93 uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
94 uInt n; /* number of codes (assumed <= 288) */
95 uInt s; /* number of simple-valued codes (0..s-1) */
96 const uIntf *d; /* list of base values for non-simple codes */
97 const uIntf *e; /* list of extra bits for non-simple codes */
98 inflate_huft * FAR *t; /* result: starting table */
99 uIntf *m; /* maximum lookup bits, returns actual */
100 inflate_huft *hp; /* space for trees */
101 uInt *hn; /* hufts used in space */
102 uIntf *v; /* working area: values in order of bit length */
103 /* Given a list of code lengths and a maximum table size, make a set of
104 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
105 if the given code set is incomplete (the tables are still built in this
106 case), or Z_DATA_ERROR if the input is invalid. */
107 {
108
109 uInt a; /* counter for codes of length k */
110 uInt c[BMAX+1]; /* bit length count table */
111 uInt f; /* i repeats in table every f entries */
112 int g; /* maximum code length */
113 int h; /* table level */
114 register uInt i; /* counter, current code */
115 register uInt j; /* counter */
116 register int k; /* number of bits in current code */
117 int l; /* bits per table (returned in m) */
118 uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
119 register uIntf *p; /* pointer into c[], b[], or v[] */
120 inflate_huft *q; /* points to current table */
121 struct inflate_huft_s r; /* table entry for structure assignment */
122 inflate_huft *u[BMAX]; /* table stack */
123 register int w; /* bits before this table == (l * h) */
124 uInt x[BMAX+1]; /* bit offsets, then code stack */
125 uIntf *xp; /* pointer into x */
126 int y; /* number of dummy codes added */
127 uInt z; /* number of entries in current table */
128
129
130 /* Generate counts for each bit length */
131 p = c;
132 #define C0 *p++ = 0;
133 #define C2 C0 C0 C0 C0
134 #define C4 C2 C2 C2 C2
135 C4 /* clear c[]--assume BMAX+1 is 16 */
136 p = b; i = n;
137 do {
138 c[*p++]++; /* assume all entries <= BMAX */
139 } while (--i);
140 if (c[0] == n) /* null input--all zero length codes */
141 {
142 *t = (inflate_huft *)Z_NULL;
143 *m = 0;
144 return Z_OK;
145 }
146
147
148 /* Find minimum and maximum length, bound *m by those */
149 l = *m;
150 for (j = 1; j <= BMAX; j++)
151 if (c[j])
152 break;
153 k = j; /* minimum code length */
154 if ((uInt)l < j)
155 l = j;
156 for (i = BMAX; i; i--)
157 if (c[i])
158 break;
159 g = i; /* maximum code length */
160 if ((uInt)l > i)
161 l = i;
162 *m = l;
163
164
165 /* Adjust last length count to fill out codes, if needed */
166 for (y = 1 << j; j < i; j++, y <<= 1)
167 if ((y -= c[j]) < 0)
168 return Z_DATA_ERROR;
169 if ((y -= c[i]) < 0)
170 return Z_DATA_ERROR;
171 c[i] += y;
172
173
174 /* Generate starting offsets into the value table for each length */
175 x[1] = j = 0;
176 p = c + 1; xp = x + 2;
177 while (--i) { /* note that i == g from above */
178 *xp++ = (j += *p++);
179 }
180
181
182 /* Make a table of values in order of bit lengths */
183 p = b; i = 0;
184 do {
185 if ((j = *p++) != 0)
186 v[x[j]++] = i;
187 } while (++i < n);
188 n = x[g]; /* set n to length of v */
189
190
191 /* Generate the Huffman codes and for each, make the table entries */
192 x[0] = i = 0; /* first Huffman code is zero */
193 p = v; /* grab values in bit order */
194 h = -1; /* no tables yet--level -1 */
195 w = -l; /* bits decoded == (l * h) */
196 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
197 q = (inflate_huft *)Z_NULL; /* ditto */
198 z = 0; /* ditto */
199
200 /* go through the bit lengths (k already is bits in shortest code) */
201 for (; k <= g; k++)
202 {
203 a = c[k];
204 while (a--)
205 {
206 /* here i is the Huffman code of length k bits for value *p */
207 /* make tables up to required level */
208 while (k > w + l)
209 {
210 h++;
211 w += l; /* previous table always l bits */
212
213 /* compute minimum size table less than or equal to l bits */
214 z = g - w;
215 z = z > (uInt)l ? (uInt)l : z; /* table size upper limit */
216 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
217 { /* too few codes for k-w bit table */
218 f -= a + 1; /* deduct codes from patterns left */
219 xp = c + k;
220 if (j < z)
221 while (++j < z) /* try smaller tables up to z bits */
222 {
223 if ((f <<= 1) <= *++xp)
224 break; /* enough codes to use up j bits */
225 f -= *xp; /* else deduct codes from patterns */
226 }
227 }
228 z = 1 << j; /* table entries for j-bit table */
229
230 /* allocate new table */
231 if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
232 return Z_DATA_ERROR; /* overflow of MANY */
233 u[h] = q = hp + *hn;
234 *hn += z;
235
236 /* connect to last table, if there is one */
237 if (h)
238 {
239 x[h] = i; /* save pattern for backing up */
240 r.bits = (Byte)l; /* bits to dump before this table */
241 r.exop = (Byte)j; /* bits in this table */
242 j = i >> (w - l);
243 r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
244 u[h-1][j] = r; /* connect to last table */
245 }
246 else
247 *t = q; /* first table is returned result */
248 }
249
250 /* set up table entry in r */
251 r.bits = (Byte)(k - w);
252 if (p >= v + n)
253 r.exop = 128 + 64; /* out of values--invalid code */
254 else if (*p < s)
255 {
256 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
257 r.base = *p++; /* simple code is just the value */
258 }
259 else
260 {
261 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
262 r.base = d[*p++ - s];
263 }
264
265 /* fill code-like entries with r */
266 f = 1 << (k - w);
267 for (j = i >> w; j < z; j += f)
268 q[j] = r;
269
270 /* backwards increment the k-bit code i */
271 for (j = 1 << (k - 1); i & j; j >>= 1)
272 i ^= j;
273 i ^= j;
274
275 /* backup over finished tables */
276 mask = (1 << w) - 1; /* needed on HP, cc -O bug */
277 while ((i & mask) != x[h])
278 {
279 h--; /* don't need to update q */
280 w -= l;
281 mask = (1 << w) - 1;
282 }
283 }
284 }
285
286
287 /* Return Z_BUF_ERROR if we were given an incomplete table */
288 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
289 }
290
291
inflate_trees_bits(c,bb,tb,hp,z)292 local int inflate_trees_bits(c, bb, tb, hp, z)
293 uIntf *c; /* 19 code lengths */
294 uIntf *bb; /* bits tree desired/actual depth */
295 inflate_huft * FAR *tb; /* bits tree result */
296 inflate_huft *hp; /* space for trees */
297 z_streamp z; /* for messages */
298 {
299 int r;
300 uInt hn = 0; /* hufts used in space */
301 uIntf *v; /* work area for huft_build */
302
303 if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
304 return Z_MEM_ERROR;
305 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
306 tb, bb, hp, &hn, v);
307 if (r == Z_DATA_ERROR)
308 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
309 else if (r == Z_BUF_ERROR || *bb == 0)
310 {
311 z->msg = (char*)"incomplete dynamic bit lengths tree";
312 r = Z_DATA_ERROR;
313 }
314 ZFREE(z, v);
315 return r;
316 }
317
318
inflate_trees_dynamic(nl,nd,c,bl,bd,tl,td,hp,z)319 local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
320 uInt nl; /* number of literal/length codes */
321 uInt nd; /* number of distance codes */
322 uIntf *c; /* that many (total) code lengths */
323 uIntf *bl; /* literal desired/actual bit depth */
324 uIntf *bd; /* distance desired/actual bit depth */
325 inflate_huft * FAR *tl; /* literal/length tree result */
326 inflate_huft * FAR *td; /* distance tree result */
327 inflate_huft *hp; /* space for trees */
328 z_streamp z; /* for messages */
329 {
330 int r;
331 uInt hn = 0; /* hufts used in space */
332 uIntf *v; /* work area for huft_build */
333
334 /* allocate work area */
335 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
336 return Z_MEM_ERROR;
337
338 /* build literal/length tree */
339 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
340 if (r != Z_OK || *bl == 0)
341 {
342 if (r == Z_DATA_ERROR)
343 z->msg = (char*)"oversubscribed literal/length tree";
344 else if (r != Z_MEM_ERROR)
345 {
346 z->msg = (char*)"incomplete literal/length tree";
347 r = Z_DATA_ERROR;
348 }
349 ZFREE(z, v);
350 return r;
351 }
352
353 /* build distance tree */
354 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
355 if (r != Z_OK || (*bd == 0 && nl > 257))
356 {
357 if (r == Z_DATA_ERROR)
358 z->msg = (char*)"oversubscribed distance tree";
359 else if (r == Z_BUF_ERROR) {
360 #ifdef PKZIP_BUG_WORKAROUND
361 r = Z_OK;
362 }
363 #else
364 z->msg = (char*)"incomplete distance tree";
365 r = Z_DATA_ERROR;
366 }
367 else if (r != Z_MEM_ERROR)
368 {
369 z->msg = (char*)"empty distance tree with lengths";
370 r = Z_DATA_ERROR;
371 }
372 ZFREE(z, v);
373 return r;
374 #endif
375 }
376
377 /* done */
378 ZFREE(z, v);
379 return Z_OK;
380 }
381
382
383 /* build fixed tables only once--keep them here */
384 #ifdef BUILDFIXED
385 local int fixed_built = 0;
386 #define FIXEDH 544 /* number of hufts used by fixed tables */
387 local inflate_huft fixed_mem[FIXEDH];
388 local uInt fixed_bl;
389 local uInt fixed_bd;
390 local inflate_huft *fixed_tl;
391 local inflate_huft *fixed_td;
392 #else
393 #include "inffixed.h"
394 #endif
395
396
inflate_trees_fixed(bl,bd,tl,td,z)397 local int inflate_trees_fixed(bl, bd, tl, td, z)
398 uIntf *bl; /* literal desired/actual bit depth */
399 uIntf *bd; /* distance desired/actual bit depth */
400 inflate_huft * FAR *tl; /* literal/length tree result */
401 inflate_huft * FAR *td; /* distance tree result */
402 z_streamp z; /* for memory allocation */
403 {
404 #ifdef BUILDFIXED
405 /* build fixed tables if not already */
406 if (!fixed_built)
407 {
408 int k; /* temporary variable */
409 uInt f = 0; /* number of hufts used in fixed_mem */
410 uIntf *c; /* length list for huft_build */
411 uIntf *v; /* work area for huft_build */
412
413 /* allocate memory */
414 if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
415 return Z_MEM_ERROR;
416 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
417 {
418 ZFREE(z, c);
419 return Z_MEM_ERROR;
420 }
421
422 /* literal table */
423 for (k = 0; k < 144; k++)
424 c[k] = 8;
425 for (; k < 256; k++)
426 c[k] = 9;
427 for (; k < 280; k++)
428 c[k] = 7;
429 for (; k < 288; k++)
430 c[k] = 8;
431 fixed_bl = 9;
432 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
433 fixed_mem, &f, v);
434
435 /* distance table */
436 for (k = 0; k < 30; k++)
437 c[k] = 5;
438 fixed_bd = 5;
439 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
440 fixed_mem, &f, v);
441
442 /* done */
443 ZFREE(z, v);
444 ZFREE(z, c);
445 fixed_built = 1;
446 }
447 #endif
448 *bl = fixed_bl;
449 *bd = fixed_bd;
450 *tl = fixed_tl;
451 *td = fixed_td;
452 return Z_OK;
453 }
454