Coverage Report

Created: 2026-01-10 06:59

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 <qpdf/Util.hh>
5
6
#include <cstring>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
11
Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
12
137
    Pipeline(identifier, next)
13
137
{
14
137
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
137
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
137
{
20
137
    if (eod > 1) {
21
0
        return;
22
0
    }
23
12.4k
    for (size_t i = 0; i < len; ++i) {
24
12.3k
        switch (buf[i]) {
25
194
        case ' ':
26
438
        case '\f':
27
767
        case '\v':
28
1.74k
        case '\t':
29
1.94k
        case '\r':
30
2.29k
        case '\n':
31
2.29k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
2.29k
            continue;
34
12.3k
        }
35
10.0k
        if (eod > 1) {
36
1
            break;
37
10.0k
        } else if (eod == 1) {
38
14
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
14
            flush();
40
14
            eod = 2;
41
10.0k
        } else {
42
10.0k
            switch (buf[i]) {
43
15
            case '~':
44
15
                eod = 1;
45
15
                break;
46
47
216
            case 'z':
48
216
                if (pos != 0) {
49
2
                    throw std::runtime_error("unexpected z during base 85 decode");
50
2
                }
51
214
                unsigned char zeroes[4];
52
214
                memset(zeroes, '\0', 4);
53
214
                next()->write(zeroes, 4);
54
214
                break;
55
56
9.83k
            default:
57
9.83k
                if (buf[i] < 33 || buf[i] > 117) {
58
24
                    error = true;
59
24
                    throw std::runtime_error("character out of range during base 85 decode");
60
9.81k
                } else {
61
9.81k
                    this->inbuf[this->pos++] = buf[i];
62
9.81k
                    if (pos == 5) {
63
1.95k
                        flush();
64
1.95k
                    }
65
9.81k
                }
66
9.81k
                break;
67
10.0k
            }
68
10.0k
        }
69
10.0k
    }
70
137
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
2.05k
{
75
2.05k
    if (this->pos == 0) {
76
80
        return;
77
80
    }
78
1.97k
    unsigned long lval = 0;
79
11.8k
    for (int i = 0; i < 5; ++i) {
80
9.87k
        lval *= 85;
81
9.87k
        lval += (this->inbuf[i] - 33U);
82
9.87k
    }
83
84
1.97k
    unsigned char outbuf[4];
85
1.97k
    memset(outbuf, 0, 4);
86
9.87k
    for (int i = 3; i >= 0; --i) {
87
7.90k
        outbuf[i] = lval & 0xff;
88
7.90k
        lval >>= 8;
89
7.90k
    }
90
91
1.97k
    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
92
    // Reset before calling getNext()->write in case that throws an exception.
93
1.97k
    auto t = this->pos - 1;
94
1.97k
    this->pos = 0;
95
1.97k
    memset(this->inbuf, 117, 5);
96
97
1.97k
    next()->write(outbuf, t);
98
1.97k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
101
{
103
101
    if (error) {
104
0
        return;
105
0
    }
106
101
    flush();
107
101
    next()->finish();
108
101
}