Coverage Report

Created: 2025-11-09 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_ASCII85Decoder.cc
Line
Count
Source
1
#include <qpdf/Pl_ASCII85Decoder.hh>
2
3
#include <qpdf/QTC.hh>
4
#include <cstring>
5
#include <stdexcept>
6
7
Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
8
139
    Pipeline(identifier, next)
9
139
{
10
139
    if (!next) {
11
0
        throw std::logic_error("Attempt to create Pl_ASCII85Decoder with nullptr as next");
12
0
    }
13
139
}
14
15
void
16
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
17
139
{
18
139
    if (eod > 1) {
19
0
        return;
20
0
    }
21
6.15k
    for (size_t i = 0; i < len; ++i) {
22
6.06k
        switch (buf[i]) {
23
194
        case ' ':
24
395
        case '\f':
25
589
        case '\v':
26
810
        case '\t':
27
1.09k
        case '\r':
28
1.39k
        case '\n':
29
1.39k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
30
            // ignore whitespace
31
1.39k
            continue;
32
6.06k
        }
33
4.66k
        if (eod > 1) {
34
2
            break;
35
4.66k
        } else if (eod == 1) {
36
16
            if (buf[i] == '>') {
37
5
                flush();
38
5
                eod = 2;
39
11
            } else {
40
11
                throw std::runtime_error("broken end-of-data sequence in base 85 data");
41
11
            }
42
4.64k
        } else {
43
4.64k
            switch (buf[i]) {
44
17
            case '~':
45
17
                eod = 1;
46
17
                break;
47
48
197
            case 'z':
49
197
                if (pos != 0) {
50
2
                    throw std::runtime_error("unexpected z during base 85 decode");
51
195
                } else {
52
195
                    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
53
195
                    unsigned char zeroes[4];
54
195
                    memset(zeroes, '\0', 4);
55
195
                    next()->write(zeroes, 4);
56
195
                }
57
195
                break;
58
59
4.43k
            default:
60
4.43k
                if (buf[i] < 33 || buf[i] > 117) {
61
25
                    error = true;
62
25
                    throw std::runtime_error("character out of range during base 85 decode");
63
4.40k
                } else {
64
4.40k
                    this->inbuf[this->pos++] = buf[i];
65
4.40k
                    if (pos == 5) {
66
870
                        flush();
67
870
                    }
68
4.40k
                }
69
4.40k
                break;
70
4.64k
            }
71
4.64k
        }
72
4.66k
    }
73
139
}
74
75
void
76
Pl_ASCII85Decoder::flush()
77
976
{
78
976
    if (this->pos == 0) {
79
81
        QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
80
81
        return;
81
81
    }
82
895
    unsigned long lval = 0;
83
5.37k
    for (int i = 0; i < 5; ++i) {
84
4.47k
        lval *= 85;
85
4.47k
        lval += (this->inbuf[i] - 33U);
86
4.47k
    }
87
88
895
    unsigned char outbuf[4];
89
895
    memset(outbuf, 0, 4);
90
4.47k
    for (int i = 3; i >= 0; --i) {
91
3.58k
        outbuf[i] = lval & 0xff;
92
3.58k
        lval >>= 8;
93
3.58k
    }
94
95
895
    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
96
    // Reset before calling getNext()->write in case that throws an exception.
97
895
    auto t = this->pos - 1;
98
895
    this->pos = 0;
99
895
    memset(this->inbuf, 117, 5);
100
101
895
    next()->write(outbuf, t);
102
895
}
103
104
void
105
Pl_ASCII85Decoder::finish()
106
101
{
107
101
    if (error) {
108
0
        return;
109
0
    }
110
101
    flush();
111
101
    next()->finish();
112
101
}