Blame


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