Coverage Report

Created: 2025-10-12 07:05

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 <cctype>
5
#include <stdexcept>
6
7
using namespace std::literals;
8
9
Pl_ASCIIHexDecoder::Pl_ASCIIHexDecoder(char const* identifier, Pipeline* next) :
10
542
    Pipeline(identifier, next)
11
542
{
12
542
    if (!next) {
13
0
        throw std::logic_error("Attempt to create Pl_ASCIIHexDecoder with nullptr as next");
14
0
    }
15
542
}
16
17
void
18
Pl_ASCIIHexDecoder::write(unsigned char const* buf, size_t len)
19
895
{
20
895
    if (eod) {
21
227
        return;
22
227
    }
23
5.67k
    for (size_t i = 0; i < len; ++i) {
24
5.18k
        char ch = static_cast<char>(toupper(buf[i]));
25
5.18k
        switch (ch) {
26
465
        case ' ':
27
666
        case '\f':
28
1.00k
        case '\v':
29
1.32k
        case '\t':
30
1.66k
        case '\r':
31
2.06k
        case '\n':
32
2.06k
            QTC::TC("libtests", "Pl_ASCIIHexDecoder ignore space");
33
            // ignore whitespace
34
2.06k
            break;
35
36
36
        case '>':
37
36
            eod = true;
38
36
            flush();
39
36
            break;
40
41
3.08k
        default:
42
3.08k
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
43
2.94k
                inbuf[pos++] = ch;
44
2.94k
                if (pos == 2) {
45
1.40k
                    flush();
46
1.40k
                }
47
2.94k
            } else {
48
133
                char t[2];
49
133
                t[0] = ch;
50
133
                t[1] = 0;
51
133
                throw std::runtime_error("character out of range during base Hex decode: "s + t);
52
133
            }
53
2.94k
            break;
54
5.18k
        }
55
5.04k
        if (eod) {
56
36
            break;
57
36
        }
58
5.04k
    }
59
668
}
60
61
void
62
Pl_ASCIIHexDecoder::flush()
63
1.92k
{
64
1.92k
    if (pos == 0) {
65
391
        QTC::TC("libtests", "Pl_ASCIIHexDecoder no-op flush");
66
391
        return;
67
391
    }
68
1.53k
    int b[2];
69
4.59k
    for (int i = 0; i < 2; ++i) {
70
3.06k
        if (inbuf[i] >= 'A') {
71
374
            b[i] = inbuf[i] - 'A' + 10;
72
2.69k
        } else {
73
2.69k
            b[i] = inbuf[i] - '0';
74
2.69k
        }
75
3.06k
    }
76
1.53k
    auto ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
77
78
1.53k
    QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush", (pos == 2) ? 0 : 1);
79
    // Reset before calling getNext()->write in case that throws an exception.
80
1.53k
    pos = 0;
81
1.53k
    inbuf[0] = '0';
82
1.53k
    inbuf[1] = '0';
83
1.53k
    inbuf[2] = '\0';
84
85
1.53k
    next()->write(&ch, 1);
86
1.53k
}
87
88
void
89
Pl_ASCIIHexDecoder::finish()
90
478
{
91
478
    flush();
92
478
    next()->finish();
93
478
}