Commit eee05a0c18e4432e9d7e02ec252052ac479ee539
1 parent
4257c7bd
Refactor: replace `getArrayNItems` with `size`, simplify array traversal, centra…
…lize type conversions, and improve clarity across multiple modules.
Showing
5 changed files
with
16 additions
and
16 deletions
libqpdf/QPDF_encryption.cc
| ... | ... | @@ -664,7 +664,7 @@ QPDF::EncryptionParameters::initialize(QPDF& qpdf) |
| 664 | 664 | |
| 665 | 665 | std::string id1; |
| 666 | 666 | auto id_obj = trailer.getKey("/ID"); |
| 667 | - if (!id_obj.isArray() || id_obj.getArrayNItems() != 2 || !id_obj.getArrayItem(0).isString()) { | |
| 667 | + if (id_obj.size() != 2 || !id_obj.getArrayItem(0).isString()) { | |
| 668 | 668 | // Treating a missing ID as the empty string enables qpdf to decrypt some invalid encrypted |
| 669 | 669 | // files with no /ID that poppler can read but Adobe Reader can't. |
| 670 | 670 | qpdf.warn(qpdf.damagedPDF("trailer", "invalid /ID in trailer dictionary")); |
| ... | ... | @@ -961,19 +961,21 @@ QPDF::decryptStream( |
| 961 | 961 | } else if ( |
| 962 | 962 | stream_dict.getKey("/DecodeParms").isArray() && |
| 963 | 963 | stream_dict.getKey("/Filter").isArray()) { |
| 964 | - QPDFObjectHandle filter = stream_dict.getKey("/Filter"); | |
| 965 | - QPDFObjectHandle decode = stream_dict.getKey("/DecodeParms"); | |
| 966 | - if (filter.getArrayNItems() == decode.getArrayNItems()) { | |
| 967 | - for (int i = 0; i < filter.getArrayNItems(); ++i) { | |
| 968 | - if (filter.getArrayItem(i).isNameAndEquals("/Crypt")) { | |
| 969 | - QPDFObjectHandle crypt_params = decode.getArrayItem(i); | |
| 964 | + auto filter = stream_dict.getKey("/Filter"); | |
| 965 | + auto decode = stream_dict.getKey("/DecodeParms"); | |
| 966 | + if (filter.size() == decode.size()) { | |
| 967 | + int i = 0; | |
| 968 | + for (auto const& item: filter.as_array()) { | |
| 969 | + if (item.isNameAndEquals("/Crypt")) { | |
| 970 | + auto crypt_params = decode.getArrayItem(i); | |
| 970 | 971 | if (crypt_params.isDictionary() && |
| 971 | 972 | crypt_params.getKey("/Name").isName()) { |
| 972 | - QTC::TC("qpdf", "QPDF_encrypt crypt array"); | |
| 973 | 973 | method = encp->interpretCF(crypt_params.getKey("/Name")); |
| 974 | 974 | method_source = "stream's Crypt decode parameters (array)"; |
| 975 | 975 | } |
| 976 | + break; | |
| 976 | 977 | } |
| 978 | + ++i; | |
| 977 | 979 | } |
| 978 | 980 | } |
| 979 | 981 | } | ... | ... |
libqpdf/QPDF_linearization.cc
| ... | ... | @@ -174,14 +174,13 @@ QPDF::readLinearizationData() |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Hint table array: offset length [ offset length ] |
| 177 | - size_t n_H_items = toS(H.getArrayNItems()); | |
| 178 | - if (!((n_H_items == 2) || (n_H_items == 4))) { | |
| 177 | + size_t n_H_items = H.size(); | |
| 178 | + if (!(n_H_items == 2 || n_H_items == 4)) { | |
| 179 | 179 | throw damagedPDF("linearization dictionary", "H has the wrong number of items"); |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | std::vector<int> H_items; |
| 183 | - for (size_t i = 0; i < n_H_items; ++i) { | |
| 184 | - QPDFObjectHandle oh(H.getArrayItem(toI(i))); | |
| 183 | + for (auto const& oh: H.as_array()) { | |
| 185 | 184 | if (oh.isInteger()) { |
| 186 | 185 | H_items.push_back(oh.getIntValueAsInt()); |
| 187 | 186 | } else { | ... | ... |
libqpdf/QPDF_objects.cc
| ... | ... | @@ -782,7 +782,7 @@ std::pair<int, std::array<int, 3>> |
| 782 | 782 | QPDF::processXRefW(QPDFObjectHandle& dict, std::function<QPDFExc(std::string_view)> damaged) |
| 783 | 783 | { |
| 784 | 784 | auto W_obj = dict.getKey("/W"); |
| 785 | - if (!(W_obj.isArray() && (W_obj.getArrayNItems() >= 3) && W_obj.getArrayItem(0).isInteger() && | |
| 785 | + if (!(W_obj.size() >= 3 && W_obj.getArrayItem(0).isInteger() && | |
| 786 | 786 | W_obj.getArrayItem(1).isInteger() && W_obj.getArrayItem(2).isInteger())) { |
| 787 | 787 | throw damaged("Cross-reference stream does not have a proper /W key"); |
| 788 | 788 | } | ... | ... |
libqpdf/QPDF_pages.cc
| ... | ... | @@ -313,7 +313,7 @@ QPDF::insertPage(QPDFObjectHandle newpage, int pos) |
| 313 | 313 | |
| 314 | 314 | newpage.replaceKey("/Parent", pages); |
| 315 | 315 | kids.insertItem(pos, newpage); |
| 316 | - int npages = kids.getArrayNItems(); | |
| 316 | + int npages = static_cast<int>(kids.size()); | |
| 317 | 317 | pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); |
| 318 | 318 | m->all_pages.insert(m->all_pages.begin() + pos, newpage); |
| 319 | 319 | for (int i = pos + 1; i < npages; ++i) { |
| ... | ... | @@ -337,7 +337,7 @@ QPDF::removePage(QPDFObjectHandle page) |
| 337 | 337 | QPDFObjectHandle kids = pages.getKey("/Kids"); |
| 338 | 338 | |
| 339 | 339 | kids.eraseItem(pos); |
| 340 | - int npages = kids.getArrayNItems(); | |
| 340 | + int npages = static_cast<int>(kids.size()); | |
| 341 | 341 | pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); |
| 342 | 342 | m->all_pages.erase(m->all_pages.begin() + pos); |
| 343 | 343 | m->pageobj_to_pages_pos.erase(page.getObjGen()); | ... | ... |
qpdf/qpdf.testcov
| ... | ... | @@ -237,7 +237,6 @@ QPDFWriter remove ADBE 0 |
| 237 | 237 | QPDFWriter remove existing Extensions 0 |
| 238 | 238 | QPDFWriter preserve ADBE 0 |
| 239 | 239 | QPDF_encryption skip 0x28 0 |
| 240 | -QPDF_encrypt crypt array 0 | |
| 241 | 240 | QPDF_encryption CFM AESV3 0 |
| 242 | 241 | qpdf-c called qpdf_get_pdf_extension_level 0 |
| 243 | 242 | qpdf-c called qpdf_set_r5_encryption_parameters 0 | ... | ... |