From 92a8c888ba45f33db212f22d9082f2051e00c6d5 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 26 Jul 2025 17:17:33 +0100 Subject: [PATCH] Refactor `Pl_Base64` to add static `encode` and `decode` methods, streamline encoding/decoding logic, and simplify usage in `JSON_writer` and `QPDF_json`. --- libqpdf/Pl_Base64.cc | 41 ++++++++++++++++++++++++++++------------- libqpdf/QPDF_json.cc | 6 +++--- libqpdf/qpdf/JSON_writer.hh | 6 ++---- libqpdf/qpdf/Pl_Base64.hh | 7 +++++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/libqpdf/Pl_Base64.cc b/libqpdf/Pl_Base64.cc index a56392e..68d3fa3 100644 --- a/libqpdf/Pl_Base64.cc +++ b/libqpdf/Pl_Base64.cc @@ -33,9 +33,6 @@ Pl_Base64::Pl_Base64(char const* identifier, Pipeline* next, action_e action) : Pipeline(identifier, next), action(action) { - if (!next) { - throw std::logic_error("Attempt to create Pl_Base64 with nullptr as next"); - } } void @@ -44,9 +41,25 @@ Pl_Base64::write(unsigned char const* data, size_t len) in_buffer.append(reinterpret_cast(data), len); } -void +std::string Pl_Base64::decode(std::string_view data) { + Pl_Base64 p("base64-decode", nullptr, a_decode); + p.decode_internal(data); + return std::move(p.out_buffer); +} + +std::string +Pl_Base64::encode(std::string_view data) +{ + Pl_Base64 p("base64-encode", nullptr, a_encode); + p.encode_internal(data); + return std::move(p.out_buffer); +} + +void +Pl_Base64::decode_internal(std::string_view data) +{ auto len = data.size(); auto res = (len / 4u + 1u) * 3u; out_buffer.reserve(res); @@ -71,7 +84,7 @@ Pl_Base64::decode(std::string_view data) } void -Pl_Base64::encode(std::string_view data) +Pl_Base64::encode_internal(std::string_view data) { auto len = data.size(); static const size_t max_len = (std::string().max_size() / 4u - 1u) * 3u; @@ -177,17 +190,19 @@ void Pl_Base64::finish() { if (action == a_decode) { - decode(in_buffer); + decode_internal(in_buffer); } else { - encode(in_buffer); + encode_internal(in_buffer); + } + if (next()) { + in_buffer.clear(); + in_buffer.shrink_to_fit(); + next()->write(reinterpret_cast(out_buffer.data()), out_buffer.size()); + out_buffer.clear(); + out_buffer.shrink_to_fit(); + next()->finish(); } - in_buffer.clear(); - in_buffer.shrink_to_fit(); - next()->write(reinterpret_cast(out_buffer.data()), out_buffer.size()); - out_buffer.clear(); - out_buffer.shrink_to_fit(); - next()->finish(); } void diff --git a/libqpdf/QPDF_json.cc b/libqpdf/QPDF_json.cc index 88afe93..e27a84b 100644 --- a/libqpdf/QPDF_json.cc +++ b/libqpdf/QPDF_json.cc @@ -216,10 +216,10 @@ static std::function provide_data(std::shared_ptr is, qpdf_offset_t start, qpdf_offset_t end) { return [is, start, end](Pipeline* p) { - Pl_Base64 decode("base64-decode", p, Pl_Base64::a_decode); auto data = is->read(QIntC::to_size(end - start), start); - decode.write(reinterpret_cast(data.data()), data.size()); - decode.finish(); + data = Pl_Base64::decode(data); + p->write(reinterpret_cast(data.data()), data.size()); + p->finish(); }; } diff --git a/libqpdf/qpdf/JSON_writer.hh b/libqpdf/qpdf/JSON_writer.hh index 3f770c5..a77138e 100644 --- a/libqpdf/qpdf/JSON_writer.hh +++ b/libqpdf/qpdf/JSON_writer.hh @@ -32,10 +32,8 @@ class JSON::Writer Writer& writeBase64(std::string_view sv) { - Pl_Concatenate cat{"writer concat", p}; - Pl_Base64 base{"writer base64", &cat, Pl_Base64::a_encode}; - base.write(reinterpret_cast(sv.data()), sv.size()); - base.finish(); + auto encoded = Pl_Base64::encode(sv); + p->write(reinterpret_cast(encoded.data()), encoded.size()); return *this; } diff --git a/libqpdf/qpdf/Pl_Base64.hh b/libqpdf/qpdf/Pl_Base64.hh index aeb6e59..7d6c47d 100644 --- a/libqpdf/qpdf/Pl_Base64.hh +++ b/libqpdf/qpdf/Pl_Base64.hh @@ -12,9 +12,12 @@ class Pl_Base64 final: public Pipeline void write(unsigned char const* buf, size_t len) final; void finish() final; + static std::string encode(std::string_view data); + static std::string decode(std::string_view data); + private: - void decode(std::string_view data); - void encode(std::string_view data); + void decode_internal(std::string_view data); + void encode_internal(std::string_view data); void flush_decode(); void flush_encode(); void reset(); -- libgit2 0.21.4