aboutsummaryrefslogtreecommitdiffstats
path: root/src/bitstream/bitstream.cpp
blob: e8f1857daa067db1fbaae11e7f12aad9d4c6c8e0 (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
66
67
68
69
70
71
72
73
74
#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 (overlap || 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];

  //      std::cout << "bufPos: "<< bytesPos+i << " buf: " << (int)Buf[bytesPos+i] << std::endl;
	}

    BitsUsed += n;
}
/*
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 (overlap || bitsReq >= 0) {
        Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
    }
    TMix t;
    t.ull   = (val << (64 - n)) >> overlap;
    *(unsigned long long*)&Buf[bytesPos-8] |= t.ull;
    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;
}

}