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>
15 #if defined(__STDC__) || defined(NEWSOS)
19 /* ------------------------------------------------------------------------ */
20 unsigned short left[2 * NC - 1], right[2 * NC - 1];
21 unsigned char c_len[NC], pt_len[NPT];
22 unsigned short c_freq[2 * NC - 1], c_table[4096], c_code[NC], p_freq[2 * NP - 1],
23 pt_table[256], pt_code[NPT], t_freq[2 * NT - 1];
25 static unsigned char *buf;
26 static unsigned short bufsiz;
27 static unsigned short blocksize;
28 static unsigned short output_pos, output_mask;
31 /* ------------------------------------------------------------------------ */
33 /* ------------------------------------------------------------------------ */
35 count_t_freq(/*void*/)
39 for (i = 0; i < NT; i++)
42 while (n > 0 && c_len[n - 1] == 0)
49 while (i < n && c_len[i] == 0) {
57 else if (count == 19) {
68 /* ------------------------------------------------------------------------ */
70 write_pt_len(n, nbit, i_special)
77 while (n > 0 && pt_len[n - 1] == 0)
86 putbits(k - 3, USHRT_MAX << 1);
88 while (i < 6 && pt_len[i] == 0)
95 /* ------------------------------------------------------------------------ */
102 while (n > 0 && c_len[n - 1] == 0)
110 while (i < n && c_len[i] == 0) {
115 for (k = 0; k < count; k++)
116 putcode(pt_len[0], pt_code[0]);
118 else if (count <= 18) {
119 putcode(pt_len[1], pt_code[1]);
120 putbits(4, count - 3);
122 else if (count == 19) {
123 putcode(pt_len[0], pt_code[0]);
124 putcode(pt_len[1], pt_code[1]);
128 putcode(pt_len[2], pt_code[2]);
129 putbits(CBIT, count - 20);
133 putcode(pt_len[k + 2], pt_code[k + 2]);
137 /* ------------------------------------------------------------------------ */
142 putcode(c_len[c], c_code[c]);
145 /* ------------------------------------------------------------------------ */
158 putcode(pt_len[c], pt_code[c]);
163 /* ------------------------------------------------------------------------ */
165 send_block( /* void */ )
168 unsigned short i, k, root, pos, size;
170 root = make_tree(NC, c_freq, c_len, c_code);
175 root = make_tree(NT, t_freq, pt_len, pt_code);
177 write_pt_len(NT, TBIT, 3);
189 root = make_tree(np, p_freq, pt_len, pt_code);
191 write_pt_len(np, pbit, -1);
198 for (i = 0; i < size; i++) {
199 if (i % CHAR_BIT == 0)
203 if (flags & (1 << (CHAR_BIT - 1))) {
204 encode_c(buf[pos++] + (1 << CHAR_BIT));
205 k = buf[pos++] << CHAR_BIT;
209 encode_c(buf[pos++]);
213 for (i = 0; i < NC; i++)
215 for (i = 0; i < np; i++)
219 /* ------------------------------------------------------------------------ */
225 static unsigned short cpos;
228 if (output_mask == 0) {
229 output_mask = 1 << (CHAR_BIT - 1);
230 if (output_pos >= bufsiz - 3 * CHAR_BIT) {
239 buf[output_pos++] = (unsigned char) c;
241 if (c >= (1 << CHAR_BIT)) {
242 buf[cpos] |= output_mask;
243 buf[output_pos++] = (unsigned char) (p >> CHAR_BIT);
244 buf[output_pos++] = (unsigned char) p;
254 /* ------------------------------------------------------------------------ */
256 alloc_buf( /* void */ )
258 bufsiz = 16 * 1024 *2; /* 65408U; */ /* t.okamoto */
259 while ((buf = (unsigned char *) malloc(bufsiz)) == NULL) {
260 bufsiz = (bufsiz / 10) * 9;
261 if (bufsiz < 4 * 1024)
267 /* ------------------------------------------------------------------------ */
269 encode_start_st1( /* void */ )
273 if (dicbit <= (MAX_DICBIT - 2)) {
274 pbit = 4; /* lh4,5 etc. */
281 for (i = 0; i < NC; i++)
283 for (i = 0; i < np; i++)
285 output_pos = output_mask = 0;
290 /* ------------------------------------------------------------------------ */
292 encode_end_st1( /* void */ )
296 putbits(CHAR_BIT - 1, 0); /* flush remaining bits */
300 /* ------------------------------------------------------------------------ */
302 /* ------------------------------------------------------------------------ */
304 read_pt_len(nn, nbit, i_special)
314 for (i = 0; i < nn; i++)
316 for (i = 0; i < 256; i++)
322 c = bitbuf >> (16 - 3);
324 unsigned short mask = 1 << (16 - 4);
325 while (mask & bitbuf) {
330 fillbuf((c < 7) ? 3 : c - 3);
332 if (i == i_special) {
340 make_table(nn, pt_len, 8, pt_table);
344 /* ------------------------------------------------------------------------ */
346 read_c_len( /* void */ )
353 for (i = 0; i < NC; i++)
355 for (i = 0; i < 4096; i++)
360 c = pt_table[bitbuf >> (16 - 8)];
362 unsigned short mask = 1 << (16 - 9);
378 c = getbits(CBIT) + 20;
387 make_table(NC, c_len, 12, c_table);
391 /* ------------------------------------------------------------------------ */
393 decode_c_st1( /*void*/ )
395 unsigned short j, mask;
397 if (blocksize == 0) {
398 blocksize = getbits(16);
399 read_pt_len(NT, TBIT, 3);
401 read_pt_len(np, pbit, -1);
404 j = c_table[bitbuf >> 4];
409 mask = 1 << (16 - 1);
417 fillbuf(c_len[j] - 12);
422 /* ------------------------------------------------------------------------ */
424 decode_p_st1( /* void */ )
426 unsigned short j, mask;
428 j = pt_table[bitbuf >> (16 - 8)];
433 mask = 1 << (16 - 1);
441 fillbuf(pt_len[j] - 8);
444 j = (1 << (j - 1)) + getbits(j - 1);
448 /* ------------------------------------------------------------------------ */
450 decode_start_st1( /* void */ )
452 if (dicbit <= (MAX_DICBIT - 2)) { /* 13 ... Changed N.Watazaki */