Coverage Report

Created: 2025-10-10 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 <cstring>
5
#include <stdexcept>
6
7
Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
8
138
    Pipeline(identifier, next)
9
138
{
10
138
    if (!next) {
11
0
        throw std::logic_error("Attempt to create Pl_ASCII85Decoder with nullptr as next");
12
0
    }
13
138
}
14
15
void
16
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
17
138
{
18
138
    if (eod > 1) {
19
0
        return;
20
0
    }
21
5.76k
    for (size_t i = 0; i < len; ++i) {
22
5.66k
        switch (buf[i]) {
23
196
        case ' ':
24
425
        case '\f':
25
782
        case '\v':
26
1.04k
        case '\t':
27
1.26k
        case '\r':
28
1.53k
        case '\n':
29
1.53k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
30
            // ignore whitespace
31
1.53k
            continue;
32
5.66k
        }
33
4.12k
        if (eod > 1) {
34
1
            break;
35
4.12k
        } else if (eod == 1) {
36
15
            if (buf[i] == '>') {
37
5
                flush();
38
5
                eod = 2;
39
10
            } else {
40
10
                throw std::runtime_error("broken end-of-data sequence in base 85 data");
41
10
            }
42
4.11k
        } else {
43
4.11k
            switch (buf[i]) {
44
16
            case '~':
45
16
                eod = 1;
46
16
                break;
47
48
344
            case 'z':
49
344
                if (pos != 0) {
50
2
                    throw std::runtime_error("unexpected z during base 85 decode");
51
342
                } else {
52
342
                    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
53
342
                    unsigned char zeroes[4];
54
342
                    memset(zeroes, '\0', 4);
55
342
                    next()->write(zeroes, 4);
56
342
                }
57
342
                break;
58
59
3.75k
            default:
60
3.75k
                if (buf[i] < 33 || buf[i] > 117) {
61
28
                    error = true;
62
28
                    throw std::runtime_error("character out of range during base 85 decode");
63
3.72k
                } else {
64
3.72k
                    this->inbuf[this->pos++] = buf[i];
65
3.72k
                    if (pos == 5) {
66
732
                        flush();
67
732
                    }
68
3.72k
                }
69
3.72k
                break;
70
4.11k
            }
71
4.11k
        }
72
4.12k
    }
73
138
}
74
75
void
76
Pl_ASCII85Decoder::flush()
77
835
{
78
835
    if (this->pos == 0) {
79
81
        QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
80
81
        return;
81
81
    }
82
754
    unsigned long lval = 0;
83
4.52k
    for (int i = 0; i < 5; ++i) {
84
3.77k
        lval *= 85;
85
3.77k
        lval += (this->inbuf[i] - 33U);
86
3.77k
    }
87
88
754
    unsigned char outbuf[4];
89
754
    memset(outbuf, 0, 4);
90
3.77k
    for (int i = 3; i >= 0; --i) {
91
3.01k
        outbuf[i] = lval & 0xff;
92
3.01k
        lval >>= 8;
93
3.01k
    }
94
95
754
    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
96
    // Reset before calling getNext()->write in case that throws an exception.
97
754
    auto t = this->pos - 1;
98
754
    this->pos = 0;
99
754
    memset(this->inbuf, 117, 5);
100
101
754
    next()->write(outbuf, t);
102
754
}
103
104
void
105
Pl_ASCII85Decoder::finish()
106
98
{
107
98
    if (error) {
108
0
        return;
109
0
    }
110
98
    flush();
111
98
    next()->finish();
112
98
}