Coverage Report

Created: 2026-06-16 06:40

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
2.81k
    Pipeline(identifier, next)
13
2.81k
{
14
2.81k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
2.81k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
6.57k
{
20
6.57k
    if (eod > 1) {
21
3.65k
        return;
22
3.65k
    }
23
577k
    for (size_t i = 0; i < len; ++i) {
24
576k
        switch (buf[i]) {
25
2.08k
        case ' ':
26
2.32k
        case '\f':
27
2.59k
        case '\v':
28
2.85k
        case '\t':
29
3.69k
        case '\r':
30
9.01k
        case '\n':
31
9.01k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
9.01k
            continue;
34
576k
        }
35
567k
        if (eod > 1) {
36
180
            break;
37
566k
        } else if (eod == 1) {
38
986
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
986
            flush();
40
986
            eod = 2;
41
565k
        } else {
42
565k
            switch (buf[i]) {
43
1.11k
            case '~':
44
1.11k
                eod = 1;
45
1.11k
                break;
46
47
926
            case 'z':
48
926
                if (pos != 0) {
49
17
                    throw std::runtime_error("unexpected z during base 85 decode");
50
17
                }
51
909
                unsigned char zeroes[4];
52
909
                memset(zeroes, '\0', 4);
53
909
                next()->write(zeroes, 4);
54
909
                break;
55
56
563k
            default:
57
563k
                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
562k
                } else {
61
562k
                    this->inbuf[this->pos++] = buf[i];
62
562k
                    if (pos == 5) {
63
111k
                        flush();
64
111k
                    }
65
562k
                }
66
562k
                break;
67
565k
            }
68
565k
        }
69
567k
    }
70
2.91k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
114k
{
75
114k
    if (this->pos == 0) {
76
1.57k
        return;
77
1.57k
    }
78
112k
    unsigned long lval = 0;
79
676k
    for (int i = 0; i < 5; ++i) {
80
563k
        lval *= 85;
81
563k
        lval += (this->inbuf[i] - 33U);
82
563k
    }
83
84
112k
    unsigned char outbuf[4];
85
112k
    memset(outbuf, 0, 4);
86
563k
    for (int i = 3; i >= 0; --i) {
87
450k
        outbuf[i] = lval & 0xff;
88
450k
        lval >>= 8;
89
450k
    }
90
91
112k
    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
112k
    auto t = this->pos - 1;
94
112k
    this->pos = 0;
95
112k
    memset(this->inbuf, 117, 5);
96
97
112k
    next()->write(outbuf, t);
98
112k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
2.49k
{
103
2.49k
    if (error) {
104
671
        return;
105
671
    }
106
1.82k
    flush();
107
1.82k
    next()->finish();
108
1.82k
}