Coverage Report

Created: 2025-12-14 06:38

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