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 /* ------------------------------------------------------------------------ */
13 #include <sys/param.h>
19 extern char *malloc ();
22 /* ------------------------------------------------------------------------ */
23 unsigned short left[2 * NC - 1], right[2 * NC - 1];
24 unsigned char c_len[NC], pt_len[NPT];
25 unsigned short c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
26 pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
28 static unsigned char *buf;
29 static unsigned int bufsiz;
30 static unsigned short blocksize;
31 static unsigned short output_pos, output_mask;
34 /* ------------------------------------------------------------------------ */
36 /* ------------------------------------------------------------------------ */
38 count_t_freq(/*void*/)
42 for (i = 0; i < NT; i++)
45 while (n > 0 && c_len[n - 1] == 0)
52 while (i < n && c_len[i] == 0) {
60 else if (count == 19) {
71 /* ------------------------------------------------------------------------ */
73 write_pt_len(n, nbit, i_special)
80 while (n > 0 && pt_len[n - 1] == 0)
89 putbits(k - 3, USHRT_MAX << 1);
91 while (i < 6 && pt_len[i] == 0)
98 /* ------------------------------------------------------------------------ */
100 write_c_len(/*void*/)
102 short i, k, n, count;
105 while (n > 0 && c_len[n - 1] == 0)
113 while (i < n && c_len[i] == 0) {
118 for (k = 0; k < count; k++)
119 putcode(pt_len[0], pt_code[0]);
121 else if (count <= 18) {
122 putcode(pt_len[1], pt_code[1]);
123 putbits(4, count - 3);
125 else if (count == 19) {
126 putcode(pt_len[0], pt_code[0]);
127 putcode(pt_len[1], pt_code[1]);
131 putcode(pt_len[2], pt_code[2]);
132 putbits(CBIT, count - 20);
136 putcode(pt_len[k + 2], pt_code[k + 2]);
140 /* ------------------------------------------------------------------------ */
145 putcode(c_len[c], c_code[c]);
148 /* ------------------------------------------------------------------------ */
161 putcode(pt_len[c], pt_code[c]);
166 /* ------------------------------------------------------------------------ */
168 send_block( /* void */ )
171 unsigned short i, k, root, pos, size;
173 root = make_tree(NC, c_freq, c_len, c_code);
178 root = make_tree(NT, t_freq, pt_len, pt_code);
180 write_pt_len(NT, TBIT, 3);
192 root = make_tree(np, p_freq, pt_len, pt_code);
194 write_pt_len(np, pbit, -1);
201 for (i = 0; i < size; i++) {
202 if (i % CHAR_BIT == 0)
206 if (flags & (1 << (CHAR_BIT - 1))) {
207 encode_c(buf[pos++] + (1 << CHAR_BIT));
208 k = buf[pos++] << CHAR_BIT;
212 encode_c(buf[pos++]);
216 for (i = 0; i < NC; i++)
218 for (i = 0; i < np; i++)
222 /* ------------------------------------------------------------------------ */
228 static unsigned short cpos;
231 if (output_mask == 0) {
232 output_mask = 1 << (CHAR_BIT - 1);
233 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
242 buf[output_pos++] = (unsigned char) c;
244 if (c >= (1 << CHAR_BIT)) {
245 buf[cpos] |= output_mask;
246 buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
247 buf[output_pos++] = (unsigned char) p;
257 /* ------------------------------------------------------------------------ */
259 alloc_buf( /* void */ )
261 bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
262 while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
263 bufsiz = (bufsiz / 10) * 9;
264 if (bufsiz < 4 * 1024)
265 fatal_error("Not enough memory");
270 /* ------------------------------------------------------------------------ */
272 encode_start_st1( /* void */ )
277 if (dicbit <= (MAX_DICBIT - 2)) {
278 pbit = 4; /* lh4,5 etc. */
287 pbit = 4; /* lh4,5 etc. */
290 pbit = 5; /* lh6,7 */
297 for (i = 0; i < NC; i++)
299 for (i = 0; i < np; i++)
301 output_pos = output_mask = 0;
306 /* ------------------------------------------------------------------------ */
308 encode_end_st1( /* void */ )
312 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
316 /* ------------------------------------------------------------------------ */
318 /* ------------------------------------------------------------------------ */
320 read_pt_len(nn, nbit, i_special)
330 for (i = 0; i < nn; i++)
332 for (i = 0; i < 256; i++)
338 c = bitbuf >> (16 - 3);
340 unsigned short mask = 1 << (16 - 4);
341 while (mask & bitbuf) {
346 fillbuf((c < 7) ? 3 : c - 3);
348 if (i == i_special) {
356 make_table(nn, pt_len, 8, pt_table);
360 /* ------------------------------------------------------------------------ */
362 read_c_len( /* void */ )
369 for (i = 0; i < NC; i++)
371 for (i = 0; i < 4096; i++)
376 c = pt_table[bitbuf >> (16 - 8)];
378 unsigned short mask = 1 << (16 - 9);
394 c = getbits(CBIT) + 20;
403 make_table(NC, c_len, 12, c_table);
407 /* ------------------------------------------------------------------------ */
409 decode_c_st1( /*void*/ )
411 unsigned short j, mask;
413 if (blocksize == 0) {
414 blocksize = getbits(16);
415 read_pt_len(NT, TBIT, 3);
417 read_pt_len(np, pbit, -1);
420 j = c_table[bitbuf >> 4];
425 mask = 1 << (16 - 1);
433 fillbuf(c_len[j] - 12);
438 /* ------------------------------------------------------------------------ */
440 decode_p_st1( /* void */ )
442 unsigned short j, mask;
444 j = pt_table[bitbuf >> (16 - 8)];
449 mask = 1 << (16 - 1);
457 fillbuf(pt_len[j] - 8);
460 j = (1 << (j - 1)) + getbits(j - 1);
464 /* ------------------------------------------------------------------------ */
466 decode_start_st1( /* void */ )
473 np = 17; /* for -lh7- */
481 if (dicbit <= 13) { /* 13 ... Changed N.Watazaki */
493 /* Local Variables: */