Coverage Report

Created: 2025-12-05 06:54

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