1 /***********************************************************
2 huf.c -- static Huffman
3 ***********************************************************/
7 #define NP (MAXDICBIT + 1)
8 #define NT (CODE_BIT + 3)
9 #define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
10 #define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
17 ushort left[2 * NC - 1], right[2 * NC - 1];
18 static uchar *buf, c_len[NC], pt_len[NPT];
19 static uint bufsiz = 0, blocksize;
20 static ushort c_freq[2 * NC - 1], c_table[4096], c_code[NC],
21 p_freq[2 * NP - 1], pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
27 init_parameter(struct lha_method *m)
33 /***** encoding *****/
40 for (i = 0; i < NT; i++)
43 while (n > 0 && c_len[n - 1] == 0)
50 while (i < n && c_len[i] == 0) {
58 else if (count == 19) {
71 write_pt_len(int n, int nbit, int i_special)
75 while (n > 0 && pt_len[n - 1] == 0)
84 putbits(k - 3, (1U << (k - 3)) - 2);
86 while (i < 6 && pt_len[i] == 0)
88 putbits(2, (i - 3) & 3);
99 while (n > 0 && c_len[n - 1] == 0)
107 while (i < n && c_len[i] == 0) {
112 for (k = 0; k < count; k++)
113 putbits(pt_len[0], pt_code[0]);
115 else if (count <= 18) {
116 putbits(pt_len[1], pt_code[1]);
117 putbits(4, count - 3);
119 else if (count == 19) {
120 putbits(pt_len[0], pt_code[0]);
121 putbits(pt_len[1], pt_code[1]);
125 putbits(pt_len[2], pt_code[2]);
126 putbits(CBIT, count - 20);
130 putbits(pt_len[k + 2], pt_code[k + 2]);
137 putbits(c_len[c], c_code[c]);
151 putbits(pt_len[c], pt_code[c]);
153 putbits(c - 1, p & (0xFFFFU >> (17 - c)));
159 uint i, k, flags, root, pos, size;
161 root = make_tree(NC, c_freq, c_len, c_code);
166 root = make_tree(NT, t_freq, pt_len, pt_code);
168 write_pt_len(NT, TBIT, 3);
182 root = make_tree(np, p_freq, pt_len, pt_code);
184 write_pt_len(np, pbit, -1);
191 for (i = 0; i < size; i++) {
192 if (i % CHAR_BIT == 0)
196 if (flags & (1U << (CHAR_BIT - 1))) {
197 encode_c(buf[pos++] + (1U << CHAR_BIT));
198 k = buf[pos++] << CHAR_BIT;
203 encode_c(buf[pos++]);
207 for (i = 0; i < NC; i++)
209 for (i = 0; i < np; i++)
213 static uint output_pos, output_mask;
216 output(uint c, uint p)
220 if ((output_mask >>= 1) == 0) {
221 output_mask = 1U << (CHAR_BIT - 1);
222 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
231 buf[output_pos++] = (uchar) c;
233 if (c >= (1U << CHAR_BIT)) {
234 buf[cpos] |= output_mask;
235 buf[output_pos++] = (uchar) (p >> CHAR_BIT);
236 buf[output_pos++] = (uchar) p;
247 huf_encode_start(struct lha_method *m)
255 while ((buf = malloc(bufsiz)) == NULL) {
256 bufsiz = (bufsiz / 10U) * 9U;
257 if (bufsiz < 4 * 1024U)
258 error("Out of memory.");
262 for (i = 0; i < NC; i++)
264 for (i = 0; i < np; i++)
266 output_pos = output_mask = 0;
275 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
279 /***** decoding *****/
282 read_pt_len(int nn, int nbit, int i_special)
290 for (i = 0; i < nn; i++)
292 for (i = 0; i < 256; i++)
298 c = bitbuf >> (BITBUFSIZ - 3);
300 mask = 1U << (BITBUFSIZ - 1 - 3);
301 while (mask & bitbuf) {
306 fillbuf((c < 7) ? 3 : c - 3);
308 if (i == i_special) {
316 make_table(nn, pt_len, 8, pt_table);
329 for (i = 0; i < NC; i++)
331 for (i = 0; i < 4096; i++)
337 c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
339 mask = 1U << (BITBUFSIZ - 1 - 8);
355 c = getbits(CBIT) + 20;
364 make_table(NC, c_len, 12, c_table);
373 if (blocksize == 0) {
374 blocksize = getbits(16);
375 read_pt_len(NT, TBIT, 3);
377 read_pt_len(np, pbit, -1);
380 j = c_table[bitbuf >> (BITBUFSIZ - 12)];
382 mask = 1U << (BITBUFSIZ - 1 - 12);
400 j = pt_table[bitbuf >> (BITBUFSIZ - 8)];
402 mask = 1U << (BITBUFSIZ - 1 - 8);
413 j = (1U << (j - 1)) + getbits(j - 1);
418 huf_decode_start(struct lha_method *m)