Coverage Report

Created: 2026-06-09 06:59

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