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 /* k=7 -> 1110 k=8 -> 11110 k=9 -> 111110 ... */
92 putbits(k - 3, USHRT_MAX << 1);
94 while (i < 6 && pt_len[i] == 0)
101 /* ------------------------------------------------------------------------ */
103 write_c_len(/*void*/)
105 short i, k, n, count;
108 while (n > 0 && c_len[n - 1] == 0)
116 while (i < n && c_len[i] == 0) {
121 for (k = 0; k < count; k++)
122 putcode(pt_len[0], pt_code[0]);
124 else if (count <= 18) {
125 putcode(pt_len[1], pt_code[1]);
126 putbits(4, count - 3);
128 else if (count == 19) {
129 putcode(pt_len[0], pt_code[0]);
130 putcode(pt_len[1], pt_code[1]);
134 putcode(pt_len[2], pt_code[2]);
135 putbits(CBIT, count - 20);
139 putcode(pt_len[k + 2], pt_code[k + 2]);
143 /* ------------------------------------------------------------------------ */
148 putcode(c_len[c], c_code[c]);
151 /* ------------------------------------------------------------------------ */
164 putcode(pt_len[c], pt_code[c]);
169 /* ------------------------------------------------------------------------ */
171 send_block( /* void */ )
174 unsigned short i, k, root, pos, size;
176 root = make_tree(NC, c_freq, c_len, c_code);
181 root = make_tree(NT, t_freq, pt_len, pt_code);
183 write_pt_len(NT, TBIT, 3);
195 root = make_tree(np, p_freq, pt_len, pt_code);
197 write_pt_len(np, pbit, -1);
204 for (i = 0; i < size; i++) {
205 if (i % CHAR_BIT == 0)
209 if (flags & (1 << (CHAR_BIT - 1))) {
210 encode_c(buf[pos++] + (1 << CHAR_BIT));
211 k = buf[pos++] << CHAR_BIT;
215 encode_c(buf[pos++]);
219 for (i = 0; i < NC; i++)
221 for (i = 0; i < np; i++)
225 /* ------------------------------------------------------------------------ */
232 static unsigned short cpos;
235 if (output_mask == 0) {
236 output_mask = 1 << (CHAR_BIT - 1);
237 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
246 buf[output_pos++] = (unsigned char) c;
248 if (c >= (1 << CHAR_BIT)) {
249 buf[cpos] |= output_mask;
250 buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
251 buf[output_pos++] = (unsigned char) p;
261 /* ------------------------------------------------------------------------ */
263 alloc_buf( /* void */ )
265 bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
266 while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
267 bufsiz = (bufsiz / 10) * 9;
268 if (bufsiz < 4 * 1024)
269 fatal_error("Not enough memory");
274 /* ------------------------------------------------------------------------ */
277 encode_start_st1( /* void */ )
283 case LZHUFF5_DICBIT: pbit = 4; np = LZHUFF5_DICBIT + 1; break;
284 case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
285 case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;
290 for (i = 0; i < NC; i++)
292 for (i = 0; i < np; i++)
294 output_pos = output_mask = 0;
300 /* ------------------------------------------------------------------------ */
303 encode_end_st1( /* void */ )
307 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
311 /* ------------------------------------------------------------------------ */
313 /* ------------------------------------------------------------------------ */
315 read_pt_len(nn, nbit, i_special)
325 for (i = 0; i < nn; i++)
327 for (i = 0; i < 256; i++)
337 unsigned short mask = 1 << (16 - 4);
338 while (mask & bitbuf) {
346 if (i == i_special) {
354 make_table(nn, pt_len, 8, pt_table);
358 /* ------------------------------------------------------------------------ */
360 read_c_len( /* void */ )
367 for (i = 0; i < NC; i++)
369 for (i = 0; i < 4096; i++)
374 c = pt_table[peekbits(8)];
376 unsigned short mask = 1 << (16 - 9);
392 c = getbits(CBIT) + 20;
401 make_table(NC, c_len, 12, c_table);
405 /* ------------------------------------------------------------------------ */
408 decode_c_st1( /*void*/ )
410 unsigned short j, mask;
412 if (blocksize == 0) {
413 blocksize = getbits(16);
414 read_pt_len(NT, TBIT, 3);
416 read_pt_len(np, pbit, -1);
419 j = c_table[peekbits(12)];
424 mask = 1 << (16 - 1);
432 fillbuf(c_len[j] - 12);
437 /* ------------------------------------------------------------------------ */
440 decode_p_st1( /* void */ )
442 unsigned short j, mask;
444 j = pt_table[peekbits(8)];
449 mask = 1 << (16 - 1);
457 fillbuf(pt_len[j] - 8);
460 j = (1 << (j - 1)) + getbits(j - 1);
464 /* ------------------------------------------------------------------------ */
467 decode_start_st1( /* void */ )
471 case LZHUFF5_DICBIT: pbit = 4; np = LZHUFF5_DICBIT + 1; break;
472 case LZHUFF6_DICBIT: pbit = 5; np = LZHUFF6_DICBIT + 1; break;
473 case LZHUFF7_DICBIT: pbit = 5; np = LZHUFF7_DICBIT + 1; break;