2022-08-16 12:59:32 +00:00
|
|
|
#include <qpdf/QPDFParser.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDF.hh>
|
2022-08-10 12:16:06 +00:00
|
|
|
#include <qpdf/QPDFObjGen.hh>
|
2022-08-16 12:59:32 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2022-12-15 09:56:46 +00:00
|
|
|
#include <qpdf/QPDFObject_private.hh>
|
2022-12-19 10:50:46 +00:00
|
|
|
#include <qpdf/QPDF_Array.hh>
|
|
|
|
#include <qpdf/QPDF_Bool.hh>
|
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
#include <qpdf/QPDF_InlineImage.hh>
|
|
|
|
#include <qpdf/QPDF_Integer.hh>
|
|
|
|
#include <qpdf/QPDF_Name.hh>
|
|
|
|
#include <qpdf/QPDF_Null.hh>
|
|
|
|
#include <qpdf/QPDF_Operator.hh>
|
|
|
|
#include <qpdf/QPDF_Real.hh>
|
|
|
|
#include <qpdf/QPDF_Reserved.hh>
|
|
|
|
#include <qpdf/QPDF_Stream.hh>
|
|
|
|
#include <qpdf/QPDF_String.hh>
|
2022-08-16 12:59:32 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2022-12-19 10:50:46 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2023-10-30 20:03:00 +00:00
|
|
|
using ObjectPtr = std::shared_ptr<QPDFObject>;
|
|
|
|
|
2022-08-16 12:59:32 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFParser::parse(bool& empty, bool content_stream)
|
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// This method must take care not to resolve any objects. Don't check the type of any object
|
|
|
|
// without first ensuring that it is a direct object. Otherwise, doing so may have the side
|
|
|
|
// effect of reading the object and changing the file pointer. If you do this, it will cause a
|
|
|
|
// logic error to be thrown from QPDF::inParse().
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
QPDF::ParseGuard pg(context);
|
|
|
|
empty = false;
|
2023-10-30 20:03:00 +00:00
|
|
|
start = input->tell();
|
2023-10-30 12:21:34 +00:00
|
|
|
|
2023-10-30 13:42:00 +00:00
|
|
|
if (!tokenizer.nextToken(*input, object_description)) {
|
|
|
|
warn(tokenizer.getErrorMessage());
|
|
|
|
}
|
2023-10-30 12:21:34 +00:00
|
|
|
|
2023-10-30 13:42:00 +00:00
|
|
|
switch (tokenizer.getType()) {
|
|
|
|
case QPDFTokenizer::tt_eof:
|
|
|
|
if (content_stream) {
|
|
|
|
// In content stream mode, leave object uninitialized to indicate EOF
|
|
|
|
return {};
|
2023-10-30 12:21:34 +00:00
|
|
|
}
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser eof in parse");
|
|
|
|
warn("unexpected EOF");
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_bad:
|
|
|
|
QTC::TC("qpdf", "QPDFParser bad token in parse");
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_brace_open:
|
|
|
|
case QPDFTokenizer::tt_brace_close:
|
|
|
|
QTC::TC("qpdf", "QPDFParser bad brace");
|
|
|
|
warn("treating unexpected brace token as null");
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_array_close:
|
|
|
|
QTC::TC("qpdf", "QPDFParser bad array close");
|
|
|
|
warn("treating unexpected array close token as null");
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_dict_close:
|
|
|
|
QTC::TC("qpdf", "QPDFParser bad dictionary close");
|
|
|
|
warn("unexpected dictionary close token");
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_array_open:
|
|
|
|
case QPDFTokenizer::tt_dict_open:
|
2023-10-30 19:47:47 +00:00
|
|
|
stack.clear();
|
2023-10-30 13:42:00 +00:00
|
|
|
stack.emplace_back(
|
|
|
|
input,
|
2023-11-01 09:10:56 +00:00
|
|
|
(tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key);
|
2023-10-30 19:47:47 +00:00
|
|
|
frame = &stack.back();
|
2023-10-30 13:42:00 +00:00
|
|
|
return parseRemainder(content_stream);
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_bool:
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_Bool>(tokenizer.getValue() == "true");
|
2023-10-30 13:42:00 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_null:
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_integer:
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str()));
|
2023-10-30 13:42:00 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_real:
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_Real>(tokenizer.getValue());
|
2023-10-30 13:42:00 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_name:
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_Name>(tokenizer.getValue());
|
2023-10-30 13:42:00 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_word:
|
|
|
|
{
|
|
|
|
auto const& value = tokenizer.getValue();
|
2023-10-30 12:21:34 +00:00
|
|
|
if (content_stream) {
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_Operator>(value);
|
2023-10-30 13:42:00 +00:00
|
|
|
} else if (value == "endobj") {
|
|
|
|
// We just saw endobj without having read anything. Treat this as a null and do
|
|
|
|
// not move the input source's offset.
|
|
|
|
input->seek(input->getLastOffset(), SEEK_SET);
|
|
|
|
empty = true;
|
2023-10-30 12:21:34 +00:00
|
|
|
return {QPDF_Null::create()};
|
|
|
|
} else {
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser treat word as string");
|
|
|
|
warn("unknown token while reading object; treating as string");
|
2023-10-30 20:03:00 +00:00
|
|
|
return withDescription<QPDF_String>(value);
|
2023-10-30 12:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-30 13:42:00 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_string:
|
|
|
|
if (decrypter) {
|
2023-10-31 11:57:34 +00:00
|
|
|
std::string s{tokenizer.getValue()};
|
|
|
|
decrypter->decryptString(s);
|
|
|
|
return withDescription<QPDF_String>(s);
|
2023-10-30 13:42:00 +00:00
|
|
|
} else {
|
2023-10-31 11:57:34 +00:00
|
|
|
return withDescription<QPDF_String>(tokenizer.getValue());
|
2023-10-30 12:21:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:42:00 +00:00
|
|
|
default:
|
|
|
|
warn("treating unknown token type as null while reading object");
|
|
|
|
return {QPDF_Null::create()};
|
2023-10-30 12:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFParser::parseRemainder(bool content_stream)
|
|
|
|
{
|
|
|
|
// This method must take care not to resolve any objects. Don't check the type of any object
|
|
|
|
// without first ensuring that it is a direct object. Otherwise, doing so may have the side
|
|
|
|
// effect of reading the object and changing the file pointer. If you do this, it will cause a
|
|
|
|
// logic error to be thrown from QPDF::inParse().
|
|
|
|
|
2023-10-30 13:42:00 +00:00
|
|
|
bad_count = 0;
|
2023-10-31 11:57:34 +00:00
|
|
|
bool b_contents = false;
|
2022-08-30 11:53:19 +00:00
|
|
|
|
2023-10-30 19:47:47 +00:00
|
|
|
while (true) {
|
2022-10-05 18:42:02 +00:00
|
|
|
if (!tokenizer.nextToken(*input, object_description)) {
|
|
|
|
warn(tokenizer.getErrorMessage());
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-27 13:40:30 +00:00
|
|
|
++good_count; // optimistically
|
2022-08-16 12:59:32 +00:00
|
|
|
|
2023-10-31 17:44:01 +00:00
|
|
|
if (int_count != 0) {
|
|
|
|
// Special handling of indirect references. Treat integer tokens as part of an indirect
|
|
|
|
// reference until proven otherwise.
|
|
|
|
if (tokenizer.getType() == QPDFTokenizer::tt_integer) {
|
|
|
|
if (++int_count > 2) {
|
|
|
|
// Process the oldest buffered integer.
|
|
|
|
addInt(int_count);
|
|
|
|
}
|
|
|
|
last_offset_buffer[int_count % 2] = input->getLastOffset();
|
|
|
|
int_buffer[int_count % 2] = QUtil::string_to_ll(tokenizer.getValue().c_str());
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (
|
|
|
|
int_count >= 2 && tokenizer.getType() == QPDFTokenizer::tt_word &&
|
|
|
|
tokenizer.getValue() == "R") {
|
|
|
|
if (context == nullptr) {
|
|
|
|
QTC::TC("qpdf", "QPDFParser indirect without context");
|
|
|
|
throw std::logic_error("QPDFParser::parse called without context on an object "
|
|
|
|
"with indirect references");
|
|
|
|
}
|
2024-01-17 13:15:13 +00:00
|
|
|
auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]);
|
|
|
|
auto gen = QIntC::to_int(int_buffer[(int_count) % 2]);
|
|
|
|
if (!(id < 1 || gen < 0 || gen >= 65535)) {
|
2023-10-31 17:44:01 +00:00
|
|
|
// This action has the desirable side effect of causing dangling references
|
|
|
|
// (references to indirect objects that don't appear in the PDF) in any parsed
|
|
|
|
// object to appear in the object cache.
|
2024-01-17 13:15:13 +00:00
|
|
|
add(std::move(context->getObject(id, gen).obj));
|
2023-10-31 17:44:01 +00:00
|
|
|
} else {
|
2024-01-17 13:15:13 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser invalid objgen");
|
2023-10-31 17:44:01 +00:00
|
|
|
addNull();
|
|
|
|
}
|
|
|
|
int_count = 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (int_count > 0) {
|
|
|
|
// Process the buffered integers before processing the current token.
|
|
|
|
if (int_count > 1) {
|
|
|
|
addInt(int_count - 1);
|
|
|
|
}
|
|
|
|
addInt(int_count);
|
|
|
|
int_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:42:02 +00:00
|
|
|
switch (tokenizer.getType()) {
|
2022-08-16 12:59:32 +00:00
|
|
|
case QPDFTokenizer::tt_eof:
|
2023-10-30 13:42:00 +00:00
|
|
|
warn("parse error while reading object");
|
2023-10-27 14:37:46 +00:00
|
|
|
if (content_stream) {
|
|
|
|
// In content stream mode, leave object uninitialized to indicate EOF
|
|
|
|
return {};
|
|
|
|
}
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser eof in parseRemainder");
|
2023-10-27 14:37:46 +00:00
|
|
|
warn("unexpected EOF");
|
|
|
|
return {QPDF_Null::create()};
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_bad:
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser bad token in parseRemainder");
|
2023-10-27 13:40:30 +00:00
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_brace_open:
|
|
|
|
case QPDFTokenizer::tt_brace_close:
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser bad brace in parseRemainder");
|
2022-08-16 13:40:14 +00:00
|
|
|
warn("treating unexpected brace token as null");
|
2023-10-27 13:40:30 +00:00
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_array_close:
|
2023-10-27 23:46:29 +00:00
|
|
|
if (frame->state == st_array) {
|
2023-10-31 11:57:34 +00:00
|
|
|
auto object = QPDF_Array::create(std::move(frame->olist), frame->null_count > 100);
|
2023-10-27 23:46:29 +00:00
|
|
|
setDescription(object, frame->offset - 1);
|
2023-10-27 16:20:54 +00:00
|
|
|
// 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 delimiter. getLastOffset points to the array end token and therefore
|
|
|
|
// can't be used here.
|
2023-10-30 19:47:47 +00:00
|
|
|
if (stack.size() <= 1) {
|
|
|
|
return object;
|
|
|
|
}
|
2023-10-27 16:20:54 +00:00
|
|
|
stack.pop_back();
|
2023-10-27 23:46:29 +00:00
|
|
|
frame = &stack.back();
|
2023-10-31 11:57:34 +00:00
|
|
|
add(std::move(object));
|
2022-08-16 12:59:32 +00:00
|
|
|
} else {
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser bad array close in parseRemainder");
|
2022-08-16 13:40:14 +00:00
|
|
|
warn("treating unexpected array close token as null");
|
2023-10-27 13:40:30 +00:00
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_dict_close:
|
2023-11-01 09:10:56 +00:00
|
|
|
if (frame->state <= st_dictionary_value) {
|
|
|
|
// Attempt to recover more or less gracefully from invalid dictionaries.
|
|
|
|
auto& dict = frame->dict;
|
2023-11-03 11:22:21 +00:00
|
|
|
|
2023-11-01 09:10:56 +00:00
|
|
|
if (frame->state == st_dictionary_value) {
|
|
|
|
QTC::TC("qpdf", "QPDFParser no val for last key");
|
|
|
|
warn(
|
|
|
|
frame->offset,
|
|
|
|
"dictionary ended prematurely; using null as value for last key");
|
|
|
|
dict[frame->key] = QPDF_Null::create();
|
|
|
|
}
|
|
|
|
|
2024-02-04 21:11:53 +00:00
|
|
|
if (!frame->olist.empty()) {
|
2023-11-03 11:22:21 +00:00
|
|
|
fixMissingKeys();
|
2024-02-04 21:11:53 +00:00
|
|
|
}
|
2023-11-01 09:10:56 +00:00
|
|
|
|
2023-10-27 23:46:29 +00:00
|
|
|
if (!frame->contents_string.empty() && dict.count("/Type") &&
|
2023-10-27 16:41:36 +00:00
|
|
|
dict["/Type"].isNameAndEquals("/Sig") && dict.count("/ByteRange") &&
|
|
|
|
dict.count("/Contents") && dict["/Contents"].isString()) {
|
2023-10-27 23:46:29 +00:00
|
|
|
dict["/Contents"] = QPDFObjectHandle::newString(frame->contents_string);
|
|
|
|
dict["/Contents"].setParsedOffset(frame->contents_offset);
|
2023-10-27 16:41:36 +00:00
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
auto object = QPDF_Dictionary::create(std::move(dict));
|
2023-10-27 23:46:29 +00:00
|
|
|
setDescription(object, frame->offset - 2);
|
2023-10-27 16:41:36 +00:00
|
|
|
// 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 start delimiter. getLastOffset points to the dictionary end token
|
|
|
|
// and therefore can't be used here.
|
2023-10-30 19:47:47 +00:00
|
|
|
if (stack.size() <= 1) {
|
|
|
|
return object;
|
|
|
|
}
|
2023-10-27 16:41:36 +00:00
|
|
|
stack.pop_back();
|
2023-10-27 23:46:29 +00:00
|
|
|
frame = &stack.back();
|
2023-10-31 11:57:34 +00:00
|
|
|
add(std::move(object));
|
2022-08-16 12:59:32 +00:00
|
|
|
} else {
|
2023-10-30 13:42:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser bad dictionary close in parseRemainder");
|
2022-08-16 13:40:14 +00:00
|
|
|
warn("unexpected dictionary close token");
|
2023-10-27 13:40:30 +00:00
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_array_open:
|
|
|
|
case QPDFTokenizer::tt_dict_open:
|
2023-10-30 19:47:47 +00:00
|
|
|
if (stack.size() > 499) {
|
2022-08-16 12:59:32 +00:00
|
|
|
QTC::TC("qpdf", "QPDFParser too deep");
|
2022-08-16 13:40:14 +00:00
|
|
|
warn("ignoring excessively deeply nested data structure");
|
2023-10-27 16:55:09 +00:00
|
|
|
return {QPDF_Null::create()};
|
2022-08-16 12:59:32 +00:00
|
|
|
} else {
|
2023-10-27 23:46:29 +00:00
|
|
|
b_contents = false;
|
|
|
|
stack.emplace_back(
|
|
|
|
input,
|
2023-05-21 17:35:09 +00:00
|
|
|
(tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array
|
2023-11-01 09:10:56 +00:00
|
|
|
: st_dictionary_key);
|
2023-10-27 23:46:29 +00:00
|
|
|
frame = &stack.back();
|
2023-10-27 16:55:09 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case QPDFTokenizer::tt_bool:
|
2023-10-31 11:57:34 +00:00
|
|
|
addScalar<QPDF_Bool>(tokenizer.getValue() == "true");
|
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_null:
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_integer:
|
2023-10-31 17:44:01 +00:00
|
|
|
if (!content_stream) {
|
|
|
|
// Buffer token in case it is part of an indirect reference.
|
|
|
|
last_offset_buffer[1] = input->getLastOffset();
|
|
|
|
int_buffer[1] = QUtil::string_to_ll(tokenizer.getValue().c_str());
|
|
|
|
int_count = 1;
|
|
|
|
} else {
|
|
|
|
addScalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str()));
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_real:
|
2023-10-31 11:57:34 +00:00
|
|
|
addScalar<QPDF_Real>(tokenizer.getValue());
|
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_name:
|
2023-11-01 09:10:56 +00:00
|
|
|
if (frame->state == st_dictionary_key) {
|
|
|
|
frame->key = tokenizer.getValue();
|
|
|
|
frame->state = st_dictionary_value;
|
|
|
|
b_contents = decrypter && frame->key == "/Contents";
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
addScalar<QPDF_Name>(tokenizer.getValue());
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_word:
|
2023-10-31 17:44:01 +00:00
|
|
|
if (content_stream) {
|
|
|
|
addScalar<QPDF_Operator>(tokenizer.getValue());
|
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDFParser treat word as string in parseRemainder");
|
|
|
|
warn("unknown token while reading object; treating as string");
|
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-31 17:44:01 +00:00
|
|
|
addScalar<QPDF_String>(tokenizer.getValue());
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
case QPDFTokenizer::tt_string:
|
|
|
|
{
|
2023-10-27 13:07:01 +00:00
|
|
|
auto const& val = tokenizer.getValue();
|
2022-08-16 12:59:32 +00:00
|
|
|
if (decrypter) {
|
|
|
|
if (b_contents) {
|
2023-10-27 23:46:29 +00:00
|
|
|
frame->contents_string = val;
|
|
|
|
frame->contents_offset = input->getLastOffset();
|
2022-08-16 12:59:32 +00:00
|
|
|
b_contents = false;
|
|
|
|
}
|
2022-10-05 18:42:02 +00:00
|
|
|
std::string s{val};
|
|
|
|
decrypter->decryptString(s);
|
2023-10-31 11:57:34 +00:00
|
|
|
addScalar<QPDF_String>(s);
|
2022-10-05 18:42:02 +00:00
|
|
|
} else {
|
2023-10-31 11:57:34 +00:00
|
|
|
addScalar<QPDF_String>(val);
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
continue;
|
2022-08-16 12:59:32 +00:00
|
|
|
|
|
|
|
default:
|
2023-05-24 15:28:17 +00:00
|
|
|
warn("treating unknown token type as null while reading object");
|
2023-10-27 13:40:30 +00:00
|
|
|
if (tooManyBadTokens()) {
|
|
|
|
return {QPDF_Null::create()};
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
addNull();
|
2022-08-16 12:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:57:34 +00:00
|
|
|
void
|
|
|
|
QPDFParser::add(std::shared_ptr<QPDFObject>&& obj)
|
|
|
|
{
|
2023-11-01 09:10:56 +00:00
|
|
|
if (frame->state != st_dictionary_value) {
|
|
|
|
// If state is st_dictionary_key then there is a missing key. Push onto olist for
|
|
|
|
// processing once the tt_dict_close token has been found.
|
|
|
|
frame->olist.emplace_back(std::move(obj));
|
|
|
|
} else {
|
|
|
|
if (auto res = frame->dict.insert_or_assign(frame->key, std::move(obj)); !res.second) {
|
|
|
|
warnDuplicateKey();
|
|
|
|
}
|
|
|
|
frame->state = st_dictionary_key;
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFParser::addNull()
|
|
|
|
{
|
|
|
|
const static ObjectPtr null_obj = QPDF_Null::create();
|
|
|
|
|
2023-11-01 09:10:56 +00:00
|
|
|
if (frame->state != st_dictionary_value) {
|
|
|
|
// If state is st_dictionary_key then there is a missing key. Push onto olist for
|
|
|
|
// processing once the tt_dict_close token has been found.
|
|
|
|
frame->olist.emplace_back(null_obj);
|
|
|
|
} else {
|
|
|
|
if (auto res = frame->dict.insert_or_assign(frame->key, null_obj); !res.second) {
|
|
|
|
warnDuplicateKey();
|
|
|
|
}
|
|
|
|
frame->state = st_dictionary_key;
|
|
|
|
}
|
2023-10-31 11:57:34 +00:00
|
|
|
++frame->null_count;
|
|
|
|
}
|
|
|
|
|
2023-10-31 17:44:01 +00:00
|
|
|
void
|
|
|
|
QPDFParser::addInt(int count)
|
|
|
|
{
|
|
|
|
auto obj = QPDF_Integer::create(int_buffer[count % 2]);
|
|
|
|
obj->setDescription(context, description, last_offset_buffer[count % 2]);
|
|
|
|
add(std::move(obj));
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:57:34 +00:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
void
|
|
|
|
QPDFParser::addScalar(Args&&... args)
|
|
|
|
{
|
|
|
|
auto obj = T::create(args...);
|
|
|
|
obj->setDescription(context, description, input->getLastOffset());
|
|
|
|
add(std::move(obj));
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:03:00 +00:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFParser::withDescription(Args&&... args)
|
|
|
|
{
|
|
|
|
auto obj = T::create(args...);
|
|
|
|
obj->setDescription(context, description, start);
|
|
|
|
return {obj};
|
|
|
|
}
|
|
|
|
|
2022-08-30 05:42:46 +00:00
|
|
|
void
|
2023-10-30 20:03:00 +00:00
|
|
|
QPDFParser::setDescription(ObjectPtr& obj, qpdf_offset_t parsed_offset)
|
2022-08-30 05:42:46 +00:00
|
|
|
{
|
2022-12-19 15:16:16 +00:00
|
|
|
if (obj) {
|
2022-12-16 18:08:56 +00:00
|
|
|
obj->setDescription(context, description, parsed_offset);
|
2022-12-15 09:56:46 +00:00
|
|
|
}
|
2022-08-30 05:42:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 11:22:21 +00:00
|
|
|
void
|
|
|
|
QPDFParser::fixMissingKeys()
|
|
|
|
{
|
|
|
|
std::set<std::string> names;
|
|
|
|
for (auto& obj: frame->olist) {
|
|
|
|
if (obj->getTypeCode() == ::ot_name) {
|
|
|
|
names.insert(obj->getStringValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int next_fake_key = 1;
|
|
|
|
for (auto const& item: frame->olist) {
|
|
|
|
while (true) {
|
|
|
|
const std::string key = "/QPDFFake" + std::to_string(next_fake_key++);
|
|
|
|
const bool found_fake = frame->dict.count(key) == 0 && names.count(key) == 0;
|
|
|
|
QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1));
|
|
|
|
if (found_fake) {
|
|
|
|
warn(
|
|
|
|
frame->offset,
|
|
|
|
"expected dictionary key but found non-name object; inserting key " + key);
|
|
|
|
frame->dict[key] = item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 13:40:30 +00:00
|
|
|
bool
|
|
|
|
QPDFParser::tooManyBadTokens()
|
|
|
|
{
|
|
|
|
if (good_count <= 4) {
|
|
|
|
if (++bad_count > 5) {
|
|
|
|
warn("too many errors; giving up on reading object");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bad_count = 1;
|
|
|
|
}
|
|
|
|
good_count = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-16 12:59:32 +00:00
|
|
|
void
|
2022-12-19 15:26:21 +00:00
|
|
|
QPDFParser::warn(QPDFExc const& e) const
|
2022-08-16 12:59:32 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// If parsing on behalf of a QPDF object and want to give a warning, we can warn through the
|
|
|
|
// object. If parsing for some other reason, such as an explicit creation of an object from a
|
2022-08-16 12:59:32 +00:00
|
|
|
// string, then just throw the exception.
|
2022-12-19 15:26:21 +00:00
|
|
|
if (context) {
|
|
|
|
context->warn(e);
|
2022-08-16 12:59:32 +00:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 13:40:14 +00:00
|
|
|
|
2023-11-01 09:10:56 +00:00
|
|
|
void
|
|
|
|
QPDFParser::warnDuplicateKey()
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDFParser duplicate dict key");
|
|
|
|
warn(
|
|
|
|
frame->offset,
|
|
|
|
"dictionary has duplicated key " + frame->key + "; last occurrence overrides earlier ones");
|
|
|
|
}
|
|
|
|
|
2022-08-16 13:40:14 +00:00
|
|
|
void
|
|
|
|
QPDFParser::warn(qpdf_offset_t offset, std::string const& msg) const
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(), object_description, offset, msg));
|
2022-08-16 13:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFParser::warn(std::string const& msg) const
|
|
|
|
{
|
|
|
|
warn(input->getLastOffset(), msg);
|
|
|
|
}
|