File: Pl_ASCII85Decoder.cc

package info (click to toggle)
qpdf 12.3.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 72,644 kB
  • sloc: cpp: 59,031; perl: 12,172; ansic: 6,809; sh: 1,231; python: 1,041; xml: 43; makefile: 42
file content (108 lines) | stat: -rw-r--r-- 2,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <qpdf/Pl_ASCII85Decoder.hh>

#include <qpdf/QTC.hh>
#include <qpdf/Util.hh>

#include <cstring>
#include <stdexcept>

using namespace qpdf;

Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
    Pipeline(identifier, next)
{
    util::assertion(next, "Attempt to create Pl_ASCII85Decoder with nullptr as next");
}

void
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
{
    if (eod > 1) {
        return;
    }
    for (size_t i = 0; i < len; ++i) {
        switch (buf[i]) {
        case ' ':
        case '\f':
        case '\v':
        case '\t':
        case '\r':
        case '\n':
            QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
            // ignore whitespace
            continue;
        }
        if (eod > 1) {
            break;
        } else if (eod == 1) {
            util::no_ci_rt_error_if(buf[i] != '>', "broken end-of-data sequence in base 85 data");
            flush();
            eod = 2;
        } else {
            switch (buf[i]) {
            case '~':
                eod = 1;
                break;

            case 'z':
                if (pos != 0) {
                    throw std::runtime_error("unexpected z during base 85 decode");
                }
                unsigned char zeroes[4];
                memset(zeroes, '\0', 4);
                next()->write(zeroes, 4);
                break;

            default:
                if (buf[i] < 33 || buf[i] > 117) {
                    error = true;
                    throw std::runtime_error("character out of range during base 85 decode");
                } else {
                    this->inbuf[this->pos++] = buf[i];
                    if (pos == 5) {
                        flush();
                    }
                }
                break;
            }
        }
    }
}

void
Pl_ASCII85Decoder::flush()
{
    if (this->pos == 0) {
        return;
    }
    unsigned long lval = 0;
    for (int i = 0; i < 5; ++i) {
        lval *= 85;
        lval += (this->inbuf[i] - 33U);
    }

    unsigned char outbuf[4];
    memset(outbuf, 0, 4);
    for (int i = 3; i >= 0; --i) {
        outbuf[i] = lval & 0xff;
        lval >>= 8;
    }

    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
    // Reset before calling getNext()->write in case that throws an exception.
    auto t = this->pos - 1;
    this->pos = 0;
    memset(this->inbuf, 117, 5);

    next()->write(outbuf, t);
}

void
Pl_ASCII85Decoder::finish()
{
    if (error) {
        return;
    }
    flush();
    next()->finish();
}