Commit d03ca88275e5f5d83701502f9ab29de833e772db
Committed by
Jay Berkenbilt
1 parent
dab48544
Refactor QPDFParser::setDescriptionFromInput and rename to setDescription
Set parsed offset at the same time as setting description.
Showing
6 changed files
with
37 additions
and
26 deletions
libqpdf/QPDFParser.cc
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | #include <qpdf/QPDF.hh> | 3 | #include <qpdf/QPDF.hh> |
| 4 | #include <qpdf/QPDFObjGen.hh> | 4 | #include <qpdf/QPDFObjGen.hh> |
| 5 | #include <qpdf/QPDFObjectHandle.hh> | 5 | #include <qpdf/QPDFObjectHandle.hh> |
| 6 | +#include <qpdf/QPDFObject_private.hh> | ||
| 6 | #include <qpdf/QTC.hh> | 7 | #include <qpdf/QTC.hh> |
| 7 | #include <qpdf/QUtil.hh> | 8 | #include <qpdf/QUtil.hh> |
| 8 | 9 | ||
| @@ -287,8 +288,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -287,8 +288,8 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 287 | if (!indirect_ref && !is_null) { | 288 | if (!indirect_ref && !is_null) { |
| 288 | // No need to set description for direct nulls - they will | 289 | // No need to set description for direct nulls - they will |
| 289 | // become implicit. | 290 | // become implicit. |
| 290 | - setDescriptionFromInput(object, input->getLastOffset()); | ||
| 291 | - object.setParsedOffset(input->getLastOffset()); | 291 | + auto os = input->getLastOffset(); |
| 292 | + setDescription(object, os, os); | ||
| 292 | } | 293 | } |
| 293 | set_offset = true; | 294 | set_offset = true; |
| 294 | olist.push_back(is_null ? null_oh : object); | 295 | olist.push_back(is_null ? null_oh : object); |
| @@ -311,13 +312,12 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -311,13 +312,12 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 311 | state_stack.pop_back(); | 312 | state_stack.pop_back(); |
| 312 | if (old_state == st_array) { | 313 | if (old_state == st_array) { |
| 313 | object = QPDFObjectHandle::newArray(olist); | 314 | object = QPDFObjectHandle::newArray(olist); |
| 314 | - setDescriptionFromInput(object, offset); | 315 | + setDescription(object, offset, offset - 1); |
| 315 | // The `offset` points to the next of "[". Set the rewind | 316 | // The `offset` points to the next of "[". Set the rewind |
| 316 | // offset to point to the beginning of "[". This has been | 317 | // offset to point to the beginning of "[". This has been |
| 317 | // explicitly tested with whitespace surrounding the array start | 318 | // explicitly tested with whitespace surrounding the array start |
| 318 | // delimiter. getLastOffset points to the array end token and | 319 | // delimiter. getLastOffset points to the array end token and |
| 319 | // therefore can't be used here. | 320 | // therefore can't be used here. |
| 320 | - object.setParsedOffset(offset - 1); | ||
| 321 | set_offset = true; | 321 | set_offset = true; |
| 322 | } else if (old_state == st_dictionary) { | 322 | } else if (old_state == st_dictionary) { |
| 323 | // Convert list to map. Alternating elements are keys. Attempt | 323 | // Convert list to map. Alternating elements are keys. Attempt |
| @@ -362,7 +362,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -362,7 +362,7 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 362 | "dictionary ended prematurely; " | 362 | "dictionary ended prematurely; " |
| 363 | "using null as value for last key"); | 363 | "using null as value for last key"); |
| 364 | val = QPDFObjectHandle::newNull(); | 364 | val = QPDFObjectHandle::newNull(); |
| 365 | - setDescriptionFromInput(val, offset); | 365 | + setDescription(val, offset); |
| 366 | } else { | 366 | } else { |
| 367 | val = olist.at(++i); | 367 | val = olist.at(++i); |
| 368 | } | 368 | } |
| @@ -386,13 +386,12 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -386,13 +386,12 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 386 | dict["/Contents"].setParsedOffset(frame.contents_offset); | 386 | dict["/Contents"].setParsedOffset(frame.contents_offset); |
| 387 | } | 387 | } |
| 388 | object = QPDFObjectHandle::newDictionary(dict); | 388 | object = QPDFObjectHandle::newDictionary(dict); |
| 389 | - setDescriptionFromInput(object, offset); | 389 | + setDescription(object, offset, offset - 2); |
| 390 | // The `offset` points to the next of "<<". Set the rewind | 390 | // The `offset` points to the next of "<<". Set the rewind |
| 391 | // offset to point to the beginning of "<<". This has been | 391 | // offset to point to the beginning of "<<". This has been |
| 392 | // explicitly tested with whitespace surrounding the dictionary | 392 | // explicitly tested with whitespace surrounding the dictionary |
| 393 | // start delimiter. getLastOffset points to the dictionary end | 393 | // start delimiter. getLastOffset points to the dictionary end |
| 394 | // token and therefore can't be used here. | 394 | // token and therefore can't be used here. |
| 395 | - object.setParsedOffset(offset - 2); | ||
| 396 | set_offset = true; | 395 | set_offset = true; |
| 397 | } | 396 | } |
| 398 | stack.pop_back(); | 397 | stack.pop_back(); |
| @@ -408,20 +407,24 @@ QPDFParser::parse(bool& empty, bool content_stream) | @@ -408,20 +407,24 @@ QPDFParser::parse(bool& empty, bool content_stream) | ||
| 408 | object = QPDFObjectHandle::newNull(); | 407 | object = QPDFObjectHandle::newNull(); |
| 409 | } | 408 | } |
| 410 | if (!set_offset) { | 409 | if (!set_offset) { |
| 411 | - setDescriptionFromInput(object, offset); | ||
| 412 | - object.setParsedOffset(offset); | 410 | + setDescription(object, offset, offset); |
| 413 | } | 411 | } |
| 414 | return object; | 412 | return object; |
| 415 | } | 413 | } |
| 416 | 414 | ||
| 417 | void | 415 | void |
| 418 | -QPDFParser::setDescriptionFromInput( | ||
| 419 | - QPDFObjectHandle oh, qpdf_offset_t offset) const | 416 | +QPDFParser::setDescription( |
| 417 | + QPDFObjectHandle oh, | ||
| 418 | + qpdf_offset_t descr_offset, | ||
| 419 | + qpdf_offset_t parsed_offset) const | ||
| 420 | { | 420 | { |
| 421 | - oh.setObjectDescription( | ||
| 422 | - context, | ||
| 423 | - (input->getName() + ", " + object_description + " at offset " + | ||
| 424 | - std::to_string(offset))); | 421 | + if (auto& obj = oh.obj) { |
| 422 | + obj->setDescription( | ||
| 423 | + context, | ||
| 424 | + (input->getName() + ", " + object_description + " at offset " + | ||
| 425 | + std::to_string(descr_offset)), | ||
| 426 | + parsed_offset); | ||
| 427 | + } | ||
| 425 | } | 428 | } |
| 426 | 429 | ||
| 427 | void | 430 | void |
libqpdf/QPDF_Stream.cc
| @@ -124,8 +124,9 @@ QPDF_Stream::QPDF_Stream( | @@ -124,8 +124,9 @@ QPDF_Stream::QPDF_Stream( | ||
| 124 | "object for dictionary"); | 124 | "object for dictionary"); |
| 125 | } | 125 | } |
| 126 | setDescription( | 126 | setDescription( |
| 127 | - qpdf, qpdf->getFilename() + ", stream object " + og.unparse(' ')); | ||
| 128 | - this->parsed_offset = offset; | 127 | + qpdf, |
| 128 | + qpdf->getFilename() + ", stream object " + og.unparse(' '), | ||
| 129 | + offset); | ||
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | std::shared_ptr<QPDFObject> | 132 | std::shared_ptr<QPDFObject> |
| @@ -282,9 +283,10 @@ QPDF_Stream::getStreamJSON( | @@ -282,9 +283,10 @@ QPDF_Stream::getStreamJSON( | ||
| 282 | } | 283 | } |
| 283 | 284 | ||
| 284 | void | 285 | void |
| 285 | -QPDF_Stream::setDescription(QPDF* qpdf, std::string const& description) | 286 | +QPDF_Stream::setDescription( |
| 287 | + QPDF* qpdf, std::string const& description, qpdf_offset_t offset) | ||
| 286 | { | 288 | { |
| 287 | - this->QPDFValue::setDescription(qpdf, description); | 289 | + this->QPDFValue::setDescription(qpdf, description, offset); |
| 288 | setDictDescription(); | 290 | setDictDescription(); |
| 289 | } | 291 | } |
| 290 | 292 |
libqpdf/qpdf/QPDFObject_private.hh
| @@ -70,9 +70,10 @@ class QPDFObject | @@ -70,9 +70,10 @@ class QPDFObject | ||
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | void | 72 | void |
| 73 | - setDescription(QPDF* qpdf, std::string const& description) | 73 | + setDescription( |
| 74 | + QPDF* qpdf, std::string const& description, qpdf_offset_t offset = -1) | ||
| 74 | { | 75 | { |
| 75 | - return value->setDescription(qpdf, description); | 76 | + return value->setDescription(qpdf, description, offset); |
| 76 | } | 77 | } |
| 77 | bool | 78 | bool |
| 78 | getDescription(QPDF*& qpdf, std::string& description) | 79 | getDescription(QPDF*& qpdf, std::string& description) |
libqpdf/qpdf/QPDFParser.hh
| @@ -40,9 +40,11 @@ class QPDFParser | @@ -40,9 +40,11 @@ class QPDFParser | ||
| 40 | void warn(qpdf_offset_t offset, std::string const& msg) const; | 40 | void warn(qpdf_offset_t offset, std::string const& msg) const; |
| 41 | void warn(std::string const& msg) const; | 41 | void warn(std::string const& msg) const; |
| 42 | static void warn(QPDF*, QPDFExc const&); | 42 | static void warn(QPDF*, QPDFExc const&); |
| 43 | - void setParsedOffset(qpdf_offset_t offset); | ||
| 44 | - void | ||
| 45 | - setDescriptionFromInput(QPDFObjectHandle oh, qpdf_offset_t offset) const; | 43 | + |
| 44 | + void setDescription( | ||
| 45 | + QPDFObjectHandle oh, | ||
| 46 | + qpdf_offset_t descr_offset, | ||
| 47 | + qpdf_offset_t parsed_offset = -1) const; | ||
| 46 | std::shared_ptr<InputSource> input; | 48 | std::shared_ptr<InputSource> input; |
| 47 | std::string const& object_description; | 49 | std::string const& object_description; |
| 48 | QPDFTokenizer& tokenizer; | 50 | QPDFTokenizer& tokenizer; |
libqpdf/qpdf/QPDFValue.hh
| @@ -24,10 +24,12 @@ class QPDFValue | @@ -24,10 +24,12 @@ class QPDFValue | ||
| 24 | virtual std::string unparse() = 0; | 24 | virtual std::string unparse() = 0; |
| 25 | virtual JSON getJSON(int json_version) = 0; | 25 | virtual JSON getJSON(int json_version) = 0; |
| 26 | virtual void | 26 | virtual void |
| 27 | - setDescription(QPDF* qpdf_p, std::string const& description) | 27 | + setDescription( |
| 28 | + QPDF* qpdf_p, std::string const& description, qpdf_offset_t offset) | ||
| 28 | { | 29 | { |
| 29 | qpdf = qpdf_p; | 30 | qpdf = qpdf_p; |
| 30 | object_description = description; | 31 | object_description = description; |
| 32 | + setParsedOffset(offset); | ||
| 31 | } | 33 | } |
| 32 | bool | 34 | bool |
| 33 | getDescription(QPDF*& qpdf_p, std::string& description) | 35 | getDescription(QPDF*& qpdf_p, std::string& description) |
libqpdf/qpdf/QPDF_Stream.hh
| @@ -26,7 +26,8 @@ class QPDF_Stream: public QPDFValue | @@ -26,7 +26,8 @@ class QPDF_Stream: public QPDFValue | ||
| 26 | virtual std::shared_ptr<QPDFObject> copy(bool shallow = false); | 26 | virtual std::shared_ptr<QPDFObject> copy(bool shallow = false); |
| 27 | virtual std::string unparse(); | 27 | virtual std::string unparse(); |
| 28 | virtual JSON getJSON(int json_version); | 28 | virtual JSON getJSON(int json_version); |
| 29 | - virtual void setDescription(QPDF*, std::string const&); | 29 | + virtual void |
| 30 | + setDescription(QPDF*, std::string const&, qpdf_offset_t offset); | ||
| 30 | virtual void disconnect(); | 31 | virtual void disconnect(); |
| 31 | QPDFObjectHandle getDict() const; | 32 | QPDFObjectHandle getDict() const; |
| 32 | bool isDataModified() const; | 33 | bool isDataModified() const; |