Commit 965f0fcd6352348760e77004c8af5b9b44e74326

Authored by m-holger
Committed by m-holger
1 parent 98c14e77

Refactor QPDF::recoverStreamLength

libqpdf/QPDF.cc
@@ -1390,6 +1390,26 @@ QPDF::Xref_table::insert_free(QPDFObjGen og) @@ -1390,6 +1390,26 @@ QPDF::Xref_table::insert_free(QPDFObjGen og)
1390 } 1390 }
1391 } 1391 }
1392 1392
  1393 +QPDFObjGen
  1394 +QPDF::Xref_table::at_offset(qpdf_offset_t offset) const noexcept
  1395 +{
  1396 + int id = 0;
  1397 + int gen = 0;
  1398 + qpdf_offset_t start = 0;
  1399 +
  1400 + int i = 0;
  1401 + for (auto const& item: table) {
  1402 + auto o = item.offset();
  1403 + if (start < o && o <= offset) {
  1404 + start = o;
  1405 + id = i;
  1406 + gen = item.gen();
  1407 + }
  1408 + ++i;
  1409 + }
  1410 + return QPDFObjGen(id, gen);
  1411 +}
  1412 +
1393 void 1413 void
1394 QPDF::showXRefTable() 1414 QPDF::showXRefTable()
1395 { 1415 {
@@ -1690,21 +1710,9 @@ QPDF::recoverStreamLength( @@ -1690,21 +1710,9 @@ QPDF::recoverStreamLength(
1690 } 1710 }
1691 1711
1692 if (length) { 1712 if (length) {
1693 - auto end = stream_offset + toO(length);  
1694 - qpdf_offset_t found_offset = 0;  
1695 - QPDFObjGen found_og;  
1696 -  
1697 // Make sure this is inside this object 1713 // Make sure this is inside this object
1698 - for (auto const& [current_og, entry]: m->xref_table.as_map()) {  
1699 - if (entry.getType() == 1) {  
1700 - qpdf_offset_t obj_offset = entry.getOffset();  
1701 - if (found_offset < obj_offset && obj_offset < end) {  
1702 - found_offset = obj_offset;  
1703 - found_og = current_og;  
1704 - }  
1705 - }  
1706 - }  
1707 - if (!found_offset || found_og == og) { 1714 + auto found = m->xref_table.at_offset(stream_offset + toO(length));
  1715 + if (found == QPDFObjGen() || found == og) {
1708 // If we are trying to recover an XRef stream the xref table will not contain and 1716 // If we are trying to recover an XRef stream the xref table will not contain and
1709 // won't contain any entries, therefore we cannot check the found length. Otherwise we 1717 // won't contain any entries, therefore we cannot check the found length. Otherwise we
1710 // found endstream\nendobj within the space allowed for this object, so we're probably 1718 // found endstream\nendobj within the space allowed for this object, so we're probably
libqpdf/qpdf/QPDF_private.hh
@@ -87,6 +87,8 @@ class QPDF::Xref_table @@ -87,6 +87,8 @@ class QPDF::Xref_table
87 return table[static_cast<size_t>(id)].stream_index(); 87 return table[static_cast<size_t>(id)].stream_index();
88 } 88 }
89 89
  90 + QPDFObjGen at_offset(qpdf_offset_t offset) const noexcept;
  91 +
90 // Temporary access to underlying map 92 // Temporary access to underlying map
91 std::map<QPDFObjGen, QPDFXRefEntry> 93 std::map<QPDFObjGen, QPDFXRefEntry>
92 as_map() 94 as_map()