Coverage Report

Created: 2026-01-17 06:30

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