Commit a686cc5f4cff6d044766f89f709946a13552e957
1 parent
db7251f1
Refactor `Pl_Base64` to use `std::string` for input buffering, simplifying `writ…
…e` and `finish` logic
Showing
2 changed files
with
17 additions
and
29 deletions
libqpdf/Pl_Base64.cc
| @@ -39,14 +39,7 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) : | @@ -39,14 +39,7 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) : | ||
| 39 | void | 39 | void |
| 40 | Pl_Base64::write(unsigned char const* data, size_t len) | 40 | Pl_Base64::write(unsigned char const* data, size_t len) |
| 41 | { | 41 | { |
| 42 | - if (finished) { | ||
| 43 | - throw std::logic_error("Pl_Base64 used after finished"); | ||
| 44 | - } | ||
| 45 | - if (action == a_decode) { | ||
| 46 | - decode(data, len); | ||
| 47 | - } else { | ||
| 48 | - encode(data, len); | ||
| 49 | - } | 42 | + in_buffer.append(reinterpret_cast<const char*>(data), len); |
| 50 | } | 43 | } |
| 51 | 44 | ||
| 52 | void | 45 | void |
| @@ -57,7 +50,7 @@ Pl_Base64::decode(unsigned char const* data, size_t len) | @@ -57,7 +50,7 @@ Pl_Base64::decode(unsigned char const* data, size_t len) | ||
| 57 | if (!util::is_space(to_c(*p))) { | 50 | if (!util::is_space(to_c(*p))) { |
| 58 | buf[pos++] = *p; | 51 | buf[pos++] = *p; |
| 59 | if (pos == 4) { | 52 | if (pos == 4) { |
| 60 | - flush(); | 53 | + flush_decode(); |
| 61 | } | 54 | } |
| 62 | } | 55 | } |
| 63 | ++p; | 56 | ++p; |
| @@ -72,7 +65,7 @@ Pl_Base64::encode(unsigned char const* data, size_t len) | @@ -72,7 +65,7 @@ Pl_Base64::encode(unsigned char const* data, size_t len) | ||
| 72 | while (len > 0) { | 65 | while (len > 0) { |
| 73 | buf[pos++] = *p; | 66 | buf[pos++] = *p; |
| 74 | if (pos == 3) { | 67 | if (pos == 3) { |
| 75 | - flush(); | 68 | + flush_encode(); |
| 76 | } | 69 | } |
| 77 | ++p; | 70 | ++p; |
| 78 | --len; | 71 | --len; |
| @@ -80,17 +73,6 @@ Pl_Base64::encode(unsigned char const* data, size_t len) | @@ -80,17 +73,6 @@ Pl_Base64::encode(unsigned char const* data, size_t len) | ||
| 80 | } | 73 | } |
| 81 | 74 | ||
| 82 | void | 75 | void |
| 83 | -Pl_Base64::flush() | ||
| 84 | -{ | ||
| 85 | - if (action == a_decode) { | ||
| 86 | - flush_decode(); | ||
| 87 | - } else { | ||
| 88 | - flush_encode(); | ||
| 89 | - } | ||
| 90 | - reset(); | ||
| 91 | -} | ||
| 92 | - | ||
| 93 | -void | ||
| 94 | Pl_Base64::flush_decode() | 76 | Pl_Base64::flush_decode() |
| 95 | { | 77 | { |
| 96 | if (end_of_data) { | 78 | if (end_of_data) { |
| @@ -129,6 +111,7 @@ Pl_Base64::flush_decode() | @@ -129,6 +111,7 @@ Pl_Base64::flush_decode() | ||
| 129 | }; | 111 | }; |
| 130 | 112 | ||
| 131 | next()->write(out, QIntC::to_size(3 - pad)); | 113 | next()->write(out, QIntC::to_size(3 - pad)); |
| 114 | + reset(); | ||
| 132 | } | 115 | } |
| 133 | 116 | ||
| 134 | void | 117 | void |
| @@ -162,23 +145,28 @@ Pl_Base64::flush_encode() | @@ -162,23 +145,28 @@ Pl_Base64::flush_encode() | ||
| 162 | out[3 - i] = '='; | 145 | out[3 - i] = '='; |
| 163 | } | 146 | } |
| 164 | next()->write(out, 4); | 147 | next()->write(out, 4); |
| 148 | + reset(); | ||
| 165 | } | 149 | } |
| 166 | 150 | ||
| 167 | void | 151 | void |
| 168 | Pl_Base64::finish() | 152 | Pl_Base64::finish() |
| 169 | { | 153 | { |
| 170 | - if (pos > 0) { | ||
| 171 | - if (finished) { | ||
| 172 | - throw std::logic_error("Pl_Base64 used after finished"); | ||
| 173 | - } | ||
| 174 | - if (action == a_decode) { | 154 | + if (action == a_decode) { |
| 155 | + decode(reinterpret_cast<unsigned char const*>(in_buffer.data()), in_buffer.size()); | ||
| 156 | + if (pos > 0) { | ||
| 175 | for (size_t i = pos; i < 4; ++i) { | 157 | for (size_t i = pos; i < 4; ++i) { |
| 176 | buf[i] = '='; | 158 | buf[i] = '='; |
| 177 | } | 159 | } |
| 160 | + flush_decode(); | ||
| 161 | + } | ||
| 162 | + } else { | ||
| 163 | + encode(reinterpret_cast<unsigned char const*>(in_buffer.data()), in_buffer.size()); | ||
| 164 | + if (pos > 0) { | ||
| 165 | + flush_encode(); | ||
| 178 | } | 166 | } |
| 179 | - flush(); | ||
| 180 | } | 167 | } |
| 181 | - finished = true; | 168 | + in_buffer.clear(); |
| 169 | + in_buffer.shrink_to_fit(); | ||
| 182 | next()->finish(); | 170 | next()->finish(); |
| 183 | } | 171 | } |
| 184 | 172 |
libqpdf/qpdf/Pl_Base64.hh
| @@ -15,7 +15,6 @@ class Pl_Base64 final: public Pipeline | @@ -15,7 +15,6 @@ class Pl_Base64 final: public Pipeline | ||
| 15 | private: | 15 | private: |
| 16 | void decode(unsigned char const* buf, size_t len); | 16 | void decode(unsigned char const* buf, size_t len); |
| 17 | void encode(unsigned char const* buf, size_t len); | 17 | void encode(unsigned char const* buf, size_t len); |
| 18 | - void flush(); | ||
| 19 | void flush_decode(); | 18 | void flush_decode(); |
| 20 | void flush_encode(); | 19 | void flush_encode(); |
| 21 | void reset(); | 20 | void reset(); |
| @@ -23,6 +22,7 @@ class Pl_Base64 final: public Pipeline | @@ -23,6 +22,7 @@ class Pl_Base64 final: public Pipeline | ||
| 23 | action_e action; | 22 | action_e action; |
| 24 | unsigned char buf[4]{0, 0, 0, 0}; | 23 | unsigned char buf[4]{0, 0, 0, 0}; |
| 25 | size_t pos{0}; | 24 | size_t pos{0}; |
| 25 | + std::string in_buffer; | ||
| 26 | bool end_of_data{false}; | 26 | bool end_of_data{false}; |
| 27 | bool finished{false}; | 27 | bool finished{false}; |
| 28 | }; | 28 | }; |