Coverage Report

Created: 2025-11-24 06:47

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