Coverage Report

Created: 2025-11-09 06:16

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