aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/scat/bitinput.c
blob: ec37e0d6a6a05cc2cb7b2cc4928ad7719fc227d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include	<u.h>
#include	<libc.h>
#include	<bio.h>
#include	"sky.h"

static int hufvals[] = {
	 1,  1,  1,  1,  1,  1,  1,  1,
	 2,  2,  2,  2,  2,  2,  2,  2,
	 4,  4,  4,  4,  4,  4,  4,  4,
	 8,  8,  8,  8,  8,  8,  8,  8,
	 3,  3,  3,  3,  5,  5,  5,  5,
	10, 10, 10, 10, 12, 12, 12, 12,
	15, 15, 15, 15,  6,  6,  7,  7,
	 9,  9, 11, 11, 13, 13,  0, 14
};

static int huflens[] = {
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	3, 3, 3, 3, 3, 3, 3, 3,
	4, 4, 4, 4, 4, 4, 4, 4,
	4, 4, 4, 4, 4, 4, 4, 4,
	4, 4, 4, 4, 5, 5, 5, 5,
	5, 5, 5, 5, 5, 5, 6, 6
};

static	int	buffer;
static	int	bits_to_go;		/* Number of bits still in buffer */

void
start_inputing_bits(void)
{
	bits_to_go = 0;
}

int
input_huffman(Biobuf *infile)
{
	int c;

	if(bits_to_go < 6) {
		c = Bgetc(infile);
		if(c < 0) {
			fprint(2, "input_huffman: unexpected EOF\n");
			exits("format");
		}
		buffer = (buffer<<8) | c;
		bits_to_go += 8;
	}
	c = (buffer >> (bits_to_go-6)) & 0x3f;
	bits_to_go -= huflens[c];
	return hufvals[c];
}

int
input_nybble(Biobuf *infile)
{
	int c;

	if(bits_to_go < 4) {
		c = Bgetc(infile);
		if(c < 0){
			fprint(2, "input_nybble: unexpected EOF\n");
			exits("format");
		}
		buffer = (buffer<<8) | c;
		bits_to_go += 8;
	}

	/*
	 * pick off the first 4 bits
	 */
	bits_to_go -= 4;
	return (buffer>>bits_to_go) & 0x0f;
}