Coverage Report

Created: 2026-05-30 06:17

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
3.22k
    Pipeline(identifier, next)
13
3.22k
{
14
3.22k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
3.22k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
6.39k
{
20
6.39k
    if (eod > 1) {
21
3.10k
        return;
22
3.10k
    }
23
841k
    for (size_t i = 0; i < len; ++i) {
24
840k
        switch (buf[i]) {
25
2.35k
        case ' ':
26
2.62k
        case '\f':
27
3.06k
        case '\v':
28
3.34k
        case '\t':
29
4.18k
        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
840k
        }
35
828k
        if (eod > 1) {
36
226
            break;
37
828k
        } else if (eod == 1) {
38
1.22k
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
1.22k
            flush();
40
1.22k
            eod = 2;
41
826k
        } else {
42
826k
            switch (buf[i]) {
43
1.36k
            case '~':
44
1.36k
                eod = 1;
45
1.36k
                break;
46
47
1.07k
            case 'z':
48
1.07k
                if (pos != 0) {
49
21
                    throw std::runtime_error("unexpected z during base 85 decode");
50
21
                }
51
1.05k
                unsigned char zeroes[4];
52
1.05k
                memset(zeroes, '\0', 4);
53
1.05k
                next()->write(zeroes, 4);
54
1.05k
                break;
55
56
824k
            default:
57
824k
                if (buf[i] < 33 || buf[i] > 117) {
58
1.24k
                    error = true;
59
1.24k
                    throw std::runtime_error("character out of range during base 85 decode");
60
823k
                } else {
61
823k
                    this->inbuf[this->pos++] = buf[i];
62
823k
                    if (pos == 5) {
63
163k
                        flush();
64
163k
                    }
65
823k
                }
66
823k
                break;
67
826k
            }
68
826k
        }
69
828k
    }
70
3.28k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
166k
{
75
166k
    if (this->pos == 0) {
76
1.90k
        return;
77
1.90k
    }
78
164k
    unsigned long lval = 0;
79
989k
    for (int i = 0; i < 5; ++i) {
80
824k
        lval *= 85;
81
824k
        lval += (this->inbuf[i] - 33U);
82
824k
    }
83
84
164k
    unsigned char outbuf[4];
85
164k
    memset(outbuf, 0, 4);
86
824k
    for (int i = 3; i >= 0; --i) {
87
659k
        outbuf[i] = lval & 0xff;
88
659k
        lval >>= 8;
89
659k
    }
90
91
164k
    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
164k
    auto t = this->pos - 1;
94
164k
    this->pos = 0;
95
164k
    memset(this->inbuf, 117, 5);
96
97
164k
    next()->write(outbuf, t);
98
164k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
2.94k
{
103
2.94k
    if (error) {
104
767
        return;
105
767
    }
106
2.18k
    flush();
107
2.18k
    next()->finish();
108
2.18k
}