Coverage Report

Created: 2026-10-17 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_ASCIIHexDecoder.cc
Line
Count
Source
1
#include <qpdf/Pl_ASCIIHexDecoder.hh>
2
3
#include <qpdf/QTC.hh>
4
#include <qpdf/Util.hh>
5
6
#include <cctype>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
using namespace std::literals;
11
12
Pl_ASCIIHexDecoder::Pl_ASCIIHexDecoder(char const* identifier, Pipeline* next) :
13
2.80k
    Pipeline(identifier, next)
14
2.80k
{
15
2.80k
    util::assertion(next, "Attempt to create Pl_ASCIIHexDecoder with nullptr as next");
16
2.80k
}
17
18
void
19
Pl_ASCIIHexDecoder::write(unsigned char const* buf, size_t len)
20
3.15k
{
21
3.15k
    if (eod) {
22
246
        return;
23
246
    }
24
133k
    for (size_t i = 0; i < len; ++i) {
25
132k
        char ch = static_cast<char>(toupper(buf[i]));
26
132k
        switch (ch) {
27
456
        case ' ':
28
763
        case '\f':
29
1.06k
        case '\v':
30
1.86k
        case '\t':
31
2.57k
        case '\r':
32
3.44k
        case '\n':
33
3.44k
            QTC::TC("libtests", "Pl_ASCIIHexDecoder ignore space");
34
            // ignore whitespace
35
3.44k
            break;
36
37
154
        case '>':
38
154
            eod = true;
39
154
            flush();
40
154
            break;
41
42
128k
        default:
43
128k
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
44
127k
                inbuf[pos++] = ch;
45
127k
                if (pos == 2) {
46
63.1k
                    flush();
47
63.1k
                }
48
127k
            } else {
49
1.07k
                char t[2];
50
1.07k
                t[0] = ch;
51
1.07k
                t[1] = 0;
52
1.07k
                throw std::runtime_error("character out of range during base Hex decode: "s + t);
53
1.07k
            }
54
127k
            break;
55
132k
        }
56
131k
        if (eod) {
57
152
            break;
58
152
        }
59
131k
    }
60
2.90k
}
61
62
void
63
Pl_ASCIIHexDecoder::flush()
64
65.9k
{
65
65.9k
    if (pos == 0) {
66
1.56k
        QTC::TC("libtests", "Pl_ASCIIHexDecoder no-op flush");
67
1.56k
        return;
68
1.56k
    }
69
64.3k
    int b[2];
70
193k
    for (int i = 0; i < 2; ++i) {
71
128k
        if (inbuf[i] >= 'A') {
72
26.4k
            b[i] = inbuf[i] - 'A' + 10;
73
102k
        } else {
74
102k
            b[i] = inbuf[i] - '0';
75
102k
        }
76
128k
    }
77
64.3k
    auto ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
78
79
64.3k
    QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush", (pos == 2) ? 0 : 1);
80
    // Reset before calling getNext()->write in case that throws an exception.
81
64.3k
    pos = 0;
82
64.3k
    inbuf[0] = '0';
83
64.3k
    inbuf[1] = '0';
84
64.3k
    inbuf[2] = '\0';
85
86
64.3k
    next()->write(&ch, 1);
87
64.3k
}
88
89
void
90
Pl_ASCIIHexDecoder::finish()
91
2.62k
{
92
2.62k
    flush();
93
2.62k
    next()->finish();
94
2.62k
}