blob: ec37e0d6a6a05cc2cb7b2cc4928ad7719fc227d4 [file] [log] [blame]
rsc8a3b2ce2004-04-24 17:05:43 +00001#include <u.h>
2#include <libc.h>
3#include <bio.h>
4#include "sky.h"
5
6static int hufvals[] = {
7 1, 1, 1, 1, 1, 1, 1, 1,
8 2, 2, 2, 2, 2, 2, 2, 2,
9 4, 4, 4, 4, 4, 4, 4, 4,
10 8, 8, 8, 8, 8, 8, 8, 8,
11 3, 3, 3, 3, 5, 5, 5, 5,
12 10, 10, 10, 10, 12, 12, 12, 12,
13 15, 15, 15, 15, 6, 6, 7, 7,
rsccbeb0b22006-04-01 19:24:03 +000014 9, 9, 11, 11, 13, 13, 0, 14
rsc8a3b2ce2004-04-24 17:05:43 +000015};
16
17static int huflens[] = {
18 3, 3, 3, 3, 3, 3, 3, 3,
19 3, 3, 3, 3, 3, 3, 3, 3,
20 3, 3, 3, 3, 3, 3, 3, 3,
21 3, 3, 3, 3, 3, 3, 3, 3,
22 4, 4, 4, 4, 4, 4, 4, 4,
23 4, 4, 4, 4, 4, 4, 4, 4,
24 4, 4, 4, 4, 5, 5, 5, 5,
rsccbeb0b22006-04-01 19:24:03 +000025 5, 5, 5, 5, 5, 5, 6, 6
rsc8a3b2ce2004-04-24 17:05:43 +000026};
27
28static int buffer;
29static int bits_to_go; /* Number of bits still in buffer */
30
31void
32start_inputing_bits(void)
33{
34 bits_to_go = 0;
35}
36
37int
38input_huffman(Biobuf *infile)
39{
40 int c;
41
42 if(bits_to_go < 6) {
43 c = Bgetc(infile);
44 if(c < 0) {
45 fprint(2, "input_huffman: unexpected EOF\n");
46 exits("format");
47 }
48 buffer = (buffer<<8) | c;
49 bits_to_go += 8;
50 }
51 c = (buffer >> (bits_to_go-6)) & 0x3f;
52 bits_to_go -= huflens[c];
53 return hufvals[c];
54}
55
56int
57input_nybble(Biobuf *infile)
58{
59 int c;
60
61 if(bits_to_go < 4) {
62 c = Bgetc(infile);
63 if(c < 0){
64 fprint(2, "input_nybble: unexpected EOF\n");
65 exits("format");
66 }
67 buffer = (buffer<<8) | c;
68 bits_to_go += 8;
69 }
70
71 /*
72 * pick off the first 4 bits
73 */
74 bits_to_go -= 4;
75 return (buffer>>bits_to_go) & 0x0f;
76}