Coverage Report

Created: 2026-01-16 06:32

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