Commit b1647555f8c3658414d524a5444bad5f85ed6c33

Authored by m-holger
Committed by GitHub
2 parents 7eda035b e8ade683

Merge pull request #1507 from m-holger/ouser

Refactor QPDF::ObjUser
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"
@@ -1104,10 +1105,6 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1104,10 +1105,6 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1104 case ObjUser::ou_root: 1105 case ObjUser::ou_root:
1105 is_root = true; 1106 is_root = true;
1106 break; 1107 break;
1107 -  
1108 - case ObjUser::ou_bad:  
1109 - stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: invalid user type");  
1110 - break;  
1111 } 1108 }
1112 } 1109 }
1113 1110
@@ -1153,7 +1150,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1153,7 +1150,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1153 pages.push_back(getUncompressedObject(oh, object_stream_data)); 1150 pages.push_back(getUncompressedObject(oh, object_stream_data));
1154 } 1151 }
1155 } 1152 }
1156 - int npages = toI(pages.size()); 1153 + size_t npages = pages.size();
1157 1154
1158 // 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
1159 // 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
@@ -1164,7 +1161,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1164,7 +1161,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1164 // 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
1165 // pages tree, and as such is a reasonable size. 1162 // pages tree, and as such is a reasonable size.
1166 m->c_linp.npages = npages; 1163 m->c_linp.npages = npages;
1167 - m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(toS(npages)); 1164 + m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(npages);
1168 1165
1169 // 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.
1170 1167
@@ -1220,7 +1217,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1220,7 +1217,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1220 // Part 7: other pages' private objects 1217 // Part 7: other pages' private objects
1221 1218
1222 // For each page in order: 1219 // For each page in order:
1223 - for (size_t i = 1; i < toS(npages); ++i) { 1220 + for (size_t i = 1; i < npages; ++i) {
1224 // Place this page's page object 1221 // Place this page's page object
1225 1222
1226 QPDFObjGen page_og(pages.at(i).getObjGen()); 1223 QPDFObjGen page_og(pages.at(i).getObjGen());
@@ -1237,7 +1234,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1237,7 +1234,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1237 1234
1238 m->c_page_offset_data.entries.at(i).nobjects = 1; 1235 m->c_page_offset_data.entries.at(i).nobjects = 1;
1239 1236
1240 - ObjUser ou(ObjUser::ou_page, toI(i)); 1237 + ObjUser ou(ObjUser::ou_page, i);
1241 if (!m->obj_user_to_objects.contains(ou)) { 1238 if (!m->obj_user_to_objects.contains(ou)) {
1242 stopOnError("found unreferenced page while calculating linearization data"); 1239 stopOnError("found unreferenced page while calculating linearization data");
1243 } 1240 }
@@ -1285,7 +1282,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1285,7 +1282,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1285 1282
1286 // 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
1287 // we were going to bother with thumbnail hint tables. 1284 // we were going to bother with thumbnail hint tables.
1288 - for (size_t i = 0; i < toS(npages); ++i) { 1285 + for (size_t i = 0; i < npages; ++i) {
1289 QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb"); 1286 QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb");
1290 thumb = getUncompressedObject(thumb, object_stream_data); 1287 thumb = getUncompressedObject(thumb, object_stream_data);
1291 QPDFObjGen thumb_og(thumb.getObjGen()); 1288 QPDFObjGen thumb_og(thumb.getObjGen());
@@ -1298,7 +1295,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1298,7 +1295,7 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1298 // 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
1299 // lc_thumbnail_private. 1296 // lc_thumbnail_private.
1300 } 1297 }
1301 - 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)];
1302 for (auto const& og: ogs) { 1299 for (auto const& og: ogs) {
1303 if (lc_thumbnail_private.erase(og)) { 1300 if (lc_thumbnail_private.erase(og)) {
1304 m->part9.emplace_back(getObject(og)); 1301 m->part9.emplace_back(getObject(og));
@@ -1375,9 +1372,9 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data) @@ -1375,9 +1372,9 @@ QPDF::calculateLinearizationData(T const&amp; object_stream_data)
1375 1372
1376 // 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.
1377 1374
1378 - for (size_t i = 1; i < toS(npages); ++i) { 1375 + for (size_t i = 1; i < npages; ++i) {
1379 CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i); 1376 CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i);
1380 - ObjUser ou(ObjUser::ou_page, toI(i)); 1377 + ObjUser ou(ObjUser::ou_page, i);
1381 if (!m->obj_user_to_objects.contains(ou)) { 1378 if (!m->obj_user_to_objects.contains(ou)) {
1382 stopOnError("found unreferenced page while calculating linearization data"); 1379 stopOnError("found unreferenced page while calculating linearization data");
1383 } 1380 }
@@ -1548,7 +1545,8 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O @@ -1548,7 +1545,8 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O
1548 phe.at(i).delta_page_length -= min_length; 1545 phe.at(i).delta_page_length -= min_length;
1549 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;
1550 1547
1551 - 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) {
1552 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));
1553 phe.at(i).shared_numerators.push_back(0); 1551 phe.at(i).shared_numerators.push_back(0);
1554 } 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,24 +415,24 @@ struct QPDF::CHSharedObject @@ -415,24 +415,24 @@ struct QPDF::CHSharedObject
415 class QPDF::ObjUser 415 class QPDF::ObjUser
416 { 416 {
417 public: 417 public:
418 - enum user_e { ou_bad, ou_page, 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() = default; 420 + ObjUser() = delete;
421 421
422 // type must be ou_root 422 // type must be ou_root
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);
430 430
431 bool operator<(ObjUser const&) const; 431 bool operator<(ObjUser const&) const;
432 432
433 - user_e ou_type{ou_bad};  
434 - int pageno{0}; // if ou_page;  
435 - std::string key; // if ou_trailer_key or ou_root_key 433 + user_e ou_type;
  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