Coverage Report

Created: 2026-02-26 06:36

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