Coverage Report

Created: 2026-03-12 07:01

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