Coverage Report

Created: 2025-08-26 07:09

/src/qpdf/libqpdf/Pl_ASCII85Decoder.cc
Line
Count
Source (jump to first uncovered line)
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
1.81k
    Pipeline(identifier, next)
9
1.81k
{
10
1.81k
    if (!next) {
11
0
        throw std::logic_error("Attempt to create Pl_ASCII85Decoder with nullptr as next");
12
0
    }
13
1.81k
}
14
15
void
16
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
17
2.20k
{
18
2.20k
    if (eod > 1) {
19
203
        return;
20
203
    }
21
440k
    for (size_t i = 0; i < len; ++i) {
22
439k
        switch (buf[i]) {
23
724
        case ' ':
24
948
        case '\f':
25
1.17k
        case '\v':
26
1.40k
        case '\t':
27
1.92k
        case '\r':
28
4.63k
        case '\n':
29
4.63k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
30
            // ignore whitespace
31
4.63k
            continue;
32
439k
        }
33
434k
        if (eod > 1) {
34
67
            break;
35
434k
        } else if (eod == 1) {
36
597
            if (buf[i] == '>') {
37
569
                flush();
38
569
                eod = 2;
39
569
            } else {
40
28
                throw std::runtime_error("broken end-of-data sequence in base 85 data");
41
28
            }
42
433k
        } else {
43
433k
            switch (buf[i]) {
44
630
            case '~':
45
630
                eod = 1;
46
630
                break;
47
48
1.64k
            case 'z':
49
1.64k
                if (pos != 0) {
50
32
                    throw std::runtime_error("unexpected z during base 85 decode");
51
1.61k
                } else {
52
1.61k
                    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
53
1.61k
                    unsigned char zeroes[4];
54
1.61k
                    memset(zeroes, '\0', 4);
55
1.61k
                    next()->write(zeroes, 4);
56
1.61k
                }
57
1.61k
                break;
58
59
431k
            default:
60
431k
                if (buf[i] < 33 || buf[i] > 117) {
61
852
                    error = true;
62
852
                    throw std::runtime_error("character out of range during base 85 decode");
63
430k
                } else {
64
430k
                    this->inbuf[this->pos++] = buf[i];
65
430k
                    if (pos == 5) {
66
85.5k
                        flush();
67
85.5k
                    }
68
430k
                }
69
430k
                break;
70
433k
            }
71
433k
        }
72
434k
    }
73
2.00k
}
74
75
void
76
Pl_ASCII85Decoder::flush()
77
87.2k
{
78
87.2k
    if (this->pos == 0) {
79
930
        QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
80
930
        return;
81
930
    }
82
86.3k
    unsigned long lval = 0;
83
518k
    for (int i = 0; i < 5; ++i) {
84
431k
        lval *= 85;
85
431k
        lval += (this->inbuf[i] - 33U);
86
431k
    }
87
88
86.3k
    unsigned char outbuf[4];
89
86.3k
    memset(outbuf, 0, 4);
90
431k
    for (int i = 3; i >= 0; --i) {
91
345k
        outbuf[i] = lval & 0xff;
92
345k
        lval >>= 8;
93
345k
    }
94
95
86.3k
    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
86.3k
    auto t = this->pos - 1;
98
86.3k
    this->pos = 0;
99
86.3k
    memset(this->inbuf, 117, 5);
100
101
86.3k
    next()->write(outbuf, t);
102
86.3k
}
103
104
void
105
Pl_ASCII85Decoder::finish()
106
1.61k
{
107
1.61k
    if (error) {
108
486
        return;
109
486
    }
110
1.12k
    flush();
111
1.12k
    next()->finish();
112
1.12k
}