]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/inftrees.c
Security-29.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / zlib / inftrees.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
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
22 */
23
24 #include "zutil.h"
25 #include "inftrees.h"
26
27 #if !defined(BUILDFIXED) && !defined(STDC)
28 # define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
29 #endif
30
31 const char inflate_copyright[] =
32 " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
33 /*
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.
38 */
39 struct internal_state {int dummy;}; /* for buggy compilers */
40
41 /* simplify the use of the inflate_huft type with some defines */
42 #define exop word.what.Exop
43 #define bits word.what.Bits
44
45
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 */
57
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,
73 12, 12, 13, 13};
74
75 /*
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.
86
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
95 used.
96
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.
105 */
106
107
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 */
110
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. */
127 {
128
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 */
148
149
150 /* Generate counts for each bit length */
151 p = c;
152 #define C0 *p++ = 0;
153 #define C2 C0 C0 C0 C0
154 #define C4 C2 C2 C2 C2
155 C4 /* clear c[]--assume BMAX+1 is 16 */
156 p = b; i = n;
157 do {
158 c[*p++]++; /* assume all entries <= BMAX */
159 } while (--i);
160 if (c[0] == n) /* null input--all zero length codes */
161 {
162 *t = (inflate_huft *)Z_NULL;
163 *m = 0;
164 return Z_OK;
165 }
166
167
168 /* Find minimum and maximum length, bound *m by those */
169 l = *m;
170 for (j = 1; j <= BMAX; j++)
171 if (c[j])
172 break;
173 k = j; /* minimum code length */
174 if ((uInt)l < j)
175 l = j;
176 for (i = BMAX; i; i--)
177 if (c[i])
178 break;
179 g = i; /* maximum code length */
180 if ((uInt)l > i)
181 l = i;
182 *m = l;
183
184
185 /* Adjust last length count to fill out codes, if needed */
186 for (y = 1 << j; j < i; j++, y <<= 1)
187 if ((y -= c[j]) < 0)
188 return Z_DATA_ERROR;
189 if ((y -= c[i]) < 0)
190 return Z_DATA_ERROR;
191 c[i] += y;
192
193
194 /* Generate starting offsets into the value table for each length */
195 x[1] = j = 0;
196 p = c + 1; xp = x + 2;
197 while (--i) { /* note that i == g from above */
198 *xp++ = (j += *p++);
199 }
200
201
202 /* Make a table of values in order of bit lengths */
203 p = b; i = 0;
204 do {
205 if ((j = *p++) != 0)
206 v[x[j]++] = i;
207 } while (++i < n);
208 n = x[g]; /* set n to length of v */
209
210
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 */
218 z = 0; /* ditto */
219
220 /* go through the bit lengths (k already is bits in shortest code) */
221 for (; k <= g; k++)
222 {
223 a = c[k];
224 while (a--)
225 {
226 /* here i is the Huffman code of length k bits for value *p */
227 /* make tables up to required level */
228 while (k > w + l)
229 {
230 h++;
231 w += l; /* previous table always l bits */
232
233 /* compute minimum size table less than or equal to l bits */
234 z = g - w;
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 */
239 xp = c + k;
240 if (j < z)
241 while (++j < z) /* try smaller tables up to z bits */
242 {
243 if ((f <<= 1) <= *++xp)
244 break; /* enough codes to use up j bits */
245 f -= *xp; /* else deduct codes from patterns */
246 }
247 }
248 z = 1 << j; /* table entries for j-bit table */
249
250 /* allocate new table */
251 if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
252 return Z_MEM_ERROR; /* not enough memory */
253 u[h] = q = hp + *hn;
254 *hn += z;
255
256 /* connect to last table, if there is one */
257 if (h)
258 {
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 */
262 j = i >> (w - l);
263 r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
264 u[h-1][j] = r; /* connect to last table */
265 }
266 else
267 *t = q; /* first table is returned result */
268 }
269
270 /* set up table entry in r */
271 r.bits = (Byte)(k - w);
272 if (p >= v + n)
273 r.exop = 128 + 64; /* out of values--invalid code */
274 else if (*p < s)
275 {
276 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
277 r.base = *p++; /* simple code is just the value */
278 }
279 else
280 {
281 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
282 r.base = d[*p++ - s];
283 }
284
285 /* fill code-like entries with r */
286 f = 1 << (k - w);
287 for (j = i >> w; j < z; j += f)
288 q[j] = r;
289
290 /* backwards increment the k-bit code i */
291 for (j = 1 << (k - 1); i & j; j >>= 1)
292 i ^= j;
293 i ^= j;
294
295 /* backup over finished tables */
296 mask = (1 << w) - 1; /* needed on HP, cc -O bug */
297 while ((i & mask) != x[h])
298 {
299 h--; /* don't need to update q */
300 w -= l;
301 mask = (1 << w) - 1;
302 }
303 }
304 }
305
306
307 /* Return Z_BUF_ERROR if we were given an incomplete table */
308 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
309 }
310
311
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 */
318 {
319 int r;
320 uInt hn = 0; /* hufts used in space */
321 uIntf *v; /* work area for huft_build */
322
323 if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
324 return Z_MEM_ERROR;
325 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
326 tb, bb, hp, &hn, v);
327 if (r == Z_DATA_ERROR)
328 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
329 else if (r == Z_BUF_ERROR || *bb == 0)
330 {
331 z->msg = (char*)"incomplete dynamic bit lengths tree";
332 r = Z_DATA_ERROR;
333 }
334 ZFREE(z, v);
335 return r;
336 }
337
338
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 */
349 {
350 int r;
351 uInt hn = 0; /* hufts used in space */
352 uIntf *v; /* work area for huft_build */
353
354 /* allocate work area */
355 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
356 return Z_MEM_ERROR;
357
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)
361 {
362 if (r == Z_DATA_ERROR)
363 z->msg = (char*)"oversubscribed literal/length tree";
364 else if (r != Z_MEM_ERROR)
365 {
366 z->msg = (char*)"incomplete literal/length tree";
367 r = Z_DATA_ERROR;
368 }
369 ZFREE(z, v);
370 return r;
371 }
372
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))
376 {
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
381 r = Z_OK;
382 }
383 #else
384 z->msg = (char*)"incomplete distance tree";
385 r = Z_DATA_ERROR;
386 }
387 else if (r != Z_MEM_ERROR)
388 {
389 z->msg = (char*)"empty distance tree with lengths";
390 r = Z_DATA_ERROR;
391 }
392 ZFREE(z, v);
393 return r;
394 #endif
395 }
396
397 /* done */
398 ZFREE(z, v);
399 return Z_OK;
400 }
401
402
403 /* build fixed tables only once--keep them here */
404 #ifdef BUILDFIXED
405 local int fixed_built = 0;
406 #define FIXEDH 544 /* number of hufts used by fixed tables */
407 local inflate_huft fixed_mem[FIXEDH];
408 local uInt fixed_bl;
409 local uInt fixed_bd;
410 local inflate_huft *fixed_tl;
411 local inflate_huft *fixed_td;
412 #else
413 #include "inffixed.h"
414 #endif
415
416
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 */
423 {
424 #ifdef BUILDFIXED
425 /* build fixed tables if not already */
426 if (!fixed_built)
427 {
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 */
432
433 /* allocate memory */
434 if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
435 return Z_MEM_ERROR;
436 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
437 {
438 ZFREE(z, c);
439 return Z_MEM_ERROR;
440 }
441
442 /* literal table */
443 for (k = 0; k < 144; k++)
444 c[k] = 8;
445 for (; k < 256; k++)
446 c[k] = 9;
447 for (; k < 280; k++)
448 c[k] = 7;
449 for (; k < 288; k++)
450 c[k] = 8;
451 fixed_bl = 9;
452 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
453 fixed_mem, &f, v);
454
455 /* distance table */
456 for (k = 0; k < 30; k++)
457 c[k] = 5;
458 fixed_bd = 5;
459 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
460 fixed_mem, &f, v);
461
462 /* done */
463 ZFREE(z, v);
464 ZFREE(z, c);
465 fixed_built = 1;
466 }
467 #endif
468 *bl = fixed_bl;
469 *bd = fixed_bd;
470 *tl = fixed_tl;
471 *td = fixed_td;
472 return Z_OK;
473 }