1 | // SPDX-FileCopyrightText: 2008 by Jakub Stachowski <qbast@go2.pl> |
2 | // Huffdic decompressor based on Python code by Igor Skochinsky |
3 | // SPDX-License-Identifier: GPL-2.0-or-later |
4 | |
5 | #include <QByteArray> |
6 | #include <QtEndian> |
7 | |
8 | namespace Mobipocket |
9 | { |
10 | class BitReader |
11 | { |
12 | public: |
13 | BitReader(QByteArrayView d) |
14 | : len(d.size() * 8) |
15 | , data(d) |
16 | { |
17 | } |
18 | |
19 | quint32 read() |
20 | { |
21 | while (rEndPos - pos < 32) { |
22 | // r does not hold sufficient data, fetch some more |
23 | qint64 bytePos = rEndPos / 8; |
24 | |
25 | if (len - rEndPos >= 32) { |
26 | r <<= 32; |
27 | r |= qFromBigEndian<quint32>(src: data.constData() + bytePos); |
28 | rEndPos += 32; |
29 | break; |
30 | } else if (len - rEndPos > 0) { |
31 | r <<= 8; |
32 | quint8 d = data.at(n: bytePos); |
33 | r |= d; |
34 | rEndPos += 8; |
35 | } else { |
36 | r <<= 8; |
37 | rEndPos += 8; |
38 | } |
39 | } |
40 | |
41 | quint64 t = r << (64 - (rEndPos - pos)); |
42 | return t >> 32; |
43 | } |
44 | |
45 | bool eat(int n) |
46 | { |
47 | pos += n; |
48 | return pos <= len; |
49 | } |
50 | |
51 | int left() |
52 | { |
53 | return len - pos; |
54 | } |
55 | |
56 | private: |
57 | quint64 r = 0; |
58 | int pos = 0; |
59 | int len = 0; |
60 | int rEndPos = 0; //< position past the LSB of r |
61 | QByteArrayView data; |
62 | }; |
63 | } // namespace Mobipocket |
64 | |