From 2033b3769a7405c77972012316e500ac6df4b006 Mon Sep 17 00:00:00 2001 From: m-holger Date: Tue, 25 Mar 2025 13:03:49 +0000 Subject: [PATCH] Amend pl::Count to use a numeric rather than string identifier --- include/qpdf/QPDFWriter.hh | 4 ++-- libqpdf/QPDFWriter.cc | 19 +++++++++---------- libqpdf/QPDF_linearization.cc | 6 ++---- libqpdf/qpdf/Pipeline_private.hh | 24 +++++++++++++++++------- libqpdf/qpdf/QPDFWriter_private.hh | 2 +- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index a33f916..e79a522 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -474,8 +474,8 @@ class QPDFWriter ~PipelinePopper(); private: - QPDFWriter* qw; - std::string stack_id; + QPDFWriter* qw{nullptr}; + unsigned long stack_id{0}; }; unsigned int bytesNeeded(long long n); diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 79d4498..0daa066 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -911,7 +911,7 @@ QPDFWriter::pushPipeline(Pipeline* p) void QPDFWriter::initializePipelineStack(Pipeline* p) { - m->pipeline = new pl::Count("pipeline stack base", p); + m->pipeline = new pl::Count(1, p); m->to_delete.emplace_back(std::shared_ptr(m->pipeline)); m->pipeline_stack.emplace_back(m->pipeline); } @@ -933,26 +933,25 @@ void QPDFWriter::activatePipelineStack( PipelinePopper& pp, bool discard, std::string* str, std::unique_ptr link) { - std::string stack_id("stack " + std::to_string(m->next_stack_id)); pl::Count* c; if (link) { - c = new pl::Count(stack_id.c_str(), m->count_buffer, std::move(link)); + c = new pl::Count(m->next_stack_id, m->count_buffer, std::move(link)); } else if (discard) { - c = new pl::Count(stack_id.c_str(), nullptr); + c = new pl::Count(m->next_stack_id, nullptr); } else if (!str) { - c = new pl::Count(stack_id.c_str(), m->pipeline_stack.back()); + c = new pl::Count(m->next_stack_id, m->pipeline_stack.back()); } else { - c = new pl::Count(stack_id.c_str(), *str); + c = new pl::Count(m->next_stack_id, *str); } - ++m->next_stack_id; + pp.stack_id = m->next_stack_id; m->pipeline_stack.emplace_back(c); m->pipeline = c; - pp.stack_id = stack_id; + ++m->next_stack_id; } QPDFWriter::PipelinePopper::~PipelinePopper() { - if (stack_id.empty()) { + if (!stack_id) { return; } qpdf_assert_debug(qw->m->pipeline_stack.size() >= 2); @@ -962,7 +961,7 @@ QPDFWriter::PipelinePopper::~PipelinePopper() // deterministic ID, but I don't think so. As of this writing, this is the only case in which // two dynamically allocated PipelinePopper objects ever exist at the same time, so the // assertion will fail if they get popped out of order from automatic destruction. - qpdf_assert_debug(qw->m->pipeline->getIdentifier() == stack_id); + qpdf_assert_debug(qw->m->pipeline->id() == stack_id); delete qw->m->pipeline_stack.back(); qw->m->pipeline_stack.pop_back(); while (!dynamic_cast(qw->m->pipeline_stack.back())) { diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index 50fe8b6..605f865 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -1758,10 +1758,8 @@ QPDF::generateHintStream( std::string b; auto c = compressed ? std::make_unique( - "count", - b, - pl::create(pl::create(hint_buffer), Pl_Flate::a_deflate)) - : std::make_unique("count", hint_buffer); + 0, b, pl::create(pl::create(hint_buffer), Pl_Flate::a_deflate)) + : std::make_unique(0, hint_buffer); BitWriter w(c.get()); diff --git a/libqpdf/qpdf/Pipeline_private.hh b/libqpdf/qpdf/Pipeline_private.hh index b632e29..c46af4c 100644 --- a/libqpdf/qpdf/Pipeline_private.hh +++ b/libqpdf/qpdf/Pipeline_private.hh @@ -69,27 +69,30 @@ namespace qpdf::pl public: // Count the number of characters written. If 'next' is not set, the content written will be // discarded. - Count(char const* identifier, Pipeline* next = nullptr) : - Pipeline(identifier, next), + Count(unsigned long id, Pipeline* next = nullptr) : + Pipeline("", next), + id_(id), pass_immediately_to_next(next) { } // Count the number of characters written. If 'next' is not set, the content written will be // discarded. - Count(char const* identifier, std::unique_ptr link = nullptr) : - Pipeline(identifier, link ? link->next_pl.get() : nullptr), + Count(unsigned long id, std::unique_ptr link) : + Pipeline("", link ? link->next_pl.get() : nullptr), link(std::move(link)), + id_(id), pass_immediately_to_next(link) { } // Write to 'str'. If 'next' is set, 'str' will be written to 'next' when 'finish' is // called. - Count(char const* identifier, std::string& str, std::unique_ptr link = nullptr) : - Pipeline(identifier, link ? link->next_pl.get() : nullptr), + Count(unsigned long id, std::string& str, std::unique_ptr link = nullptr) : + Pipeline("", link ? link->next_pl.get() : nullptr), str(&str), - link(std::move(link)) + link(std::move(link)), + id_(id) { } @@ -127,10 +130,17 @@ namespace qpdf::pl return str ? static_cast(str->size()) : count; } + unsigned long long + id() const + { + return id_; + } + private: qpdf_offset_t count{0}; std::string* str{nullptr}; std::unique_ptr link{nullptr}; + unsigned long id_{0}; bool pass_immediately_to_next{false}; }; } // namespace qpdf::pl diff --git a/libqpdf/qpdf/QPDFWriter_private.hh b/libqpdf/qpdf/QPDFWriter_private.hh index 767e263..1afb3f6 100644 --- a/libqpdf/qpdf/QPDFWriter_private.hh +++ b/libqpdf/qpdf/QPDFWriter_private.hh @@ -114,7 +114,7 @@ class QPDFWriter::Members std::map contents_to_page_seq; std::map> object_stream_to_objects; std::vector pipeline_stack; - unsigned long long next_stack_id{0}; + unsigned long next_stack_id{2}; std::string count_buffer; bool deterministic_id{false}; Pl_MD5* md5_pipeline{nullptr}; -- libgit2 0.21.4