Coverage Report

Created: 2026-01-16 06:33

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
2.83k
    Pipeline(identifier, next)
13
2.83k
{
14
2.83k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
2.83k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
3.21k
{
20
3.21k
    if (eod > 1) {
21
206
        return;
22
206
    }
23
1.09M
    for (size_t i = 0; i < len; ++i) {
24
1.09M
        switch (buf[i]) {
25
1.06k
        case ' ':
26
1.34k
        case '\f':
27
1.55k
        case '\v':
28
2.29k
        case '\t':
29
3.48k
        case '\r':
30
19.1k
        case '\n':
31
19.1k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
19.1k
            continue;
34
1.09M
        }
35
1.07M
        if (eod > 1) {
36
263
            break;
37
1.07M
        } else if (eod == 1) {
38
1.54k
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
1.54k
            flush();
40
1.54k
            eod = 2;
41
1.07M
        } else {
42
1.07M
            switch (buf[i]) {
43
1.64k
            case '~':
44
1.64k
                eod = 1;
45
1.64k
                break;
46
47
2.36k
            case 'z':
48
2.36k
                if (pos != 0) {
49
27
                    throw std::runtime_error("unexpected z during base 85 decode");
50
27
                }
51
2.34k
                unsigned char zeroes[4];
52
2.34k
                memset(zeroes, '\0', 4);
53
2.34k
                next()->write(zeroes, 4);
54
2.34k
                break;
55
56
1.07M
            default:
57
1.07M
                if (buf[i] < 33 || buf[i] > 117) {
58
575
                    error = true;
59
575
                    throw std::runtime_error("character out of range during base 85 decode");
60
1.07M
                } else {
61
1.07M
                    this->inbuf[this->pos++] = buf[i];
62
1.07M
                    if (pos == 5) {
63
213k
                        flush();
64
213k
                    }
65
1.07M
                }
66
1.07M
                break;
67
1.07M
            }
68
1.07M
        }
69
1.07M
    }
70
3.00k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
217k
{
75
217k
    if (this->pos == 0) {
76
2.08k
        return;
77
2.08k
    }
78
214k
    unsigned long lval = 0;
79
1.28M
    for (int i = 0; i < 5; ++i) {
80
1.07M
        lval *= 85;
81
1.07M
        lval += (this->inbuf[i] - 33U);
82
1.07M
    }
83
84
214k
    unsigned char outbuf[4];
85
214k
    memset(outbuf, 0, 4);
86
1.07M
    for (int i = 3; i >= 0; --i) {
87
859k
        outbuf[i] = lval & 0xff;
88
859k
        lval >>= 8;
89
859k
    }
90
91
214k
    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
214k
    auto t = this->pos - 1;
94
214k
    this->pos = 0;
95
214k
    memset(this->inbuf, 117, 5);
96
97
214k
    next()->write(outbuf, t);
98
214k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
2.83k
{
103
2.83k
    if (error) {
104
572
        return;
105
572
    }
106
2.25k
    flush();
107
2.25k
    next()->finish();
108
2.25k
}