Coverage Report

Created: 2026-07-16 06:55

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