diff options
Diffstat (limited to 'inftrees.c')
-rw-r--r-- | inftrees.c | 711 |
1 files changed, 289 insertions, 422 deletions
@@ -1,454 +1,321 @@ | |||
1 | /* inftrees.c -- generate Huffman trees for efficient decoding | 1 | /* inftrees.c -- generate Huffman trees for efficient decoding |
2 | * Copyright (C) 1995-2002 Mark Adler | 2 | * Copyright (C) 1995-2003 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 "zutil.h" | 6 | #include "zutil.h" |
7 | #include "inftrees.h" | 7 | #include "inftrees.h" |
8 | 8 | ||
9 | #if !defined(BUILDFIXED) && !defined(STDC) | 9 | #define MAXBITS 15 |
10 | # define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */ | ||
11 | #endif | ||
12 | 10 | ||
13 | const char inflate_copyright[] = | 11 | const char inflate_copyright[] = |
14 | " inflate 1.1.4 Copyright 1995-2002 Mark Adler "; | 12 | " inflate 1.2.0 Copyright 1995-2003 Mark Adler "; |
15 | /* | 13 | /* |
16 | 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 |
17 | 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 |
18 | include such an acknowledgment, I would appreciate that you keep this | 16 | include such an acknowledgment, I would appreciate that you keep this |
19 | copyright string in the executable of your product. | 17 | copyright string in the executable of your product. |
20 | */ | 18 | */ |
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 | 19 | ||
57 | /* | 20 | /* |
58 | Huffman code decoding is performed using a multi-level table lookup. | 21 | Build a set of tables to decode the provided canonical Huffman code. |
59 | 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, |
60 | 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 |
61 | 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 |
62 | is not very long. The most common codes are necessarily the | 25 | to be generated, CODES, LENS, or DISTS. On return, zero is success, |
63 | 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 |
64 | 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 |
65 | 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 |
66 | 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 |
67 | then traded against the time it takes to make longer tables. | 30 | longest code or if it is less than the shortest code. |
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 | */ | 31 | */ |
88 | 32 | int inflate_table(type, lens, codes, table, bits, work) | |
89 | 33 | codetype type; | |
90 | /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ | 34 | unsigned short FAR *lens; |
91 | #define BMAX 15 /* maximum bit length of any code */ | 35 | unsigned codes; |
92 | 36 | code * FAR *table; | |
93 | local int huft_build(b, n, s, d, e, t, m, hp, hn, v) | 37 | unsigned *bits; |
94 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ | 38 | unsigned short FAR *work; |
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), or Z_DATA_ERROR if the input is invalid. */ | ||
108 | { | 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 FAR *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, 73, 194}; | ||
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) return -1; /* no codes! */ | ||
118 | for (min = 1; min <= MAXBITS; min++) | ||
119 | if (count[min] != 0) break; | ||
120 | if (root < min) root = min; | ||
121 | |||
122 | /* check for an over-subscribed or incomplete set of lengths */ | ||
123 | left = 1; | ||
124 | for (len = 1; len <= MAXBITS; len++) { | ||
125 | left <<= 1; | ||
126 | left -= count[len]; | ||
127 | if (left < 0) return -1; /* over-subscribed */ | ||
128 | } | ||
129 | if (left > 0 && (type == CODES || (codes - count[0] != 1))) | ||
130 | return -1; /* incomplete set */ | ||
131 | |||
132 | /* generate offsets into symbol table for each length for sorting */ | ||
133 | offs[1] = 0; | ||
134 | for (len = 1; len < MAXBITS; len++) | ||
135 | offs[len + 1] = offs[len] + count[len]; | ||
136 | |||
137 | /* sort symbols by length, by symbol order within each length */ | ||
138 | for (sym = 0; sym < codes; sym++) | ||
139 | if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; | ||
140 | |||
141 | /* | ||
142 | Create and fill in decoding tables. In this loop, the table being | ||
143 | filled is at next and has curr index bits. The code being used is huff | ||
144 | with length len. That code is converted to an index by dropping drop | ||
145 | bits off of the bottom. For codes where len is less than drop + curr, | ||
146 | those top drop + curr - len bits are incremented through all values to | ||
147 | fill the table with replicated entries. | ||
148 | |||
149 | root is the number of index bits for the root table. When len exceeds | ||
150 | root, sub-tables are created pointed to by the root entry with an index | ||
151 | of the low root bits of huff. This is saved in low to check for when a | ||
152 | new sub-table should be started. drop is zero when the root table is | ||
153 | being filled, and drop is root when sub-tables are being filled. | ||
154 | |||
155 | When a new sub-table is needed, it is necessary to look ahead in the | ||
156 | code lengths to determine what size sub-table is needed. The length | ||
157 | counts are used for this, and so count[] is decremented as codes are | ||
158 | entered in the tables. | ||
159 | |||
160 | used keeps track of how many table entries have been allocated from the | ||
161 | provided *table space. It is checked when a LENS table is being made | ||
162 | against the space in *table, ENOUGH, minus the maximum space needed by | ||
163 | the worst case distance code, MAXD. This should never happen, but the | ||
164 | sufficiency of ENOUGH has not been proven exhaustively, hence the check. | ||
165 | This assumes that when type == LENS, bits == 9. | ||
166 | |||
167 | sym increments through all symbols, and the loop terminates when | ||
168 | all codes of length max, i.e. all codes, have been processed. This | ||
169 | routine permits incomplete codes, so another loop after this one fills | ||
170 | in the rest of the decoding tables with invalid code markers. | ||
171 | */ | ||
172 | |||
173 | /* set up for code type */ | ||
174 | switch (type) { | ||
175 | case CODES: | ||
176 | base = extra = work; /* dummy value--not used */ | ||
177 | end = 19; | ||
178 | break; | ||
179 | case LENS: | ||
180 | base = lbase; | ||
181 | base -= 257; | ||
182 | extra = lext; | ||
183 | extra -= 257; | ||
184 | end = 256; | ||
185 | break; | ||
186 | default: /* DISTS */ | ||
187 | base = dbase; | ||
188 | extra = dext; | ||
189 | end = -1; | ||
190 | } | ||
109 | 191 | ||
110 | uInt a; /* counter for codes of length k */ | 192 | /* initialize state for loop */ |
111 | uInt c[BMAX+1]; /* bit length count table */ | 193 | huff = 0; /* starting code */ |
112 | uInt f; /* i repeats in table every f entries */ | 194 | sym = 0; /* starting code symbol */ |
113 | int g; /* maximum code length */ | 195 | len = min; /* starting code length */ |
114 | int h; /* table level */ | 196 | next = *table; /* current table to fill in */ |
115 | register uInt i; /* counter, current code */ | 197 | curr = root; /* current table index bits */ |
116 | register uInt j; /* counter */ | 198 | drop = 0; /* current bits to drop from code for index */ |
117 | register int k; /* number of bits in current code */ | 199 | low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
118 | int l; /* bits per table (returned in m) */ | 200 | used = 1U << root; /* use root table entries */ |
119 | uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */ | 201 | mask = used - 1; /* mask for comparing low */ |
120 | register uIntf *p; /* pointer into c[], b[], or v[] */ | 202 | |
121 | inflate_huft *q; /* points to current table */ | 203 | /* check available table space */ |
122 | struct inflate_huft_s r; /* table entry for structure assignment */ | 204 | if (type == LENS && used >= ENOUGH - MAXD) |
123 | inflate_huft *u[BMAX]; /* table stack */ | 205 | return 1; |
124 | register int w; /* bits before this table == (l * h) */ | 206 | |
125 | uInt x[BMAX+1]; /* bit offsets, then code stack */ | 207 | /* process all codes and make table entries */ |
126 | uIntf *xp; /* pointer into x */ | 208 | for (;;) { |
127 | int y; /* number of dummy codes added */ | 209 | /* create table entry */ |
128 | uInt z; /* number of entries in current table */ | 210 | this.bits = (unsigned char)(len - drop); |
129 | 211 | if ((int)(work[sym]) < end) { | |
130 | 212 | this.op = (unsigned char)0; | |
131 | /* Generate counts for each bit length */ | 213 | this.val = work[sym]; |
132 | p = c; | ||
133 | #define C0 *p++ = 0; | ||
134 | #define C2 C0 C0 C0 C0 | ||
135 | #define C4 C2 C2 C2 C2 | ||
136 | C4 /* clear c[]--assume BMAX+1 is 16 */ | ||
137 | p = b; i = n; | ||
138 | do { | ||
139 | c[*p++]++; /* assume all entries <= BMAX */ | ||
140 | } while (--i); | ||
141 | if (c[0] == n) /* null input--all zero length codes */ | ||
142 | { | ||
143 | *t = (inflate_huft *)Z_NULL; | ||
144 | *m = 0; | ||
145 | return Z_OK; | ||
146 | } | ||
147 | |||
148 | |||
149 | /* Find minimum and maximum length, bound *m by those */ | ||
150 | l = *m; | ||
151 | for (j = 1; j <= BMAX; j++) | ||
152 | if (c[j]) | ||
153 | break; | ||
154 | k = j; /* minimum code length */ | ||
155 | if ((uInt)l < j) | ||
156 | l = j; | ||
157 | for (i = BMAX; i; i--) | ||
158 | if (c[i]) | ||
159 | break; | ||
160 | g = i; /* maximum code length */ | ||
161 | if ((uInt)l > i) | ||
162 | l = i; | ||
163 | *m = l; | ||
164 | |||
165 | |||
166 | /* Adjust last length count to fill out codes, if needed */ | ||
167 | for (y = 1 << j; j < i; j++, y <<= 1) | ||
168 | if ((y -= c[j]) < 0) | ||
169 | return Z_DATA_ERROR; | ||
170 | if ((y -= c[i]) < 0) | ||
171 | return Z_DATA_ERROR; | ||
172 | c[i] += y; | ||
173 | |||
174 | |||
175 | /* Generate starting offsets into the value table for each length */ | ||
176 | x[1] = j = 0; | ||
177 | p = c + 1; xp = x + 2; | ||
178 | while (--i) { /* note that i == g from above */ | ||
179 | *xp++ = (j += *p++); | ||
180 | } | ||
181 | |||
182 | |||
183 | /* Make a table of values in order of bit lengths */ | ||
184 | p = b; i = 0; | ||
185 | do { | ||
186 | if ((j = *p++) != 0) | ||
187 | v[x[j]++] = i; | ||
188 | } while (++i < n); | ||
189 | n = x[g]; /* set n to length of v */ | ||
190 | |||
191 | |||
192 | /* Generate the Huffman codes and for each, make the table entries */ | ||
193 | x[0] = i = 0; /* first Huffman code is zero */ | ||
194 | p = v; /* grab values in bit order */ | ||
195 | h = -1; /* no tables yet--level -1 */ | ||
196 | w = -l; /* bits decoded == (l * h) */ | ||
197 | u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */ | ||
198 | q = (inflate_huft *)Z_NULL; /* ditto */ | ||
199 | z = 0; /* ditto */ | ||
200 | |||
201 | /* go through the bit lengths (k already is bits in shortest code) */ | ||
202 | for (; k <= g; k++) | ||
203 | { | ||
204 | a = c[k]; | ||
205 | while (a--) | ||
206 | { | ||
207 | /* here i is the Huffman code of length k bits for value *p */ | ||
208 | /* make tables up to required level */ | ||
209 | while (k > w + l) | ||
210 | { | ||
211 | h++; | ||
212 | w += l; /* previous table always l bits */ | ||
213 | |||
214 | /* compute minimum size table less than or equal to l bits */ | ||
215 | z = g - w; | ||
216 | z = z > (uInt)l ? l : z; /* table size upper limit */ | ||
217 | if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | ||
218 | { /* too few codes for k-w bit table */ | ||
219 | f -= a + 1; /* deduct codes from patterns left */ | ||
220 | xp = c + k; | ||
221 | if (j < z) | ||
222 | while (++j < z) /* try smaller tables up to z bits */ | ||
223 | { | ||
224 | if ((f <<= 1) <= *++xp) | ||
225 | break; /* enough codes to use up j bits */ | ||
226 | f -= *xp; /* else deduct codes from patterns */ | ||
227 | } | ||
228 | } | 214 | } |
229 | z = 1 << j; /* table entries for j-bit table */ | 215 | else if ((int)(work[sym]) > end) { |
230 | 216 | this.op = (unsigned char)(extra[work[sym]]); | |
231 | /* allocate new table */ | 217 | this.val = base[work[sym]]; |
232 | if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ | 218 | } |
233 | return Z_DATA_ERROR; /* overflow of MANY */ | 219 | else { |
234 | u[h] = q = hp + *hn; | 220 | this.op = (unsigned char)(32 + 64); /* end of block */ |
235 | *hn += z; | 221 | this.val = 0; |
236 | |||
237 | /* connect to last table, if there is one */ | ||
238 | if (h) | ||
239 | { | ||
240 | x[h] = i; /* save pattern for backing up */ | ||
241 | r.bits = (Byte)l; /* bits to dump before this table */ | ||
242 | r.exop = (Byte)j; /* bits in this table */ | ||
243 | j = i >> (w - l); | ||
244 | r.base = (uInt)(q - u[h-1] - j); /* offset to this table */ | ||
245 | u[h-1][j] = r; /* connect to last table */ | ||
246 | } | 222 | } |
247 | else | ||
248 | *t = q; /* first table is returned result */ | ||
249 | } | ||
250 | |||
251 | /* set up table entry in r */ | ||
252 | r.bits = (Byte)(k - w); | ||
253 | if (p >= v + n) | ||
254 | r.exop = 128 + 64; /* out of values--invalid code */ | ||
255 | else if (*p < s) | ||
256 | { | ||
257 | r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ | ||
258 | r.base = *p++; /* simple code is just the value */ | ||
259 | } | ||
260 | else | ||
261 | { | ||
262 | r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */ | ||
263 | r.base = d[*p++ - s]; | ||
264 | } | ||
265 | |||
266 | /* fill code-like entries with r */ | ||
267 | f = 1 << (k - w); | ||
268 | for (j = i >> w; j < z; j += f) | ||
269 | q[j] = r; | ||
270 | |||
271 | /* backwards increment the k-bit code i */ | ||
272 | for (j = 1 << (k - 1); i & j; j >>= 1) | ||
273 | i ^= j; | ||
274 | i ^= j; | ||
275 | |||
276 | /* backup over finished tables */ | ||
277 | mask = (1 << w) - 1; /* needed on HP, cc -O bug */ | ||
278 | while ((i & mask) != x[h]) | ||
279 | { | ||
280 | h--; /* don't need to update q */ | ||
281 | w -= l; | ||
282 | mask = (1 << w) - 1; | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | |||
287 | 223 | ||
288 | /* Return Z_BUF_ERROR if we were given an incomplete table */ | 224 | /* replicate for those indices with low len bits equal to huff */ |
289 | return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; | 225 | incr = 1U << (len - drop); |
290 | } | 226 | fill = 1U << curr; |
227 | do { | ||
228 | fill -= incr; | ||
229 | next[(huff >> drop) + fill] = this; | ||
230 | } while (fill != 0); | ||
231 | |||
232 | /* backwards increment the len-bit code huff */ | ||
233 | incr = 1U << (len - 1); | ||
234 | while (huff & incr) | ||
235 | incr >>= 1; | ||
236 | if (incr != 0) { | ||
237 | huff &= incr - 1; | ||
238 | huff += incr; | ||
239 | } | ||
240 | else | ||
241 | huff = 0; | ||
291 | 242 | ||
243 | /* go to next symbol, update count, len */ | ||
244 | sym++; | ||
245 | if (--(count[len]) == 0) { | ||
246 | if (len == max) break; | ||
247 | len = lens[work[sym]]; | ||
248 | } | ||
292 | 249 | ||
293 | int inflate_trees_bits(c, bb, tb, hp, z) | 250 | /* create new sub-table if needed */ |
294 | uIntf *c; /* 19 code lengths */ | 251 | if (len > root && (huff & mask) != low) { |
295 | uIntf *bb; /* bits tree desired/actual depth */ | 252 | /* if first time, transition to sub-tables */ |
296 | inflate_huft * FAR *tb; /* bits tree result */ | 253 | if (drop == 0) |
297 | inflate_huft *hp; /* space for trees */ | 254 | drop = root; |
298 | z_streamp z; /* for messages */ | 255 | |
299 | { | 256 | /* increment past last table */ |
300 | int r; | 257 | next += 1U << curr; |
301 | uInt hn = 0; /* hufts used in space */ | 258 | |
302 | uIntf *v; /* work area for huft_build */ | 259 | /* determine length of next table */ |
303 | 260 | curr = len - drop; | |
304 | if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL) | 261 | left = (int)(1 << curr); |
305 | return Z_MEM_ERROR; | 262 | while (curr + drop < max) { |
306 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, | 263 | left -= count[curr + drop]; |
307 | tb, bb, hp, &hn, v); | 264 | if (left <= 0) break; |
308 | if (r == Z_DATA_ERROR) | 265 | curr++; |
309 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; | 266 | left <<= 1; |
310 | else if (r == Z_BUF_ERROR || *bb == 0) | 267 | } |
311 | { | ||
312 | z->msg = (char*)"incomplete dynamic bit lengths tree"; | ||
313 | r = Z_DATA_ERROR; | ||
314 | } | ||
315 | ZFREE(z, v); | ||
316 | return r; | ||
317 | } | ||
318 | 268 | ||
269 | /* check for enough space */ | ||
270 | used += 1U << curr; | ||
271 | if (type == LENS && used >= ENOUGH - MAXD) | ||
272 | return 1; | ||
319 | 273 | ||
320 | int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z) | 274 | /* point entry in root table to sub-table */ |
321 | uInt nl; /* number of literal/length codes */ | 275 | low = huff & mask; |
322 | uInt nd; /* number of distance codes */ | 276 | (*table)[low].op = (unsigned char)curr; |
323 | uIntf *c; /* that many (total) code lengths */ | 277 | (*table)[low].bits = (unsigned char)root; |
324 | uIntf *bl; /* literal desired/actual bit depth */ | 278 | (*table)[low].val = (unsigned short)(next - *table); |
325 | uIntf *bd; /* distance desired/actual bit depth */ | 279 | } |
326 | inflate_huft * FAR *tl; /* literal/length tree result */ | ||
327 | inflate_huft * FAR *td; /* distance tree result */ | ||
328 | inflate_huft *hp; /* space for trees */ | ||
329 | z_streamp z; /* for messages */ | ||
330 | { | ||
331 | int r; | ||
332 | uInt hn = 0; /* hufts used in space */ | ||
333 | uIntf *v; /* work area for huft_build */ | ||
334 | |||
335 | /* allocate work area */ | ||
336 | if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) | ||
337 | return Z_MEM_ERROR; | ||
338 | |||
339 | /* build literal/length tree */ | ||
340 | r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v); | ||
341 | if (r != Z_OK || *bl == 0) | ||
342 | { | ||
343 | if (r == Z_DATA_ERROR) | ||
344 | z->msg = (char*)"oversubscribed literal/length tree"; | ||
345 | else if (r != Z_MEM_ERROR) | ||
346 | { | ||
347 | z->msg = (char*)"incomplete literal/length tree"; | ||
348 | r = Z_DATA_ERROR; | ||
349 | } | ||
350 | ZFREE(z, v); | ||
351 | return r; | ||
352 | } | ||
353 | |||
354 | /* build distance tree */ | ||
355 | r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v); | ||
356 | if (r != Z_OK || (*bd == 0 && nl > 257)) | ||
357 | { | ||
358 | if (r == Z_DATA_ERROR) | ||
359 | z->msg = (char*)"oversubscribed distance tree"; | ||
360 | else if (r == Z_BUF_ERROR) { | ||
361 | #ifdef PKZIP_BUG_WORKAROUND | ||
362 | r = Z_OK; | ||
363 | } | ||
364 | #else | ||
365 | z->msg = (char*)"incomplete distance tree"; | ||
366 | r = Z_DATA_ERROR; | ||
367 | } | ||
368 | else if (r != Z_MEM_ERROR) | ||
369 | { | ||
370 | z->msg = (char*)"empty distance tree with lengths"; | ||
371 | r = Z_DATA_ERROR; | ||
372 | } | 280 | } |
373 | ZFREE(z, v); | ||
374 | return r; | ||
375 | #endif | ||
376 | } | ||
377 | |||
378 | /* done */ | ||
379 | ZFREE(z, v); | ||
380 | return Z_OK; | ||
381 | } | ||
382 | 281 | ||
282 | /* | ||
283 | Fill in rest of table for incomplete codes. This loop is similar to the | ||
284 | loop above in incrementing huff for table indices. It is assumed that | ||
285 | len is equal to curr + drop, so there is no loop needed to increment | ||
286 | through high index bits. When the current sub-table is filled, the loop | ||
287 | drops back to the root table to fill in any remaining entries there. | ||
288 | */ | ||
289 | this.op = (unsigned char)64; /* invalid code marker */ | ||
290 | this.bits = (unsigned char)(len - drop); | ||
291 | this.val = (unsigned short)0; | ||
292 | while (huff != 0) { | ||
293 | /* when done with sub-table, drop back to root table */ | ||
294 | if (drop != 0 && (huff & mask) != low) { | ||
295 | drop = 0; | ||
296 | len = root; | ||
297 | next = *table; | ||
298 | curr = root; | ||
299 | this.bits = (unsigned char)len; | ||
300 | } | ||
301 | |||
302 | /* put invalid code marker in table */ | ||
303 | next[huff >> drop] = this; | ||
383 | 304 | ||
384 | /* build fixed tables only once--keep them here */ | 305 | /* backwards increment the len-bit code huff */ |
385 | #ifdef BUILDFIXED | 306 | incr = 1U << (len - 1); |
386 | local int fixed_built = 0; | 307 | while (huff & incr) |
387 | #define FIXEDH 544 /* number of hufts used by fixed tables */ | 308 | incr >>= 1; |
388 | local inflate_huft fixed_mem[FIXEDH]; | 309 | if (incr != 0) { |
389 | local uInt fixed_bl; | 310 | huff &= incr - 1; |
390 | local uInt fixed_bd; | 311 | huff += incr; |
391 | local inflate_huft *fixed_tl; | 312 | } |
392 | local inflate_huft *fixed_td; | 313 | else |
393 | #else | 314 | huff = 0; |
394 | #include "inffixed.h" | ||
395 | #endif | ||
396 | |||
397 | |||
398 | int inflate_trees_fixed(bl, bd, tl, td, z) | ||
399 | uIntf *bl; /* literal desired/actual bit depth */ | ||
400 | uIntf *bd; /* distance desired/actual bit depth */ | ||
401 | inflate_huft * FAR *tl; /* literal/length tree result */ | ||
402 | inflate_huft * FAR *td; /* distance tree result */ | ||
403 | z_streamp z; /* for memory allocation */ | ||
404 | { | ||
405 | #ifdef BUILDFIXED | ||
406 | /* build fixed tables if not already */ | ||
407 | if (!fixed_built) | ||
408 | { | ||
409 | int k; /* temporary variable */ | ||
410 | uInt f = 0; /* number of hufts used in fixed_mem */ | ||
411 | uIntf *c; /* length list for huft_build */ | ||
412 | uIntf *v; /* work area for huft_build */ | ||
413 | |||
414 | /* allocate memory */ | ||
415 | if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) | ||
416 | return Z_MEM_ERROR; | ||
417 | if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) | ||
418 | { | ||
419 | ZFREE(z, c); | ||
420 | return Z_MEM_ERROR; | ||
421 | } | 315 | } |
422 | 316 | ||
423 | /* literal table */ | 317 | /* set return parameters */ |
424 | for (k = 0; k < 144; k++) | 318 | *table += used; |
425 | c[k] = 8; | 319 | *bits = root; |
426 | for (; k < 256; k++) | 320 | return 0; |
427 | c[k] = 9; | ||
428 | for (; k < 280; k++) | ||
429 | c[k] = 7; | ||
430 | for (; k < 288; k++) | ||
431 | c[k] = 8; | ||
432 | fixed_bl = 9; | ||
433 | huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, | ||
434 | fixed_mem, &f, v); | ||
435 | |||
436 | /* distance table */ | ||
437 | for (k = 0; k < 30; k++) | ||
438 | c[k] = 5; | ||
439 | fixed_bd = 5; | ||
440 | huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, | ||
441 | fixed_mem, &f, v); | ||
442 | |||
443 | /* done */ | ||
444 | ZFREE(z, v); | ||
445 | ZFREE(z, c); | ||
446 | fixed_built = 1; | ||
447 | } | ||
448 | #endif | ||
449 | *bl = fixed_bl; | ||
450 | *bd = fixed_bd; | ||
451 | *tl = fixed_tl; | ||
452 | *td = fixed_td; | ||
453 | return Z_OK; | ||
454 | } | 321 | } |