|
7ef7284…
|
drh
|
1 |
/* inftrees.c -- generate Huffman trees for efficient decoding |
|
6ea30fb…
|
florian
|
2 |
* Copyright (C) 1995-2026 Mark Adler |
|
7ef7284…
|
drh
|
3 |
* For conditions of distribution and use, see copyright notice in zlib.h |
|
7ef7284…
|
drh
|
4 |
*/ |
|
7ef7284…
|
drh
|
5 |
|
|
6ea30fb…
|
florian
|
6 |
#ifdef MAKEFIXED |
|
6ea30fb…
|
florian
|
7 |
# ifndef BUILDFIXED |
|
6ea30fb…
|
florian
|
8 |
# define BUILDFIXED |
|
6ea30fb…
|
florian
|
9 |
# endif |
|
6ea30fb…
|
florian
|
10 |
#endif |
|
6ea30fb…
|
florian
|
11 |
#ifdef BUILDFIXED |
|
6ea30fb…
|
florian
|
12 |
# define Z_ONCE |
|
6ea30fb…
|
florian
|
13 |
#endif |
|
6ea30fb…
|
florian
|
14 |
|
|
7ef7284…
|
drh
|
15 |
#include "zutil.h" |
|
7ef7284…
|
drh
|
16 |
#include "inftrees.h" |
|
6ea30fb…
|
florian
|
17 |
#include "inflate.h" |
|
6ea30fb…
|
florian
|
18 |
|
|
6ea30fb…
|
florian
|
19 |
#ifndef NULL |
|
6ea30fb…
|
florian
|
20 |
# define NULL 0 |
|
6ea30fb…
|
florian
|
21 |
#endif |
|
7ef7284…
|
drh
|
22 |
|
|
7ef7284…
|
drh
|
23 |
#define MAXBITS 15 |
|
7ef7284…
|
drh
|
24 |
|
|
7ef7284…
|
drh
|
25 |
const char inflate_copyright[] = |
|
6ea30fb…
|
florian
|
26 |
" inflate 1.3.2 Copyright 1995-2026 Mark Adler "; |
|
7ef7284…
|
drh
|
27 |
/* |
|
7ef7284…
|
drh
|
28 |
If you use the zlib library in a product, an acknowledgment is welcome |
|
7ef7284…
|
drh
|
29 |
in the documentation of your product. If for some reason you cannot |
|
7ef7284…
|
drh
|
30 |
include such an acknowledgment, I would appreciate that you keep this |
|
7ef7284…
|
drh
|
31 |
copyright string in the executable of your product. |
|
7ef7284…
|
drh
|
32 |
*/ |
|
7ef7284…
|
drh
|
33 |
|
|
7ef7284…
|
drh
|
34 |
/* |
|
7ef7284…
|
drh
|
35 |
Build a set of tables to decode the provided canonical Huffman code. |
|
7ef7284…
|
drh
|
36 |
The code lengths are lens[0..codes-1]. The result starts at *table, |
|
7ef7284…
|
drh
|
37 |
whose indices are 0..2^bits-1. work is a writable array of at least |
|
7ef7284…
|
drh
|
38 |
lens shorts, which is used as a work area. type is the type of code |
|
7ef7284…
|
drh
|
39 |
to be generated, CODES, LENS, or DISTS. On return, zero is success, |
|
7ef7284…
|
drh
|
40 |
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table |
|
7ef7284…
|
drh
|
41 |
on return points to the next available entry's address. bits is the |
|
7ef7284…
|
drh
|
42 |
requested root table index bits, and on return it is the actual root |
|
7ef7284…
|
drh
|
43 |
table index bits. It will differ if the request is greater than the |
|
7ef7284…
|
drh
|
44 |
longest code or if it is less than the shortest code. |
|
7ef7284…
|
drh
|
45 |
*/ |
|
f1f1d6c…
|
drh
|
46 |
int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, |
|
f1f1d6c…
|
drh
|
47 |
unsigned codes, code FAR * FAR *table, |
|
f1f1d6c…
|
drh
|
48 |
unsigned FAR *bits, unsigned short FAR *work) { |
|
7ef7284…
|
drh
|
49 |
unsigned len; /* a code's length in bits */ |
|
7ef7284…
|
drh
|
50 |
unsigned sym; /* index of code symbols */ |
|
7ef7284…
|
drh
|
51 |
unsigned min, max; /* minimum and maximum code lengths */ |
|
7ef7284…
|
drh
|
52 |
unsigned root; /* number of index bits for root table */ |
|
7ef7284…
|
drh
|
53 |
unsigned curr; /* number of index bits for current table */ |
|
7ef7284…
|
drh
|
54 |
unsigned drop; /* code bits to drop for sub-table */ |
|
7ef7284…
|
drh
|
55 |
int left; /* number of prefix codes available */ |
|
7ef7284…
|
drh
|
56 |
unsigned used; /* code entries in table used */ |
|
7ef7284…
|
drh
|
57 |
unsigned huff; /* Huffman code */ |
|
7ef7284…
|
drh
|
58 |
unsigned incr; /* for incrementing code, index */ |
|
7ef7284…
|
drh
|
59 |
unsigned fill; /* index for replicating entries */ |
|
7ef7284…
|
drh
|
60 |
unsigned low; /* low bits for current root entry */ |
|
7ef7284…
|
drh
|
61 |
unsigned mask; /* mask for low root bits */ |
|
7ef7284…
|
drh
|
62 |
code here; /* table entry for duplication */ |
|
7ef7284…
|
drh
|
63 |
code FAR *next; /* next available space in table */ |
|
6ea30fb…
|
florian
|
64 |
const unsigned short FAR *base = NULL; /* base value table to use */ |
|
6ea30fb…
|
florian
|
65 |
const unsigned short FAR *extra = NULL; /* extra bits table to use */ |
|
6ea30fb…
|
florian
|
66 |
unsigned match = 0; /* use base and extra for symbol >= match */ |
|
7ef7284…
|
drh
|
67 |
unsigned short count[MAXBITS+1]; /* number of codes of each length */ |
|
7ef7284…
|
drh
|
68 |
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ |
|
7ef7284…
|
drh
|
69 |
static const unsigned short lbase[31] = { /* Length codes 257..285 base */ |
|
7ef7284…
|
drh
|
70 |
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
|
7ef7284…
|
drh
|
71 |
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
|
7ef7284…
|
drh
|
72 |
static const unsigned short lext[31] = { /* Length codes 257..285 extra */ |
|
7ef7284…
|
drh
|
73 |
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, |
|
6ea30fb…
|
florian
|
74 |
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 199, 75}; |
|
7ef7284…
|
drh
|
75 |
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ |
|
7ef7284…
|
drh
|
76 |
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
|
7ef7284…
|
drh
|
77 |
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
|
7ef7284…
|
drh
|
78 |
8193, 12289, 16385, 24577, 0, 0}; |
|
7ef7284…
|
drh
|
79 |
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ |
|
7ef7284…
|
drh
|
80 |
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, |
|
7ef7284…
|
drh
|
81 |
23, 23, 24, 24, 25, 25, 26, 26, 27, 27, |
|
7ef7284…
|
drh
|
82 |
28, 28, 29, 29, 64, 64}; |
|
7ef7284…
|
drh
|
83 |
|
|
7ef7284…
|
drh
|
84 |
/* |
|
7ef7284…
|
drh
|
85 |
Process a set of code lengths to create a canonical Huffman code. The |
|
7ef7284…
|
drh
|
86 |
code lengths are lens[0..codes-1]. Each length corresponds to the |
|
7ef7284…
|
drh
|
87 |
symbols 0..codes-1. The Huffman code is generated by first sorting the |
|
7ef7284…
|
drh
|
88 |
symbols by length from short to long, and retaining the symbol order |
|
7ef7284…
|
drh
|
89 |
for codes with equal lengths. Then the code starts with all zero bits |
|
7ef7284…
|
drh
|
90 |
for the first code of the shortest length, and the codes are integer |
|
7ef7284…
|
drh
|
91 |
increments for the same length, and zeros are appended as the length |
|
7ef7284…
|
drh
|
92 |
increases. For the deflate format, these bits are stored backwards |
|
7ef7284…
|
drh
|
93 |
from their more natural integer increment ordering, and so when the |
|
7ef7284…
|
drh
|
94 |
decoding tables are built in the large loop below, the integer codes |
|
7ef7284…
|
drh
|
95 |
are incremented backwards. |
|
7ef7284…
|
drh
|
96 |
|
|
7ef7284…
|
drh
|
97 |
This routine assumes, but does not check, that all of the entries in |
|
7ef7284…
|
drh
|
98 |
lens[] are in the range 0..MAXBITS. The caller must assure this. |
|
7ef7284…
|
drh
|
99 |
1..MAXBITS is interpreted as that code length. zero means that that |
|
7ef7284…
|
drh
|
100 |
symbol does not occur in this code. |
|
7ef7284…
|
drh
|
101 |
|
|
7ef7284…
|
drh
|
102 |
The codes are sorted by computing a count of codes for each length, |
|
7ef7284…
|
drh
|
103 |
creating from that a table of starting indices for each length in the |
|
7ef7284…
|
drh
|
104 |
sorted table, and then entering the symbols in order in the sorted |
|
7ef7284…
|
drh
|
105 |
table. The sorted table is work[], with that space being provided by |
|
7ef7284…
|
drh
|
106 |
the caller. |
|
7ef7284…
|
drh
|
107 |
|
|
7ef7284…
|
drh
|
108 |
The length counts are used for other purposes as well, i.e. finding |
|
7ef7284…
|
drh
|
109 |
the minimum and maximum length codes, determining if there are any |
|
7ef7284…
|
drh
|
110 |
codes at all, checking for a valid set of lengths, and looking ahead |
|
7ef7284…
|
drh
|
111 |
at length counts to determine sub-table sizes when building the |
|
7ef7284…
|
drh
|
112 |
decoding tables. |
|
7ef7284…
|
drh
|
113 |
*/ |
|
7ef7284…
|
drh
|
114 |
|
|
7ef7284…
|
drh
|
115 |
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ |
|
7ef7284…
|
drh
|
116 |
for (len = 0; len <= MAXBITS; len++) |
|
7ef7284…
|
drh
|
117 |
count[len] = 0; |
|
7ef7284…
|
drh
|
118 |
for (sym = 0; sym < codes; sym++) |
|
7ef7284…
|
drh
|
119 |
count[lens[sym]]++; |
|
7ef7284…
|
drh
|
120 |
|
|
7ef7284…
|
drh
|
121 |
/* bound code lengths, force root to be within code lengths */ |
|
7ef7284…
|
drh
|
122 |
root = *bits; |
|
7ef7284…
|
drh
|
123 |
for (max = MAXBITS; max >= 1; max--) |
|
7ef7284…
|
drh
|
124 |
if (count[max] != 0) break; |
|
7ef7284…
|
drh
|
125 |
if (root > max) root = max; |
|
7ef7284…
|
drh
|
126 |
if (max == 0) { /* no symbols to code at all */ |
|
7ef7284…
|
drh
|
127 |
here.op = (unsigned char)64; /* invalid code marker */ |
|
7ef7284…
|
drh
|
128 |
here.bits = (unsigned char)1; |
|
7ef7284…
|
drh
|
129 |
here.val = (unsigned short)0; |
|
7ef7284…
|
drh
|
130 |
*(*table)++ = here; /* make a table to force an error */ |
|
7ef7284…
|
drh
|
131 |
*(*table)++ = here; |
|
7ef7284…
|
drh
|
132 |
*bits = 1; |
|
7ef7284…
|
drh
|
133 |
return 0; /* no symbols, but wait for decoding to report error */ |
|
7ef7284…
|
drh
|
134 |
} |
|
7ef7284…
|
drh
|
135 |
for (min = 1; min < max; min++) |
|
7ef7284…
|
drh
|
136 |
if (count[min] != 0) break; |
|
7ef7284…
|
drh
|
137 |
if (root < min) root = min; |
|
7ef7284…
|
drh
|
138 |
|
|
7ef7284…
|
drh
|
139 |
/* check for an over-subscribed or incomplete set of lengths */ |
|
7ef7284…
|
drh
|
140 |
left = 1; |
|
7ef7284…
|
drh
|
141 |
for (len = 1; len <= MAXBITS; len++) { |
|
7ef7284…
|
drh
|
142 |
left <<= 1; |
|
7ef7284…
|
drh
|
143 |
left -= count[len]; |
|
7ef7284…
|
drh
|
144 |
if (left < 0) return -1; /* over-subscribed */ |
|
7ef7284…
|
drh
|
145 |
} |
|
7ef7284…
|
drh
|
146 |
if (left > 0 && (type == CODES || max != 1)) |
|
7ef7284…
|
drh
|
147 |
return -1; /* incomplete set */ |
|
7ef7284…
|
drh
|
148 |
|
|
7ef7284…
|
drh
|
149 |
/* generate offsets into symbol table for each length for sorting */ |
|
7ef7284…
|
drh
|
150 |
offs[1] = 0; |
|
7ef7284…
|
drh
|
151 |
for (len = 1; len < MAXBITS; len++) |
|
7ef7284…
|
drh
|
152 |
offs[len + 1] = offs[len] + count[len]; |
|
7ef7284…
|
drh
|
153 |
|
|
7ef7284…
|
drh
|
154 |
/* sort symbols by length, by symbol order within each length */ |
|
7ef7284…
|
drh
|
155 |
for (sym = 0; sym < codes; sym++) |
|
7ef7284…
|
drh
|
156 |
if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; |
|
7ef7284…
|
drh
|
157 |
|
|
7ef7284…
|
drh
|
158 |
/* |
|
7ef7284…
|
drh
|
159 |
Create and fill in decoding tables. In this loop, the table being |
|
7ef7284…
|
drh
|
160 |
filled is at next and has curr index bits. The code being used is huff |
|
7ef7284…
|
drh
|
161 |
with length len. That code is converted to an index by dropping drop |
|
7ef7284…
|
drh
|
162 |
bits off of the bottom. For codes where len is less than drop + curr, |
|
7ef7284…
|
drh
|
163 |
those top drop + curr - len bits are incremented through all values to |
|
7ef7284…
|
drh
|
164 |
fill the table with replicated entries. |
|
7ef7284…
|
drh
|
165 |
|
|
7ef7284…
|
drh
|
166 |
root is the number of index bits for the root table. When len exceeds |
|
7ef7284…
|
drh
|
167 |
root, sub-tables are created pointed to by the root entry with an index |
|
7ef7284…
|
drh
|
168 |
of the low root bits of huff. This is saved in low to check for when a |
|
7ef7284…
|
drh
|
169 |
new sub-table should be started. drop is zero when the root table is |
|
7ef7284…
|
drh
|
170 |
being filled, and drop is root when sub-tables are being filled. |
|
7ef7284…
|
drh
|
171 |
|
|
7ef7284…
|
drh
|
172 |
When a new sub-table is needed, it is necessary to look ahead in the |
|
7ef7284…
|
drh
|
173 |
code lengths to determine what size sub-table is needed. The length |
|
7ef7284…
|
drh
|
174 |
counts are used for this, and so count[] is decremented as codes are |
|
7ef7284…
|
drh
|
175 |
entered in the tables. |
|
7ef7284…
|
drh
|
176 |
|
|
7ef7284…
|
drh
|
177 |
used keeps track of how many table entries have been allocated from the |
|
7ef7284…
|
drh
|
178 |
provided *table space. It is checked for LENS and DIST tables against |
|
7ef7284…
|
drh
|
179 |
the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in |
|
7ef7284…
|
drh
|
180 |
the initial root table size constants. See the comments in inftrees.h |
|
7ef7284…
|
drh
|
181 |
for more information. |
|
7ef7284…
|
drh
|
182 |
|
|
7ef7284…
|
drh
|
183 |
sym increments through all symbols, and the loop terminates when |
|
7ef7284…
|
drh
|
184 |
all codes of length max, i.e. all codes, have been processed. This |
|
7ef7284…
|
drh
|
185 |
routine permits incomplete codes, so another loop after this one fills |
|
7ef7284…
|
drh
|
186 |
in the rest of the decoding tables with invalid code markers. |
|
7ef7284…
|
drh
|
187 |
*/ |
|
7ef7284…
|
drh
|
188 |
|
|
7ef7284…
|
drh
|
189 |
/* set up for code type */ |
|
7ef7284…
|
drh
|
190 |
switch (type) { |
|
7ef7284…
|
drh
|
191 |
case CODES: |
|
e38d5e1…
|
jan.nijtmans
|
192 |
match = 20; |
|
7ef7284…
|
drh
|
193 |
break; |
|
7ef7284…
|
drh
|
194 |
case LENS: |
|
7ef7284…
|
drh
|
195 |
base = lbase; |
|
7ef7284…
|
drh
|
196 |
extra = lext; |
|
e38d5e1…
|
jan.nijtmans
|
197 |
match = 257; |
|
7ef7284…
|
drh
|
198 |
break; |
|
6ea30fb…
|
florian
|
199 |
case DISTS: |
|
7ef7284…
|
drh
|
200 |
base = dbase; |
|
7ef7284…
|
drh
|
201 |
extra = dext; |
|
7ef7284…
|
drh
|
202 |
} |
|
7ef7284…
|
drh
|
203 |
|
|
7ef7284…
|
drh
|
204 |
/* initialize state for loop */ |
|
7ef7284…
|
drh
|
205 |
huff = 0; /* starting code */ |
|
7ef7284…
|
drh
|
206 |
sym = 0; /* starting code symbol */ |
|
7ef7284…
|
drh
|
207 |
len = min; /* starting code length */ |
|
7ef7284…
|
drh
|
208 |
next = *table; /* current table to fill in */ |
|
7ef7284…
|
drh
|
209 |
curr = root; /* current table index bits */ |
|
7ef7284…
|
drh
|
210 |
drop = 0; /* current bits to drop from code for index */ |
|
7ef7284…
|
drh
|
211 |
low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
|
7ef7284…
|
drh
|
212 |
used = 1U << root; /* use root table entries */ |
|
7ef7284…
|
drh
|
213 |
mask = used - 1; /* mask for comparing low */ |
|
7ef7284…
|
drh
|
214 |
|
|
7ef7284…
|
drh
|
215 |
/* check available table space */ |
|
bb4776e…
|
jan.nijtmans
|
216 |
if ((type == LENS && used > ENOUGH_LENS) || |
|
bb4776e…
|
jan.nijtmans
|
217 |
(type == DISTS && used > ENOUGH_DISTS)) |
|
7ef7284…
|
drh
|
218 |
return 1; |
|
7ef7284…
|
drh
|
219 |
|
|
7ef7284…
|
drh
|
220 |
/* process all codes and make table entries */ |
|
7ef7284…
|
drh
|
221 |
for (;;) { |
|
7ef7284…
|
drh
|
222 |
/* create table entry */ |
|
7ef7284…
|
drh
|
223 |
here.bits = (unsigned char)(len - drop); |
|
e38d5e1…
|
jan.nijtmans
|
224 |
if (work[sym] + 1U < match) { |
|
7ef7284…
|
drh
|
225 |
here.op = (unsigned char)0; |
|
7ef7284…
|
drh
|
226 |
here.val = work[sym]; |
|
7ef7284…
|
drh
|
227 |
} |
|
e38d5e1…
|
jan.nijtmans
|
228 |
else if (work[sym] >= match) { |
|
e38d5e1…
|
jan.nijtmans
|
229 |
here.op = (unsigned char)(extra[work[sym] - match]); |
|
e38d5e1…
|
jan.nijtmans
|
230 |
here.val = base[work[sym] - match]; |
|
7ef7284…
|
drh
|
231 |
} |
|
7ef7284…
|
drh
|
232 |
else { |
|
7ef7284…
|
drh
|
233 |
here.op = (unsigned char)(32 + 64); /* end of block */ |
|
7ef7284…
|
drh
|
234 |
here.val = 0; |
|
7ef7284…
|
drh
|
235 |
} |
|
7ef7284…
|
drh
|
236 |
|
|
7ef7284…
|
drh
|
237 |
/* replicate for those indices with low len bits equal to huff */ |
|
7ef7284…
|
drh
|
238 |
incr = 1U << (len - drop); |
|
7ef7284…
|
drh
|
239 |
fill = 1U << curr; |
|
7ef7284…
|
drh
|
240 |
min = fill; /* save offset to next table */ |
|
7ef7284…
|
drh
|
241 |
do { |
|
7ef7284…
|
drh
|
242 |
fill -= incr; |
|
7ef7284…
|
drh
|
243 |
next[(huff >> drop) + fill] = here; |
|
7ef7284…
|
drh
|
244 |
} while (fill != 0); |
|
7ef7284…
|
drh
|
245 |
|
|
7ef7284…
|
drh
|
246 |
/* backwards increment the len-bit code huff */ |
|
7ef7284…
|
drh
|
247 |
incr = 1U << (len - 1); |
|
7ef7284…
|
drh
|
248 |
while (huff & incr) |
|
7ef7284…
|
drh
|
249 |
incr >>= 1; |
|
7ef7284…
|
drh
|
250 |
if (incr != 0) { |
|
7ef7284…
|
drh
|
251 |
huff &= incr - 1; |
|
7ef7284…
|
drh
|
252 |
huff += incr; |
|
7ef7284…
|
drh
|
253 |
} |
|
7ef7284…
|
drh
|
254 |
else |
|
7ef7284…
|
drh
|
255 |
huff = 0; |
|
7ef7284…
|
drh
|
256 |
|
|
7ef7284…
|
drh
|
257 |
/* go to next symbol, update count, len */ |
|
7ef7284…
|
drh
|
258 |
sym++; |
|
7ef7284…
|
drh
|
259 |
if (--(count[len]) == 0) { |
|
7ef7284…
|
drh
|
260 |
if (len == max) break; |
|
7ef7284…
|
drh
|
261 |
len = lens[work[sym]]; |
|
7ef7284…
|
drh
|
262 |
} |
|
7ef7284…
|
drh
|
263 |
|
|
7ef7284…
|
drh
|
264 |
/* create new sub-table if needed */ |
|
7ef7284…
|
drh
|
265 |
if (len > root && (huff & mask) != low) { |
|
7ef7284…
|
drh
|
266 |
/* if first time, transition to sub-tables */ |
|
7ef7284…
|
drh
|
267 |
if (drop == 0) |
|
7ef7284…
|
drh
|
268 |
drop = root; |
|
7ef7284…
|
drh
|
269 |
|
|
7ef7284…
|
drh
|
270 |
/* increment past last table */ |
|
7ef7284…
|
drh
|
271 |
next += min; /* here min is 1 << curr */ |
|
7ef7284…
|
drh
|
272 |
|
|
7ef7284…
|
drh
|
273 |
/* determine length of next table */ |
|
7ef7284…
|
drh
|
274 |
curr = len - drop; |
|
7ef7284…
|
drh
|
275 |
left = (int)(1 << curr); |
|
7ef7284…
|
drh
|
276 |
while (curr + drop < max) { |
|
7ef7284…
|
drh
|
277 |
left -= count[curr + drop]; |
|
7ef7284…
|
drh
|
278 |
if (left <= 0) break; |
|
7ef7284…
|
drh
|
279 |
curr++; |
|
7ef7284…
|
drh
|
280 |
left <<= 1; |
|
7ef7284…
|
drh
|
281 |
} |
|
7ef7284…
|
drh
|
282 |
|
|
7ef7284…
|
drh
|
283 |
/* check for enough space */ |
|
7ef7284…
|
drh
|
284 |
used += 1U << curr; |
|
bb4776e…
|
jan.nijtmans
|
285 |
if ((type == LENS && used > ENOUGH_LENS) || |
|
bb4776e…
|
jan.nijtmans
|
286 |
(type == DISTS && used > ENOUGH_DISTS)) |
|
7ef7284…
|
drh
|
287 |
return 1; |
|
7ef7284…
|
drh
|
288 |
|
|
7ef7284…
|
drh
|
289 |
/* point entry in root table to sub-table */ |
|
7ef7284…
|
drh
|
290 |
low = huff & mask; |
|
7ef7284…
|
drh
|
291 |
(*table)[low].op = (unsigned char)curr; |
|
7ef7284…
|
drh
|
292 |
(*table)[low].bits = (unsigned char)root; |
|
7ef7284…
|
drh
|
293 |
(*table)[low].val = (unsigned short)(next - *table); |
|
7ef7284…
|
drh
|
294 |
} |
|
7ef7284…
|
drh
|
295 |
} |
|
7ef7284…
|
drh
|
296 |
|
|
7ef7284…
|
drh
|
297 |
/* fill in remaining table entry if code is incomplete (guaranteed to have |
|
7ef7284…
|
drh
|
298 |
at most one remaining entry, since if the code is incomplete, the |
|
7ef7284…
|
drh
|
299 |
maximum code length that was allowed to get this far is one bit) */ |
|
7ef7284…
|
drh
|
300 |
if (huff != 0) { |
|
7ef7284…
|
drh
|
301 |
here.op = (unsigned char)64; /* invalid code marker */ |
|
7ef7284…
|
drh
|
302 |
here.bits = (unsigned char)(len - drop); |
|
7ef7284…
|
drh
|
303 |
here.val = (unsigned short)0; |
|
7ef7284…
|
drh
|
304 |
next[huff] = here; |
|
7ef7284…
|
drh
|
305 |
} |
|
7ef7284…
|
drh
|
306 |
|
|
7ef7284…
|
drh
|
307 |
/* set return parameters */ |
|
7ef7284…
|
drh
|
308 |
*table += used; |
|
7ef7284…
|
drh
|
309 |
*bits = root; |
|
7ef7284…
|
drh
|
310 |
return 0; |
|
7ef7284…
|
drh
|
311 |
} |
|
6ea30fb…
|
florian
|
312 |
|
|
6ea30fb…
|
florian
|
313 |
#ifdef BUILDFIXED |
|
6ea30fb…
|
florian
|
314 |
/* |
|
6ea30fb…
|
florian
|
315 |
If this is compiled with BUILDFIXED defined, and if inflate will be used in |
|
6ea30fb…
|
florian
|
316 |
multiple threads, and if atomics are not available, then inflate() must be |
|
6ea30fb…
|
florian
|
317 |
called with a fixed block (e.g. 0x03 0x00) to initialize the tables and must |
|
6ea30fb…
|
florian
|
318 |
return before any other threads are allowed to call inflate. |
|
6ea30fb…
|
florian
|
319 |
*/ |
|
6ea30fb…
|
florian
|
320 |
|
|
6ea30fb…
|
florian
|
321 |
static code *lenfix, *distfix; |
|
6ea30fb…
|
florian
|
322 |
static code fixed[544]; |
|
6ea30fb…
|
florian
|
323 |
|
|
6ea30fb…
|
florian
|
324 |
/* State for z_once(). */ |
|
6ea30fb…
|
florian
|
325 |
local z_once_t built = Z_ONCE_INIT; |
|
6ea30fb…
|
florian
|
326 |
|
|
6ea30fb…
|
florian
|
327 |
local void buildtables(void) { |
|
6ea30fb…
|
florian
|
328 |
unsigned sym, bits; |
|
6ea30fb…
|
florian
|
329 |
static code *next; |
|
6ea30fb…
|
florian
|
330 |
unsigned short lens[288], work[288]; |
|
6ea30fb…
|
florian
|
331 |
|
|
6ea30fb…
|
florian
|
332 |
/* literal/length table */ |
|
6ea30fb…
|
florian
|
333 |
sym = 0; |
|
6ea30fb…
|
florian
|
334 |
while (sym < 144) lens[sym++] = 8; |
|
6ea30fb…
|
florian
|
335 |
while (sym < 256) lens[sym++] = 9; |
|
6ea30fb…
|
florian
|
336 |
while (sym < 280) lens[sym++] = 7; |
|
6ea30fb…
|
florian
|
337 |
while (sym < 288) lens[sym++] = 8; |
|
6ea30fb…
|
florian
|
338 |
next = fixed; |
|
6ea30fb…
|
florian
|
339 |
lenfix = next; |
|
6ea30fb…
|
florian
|
340 |
bits = 9; |
|
6ea30fb…
|
florian
|
341 |
inflate_table(LENS, lens, 288, &(next), &(bits), work); |
|
6ea30fb…
|
florian
|
342 |
|
|
6ea30fb…
|
florian
|
343 |
/* distance table */ |
|
6ea30fb…
|
florian
|
344 |
sym = 0; |
|
6ea30fb…
|
florian
|
345 |
while (sym < 32) lens[sym++] = 5; |
|
6ea30fb…
|
florian
|
346 |
distfix = next; |
|
6ea30fb…
|
florian
|
347 |
bits = 5; |
|
6ea30fb…
|
florian
|
348 |
inflate_table(DISTS, lens, 32, &(next), &(bits), work); |
|
6ea30fb…
|
florian
|
349 |
} |
|
6ea30fb…
|
florian
|
350 |
#else /* !BUILDFIXED */ |
|
6ea30fb…
|
florian
|
351 |
# include "inffixed.h" |
|
6ea30fb…
|
florian
|
352 |
#endif /* BUILDFIXED */ |
|
6ea30fb…
|
florian
|
353 |
|
|
6ea30fb…
|
florian
|
354 |
/* |
|
6ea30fb…
|
florian
|
355 |
Return state with length and distance decoding tables and index sizes set to |
|
6ea30fb…
|
florian
|
356 |
fixed code decoding. Normally this returns fixed tables from inffixed.h. |
|
6ea30fb…
|
florian
|
357 |
If BUILDFIXED is defined, then instead this routine builds the tables the |
|
6ea30fb…
|
florian
|
358 |
first time it's called, and returns those tables the first time and |
|
6ea30fb…
|
florian
|
359 |
thereafter. This reduces the size of the code by about 2K bytes, in |
|
6ea30fb…
|
florian
|
360 |
exchange for a little execution time. However, BUILDFIXED should not be |
|
6ea30fb…
|
florian
|
361 |
used for threaded applications if atomics are not available, as it will |
|
6ea30fb…
|
florian
|
362 |
not be thread-safe. |
|
6ea30fb…
|
florian
|
363 |
*/ |
|
6ea30fb…
|
florian
|
364 |
void inflate_fixed(struct inflate_state FAR *state) { |
|
6ea30fb…
|
florian
|
365 |
#ifdef BUILDFIXED |
|
6ea30fb…
|
florian
|
366 |
z_once(&built, buildtables); |
|
6ea30fb…
|
florian
|
367 |
#endif /* BUILDFIXED */ |
|
6ea30fb…
|
florian
|
368 |
state->lencode = lenfix; |
|
6ea30fb…
|
florian
|
369 |
state->lenbits = 9; |
|
6ea30fb…
|
florian
|
370 |
state->distcode = distfix; |
|
6ea30fb…
|
florian
|
371 |
state->distbits = 5; |
|
6ea30fb…
|
florian
|
372 |
} |
|
6ea30fb…
|
florian
|
373 |
|
|
6ea30fb…
|
florian
|
374 |
#ifdef MAKEFIXED |
|
6ea30fb…
|
florian
|
375 |
#include <stdio.h> |
|
6ea30fb…
|
florian
|
376 |
|
|
6ea30fb…
|
florian
|
377 |
/* |
|
6ea30fb…
|
florian
|
378 |
Write out the inffixed.h that will be #include'd above. Defining MAKEFIXED |
|
6ea30fb…
|
florian
|
379 |
also defines BUILDFIXED, so the tables are built on the fly. main() writes |
|
6ea30fb…
|
florian
|
380 |
those tables to stdout, which would directed to inffixed.h. Compile this |
|
6ea30fb…
|
florian
|
381 |
along with zutil.c: |
|
6ea30fb…
|
florian
|
382 |
|
|
6ea30fb…
|
florian
|
383 |
cc -DMAKEFIXED -o fix inftrees.c zutil.c |
|
6ea30fb…
|
florian
|
384 |
./fix > inffixed.h |
|
6ea30fb…
|
florian
|
385 |
*/ |
|
6ea30fb…
|
florian
|
386 |
int main(void) { |
|
6ea30fb…
|
florian
|
387 |
unsigned low, size; |
|
6ea30fb…
|
florian
|
388 |
struct inflate_state state; |
|
6ea30fb…
|
florian
|
389 |
|
|
6ea30fb…
|
florian
|
390 |
inflate_fixed(&state); |
|
6ea30fb…
|
florian
|
391 |
puts("/* inffixed.h -- table for decoding fixed codes"); |
|
6ea30fb…
|
florian
|
392 |
puts(" * Generated automatically by makefixed()."); |
|
6ea30fb…
|
florian
|
393 |
puts(" */"); |
|
6ea30fb…
|
florian
|
394 |
puts(""); |
|
6ea30fb…
|
florian
|
395 |
puts("/* WARNING: this file should *not* be used by applications."); |
|
6ea30fb…
|
florian
|
396 |
puts(" It is part of the implementation of this library and is"); |
|
6ea30fb…
|
florian
|
397 |
puts(" subject to change. Applications should only use zlib.h."); |
|
6ea30fb…
|
florian
|
398 |
puts(" */"); |
|
6ea30fb…
|
florian
|
399 |
puts(""); |
|
6ea30fb…
|
florian
|
400 |
size = 1U << 9; |
|
6ea30fb…
|
florian
|
401 |
printf("static const code lenfix[%u] = {", size); |
|
6ea30fb…
|
florian
|
402 |
low = 0; |
|
6ea30fb…
|
florian
|
403 |
for (;;) { |
|
6ea30fb…
|
florian
|
404 |
if ((low % 7) == 0) printf("\n "); |
|
6ea30fb…
|
florian
|
405 |
printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, |
|
6ea30fb…
|
florian
|
406 |
state.lencode[low].bits, state.lencode[low].val); |
|
6ea30fb…
|
florian
|
407 |
if (++low == size) break; |
|
6ea30fb…
|
florian
|
408 |
putchar(','); |
|
6ea30fb…
|
florian
|
409 |
} |
|
6ea30fb…
|
florian
|
410 |
puts("\n};"); |
|
6ea30fb…
|
florian
|
411 |
size = 1U << 5; |
|
6ea30fb…
|
florian
|
412 |
printf("\nstatic const code distfix[%u] = {", size); |
|
6ea30fb…
|
florian
|
413 |
low = 0; |
|
6ea30fb…
|
florian
|
414 |
for (;;) { |
|
6ea30fb…
|
florian
|
415 |
if ((low % 6) == 0) printf("\n "); |
|
6ea30fb…
|
florian
|
416 |
printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, |
|
6ea30fb…
|
florian
|
417 |
state.distcode[low].val); |
|
6ea30fb…
|
florian
|
418 |
if (++low == size) break; |
|
6ea30fb…
|
florian
|
419 |
putchar(','); |
|
6ea30fb…
|
florian
|
420 |
} |
|
6ea30fb…
|
florian
|
421 |
puts("\n};"); |
|
6ea30fb…
|
florian
|
422 |
return 0; |
|
6ea30fb…
|
florian
|
423 |
} |
|
6ea30fb…
|
florian
|
424 |
#endif /* MAKEFIXED */ |