40{
41 unsigned len;
42 unsigned sym;
43 unsigned min, max;
44 unsigned root;
45 unsigned curr;
46 unsigned drop;
47 int left;
48 unsigned used;
49 unsigned huff;
50 unsigned incr;
51 unsigned fill;
52 unsigned low;
53 unsigned mask;
56 const unsigned short FAR *base;
57 const unsigned short FAR *extra;
58 int end;
59 unsigned short count[
MAXBITS+1];
61 static const unsigned short lbase[31] = {
62 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
63 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
64 static const unsigned short lext[31] = {
65 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
66 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
67 static const unsigned short dbase[32] = {
68 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
69 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
70 8193, 12289, 16385, 24577, 0, 0};
71 static const unsigned short dext[32] = {
72 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
73 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
74 28, 28, 29, 29, 64, 64};
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
108 for (len = 0; len <=
MAXBITS; len++)
109 count[len] = 0;
110 for (sym = 0; sym < codes; sym++)
111 count[lens[sym]]++;
112
113
114 root = *bits;
115 for (max =
MAXBITS; max >= 1; max--)
116 if (count[max] != 0) break;
117 if (root > max) root = max;
118 if (max == 0) {
119 here.
op = (
unsigned char)64;
120 here.
bits = (
unsigned char)1;
121 here.
val = (
unsigned short)0;
122 *(*table)++ = here;
123 *(*table)++ = here;
124 *bits = 1;
125 return 0;
126 }
127 for (min = 1; min < max; min++)
128 if (count[min] != 0) break;
129 if (root < min) root = min;
130
131
132 left = 1;
133 for (len = 1; len <=
MAXBITS; len++) {
134 left <<= 1;
135 left -= count[len];
136 if (left < 0) return -1;
137 }
138 if (left > 0 && (
type ==
CODES || max != 1))
139 return -1;
140
141
142 offs[1] = 0;
143 for (len = 1; len <
MAXBITS; len++)
144 offs[len + 1] = offs[len] + count[len];
145
146
147 for (sym = 0; sym < codes; sym++)
148 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
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
184 base = extra = work;
185 end = 19;
186 break;
188 base = lbase;
189 base -= 257;
190 extra = lext;
191 extra -= 257;
192 end = 256;
193 break;
194 default:
195 base = dbase;
196 extra = dext;
197 end = -1;
198 }
199
200
201 huff = 0;
202 sym = 0;
203 len = min;
204 next = *table;
205 curr = root;
206 drop = 0;
207 low = (unsigned)(-1);
208 used = 1U << root;
209 mask = used - 1;
210
211
214 return 1;
215
216
217 for (;;) {
218
219 here.
bits = (
unsigned char)(len - drop);
220 if ((int)(work[sym]) < end) {
221 here.
op = (
unsigned char)0;
222 here.
val = work[sym];
223 }
224 else if ((int)(work[sym]) > end) {
225 here.
op = (
unsigned char)(extra[work[sym]]);
226 here.
val = base[work[sym]];
227 }
228 else {
229 here.
op = (
unsigned char)(32 + 64);
231 }
232
233
234 incr = 1U << (len - drop);
235 fill = 1U << curr;
236 min = fill;
237 do {
238 fill -= incr;
239 next[(huff >> drop) + fill] = here;
240 } while (fill != 0);
241
242
243 incr = 1U << (len - 1);
244 while (huff & incr)
245 incr >>= 1;
246 if (incr != 0) {
247 huff &= incr - 1;
248 huff += incr;
249 }
250 else
251 huff = 0;
252
253
254 sym++;
255 if (--(count[len]) == 0) {
256 if (len == max) break;
257 len = lens[work[sym]];
258 }
259
260
261 if (len > root && (huff & mask) != low) {
262
263 if (drop == 0)
264 drop = root;
265
266
267 next += min;
268
269
270 curr = len - drop;
271 left = (int)(1 << curr);
272 while (curr + drop < max) {
273 left -= count[curr + drop];
274 if (left <= 0) break;
275 curr++;
276 left <<= 1;
277 }
278
279
280 used += 1U << curr;
283 return 1;
284
285
286 low = huff & mask;
287 (*table)[low].op = (unsigned char)curr;
288 (*table)[low].bits = (unsigned char)root;
289 (*table)[low].val = (unsigned short)(next - *table);
290 }
291 }
292
293
294
295
296
297
298
299
300 here.
op = (
unsigned char)64;
301 here.
bits = (
unsigned char)(len - drop);
302 here.
val = (
unsigned short)0;
303 while (huff != 0) {
304
305 if (drop != 0 && (huff & mask) != low) {
306 drop = 0;
307 len = root;
308 next = *table;
309 here.
bits = (
unsigned char)len;
310 }
311
312
313 next[huff >> drop] = here;
314
315
316 incr = 1U << (len - 1);
317 while (huff & incr)
318 incr >>= 1;
319 if (incr != 0) {
320 huff &= incr - 1;
321 huff += incr;
322 }
323 else
324 huff = 0;
325 }
326
327
328 *table += used;
329 *bits = root;
330 return 0;
331}
type
CUBE COVER and CUBE typedefs ///.