Coverage Report

Created: 2026-10-17 06:57

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