Coverage Report

Created: 2025-08-03 06:16

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