Coverage Report

Created: 2026-01-17 06:34

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