2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-23 03:18:59 +00:00

Refactor QPDFParser::setDescriptionFromInput and rename to setDescription

Set parsed offset at the same time as setting description.
This commit is contained in:
m-holger 2022-12-15 09:56:46 +00:00 committed by Jay Berkenbilt
parent dab48544d2
commit d03ca88275
6 changed files with 37 additions and 26 deletions

View File

@ -3,6 +3,7 @@
#include <qpdf/QPDF.hh> #include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjGen.hh> #include <qpdf/QPDFObjGen.hh>
#include <qpdf/QPDFObjectHandle.hh> #include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QTC.hh> #include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
@ -287,8 +288,8 @@ QPDFParser::parse(bool& empty, bool content_stream)
if (!indirect_ref && !is_null) { if (!indirect_ref && !is_null) {
// No need to set description for direct nulls - they will // No need to set description for direct nulls - they will
// become implicit. // become implicit.
setDescriptionFromInput(object, input->getLastOffset()); auto os = input->getLastOffset();
object.setParsedOffset(input->getLastOffset()); setDescription(object, os, os);
} }
set_offset = true; set_offset = true;
olist.push_back(is_null ? null_oh : object); olist.push_back(is_null ? null_oh : object);
@ -311,13 +312,12 @@ QPDFParser::parse(bool& empty, bool content_stream)
state_stack.pop_back(); state_stack.pop_back();
if (old_state == st_array) { if (old_state == st_array) {
object = QPDFObjectHandle::newArray(olist); object = QPDFObjectHandle::newArray(olist);
setDescriptionFromInput(object, offset); setDescription(object, offset, offset - 1);
// The `offset` points to the next of "[". Set the rewind // The `offset` points to the next of "[". Set the rewind
// offset to point to the beginning of "[". This has been // offset to point to the beginning of "[". This has been
// explicitly tested with whitespace surrounding the array start // explicitly tested with whitespace surrounding the array start
// delimiter. getLastOffset points to the array end token and // delimiter. getLastOffset points to the array end token and
// therefore can't be used here. // therefore can't be used here.
object.setParsedOffset(offset - 1);
set_offset = true; set_offset = true;
} else if (old_state == st_dictionary) { } else if (old_state == st_dictionary) {
// Convert list to map. Alternating elements are keys. Attempt // Convert list to map. Alternating elements are keys. Attempt
@ -362,7 +362,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
"dictionary ended prematurely; " "dictionary ended prematurely; "
"using null as value for last key"); "using null as value for last key");
val = QPDFObjectHandle::newNull(); val = QPDFObjectHandle::newNull();
setDescriptionFromInput(val, offset); setDescription(val, offset);
} else { } else {
val = olist.at(++i); val = olist.at(++i);
} }
@ -386,13 +386,12 @@ QPDFParser::parse(bool& empty, bool content_stream)
dict["/Contents"].setParsedOffset(frame.contents_offset); dict["/Contents"].setParsedOffset(frame.contents_offset);
} }
object = QPDFObjectHandle::newDictionary(dict); object = QPDFObjectHandle::newDictionary(dict);
setDescriptionFromInput(object, offset); setDescription(object, offset, offset - 2);
// The `offset` points to the next of "<<". Set the rewind // The `offset` points to the next of "<<". Set the rewind
// offset to point to the beginning of "<<". This has been // offset to point to the beginning of "<<". This has been
// explicitly tested with whitespace surrounding the dictionary // explicitly tested with whitespace surrounding the dictionary
// start delimiter. getLastOffset points to the dictionary end // start delimiter. getLastOffset points to the dictionary end
// token and therefore can't be used here. // token and therefore can't be used here.
object.setParsedOffset(offset - 2);
set_offset = true; set_offset = true;
} }
stack.pop_back(); stack.pop_back();
@ -408,20 +407,24 @@ QPDFParser::parse(bool& empty, bool content_stream)
object = QPDFObjectHandle::newNull(); object = QPDFObjectHandle::newNull();
} }
if (!set_offset) { if (!set_offset) {
setDescriptionFromInput(object, offset); setDescription(object, offset, offset);
object.setParsedOffset(offset);
} }
return object; return object;
} }
void void
QPDFParser::setDescriptionFromInput( QPDFParser::setDescription(
QPDFObjectHandle oh, qpdf_offset_t offset) const QPDFObjectHandle oh,
qpdf_offset_t descr_offset,
qpdf_offset_t parsed_offset) const
{ {
oh.setObjectDescription( if (auto& obj = oh.obj) {
obj->setDescription(
context, context,
(input->getName() + ", " + object_description + " at offset " + (input->getName() + ", " + object_description + " at offset " +
std::to_string(offset))); std::to_string(descr_offset)),
parsed_offset);
}
} }
void void

