Commit 07db3200cb0ef058b8a66ece8d9757adfba49fd1

Authored by m-holger
Committed by Jay Berkenbilt
1 parent 710d2e54

Remove some if statements and simplify some boolean expressions

Use QPDFObjectHandle::isNameAndEquals, isDictionaryOfType and
isStreamOfType.
examples/pdf-create.cc
@@ -290,15 +290,14 @@ static void check(char const* filename, @@ -290,15 +290,14 @@ static void check(char const* filename,
290 QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace"); 290 QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
291 QPDFObjectHandle filter = image_dict.getKey("/Filter"); 291 QPDFObjectHandle filter = image_dict.getKey("/Filter");
292 bool this_errors = false; 292 bool this_errors = false;
293 - if (! (filter.isName() && (filter.getName() == desired_filter))) 293 + if (! filter.isNameAndEquals(desired_filter))
294 { 294 {
295 this_errors = errors = true; 295 this_errors = errors = true;
296 std::cout << "page " << pageno << ": expected filter " 296 std::cout << "page " << pageno << ": expected filter "
297 << desired_filter << "; actual filter = " 297 << desired_filter << "; actual filter = "
298 << filter.unparse() << std::endl; 298 << filter.unparse() << std::endl;
299 } 299 }
300 - if (! (color_space.isName() &&  
301 - (color_space.getName() == desired_color_space))) 300 + if (! color_space.isNameAndEquals(desired_color_space))
302 { 301 {
303 this_errors = errors = true; 302 this_errors = errors = true;
304 std::cout << "page " << pageno << ": expected color space " 303 std::cout << "page " << pageno << ": expected color space "
examples/pdf-invert-images.cc
@@ -167,9 +167,8 @@ int main(int argc, char* argv[]) @@ -167,9 +167,8 @@ int main(int argc, char* argv[])
167 // keys to determine the image type. 167 // keys to determine the image type.
168 if (image.pipeStreamData(0, qpdf_ef_compress, 168 if (image.pipeStreamData(0, qpdf_ef_compress,
169 qpdf_dl_all) && 169 qpdf_dl_all) &&
170 - color_space.isName() && 170 + color_space.isNameAndEquals("/DeviceGray") &&
171 bits_per_component.isInteger() && 171 bits_per_component.isInteger() &&
172 - (color_space.getName() == "/DeviceGray") &&  
173 (bits_per_component.getIntValue() == 8)) 172 (bits_per_component.getIntValue() == 8))
174 { 173 {
175 inv->registerImage(image, p); 174 inv->registerImage(image, p);
libqpdf/QPDF.cc
@@ -1100,10 +1100,7 @@ QPDF::read_xrefStream(qpdf_offset_t xref_offset) @@ -1100,10 +1100,7 @@ QPDF::read_xrefStream(qpdf_offset_t xref_offset)
1100 { 1100 {
1101 // ignore -- report error below 1101 // ignore -- report error below
1102 } 1102 }
1103 - if (xref_obj.isInitialized() &&  
1104 - xref_obj.isStream() &&  
1105 - xref_obj.getDict().getKey("/Type").isName() &&  
1106 - xref_obj.getDict().getKey("/Type").getName() == "/XRef") 1103 + if (xref_obj.isStreamOfType("/XRef"))
1107 { 1104 {
1108 QTC::TC("qpdf", "QPDF found xref stream"); 1105 QTC::TC("qpdf", "QPDF found xref stream");
1109 found = true; 1106 found = true;
@@ -2202,8 +2199,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number) @@ -2202,8 +2199,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
2202 this->m->obj_cache[stream_og].end_after_space; 2199 this->m->obj_cache[stream_og].end_after_space;
2203 2200
2204 QPDFObjectHandle dict = obj_stream.getDict(); 2201 QPDFObjectHandle dict = obj_stream.getDict();
2205 - if (! (dict.getKey("/Type").isName() &&  
2206 - dict.getKey("/Type").getName() == "/ObjStm")) 2202 + if (! dict.isDictionaryOfType("/ObjStm"))
2207 { 2203 {
2208 QTC::TC("qpdf", "QPDF ERR object stream with wrong type"); 2204 QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
2209 warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), 2205 warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
@@ -2849,13 +2845,10 @@ QPDF::getCompressibleObjGens() @@ -2849,13 +2845,10 @@ QPDF::getCompressibleObjGens()
2849 { 2845 {
2850 QTC::TC("qpdf", "QPDF exclude encryption dictionary"); 2846 QTC::TC("qpdf", "QPDF exclude encryption dictionary");
2851 } 2847 }
2852 - else if ((! obj.isStream()) &&  
2853 - (! (obj.isDictionary() && 2848 + else if (! (obj.isStream() ||
  2849 + (obj.isDictionaryOfType("/Sig") &&
2854 obj.hasKey("/ByteRange") && 2850 obj.hasKey("/ByteRange") &&
2855 - obj.hasKey("/Contents") &&  
2856 - obj.hasKey("/Type") &&  
2857 - obj.getKey("/Type").isName() &&  
2858 - obj.getKey("/Type").getName() == "/Sig"))) 2851 + obj.hasKey("/Contents"))))
2859 { 2852 {
2860 result.push_back(og); 2853 result.push_back(og);
2861 } 2854 }
libqpdf/QPDFAcroFormDocumentHelper.cc
@@ -280,9 +280,7 @@ QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h) @@ -280,9 +280,7 @@ QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h)
280 { 280 {
281 QPDFObjectHandle oh = h.getObjectHandle(); 281 QPDFObjectHandle oh = h.getObjectHandle();
282 QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull()); 282 QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());
283 - if (! (oh.isDictionary() &&  
284 - oh.getKey("/Subtype").isName() &&  
285 - (oh.getKey("/Subtype").getName() == "/Widget"))) 283 + if (! oh.isDictionaryOfType("", "/Widget"))
286 { 284 {
287 return result; 285 return result;
288 } 286 }
libqpdf/QPDFFileSpecObjectHelper.cc
@@ -15,8 +15,7 @@ QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper( @@ -15,8 +15,7 @@ QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(
15 oh.warnIfPossible("Embedded file object is not a dictionary"); 15 oh.warnIfPossible("Embedded file object is not a dictionary");
16 return; 16 return;
17 } 17 }
18 - auto type = oh.getKey("/Type");  
19 - if (! (type.isName() && (type.getName() == "/Filespec"))) 18 + if (! oh.isDictionaryOfType("/Filespec"))
20 { 19 {
21 oh.warnIfPossible("Embedded file object's type is not /Filespec"); 20 oh.warnIfPossible("Embedded file object's type is not /Filespec");
22 } 21 }
libqpdf/QPDFObjectHandle.cc
@@ -1055,23 +1055,9 @@ QPDFObjectHandle::getDictAsMap() @@ -1055,23 +1055,9 @@ QPDFObjectHandle::getDictAsMap()
1055 bool 1055 bool
1056 QPDFObjectHandle::isOrHasName(std::string const& value) 1056 QPDFObjectHandle::isOrHasName(std::string const& value)
1057 { 1057 {
1058 - if (isName() && (getName() == value))  
1059 - {  
1060 - return true;  
1061 - }  
1062 - else if (isArray())  
1063 - {  
1064 - int n = getArrayNItems();  
1065 - for (int i = 0; i < n; ++i)  
1066 - {  
1067 - QPDFObjectHandle item = getArrayItem(0);  
1068 - if (item.isName() && (item.getName() == value))  
1069 - {  
1070 - return true;  
1071 - }  
1072 - }  
1073 - }  
1074 - return false; 1058 + return isNameAndEquals(value) ||
  1059 + (isArray() && (getArrayNItems() > 0) &&
  1060 + getArrayItem(0).isNameAndEquals(value));
1075 } 1061 }
1076 1062
1077 void 1063 void
@@ -2520,8 +2506,7 @@ QPDFObjectHandle::parseInternal(PointerHolder&lt;InputSource&gt; input, @@ -2520,8 +2506,7 @@ QPDFObjectHandle::parseInternal(PointerHolder&lt;InputSource&gt; input,
2520 } 2506 }
2521 if (!contents_string.empty() && 2507 if (!contents_string.empty() &&
2522 dict.count("/Type") && 2508 dict.count("/Type") &&
2523 - dict["/Type"].isName() &&  
2524 - dict["/Type"].getName() == "/Sig" && 2509 + dict["/Type"].isNameAndEquals("/Sig") &&
2525 dict.count("/ByteRange") && 2510 dict.count("/ByteRange") &&
2526 dict.count("/Contents") && 2511 dict.count("/Contents") &&
2527 dict["/Contents"].isString()) 2512 dict["/Contents"].isString())
@@ -3237,7 +3222,7 @@ QPDFObjectHandle::isPageObject() @@ -3237,7 +3222,7 @@ QPDFObjectHandle::isPageObject()
3237 if (this->hasKey("/Type")) 3222 if (this->hasKey("/Type"))
3238 { 3223 {
3239 QPDFObjectHandle type = this->getKey("/Type"); 3224 QPDFObjectHandle type = this->getKey("/Type");
3240 - if (type.isName() && (type.getName() == "/Page")) 3225 + if (type.isNameAndEquals("/Page"))
3241 { 3226 {
3242 return true; 3227 return true;
3243 } 3228 }
@@ -3263,39 +3248,22 @@ QPDFObjectHandle::isPagesObject() @@ -3263,39 +3248,22 @@ QPDFObjectHandle::isPagesObject()
3263 } 3248 }
3264 // getAllPages repairs /Type when traversing the page tree. 3249 // getAllPages repairs /Type when traversing the page tree.
3265 getOwningQPDF()->getAllPages(); 3250 getOwningQPDF()->getAllPages();
3266 - return (this->isDictionary() &&  
3267 - this->hasKey("/Type") &&  
3268 - this->getKey("/Type").isName() &&  
3269 - this->getKey("/Type").getName() == "/Pages"); 3251 + return isDictionaryOfType("/Pages");
3270 } 3252 }
3271 3253
3272 bool 3254 bool
3273 QPDFObjectHandle::isFormXObject() 3255 QPDFObjectHandle::isFormXObject()
3274 { 3256 {
3275 - if (! this->isStream())  
3276 - {  
3277 - return false;  
3278 - }  
3279 - QPDFObjectHandle dict = this->getDict();  
3280 - return (dict.getKey("/Type").isName() &&  
3281 - ("/XObject" == dict.getKey("/Type").getName()) &&  
3282 - dict.getKey("/Subtype").isName() &&  
3283 - ("/Form" == dict.getKey("/Subtype").getName())); 3257 + return isStreamOfType("/XObject", "/Form");
3284 } 3258 }
3285 3259
3286 bool 3260 bool
3287 QPDFObjectHandle::isImage(bool exclude_imagemask) 3261 QPDFObjectHandle::isImage(bool exclude_imagemask)
3288 { 3262 {
3289 - if (! this->isStream())  
3290 - {  
3291 - return false;  
3292 - }  
3293 - QPDFObjectHandle dict = this->getDict();  
3294 - return (dict.hasKey("/Subtype") &&  
3295 - (dict.getKey("/Subtype").getName() == "/Image") && 3263 + return (isStreamOfType("", "/Image") &&
3296 ((! exclude_imagemask) || 3264 ((! exclude_imagemask) ||
3297 - (! (dict.getKey("/ImageMask").isBool() &&  
3298 - dict.getKey("/ImageMask").getBoolValue())))); 3265 + (! (getDict().getKey("/ImageMask").isBool() &&
  3266 + getDict().getKey("/ImageMask").getBoolValue()))));
3299 } 3267 }
3300 3268
3301 void 3269 void
libqpdf/QPDFPageObjectHelper.cc
@@ -558,10 +558,7 @@ QPDFPageObjectHelper::getAnnotations(std::string const&amp; only_subtype) @@ -558,10 +558,7 @@ QPDFPageObjectHelper::getAnnotations(std::string const&amp; only_subtype)
558 for (int i = 0; i < nannots; ++i) 558 for (int i = 0; i < nannots; ++i)
559 { 559 {
560 QPDFObjectHandle annot = annots.getArrayItem(i); 560 QPDFObjectHandle annot = annots.getArrayItem(i);
561 - if (only_subtype.empty() ||  
562 - (annot.isDictionary() &&  
563 - annot.getKey("/Subtype").isName() &&  
564 - (only_subtype == annot.getKey("/Subtype").getName()))) 561 + if (annot.isDictionaryOfType("", only_subtype))
565 { 562 {
566 result.push_back(QPDFAnnotationObjectHelper(annot)); 563 result.push_back(QPDFAnnotationObjectHelper(annot));
567 } 564 }
libqpdf/QPDFWriter.cc
@@ -1241,9 +1241,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object) @@ -1241,9 +1241,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object)
1241 " another file."); 1241 " another file.");
1242 } 1242 }
1243 1243
1244 - if (this->m->qdf_mode &&  
1245 - object.isStream() && object.getDict().getKey("/Type").isName() &&  
1246 - (object.getDict().getKey("/Type").getName() == "/XRef")) 1244 + if (this->m->qdf_mode && object.isStreamOfType("/XRef"))
1247 { 1245 {
1248 // As a special case, do not output any extraneous XRef 1246 // As a special case, do not output any extraneous XRef
1249 // streams in QDF mode. Doing so will confuse fix-qdf, 1247 // streams in QDF mode. Doing so will confuse fix-qdf,
@@ -1474,8 +1472,7 @@ QPDFWriter::willFilterStream(QPDFObjectHandle stream, @@ -1474,8 +1472,7 @@ QPDFWriter::willFilterStream(QPDFObjectHandle stream,
1474 QPDFObjGen old_og = stream.getObjGen(); 1472 QPDFObjGen old_og = stream.getObjGen();
1475 QPDFObjectHandle stream_dict = stream.getDict(); 1473 QPDFObjectHandle stream_dict = stream.getDict();
1476 1474
1477 - if (stream_dict.getKey("/Type").isName() &&  
1478 - (stream_dict.getKey("/Type").getName() == "/Metadata")) 1475 + if (stream_dict.isDictionaryOfType("/Metadata"))
1479 { 1476 {
1480 is_metadata = true; 1477 is_metadata = true;
1481 } 1478 }
@@ -1691,11 +1688,8 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, @@ -1691,11 +1688,8 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1691 QTC::TC("qpdf", "QPDFWriter preserve Extensions"); 1688 QTC::TC("qpdf", "QPDFWriter preserve Extensions");
1692 QPDFObjectHandle adbe = extensions.getKey("/ADBE"); 1689 QPDFObjectHandle adbe = extensions.getKey("/ADBE");
1693 if (adbe.isDictionary() && 1690 if (adbe.isDictionary() &&
1694 - adbe.hasKey("/BaseVersion") &&  
1695 - adbe.getKey("/BaseVersion").isName() &&  
1696 - (adbe.getKey("/BaseVersion").getName() ==  
1697 - "/" + this->m->final_pdf_version) &&  
1698 - adbe.hasKey("/ExtensionLevel") && 1691 + adbe.getKey("/BaseVersion").isNameAndEquals(
  1692 + "/" + this->m->final_pdf_version) &&
1699 adbe.getKey("/ExtensionLevel").isInteger() && 1693 adbe.getKey("/ExtensionLevel").isInteger() &&
1700 (adbe.getKey("/ExtensionLevel").getIntValue() == 1694 (adbe.getKey("/ExtensionLevel").getIntValue() ==
1701 this->m->final_extension_level)) 1695 this->m->final_extension_level))
@@ -1764,7 +1758,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, @@ -1764,7 +1758,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1764 for (int i = 0; i < filter.getArrayNItems(); ++i) 1758 for (int i = 0; i < filter.getArrayNItems(); ++i)
1765 { 1759 {
1766 QPDFObjectHandle item = filter.getArrayItem(i); 1760 QPDFObjectHandle item = filter.getArrayItem(i);
1767 - if (item.isName() && item.getName() == "/Crypt") 1761 + if (item.isNameAndEquals("/Crypt"))
1768 { 1762 {
1769 idx = i; 1763 idx = i;
1770 break; 1764 break;
@@ -1802,9 +1796,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, @@ -1802,9 +1796,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1802 writeString(QPDF_Name::normalizeName(key)); 1796 writeString(QPDF_Name::normalizeName(key));
1803 writeString(" "); 1797 writeString(" ");
1804 if (key == "/Contents" && 1798 if (key == "/Contents" &&
1805 - object.hasKey("/Type") &&  
1806 - object.getKey("/Type").isName() &&  
1807 - object.getKey("/Type").getName() == "/Sig" && 1799 + object.isDictionaryOfType("/Sig") &&
1808 object.hasKey("/ByteRange")) 1800 object.hasKey("/ByteRange"))
1809 { 1801 {
1810 QTC::TC("qpdf", "QPDFWriter no encryption sig contents"); 1802 QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
libqpdf/QPDF_Stream.cc
@@ -35,10 +35,8 @@ class SF_Crypt: public QPDFStreamFilter @@ -35,10 +35,8 @@ class SF_Crypt: public QPDFStreamFilter
35 for (auto const& key: decode_parms.getKeys()) 35 for (auto const& key: decode_parms.getKeys())
36 { 36 {
37 if (((key == "/Type") || (key == "/Name")) && 37 if (((key == "/Type") || (key == "/Name")) &&
38 - (decode_parms.getKey("/Type").isNull() ||  
39 - (decode_parms.getKey("/Type").isName() &&  
40 - (decode_parms.getKey("/Type").getName() ==  
41 - "/CryptFilterDecodeParms")))) 38 + ((! decode_parms.hasKey("/Type")) ||
  39 + decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")))
42 { 40 {
43 // we handle this in decryptStream 41 // we handle this in decryptStream
44 } 42 }
libqpdf/QPDF_encryption.cc
@@ -1262,9 +1262,7 @@ QPDF::decryptStream(PointerHolder&lt;EncryptionParameters&gt; encp, @@ -1262,9 +1262,7 @@ QPDF::decryptStream(PointerHolder&lt;EncryptionParameters&gt; encp,
1262 { 1262 {
1263 QPDFObjectHandle decode_parms = 1263 QPDFObjectHandle decode_parms =
1264 stream_dict.getKey("/DecodeParms"); 1264 stream_dict.getKey("/DecodeParms");
1265 - if (decode_parms.getKey("/Type").isName() &&  
1266 - (decode_parms.getKey("/Type").getName() ==  
1267 - "/CryptFilterDecodeParms")) 1265 + if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))
1268 { 1266 {
1269 QTC::TC("qpdf", "QPDF_encryption stream crypt filter"); 1267 QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
1270 method = interpretCF(encp, decode_parms.getKey("/Name")); 1268 method = interpretCF(encp, decode_parms.getKey("/Name"));
@@ -1280,8 +1278,7 @@ QPDF::decryptStream(PointerHolder&lt;EncryptionParameters&gt; encp, @@ -1280,8 +1278,7 @@ QPDF::decryptStream(PointerHolder&lt;EncryptionParameters&gt; encp,
1280 { 1278 {
1281 for (int i = 0; i < filter.getArrayNItems(); ++i) 1279 for (int i = 0; i < filter.getArrayNItems(); ++i)
1282 { 1280 {
1283 - if (filter.getArrayItem(i).isName() &&  
1284 - (filter.getArrayItem(i).getName() == "/Crypt")) 1281 + if (filter.getArrayItem(i).isNameAndEquals("/Crypt"))
1285 { 1282 {
1286 QPDFObjectHandle crypt_params = 1283 QPDFObjectHandle crypt_params =
1287 decode.getArrayItem(i); 1284 decode.getArrayItem(i);
libqpdf/QPDF_optimization.cc
@@ -382,17 +382,13 @@ QPDF::updateObjectMapsInternal( @@ -382,17 +382,13 @@ QPDF::updateObjectMapsInternal(
382 382
383 bool is_page_node = false; 383 bool is_page_node = false;
384 384
385 - if (oh.isDictionary() && oh.hasKey("/Type")) 385 + if (oh.isDictionaryOfType("/Page"))
386 { 386 {
387 - std::string type = oh.getKey("/Type").getName();  
388 - if (type == "/Page")  
389 - {  
390 - is_page_node = true;  
391 - if (! top)  
392 - {  
393 - return;  
394 - }  
395 - } 387 + is_page_node = true;
  388 + if (! top)
  389 + {
  390 + return;
  391 + }
396 } 392 }
397 393
398 if (oh.isIndirect()) 394 if (oh.isIndirect())
libqpdf/QPDF_pages.cc
@@ -139,8 +139,7 @@ QPDF::getAllPagesInternal(QPDFObjectHandle cur_node, @@ -139,8 +139,7 @@ QPDF::getAllPagesInternal(QPDFObjectHandle cur_node,
139 result.push_back(cur_node); 139 result.push_back(cur_node);
140 } 140 }
141 141
142 - QPDFObjectHandle type_key = cur_node.getKey("/Type");  
143 - if (! (type_key.isName() && (type_key.getName() == wanted_type))) 142 + if (! cur_node.isDictionaryOfType(wanted_type))
144 { 143 {
145 warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), 144 warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
146 "page tree node", 145 "page tree node",