Coverage Report

Created: 2026-03-07 06:28

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