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