aboutsummaryrefslogtreecommitdiff
path: root/bitreader.cc
blob: dd85f74d239c643a8dee96fe66ac944773844a5b (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
#include "bitreader.h"

BitReader::BitReader(QIODevice *device)
	: child(device), buf(0), avail(0)
{
}

BitReader::~BitReader()
{
}

quint64 BitReader::readBits(int n)
{
	quint64 x = peekBits(n);

	Q_ASSERT(avail >= n);
	buf -= x << (avail - n);
	avail -= n;

	return x;
}

quint64 BitReader::peekBits(int n)
{
	while (n > avail) {
		char c;
		child->getChar(&c);
		buf = (buf << 8) | (quint8)(c);
		avail += 8;
	}
	quint64 x = buf >> (avail - n);

	return x;
}

void BitReader::skipUntilNextByte()
{
	int skip = avail % 8;
	readBits(skip);
}

bool BitReader::atEnd()
{
	return avail == 0 && child->atEnd();
}