Coverage Report

Created: 2026-09-14 06:14

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
1.84k
    Pipeline(identifier, next)
14
1.84k
{
15
1.84k
    util::assertion(next, "Attempt to create Pl_ASCIIHexDecoder with nullptr as next");
16
1.84k
}
17
18
void
19
Pl_ASCIIHexDecoder::write(unsigned char const* buf, size_t len)
20
2.38k
{
21
2.38k
    if (eod) {
22
256
        return;
23
256
    }
24
285k
    for (size_t i = 0; i < len; ++i) {
25
285k
        char ch = static_cast<char>(toupper(buf[i]));
26
285k
        switch (ch) {
27
307
        case ' ':
28
583
        case '\f':
29
878
        case '\v':
30
1.11k
        case '\t':
31
1.37k
        case '\r':
32
5.78k
        case '\n':
33
5.78k
            QTC::TC("libtests", "Pl_ASCIIHexDecoder ignore space");
34
            // ignore whitespace
35
5.78k
            break;
36
37
212
        case '>':
38
212
            eod = true;
39
212
            flush();
40
212
            break;
41
42
279k
        default:
43
279k
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
44
277k
                inbuf[pos++] = ch;
45
277k
                if (pos == 2) {
46
138k
                    flush();
47
138k
                }
48
277k
            } else {
49
1.36k
                char t[2];
50
1.36k
                t[0] = ch;
51
1.36k
                t[1] = 0;
52
1.36k
                throw std::runtime_error("character out of range during base Hex decode: "s + t);
53
1.36k
            }
54
277k
            break;
55
285k
        }
56
283k
        if (eod) {
57
211
            break;
58
211
        }
59
283k
    }
60
2.12k
}
61
62
void
63
Pl_ASCIIHexDecoder::flush()
64
140k
{
65
140k
    if (pos == 0) {
66
1.04k
        QTC::TC("libtests", "Pl_ASCIIHexDecoder no-op flush");
67
1.04k
        return;
68
1.04k
    }
69
139k
    int b[2];
70
417k
    for (int i = 0; i < 2; ++i) {
71
278k
        if (inbuf[i] >= 'A') {
72
112k
            b[i] = inbuf[i] - 'A' + 10;
73
165k
        } else {
74
165k
            b[i] = inbuf[i] - '0';
75
165k
        }
76
278k
    }
77
139k
    auto ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
78
79
139k
    QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush", (pos == 2) ? 0 : 1);
80
    // Reset before calling getNext()->write in case that throws an exception.
81
139k
    pos = 0;
82
139k
    inbuf[0] = '0';
83
139k
    inbuf[1] = '0';
84
139k
    inbuf[2] = '\0';
85
86
139k
    next()->write(&ch, 1);
87
139k
}
88
89
void
90
Pl_ASCIIHexDecoder::finish()
91
1.53k
{
92
1.53k
    flush();
93
1.53k
    next()->finish();
94
1.53k
}