/src/qpdf/libqpdf/BitStream.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/BitStream.hh> |
2 | | |
3 | | #include <qpdf/QIntC.hh> |
4 | | |
5 | | // See comments in bits_functions.hh |
6 | | #define BITS_READ 1 |
7 | | #include <qpdf/bits_functions.hh> |
8 | | |
9 | | BitStream::BitStream(unsigned char const* p, size_t nbytes) : |
10 | 55.7k | start(p), |
11 | 55.7k | nbytes(nbytes) |
12 | 55.7k | { |
13 | 55.7k | reset(); |
14 | 55.7k | } |
15 | | |
16 | | void |
17 | | BitStream::reset() |
18 | 55.7k | { |
19 | 55.7k | p = start; |
20 | 55.7k | bit_offset = 7; |
21 | 55.7k | if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8) { |
22 | 0 | throw std::runtime_error("array too large for bitstream"); |
23 | 0 | } |
24 | 55.7k | bits_available = 8 * nbytes; |
25 | 55.7k | } |
26 | | |
27 | | unsigned long long |
28 | | BitStream::getBits(size_t nbits) |
29 | 0 | { |
30 | 0 | return read_bits(this->p, this->bit_offset, this->bits_available, nbits); |
31 | 0 | } |
32 | | |
33 | | long long |
34 | | BitStream::getBitsSigned(size_t nbits) |
35 | 156k | { |
36 | 156k | unsigned long long bits = read_bits(this->p, this->bit_offset, this->bits_available, nbits); |
37 | 156k | long long result = 0; |
38 | 156k | if (static_cast<long long>(bits) > 1LL << (nbits - 1)) { |
39 | 20.4k | result = static_cast<long long>(bits - (1ULL << nbits)); |
40 | 135k | } else { |
41 | 135k | result = static_cast<long long>(bits); |
42 | 135k | } |
43 | 156k | return result; |
44 | 156k | } |
45 | | |
46 | | int |
47 | | BitStream::getBitsInt(size_t nbits) |
48 | 0 | { |
49 | 0 | return static_cast<int>( |
50 | | // line-break |
51 | 0 | QIntC::to_uint(read_bits(this->p, this->bit_offset, this->bits_available, nbits))); |
52 | 0 | } |
53 | | |
54 | | void |
55 | | BitStream::skipToNextByte() |
56 | 0 | { |
57 | 0 | if (bit_offset != 7) { |
58 | 0 | size_t bits_to_skip = bit_offset + 1; |
59 | 0 | if (bits_available < bits_to_skip) { |
60 | 0 | throw std::logic_error("INTERNAL ERROR: overflow skipping to next byte in bitstream"); |
61 | 0 | } |
62 | 0 | bit_offset = 7; |
63 | 0 | ++p; |
64 | 0 | bits_available -= bits_to_skip; |
65 | 0 | } |
66 | 0 | } |