Coverage Report

Created: 2026-12-01 06:45

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
4.79k
    Pipeline(identifier, next)
13
4.79k
{
14
4.79k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
4.79k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
4.99k
{
20
4.99k
    if (eod > 1) {
21
199
        return;
22
199
    }
23
570k
    for (size_t i = 0; i < len; ++i) {
24
567k
        switch (buf[i]) {
25
4.97k
        case ' ':
26
5.21k
        case '\f':
27
5.42k
        case '\v':
28
5.64k
        case '\t':
29
10.1k
        case '\r':
30
11.7k
        case '\n':
31
11.7k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
11.7k
            continue;
34
567k
        }
35
555k
        if (eod > 1) {
36
741
            break;
37
554k
        } else if (eod == 1) {
38
1.85k
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
1.85k
            flush();
40
1.85k
            eod = 2;
41
552k
        } else {
42
552k
            switch (buf[i]) {
43
1.84k
            case '~':
44
1.84k
                eod = 1;
45
1.84k
                break;
46
47
2.62k
            case 'z':
48
2.62k
                if (pos != 0) {
49
21
                    throw std::runtime_error("unexpected z during base 85 decode");
50
21
                }
51
2.59k
                unsigned char zeroes[4];
52
2.59k
                memset(zeroes, '\0', 4);
53
2.59k
                next()->write(zeroes, 4);
54
2.59k
                break;
55
56
548k
            default:
57
548k
                if (buf[i] < 33 || buf[i] > 117) {
58
211
                    error = true;
59
211
                    throw std::runtime_error("character out of range during base 85 decode");
60
548k
                } else {
61
548k
                    this->inbuf[this->pos++] = buf[i];
62
548k
                    if (pos == 5) {
63
108k
                        flush();
64
108k
                    }
65
548k
                }
66
548k
                break;
67
552k
            }
68
552k
        }
69
555k
    }
70
4.79k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
114k
{
75
114k
    if (this->pos == 0) {
76
3.37k
        return;
77
3.37k
    }
78
111k
    unsigned long lval = 0;
79
667k
    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
444k
        outbuf[i] = lval & 0xff;
88
444k
        lval >>= 8;
89
444k
    }
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
4.78k
{
103
4.78k
    if (error) {
104
190
        return;
105
190
    }
106
4.59k
    flush();
107
4.59k
    next()->finish();
108
4.59k
}