Commit 92a8c888ba45f33db212f22d9082f2051e00c6d5
1 parent
05e52843
Refactor `Pl_Base64` to add static `encode` and `decode` methods, streamline enc…
…oding/decoding logic, and simplify usage in `JSON_writer` and `QPDF_json`.
Showing
4 changed files
with
38 additions
and
22 deletions
libqpdf/Pl_Base64.cc
| @@ -33,9 +33,6 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) : | @@ -33,9 +33,6 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) : | ||
| 33 | Pipeline(identifier, next), | 33 | Pipeline(identifier, next), |
| 34 | action(action) | 34 | action(action) |
| 35 | { | 35 | { |
| 36 | - if (!next) { | ||
| 37 | - throw std::logic_error("Attempt to create Pl_Base64 with nullptr as next"); | ||
| 38 | - } | ||
| 39 | } | 36 | } |
| 40 | 37 | ||
| 41 | void | 38 | void |
| @@ -44,9 +41,25 @@ Pl_Base64::write(unsigned char const* data, size_t len) | @@ -44,9 +41,25 @@ Pl_Base64::write(unsigned char const* data, size_t len) | ||
| 44 | in_buffer.append(reinterpret_cast<const char*>(data), len); | 41 | in_buffer.append(reinterpret_cast<const char*>(data), len); |
| 45 | } | 42 | } |
| 46 | 43 | ||
| 47 | -void | 44 | +std::string |
| 48 | Pl_Base64::decode(std::string_view data) | 45 | Pl_Base64::decode(std::string_view data) |
| 49 | { | 46 | { |
| 47 | + Pl_Base64 p("base64-decode", nullptr, a_decode); | ||
| 48 | + p.decode_internal(data); | ||
| 49 | + return std::move(p.out_buffer); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +std::string | ||
| 53 | +Pl_Base64::encode(std::string_view data) | ||
| 54 | +{ | ||
| 55 | + Pl_Base64 p("base64-encode", nullptr, a_encode); | ||
| 56 | + p.encode_internal(data); | ||
| 57 | + return std::move(p.out_buffer); | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +void | ||
| 61 | +Pl_Base64::decode_internal(std::string_view data) | ||
| 62 | +{ | ||
| 50 | auto len = data.size(); | 63 | auto len = data.size(); |
| 51 | auto res = (len / 4u + 1u) * 3u; | 64 | auto res = (len / 4u + 1u) * 3u; |
| 52 | out_buffer.reserve(res); | 65 | out_buffer.reserve(res); |
| @@ -71,7 +84,7 @@ Pl_Base64::decode(std::string_view data) | @@ -71,7 +84,7 @@ Pl_Base64::decode(std::string_view data) | ||
| 71 | } | 84 | } |
| 72 | 85 | ||
| 73 | void | 86 | void |
| 74 | -Pl_Base64::encode(std::string_view data) | 87 | +Pl_Base64::encode_internal(std::string_view data) |
| 75 | { | 88 | { |
| 76 | auto len = data.size(); | 89 | auto len = data.size(); |
| 77 | static const size_t max_len = (std::string().max_size() / 4u - 1u) * 3u; | 90 | static const size_t max_len = (std::string().max_size() / 4u - 1u) * 3u; |
| @@ -177,17 +190,19 @@ void | @@ -177,17 +190,19 @@ void | ||
| 177 | Pl_Base64::finish() | 190 | Pl_Base64::finish() |
| 178 | { | 191 | { |
| 179 | if (action == a_decode) { | 192 | if (action == a_decode) { |
| 180 | - decode(in_buffer); | 193 | + decode_internal(in_buffer); |
| 181 | 194 | ||
| 182 | } else { | 195 | } else { |
| 183 | - encode(in_buffer); | 196 | + encode_internal(in_buffer); |
| 197 | + } | ||
| 198 | + if (next()) { | ||
| 199 | + in_buffer.clear(); | ||
| 200 | + in_buffer.shrink_to_fit(); | ||
| 201 | + next()->write(reinterpret_cast<unsigned char const*>(out_buffer.data()), out_buffer.size()); | ||
| 202 | + out_buffer.clear(); | ||
| 203 | + out_buffer.shrink_to_fit(); | ||
| 204 | + next()->finish(); | ||
| 184 | } | 205 | } |
| 185 | - in_buffer.clear(); | ||
| 186 | - in_buffer.shrink_to_fit(); | ||
| 187 | - next()->write(reinterpret_cast<unsigned char const*>(out_buffer.data()), out_buffer.size()); | ||
| 188 | - out_buffer.clear(); | ||
| 189 | - out_buffer.shrink_to_fit(); | ||
| 190 | - next()->finish(); | ||
| 191 | } | 206 | } |
| 192 | 207 | ||
| 193 | void | 208 | void |
libqpdf/QPDF_json.cc
| @@ -216,10 +216,10 @@ static std::function<void(Pipeline*)> | @@ -216,10 +216,10 @@ static std::function<void(Pipeline*)> | ||
| 216 | provide_data(std::shared_ptr<InputSource> is, qpdf_offset_t start, qpdf_offset_t end) | 216 | provide_data(std::shared_ptr<InputSource> is, qpdf_offset_t start, qpdf_offset_t end) |
| 217 | { | 217 | { |
| 218 | return [is, start, end](Pipeline* p) { | 218 | return [is, start, end](Pipeline* p) { |
| 219 | - Pl_Base64 decode("base64-decode", p, Pl_Base64::a_decode); | ||
| 220 | auto data = is->read(QIntC::to_size(end - start), start); | 219 | auto data = is->read(QIntC::to_size(end - start), start); |
| 221 | - decode.write(reinterpret_cast<const unsigned char*>(data.data()), data.size()); | ||
| 222 | - decode.finish(); | 220 | + data = Pl_Base64::decode(data); |
| 221 | + p->write(reinterpret_cast<const unsigned char*>(data.data()), data.size()); | ||
| 222 | + p->finish(); | ||
| 223 | }; | 223 | }; |
| 224 | } | 224 | } |
| 225 | 225 |
libqpdf/qpdf/JSON_writer.hh
| @@ -32,10 +32,8 @@ class JSON::Writer | @@ -32,10 +32,8 @@ class JSON::Writer | ||
| 32 | Writer& | 32 | Writer& |
| 33 | writeBase64(std::string_view sv) | 33 | writeBase64(std::string_view sv) |
| 34 | { | 34 | { |
| 35 | - Pl_Concatenate cat{"writer concat", p}; | ||
| 36 | - Pl_Base64 base{"writer base64", &cat, Pl_Base64::a_encode}; | ||
| 37 | - base.write(reinterpret_cast<unsigned char const*>(sv.data()), sv.size()); | ||
| 38 | - base.finish(); | 35 | + auto encoded = Pl_Base64::encode(sv); |
| 36 | + p->write(reinterpret_cast<unsigned char const*>(encoded.data()), encoded.size()); | ||
| 39 | return *this; | 37 | return *this; |
| 40 | } | 38 | } |
| 41 | 39 |
libqpdf/qpdf/Pl_Base64.hh
| @@ -12,9 +12,12 @@ class Pl_Base64 final: public Pipeline | @@ -12,9 +12,12 @@ class Pl_Base64 final: public Pipeline | ||
| 12 | void write(unsigned char const* buf, size_t len) final; | 12 | void write(unsigned char const* buf, size_t len) final; |
| 13 | void finish() final; | 13 | void finish() final; |
| 14 | 14 | ||
| 15 | + static std::string encode(std::string_view data); | ||
| 16 | + static std::string decode(std::string_view data); | ||
| 17 | + | ||
| 15 | private: | 18 | private: |
| 16 | - void decode(std::string_view data); | ||
| 17 | - void encode(std::string_view data); | 19 | + void decode_internal(std::string_view data); |
| 20 | + void encode_internal(std::string_view data); | ||
| 18 | void flush_decode(); | 21 | void flush_decode(); |
| 19 | void flush_encode(); | 22 | void flush_encode(); |
| 20 | void reset(); | 23 | void reset(); |