Coverage Report

Created: 2026-02-26 06:36

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
624
    Pipeline(identifier, next)
13
624
{
14
624
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
624
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
781
{
20
781
    if (eod > 1) {
21
0
        return;
22
0
    }
23
281k
    for (size_t i = 0; i < len; ++i) {
24
280k
        switch (buf[i]) {
25
741
        case ' ':
26
1.04k
        case '\f':
27
1.33k
        case '\v':
28
1.61k
        case '\t':
29
1.95k
        case '\r':
30
3.96k
        case '\n':
31
3.96k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
3.96k
            continue;
34
280k
        }
35
277k
        if (eod > 1) {
36
12
            break;
37
277k
        } else if (eod == 1) {
38
40
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
40
            flush();
40
40
            eod = 2;
41
276k
        } else {
42
276k
            switch (buf[i]) {
43
39
            case '~':
44
39
                eod = 1;
45
39
                break;
46
47
1.65k
            case 'z':
48
1.65k
                if (pos != 0) {
49
35
                    throw std::runtime_error("unexpected z during base 85 decode");
50
35
                }
51
1.62k
                unsigned char zeroes[4];
52
1.62k
                memset(zeroes, '\0', 4);
53
1.62k
                next()->write(zeroes, 4);
54
1.62k
                break;
55
56
275k
            default:
57
275k
                if (buf[i] < 33 || buf[i] > 117) {
58
185
                    error = true;
59
185
                    throw std::runtime_error("character out of range during base 85 decode");
60
275k
                } else {
61
275k
                    this->inbuf[this->pos++] = buf[i];
62
275k
                    if (pos == 5) {
63
54.8k
                        flush();
64
54.8k
                    }
65
275k
                }
66
275k
                break;
67
276k
            }
68
276k
        }
69
277k
    }
70
781
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
55.3k
{
75
55.3k
    if (this->pos == 0) {
76
237
        return;
77
237
    }
78
55.0k
    unsigned long lval = 0;
79
330k
    for (int i = 0; i < 5; ++i) {
80
275k
        lval *= 85;
81
275k
        lval += (this->inbuf[i] - 33U);
82
275k
    }
83
84
55.0k
    unsigned char outbuf[4];
85
55.0k
    memset(outbuf, 0, 4);
86
275k
    for (int i = 3; i >= 0; --i) {
87
220k
        outbuf[i] = lval & 0xff;
88
220k
        lval >>= 8;
89
220k
    }
90
91
55.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
55.0k
    auto t = this->pos - 1;
94
55.0k
    this->pos = 0;
95
55.0k
    memset(this->inbuf, 117, 5);
96
97
55.0k
    next()->write(outbuf, t);
98
55.0k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
595
{
103
595
    if (error) {
104
168
        return;
105
168
    }
106
427
    flush();
107
427
    next()->finish();
108
427
}