1 /* ------------------------------------------------------------------------ */
3 /* huf.c -- new static Huffman */
5 /* Modified Nobutaka Watazaki */
7 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
8 /* Ver. 1.14i Support LH7 & Bug Fixed 2000.10. 6 t.okamoto */
9 /* ------------------------------------------------------------------------ */
15 #include <sys/param.h>
21 extern char *malloc ();
24 /* ------------------------------------------------------------------------ */
25 unsigned short left[2 * NC - 1], right[2 * NC - 1];
26 unsigned char c_len[NC], pt_len[NPT];
27 unsigned short c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
28 pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
30 static unsigned char *buf;
31 static unsigned int bufsiz;
32 static unsigned short blocksize;
33 static unsigned short output_pos, output_mask;
36 /* ------------------------------------------------------------------------ */
38 /* ------------------------------------------------------------------------ */
40 count_t_freq(/*void*/)
44 for (i = 0; i < NT; i++)
47 while (n > 0 && c_len[n - 1] == 0)
54 while (i < n && c_len[i] == 0) {
62 else if (count == 19) {
73 /* ------------------------------------------------------------------------ */
75 write_pt_len(n, nbit, i_special)
82 while (n > 0 && pt_len[n - 1] == 0)
91 putbits(k - 3, USHRT_MAX << 1);
93 while (i < 6 && pt_len[i] == 0)
100 /* ------------------------------------------------------------------------ */
102 write_c_len(/*void*/)
104 short i, k, n, count;
107 while (n > 0 && c_len[n - 1] == 0)
115 while (i < n && c_len[i] == 0) {
120 for (k = 0; k < count; k++)
121 putcode(pt_len[0], pt_code[0]);
123 else if (count <= 18) {
124 putcode(pt_len[1], pt_code[1]);
125 putbits(4, count - 3);
127 else if (count == 19) {
128 putcode(pt_len[0], pt_code[0]);
129 putcode(pt_len[1], pt_code[1]);
133 putcode(pt_len[2], pt_code[2]);
134 putbits(CBIT, count - 20);
138 putcode(pt_len[k + 2], pt_code[k + 2]);
142 /* ------------------------------------------------------------------------ */
147 putcode(c_len[c], c_code[c]);
150 /* ------------------------------------------------------------------------ */
163 putcode(pt_len[c], pt_code[c]);
168 /* ------------------------------------------------------------------------ */
170 send_block( /* void */ )
173 unsigned short i, k, root, pos, size;
175 root = make_tree(NC, c_freq, c_len, c_code);
180 root = make_tree(NT, t_freq, pt_len, pt_code);
182 write_pt_len(NT, TBIT, 3);
194 root = make_tree(np, p_freq, pt_len, pt_code);
196 write_pt_len(np, pbit, -1);
203 for (i = 0; i < size; i++) {
204 if (i % CHAR_BIT == 0)
208 if (flags & (1 << (CHAR_BIT - 1))) {
209 encode_c(buf[pos++] + (1 << CHAR_BIT));
210 k = buf[pos++] << CHAR_BIT;
214 encode_c(buf[pos++]);
218 for (i = 0; i < NC; i++)
220 for (i = 0; i < np; i++)
224 /* ------------------------------------------------------------------------ */
231 static unsigned short cpos;
234 if (output_mask == 0) {
235 output_mask = 1 << (CHAR_BIT - 1);
236 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
245 buf[output_pos++] = (unsigned char) c;
247 if (c >= (1 << CHAR_BIT)) {
248 buf[cpos] |= output_mask;
249 buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
250 buf[output_pos++] = (unsigned char) p;
260 /* ------------------------------------------------------------------------ */
262 alloc_buf( /* void */ )
264 bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
265 while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
266 bufsiz = (bufsiz / 10) * 9;
267 if (bufsiz < 4 * 1024)
268 fatal_error("Not enough memory");
273 /* ------------------------------------------------------------------------ */
276 encode_start_st1( /* void */ )
282 case LZHUFF5_DICBIT: pbit = 4; np = LZHUFF5_DICBIT + 1; break;
283 case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
284 case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;
289 for (i = 0; i < NC; i++)
291 for (i = 0; i < np; i++)
293 output_pos = output_mask = 0;
299 /* ------------------------------------------------------------------------ */
302 encode_end_st1( /* void */ )
306 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
310 /* ------------------------------------------------------------------------ */
312 /* ------------------------------------------------------------------------ */
314 read_pt_len(nn, nbit, i_special)
324 for (i = 0; i < nn; i++)
326 for (i = 0; i < 256; i++)
332 c = bitbuf >> (16 - 3);
334 unsigned short mask = 1 << (16 - 4);
335 while (mask & bitbuf) {
340 fillbuf((c < 7) ? 3 : c - 3);
342 if (i == i_special) {
350 make_table(nn, pt_len, 8, pt_table);
354 /* ------------------------------------------------------------------------ */
356 read_c_len( /* void */ )
363 for (i = 0; i < NC; i++)
365 for (i = 0; i < 4096; i++)
370 c = pt_table[bitbuf >> (16 - 8)];
372 unsigned short mask = 1 << (16 - 9);
388 c = getbits(CBIT) + 20;
397 make_table(NC, c_len, 12, c_table);
401 /* ------------------------------------------------------------------------ */
404 decode_c_st1( /*void*/ )
406 unsigned short j, mask;
408 if (blocksize == 0) {
409 blocksize = getbits(16);
410 read_pt_len(NT, TBIT, 3);
412 read_pt_len(np, pbit, -1);
415 j = c_table[bitbuf >> 4];
420 mask = 1 << (16 - 1);
428 fillbuf(c_len[j] - 12);
433 /* ------------------------------------------------------------------------ */
436 decode_p_st1( /* void */ )
438 unsigned short j, mask;
440 j = pt_table[bitbuf >> (16 - 8)];
445 mask = 1 << (16 - 1);
453 fillbuf(pt_len[j] - 8);
456 j = (1 << (j - 1)) + getbits(j - 1);
460 /* ------------------------------------------------------------------------ */
463 decode_start_st1( /* void */ )
467 case LZHUFF5_DICBIT: pbit = 4; np = LZHUFF5_DICBIT + 1; break;
468 case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
469 case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;
479 /* Local Variables: */
483 /* vi: set tabstop=4: */