1 /* ------------------------------------------------------------------------ */
3 /* huf.c -- new static Huffman */
5 /* Modified Nobutaka Watazaki */
7 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
8 /* ------------------------------------------------------------------------ */
12 #include <sys/param.h>
18 extern char *malloc ();
21 /* ------------------------------------------------------------------------ */
22 unsigned short left[2 * NC - 1], right[2 * NC - 1];
23 unsigned char c_len[NC], pt_len[NPT];
24 unsigned short c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
25 pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
27 static unsigned char *buf;
28 static unsigned short bufsiz;
29 static unsigned short blocksize;
30 static unsigned short output_pos, output_mask;
33 /* ------------------------------------------------------------------------ */
35 /* ------------------------------------------------------------------------ */
37 count_t_freq(/*void*/)
41 for (i = 0; i < NT; i++)
44 while (n > 0 && c_len[n - 1] == 0)
51 while (i < n && c_len[i] == 0) {
59 else if (count == 19) {
70 /* ------------------------------------------------------------------------ */
72 write_pt_len(n, nbit, i_special)
79 while (n > 0 && pt_len[n - 1] == 0)
88 putbits(k - 3, USHRT_MAX << 1);
90 while (i < 6 && pt_len[i] == 0)
97 /* ------------------------------------------------------------------------ */
101 short i, k, n, count;
104 while (n > 0 && c_len[n - 1] == 0)
112 while (i < n && c_len[i] == 0) {
117 for (k = 0; k < count; k++)
118 putcode(pt_len[0], pt_code[0]);
120 else if (count <= 18) {
121 putcode(pt_len[1], pt_code[1]);
122 putbits(4, count - 3);
124 else if (count == 19) {
125 putcode(pt_len[0], pt_code[0]);
126 putcode(pt_len[1], pt_code[1]);
130 putcode(pt_len[2], pt_code[2]);
131 putbits(CBIT, count - 20);
135 putcode(pt_len[k + 2], pt_code[k + 2]);
139 /* ------------------------------------------------------------------------ */
144 putcode(c_len[c], c_code[c]);
147 /* ------------------------------------------------------------------------ */
160 putcode(pt_len[c], pt_code[c]);
165 /* ------------------------------------------------------------------------ */
167 send_block( /* void */ )
170 unsigned short i, k, root, pos, size;
172 root = make_tree(NC, c_freq, c_len, c_code);
177 root = make_tree(NT, t_freq, pt_len, pt_code);
179 write_pt_len(NT, TBIT, 3);
191 root = make_tree(np, p_freq, pt_len, pt_code);
193 write_pt_len(np, pbit, -1);
200 for (i = 0; i < size; i++) {
201 if (i % CHAR_BIT == 0)
205 if (flags & (1 << (CHAR_BIT - 1))) {
206 encode_c(buf[pos++] + (1 << CHAR_BIT));
207 k = buf[pos++] << CHAR_BIT;
211 encode_c(buf[pos++]);
215 for (i = 0; i < NC; i++)
217 for (i = 0; i < np; i++)
221 /* ------------------------------------------------------------------------ */
227 static unsigned short cpos;
230 if (output_mask == 0) {
231 output_mask = 1 << (CHAR_BIT - 1);
232 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
241 buf[output_pos++] = (unsigned char) c;
243 if (c >= (1 << CHAR_BIT)) {
244 buf[cpos] |= output_mask;
245 buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
246 buf[output_pos++] = (unsigned char) p;
256 /* ------------------------------------------------------------------------ */
258 alloc_buf( /* void */ )
260 bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
261 while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
262 bufsiz = (bufsiz / 10) * 9;
263 if (bufsiz < 4 * 1024)
269 /* ------------------------------------------------------------------------ */
271 encode_start_st1( /* void */ )
275 if (dicbit <= (MAX_DICBIT - 2)) {
276 pbit = 4; /* lh4,5 etc. */
283 for (i = 0; i < NC; i++)
285 for (i = 0; i < np; i++)
287 output_pos = output_mask = 0;
292 /* ------------------------------------------------------------------------ */
294 encode_end_st1( /* void */ )
298 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
302 /* ------------------------------------------------------------------------ */
304 /* ------------------------------------------------------------------------ */
306 read_pt_len(nn, nbit, i_special)
316 for (i = 0; i < nn; i++)
318 for (i = 0; i < 256; i++)
324 c = bitbuf >> (16 - 3);
326 unsigned short mask = 1 << (16 - 4);
327 while (mask & bitbuf) {
332 fillbuf((c < 7) ? 3 : c - 3);
334 if (i == i_special) {
342 make_table(nn, pt_len, 8, pt_table);
346 /* ------------------------------------------------------------------------ */
348 read_c_len( /* void */ )
355 for (i = 0; i < NC; i++)
357 for (i = 0; i < 4096; i++)
362 c = pt_table[bitbuf >> (16 - 8)];
364 unsigned short mask = 1 << (16 - 9);
380 c = getbits(CBIT) + 20;
389 make_table(NC, c_len, 12, c_table);
393 /* ------------------------------------------------------------------------ */
395 decode_c_st1( /*void*/ )
397 unsigned short j, mask;
399 if (blocksize == 0) {
400 blocksize = getbits(16);
401 read_pt_len(NT, TBIT, 3);
403 read_pt_len(np, pbit, -1);
406 j = c_table[bitbuf >> 4];
411 mask = 1 << (16 - 1);
419 fillbuf(c_len[j] - 12);
424 /* ------------------------------------------------------------------------ */
426 decode_p_st1( /* void */ )
428 unsigned short j, mask;
430 j = pt_table[bitbuf >> (16 - 8)];
435 mask = 1 << (16 - 1);
443 fillbuf(pt_len[j] - 8);
446 j = (1 << (j - 1)) + getbits(j - 1);
450 /* ------------------------------------------------------------------------ */
452 decode_start_st1( /* void */ )
454 if (dicbit <= (MAX_DICBIT - 2)) { /* 13 ... Changed N.Watazaki */
464 /* Local Variables: */