From 0ef2def8ad1f280adc34825b1588d9811d9ac031 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 16 Dec 2022 18:08:56 +0000 Subject: Delay adding offsets to object descriptions until necessary --- libqpdf/QPDFParser.cc | 19 ++++++------------- libqpdf/qpdf/QPDFParser.hh | 10 +++++----- libqpdf/qpdf/QPDFValue.hh | 7 +++++++ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/libqpdf/QPDFParser.cc b/libqpdf/QPDFParser.cc index 70c328e1..0ce97a8e 100644 --- a/libqpdf/QPDFParser.cc +++ b/libqpdf/QPDFParser.cc @@ -288,8 +288,7 @@ QPDFParser::parse(bool& empty, bool content_stream) if (!indirect_ref && !is_null) { // No need to set description for direct nulls - they will // become implicit. - auto os = input->getLastOffset(); - setDescription(object, os, os); + setDescription(object, input->getLastOffset()); } set_offset = true; olist.push_back(is_null ? null_oh : object); @@ -312,7 +311,7 @@ QPDFParser::parse(bool& empty, bool content_stream) state_stack.pop_back(); if (old_state == st_array) { object = QPDFObjectHandle::newArray(olist); - setDescription(object, offset, offset - 1); + setDescription(object, offset - 1); // The `offset` points to the next of "[". Set the rewind // offset to point to the beginning of "[". This has been // explicitly tested with whitespace surrounding the array start @@ -386,7 +385,7 @@ QPDFParser::parse(bool& empty, bool content_stream) dict["/Contents"].setParsedOffset(frame.contents_offset); } object = QPDFObjectHandle::newDictionary(dict); - setDescription(object, offset, offset - 2); + setDescription(object, offset - 2); // The `offset` points to the next of "<<". Set the rewind // offset to point to the beginning of "<<". This has been // explicitly tested with whitespace surrounding the dictionary @@ -407,22 +406,16 @@ QPDFParser::parse(bool& empty, bool content_stream) object = QPDFObjectHandle::newNull(); } if (!set_offset) { - setDescription(object, offset, offset); + setDescription(object, offset); } return object; } void -QPDFParser::setDescription( - QPDFObjectHandle oh, - qpdf_offset_t descr_offset, - qpdf_offset_t parsed_offset) const +QPDFParser::setDescription(QPDFObjectHandle oh, qpdf_offset_t parsed_offset) { if (auto& obj = oh.obj) { - auto descr = std::make_shared( - input->getName() + ", " + object_description + " at offset " + - std::to_string(descr_offset)); - obj->setDescription(context, descr, parsed_offset); + obj->setDescription(context, description, parsed_offset); } } diff --git a/libqpdf/qpdf/QPDFParser.hh b/libqpdf/qpdf/QPDFParser.hh index 28768df2..02ae83ed 100644 --- a/libqpdf/qpdf/QPDFParser.hh +++ b/libqpdf/qpdf/QPDFParser.hh @@ -20,7 +20,9 @@ class QPDFParser object_description(object_description), tokenizer(tokenizer), decrypter(decrypter), - context(context) + context(context), + description(std::make_shared( + input->getName() + ", " + object_description + " at offset $PO")) { } virtual ~QPDFParser() = default; @@ -40,15 +42,13 @@ class QPDFParser void warn(qpdf_offset_t offset, std::string const& msg) const; void warn(std::string const& msg) const; static void warn(QPDF*, QPDFExc const&); - void setDescription( - QPDFObjectHandle oh, - qpdf_offset_t descr_offset, - qpdf_offset_t parsed_offset = -1) const; + void setDescription(QPDFObjectHandle oh, qpdf_offset_t parsed_offset); std::shared_ptr input; std::string const& object_description; QPDFTokenizer& tokenizer; QPDFObjectHandle::StringDecrypter* decrypter; QPDF* context; + std::shared_ptr description; }; #endif // QPDFPARSER_HH diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index c7e63d6c..2d3b9de3 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -52,6 +52,13 @@ class QPDFValue if (auto pos = description.find("$OG"); pos != std::string::npos) { description.replace(pos, 3, og.unparse(' ')); } + if (auto pos = description.find("$PO"); pos != std::string::npos) { + qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2 + : (type_code == ::ot_array) ? 1 + : 0; + + description.replace(pos, 3, std::to_string(parsed_offset + shift)); + } return qpdf != nullptr; } bool -- cgit v1.2.3-70-g09d2