Coverage Report

Created: 2026-01-10 07:00

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