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