Coverage Report

Created: 2026-01-25 06:25

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