blob: 7de48730a204052336f15720e0c40a798747a591 (
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
|
#include "bitstream.h"
namespace NBitStream {
union TMix {
unsigned long long ull = 0;
uint8_t bytes[8];
};
TBitStream::TBitStream(const char* buf, int size)
: Buf(buf, buf+size)
{}
TBitStream::TBitStream()
{}
void TBitStream::Write(unsigned long long val, int n) {
if (n > 23 || n < 0)
abort();
const int bitsLeft = Buf.size() * 8 - BitsUsed;
const int bitsReq = n - bitsLeft;
const int bytesPos = BitsUsed / 8;
const int overlap = BitsUsed % 8;
if (bitsReq > 0) {
Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
}
TMix t;
t.ull = val;
t.ull = (t.ull << (64 - n) >> overlap);
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
Buf[bytesPos+i] |= t.bytes[7-i];
}
BitsUsed += n;
}
unsigned long long TBitStream::Read(int n) {
if (n >23 || n < 0)
abort();
const int bytesPos = ReadPos / 8;
const int overlap = ReadPos % 8;
TMix t;
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
t.bytes[7-i] = (uint8_t)Buf[bytesPos+i];
}
t.ull = (t.ull << overlap >> (64 - n));
ReadPos += n;
return t.ull;
}
unsigned long long TBitStream::GetSizeInBits() const {
return BitsUsed;
}
}
|