Coverage Report

Created: 2026-09-14 06:13

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
141
    Pipeline(identifier, next)
13
141
{
14
141
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
141
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
141
{
20
141
    if (eod > 1) {
21
0
        return;
22
0
    }
23
7.35k
    for (size_t i = 0; i < len; ++i) {
24
7.24k
        switch (buf[i]) {
25
281
        case ' ':
26
550
        case '\f':
27
862
        case '\v':
28
1.13k
        case '\t':
29
2.52k
        case '\r':
30
2.74k
        case '\n':
31
2.74k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
2.74k
            continue;
34
7.24k
        }
35
4.50k
        if (eod > 1) {
36
1
            break;
37
4.50k
        } else if (eod == 1) {
38
15
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
15
            flush();
40
15
            eod = 2;
41
4.48k
        } else {
42
4.48k
            switch (buf[i]) {
43
17
            case '~':
44
17
                eod = 1;
45
17
                break;
46
47
455
            case 'z':
48
455
                if (pos != 0) {
49
2
                    throw std::runtime_error("unexpected z during base 85 decode");
50
2
                }
51
453
                unsigned char zeroes[4];
52
453
                memset(zeroes, '\0', 4);
53
453
                next()->write(zeroes, 4);
54
453
                break;
55
56
4.01k
            default:
57
4.01k
                if (buf[i] < 33 || buf[i] > 117) {
58
28
                    error = true;
59
28
                    throw std::runtime_error("character out of range during base 85 decode");
60
3.98k
                } else {
61
3.98k
                    this->inbuf[this->pos++] = buf[i];
62
3.98k
                    if (pos == 5) {
63
786
                        flush();
64
786
                    }
65
3.98k
                }
66
3.98k
                break;
67
4.48k
            }
68
4.48k
        }
69
4.50k
    }
70
141
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
890
{
75
890
    if (this->pos == 0) {
76
81
        return;
77
81
    }
78
809
    unsigned long lval = 0;
79
4.85k
    for (int i = 0; i < 5; ++i) {
80
4.04k
        lval *= 85;
81
4.04k
        lval += (this->inbuf[i] - 33U);
82
4.04k
    }
83
84
809
    unsigned char outbuf[4];
85
809
    memset(outbuf, 0, 4);
86
4.04k
    for (int i = 3; i >= 0; --i) {
87
3.23k
        outbuf[i] = lval & 0xff;
88
3.23k
        lval >>= 8;
89
3.23k
    }
90
91
809
    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
809
    auto t = this->pos - 1;
94
809
    this->pos = 0;
95
809
    memset(this->inbuf, 117, 5);
96
97
809
    next()->write(outbuf, t);
98
809
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
100
{
103
100
    if (error) {
104
0
        return;
105
0
    }
106
100
    flush();
107
100
    next()->finish();
108
100
}