Coverage Report

Created: 2026-02-26 06:36

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