aboutsummaryrefslogtreecommitdiff
path: root/bitreader.cc
diff options
context:
space:
mode:
Diffstat (limited to 'bitreader.cc')
-rw-r--r--bitreader.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/bitreader.cc b/bitreader.cc
new file mode 100644
index 0000000..dd85f74
--- /dev/null
+++ b/bitreader.cc
@@ -0,0 +1,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();
+}