Coverage Report

Created: 2026-07-16 06:54

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