Coverage Report

Created: 2026-10-17 07:02

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.70k
    Pipeline(identifier, next)
13
3.70k
{
14
3.70k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
3.70k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
6.04k
{
20
6.04k
    if (eod > 1) {
21
2.28k
        return;
22
2.28k
    }
23
712k
    for (size_t i = 0; i < len; ++i) {
24
710k
        switch (buf[i]) {
25
1.71k
        case ' ':
26
2.19k
        case '\f':
27
2.42k
        case '\v':
28
2.70k
        case '\t':
29
3.59k
        case '\r':
30
8.00k
        case '\n':
31
8.00k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
8.00k
            continue;
34
710k
        }
35
702k
        if (eod > 1) {
36
252
            break;
37
702k
        } else if (eod == 1) {
38
1.52k
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
1.52k
            flush();
40
1.52k
            eod = 2;
41
700k
        } else {
42
700k
            switch (buf[i]) {
43
1.76k
            case '~':
44
1.76k
                eod = 1;
45
1.76k
                break;
46
47
1.20k
            case 'z':
48
1.20k
                if (pos != 0) {
49
44
                    throw std::runtime_error("unexpected z during base 85 decode");
50
44
                }
51
1.16k
                unsigned char zeroes[4];
52
1.16k
                memset(zeroes, '\0', 4);
53
1.16k
                next()->write(zeroes, 4);
54
1.16k
                break;
55
56
697k
            default:
57
697k
                if (buf[i] < 33 || buf[i] > 117) {
58
1.16k
                    error = true;
59
1.16k
                    throw std::runtime_error("character out of range during base 85 decode");
60
696k
                } else {
61
696k
                    this->inbuf[this->pos++] = buf[i];
62
696k
                    if (pos == 5) {
63
137k
                        flush();
64
137k
                    }
65
696k
                }
66
696k
                break;
67
700k
            }
68
700k
        }
69
702k
    }
70
3.75k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
142k
{
75
142k
    if (this->pos == 0) {
76
2.25k
        return;
77
2.25k
    }
78
139k
    unsigned long lval = 0;
79
839k
    for (int i = 0; i < 5; ++i) {
80
699k
        lval *= 85;
81
699k
        lval += (this->inbuf[i] - 33U);
82
699k
    }
83
84
139k
    unsigned char outbuf[4];
85
139k
    memset(outbuf, 0, 4);
86
699k
    for (int i = 3; i >= 0; --i) {
87
559k
        outbuf[i] = lval & 0xff;
88
559k
        lval >>= 8;
89
559k
    }
90
91
139k
    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
139k
    auto t = this->pos - 1;
94
139k
    this->pos = 0;
95
139k
    memset(this->inbuf, 117, 5);
96
97
139k
    next()->write(outbuf, t);
98
139k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
3.43k
{
103
3.43k
    if (error) {
104
699
        return;
105
699
    }
106
2.73k
    flush();
107
2.73k
    next()->finish();
108
2.73k
}