Coverage Report

Created: 2025-11-11 07:02

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