2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 00:10:54 +00:00

Delay adding offsets to object descriptions until necessary

This commit is contained in:
m-holger 2022-12-16 18:08:56 +00:00 committed by Jay Berkenbilt
parent c833c25499
commit 0ef2def8ad
3 changed files with 18 additions and 18 deletions

View File

@ -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<std::string>(
input->getName() + ", " + object_description + " at offset " +
std::to_string(descr_offset));
obj->setDescription(context, descr, parsed_offset);
obj->setDescription(context, description, parsed_offset);
}
}

View File

@ -20,7 +20,9 @@ class QPDFParser
object_description(object_description),
tokenizer(tokenizer),
decrypter(decrypter),
context(context)
context(context),
description(std::make_shared<std::string>(
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<InputSource> input;
std::string const& object_description;
QPDFTokenizer& tokenizer;
QPDFObjectHandle::StringDecrypter* decrypter;
QPDF* context;
std::shared_ptr<std::string> description;
};
#endif // QPDFPARSER_HH

View File

@ -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