39{
40 unsigned len;
41 unsigned sym;
43 unsigned root;
44 unsigned curr;
45 unsigned drop;
46 int left;
47 unsigned used;
48 unsigned huff;
49 unsigned incr;
50 unsigned fill;
51 unsigned low;
52 unsigned mask;
55 const unsigned short FAR *base;
56 const unsigned short FAR *extra;
57 unsigned match;
58 unsigned short count[
MAXBITS+1];
60 static const unsigned short lbase[31] = {
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] = {
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, 199, 202};
66 static const unsigned short dbase[32] = {
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] = {
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 for (len = 0; len <=
MAXBITS; len++)
108 count[len] = 0;
109 for (sym = 0; sym < codes; sym++)
110 count[lens[sym]]++;
111
112
113 root = *bits;
115 if (count[max] != 0) break;
116 if (root > max) root =
max;
117 if (max == 0) {
118 here.
op = (
unsigned char)64;
119 here.
bits = (
unsigned char)1;
120 here.
val = (
unsigned short)0;
121 *(*table)++ = here;
122 *(*table)++ = here;
123 *bits = 1;
124 return 0;
125 }
127 if (count[min] != 0) break;
128 if (root < min) root =
min;
129
130
131 left = 1;
132 for (len = 1; len <=
MAXBITS; len++) {
133 left <<= 1;
134 left -= count[len];
135 if (left < 0) return -1;
136 }
137 if (left > 0 && (type ==
CODES || max != 1))
138 return -1;
139
140
141 offs[1] = 0;
142 for (len = 1; len <
MAXBITS; len++)
143 offs[len + 1] = offs[len] + count[len];
144
145
146 for (sym = 0; sym < codes; sym++)
147 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 switch (type) {
183 base = extra = work;
184 match = 20;
185 break;
187 base = lbase;
188 extra = lext;
189 match = 257;
190 break;
191 default:
192 base = dbase;
193 extra = dext;
194 match = 0;
195 }
196
197
198 huff = 0;
199 sym = 0;
201 next = *table;
202 curr = root;
203 drop = 0;
204 low = (unsigned)(-1);
205 used = 1U << root;
206 mask = used - 1;
207
208
211 return 1;
212
213
214 for (;;) {
215
216 here.
bits = (
unsigned char)(len - drop);
217 if (work[sym] + 1U < match) {
218 here.
op = (
unsigned char)0;
219 here.
val = work[sym];
220 }
221 else if (work[sym] >= match) {
222 here.
op = (
unsigned char)(extra[work[sym] - match]);
223 here.
val = base[work[sym] - match];
224 }
225 else {
226 here.
op = (
unsigned char)(32 + 64);
228 }
229
230
231 incr = 1U << (len - drop);
232 fill = 1U << curr;
234 do {
235 fill -= incr;
236 next[(huff >> drop) + fill] = here;
237 } while (fill != 0);
238
239
240 incr = 1U << (len - 1);
241 while (huff & incr)
242 incr >>= 1;
243 if (incr != 0) {
244 huff &= incr - 1;
245 huff += incr;
246 }
247 else
248 huff = 0;
249
250
251 sym++;
252 if (--(count[len]) == 0) {
253 if (len == max) break;
254 len = lens[work[sym]];
255 }
256
257
258 if (len > root && (huff & mask) != low) {
259
260 if (drop == 0)
261 drop = root;
262
263
265
266
267 curr = len - drop;
268 left = (int)(1 << curr);
269 while (curr + drop < max) {
270 left -= count[curr + drop];
271 if (left <= 0) break;
272 curr++;
273 left <<= 1;
274 }
275
276
277 used += 1U << curr;
280 return 1;
281
282
283 low = huff & mask;
284 (*table)[low].op = (unsigned char)curr;
285 (*table)[low].bits = (unsigned char)root;
286 (*table)[low].val = (unsigned short)(next - *table);
287 }
288 }
289
290
291
292
293 if (huff != 0) {
294 here.
op = (
unsigned char)64;
295 here.
bits = (
unsigned char)(len - drop);
296 here.
val = (
unsigned short)0;
297 next[huff] = here;
298 }
299
300
301 *table += used;
302 *bits = root;
303 return 0;
304}
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments