From c436f0357b473d0e4a58a3f895190887f7e3626d Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 4 Jun 2025 14:46:07 +0100 Subject: [PATCH] Refactor `QPDFObjectHandle` and related classes to replace `Buffer` usage with `std::string_view`, improving performance and code clarity. --- include/qpdf/QPDF.hh | 3 ++- include/qpdf/QPDFObjectHandle.hh | 2 +- libqpdf/JSON.cc | 4 ++-- libqpdf/Pl_QPDFTokenizer.cc | 12 ++++++++---- libqpdf/QPDF.cc | 9 ++------- libqpdf/QPDFObjectHandle.cc | 18 +++++++----------- libqpdf/QPDFParser.cc | 1 - libqpdf/QPDF_objects.cc | 1 - 8 files changed, 22 insertions(+), 28 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index 2fd40da..f1cc39f 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -95,7 +95,8 @@ class QPDF // Parse a PDF file loaded into a memory buffer. This works exactly like processFile except // that the PDF file is in memory instead of on disk. The description appears in any warning or - // error message in place of the file name. + // error message in place of the file name. The buffer is owned by the caller and must remain + // valid for the lifetime of the QPDF object. QPDF_DLL void processMemoryFile( char const* description, char const* buf, size_t length, char const* password = nullptr); diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 4a9aa57..6eaeedb 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -1351,7 +1351,7 @@ class QPDFObjectHandle: public qpdf::BaseHandle void setParsedOffset(qpdf_offset_t offset); void parseContentStream_internal(std::string const& description, ParserCallbacks* callbacks); static void parseContentStream_data( - std::shared_ptr, + std::string_view stream_data, std::string const& description, ParserCallbacks* callbacks, QPDF* context); diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index 852e53c..236babc 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -2,7 +2,7 @@ #include -#include +#include #include #include #include @@ -1348,7 +1348,7 @@ JSON::parse(InputSource& is, Reactor* reactor) JSON JSON::parse(std::string const& s) { - BufferInputSource bis("json input", s); + is::OffsetBuffer bis("json input", s); JSONParser jp(bis, nullptr); return jp.parse(); } diff --git a/libqpdf/Pl_QPDFTokenizer.cc b/libqpdf/Pl_QPDFTokenizer.cc index b60ab78..a41612b 100644 --- a/libqpdf/Pl_QPDFTokenizer.cc +++ b/libqpdf/Pl_QPDFTokenizer.cc @@ -1,9 +1,13 @@ #include -#include +#include +#include #include + #include +using namespace qpdf; + class Pl_QPDFTokenizer::Members { public: @@ -13,7 +17,8 @@ class Pl_QPDFTokenizer::Members QPDFObjectHandle::TokenFilter* filter{nullptr}; QPDFTokenizer tokenizer; - Pl_Buffer buf{"tokenizer buffer"}; + std::string buffer; + pl::String buf{"pl_tokenizer", nullptr, buffer}; }; Pl_QPDFTokenizer::Pl_QPDFTokenizer( @@ -39,8 +44,7 @@ Pl_QPDFTokenizer::write(unsigned char const* data, size_t len) void Pl_QPDFTokenizer::finish() { - m->buf.finish(); - auto input = BufferInputSource("tokenizer data", m->buf.getBuffer(), true); + auto input = is::OffsetBuffer("tokenizer data", m->buffer); std::string empty; while (true) { auto token = m->tokenizer.readToken(input, empty, true); diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index a0c2054..cf39b61 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -259,12 +258,8 @@ void QPDF::processMemoryFile( char const* description, char const* buf, size_t length, char const* password) { - processInputSource( - std::shared_ptr( - // line-break - new BufferInputSource( - description, new Buffer(QUtil::unsigned_char_pointer(buf), length), true)), - password); + auto is = std::make_shared(description, std::string_view{buf, length}); + processInputSource(is, password); } void diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 84d88d3..b8d8639 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -1452,10 +1451,7 @@ QPDFObjectHandle QPDFObjectHandle::parse( QPDF* context, std::string const& object_str, std::string const& object_description) { - // BufferInputSource does not modify the input, but Buffer either requires a string& or copies - // the string. - Buffer buf(const_cast(object_str)); - auto input = BufferInputSource("parsed object", &buf); + auto input = is::OffsetBuffer("parsed object", object_str); auto result = QPDFParser::parse(input, object_description, context); size_t offset = QIntC::to_size(input.tell()); while (offset < object_str.length()) { @@ -1549,11 +1545,11 @@ void QPDFObjectHandle::parseContentStream_internal( std::string const& description, ParserCallbacks* callbacks) { - Pl_Buffer buf("concatenated stream data buffer"); + std::string stream_data; + pl::String buf(stream_data); std::string all_description; pipeContentStreams(&buf, description, all_description); - auto stream_data = buf.getBufferSharedPointer(); - callbacks->contentSize(stream_data->getSize()); + callbacks->contentSize(stream_data.size()); try { parseContentStream_data(stream_data, all_description, callbacks, getOwningQPDF()); } catch (TerminateParsing&) { @@ -1564,13 +1560,13 @@ QPDFObjectHandle::parseContentStream_internal( void QPDFObjectHandle::parseContentStream_data( - std::shared_ptr stream_data, + std::string_view stream_data, std::string const& description, ParserCallbacks* callbacks, QPDF* context) { - size_t stream_length = stream_data->getSize(); - auto input = BufferInputSource(description, stream_data.get()); + size_t stream_length = stream_data.size(); + auto input = is::OffsetBuffer(description, stream_data); Tokenizer tokenizer; tokenizer.allowEOF(); auto sp_description = QPDFParser::make_description(description, "content"); diff --git a/libqpdf/QPDFParser.cc b/libqpdf/QPDFParser.cc index 942e623..f671f64 100644 --- a/libqpdf/QPDFParser.cc +++ b/libqpdf/QPDFParser.cc @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/libqpdf/QPDF_objects.cc b/libqpdf/QPDF_objects.cc index 4591677..70eea69 100644 --- a/libqpdf/QPDF_objects.cc +++ b/libqpdf/QPDF_objects.cc @@ -12,7 +12,6 @@ #include #include -#include #include #include #include -- libgit2 0.21.4