From efc70b475317ac1264fa5dbce5d45c4c167bc488 Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 4 Jun 2025 17:04:55 +0100 Subject: [PATCH] In QPDFObjectHandle.cc remove `LastChar` class and refactor content stream processing to simplify buffer handling --- libqpdf/QPDFObjectHandle.cc | 68 ++++++++++++-------------------------------------------------------- libqpdf/qpdf/Pipeline_private.hh | 6 ++++++ 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index f0baa22..440e3e5 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -1,9 +1,13 @@ +#include + #include -#include +#include #include +#include #include #include +#include #include #include #include @@ -11,13 +15,9 @@ #include #include #include - -#include #include -#include #include -#include #include #include #include @@ -174,48 +174,6 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing() throw TerminateParsing(); } -namespace -{ - class LastChar final: public Pipeline - { - public: - LastChar(Pipeline& next); - ~LastChar() final = default; - void write(unsigned char const* data, size_t len) final; - void finish() final; - unsigned char getLastChar(); - - private: - unsigned char last_char{0}; - }; -} // namespace - -LastChar::LastChar(Pipeline& next) : - Pipeline("lastchar", &next) -{ -} - -void -LastChar::write(unsigned char const* data, size_t len) -{ - if (len > 0) { - last_char = data[len - 1]; - } - next()->write(data, len); -} - -void -LastChar::finish() -{ - next()->finish(); -} - -unsigned char -LastChar::getLastChar() -{ - return last_char; -} - std::pair Name::analyzeJSONEncoding(const std::string& name) { @@ -1527,16 +1485,14 @@ void QPDFObjectHandle::pipeContentStreams( Pipeline* p, std::string const& description, std::string& all_description) { - std::vector streams = - arrayOrStreamToStreamArray(description, all_description); bool need_newline = false; - Pl_Buffer buf("concatenated content stream buffer"); - for (auto stream: streams) { + std::string buffer; + pl::String buf(buffer); + for (auto stream: arrayOrStreamToStreamArray(description, all_description)) { if (need_newline) { buf.writeCStr("\n"); } - LastChar lc(buf); - if (!stream.pipeStreamData(&lc, 0, qpdf_dl_specialized)) { + if (!stream.pipeStreamData(&buf, 0, qpdf_dl_specialized)) { QTC::TC("qpdf", "QPDFObjectHandle errors in parsecontent"); throw QPDFExc( qpdf_e_damaged_pdf, @@ -1545,11 +1501,11 @@ QPDFObjectHandle::pipeContentStreams( 0, "errors while decoding content stream"); } - lc.finish(); - need_newline = (lc.getLastChar() != static_cast('\n')); + need_newline = buffer.empty() || buffer.back() != '\n'; QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1); + p->writeString(buffer); + buffer.clear(); } - p->writeString(buf.getString()); p->finish(); } diff --git a/libqpdf/qpdf/Pipeline_private.hh b/libqpdf/qpdf/Pipeline_private.hh index 084bf4e..6ced77d 100644 --- a/libqpdf/qpdf/Pipeline_private.hh +++ b/libqpdf/qpdf/Pipeline_private.hh @@ -46,6 +46,12 @@ namespace qpdf::pl { } + String(std::string& str) : + Pipeline("", nullptr), + str(str) + { + } + ~String() final = default; void -- libgit2 0.21.4