annotate lib/zlib/inftrees.c @ 2135:e99081a3a66a

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