-
Notifications
You must be signed in to change notification settings - Fork 15
/
bitstream.hh
152 lines (146 loc) · 2.76 KB
/
bitstream.hh
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Bitwise stream container
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace CODE {
template <int SIZE>
class Bitstream
{
public:
static const int BITS = SIZE;
static const int BYTES = (SIZE + 7) / 8;
private:
uint8_t buf_[BYTES];
int pos_ = 0;
public:
void seek(int pos)
{
pos_ = pos;
}
int tell()
{
return pos_;
}
uint8_t *data()
{
return buf_;
}
void reset()
{
pos_ = 0;
for (int i = 0; i < BYTES; ++i)
buf_[i] = 0;
}
void putbit(bool b)
{
int bit = pos_ % 8;
int byte = pos_ / 8;
++pos_;
uint8_t mask = 1 << bit;
uint8_t tmp = ~mask & buf_[byte];
buf_[byte] = tmp | b << bit;
}
bool getbit()
{
int bit = pos_ % 8;
int byte = pos_ / 8;
++pos_;
return (buf_[byte] >> bit) & 1;
}
void putbyte(const uint8_t b)
{
int bit = pos_ % 8;
int byte = pos_ / 8;
pos_ += 8;
if (bit) {
uint8_t mask = (1 << bit) - 1;
uint8_t lsb = mask & buf_[byte];
buf_[byte++] = lsb | b << bit;
uint8_t msb = ~mask & buf_[byte];
buf_[byte] = msb | b >> (8 - bit);
} else {
buf_[byte] = b;
}
}
uint8_t getbyte()
{
int bit = pos_ % 8;
int byte = pos_ / 8;
pos_ += 8;
if (bit) {
uint8_t lsb = buf_[byte++] >> bit;
uint8_t msb = buf_[byte] << (8 - bit);
return msb | lsb;
}
return buf_[byte];
}
void writebytes(const uint8_t *buf, int len)
{
int bit = pos_ % 8;
int byte = pos_ / 8;
pos_ += 8 * len;
if (bit) {
uint8_t mask = (1 << bit) - 1;
for (int i = 0; i < len; ++i) {
uint8_t lsb = mask & buf_[byte];
buf_[byte++] = lsb | buf[i] << bit;
uint8_t msb = ~mask & buf_[byte];
buf_[byte] = msb | buf[i] >> (8 - bit);
}
} else {
for (int i = 0; i < len; ++i)
buf_[byte++] = buf[i];
}
}
void readbytes(uint8_t *buf, int len)
{
int bit = pos_ % 8;
int byte = pos_ / 8;
pos_ += 8 * len;
if (bit) {
for (int i = 0; i < len; ++i) {
uint8_t lsb = buf_[byte++] >> bit;
uint8_t msb = buf_[byte] << (8 - bit);
buf[i] = msb | lsb;
}
} else {
for (int i = 0; i < len; ++i)
buf[i] = buf_[byte++];
}
}
template <typename TYPE>
void writebits(TYPE b, int num)
{
for (int sum = 0; num;) {
int bit = pos_ % 8;
int byte = pos_ / 8;
int copy = std::min(8 - bit, num);
uint8_t mask = (1 << copy) - 1;
uint8_t src = (mask & (b >> sum)) << bit;
uint8_t dst = ~(mask << bit) & buf_[byte];
buf_[byte++] = dst | src;
pos_ += copy;
num -= copy;
sum += copy;
}
}
template <typename TYPE>
void readbits(TYPE *b, int num)
{
TYPE a = 0;
for (int sum = 0; num;) {
int bit = pos_ % 8;
int byte = pos_ / 8;
int copy = std::min(8 - bit, num);
uint8_t mask = (1 << copy) - 1;
TYPE tmp = mask & (buf_[byte++] >> bit);
a |= tmp << sum;
pos_ += copy;
num -= copy;
sum += copy;
}
*b = a;
}
};
}