mirror of
https://github.com/qpdf/qpdf.git
synced 2025-02-08 22:58:25 +00:00
Remove some if statements and simplify some boolean expressions
Use QPDFObjectHandle::isNameAndEquals, isDictionaryOfType and isStreamOfType.
This commit is contained in:
parent
710d2e54f0
commit
07db3200cb
@ -290,15 +290,14 @@ static void check(char const* filename,
|
|||||||
QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
|
QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
|
||||||
QPDFObjectHandle filter = image_dict.getKey("/Filter");
|
QPDFObjectHandle filter = image_dict.getKey("/Filter");
|
||||||
bool this_errors = false;
|
bool this_errors = false;
|
||||||
if (! (filter.isName() && (filter.getName() == desired_filter)))
|
if (! filter.isNameAndEquals(desired_filter))
|
||||||
{
|
{
|
||||||
this_errors = errors = true;
|
this_errors = errors = true;
|
||||||
std::cout << "page " << pageno << ": expected filter "
|
std::cout << "page " << pageno << ": expected filter "
|
||||||
<< desired_filter << "; actual filter = "
|
<< desired_filter << "; actual filter = "
|
||||||
<< filter.unparse() << std::endl;
|
<< filter.unparse() << std::endl;
|
||||||
}
|
}
|
||||||
if (! (color_space.isName() &&
|
if (! color_space.isNameAndEquals(desired_color_space))
|
||||||
(color_space.getName() == desired_color_space)))
|
|
||||||
{
|
{
|
||||||
this_errors = errors = true;
|
this_errors = errors = true;
|
||||||
std::cout << "page " << pageno << ": expected color space "
|
std::cout << "page " << pageno << ": expected color space "
|
||||||
|
@ -167,9 +167,8 @@ int main(int argc, char* argv[])
|
|||||||
// keys to determine the image type.
|
// keys to determine the image type.
|
||||||
if (image.pipeStreamData(0, qpdf_ef_compress,
|
if (image.pipeStreamData(0, qpdf_ef_compress,
|
||||||
qpdf_dl_all) &&
|
qpdf_dl_all) &&
|
||||||
color_space.isName() &&
|
color_space.isNameAndEquals("/DeviceGray") &&
|
||||||
bits_per_component.isInteger() &&
|
bits_per_component.isInteger() &&
|
||||||
(color_space.getName() == "/DeviceGray") &&
|
|
||||||
(bits_per_component.getIntValue() == 8))
|
(bits_per_component.getIntValue() == 8))
|
||||||
{
|
{
|
||||||
inv->registerImage(image, p);
|
inv->registerImage(image, p);
|
||||||
|
@ -1100,10 +1100,7 @@ QPDF::read_xrefStream(qpdf_offset_t xref_offset)
|
|||||||
{
|
{
|
||||||
// ignore -- report error below
|
// ignore -- report error below
|
||||||
}
|
}
|
||||||
if (xref_obj.isInitialized() &&
|
if (xref_obj.isStreamOfType("/XRef"))
|
||||||
xref_obj.isStream() &&
|
|
||||||
xref_obj.getDict().getKey("/Type").isName() &&
|
|
||||||
xref_obj.getDict().getKey("/Type").getName() == "/XRef")
|
|
||||||
{
|
{
|
||||||
QTC::TC("qpdf", "QPDF found xref stream");
|
QTC::TC("qpdf", "QPDF found xref stream");
|
||||||
found = true;
|
found = true;
|
||||||
@ -2202,8 +2199,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
|
|||||||
this->m->obj_cache[stream_og].end_after_space;
|
this->m->obj_cache[stream_og].end_after_space;
|
||||||
|
|
||||||
QPDFObjectHandle dict = obj_stream.getDict();
|
QPDFObjectHandle dict = obj_stream.getDict();
|
||||||
if (! (dict.getKey("/Type").isName() &&
|
if (! dict.isDictionaryOfType("/ObjStm"))
|
||||||
dict.getKey("/Type").getName() == "/ObjStm"))
|
|
||||||
{
|
{
|
||||||
QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
|
QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
|
||||||
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
||||||
@ -2849,13 +2845,10 @@ QPDF::getCompressibleObjGens()
|
|||||||
{
|
{
|
||||||
QTC::TC("qpdf", "QPDF exclude encryption dictionary");
|
QTC::TC("qpdf", "QPDF exclude encryption dictionary");
|
||||||
}
|
}
|
||||||
else if ((! obj.isStream()) &&
|
else if (! (obj.isStream() ||
|
||||||
(! (obj.isDictionary() &&
|
(obj.isDictionaryOfType("/Sig") &&
|
||||||
obj.hasKey("/ByteRange") &&
|
obj.hasKey("/ByteRange") &&
|
||||||
obj.hasKey("/Contents") &&
|
obj.hasKey("/Contents"))))
|
||||||
obj.hasKey("/Type") &&
|
|
||||||
obj.getKey("/Type").isName() &&
|
|
||||||
obj.getKey("/Type").getName() == "/Sig")))
|
|
||||||
{
|
{
|
||||||
result.push_back(og);
|
result.push_back(og);
|
||||||
}
|
}
|
||||||
|
@ -280,9 +280,7 @@ QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h)
|
|||||||
{
|
{
|
||||||
QPDFObjectHandle oh = h.getObjectHandle();
|
QPDFObjectHandle oh = h.getObjectHandle();
|
||||||
QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());
|
QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());
|
||||||
if (! (oh.isDictionary() &&
|
if (! oh.isDictionaryOfType("", "/Widget"))
|
||||||
oh.getKey("/Subtype").isName() &&
|
|
||||||
(oh.getKey("/Subtype").getName() == "/Widget")))
|
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,7 @@ QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(
|
|||||||
oh.warnIfPossible("Embedded file object is not a dictionary");
|
oh.warnIfPossible("Embedded file object is not a dictionary");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto type = oh.getKey("/Type");
|
if (! oh.isDictionaryOfType("/Filespec"))
|
||||||
if (! (type.isName() && (type.getName() == "/Filespec")))
|
|
||||||
{
|
{
|
||||||
oh.warnIfPossible("Embedded file object's type is not /Filespec");
|
oh.warnIfPossible("Embedded file object's type is not /Filespec");
|
||||||
}
|
}
|
||||||
|
@ -1055,23 +1055,9 @@ QPDFObjectHandle::getDictAsMap()
|
|||||||
bool
|
bool
|
||||||
QPDFObjectHandle::isOrHasName(std::string const& value)
|
QPDFObjectHandle::isOrHasName(std::string const& value)
|
||||||
{
|
{
|
||||||
if (isName() && (getName() == value))
|
return isNameAndEquals(value) ||
|
||||||
{
|
(isArray() && (getArrayNItems() > 0) &&
|
||||||
return true;
|
getArrayItem(0).isNameAndEquals(value));
|
||||||
}
|
|
||||||
else if (isArray())
|
|
||||||
{
|
|
||||||
int n = getArrayNItems();
|
|
||||||
for (int i = 0; i < n; ++i)
|
|
||||||
{
|
|
||||||
QPDFObjectHandle item = getArrayItem(0);
|
|
||||||
if (item.isName() && (item.getName() == value))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -2520,8 +2506,7 @@ QPDFObjectHandle::parseInternal(PointerHolder<InputSource> input,
|
|||||||
}
|
}
|
||||||
if (!contents_string.empty() &&
|
if (!contents_string.empty() &&
|
||||||
dict.count("/Type") &&
|
dict.count("/Type") &&
|
||||||
dict["/Type"].isName() &&
|
dict["/Type"].isNameAndEquals("/Sig") &&
|
||||||
dict["/Type"].getName() == "/Sig" &&
|
|
||||||
dict.count("/ByteRange") &&
|
dict.count("/ByteRange") &&
|
||||||
dict.count("/Contents") &&
|
dict.count("/Contents") &&
|
||||||
dict["/Contents"].isString())
|
dict["/Contents"].isString())
|
||||||
@ -3237,7 +3222,7 @@ QPDFObjectHandle::isPageObject()
|
|||||||
if (this->hasKey("/Type"))
|
if (this->hasKey("/Type"))
|
||||||
{
|
{
|
||||||
QPDFObjectHandle type = this->getKey("/Type");
|
QPDFObjectHandle type = this->getKey("/Type");
|
||||||
if (type.isName() && (type.getName() == "/Page"))
|
if (type.isNameAndEquals("/Page"))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -3263,39 +3248,22 @@ QPDFObjectHandle::isPagesObject()
|
|||||||
}
|
}
|
||||||
// getAllPages repairs /Type when traversing the page tree.
|
// getAllPages repairs /Type when traversing the page tree.
|
||||||
getOwningQPDF()->getAllPages();
|
getOwningQPDF()->getAllPages();
|
||||||
return (this->isDictionary() &&
|
return isDictionaryOfType("/Pages");
|
||||||
this->hasKey("/Type") &&
|
|
||||||
this->getKey("/Type").isName() &&
|
|
||||||
this->getKey("/Type").getName() == "/Pages");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
QPDFObjectHandle::isFormXObject()
|
QPDFObjectHandle::isFormXObject()
|
||||||
{
|
{
|
||||||
if (! this->isStream())
|
return isStreamOfType("/XObject", "/Form");
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
QPDFObjectHandle dict = this->getDict();
|
|
||||||
return (dict.getKey("/Type").isName() &&
|
|
||||||
("/XObject" == dict.getKey("/Type").getName()) &&
|
|
||||||
dict.getKey("/Subtype").isName() &&
|
|
||||||
("/Form" == dict.getKey("/Subtype").getName()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
QPDFObjectHandle::isImage(bool exclude_imagemask)
|
QPDFObjectHandle::isImage(bool exclude_imagemask)
|
||||||
{
|
{
|
||||||
if (! this->isStream())
|
return (isStreamOfType("", "/Image") &&
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
QPDFObjectHandle dict = this->getDict();
|
|
||||||
return (dict.hasKey("/Subtype") &&
|
|
||||||
(dict.getKey("/Subtype").getName() == "/Image") &&
|
|
||||||
((! exclude_imagemask) ||
|
((! exclude_imagemask) ||
|
||||||
(! (dict.getKey("/ImageMask").isBool() &&
|
(! (getDict().getKey("/ImageMask").isBool() &&
|
||||||
dict.getKey("/ImageMask").getBoolValue()))));
|
getDict().getKey("/ImageMask").getBoolValue()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -558,10 +558,7 @@ QPDFPageObjectHelper::getAnnotations(std::string const& only_subtype)
|
|||||||
for (int i = 0; i < nannots; ++i)
|
for (int i = 0; i < nannots; ++i)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle annot = annots.getArrayItem(i);
|
QPDFObjectHandle annot = annots.getArrayItem(i);
|
||||||
if (only_subtype.empty() ||
|
if (annot.isDictionaryOfType("", only_subtype))
|
||||||
(annot.isDictionary() &&
|
|
||||||
annot.getKey("/Subtype").isName() &&
|
|
||||||
(only_subtype == annot.getKey("/Subtype").getName())))
|
|
||||||
{
|
{
|
||||||
result.push_back(QPDFAnnotationObjectHelper(annot));
|
result.push_back(QPDFAnnotationObjectHelper(annot));
|
||||||
}
|
}
|
||||||
|
@ -1241,9 +1241,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object)
|
|||||||
" another file.");
|
" another file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->m->qdf_mode &&
|
if (this->m->qdf_mode && object.isStreamOfType("/XRef"))
|
||||||
object.isStream() && object.getDict().getKey("/Type").isName() &&
|
|
||||||
(object.getDict().getKey("/Type").getName() == "/XRef"))
|
|
||||||
{
|
{
|
||||||
// As a special case, do not output any extraneous XRef
|
// As a special case, do not output any extraneous XRef
|
||||||
// streams in QDF mode. Doing so will confuse fix-qdf,
|
// streams in QDF mode. Doing so will confuse fix-qdf,
|
||||||
@ -1474,8 +1472,7 @@ QPDFWriter::willFilterStream(QPDFObjectHandle stream,
|
|||||||
QPDFObjGen old_og = stream.getObjGen();
|
QPDFObjGen old_og = stream.getObjGen();
|
||||||
QPDFObjectHandle stream_dict = stream.getDict();
|
QPDFObjectHandle stream_dict = stream.getDict();
|
||||||
|
|
||||||
if (stream_dict.getKey("/Type").isName() &&
|
if (stream_dict.isDictionaryOfType("/Metadata"))
|
||||||
(stream_dict.getKey("/Type").getName() == "/Metadata"))
|
|
||||||
{
|
{
|
||||||
is_metadata = true;
|
is_metadata = true;
|
||||||
}
|
}
|
||||||
@ -1691,11 +1688,8 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
|||||||
QTC::TC("qpdf", "QPDFWriter preserve Extensions");
|
QTC::TC("qpdf", "QPDFWriter preserve Extensions");
|
||||||
QPDFObjectHandle adbe = extensions.getKey("/ADBE");
|
QPDFObjectHandle adbe = extensions.getKey("/ADBE");
|
||||||
if (adbe.isDictionary() &&
|
if (adbe.isDictionary() &&
|
||||||
adbe.hasKey("/BaseVersion") &&
|
adbe.getKey("/BaseVersion").isNameAndEquals(
|
||||||
adbe.getKey("/BaseVersion").isName() &&
|
"/" + this->m->final_pdf_version) &&
|
||||||
(adbe.getKey("/BaseVersion").getName() ==
|
|
||||||
"/" + this->m->final_pdf_version) &&
|
|
||||||
adbe.hasKey("/ExtensionLevel") &&
|
|
||||||
adbe.getKey("/ExtensionLevel").isInteger() &&
|
adbe.getKey("/ExtensionLevel").isInteger() &&
|
||||||
(adbe.getKey("/ExtensionLevel").getIntValue() ==
|
(adbe.getKey("/ExtensionLevel").getIntValue() ==
|
||||||
this->m->final_extension_level))
|
this->m->final_extension_level))
|
||||||
@ -1764,7 +1758,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
|||||||
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
||||||
{
|
{
|
||||||
QPDFObjectHandle item = filter.getArrayItem(i);
|
QPDFObjectHandle item = filter.getArrayItem(i);
|
||||||
if (item.isName() && item.getName() == "/Crypt")
|
if (item.isNameAndEquals("/Crypt"))
|
||||||
{
|
{
|
||||||
idx = i;
|
idx = i;
|
||||||
break;
|
break;
|
||||||
@ -1802,9 +1796,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
|||||||
writeString(QPDF_Name::normalizeName(key));
|
writeString(QPDF_Name::normalizeName(key));
|
||||||
writeString(" ");
|
writeString(" ");
|
||||||
if (key == "/Contents" &&
|
if (key == "/Contents" &&
|
||||||
object.hasKey("/Type") &&
|
object.isDictionaryOfType("/Sig") &&
|
||||||
object.getKey("/Type").isName() &&
|
|
||||||
object.getKey("/Type").getName() == "/Sig" &&
|
|
||||||
object.hasKey("/ByteRange"))
|
object.hasKey("/ByteRange"))
|
||||||
{
|
{
|
||||||
QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
|
QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
|
||||||
|
@ -35,10 +35,8 @@ class SF_Crypt: public QPDFStreamFilter
|
|||||||
for (auto const& key: decode_parms.getKeys())
|
for (auto const& key: decode_parms.getKeys())
|
||||||
{
|
{
|
||||||
if (((key == "/Type") || (key == "/Name")) &&
|
if (((key == "/Type") || (key == "/Name")) &&
|
||||||
(decode_parms.getKey("/Type").isNull() ||
|
((! decode_parms.hasKey("/Type")) ||
|
||||||
(decode_parms.getKey("/Type").isName() &&
|
decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")))
|
||||||
(decode_parms.getKey("/Type").getName() ==
|
|
||||||
"/CryptFilterDecodeParms"))))
|
|
||||||
{
|
{
|
||||||
// we handle this in decryptStream
|
// we handle this in decryptStream
|
||||||
}
|
}
|
||||||
|
@ -1262,9 +1262,7 @@ QPDF::decryptStream(PointerHolder<EncryptionParameters> encp,
|
|||||||
{
|
{
|
||||||
QPDFObjectHandle decode_parms =
|
QPDFObjectHandle decode_parms =
|
||||||
stream_dict.getKey("/DecodeParms");
|
stream_dict.getKey("/DecodeParms");
|
||||||
if (decode_parms.getKey("/Type").isName() &&
|
if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))
|
||||||
(decode_parms.getKey("/Type").getName() ==
|
|
||||||
"/CryptFilterDecodeParms"))
|
|
||||||
{
|
{
|
||||||
QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
|
QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
|
||||||
method = interpretCF(encp, decode_parms.getKey("/Name"));
|
method = interpretCF(encp, decode_parms.getKey("/Name"));
|
||||||
@ -1280,8 +1278,7 @@ QPDF::decryptStream(PointerHolder<EncryptionParameters> encp,
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
||||||
{
|
{
|
||||||
if (filter.getArrayItem(i).isName() &&
|
if (filter.getArrayItem(i).isNameAndEquals("/Crypt"))
|
||||||
(filter.getArrayItem(i).getName() == "/Crypt"))
|
|
||||||
{
|
{
|
||||||
QPDFObjectHandle crypt_params =
|
QPDFObjectHandle crypt_params =
|
||||||
decode.getArrayItem(i);
|
decode.getArrayItem(i);
|
||||||
|
@ -382,17 +382,13 @@ QPDF::updateObjectMapsInternal(
|
|||||||
|
|
||||||
bool is_page_node = false;
|
bool is_page_node = false;
|
||||||
|
|
||||||
if (oh.isDictionary() && oh.hasKey("/Type"))
|
if (oh.isDictionaryOfType("/Page"))
|
||||||
{
|
{
|
||||||
std::string type = oh.getKey("/Type").getName();
|
is_page_node = true;
|
||||||
if (type == "/Page")
|
if (! top)
|
||||||
{
|
{
|
||||||
is_page_node = true;
|
return;
|
||||||
if (! top)
|
}
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oh.isIndirect())
|
if (oh.isIndirect())
|
||||||
|
@ -139,8 +139,7 @@ QPDF::getAllPagesInternal(QPDFObjectHandle cur_node,
|
|||||||
result.push_back(cur_node);
|
result.push_back(cur_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPDFObjectHandle type_key = cur_node.getKey("/Type");
|
if (! cur_node.isDictionaryOfType(wanted_type))
|
||||||
if (! (type_key.isName() && (type_key.getName() == wanted_type)))
|
|
||||||
{
|
{
|
||||||
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
||||||
"page tree node",
|
"page tree node",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user