View File

@ -124,8 +124,9 @@ QPDF_Stream::QPDF_Stream(
"object for dictionary"); "object for dictionary");
} }
setDescription( setDescription(
qpdf, qpdf->getFilename() + ", stream object " + og.unparse(' ')); qpdf,
this->parsed_offset = offset; qpdf->getFilename() + ", stream object " + og.unparse(' '),
offset);
} }
std::shared_ptr<QPDFObject> std::shared_ptr<QPDFObject>
@ -282,9 +283,10 @@ QPDF_Stream::getStreamJSON(
} }
void void
QPDF_Stream::setDescription(QPDF* qpdf, std::string const& description) QPDF_Stream::setDescription(
QPDF* qpdf, std::string const& description, qpdf_offset_t offset)
{ {
this->QPDFValue::setDescription(qpdf, description); this->QPDFValue::setDescription(qpdf, description, offset);
setDictDescription(); setDictDescription();
} }

View File

@ -70,9 +70,10 @@ class QPDFObject
} }
void void
setDescription(QPDF* qpdf, std::string const& description) setDescription(
QPDF* qpdf, std::string const& description, qpdf_offset_t offset = -1)
{ {
return value->setDescription(qpdf, description); return value->setDescription(qpdf, description, offset);
} }
bool bool
getDescription(QPDF*& qpdf, std::string& description) getDescription(QPDF*& qpdf, std::string& description)

View File

@ -40,9 +40,11 @@ class QPDFParser
void warn(qpdf_offset_t offset, std::string const& msg) const; void warn(qpdf_offset_t offset, std::string const& msg) const;
void warn(std::string const& msg) const; void warn(std::string const& msg) const;
static void warn(QPDF*, QPDFExc const&); static void warn(QPDF*, QPDFExc const&);
void setParsedOffset(qpdf_offset_t offset);
void void setDescription(
setDescriptionFromInput(QPDFObjectHandle oh, qpdf_offset_t offset) const; QPDFObjectHandle oh,
qpdf_offset_t descr_offset,
qpdf_offset_t parsed_offset = -1) const;
std::shared_ptr<InputSource> input; std::shared_ptr<InputSource> input;
std::string const& object_description; std::string const& object_description;
QPDFTokenizer& tokenizer; QPDFTokenizer& tokenizer;

View File

@ -24,10 +24,12 @@ class QPDFValue
virtual std::string unparse() = 0; virtual std::string unparse() = 0;
virtual JSON getJSON(int json_version) = 0; virtual JSON getJSON(int json_version) = 0;
virtual void virtual void
setDescription(QPDF* qpdf_p, std::string const& description) setDescription(
QPDF* qpdf_p, std::string const& description, qpdf_offset_t offset)
{ {
qpdf = qpdf_p; qpdf = qpdf_p;
object_description = description; object_description = description;
setParsedOffset(offset);
} }
bool bool
getDescription(QPDF*& qpdf_p, std::string& description) getDescription(QPDF*& qpdf_p, std::string& description)

View File

@ -26,7 +26,8 @@ class QPDF_Stream: public QPDFValue
virtual std::shared_ptr<QPDFObject> copy(bool shallow = false); virtual std::shared_ptr<QPDFObject> copy(bool shallow = false);
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(int json_version); virtual JSON getJSON(int json_version);
virtual void setDescription(QPDF*, std::string const&); virtual void
setDescription(QPDF*, std::string const&, qpdf_offset_t offset);
virtual void disconnect(); virtual void disconnect();
QPDFObjectHandle getDict() const; QPDFObjectHandle getDict() const;
bool isDataModified() const; bool isDataModified() const;