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