Coverage Report

Created: 2025-10-12 07:05

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
602
    Pipeline(identifier, next)
9
602
{
10
602
    if (!next) {
11
0
        throw std::logic_error("Attempt to create Pl_ASCII85Decoder with nullptr as next");
12
0
    }
13
602
}
14
15
void
16
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
17
585
{
18
585
    if (eod > 1) {
19
0
        return;
20
0
    }
21
262k
    for (size_t i = 0; i < len; ++i) {
22
262k
        switch (buf[i]) {
23
2.75k
        case ' ':
24
2.96k
        case '\f':
25
3.18k
        case '\v':
26
3.40k
        case '\t':
27
8.97k
        case '\r':
28
16.1k
        case '\n':
29
16.1k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
30
            // ignore whitespace
31
16.1k
            continue;
32
262k
        }
33
246k
        if (eod > 1) {
34
22
            break;
35
246k
        } else if (eod == 1) {
36
77
            if (buf[i] == '>') {
37
50
                flush();
38
50
                eod = 2;
39
50
            } else {
40
27
                throw std::runtime_error("broken end-of-data sequence in base 85 data");
41
27
            }
42
245k
        } else {
43
245k
            switch (buf[i]) {
44
77
            case '~':
45
77
                eod = 1;
46
77
                break;
47
48
1.79k
            case 'z':
49
1.79k
                if (pos != 0) {
50
36
                    throw std::runtime_error("unexpected z during base 85 decode");
51
1.75k
                } else {
52
1.75k
                    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
53
1.75k
                    unsigned char zeroes[4];
54
1.75k
                    memset(zeroes, '\0', 4);
55
1.75k
                    next()->write(zeroes, 4);
56
1.75k
                }
57
1.75k
                break;
58
59
244k
            default:
60
244k
                if (buf[i] < 33 || buf[i] > 117) {
61
190
                    error = true;
62
190
                    throw std::runtime_error("character out of range during base 85 decode");
63
243k
                } else {
64
243k
                    this->inbuf[this->pos++] = buf[i];
65
243k
                    if (pos == 5) {
66
48.6k
                        flush();
67
48.6k
                    }
68
243k
                }
69
243k
                break;
70
245k
            }
71
245k
        }
72
246k
    }
73
585
}
74
75
void
76
Pl_ASCII85Decoder::flush()
77
49.0k
{
78
49.0k
    if (this->pos == 0) {
79
244
        QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
80
244
        return;
81
244
    }
82
48.8k
    unsigned long lval = 0;
83
292k
    for (int i = 0; i < 5; ++i) {
84
244k
        lval *= 85;
85
244k
        lval += (this->inbuf[i] - 33U);
86
244k
    }
87
88
48.8k
    unsigned char outbuf[4];
89
48.8k
    memset(outbuf, 0, 4);
90
244k
    for (int i = 3; i >= 0; --i) {
91
195k
        outbuf[i] = lval & 0xff;
92
195k
        lval >>= 8;
93
195k
    }
94
95
48.8k
    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
48.8k
    auto t = this->pos - 1;
98
48.8k
    this->pos = 0;
99
48.8k
    memset(this->inbuf, 117, 5);
100
101
48.8k
    next()->write(outbuf, t);
102
48.8k
}
103
104
void
105
Pl_ASCII85Decoder::finish()
106
575
{
107
575
    if (error) {
108
166
        return;
109
166
    }
110
409
    flush();
111
409
    next()->finish();
112
409
}