Coverage Report

Created: 2026-02-26 06:35

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 <qpdf/Util.hh>
5
6
#include <cstring>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
11
Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
12
142
    Pipeline(identifier, next)
13
142
{
14
142
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
142
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
142
{
20
142
    if (eod > 1) {
21
0
        return;
22
0
    }
23
5.51k
    for (size_t i = 0; i < len; ++i) {
24
5.40k
        switch (buf[i]) {
25
258
        case ' ':
26
454
        case '\f':
27
649
        case '\v':
28
906
        case '\t':
29
1.22k
        case '\r':
30
1.43k
        case '\n':
31
1.43k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
1.43k
            continue;
34
5.40k
        }
35
3.97k
        if (eod > 1) {
36
1
            break;
37
3.97k
        } else if (eod == 1) {
38
15
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
15
            flush();
40
15
            eod = 2;
41
3.95k
        } else {
42
3.95k
            switch (buf[i]) {
43
16
            case '~':
44
16
                eod = 1;
45
16
                break;
46
47
215
            case 'z':
48
215
                if (pos != 0) {
49
5
                    throw std::runtime_error("unexpected z during base 85 decode");
50
5
                }
51
210
                unsigned char zeroes[4];
52
210
                memset(zeroes, '\0', 4);
53
210
                next()->write(zeroes, 4);
54
210
                break;
55
56
3.72k
            default:
57
3.72k
                if (buf[i] < 33 || buf[i] > 117) {
58
27
                    error = true;
59
27
                    throw std::runtime_error("character out of range during base 85 decode");
60
3.69k
                } else {
61
3.69k
                    this->inbuf[this->pos++] = buf[i];
62
3.69k
                    if (pos == 5) {
63
727
                        flush();
64
727
                    }
65
3.69k
                }
66
3.69k
                break;
67
3.95k
            }
68
3.95k
        }
69
3.97k
    }
70
142
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
830
{
75
830
    if (this->pos == 0) {
76
79
        return;
77
79
    }
78
751
    unsigned long lval = 0;
79
4.50k
    for (int i = 0; i < 5; ++i) {
80
3.75k
        lval *= 85;
81
3.75k
        lval += (this->inbuf[i] - 33U);
82
3.75k
    }
83
84
751
    unsigned char outbuf[4];
85
751
    memset(outbuf, 0, 4);
86
3.75k
    for (int i = 3; i >= 0; --i) {
87
3.00k
        outbuf[i] = lval & 0xff;
88
3.00k
        lval >>= 8;
89
3.00k
    }
90
91
751
    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
92
    // Reset before calling getNext()->write in case that throws an exception.
93
751
    auto t = this->pos - 1;
94
751
    this->pos = 0;
95
751
    memset(this->inbuf, 117, 5);
96
97
751
    next()->write(outbuf, t);
98
751
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
99
{
103
99
    if (error) {
104
0
        return;
105
0
    }
106
99
    flush();
107
99
    next()->finish();
108
99
}