OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / zlib / 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 #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.3 Copyright 1995-1998 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 local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
94 uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
95 uInt n;                 /* number of codes (assumed <= 288) */
96 uInt s;                 /* number of simple-valued codes (0..s-1) */
97 const uIntf *d;         /* list of base values for non-simple codes */
98 const uIntf *e;         /* list of extra bits for non-simple codes */
99 inflate_huft * FAR *t;  /* result: starting table */
100 uIntf *m;               /* maximum lookup bits, returns actual */
101 inflate_huft *hp;       /* space for trees */
102 uInt *hn;               /* hufts used in space */
103 uIntf *v;               /* working area: values in order of bit length */
104 /* Given a list of code lengths and a maximum table size, make a set of
105    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
106    if the given code set is incomplete (the tables are still built in this
107    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
108    lengths), or Z_MEM_ERROR if not enough memory. */
109 {
110
111   uInt a;                       /* counter for codes of length k */
112   uInt c[BMAX+1];               /* bit length count table */
113   uInt f;                       /* i repeats in table every f entries */
114   int g;                        /* maximum code length */
115   int h;                        /* table level */
116   register uInt i;              /* counter, current code */
117   register uInt j;              /* counter */
118   register int k;               /* number of bits in current code */
119   int l;                        /* bits per table (returned in m) */
120   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
121   register uIntf *p;            /* pointer into c[], b[], or v[] */
122   inflate_huft *q;              /* points to current table */
123   struct inflate_huft_s r;      /* table entry for structure assignment */
124   inflate_huft *u[BMAX];        /* table stack */
125   register int w;               /* bits before this table == (l * h) */
126   uInt x[BMAX+1];               /* bit offsets, then code stack */
127   uIntf *xp;                    /* pointer into x */
128   int y;                        /* number of dummy codes added */
129   uInt z;                       /* number of entries in current table */
130
131
132   /* Generate counts for each bit length */
133   p = c;
134 #define C0 *p++ = 0;
135 #define C2 C0 C0 C0 C0
136 #define C4 C2 C2 C2 C2
137   C4                            /* clear c[]--assume BMAX+1 is 16 */
138   p = b;  i = n;
139   do {
140     c[*p++]++;                  /* assume all entries <= BMAX */
141   } while (--i);
142   if (c[0] == n)                /* null input--all zero length codes */
143   {
144     *t = (inflate_huft *)Z_NULL;
145     *m = 0;
146     return Z_OK;
147   }
148
149
150   /* Find minimum and maximum length, bound *m by those */
151   l = *m;
152   for (j = 1; j <= BMAX; j++)
153     if (c[j])
154       break;
155   k = j;                        /* minimum code length */
156   if ((uInt)l < j)
157     l = j;
158   for (i = BMAX; i; i--)
159     if (c[i])
160       break;
161   g = i;                        /* maximum code length */
162   if ((uInt)l > i)
163     l = i;
164   *m = l;
165
166
167   /* Adjust last length count to fill out codes, if needed */
168   for (y = 1 << j; j < i; j++, y <<= 1)
169     if ((y -= c[j]) < 0)
170       return Z_DATA_ERROR;
171   if ((y -= c[i]) < 0)
172     return Z_DATA_ERROR;
173   c[i] += y;
174
175
176   /* Generate starting offsets into the value table for each length */
177   x[1] = j = 0;
178   p = c + 1;  xp = x + 2;
179   while (--i) {                 /* note that i == g from above */
180     *xp++ = (j += *p++);
181   }
182
183
184   /* Make a table of values in order of bit lengths */
185   p = b;  i = 0;
186   do {
187     if ((j = *p++) != 0)
188       v[x[j]++] = i;
189   } while (++i < n);
190   n = x[g];                     /* set n to length of v */
191
192
193   /* Generate the Huffman codes and for each, make the table entries */
194   x[0] = i = 0;                 /* first Huffman code is zero */
195   p = v;                        /* grab values in bit order */
196   h = -1;                       /* no tables yet--level -1 */
197   w = -l;                       /* bits decoded == (l * h) */
198   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
199   q = (inflate_huft *)Z_NULL;   /* ditto */
200   z = 0;                        /* ditto */
201
202   /* go through the bit lengths (k already is bits in shortest code) */
203   for (; k <= g; k++)
204   {
205     a = c[k];
206     while (a--)
207     {
208       /* here i is the Huffman code of length k bits for value *p */
209       /* make tables up to required level */
210       while (k > w + l)
211       {
212         h++;
213         w += l;                 /* previous table always l bits */
214
215         /* compute minimum size table less than or equal to l bits */
216         z = g - w;
217         z = z > (uInt)l ? l : z;        /* table size upper limit */
218         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
219         {                       /* too few codes for k-w bit table */
220           f -= a + 1;           /* deduct codes from patterns left */
221           xp = c + k;
222           if (j < z)
223             while (++j < z)     /* try smaller tables up to z bits */
224             {
225               if ((f <<= 1) <= *++xp)
226                 break;          /* enough codes to use up j bits */
227               f -= *xp;         /* else deduct codes from patterns */
228             }
229         }
230         z = 1 << j;             /* table entries for j-bit table */
231
232         /* allocate new table */
233         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
234           return Z_MEM_ERROR;   /* not enough memory */
235         u[h] = q = hp + *hn;
236         *hn += z;
237
238         /* connect to last table, if there is one */
239         if (h)
240         {
241           x[h] = i;             /* save pattern for backing up */
242           r.bits = (Byte)l;     /* bits to dump before this table */
243           r.exop = (Byte)j;     /* bits in this table */
244           j = i >> (w - l);
245           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
246           u[h-1][j] = r;        /* connect to last table */
247         }
248         else
249           *t = q;               /* first table is returned result */
250       }
251
252       /* set up table entry in r */
253       r.bits = (Byte)(k - w);
254       if (p >= v + n)
255         r.exop = 128 + 64;      /* out of values--invalid code */
256       else if (*p < s)
257       {
258         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
259         r.base = *p++;          /* simple code is just the value */
260       }
261       else
262       {
263         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
264         r.base = d[*p++ - s];
265       }
266
267       /* fill code-like entries with r */
268       f = 1 << (k - w);
269       for (j = i >> w; j < z; j += f)
270         q[j] = r;
271
272       /* backwards increment the k-bit code i */
273       for (j = 1 << (k - 1); i & j; j >>= 1)
274         i ^= j;
275       i ^= j;
276
277       /* backup over finished tables */
278       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
279       while ((i & mask) != x[h])
280       {
281         h--;                    /* don't need to update q */
282         w -= l;
283         mask = (1 << w) - 1;
284       }
285     }
286   }
287
288
289   /* Return Z_BUF_ERROR if we were given an incomplete table */
290   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
291 }
292
293
294 int inflate_trees_bits(c, bb, tb, hp, z)
295 uIntf *c;               /* 19 code lengths */
296 uIntf *bb;              /* bits tree desired/actual depth */
297 inflate_huft * FAR *tb; /* bits tree result */
298 inflate_huft *hp;       /* space for trees */
299 z_streamp z;            /* for messages */
300 {
301   int r;
302   uInt hn = 0;          /* hufts used in space */
303   uIntf *v;             /* work area for huft_build */
304
305   if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
306     return Z_MEM_ERROR;
307   r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
308                  tb, bb, hp, &hn, v);
309   if (r == Z_DATA_ERROR)
310     z->msg = (char*)"oversubscribed dynamic bit lengths tree";
311   else if (r == Z_BUF_ERROR || *bb == 0)
312   {
313     z->msg = (char*)"incomplete dynamic bit lengths tree";
314     r = Z_DATA_ERROR;
315   }
316   ZFREE(z, v);
317   return r;
318 }
319
320
321 int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
322 uInt nl;                /* number of literal/length codes */
323 uInt nd;                /* number of distance codes */
324 uIntf *c;               /* that many (total) code lengths */
325 uIntf *bl;              /* literal desired/actual bit depth */
326 uIntf *bd;              /* distance desired/actual bit depth */
327 inflate_huft * FAR *tl; /* literal/length tree result */
328 inflate_huft * FAR *td; /* distance tree result */
329 inflate_huft *hp;       /* space for trees */
330 z_streamp z;            /* for messages */
331 {
332   int r;
333   uInt hn = 0;          /* hufts used in space */
334   uIntf *v;             /* work area for huft_build */
335
336   /* allocate work area */
337   if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
338     return Z_MEM_ERROR;
339
340   /* build literal/length tree */
341   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
342   if (r != Z_OK || *bl == 0)
343   {
344     if (r == Z_DATA_ERROR)
345       z->msg = (char*)"oversubscribed literal/length tree";
346     else if (r != Z_MEM_ERROR)
347     {
348       z->msg = (char*)"incomplete literal/length tree";
349       r = Z_DATA_ERROR;
350     }
351     ZFREE(z, v);
352     return r;
353   }
354
355   /* build distance tree */
356   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
357   if (r != Z_OK || (*bd == 0 && nl > 257))
358   {
359     if (r == Z_DATA_ERROR)
360       z->msg = (char*)"oversubscribed distance tree";
361     else if (r == Z_BUF_ERROR) {
362 #ifdef PKZIP_BUG_WORKAROUND
363       r = Z_OK;
364     }
365 #else
366       z->msg = (char*)"incomplete distance tree";
367       r = Z_DATA_ERROR;
368     }
369     else if (r != Z_MEM_ERROR)
370     {
371       z->msg = (char*)"empty distance tree with lengths";
372       r = Z_DATA_ERROR;
373     }
374     ZFREE(z, v);
375     return r;
376 #endif
377   }
378
379   /* done */
380   ZFREE(z, v);
381   return Z_OK;
382 }
383
384
385 /* build fixed tables only once--keep them here */
386 #ifdef BUILDFIXED
387 local int fixed_built = 0;
388 #define FIXEDH 544      /* number of hufts used by fixed tables */
389 local inflate_huft fixed_mem[FIXEDH];
390 local uInt fixed_bl;
391 local uInt fixed_bd;
392 local inflate_huft *fixed_tl;
393 local inflate_huft *fixed_td;
394 #else
395 #include "inffixed.h"
396 #endif
397
398
399 int inflate_trees_fixed(bl, bd, tl, td, z)
400 uIntf *bl;               /* literal desired/actual bit depth */
401 uIntf *bd;               /* distance desired/actual bit depth */
402 inflate_huft * FAR *tl;  /* literal/length tree result */
403 inflate_huft * FAR *td;  /* distance tree result */
404 z_streamp z;             /* for memory allocation */
405 {
406 #ifdef BUILDFIXED
407   /* build fixed tables if not already */
408   if (!fixed_built)
409   {
410     int k;              /* temporary variable */
411     uInt f = 0;         /* number of hufts used in fixed_mem */
412     uIntf *c;           /* length list for huft_build */
413     uIntf *v;           /* work area for huft_build */
414
415     /* allocate memory */
416     if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
417       return Z_MEM_ERROR;
418     if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
419     {
420       ZFREE(z, c);
421       return Z_MEM_ERROR;
422     }
423
424     /* literal table */
425     for (k = 0; k < 144; k++)
426       c[k] = 8;
427     for (; k < 256; k++)
428       c[k] = 9;
429     for (; k < 280; k++)
430       c[k] = 7;
431     for (; k < 288; k++)
432       c[k] = 8;
433     fixed_bl = 9;
434     huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
435                fixed_mem, &f, v);
436
437     /* distance table */
438     for (k = 0; k < 30; k++)
439       c[k] = 5;
440     fixed_bd = 5;
441     huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
442                fixed_mem, &f, v);
443
444     /* done */
445     ZFREE(z, v);
446     ZFREE(z, c);
447     fixed_built = 1;
448   }
449 #endif
450   *bl = fixed_bl;
451   *bd = fixed_bd;
452   *tl = fixed_tl;
453   *td = fixed_td;
454   return Z_OK;
455 }