Coverage Report

Created: 2026-10-17 06:58

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
2.61k
    Pipeline(identifier, next)
13
2.61k
{
14
2.61k
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
15
2.61k
}
16
17
void
18
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
19
11.3k
{
20
11.3k
    if (eod > 1) {
21
8.45k
        return;
22
8.45k
    }
23
372k
    for (size_t i = 0; i < len; ++i) {
24
371k
        switch (buf[i]) {
25
1.18k
        case ' ':
26
1.41k
        case '\f':
27
1.73k
        case '\v':
28
1.97k
        case '\t':
29
2.53k
        case '\r':
30
4.38k
        case '\n':
31
4.38k
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
32
            // ignore whitespace
33
4.38k
            continue;
34
371k
        }
35
366k
        if (eod > 1) {
36
86
            break;
37
366k
        } else if (eod == 1) {
38
612
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
39
612
            flush();
40
612
            eod = 2;
41
366k
        } else {
42
366k
            switch (buf[i]) {
43
679
            case '~':
44
679
                eod = 1;
45
679
                break;
46
47
2.90k
            case 'z':
48
2.90k
                if (pos != 0) {
49
59
                    throw std::runtime_error("unexpected z during base 85 decode");
50
59
                }
51
2.84k
                unsigned char zeroes[4];
52
2.84k
                memset(zeroes, '\0', 4);
53
2.84k
                next()->write(zeroes, 4);
54
2.84k
                break;
55
56
362k
            default:
57
362k
                if (buf[i] < 33 || buf[i] > 117) {
58
1.22k
                    error = true;
59
1.22k
                    throw std::runtime_error("character out of range during base 85 decode");
60
361k
                } else {
61
361k
                    this->inbuf[this->pos++] = buf[i];
62
361k
                    if (pos == 5) {
63
71.4k
                        flush();
64
71.4k
                    }
65
361k
                }
66
361k
                break;
67
366k
            }
68
366k
        }
69
366k
    }
70
2.88k
}
71
72
void
73
Pl_ASCII85Decoder::flush()
74
73.4k
{
75
73.4k
    if (this->pos == 0) {
76
1.10k
        return;
77
1.10k
    }
78
72.3k
    unsigned long lval = 0;
79
434k
    for (int i = 0; i < 5; ++i) {
80
361k
        lval *= 85;
81
361k
        lval += (this->inbuf[i] - 33U);
82
361k
    }
83
84
72.3k
    unsigned char outbuf[4];
85
72.3k
    memset(outbuf, 0, 4);
86
361k
    for (int i = 3; i >= 0; --i) {
87
289k
        outbuf[i] = lval & 0xff;
88
289k
        lval >>= 8;
89
289k
    }
90
91
72.3k
    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
72.3k
    auto t = this->pos - 1;
94
72.3k
    this->pos = 0;
95
72.3k
    memset(this->inbuf, 117, 5);
96
97
72.3k
    next()->write(outbuf, t);
98
72.3k
}
99
100
void
101
Pl_ASCII85Decoder::finish()
102
2.34k
{
103
2.34k
    if (error) {
104
899
        return;
105
899
    }
106
1.44k
    flush();
107
1.44k
    next()->finish();
108
1.44k
}