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