1 /***********************************************************
2 huf.c -- static Huffman
3 ***********************************************************/
10 count_t_freq(struct lzh_ostream *wp, uchar *c_len, ushort *t_freq)
14 for (i = 0; i < NT; i++)
17 while (n > 0 && c_len[n - 1] == 0)
24 while (i < n && c_len[i] == 0) {
32 else if (count == 19) {
45 write_pt_len(struct lzh_ostream *wp, int n, int nbit, int i_special,
50 while (n > 0 && pt_len[n - 1] == 0)
59 putbits(wp, k - 3, (1U << (k - 3)) - 2);
61 while (i < 6 && pt_len[i] == 0)
63 putbits(wp, 2, (i - 3) & 3);
69 write_c_len(struct lzh_ostream *wp, uchar *c_len, uchar *t_len, ushort *t_code)
74 while (n > 0 && c_len[n - 1] == 0)
82 while (i < n && c_len[i] == 0) {
87 for (k = 0; k < count; k++)
88 putbits(wp, t_len[0], t_code[0]);
90 else if (count <= 18) {
91 putbits(wp, t_len[1], t_code[1]);
92 putbits(wp, 4, count - 3);
94 else if (count == 19) {
95 putbits(wp, t_len[0], t_code[0]);
96 putbits(wp, t_len[1], t_code[1]);
100 putbits(wp, t_len[2], t_code[2]);
101 putbits(wp, CBIT, count - 20);
105 putbits(wp, t_len[k + 2], t_code[k + 2]);
110 encode_c(struct lzh_ostream *wp, int c, uchar *c_len, ushort *c_code)
112 putbits(wp, c_len[c], c_code[c]);
116 encode_p(struct lzh_ostream *wp, uint p, uchar *p_len, ushort *p_code)
126 putbits(wp, p_len[c], p_code[c]);
128 putbits(wp, c - 1, p & (0xFFFFU >> (17 - c)));
132 size frequency bitlength Huffman coding
133 -----------------------------------------------------------
134 NC c_freq c_len c_code
135 NT t_freq t_len t_code
136 np p_freq p_len p_code
138 output format for a block.
145 +-----+--------------------+
146 | len | t_len | Huffman tree for c_len[]
147 +-----+--------------------+
151 +-------+------------------+
152 | len | c_len | Huffman tree for characters and length
153 +-------+------------------+
157 +---------+--------------------+
158 | len | p_len | Huffman tree for offset
159 +---------+--------------------+
161 (pbit=4bit(lh4,5) or 5bit(lh6,7))
163 +---------------------+
165 +---------------------+
168 In special case, only one kind characters in a block.
179 | 0 | 0 | Huffman tree for c_len[]
184 | 0 | 0 | Huffman tree for characters and length
188 +---------+--------------+
189 | 0 | offset value | Huffman tree for offset
190 +---------+--------------+
192 (pbit=4bit(lh4,5) or 5bit(lh6,7))
194 +---------------------+
196 +---------------------+
199 send_block(struct lzh_ostream *wp)
201 uint i, k, flags, root, pos, size;
212 /* make Huffman tree for characters and length */
213 root = make_tree(NC, wp->c_freq, c_len, c_code);
214 size = wp->c_freq[root];
215 putbits(wp, 16, size);
217 ushort t_freq[2 * NT - 1];
219 /* make Huffman tree for c_len */
220 count_t_freq(wp, c_len, t_freq);
221 root = make_tree(NT, t_freq, t_len, t_code);
223 write_pt_len(wp, NT, TBIT, 3, t_len);
227 putbits(wp, TBIT, 0);
228 putbits(wp, TBIT, root);
230 write_c_len(wp, c_len, t_len, t_code);
234 putbits(wp, TBIT, 0);
235 putbits(wp, TBIT, 0);
236 putbits(wp, CBIT, 0);
237 putbits(wp, CBIT, root);
240 /* make Huffman tree for offset */
241 root = make_tree(wp->np, wp->p_freq, p_len, p_code);
242 if (root >= wp->np) {
243 write_pt_len(wp, wp->np, wp->pbit, -1, p_len);
246 putbits(wp, wp->pbit, 0);
247 putbits(wp, wp->pbit, root);
250 /* write Huffman encoding */
252 for (i = 0; i < size; i++) {
253 if (i % CHAR_BIT == 0)
254 flags = wp->buf[pos++];
257 if (flags & (1U << (CHAR_BIT - 1))) {
259 encode_c(wp, wp->buf[pos++] + (1U << CHAR_BIT), c_len, c_code);
261 k = wp->buf[pos++] << CHAR_BIT;
263 encode_p(wp, k, p_len, p_code);
266 /* write character */
267 encode_c(wp, wp->buf[pos++], c_len, c_code);
273 /* clear frequency table */
274 for (i = 0; i < NC; i++)
276 for (i = 0; i < wp->np; i++)
281 call with output(wp, c, 0) c < 256
282 or output(wp, len, off) len >= 256
289 +----+----+----+----+----+----+----+----+----+----+----+----+----+
290 buf | 40 | c1 | c2 |len | off | c4 |len | off | c6 | c7 | c8 |
291 +----+----+----+----+----+----+----+----+----+----+----+----+----+
295 buf[cpos] = 32 + 8 (it points <len, off>)
297 c_freq[] has frequency of cN and len.
298 p_freq[] has frequency of length of off bits.
301 output(struct lzh_ostream *wp, uint c, uint p)
303 wp->output_mask >>= 1;
304 if (wp->output_mask == 0) {
305 wp->output_mask = 128;
306 /* max byte size of one block: 3 * CHAR_BIT + 1 */
307 if (wp->output_pos > wp->bufsiz - (3 * CHAR_BIT + 1)) {
313 wp->cpos = wp->output_pos++;
314 wp->buf[wp->cpos] = 0;
316 wp->buf[wp->output_pos++] = (uchar) c; /* char or length */
319 if (c >= (1U << CHAR_BIT)) {
320 /* c is length, p is offset */
321 wp->buf[wp->cpos] |= wp->output_mask;
322 wp->buf[wp->output_pos++] = (uchar) (p >> CHAR_BIT);
323 wp->buf[wp->output_pos++] = (uchar) p;
326 /* count frequency of p's bit length */
327 int n = 0; /* max of n is np-1 */
338 init_enc_parameter(struct lzh_ostream *wp, struct lha_method *m)
340 wp->np = m->dicbit + 1;
345 huf_encode_start(struct lzh_ostream *wp, struct lha_method *m)
349 init_enc_parameter(wp, m);
350 wp->output_pos = wp->output_mask = wp->cpos = 0;
353 wp->bufsiz = 16 * 1024U;
354 while ((wp->buf = malloc(wp->bufsiz)) == NULL) {
355 wp->bufsiz = (wp->bufsiz / 10U) * 9U;
356 if (wp->bufsiz < 4 * 1024U)
357 error("Out of memory.");
361 for (i = 0; i < NC; i++)
363 for (i = 0; i < wp->np; i++)
370 huf_encode_end(struct lzh_ostream *wp)
372 if (!wp->unpackable) {
374 putbits(wp, CHAR_BIT - 1, 0); /* flush remaining bits */
383 /***** decoding *****/
386 read_pt_len(struct huf_t *huf, struct lzh_istream *rp, int nn, int nbit,
387 int i_special, uchar *pt_len, ushort *pt_table)
392 n = getbits(rp, nbit);
394 c = getbits(rp, nbit);
395 for (i = 0; i < nn; i++)
397 for (i = 0; i < 256; i++)
403 c = rp->bitbuf >> (BITBUFSIZ - 3);
405 mask = 1U << (BITBUFSIZ - 1 - 3);
406 while (mask & rp->bitbuf) {
411 fillbuf(rp, (c < 7) ? 3 : c - 3);
413 if (i == i_special) {
421 make_table(huf, nn, pt_len, 8, pt_table);
426 read_c_len(struct huf_t *huf, struct lzh_istream *rp, uchar *t_len, ushort *t_table)
431 n = getbits(rp, CBIT);
433 c = getbits(rp, CBIT);
434 for (i = 0; i < NC; i++)
436 for (i = 0; i < 4096; i++)
442 c = t_table[rp->bitbuf >> (BITBUFSIZ - 8)];
444 mask = 1U << (BITBUFSIZ - 1 - 8);
446 if (rp->bitbuf & mask)
453 fillbuf(rp, t_len[c]);
458 c = getbits(rp, 4) + 3;
460 c = getbits(rp, CBIT) + 20;
465 rp->c_len[i++] = c - 2;
469 make_table(huf, NC, rp->c_len, 12, rp->c_table);
474 decode_c(struct huf_t *huf, struct lzh_istream *rp)
478 if (rp->blocksize == 0) {
480 unsigned short t_table[256];
482 rp->blocksize = getbits(rp, 16);
483 read_pt_len(huf, rp, NT, TBIT, 3, t_len, t_table);
484 read_c_len(huf, rp, t_len, t_table);
485 read_pt_len(huf, rp, rp->np, rp->pbit, -1, rp->p_len, rp->p_table);
488 j = rp->c_table[rp->bitbuf >> (BITBUFSIZ - 12)];
490 mask = 1U << (BITBUFSIZ - 1 - 12);
492 if (rp->bitbuf & mask)
499 fillbuf(rp, rp->c_len[j]);
504 decode_p(struct huf_t *huf, struct lzh_istream *rp)
508 j = rp->p_table[rp->bitbuf >> (BITBUFSIZ - 8)];
510 mask = 1U << (BITBUFSIZ - 1 - 8);
512 if (rp->bitbuf & mask)
517 } while (j >= rp->np);
519 fillbuf(rp, rp->p_len[j]);
521 j = (1U << (j - 1)) + getbits(rp, j - 1);
526 init_dec_parameter(struct lzh_istream *rp, struct lha_method *m)
528 rp->np = m->dicbit + 1;
533 huf_decode_start(struct lzh_istream *rp, struct lha_method *m)
535 init_dec_parameter(rp, m);