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
1.61k
    Pipeline(identifier, next)
13
1.61k
{
14
1.61k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
1.61k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
2.12k
{
20
2.12k
    if (eod > 1) {
21
195
        return;
22
195
    }
23
556k
    for (size_t i = 0; i < len; ++i) {
24
555k
        switch (buf[i]) {
25
843
        case ' ':
26
1.08k
        case '\f':
27
1.39k
        case '\v':
28
1.62k
        case '\t':
29
2.09k
        case '\r':
30
3.92k
        case '\n':
31
3.92k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
3.92k
            continue;
34
555k
        }
35
551k
        if (eod > 1) {
36
69
            break;
37
551k
        } else if (eod == 1) {
38
539
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
539
            flush();
40
539
            eod = 2;
41
551k
        } else {
42
551k
            switch (buf[i]) {
43
557
            case '~':
44
557
                eod = 1;
45
557
                break;
46
47
1.90k
            case 'z':
48
1.90k
                if (pos != 0) {
49
36
                    throw std::runtime_error("unexpected z during base 85 decode");
50
36
                }
51
1.87k
                unsigned char zeroes[4];
52
1.87k
                memset(zeroes, '\0', 4);
53
1.87k
                next()->write(zeroes, 4);
54
1.87k
                break;
55
56
548k
            default:
57
548k
                if (buf[i] < 33 || buf[i] > 117) {
58
579
                    error = true;
59
579
                    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
109k
                        flush();
64
109k
                    }
65
548k
                }
66
548k
                break;
67
551k
            }
68
551k
        }
69
551k
    }
70
1.92k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
110k
{
75
110k
    if (this->pos == 0) {
76
822
        return;
77
822
    }
78
109k
    unsigned long lval = 0;
79
659k
    for (int i = 0; i < 5; ++i) {
80
549k
        lval *= 85;
81
549k
        lval += (this->inbuf[i] - 33U);
82
549k
    }
83
84
109k
    unsigned char outbuf[4];
85
109k
    memset(outbuf, 0, 4);
86
549k
    for (int i = 3; i >= 0; --i) {
87
439k
        outbuf[i] = lval & 0xff;
88
439k
        lval >>= 8;
89
439k
    }
90
91
109k
    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
109k
    auto t = this->pos - 1;
94
109k
    this->pos = 0;
95
109k
    memset(this->inbuf, 117, 5);
96
97
109k
    next()->write(outbuf, t);
98
109k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
1.42k
{
103
1.42k
    if (error) {
104
355
        return;
105
355
    }
106
1.07k
    flush();
107
1.07k
    next()->finish();
108
1.07k
}