diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index a63f050..73f0eef 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -19,6 +19,7 @@ #include #include #include +#include using namespace qpdf; using namespace std::literals; @@ -221,7 +222,7 @@ QPDF::readLinearizationData() // file_size initialized by isLinearized() m->linp.first_page_object = O.getIntValueAsInt(); m->linp.first_page_end = E.getIntValue(); - m->linp.npages = N.getIntValueAsInt(); + m->linp.npages = N.getUIntValueAsUInt(); m->linp.xref_zero_offset = T.getIntValue(); m->linp.first_page = first_page; m->linp.H_offset = H0_offset; @@ -333,7 +334,7 @@ QPDF::readHPageOffset(BitStream h) std::vector& entries = t.entries; entries.clear(); - int nitems = m->linp.npages; + int nitems = toI(m->linp.npages); load_vector_int(h, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects); load_vector_int( h, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length); @@ -423,13 +424,13 @@ QPDF::checkLinearizationInternal() } // N: number of pages - int npages = toI(pages.size()); - if (p.npages != npages) { + size_t npages = pages.size(); + if (std::cmp_not_equal(p.npages, npages)) { // Not tested in the test suite linearizationWarning("page count (/N) mismatch"); } - for (size_t i = 0; i < toS(npages); ++i) { + for (size_t i = 0; i < npages; ++i) { QPDFObjectHandle const& page = pages.at(i); QPDFObjGen og(page.getObjGen()); if (m->xref_table[og].getType() == 2) { @@ -620,7 +621,7 @@ QPDF::checkHPageOffset( // Empirically, it also seems that Acrobat sometimes puts items under a page's /Resources // dictionary in with shared objects even when they are private. - int npages = toI(pages.size()); + size_t npages = pages.size(); qpdf_offset_t table_offset = adjusted_offset(m->page_offset_hints.first_page_offset); QPDFObjGen first_page_og(pages.at(0).getObjGen()); if (!m->xref_table.contains(first_page_og)) { @@ -631,8 +632,8 @@ QPDF::checkHPageOffset( linearizationWarning("first page object offset mismatch"); } - for (int pageno = 0; pageno < npages; ++pageno) { - QPDFObjGen page_og(pages.at(toS(pageno)).getObjGen()); + for (size_t pageno = 0; pageno < npages; ++pageno) { + QPDFObjGen page_og(pages.at(pageno).getObjGen()); int first_object = page_og.getObj(); if (!m->xref_table.contains(page_og)) { stopOnError("unknown object in page offset hint table"); @@ -897,7 +898,7 @@ QPDF::dumpHPageOffset() << "nbits_shared_numerator: " << t.nbits_shared_numerator << "\n" << "shared_denominator: " << t.shared_denominator << "\n"; - for (size_t i1 = 0; i1 < toS(m->linp.npages); ++i1) { + for (size_t i1 = 0; i1 < m->linp.npages; ++i1) { HPageOffsetEntry& pe = t.entries.at(i1); *m->log->getInfo() << "Page " << i1 << ":\n" << " nobjects: " << pe.delta_nobjects + t.min_nobjects << "\n" @@ -1104,10 +1105,6 @@ QPDF::calculateLinearizationData(T const& object_stream_data) case ObjUser::ou_root: is_root = true; break; - - case ObjUser::ou_bad: - stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: invalid user type"); - break; } } @@ -1153,7 +1150,7 @@ QPDF::calculateLinearizationData(T const& object_stream_data) pages.push_back(getUncompressedObject(oh, object_stream_data)); } } - int npages = toI(pages.size()); + size_t npages = pages.size(); // We will be initializing some values of the computed hint tables. Specifically, we can // 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& object_stream_data) // npages is the size of the existing pages vector, which has been created by traversing the // pages tree, and as such is a reasonable size. m->c_linp.npages = npages; - m->c_page_offset_data.entries = std::vector(toS(npages)); + m->c_page_offset_data.entries = std::vector(npages); // Part 4: open document objects. We don't care about the order. @@ -1220,7 +1217,7 @@ QPDF::calculateLinearizationData(T const& object_stream_data) // Part 7: other pages' private objects // For each page in order: - for (size_t i = 1; i < toS(npages); ++i) { + for (size_t i = 1; i < npages; ++i) { // Place this page's page object QPDFObjGen page_og(pages.at(i).getObjGen()); @@ -1237,7 +1234,7 @@ QPDF::calculateLinearizationData(T const& object_stream_data) m->c_page_offset_data.entries.at(i).nobjects = 1; - ObjUser ou(ObjUser::ou_page, toI(i)); + ObjUser ou(ObjUser::ou_page, i); if (!m->obj_user_to_objects.contains(ou)) { stopOnError("found unreferenced page while calculating linearization data"); } @@ -1285,7 +1282,7 @@ QPDF::calculateLinearizationData(T const& object_stream_data) // Place private thumbnail images in page order. Slightly more information would be required if // we were going to bother with thumbnail hint tables. - for (size_t i = 0; i < toS(npages); ++i) { + for (size_t i = 0; i < npages; ++i) { QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb"); thumb = getUncompressedObject(thumb, object_stream_data); QPDFObjGen thumb_og(thumb.getObjGen()); @@ -1298,7 +1295,7 @@ QPDF::calculateLinearizationData(T const& object_stream_data) // there's nothing to prevent it from having been in some set other than // lc_thumbnail_private. } - std::set& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, toI(i))]; + std::set& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, i)]; for (auto const& og: ogs) { if (lc_thumbnail_private.erase(og)) { m->part9.emplace_back(getObject(og)); @@ -1375,9 +1372,9 @@ QPDF::calculateLinearizationData(T const& object_stream_data) // Now compute the list of shared objects for each page after the first page. - for (size_t i = 1; i < toS(npages); ++i) { + for (size_t i = 1; i < npages; ++i) { CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i); - ObjUser ou(ObjUser::ou_page, toI(i)); + ObjUser ou(ObjUser::ou_page, i); if (!m->obj_user_to_objects.contains(ou)) { stopOnError("found unreferenced page while calculating linearization data"); } @@ -1548,7 +1545,8 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::O phe.at(i).delta_page_length -= min_length; phe.at(i).delta_content_length = phe.at(i).delta_page_length; - for (size_t j = 0; j < toS(cphe.at(i).nshared_objects); ++j) { + auto nso = toS(cphe.at(i).nshared_objects); + for (size_t j = 0; j < nso; ++j) { phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j)); phe.at(i).shared_numerators.push_back(0); } diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index 19de4f4..e773737 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -15,7 +15,7 @@ QPDF::ObjUser::ObjUser(user_e type) : qpdf_assert_debug(type == ou_root); } -QPDF::ObjUser::ObjUser(user_e type, int pageno) : +QPDF::ObjUser::ObjUser(user_e type, size_t pageno) : ou_type(type), pageno(pageno) { @@ -98,12 +98,10 @@ QPDF::optimize_internal( pushInheritedAttributesToPage(allow_changes, false); // Traverse pages - int n = toI(m->all_pages.size()); - for (int pageno = 0; pageno < n; ++pageno) { + size_t n = m->all_pages.size(); + for (size_t pageno = 0; pageno < n; ++pageno) { updateObjectMaps( - ObjUser(ObjUser::ou_page, pageno), - m->all_pages.at(toS(pageno)), - skip_stream_parameters); + ObjUser(ObjUser::ou_page, pageno), m->all_pages.at(pageno), skip_stream_parameters); } // Traverse document-level items diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index 0750623..69b3110 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -356,7 +356,7 @@ struct QPDF::LinParameters qpdf_offset_t file_size{0}; // /L int first_page_object{0}; // /O qpdf_offset_t first_page_end{0}; // /E - int npages{0}; // /N + size_t npages{0}; // /N qpdf_offset_t xref_zero_offset{0}; // /T int first_page{0}; // /P qpdf_offset_t H_offset{0}; // offset of primary hint stream @@ -415,24 +415,24 @@ struct QPDF::CHSharedObject class QPDF::ObjUser { public: - enum user_e { ou_bad, ou_page, ou_thumb, ou_trailer_key, ou_root_key, ou_root }; + enum user_e { ou_page = 1, ou_thumb, ou_trailer_key, ou_root_key, ou_root }; - ObjUser() = default; + ObjUser() = delete; // type must be ou_root ObjUser(user_e type); // type must be one of ou_page or ou_thumb - ObjUser(user_e type, int pageno); + ObjUser(user_e type, size_t pageno); // type must be one of ou_trailer_key or ou_root_key ObjUser(user_e type, std::string const& key); bool operator<(ObjUser const&) const; - user_e ou_type{ou_bad}; - int pageno{0}; // if ou_page; - std::string key; // if ou_trailer_key or ou_root_key + user_e ou_type; + size_t pageno{0}; // if ou_page; + std::string key; // if ou_trailer_key or ou_root_key }; struct QPDF::UpdateObjectMapsFrame