aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/lzp/bit.c
blob: 96783575bce33e33e31ea3a5c69cf954fb7ce6e6 (plain) (blame)
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
#include "bit.h"

// Bit I/O
//

const unsigned char* inPtr = 0;
int inBytes = 0;
unsigned char* outPtr = 0;
int outBytes = 0;

int bit_buf;
int bit_count;

void init_bits() {

	bit_count = bit_buf=0;

}

void put_bits(int n, int x) {

	bit_buf |= x<<bit_count;
	bit_count += n;

	while(bit_count >= 8) {

		*outPtr = bit_buf;
		outPtr++;
        outBytes++;

		bit_buf >>= 8;
		bit_count -= 8;

	}

}

void flush_bits() {

	put_bits(7, 0);
	bit_count = bit_buf = 0;

}

int get_bits(int n) {

	int x;

	while(bit_count < n) {

		bit_buf |= *inPtr<<bit_count;
		inPtr++;
		inBytes++;

		bit_count += 8;

	}

	x = bit_buf&((1<<n)-1);
	bit_buf >>= n;
	bit_count -= n;

	return(x);

}