Coverage Report

Created: 2026-01-25 06:25

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