Coverage Report

Created: 2026-11-12 06:16

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