Coverage Report

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