Commit e8ade6833a2630bb751712890f5376f9969c7f8b

Authored by m-holger
1 parent d11d75a9

In `ObjUser` and in linearization calculations refactor to use `size_t` for page…

… numbers and related variables, replacing `int`, to enhance consistency and avoid unnecessary checked conversions.
libqpdf/QPDF_linearization.cc
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 #include <algorithm> 19 #include <algorithm>
20 #include <cmath> 20 #include <cmath>
21 #include <cstring> 21 #include <cstring>
  22 +#include <utility>
22 23
23 using namespace qpdf; 24 using namespace qpdf;
24 using namespace std::literals; 25 using namespace std::literals;
@@ -221,7 +222,7 @@ QPDF::readLinearizationData() @@ -221,7 +222,7 @@ QPDF::readLinearizationData()
221 // file_size initialized by isLinearized() 222 // file_size initialized by isLinearized()
222 m->linp.first_page_object = O.getIntValueAsInt(); 223 m->linp.first_page_object = O.getIntValueAsInt();
223 m->linp.first_page_end = E.getIntValue(); 224 m->linp.first_page_end = E.getIntValue();
224 - m->linp.npages = N.getIntValueAsInt(); 225 + m->linp.npages = N.getUIntValueAsUInt();
225 m->linp.xref_zero_offset = T.getIntValue(); 226 m->linp.xref_zero_offset = T.getIntValue();
226 m->linp.first_page = first_page; 227 m->linp.first_page = first_page;
227 m->linp.H_offset = H0_offset; 228 m->linp.H_offset = H0_offset;
@@ -333,7 +334,7 @@ QPDF::readHPageOffset(BitStream h) @@ -333,7 +334,7 @@ QPDF::readHPageOffset(BitStream h)
333 334
334 std::vector<HPageOffsetEntry>& entries = t.entries; 335 std::vector<HPageOffsetEntry>& entries = t.entries;
335 entries.clear(); 336 entries.clear();
336 - int nitems = m->linp.npages; 337 + int nitems = toI(m->linp.npages);
337 load_vector_int(h, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects); 338 load_vector_int(h, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects);
338 load_vector_int( 339 load_vector_int(
339 h, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length); 340 h, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length);
@@ -423,13 +424,13 @@ QPDF::checkLinearizationInternal() @@ -423,13 +424,13 @@ QPDF::checkLinearizationInternal()
423 } 424 }
424 425
425 // N: number of pages 426 // N: number of pages
426 - int npages = toI(pages.size());  
427 - if (p.npages != npages) { 427 + size_t npages = pages.size();
  428 + if (std::cmp_not_equal(p.npages, npages)) {
428 // Not tested in the test suite 429 // Not tested in the test suite
429 linearizationWarning("page count (/N) mismatch"); 430 linearizationWarning("page count (/N) mismatch");
430 } 431 }
431 432
432 - for (size_t i = 0; i < toS(npages); ++i) { 433 + for (size_t i = 0; i < npages; ++i) {
433 QPDFObjectHandle const& page = pages.at(i); 434 QPDFObjectHandle const& page = pages.at(i);
434 QPDFObjGen og(page.getObjGen()); 435 QPDFObjGen og(page.getObjGen());
435 if (m->xref_table[og].getType() == 2) { 436 if (m->xref_table[og].getType() == 2) {
@@ -620,7 +621,7 @@ QPDF::checkHPageOffset( @@ -620,7 +621,7 @@ QPDF::checkHPageOffset(
620 // Empirically, it also seems that Acrobat sometimes puts items under a page's /Resources 621 // Empirically, it also seems that Acrobat sometimes puts items under a page's /Resources
621 // dictionary in with shared objects even when they are private. 622 // dictionary in with shared objects even when they are private.
622 623
623 - int npages = toI(pages.size()); 624 + size_t npages = pages.size();
624 qpdf_offset_t table_offset = adjusted_offset(m->page_offset_hints.first_page_offset); 625 qpdf_offset_t table_offset = adjusted_offset(m->page_offset_hints.first_page_offset);
625 QPDFObjGen first_page_og(pages.at(0).getObjGen()); 626 QPDFObjGen first_page_og(pages.at(0).getObjGen());
626 if (!m->xref_table.contains(first_page_og)) { 627 if (!m->xref_table.contains(first_page_og)) {
@@ -631,8 +632,8 @@ QPDF::checkHPageOffset( @@ -631,8 +632,8 @@ QPDF::checkHPageOffset(
631 linearizationWarning("first page object offset mismatch"); 632 linearizationWarning("first page object offset mismatch");
632 } 633 }
633 634
634 - for (int pageno = 0; pageno < npages; ++pageno) {  
635 - QPDFObjGen page_og(pages.at(toS(pageno)).getObjGen()); 635 + for (size_t pageno = 0; pageno < npages; ++pageno) {
  636 + QPDFObjGen page_og(pages.at(pageno).getObjGen());
636 int first_object = page_og.getObj(); 637 int first_object = page_og.getObj();
637 if (!m->xref_table.contains(page_og)) { 638 if (!m->xref_table.contains(page_og)) {
638 stopOnError("unknown object in page offset hint table"); 639 stopOnError("unknown object in page offset hint table");
@@ -897,7 +898,7 @@ QPDF::dumpHPageOffset() @@ -897,7 +898,7 @@ QPDF::dumpHPageOffset()
897 << "nbits_shared_numerator: " << t.nbits_shared_numerator << "\n" 898 << "nbits_shared_numerator: " << t.nbits_shared_numerator << "\n"
898 << "shared_denominator: " << t.shared_denominator << "\n"; 899 << "shared_denominator: " << t.shared_denominator << "\n";
899 900
900 - for (size_t i1 = 0; i1 < toS(m->linp.npages); ++i1) { 901 + for (size_t i1 = 0; i1 < m->linp.npages; ++i1) {
901 HPageOffsetEntry& pe = t.entries.at(i1); 902 HPageOffsetEntry& pe = t.entries.at(i1);
902 *m->log->getInfo() << "Page " << i1 << ":\n" 903 *m->log->getInfo() << "Page " << i1 << ":\n"
903 << " nobjects: " << pe.delta_nobjects + t.min_nobjects << "\n" 904 << " nobjects: " << pe.delta_nobjects + t.min_nobjects << "\n"
@@ -1149,7 +1150,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1149,7 +1150,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1149 pages.push_back(getUncompressedObject(oh, object_stream_data)); 1150 pages.push_back(getUncompressedObject(oh, object_stream_data));
1150 } 1151 }
1151 } 1152 }
1152 - int npages = toI(pages.size()); 1153 + size_t npages = pages.size();
1153 1154
1154 // We will be initializing some values of the computed hint tables. Specifically, we can 1155 // We will be initializing some values of the computed hint tables. Specifically, we can
1155 // initialize any items that deal with object numbers or counts but not any items that deal with 1156 // initialize any items that deal with object numbers or counts but not any items that deal with
@@ -1160,7 +1161,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1160,7 +1161,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1160 // npages is the size of the existing pages vector, which has been created by traversing the 1161 // npages is the size of the existing pages vector, which has been created by traversing the
1161 // pages tree, and as such is a reasonable size. 1162 // pages tree, and as such is a reasonable size.
1162 m->c_linp.npages = npages; 1163 m->c_linp.npages = npages;
1163 - m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(toS(npages)); 1164 + m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(npages);
1164 1165
1165 // Part 4: open document objects. We don't care about the order. 1166 // Part 4: open document objects. We don't care about the order.
1166 1167
@@ -1216,7 +1217,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1216,7 +1217,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1216 // Part 7: other pages' private objects 1217 // Part 7: other pages' private objects
1217 1218
1218 // For each page in order: 1219 // For each page in order:
1219 - for (size_t i = 1; i < toS(npages); ++i) { 1220 + for (size_t i = 1; i < npages; ++i) {
1220 // Place this page's page object 1221 // Place this page's page object
1221 1222
1222 QPDFObjGen page_og(pages.at(i).getObjGen()); 1223 QPDFObjGen page_og(pages.at(i).getObjGen());
@@ -1233,7 +1234,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1233,7 +1234,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1233 1234
1234 m->c_page_offset_data.entries.at(i).nobjects = 1; 1235 m->c_page_offset_data.entries.at(i).nobjects = 1;
1235 1236
1236 - ObjUser ou(ObjUser::ou_page, toI(i)); 1237 + ObjUser ou(ObjUser::ou_page, i);
1237 if (!m->obj_user_to_objects.contains(ou)) { 1238 if (!m->obj_user_to_objects.contains(ou)) {
1238 stopOnError("found unreferenced page while calculating linearization data"); 1239 stopOnError("found unreferenced page while calculating linearization data");
1239 } 1240 }
@@ -1281,7 +1282,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1281,7 +1282,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1281 1282
1282 // Place private thumbnail images in page order. Slightly more information would be required if 1283 // Place private thumbnail images in page order. Slightly more information would be required if
1283 // we were going to bother with thumbnail hint tables. 1284 // we were going to bother with thumbnail hint tables.
1284 - for (size_t i = 0; i < toS(npages); ++i) { 1285 + for (size_t i = 0; i < npages; ++i) {
1285 QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb"); 1286 QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb");
1286 thumb = getUncompressedObject(thumb, object_stream_data); 1287 thumb = getUncompressedObject(thumb, object_stream_data);
1287 QPDFObjGen thumb_og(thumb.getObjGen()); 1288 QPDFObjGen thumb_og(thumb.getObjGen());
@@ -1294,7 +1295,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1294,7 +1295,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1294 // there's nothing to prevent it from having been in some set other than 1295 // there's nothing to prevent it from having been in some set other than
1295 // lc_thumbnail_private. 1296 // lc_thumbnail_private.
1296 } 1297 }
1297 - std::set<QPDFObjGen>& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, toI(i))]; 1298 + std::set<QPDFObjGen>& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, i)];
1298 for (auto const& og: ogs) { 1299 for (auto const& og: ogs) {
1299 if (lc_thumbnail_private.erase(og)) { 1300 if (lc_thumbnail_private.erase(og)) {
1300 m->part9.emplace_back(getObject(og)); 1301 m->part9.emplace_back(getObject(og));
@@ -1371,9 +1372,9 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1371,9 +1372,9 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1371 1372
1372 // Now compute the list of shared objects for each page after the first page. 1373 // Now compute the list of shared objects for each page after the first page.
1373 1374
1374 - for (size_t i = 1; i < toS(npages); ++i) { 1375 + for (size_t i = 1; i < npages; ++i) {
1375 CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i); 1376 CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i);
1376 - ObjUser ou(ObjUser::ou_page, toI(i)); 1377 + ObjUser ou(ObjUser::ou_page, i);
1377 if (!m->obj_user_to_objects.contains(ou)) { 1378 if (!m->obj_user_to_objects.contains(ou)) {
1378 stopOnError("found unreferenced page while calculating linearization data"); 1379 stopOnError("found unreferenced page while calculating linearization data");
1379 } 1380 }
@@ -1544,7 +1545,8 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O @@ -1544,7 +1545,8 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O
1544 phe.at(i).delta_page_length -= min_length; 1545 phe.at(i).delta_page_length -= min_length;
1545 phe.at(i).delta_content_length = phe.at(i).delta_page_length; 1546 phe.at(i).delta_content_length = phe.at(i).delta_page_length;
1546 1547
1547 - for (size_t j = 0; j < toS(cphe.at(i).nshared_objects); ++j) { 1548 + auto nso = toS(cphe.at(i).nshared_objects);
  1549 + for (size_t j = 0; j < nso; ++j) {
1548 phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j)); 1550 phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j));
1549 phe.at(i).shared_numerators.push_back(0); 1551 phe.at(i).shared_numerators.push_back(0);
1550 } 1552 }
libqpdf/QPDF_optimization.cc
@@ -15,7 +15,7 @@ QPDF::ObjUser::ObjUser(user_e type) : @@ -15,7 +15,7 @@ QPDF::ObjUser::ObjUser(user_e type) :
15 qpdf_assert_debug(type == ou_root); 15 qpdf_assert_debug(type == ou_root);
16 } 16 }
17 17
18 -QPDF::ObjUser::ObjUser(user_e type, int pageno) : 18 +QPDF::ObjUser::ObjUser(user_e type, size_t pageno) :
19 ou_type(type), 19 ou_type(type),
20 pageno(pageno) 20 pageno(pageno)
21 { 21 {
@@ -98,12 +98,10 @@ QPDF::optimize_internal( @@ -98,12 +98,10 @@ QPDF::optimize_internal(
98 pushInheritedAttributesToPage(allow_changes, false); 98 pushInheritedAttributesToPage(allow_changes, false);
99 99
100 // Traverse pages 100 // Traverse pages
101 - int n = toI(m->all_pages.size());  
102 - for (int pageno = 0; pageno < n; ++pageno) { 101 + size_t n = m->all_pages.size();
  102 + for (size_t pageno = 0; pageno < n; ++pageno) {
103 updateObjectMaps( 103 updateObjectMaps(
104 - ObjUser(ObjUser::ou_page, pageno),  
105 - m->all_pages.at(toS(pageno)),  
106 - skip_stream_parameters); 104 + ObjUser(ObjUser::ou_page, pageno), m->all_pages.at(pageno), skip_stream_parameters);
107 } 105 }
108 106
109 // Traverse document-level items 107 // Traverse document-level items
libqpdf/qpdf/QPDF_private.hh
@@ -356,7 +356,7 @@ struct QPDF::LinParameters @@ -356,7 +356,7 @@ struct QPDF::LinParameters
356 qpdf_offset_t file_size{0}; // /L 356 qpdf_offset_t file_size{0}; // /L
357 int first_page_object{0}; // /O 357 int first_page_object{0}; // /O
358 qpdf_offset_t first_page_end{0}; // /E 358 qpdf_offset_t first_page_end{0}; // /E
359 - int npages{0}; // /N 359 + size_t npages{0}; // /N
360 qpdf_offset_t xref_zero_offset{0}; // /T 360 qpdf_offset_t xref_zero_offset{0}; // /T
361 int first_page{0}; // /P 361 int first_page{0}; // /P
362 qpdf_offset_t H_offset{0}; // offset of primary hint stream 362 qpdf_offset_t H_offset{0}; // offset of primary hint stream
@@ -415,7 +415,7 @@ struct QPDF::CHSharedObject @@ -415,7 +415,7 @@ struct QPDF::CHSharedObject
415 class QPDF::ObjUser 415 class QPDF::ObjUser
416 { 416 {
417 public: 417 public:
418 - enum user_e {ou_page = 1, ou_thumb, ou_trailer_key, ou_root_key, ou_root }; 418 + enum user_e { ou_page = 1, ou_thumb, ou_trailer_key, ou_root_key, ou_root };
419 419
420 ObjUser() = delete; 420 ObjUser() = delete;
421 421
@@ -423,7 +423,7 @@ class QPDF::ObjUser @@ -423,7 +423,7 @@ class QPDF::ObjUser
423 ObjUser(user_e type); 423 ObjUser(user_e type);
424 424
425 // type must be one of ou_page or ou_thumb 425 // type must be one of ou_page or ou_thumb
426 - ObjUser(user_e type, int pageno); 426 + ObjUser(user_e type, size_t pageno);
427 427
428 // type must be one of ou_trailer_key or ou_root_key 428 // type must be one of ou_trailer_key or ou_root_key
429 ObjUser(user_e type, std::string const& key); 429 ObjUser(user_e type, std::string const& key);
@@ -431,8 +431,8 @@ class QPDF::ObjUser @@ -431,8 +431,8 @@ class QPDF::ObjUser
431 bool operator<(ObjUser const&) const; 431 bool operator<(ObjUser const&) const;
432 432
433 user_e ou_type; 433 user_e ou_type;
434 - int pageno{0}; // if ou_page;  
435 - std::string key; // if ou_trailer_key or ou_root_key 434 + size_t pageno{0}; // if ou_page;
  435 + std::string key; // if ou_trailer_key or ou_root_key
436 }; 436 };
437 437
438 struct QPDF::UpdateObjectMapsFrame 438 struct QPDF::UpdateObjectMapsFrame