Coverage Report

Created: 2026-03-07 06:25

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
1.66k
    Pipeline(identifier, next)
13
1.66k
{
14
1.66k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
1.66k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
2.22k
{
20
2.22k
    if (eod > 1) {
21
184
        return;
22
184
    }
23
564k
    for (size_t i = 0; i < len; ++i) {
24
563k
        switch (buf[i]) {
25
736
        case ' ':
26
986
        case '\f':
27
1.30k
        case '\v':
28
1.51k
        case '\t':
29
1.96k
        case '\r':
30
3.85k
        case '\n':
31
3.85k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
3.85k
            continue;
34
563k
        }
35
559k
        if (eod > 1) {
36
68
            break;
37
559k
        } else if (eod == 1) {
38
537
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
537
            flush();
40
537
            eod = 2;
41
559k
        } else {
42
559k
            switch (buf[i]) {
43
557
            case '~':
44
557
                eod = 1;
45
557
                break;
46
47
1.88k
            case 'z':
48
1.88k
                if (pos != 0) {
49
34
                    throw std::runtime_error("unexpected z during base 85 decode");
50
34
                }
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
556k
            default:
57
556k
                if (buf[i] < 33 || buf[i] > 117) {
58
629
                    error = true;
59
629
                    throw std::runtime_error("character out of range during base 85 decode");
60
555k
                } else {
61
555k
                    this->inbuf[this->pos++] = buf[i];
62
555k
                    if (pos == 5) {
63
110k
                        flush();
64
110k
                    }
65
555k
                }
66
555k
                break;
67
559k
            }
68
559k
        }
69
559k
    }
70
2.04k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
112k
{
75
112k
    if (this->pos == 0) {
76
829
        return;
77
829
    }
78
111k
    unsigned long lval = 0;
79
668k
    for (int i = 0; i < 5; ++i) {
80
556k
        lval *= 85;
81
556k
        lval += (this->inbuf[i] - 33U);
82
556k
    }
83
84
111k
    unsigned char outbuf[4];
85
111k
    memset(outbuf, 0, 4);
86
556k
    for (int i = 3; i >= 0; --i) {
87
445k
        outbuf[i] = lval & 0xff;
88
445k
        lval >>= 8;
89
445k
    }
90
91
111k
    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
111k
    auto t = this->pos - 1;
94
111k
    this->pos = 0;
95
111k
    memset(this->inbuf, 117, 5);
96
97
111k
    next()->write(outbuf, t);
98
111k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
1.46k
{
103
1.46k
    if (error) {
104
385
        return;
105
385
    }
106
1.07k
    flush();
107
1.07k
    next()->finish();
108
1.07k
}