From 6408c5c364e8f567350bd935da97d5ef14acdaa2 Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 14 Oct 2024 11:11:24 +0100 Subject: [PATCH] Refactor Qbjects::update_table Decompose into new methods update_entry and Entry::update. --- libqpdf/QPDF_objects.cc | 40 ++++++++++++++++++++++-------------- libqpdf/qpdf/QPDF_objects.hh | 7 ++++++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/libqpdf/QPDF_objects.cc b/libqpdf/QPDF_objects.cc index aebcd910..7e0371a3 100644 --- a/libqpdf/QPDF_objects.cc +++ b/libqpdf/QPDF_objects.cc @@ -1562,8 +1562,7 @@ Objects::resolve(int id, int gen) // has to be resolved during object parsing, such as stream length. QTC::TC("qpdf", "QPDF recursion loop in resolve"); qpdf.warn(qpdf.damagedPDF("", "loop detected resolving object " + og.unparse(' '))); - update_table(id, gen, QPDF_Null::create()); - return table[id].object.get(); + return update_table(id, gen, QPDF_Null::create()).get(); } ResolveRecorder rr(&qpdf, og); @@ -1597,7 +1596,7 @@ Objects::resolve(int id, int gen) if (unresolved(id, gen)) { // PDF spec says unknown objects resolve to the null object. QTC::TC("qpdf", "QPDF resolve failure to null"); - update_table(id, gen, QPDF_Null::create()); + return update_table(id, gen, QPDF_Null::create()).get(); } return table[id].object.get(); @@ -1716,19 +1715,32 @@ Objects::~Objects() } } -void -Objects::update_table(int id, int gen, const std::shared_ptr& obj) +std::shared_ptr +Objects::Entry::update(int a_gen, const std::shared_ptr& obj) { - obj->make_indirect(qpdf, id, gen); - auto& e = table[id]; - if (e) { - if (e.gen != gen) { + if (*this) { + if (gen != a_gen) { throw std::logic_error("Internal eror in Objects::update_table"); } - e.object->assign(obj); + object->assign(obj); } else { - e = Entry(gen, obj); + gen = a_gen; + object = obj; } + return object; +} + +std::shared_ptr +Objects::update_entry(Entry& e, int id, int gen, const std::shared_ptr& obj) +{ + obj->make_indirect(qpdf, id, gen); + return e.update(gen, obj); +} + +std::shared_ptr +Objects::update_table(int id, int gen, const std::shared_ptr& obj) +{ + return update_entry(table[id], id, gen, obj); } bool @@ -1781,9 +1793,7 @@ Objects::initialize() std::shared_ptr Objects::make_indirect(std::shared_ptr const& obj) { - auto next = next_id(); - update_table(next, 0, obj); - return table[next].object; + return update_table(next_id(), 0, obj); } std::shared_ptr @@ -1853,7 +1863,7 @@ Objects::replace(int id, int gen, QPDFObjectHandle oh) if (e && e.gen < gen) { erase(id, gen); } - update_table(id, gen, oh.getObj()); + update_entry(e, id, gen, oh.getObj()); } void diff --git a/libqpdf/qpdf/QPDF_objects.hh b/libqpdf/qpdf/QPDF_objects.hh index 9812ade2..da8b1c71 100644 --- a/libqpdf/qpdf/QPDF_objects.hh +++ b/libqpdf/qpdf/QPDF_objects.hh @@ -523,6 +523,8 @@ class QPDF::Objects return static_cast(object); } + std::shared_ptr update(int gen, const std::shared_ptr& obj); + int gen{0}; std::shared_ptr object; }; // Entry @@ -531,7 +533,10 @@ class QPDF::Objects bool unresolved(int id, int gen); int next_id(); - void update_table(int id, int gen, std::shared_ptr const& object); + std::shared_ptr + update_entry(Entry& e, int id, int gen, const std::shared_ptr& obj); + std::shared_ptr + update_table(int id, int gen, std::shared_ptr const& object); QPDFObjectHandle readObjectInStream(std::shared_ptr& input, int obj); void resolveObjectsInStream(int obj_stream_number);