34 {
35 unsigned len;
36 unsigned sym;
38 unsigned root;
39 unsigned curr;
40 unsigned drop;
41 int left;
42 unsigned used;
43 unsigned huff;
44 unsigned incr;
45 unsigned fill;
46 unsigned low;
47 unsigned mask;
50 const unsigned short FAR *base;
51 const unsigned short FAR *extra;
52 unsigned match;
53 unsigned short count[
MAXBITS+1];
55 static const unsigned short lbase[31] = {
56 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
57 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
58 static const unsigned short lext[31] = {
59 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
60 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 77};
61 static const unsigned short dbase[32] = {
62 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
63 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
64 8193, 12289, 16385, 24577, 0, 0};
65 static const unsigned short dext[32] = {
66 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
67 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
68 28, 28, 29, 29, 64, 64};
69
70
71
72
73
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 for (len = 0; len <=
MAXBITS; len++)
103 count[len] = 0;
104 for (sym = 0; sym < codes; sym++)
105 count[lens[sym]]++;
106
107
108 root = *bits;
110 if (count[max] != 0) break;
111 if (root > max) root =
max;
112 if (max == 0) {
113 here.
op = (
unsigned char)64;
114 here.
bits = (
unsigned char)1;
115 here.
val = (
unsigned short)0;
116 *(*table)++ = here;
117 *(*table)++ = here;
118 *bits = 1;
119 return 0;
120 }
122 if (count[min] != 0) break;
123 if (root < min) root =
min;
124
125
126 left = 1;
127 for (len = 1; len <=
MAXBITS; len++) {
128 left <<= 1;
129 left -= count[len];
130 if (left < 0) return -1;
131 }
132 if (left > 0 && (type ==
CODES || max != 1))
133 return -1;
134
135
136 offs[1] = 0;
137 for (len = 1; len <
MAXBITS; len++)
138 offs[len + 1] = offs[len] + count[len];
139
140
141 for (sym = 0; sym < codes; sym++)
142 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
143
144
145
146
147
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 switch (type) {
178 base = extra = work;
179 match = 20;
180 break;
182 base = lbase;
183 extra = lext;
184 match = 257;
185 break;
186 default:
187 base = dbase;
188 extra = dext;
189 match = 0;
190 }
191
192
193 huff = 0;
194 sym = 0;
197 curr = root;
198 drop = 0;
199 low = (unsigned)(-1);
200 used = 1U << root;
201 mask = used - 1;
202
203
206 return 1;
207
208
209 for (;;) {
210
211 here.
bits = (
unsigned char)(len - drop);
212 if (work[sym] + 1U < match) {
213 here.
op = (
unsigned char)0;
214 here.
val = work[sym];
215 }
216 else if (work[sym] >= match) {
217 here.
op = (
unsigned char)(extra[work[sym] - match]);
218 here.
val = base[work[sym] - match];
219 }
220 else {
221 here.
op = (
unsigned char)(32 + 64);
223 }
224
225
226 incr = 1U << (len - drop);
227 fill = 1U << curr;
229 do {
230 fill -= incr;
231 next[(huff >> drop) + fill] = here;
232 } while (fill != 0);
233
234
235 incr = 1U << (len - 1);
236 while (huff & incr)
237 incr >>= 1;
238 if (incr != 0) {
239 huff &= incr - 1;
240 huff += incr;
241 }
242 else
243 huff = 0;
244
245
246 sym++;
247 if (--(count[len]) == 0) {
248 if (len == max) break;
249 len = lens[work[sym]];
250 }
251
252
253 if (len > root && (huff & mask) != low) {
254
255 if (drop == 0)
256 drop = root;
257
258
260
261
262 curr = len - drop;
263 left = (int)(1 << curr);
264 while (curr + drop < max) {
265 left -= count[curr + drop];
266 if (left <= 0) break;
267 curr++;
268 left <<= 1;
269 }
270
271
272 used += 1U << curr;
275 return 1;
276
277
278 low = huff & mask;
279 (*table)[low].op = (unsigned char)curr;
280 (*table)[low].bits = (unsigned char)root;
281 (*table)[low].val = (unsigned short)(next - *table);
282 }
283 }
284
285
286
287
288 if (huff != 0) {
289 here.
op = (
unsigned char)64;
290 here.
bits = (
unsigned char)(len - drop);
291 here.
val = (
unsigned short)0;
292 next[huff] = here;
293 }
294
295
297 *bits = root;
298 return 0;
299}
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