From 87ee8ad0710ccc8e7712487caaf707032974dca1 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 25 Jul 2024 10:22:35 +0100 Subject: [PATCH] In QPDFParser constructor add add parameter parse_pdf Prepare for treating indirect references differently depending on whether we are parsing a PDF file (in which case reference to objects not in the xref table are null even if they are in the object cache) or whether parse from user code (in which case an indirect reference can refer to a user created object). --- libqpdf/QPDF.cc | 10 ++++++---- libqpdf/QPDFObjectHandle.cc | 6 ++++-- libqpdf/QPDFOutlineDocumentHelper.cc | 2 +- libqpdf/QPDF_json.cc | 4 ++-- libqpdf/qpdf/QPDFParser.hh | 3 ++- libqpdf/qpdf/qpdf-c_impl.hh | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 8c588c7c..93d50829 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -1462,7 +1462,8 @@ QPDF::readTrailer() { qpdf_offset_t offset = m->file->tell(); bool empty = false; - auto object = QPDFParser(m->file, "trailer", m->tokenizer, nullptr, this).parse(empty, false); + auto object = + QPDFParser(m->file, "trailer", m->tokenizer, nullptr, this, true).parse(empty, false); if (empty) { // Nothing in the PDF spec appears to allow empty objects, but they have been encountered in // actual PDF files and Adobe Reader appears to ignore them. @@ -1484,8 +1485,9 @@ QPDF::readObject(std::string const& description, QPDFObjGen og) StringDecrypter decrypter{this, og}; StringDecrypter* decrypter_ptr = m->encp->encrypted ? &decrypter : nullptr; - auto object = QPDFParser(m->file, m->last_object_description, m->tokenizer, decrypter_ptr, this) - .parse(empty, false); + auto object = + QPDFParser(m->file, m->last_object_description, m->tokenizer, decrypter_ptr, this, true) + .parse(empty, false); if (empty) { // Nothing in the PDF spec appears to allow empty objects, but they have been encountered in // actual PDF files and Adobe Reader appears to ignore them. @@ -1604,7 +1606,7 @@ QPDF::readObjectInStream(std::shared_ptr& input, int obj) m->last_object_description += " 0"; bool empty = false; - auto object = QPDFParser(input, m->last_object_description, m->tokenizer, nullptr, this) + auto object = QPDFParser(input, m->last_object_description, m->tokenizer, nullptr, this, true) .parse(empty, false); if (empty) { // Nothing in the PDF spec appears to allow empty objects, but they have been encountered in diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 4eaa2236..65b3c836 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2146,7 +2146,8 @@ QPDFObjectHandle::parseContentStream_data( tokenizer.readToken(input, "content", true); qpdf_offset_t offset = input->getLastOffset(); input->seek(offset, SEEK_SET); - auto obj = QPDFParser(input, "content", tokenizer, nullptr, context).parse(empty, true); + auto obj = + QPDFParser(input, "content", tokenizer, nullptr, context, false).parse(empty, true); if (!obj.isInitialized()) { // EOF break; @@ -2205,7 +2206,8 @@ QPDFObjectHandle::parse( StringDecrypter* decrypter, QPDF* context) { - return QPDFParser(input, object_description, tokenizer, decrypter, context).parse(empty, false); + return QPDFParser(input, object_description, tokenizer, decrypter, context, false) + .parse(empty, false); } #ifndef QPDF_FUTURE diff --git a/libqpdf/QPDFOutlineDocumentHelper.cc b/libqpdf/QPDFOutlineDocumentHelper.cc index dd9db5c1..ba4e4291 100644 --- a/libqpdf/QPDFOutlineDocumentHelper.cc +++ b/libqpdf/QPDFOutlineDocumentHelper.cc @@ -71,7 +71,7 @@ QPDFOutlineDocumentHelper::resolveNamedDest(QPDFObjectHandle name) m->dest_dict = qpdf.getRoot().getKey("/Dests"); } QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest"); - result= m->dest_dict.getKeyIfDict(name.getName()); + result = m->dest_dict.getKeyIfDict(name.getName()); } else if (name.isString()) { if (!m->names_dest) { auto dests = qpdf.getRoot().getKey("/Names").getKeyIfDict("/Dests"); diff --git a/libqpdf/QPDF_json.cc b/libqpdf/QPDF_json.cc index 30aca255..dd4672fe 100644 --- a/libqpdf/QPDF_json.cc +++ b/libqpdf/QPDF_json.cc @@ -272,10 +272,10 @@ class QPDF::JSONReactor: public JSON::Reactor struct StackFrame { StackFrame(state_e state) : - state(state){}; + state(state) {}; StackFrame(state_e state, QPDFObjectHandle&& object) : state(state), - object(object){}; + object(object) {}; state_e state; QPDFObjectHandle object; }; diff --git a/libqpdf/qpdf/QPDFParser.hh b/libqpdf/qpdf/QPDFParser.hh index 9e2c3c2e..d6b76a21 100644 --- a/libqpdf/qpdf/QPDFParser.hh +++ b/libqpdf/qpdf/QPDFParser.hh @@ -16,7 +16,8 @@ class QPDFParser std::string const& object_description, QPDFTokenizer& tokenizer, QPDFObjectHandle::StringDecrypter* decrypter, - QPDF* context) : + QPDF* context, + bool parse_pdf) : input(input), object_description(object_description), tokenizer(tokenizer), diff --git a/libqpdf/qpdf/qpdf-c_impl.hh b/libqpdf/qpdf/qpdf-c_impl.hh index 0d52cf10..866b6252 100644 --- a/libqpdf/qpdf/qpdf-c_impl.hh +++ b/libqpdf/qpdf/qpdf-c_impl.hh @@ -16,7 +16,7 @@ struct _qpdf_data _qpdf_data() = default; _qpdf_data(std::unique_ptr&& qpdf) : - qpdf(std::move(qpdf)){}; + qpdf(std::move(qpdf)) {}; ~_qpdf_data() = default;