Coverage Report

Created: 2025-11-09 06:21

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
3.90k
    Pipeline(identifier, next)
9
3.90k
{
10
3.90k
    if (!next) {
11
0
        throw std::logic_error("Attempt to create Pl_ASCII85Decoder with nullptr as next");
12
0
    }
13
3.90k
}
14
15
void
16
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
17
2.99k
{
18
2.99k
    if (eod > 1) {
19
0
        return;
20
0
    }
21
937k
    for (size_t i = 0; i < len; ++i) {
22
935k
        switch (buf[i]) {
23
7.73k
        case ' ':
24
21.2k
        case '\f':
25
22.6k
        case '\v':
26
22.8k
        case '\t':
27
27.8k
        case '\r':
28
37.7k
        case '\n':
29
37.7k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
30
            // ignore whitespace
31
37.7k
            continue;
32
935k
        }
33
898k
        if (eod > 1) {
34
149
            break;
35
897k
        } else if (eod == 1) {
36
1.39k
            if (buf[i] == '>') {
37
1.36k
                flush();
38
1.36k
                eod = 2;
39
1.36k
            } else {
40
30
                throw std::runtime_error("broken end-of-data sequence in base 85 data");
41
30
            }
42
896k
        } else {
43
896k
            switch (buf[i]) {
44
1.43k
            case '~':
45
1.43k
                eod = 1;
46
1.43k
                break;
47
48
1.46k
            case 'z':
49
1.46k
                if (pos != 0) {
50
22
                    throw std::runtime_error("unexpected z during base 85 decode");
51
1.44k
                } else {
52
1.44k
                    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
53
1.44k
                    unsigned char zeroes[4];
54
1.44k
                    memset(zeroes, '\0', 4);
55
1.44k
                    next()->write(zeroes, 4);
56
1.44k
                }
57
1.44k
                break;
58
59
893k
            default:
60
893k
                if (buf[i] < 33 || buf[i] > 117) {
61
708
                    error = true;
62
708
                    throw std::runtime_error("character out of range during base 85 decode");
63
892k
                } else {
64
892k
                    this->inbuf[this->pos++] = buf[i];
65
892k
                    if (pos == 5) {
66
177k
                        flush();
67
177k
                    }
68
892k
                }
69
892k
                break;
70
896k
            }
71
896k
        }
72
898k
    }
73
2.99k
}
74
75
void
76
Pl_ASCII85Decoder::flush()
77
181k
{
78
181k
    if (this->pos == 0) {
79
2.81k
        QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
80
2.81k
        return;
81
2.81k
    }
82
178k
    unsigned long lval = 0;
83
1.07M
    for (int i = 0; i < 5; ++i) {
84
894k
        lval *= 85;
85
894k
        lval += (this->inbuf[i] - 33U);
86
894k
    }
87
88
178k
    unsigned char outbuf[4];
89
178k
    memset(outbuf, 0, 4);
90
894k
    for (int i = 3; i >= 0; --i) {
91
715k
        outbuf[i] = lval & 0xff;
92
715k
        lval >>= 8;
93
715k
    }
94
95
178k
    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
178k
    auto t = this->pos - 1;
98
178k
    this->pos = 0;
99
178k
    memset(this->inbuf, 117, 5);
100
101
178k
    next()->write(outbuf, t);
102
178k
}
103
104
void
105
Pl_ASCII85Decoder::finish()
106
3.34k
{
107
3.34k
    if (error) {
108
464
        return;
109
464
    }
110
2.88k
    flush();
111
2.88k
    next()->finish();
112
2.88k
}