aboutsummaryrefslogtreecommitdiffstats
path: root/lib/zlib_inflate/inftrees.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/zlib_inflate/inftrees.c')
-rw-r--r--lib/zlib_inflate/inftrees.c683
1 files changed, 300 insertions, 383 deletions
diff --git a/lib/zlib_inflate/inftrees.c b/lib/zlib_inflate/inftrees.c
index 874950ec4858..62343c53bf7e 100644
--- a/lib/zlib_inflate/inftrees.c
+++ b/lib/zlib_inflate/inftrees.c
@@ -1,412 +1,329 @@
1/* inftrees.c -- generate Huffman trees for efficient decoding 1/* inftrees.c -- generate Huffman trees for efficient decoding
2 * Copyright (C) 1995-1998 Mark Adler 2 * Copyright (C) 1995-2005 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6#include <linux/zutil.h> 6#include <linux/zutil.h>
7#include "inftrees.h" 7#include "inftrees.h"
8#include "infutil.h"
9 8
10static const char inflate_copyright[] __attribute_used__ = 9#define MAXBITS 15
11 " inflate 1.1.3 Copyright 1995-1998 Mark Adler "; 10
11const char inflate_copyright[] =
12 " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
12/* 13/*
13 If you use the zlib library in a product, an acknowledgment is welcome 14 If you use the zlib library in a product, an acknowledgment is welcome
14 in the documentation of your product. If for some reason you cannot 15 in the documentation of your product. If for some reason you cannot
15 include such an acknowledgment, I would appreciate that you keep this 16 include such an acknowledgment, I would appreciate that you keep this
16 copyright string in the executable of your product. 17 copyright string in the executable of your product.
17 */ 18 */
18struct internal_state;
19
20/* simplify the use of the inflate_huft type with some defines */
21#define exop word.what.Exop
22#define bits word.what.Bits
23
24
25static int huft_build (
26 uInt *, /* code lengths in bits */
27 uInt, /* number of codes */
28 uInt, /* number of "simple" codes */
29 const uInt *, /* list of base values for non-simple codes */
30 const uInt *, /* list of extra bits for non-simple codes */
31 inflate_huft **, /* result: starting table */
32 uInt *, /* maximum lookup bits (returns actual) */
33 inflate_huft *, /* space for trees */
34 uInt *, /* hufts used in space */
35 uInt * ); /* space for values */
36
37/* Tables for deflate from PKZIP's appnote.txt. */
38static const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
39 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
40 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
41 /* see note #13 above about 258 */
42static const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
43 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
44 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
45static const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
46 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
47 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
48 8193, 12289, 16385, 24577};
49static const uInt cpdext[30] = { /* Extra bits for distance codes */
50 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
51 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
52 12, 12, 13, 13};
53 19
54/* 20/*
55 Huffman code decoding is performed using a multi-level table lookup. 21 Build a set of tables to decode the provided canonical Huffman code.
56 The fastest way to decode is to simply build a lookup table whose 22 The code lengths are lens[0..codes-1]. The result starts at *table,
57 size is determined by the longest code. However, the time it takes 23 whose indices are 0..2^bits-1. work is a writable array of at least
58 to build this table can also be a factor if the data being decoded 24 lens shorts, which is used as a work area. type is the type of code
59 is not very long. The most common codes are necessarily the 25 to be generated, CODES, LENS, or DISTS. On return, zero is success,
60 shortest codes, so those codes dominate the decoding time, and hence 26 -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
61 the speed. The idea is you can have a shorter table that decodes the 27 on return points to the next available entry's address. bits is the
62 shorter, more probable codes, and then point to subsidiary tables for 28 requested root table index bits, and on return it is the actual root
63 the longer codes. The time it costs to decode the longer codes is 29 table index bits. It will differ if the request is greater than the
64 then traded against the time it takes to make longer tables. 30 longest code or if it is less than the shortest code.
65
66 This results of this trade are in the variables lbits and dbits
67 below. lbits is the number of bits the first level table for literal/
68 length codes can decode in one step, and dbits is the same thing for
69 the distance codes. Subsequent tables are also less than or equal to
70 those sizes. These values may be adjusted either when all of the
71 codes are shorter than that, in which case the longest code length in
72 bits is used, or when the shortest code is *longer* than the requested
73 table size, in which case the length of the shortest code in bits is
74 used.
75
76 There are two different values for the two tables, since they code a
77 different number of possibilities each. The literal/length table
78 codes 286 possible values, or in a flat code, a little over eight
79 bits. The distance table codes 30 possible values, or a little less
80 than five bits, flat. The optimum values for speed end up being
81 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
82 The optimum values may differ though from machine to machine, and
83 possibly even between compilers. Your mileage may vary.
84 */ 31 */
85 32int zlib_inflate_table(type, lens, codes, table, bits, work)
86 33codetype type;
87/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ 34unsigned short *lens;
88#define BMAX 15 /* maximum bit length of any code */ 35unsigned codes;
89 36code **table;
90static int huft_build( 37unsigned *bits;
91 uInt *b, /* code lengths in bits (all assumed <= BMAX) */ 38unsigned short *work;
92 uInt n, /* number of codes (assumed <= 288) */
93 uInt s, /* number of simple-valued codes (0..s-1) */
94 const uInt *d, /* list of base values for non-simple codes */
95 const uInt *e, /* list of extra bits for non-simple codes */
96 inflate_huft **t, /* result: starting table */
97 uInt *m, /* maximum lookup bits, returns actual */
98 inflate_huft *hp, /* space for trees */
99 uInt *hn, /* hufts used in space */
100 uInt *v /* working area: values in order of bit length */
101)
102/* Given a list of code lengths and a maximum table size, make a set of
103 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
104 if the given code set is incomplete (the tables are still built in this
105 case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
106 lengths), or Z_MEM_ERROR if not enough memory. */
107{ 39{
40 unsigned len; /* a code's length in bits */
41 unsigned sym; /* index of code symbols */
42 unsigned min, max; /* minimum and maximum code lengths */
43 unsigned root; /* number of index bits for root table */
44 unsigned curr; /* number of index bits for current table */
45 unsigned drop; /* code bits to drop for sub-table */
46 int left; /* number of prefix codes available */
47 unsigned used; /* code entries in table used */
48 unsigned huff; /* Huffman code */
49 unsigned incr; /* for incrementing code, index */
50 unsigned fill; /* index for replicating entries */
51 unsigned low; /* low bits for current root entry */
52 unsigned mask; /* mask for low root bits */
53 code this; /* table entry for duplication */
54 code *next; /* next available space in table */
55 const unsigned short *base; /* base value table to use */
56 const unsigned short *extra; /* extra bits table to use */
57 int end; /* use base and extra for symbol > end */
58 unsigned short count[MAXBITS+1]; /* number of codes of each length */
59 unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
60 static const unsigned short lbase[31] = { /* Length codes 257..285 base */
61 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
62 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
63 static const unsigned short lext[31] = { /* Length codes 257..285 extra */
64 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
66 static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
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, 0, 0};
70 static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
71 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
72 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
73 28, 28, 29, 29, 64, 64};
74
75 /*
76 Process a set of code lengths to create a canonical Huffman code. The
77 code lengths are lens[0..codes-1]. Each length corresponds to the
78 symbols 0..codes-1. The Huffman code is generated by first sorting the
79 symbols by length from short to long, and retaining the symbol order
80 for codes with equal lengths. Then the code starts with all zero bits
81 for the first code of the shortest length, and the codes are integer
82 increments for the same length, and zeros are appended as the length
83 increases. For the deflate format, these bits are stored backwards
84 from their more natural integer increment ordering, and so when the
85 decoding tables are built in the large loop below, the integer codes
86 are incremented backwards.
87
88 This routine assumes, but does not check, that all of the entries in
89 lens[] are in the range 0..MAXBITS. The caller must assure this.
90 1..MAXBITS is interpreted as that code length. zero means that that
91 symbol does not occur in this code.
92
93 The codes are sorted by computing a count of codes for each length,
94 creating from that a table of starting indices for each length in the
95 sorted table, and then entering the symbols in order in the sorted
96 table. The sorted table is work[], with that space being provided by
97 the caller.
98
99 The length counts are used for other purposes as well, i.e. finding
100 the minimum and maximum length codes, determining if there are any
101 codes at all, checking for a valid set of lengths, and looking ahead
102 at length counts to determine sub-table sizes when building the
103 decoding tables.
104 */
105
106 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
107 for (len = 0; len <= MAXBITS; len++)
108 count[len] = 0;
109 for (sym = 0; sym < codes; sym++)
110 count[lens[sym]]++;
111
112 /* bound code lengths, force root to be within code lengths */
113 root = *bits;
114 for (max = MAXBITS; max >= 1; max--)
115 if (count[max] != 0) break;
116 if (root > max) root = max;
117 if (max == 0) { /* no symbols to code at all */
118 this.op = (unsigned char)64; /* invalid code marker */
119 this.bits = (unsigned char)1;
120 this.val = (unsigned short)0;
121 *(*table)++ = this; /* make a table to force an error */
122 *(*table)++ = this;
123 *bits = 1;
124 return 0; /* no symbols, but wait for decoding to report error */
125 }
126 for (min = 1; min <= MAXBITS; min++)
127 if (count[min] != 0) break;
128 if (root < min) root = min;
129
130 /* check for an over-subscribed or incomplete set of lengths */
131 left = 1;
132 for (len = 1; len <= MAXBITS; len++) {
133 left <<= 1;
134 left -= count[len];
135 if (left < 0) return -1; /* over-subscribed */
136 }
137 if (left > 0 && (type == CODES || max != 1))
138 return -1; /* incomplete set */
139
140 /* generate offsets into symbol table for each length for sorting */
141 offs[1] = 0;
142 for (len = 1; len < MAXBITS; len++)
143 offs[len + 1] = offs[len] + count[len];
144
145 /* sort symbols by length, by symbol order within each length */
146 for (sym = 0; sym < codes; sym++)
147 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
148
149 /*
150 Create and fill in decoding tables. In this loop, the table being
151 filled is at next and has curr index bits. The code being used is huff
152 with length len. That code is converted to an index by dropping drop
153 bits off of the bottom. For codes where len is less than drop + curr,
154 those top drop + curr - len bits are incremented through all values to
155 fill the table with replicated entries.
156
157 root is the number of index bits for the root table. When len exceeds
158 root, sub-tables are created pointed to by the root entry with an index
159 of the low root bits of huff. This is saved in low to check for when a
160 new sub-table should be started. drop is zero when the root table is
161 being filled, and drop is root when sub-tables are being filled.
162
163 When a new sub-table is needed, it is necessary to look ahead in the
164 code lengths to determine what size sub-table is needed. The length
165 counts are used for this, and so count[] is decremented as codes are
166 entered in the tables.
167
168 used keeps track of how many table entries have been allocated from the
169 provided *table space. It is checked when a LENS table is being made
170 against the space in *table, ENOUGH, minus the maximum space needed by
171 the worst case distance code, MAXD. This should never happen, but the
172 sufficiency of ENOUGH has not been proven exhaustively, hence the check.
173 This assumes that when type == LENS, bits == 9.
174
175 sym increments through all symbols, and the loop terminates when
176 all codes of length max, i.e. all codes, have been processed. This
177 routine permits incomplete codes, so another loop after this one fills
178 in the rest of the decoding tables with invalid code markers.
179 */
180
181 /* set up for code type */
182 switch (type) {
183 case CODES:
184 base = extra = work; /* dummy value--not used */
185 end = 19;
186 break;
187 case LENS:
188 base = lbase;
189 base -= 257;
190 extra = lext;
191 extra -= 257;
192 end = 256;
193 break;
194 default: /* DISTS */
195 base = dbase;
196 extra = dext;
197 end = -1;
198 }
108 199
109 uInt a; /* counter for codes of length k */ 200 /* initialize state for loop */
110 uInt c[BMAX+1]; /* bit length count table */ 201 huff = 0; /* starting code */
111 uInt f; /* i repeats in table every f entries */ 202 sym = 0; /* starting code symbol */
112 int g; /* maximum code length */ 203 len = min; /* starting code length */
113 int h; /* table level */ 204 next = *table; /* current table to fill in */
114 register uInt i; /* counter, current code */ 205 curr = root; /* current table index bits */
115 register uInt j; /* counter */ 206 drop = 0; /* current bits to drop from code for index */
116 register int k; /* number of bits in current code */ 207 low = (unsigned)(-1); /* trigger new sub-table when len > root */
117 int l; /* bits per table (returned in m) */ 208 used = 1U << root; /* use root table entries */
118 uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */ 209 mask = used - 1; /* mask for comparing low */
119 register uInt *p; /* pointer into c[], b[], or v[] */ 210
120 inflate_huft *q; /* points to current table */ 211 /* check available table space */
121 struct inflate_huft_s r; /* table entry for structure assignment */ 212 if (type == LENS && used >= ENOUGH - MAXD)
122 inflate_huft *u[BMAX]; /* table stack */ 213 return 1;
123 register int w; /* bits before this table == (l * h) */ 214
124 uInt x[BMAX+1]; /* bit offsets, then code stack */ 215 /* process all codes and make table entries */
125 uInt *xp; /* pointer into x */ 216 for (;;) {
126 int y; /* number of dummy codes added */ 217 /* create table entry */
127 uInt z; /* number of entries in current table */ 218 this.bits = (unsigned char)(len - drop);
128 219 if ((int)(work[sym]) < end) {
129 220 this.op = (unsigned char)0;
130 /* Generate counts for each bit length */ 221 this.val = work[sym];
131 p = c;
132#define C0 *p++ = 0;
133#define C2 C0 C0 C0 C0
134#define C4 C2 C2 C2 C2
135 C4 /* clear c[]--assume BMAX+1 is 16 */
136 p = b; i = n;
137 do {
138 c[*p++]++; /* assume all entries <= BMAX */
139 } while (--i);
140 if (c[0] == n) /* null input--all zero length codes */
141 {
142 *t = NULL;
143 *m = 0;
144 return Z_OK;
145 }
146
147
148 /* Find minimum and maximum length, bound *m by those */
149 l = *m;
150 for (j = 1; j <= BMAX; j++)
151 if (c[j])
152 break;
153 k = j; /* minimum code length */
154 if ((uInt)l < j)
155 l = j;
156 for (i = BMAX; i; i--)
157 if (c[i])
158 break;
159 g = i; /* maximum code length */
160 if ((uInt)l > i)
161 l = i;
162 *m = l;
163
164
165 /* Adjust last length count to fill out codes, if needed */
166 for (y = 1 << j; j < i; j++, y <<= 1)
167 if ((y -= c[j]) < 0)
168 return Z_DATA_ERROR;
169 if ((y -= c[i]) < 0)
170 return Z_DATA_ERROR;
171 c[i] += y;
172
173
174 /* Generate starting offsets into the value table for each length */
175 x[1] = j = 0;
176 p = c + 1; xp = x + 2;
177 while (--i) { /* note that i == g from above */
178 *xp++ = (j += *p++);
179 }
180
181
182 /* Make a table of values in order of bit lengths */
183 p = b; i = 0;
184 do {
185 if ((j = *p++) != 0)
186 v[x[j]++] = i;
187 } while (++i < n);
188 n = x[g]; /* set n to length of v */
189
190
191 /* Generate the Huffman codes and for each, make the table entries */
192 x[0] = i = 0; /* first Huffman code is zero */
193 p = v; /* grab values in bit order */
194 h = -1; /* no tables yet--level -1 */
195 w = -l; /* bits decoded == (l * h) */
196 u[0] = NULL; /* just to keep compilers happy */
197 q = NULL; /* ditto */
198 z = 0; /* ditto */
199
200 /* go through the bit lengths (k already is bits in shortest code) */
201 for (; k <= g; k++)
202 {
203 a = c[k];
204 while (a--)
205 {
206 /* here i is the Huffman code of length k bits for value *p */
207 /* make tables up to required level */
208 while (k > w + l)
209 {
210 h++;
211 w += l; /* previous table always l bits */
212
213 /* compute minimum size table less than or equal to l bits */
214 z = g - w;
215 z = z > (uInt)l ? l : z; /* table size upper limit */
216 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
217 { /* too few codes for k-w bit table */
218 f -= a + 1; /* deduct codes from patterns left */
219 xp = c + k;
220 if (j < z)
221 while (++j < z) /* try smaller tables up to z bits */
222 {
223 if ((f <<= 1) <= *++xp)
224 break; /* enough codes to use up j bits */
225 f -= *xp; /* else deduct codes from patterns */
226 }
227 } 222 }
228 z = 1 << j; /* table entries for j-bit table */ 223 else if ((int)(work[sym]) > end) {
229 224 this.op = (unsigned char)(extra[work[sym]]);
230 /* allocate new table */ 225 this.val = base[work[sym]];
231 if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ 226 }
232 return Z_DATA_ERROR; /* overflow of MANY */ 227 else {
233 u[h] = q = hp + *hn; 228 this.op = (unsigned char)(32 + 64); /* end of block */
234 *hn += z; 229 this.val = 0;
235
236 /* connect to last table, if there is one */
237 if (h)
238 {
239 x[h] = i; /* save pattern for backing up */
240 r.bits = (Byte)l; /* bits to dump before this table */
241 r.exop = (Byte)j; /* bits in this table */
242 j = i >> (w - l);
243 r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
244 u[h-1][j] = r; /* connect to last table */
245 } 230 }
246 else
247 *t = q; /* first table is returned result */
248 }
249
250 /* set up table entry in r */
251 r.bits = (Byte)(k - w);
252 if (p >= v + n)
253 r.exop = 128 + 64; /* out of values--invalid code */
254 else if (*p < s)
255 {
256 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
257 r.base = *p++; /* simple code is just the value */
258 }
259 else
260 {
261 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
262 r.base = d[*p++ - s];
263 }
264
265 /* fill code-like entries with r */
266 f = 1 << (k - w);
267 for (j = i >> w; j < z; j += f)
268 q[j] = r;
269
270 /* backwards increment the k-bit code i */
271 for (j = 1 << (k - 1); i & j; j >>= 1)
272 i ^= j;
273 i ^= j;
274
275 /* backup over finished tables */
276 mask = (1 << w) - 1; /* needed on HP, cc -O bug */
277 while ((i & mask) != x[h])
278 {
279 h--; /* don't need to update q */
280 w -= l;
281 mask = (1 << w) - 1;
282 }
283 }
284 }
285 231
232 /* replicate for those indices with low len bits equal to huff */
233 incr = 1U << (len - drop);
234 fill = 1U << curr;
235 min = fill; /* save offset to next table */
236 do {
237 fill -= incr;
238 next[(huff >> drop) + fill] = this;
239 } while (fill != 0);
240
241 /* backwards increment the len-bit code huff */
242 incr = 1U << (len - 1);
243 while (huff & incr)
244 incr >>= 1;
245 if (incr != 0) {
246 huff &= incr - 1;
247 huff += incr;
248 }
249 else
250 huff = 0;
286 251
287 /* Return Z_BUF_ERROR if we were given an incomplete table */ 252 /* go to next symbol, update count, len */
288 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; 253 sym++;
289} 254 if (--(count[len]) == 0) {
255 if (len == max) break;
256 len = lens[work[sym]];
257 }
290 258
259 /* create new sub-table if needed */
260 if (len > root && (huff & mask) != low) {
261 /* if first time, transition to sub-tables */
262 if (drop == 0)
263 drop = root;
264
265 /* increment past last table */
266 next += min; /* here min is 1 << curr */
267
268 /* determine length of next table */
269 curr = len - drop;
270 left = (int)(1 << curr);
271 while (curr + drop < max) {
272 left -= count[curr + drop];
273 if (left <= 0) break;
274 curr++;
275 left <<= 1;
276 }
291 277
292int zlib_inflate_trees_bits( 278 /* check for enough space */
293 uInt *c, /* 19 code lengths */ 279 used += 1U << curr;
294 uInt *bb, /* bits tree desired/actual depth */ 280 if (type == LENS && used >= ENOUGH - MAXD)
295 inflate_huft **tb, /* bits tree result */ 281 return 1;
296 inflate_huft *hp, /* space for trees */
297 z_streamp z /* for messages */
298)
299{
300 int r;
301 uInt hn = 0; /* hufts used in space */
302 uInt *v; /* work area for huft_build */
303
304 v = WS(z)->tree_work_area_1;
305 r = huft_build(c, 19, 19, NULL, NULL, tb, bb, hp, &hn, v);
306 if (r == Z_DATA_ERROR)
307 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
308 else if (r == Z_BUF_ERROR || *bb == 0)
309 {
310 z->msg = (char*)"incomplete dynamic bit lengths tree";
311 r = Z_DATA_ERROR;
312 }
313 return r;
314}
315 282
316int zlib_inflate_trees_dynamic( 283 /* point entry in root table to sub-table */
317 uInt nl, /* number of literal/length codes */ 284 low = huff & mask;
318 uInt nd, /* number of distance codes */ 285 (*table)[low].op = (unsigned char)curr;
319 uInt *c, /* that many (total) code lengths */ 286 (*table)[low].bits = (unsigned char)root;
320 uInt *bl, /* literal desired/actual bit depth */ 287 (*table)[low].val = (unsigned short)(next - *table);
321 uInt *bd, /* distance desired/actual bit depth */ 288 }
322 inflate_huft **tl, /* literal/length tree result */
323 inflate_huft **td, /* distance tree result */
324 inflate_huft *hp, /* space for trees */
325 z_streamp z /* for messages */
326)
327{
328 int r;
329 uInt hn = 0; /* hufts used in space */
330 uInt *v; /* work area for huft_build */
331
332 /* allocate work area */
333 v = WS(z)->tree_work_area_2;
334
335 /* build literal/length tree */
336 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
337 if (r != Z_OK || *bl == 0)
338 {
339 if (r == Z_DATA_ERROR)
340 z->msg = (char*)"oversubscribed literal/length tree";
341 else if (r != Z_MEM_ERROR)
342 {
343 z->msg = (char*)"incomplete literal/length tree";
344 r = Z_DATA_ERROR;
345 }
346 return r;
347 }
348
349 /* build distance tree */
350 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
351 if (r != Z_OK || (*bd == 0 && nl > 257))
352 {
353 if (r == Z_DATA_ERROR)
354 z->msg = (char*)"oversubscribed distance tree";
355 else if (r == Z_BUF_ERROR) {
356#ifdef PKZIP_BUG_WORKAROUND
357 r = Z_OK;
358 }
359#else
360 z->msg = (char*)"incomplete distance tree";
361 r = Z_DATA_ERROR;
362 }
363 else if (r != Z_MEM_ERROR)
364 {
365 z->msg = (char*)"empty distance tree with lengths";
366 r = Z_DATA_ERROR;
367 } 289 }
368 return r;
369#endif
370 }
371 290
372 /* done */ 291 /*
373 return Z_OK; 292 Fill in rest of table for incomplete codes. This loop is similar to the
374} 293 loop above in incrementing huff for table indices. It is assumed that
294 len is equal to curr + drop, so there is no loop needed to increment
295 through high index bits. When the current sub-table is filled, the loop
296 drops back to the root table to fill in any remaining entries there.
297 */
298 this.op = (unsigned char)64; /* invalid code marker */
299 this.bits = (unsigned char)(len - drop);
300 this.val = (unsigned short)0;
301 while (huff != 0) {
302 /* when done with sub-table, drop back to root table */
303 if (drop != 0 && (huff & mask) != low) {
304 drop = 0;
305 len = root;
306 next = *table;
307 this.bits = (unsigned char)len;
308 }
375 309
310 /* put invalid code marker in table */
311 next[huff >> drop] = this;
376 312
377int zlib_inflate_trees_fixed( 313 /* backwards increment the len-bit code huff */
378 uInt *bl, /* literal desired/actual bit depth */ 314 incr = 1U << (len - 1);
379 uInt *bd, /* distance desired/actual bit depth */ 315 while (huff & incr)
380 inflate_huft **tl, /* literal/length tree result */ 316 incr >>= 1;
381 inflate_huft **td, /* distance tree result */ 317 if (incr != 0) {
382 inflate_huft *hp, /* space for trees */ 318 huff &= incr - 1;
383 z_streamp z /* for memory allocation */ 319 huff += incr;
384) 320 }
385{ 321 else
386 int i; /* temporary variable */ 322 huff = 0;
387 unsigned l[288]; /* length list for huft_build */ 323 }
388 uInt *v; /* work area for huft_build */ 324
389 325 /* set return parameters */
390 /* set up literal table */ 326 *table += used;
391 for (i = 0; i < 144; i++) 327 *bits = root;
392 l[i] = 8; 328 return 0;
393 for (; i < 256; i++)
394 l[i] = 9;
395 for (; i < 280; i++)
396 l[i] = 7;
397 for (; i < 288; i++) /* make a complete, but wrong code set */
398 l[i] = 8;
399 *bl = 9;
400 v = WS(z)->tree_work_area_1;
401 if ((i = huft_build(l, 288, 257, cplens, cplext, tl, bl, hp, &i, v)) != 0)
402 return i;
403
404 /* set up distance table */
405 for (i = 0; i < 30; i++) /* make an incomplete code set */
406 l[i] = 5;
407 *bd = 5;
408 if ((i = huft_build(l, 30, 0, cpdist, cpdext, td, bd, hp, &i, v)) > 1)
409 return i;
410
411 return Z_OK;
412} 329}