Coverage Report

Created: 2026-03-12 06:58

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