mirror of https://github.com/qpdf/qpdf.git
Refactor dictionary processing loop in QPDFParser::parse
This commit is contained in:
parent
d67a54ae93
commit
3f567ae02d
|
@ -344,24 +344,21 @@ QPDFParser::parse(bool& empty, bool content_stream)
|
||||||
if (obj->getTypeCode() == ::ot_name) {
|
if (obj->getTypeCode() == ::ot_name) {
|
||||||
names.insert(obj->getStringValue());
|
names.insert(obj->getStringValue());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
obj = null_oh;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, QPDFObjectHandle> dict;
|
std::map<std::string, QPDFObjectHandle> dict;
|
||||||
int next_fake_key = 1;
|
int next_fake_key = 1;
|
||||||
size_t n_elements = olist.size();
|
for (auto iter = olist.begin(); iter != olist.end();) {
|
||||||
for (unsigned int i = 0; i < n_elements; ++i) {
|
// Calculate key.
|
||||||
QPDFObjectHandle key_obj = olist.at(i);
|
std::string key;
|
||||||
QPDFObjectHandle val;
|
if (*iter && (*iter)->getTypeCode() == ::ot_name) {
|
||||||
if (key_obj.isIndirect() || (!key_obj.isName())) {
|
key = (*iter)->getStringValue();
|
||||||
bool found_fake = false;
|
++iter;
|
||||||
std::string candidate;
|
} else {
|
||||||
while (!found_fake) {
|
for (bool found_fake = false; !found_fake;) {
|
||||||
candidate =
|
key = "/QPDFFake" + std::to_string(next_fake_key++);
|
||||||
"/QPDFFake" + std::to_string(next_fake_key++);
|
found_fake = (names.count(key) == 0);
|
||||||
found_fake = (names.count(candidate) == 0);
|
|
||||||
QTC::TC(
|
QTC::TC(
|
||||||
"qpdf",
|
"qpdf",
|
||||||
"QPDFParser found fake",
|
"QPDFParser found fake",
|
||||||
|
@ -371,21 +368,8 @@ QPDFParser::parse(bool& empty, bool content_stream)
|
||||||
offset,
|
offset,
|
||||||
"expected dictionary key but found"
|
"expected dictionary key but found"
|
||||||
" non-name object; inserting key " +
|
" non-name object; inserting key " +
|
||||||
candidate);
|
key);
|
||||||
val = key_obj;
|
|
||||||
key_obj = QPDFObjectHandle::newName(candidate);
|
|
||||||
} else if (i + 1 >= olist.size()) {
|
|
||||||
QTC::TC("qpdf", "QPDFParser no val for last key");
|
|
||||||
warn(
|
|
||||||
offset,
|
|
||||||
"dictionary ended prematurely; "
|
|
||||||
"using null as value for last key");
|
|
||||||
val = QPDFObjectHandle::newNull();
|
|
||||||
setDescription(val.obj, offset);
|
|
||||||
} else {
|
|
||||||
val = olist.at(++i);
|
|
||||||
}
|
}
|
||||||
std::string key = key_obj.getName();
|
|
||||||
if (dict.count(key) > 0) {
|
if (dict.count(key) > 0) {
|
||||||
QTC::TC("qpdf", "QPDFParser duplicate dict key");
|
QTC::TC("qpdf", "QPDFParser duplicate dict key");
|
||||||
warn(
|
warn(
|
||||||
|
@ -394,7 +378,22 @@ QPDFParser::parse(bool& empty, bool content_stream)
|
||||||
"; last occurrence overrides earlier "
|
"; last occurrence overrides earlier "
|
||||||
"ones");
|
"ones");
|
||||||
}
|
}
|
||||||
dict[key] = val;
|
|
||||||
|
// Calculate value.
|
||||||
|
std::shared_ptr<QPDFObject> val;
|
||||||
|
if (iter != olist.end()) {
|
||||||
|
val = *iter ? *iter : QPDF_Null::create();
|
||||||
|
++iter;
|
||||||
|
} else {
|
||||||
|
QTC::TC("qpdf", "QPDFParser no val for last key");
|
||||||
|
warn(
|
||||||
|
offset,
|
||||||
|
"dictionary ended prematurely; "
|
||||||
|
"using null as value for last key");
|
||||||
|
val = QPDF_Null::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
dict[std::move(key)] = std::move(val);
|
||||||
}
|
}
|
||||||
if (!frame.contents_string.empty() && dict.count("/Type") &&
|
if (!frame.contents_string.empty() && dict.count("/Type") &&
|
||||||
dict["/Type"].isNameAndEquals("/Sig") &&
|
dict["/Type"].isNameAndEquals("/Sig") &&
|
||||||
|
|
Loading…
Reference in New Issue