Coverage Report

Created: 2026-06-09 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
147
    Pipeline(identifier, next)
13
147
{
14
147
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
147
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
147
{
20
147
    if (eod > 1) {
21
0
        return;
22
0
    }
23
5.01k
    for (size_t i = 0; i < len; ++i) {
24
4.89k
        switch (buf[i]) {
25
194
        case ' ':
26
391
        case '\f':
27
661
        case '\v':
28
989
        case '\t':
29
1.18k
        case '\r':
30
1.46k
        case '\n':
31
1.46k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
1.46k
            continue;
34
4.89k
        }
35
3.43k
        if (eod > 1) {
36
1
            break;
37
3.42k
        } else if (eod == 1) {
38
19
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
19
            flush();
40
19
            eod = 2;
41
3.41k
        } else {
42
3.41k
            switch (buf[i]) {
43
21
            case '~':
44
21
                eod = 1;
45
21
                break;
46
47
250
            case 'z':
48
250
                if (pos != 0) {
49
2
                    throw std::runtime_error("unexpected z during base 85 decode");
50
2
                }
51
248
                unsigned char zeroes[4];
52
248
                memset(zeroes, '\0', 4);
53
248
                next()->write(zeroes, 4);
54
248
                break;
55
56
3.13k
            default:
57
3.13k
                if (buf[i] < 33 || buf[i] > 117) {
58
27
                    error = true;
59
27
                    throw std::runtime_error("character out of range during base 85 decode");
60
3.11k
                } else {
61
3.11k
                    this->inbuf[this->pos++] = buf[i];
62
3.11k
                    if (pos == 5) {
63
606
                        flush();
64
606
                    }
65
3.11k
                }
66
3.11k
                break;
67
3.41k
            }
68
3.41k
        }
69
3.43k
    }
70
147
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
713
{
75
713
    if (this->pos == 0) {
76
77
        return;
77
77
    }
78
636
    unsigned long lval = 0;
79
3.81k
    for (int i = 0; i < 5; ++i) {
80
3.18k
        lval *= 85;
81
3.18k
        lval += (this->inbuf[i] - 33U);
82
3.18k
    }
83
84
636
    unsigned char outbuf[4];
85
636
    memset(outbuf, 0, 4);
86
3.18k
    for (int i = 3; i >= 0; --i) {
87
2.54k
        outbuf[i] = lval & 0xff;
88
2.54k
        lval >>= 8;
89
2.54k
    }
90
91
636
    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
636
    auto t = this->pos - 1;
94
636
    this->pos = 0;
95
636
    memset(this->inbuf, 117, 5);
96
97
636
    next()->write(outbuf, t);
98
636
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
103
{
103
103
    if (error) {
104
0
        return;
105
0
    }
106
103
    flush();
107
103
    next()->finish();
108
103
}