From 306e809332f328117563e3446fd6d240e2adc112 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 16 Oct 2025 02:13:46 +0100 Subject: [PATCH] Refactor linearization data: encapsulate `first_xref_item_offset` and `uncompressed_after_compressed` in `QPDF::Doc::Objects`, update usage in `QPDF_linearization`, and streamline related logic. --- libqpdf/QPDF_linearization.cc | 6 +++--- libqpdf/QPDF_objects.cc | 6 +++--- libqpdf/qpdf/QPDF_private.hh | 23 ++++++++++++++++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index 6bc128a..dfeb2dc 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -713,10 +713,10 @@ Lin::checkLinearizationInternal() break; } } - if (m->file->tell() != m->first_xref_item_offset) { + if (m->file->tell() != objects.first_xref_item_offset()) { linearizationWarning( "space before first xref item (/T) mismatch (computed = " + - std::to_string(m->first_xref_item_offset) + + std::to_string(objects.first_xref_item_offset()) + "; file = " + std::to_string(m->file->tell())); } @@ -727,7 +727,7 @@ Lin::checkLinearizationInternal() // compressed objects are supposed to be at the end of the containing xref section if any object // streams are in use. - if (m->uncompressed_after_compressed) { + if (objects.uncompressed_after_compressed()) { linearizationWarning( "linearized file contains an uncompressed object after a compressed " "one in a cross-reference stream"); diff --git a/libqpdf/QPDF_objects.cc b/libqpdf/QPDF_objects.cc index a9e8b27..cd8a849 100644 --- a/libqpdf/QPDF_objects.cc +++ b/libqpdf/QPDF_objects.cc @@ -694,7 +694,7 @@ Objects::read_xrefTable(qpdf_offset_t xref_offset) for (qpdf_offset_t i = obj; i - num < obj; ++i) { if (i == 0) { // This is needed by checkLinearization() - m->first_xref_item_offset = m->file->tell(); + first_xref_item_offset_ = m->file->tell(); } // For xref_table, these will always be small enough to be ints qpdf_offset_t f1 = 0; @@ -956,14 +956,14 @@ Objects::processXRefStream( // object record, in which case the generation number appears as the third field. if (saw_first_compressed_object) { if (fields[0] != 2) { - m->uncompressed_after_compressed = true; + uncompressed_after_compressed_ = true; } } else if (fields[0] == 2) { saw_first_compressed_object = true; } if (obj == 0) { // This is needed by checkLinearization() - m->first_xref_item_offset = xref_offset; + first_xref_item_offset_ = xref_offset; } else if (fields[0] == 0) { // Ignore fields[2], which we don't care about in this case. This works around the // issue of some PDF files that put invalid values, like -1, here for deleted diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index c184262..79efe70 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -323,6 +323,7 @@ class QPDF::Doc qpdf::Doc::Config& cf; QPDF::Doc::Pages& pages; + QPDF::Doc::Objects& objects; }; Doc() = delete; @@ -957,6 +958,17 @@ class QPDF::Doc::Objects: Common return streams_; } + // actual value from file + qpdf_offset_t + first_xref_item_offset() const + { + return first_xref_item_offset_; + } + bool + uncompressed_after_compressed() const + { + return uncompressed_after_compressed_; + } void parse(char const* password); std::shared_ptr const& resolve(QPDFObjGen og); void inParse(bool); @@ -1028,6 +1040,10 @@ class QPDF::Doc::Objects: Common Foreign foreign_; Streams streams_; + + // Linearization data + qpdf_offset_t first_xref_item_offset_{0}; // actual value from file + bool uncompressed_after_compressed_{false}; }; // class QPDF::Doc::Objects // This class is used to represent a PDF Pages tree. @@ -1172,10 +1188,6 @@ class QPDF::Members: Doc bool in_parse{false}; bool parsed{false}; std::set resolved_object_streams; - - // Linearization data - qpdf_offset_t first_xref_item_offset{0}; // actual value from file - bool uncompressed_after_compressed{false}; }; // The Resolver class is restricted to QPDFObject and BaseHandle so that only it can resolve @@ -1197,7 +1209,8 @@ inline QPDF::Doc::Common::Common(QPDF& qpdf, QPDF::Members* m) : qpdf(qpdf), m(m), cf(m->cf), - pages(m->pages) + pages(m->pages), + objects(m->objects) { } -- libgit2 0.21.4