Coverage Report

Created: 2026-01-09 06:28

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