Commit 7d7a7d087c82937f8b42229a4dd2d095f5bf2824

Authored by m-holger
1 parent 5d3d583b

Refactor `NNTreeImpl`: replace `QPDFObjectHandle oh` with `qpdf::Dictionary tree_root`.

libqpdf/NNTree.cc
... ... @@ -99,7 +99,7 @@ void
99 99 NNTreeIterator::increment(bool backward)
100 100 {
101 101 if (item_number < 0) {
102   - deepen(impl.oh, !backward, true);
  102 + deepen(impl.tree_root, !backward, true);
103 103 return;
104 104 }
105 105  
... ... @@ -345,7 +345,7 @@ NNTreeIterator::insertAfter(QPDFObjectHandle const&amp; key, QPDFObjectHandle const&amp;
345 345 {
346 346 if (!valid()) {
347 347 impl.insertFirst(key, value);
348   - deepen(impl.oh, true, false);
  348 + deepen(impl.tree_root, true, false);
349 349 return;
350 350 }
351 351  
... ... @@ -415,7 +415,7 @@ NNTreeIterator::remove()
415 415  
416 416 if (path.empty()) {
417 417 // Special case: if this is the root node, we can leave it empty.
418   - setItemNumber(impl.oh, -1);
  418 + setItemNumber(impl.tree_root, -1);
419 419 return;
420 420 }
421 421  
... ... @@ -454,7 +454,7 @@ NNTreeIterator::remove()
454 454 element->node.removeKey("/Kids");
455 455 element->node.replaceKey(impl.itemsKey(), Array::empty());
456 456 path.clear();
457   - setItemNumber(impl.oh, -1);
  457 + setItemNumber(impl.tree_root, -1);
458 458 return;
459 459 }
460 460  
... ... @@ -559,7 +559,7 @@ NNTreeImpl::iterator
559 559 NNTreeImpl::begin()
560 560 {
561 561 iterator result(*this);
562   - result.deepen(oh, true, true);
  562 + result.deepen(tree_root, true, true);
563 563 return result;
564 564 }
565 565  
... ... @@ -567,7 +567,7 @@ NNTreeImpl::iterator
567 567 NNTreeImpl::last()
568 568 {
569 569 iterator result(*this);
570   - result.deepen(oh, false, true);
  570 + result.deepen(tree_root, false, true);
571 571 return result;
572 572 }
573 573  
... ... @@ -623,7 +623,7 @@ int
623 623 NNTreeImpl::compareKeyItem(QPDFObjectHandle const& key, Array const& items, int idx) const
624 624 {
625 625 if (!keyValid(items[2 * idx])) {
626   - error(oh, ("item at index " + std::to_string(2 * idx) + " is not the right type"));
  626 + error(tree_root, ("item at index " + std::to_string(2 * idx) + " is not the right type"));
627 627 }
628 628 return compareKeys(key, items[2 * idx]);
629 629 }
... ... @@ -633,7 +633,7 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle const&amp; key, Array const&amp; kids, int id
633 633 {
634 634 Dictionary kid = kids[idx];
635 635 if (!kid) {
636   - error(oh, "invalid kid at index " + std::to_string(idx));
  636 + error(tree_root, "invalid kid at index " + std::to_string(idx));
637 637 }
638 638 Array limits = kid["/Limits"];
639 639 if (!(keyValid(limits[0]) && keyValid(limits[1]))) {
... ... @@ -658,8 +658,8 @@ NNTreeImpl::repair()
658 658 repl.insert(key, value);
659 659 }
660 660 }
661   - oh.replaceKey("/Kids", new_node["/Kids"]);
662   - oh.replaceKey(itemsKey(), new_node[itemsKey()]);
  661 + tree_root.replaceKey("/Kids", new_node["/Kids"]);
  662 + tree_root.replaceKey(itemsKey(), new_node[itemsKey()]);
663 663 }
664 664  
665 665 NNTreeImpl::iterator
... ... @@ -669,7 +669,7 @@ NNTreeImpl::find(QPDFObjectHandle const&amp; key, bool return_prev_if_not_found)
669 669 return findInternal(key, return_prev_if_not_found);
670 670 } catch (QPDFExc& e) {
671 671 if (auto_repair) {
672   - warn(oh, std::string("attempting to repair after error: ") + e.what());
  672 + warn(tree_root, std::string("attempting to repair after error: ") + e.what());
673 673 repair();
674 674 return findInternal(key, return_prev_if_not_found);
675 675 } else {
... ... @@ -686,10 +686,10 @@ NNTreeImpl::findInternal(QPDFObjectHandle const&amp; key, bool return_prev_if_not_fo
686 686 return end();
687 687 }
688 688 if (!keyValid(first_item->first)) {
689   - error(oh, "encountered invalid key in find");
  689 + error(tree_root, "encountered invalid key in find");
690 690 }
691 691 if (!value_valid(first_item->second)) {
692   - error(oh, "encountered invalid value in find");
  692 + error(tree_root, "encountered invalid value in find");
693 693 }
694 694 if (compareKeys(key, first_item->first) < 0) {
695 695 // Before the first key
... ... @@ -697,7 +697,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const&amp; key, bool return_prev_if_not_fo
697 697 }
698 698  
699 699 QPDFObjGen::set seen;
700   - auto node = oh;
  700 + auto node = tree_root;
701 701 iterator result(*this);
702 702  
703 703 while (true) {
... ... @@ -715,7 +715,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const&amp; key, bool return_prev_if_not_fo
715 715 error(node, "encountered invalid key in find");
716 716 }
717 717 if (!result.impl.value_valid(result.ivalue.second)) {
718   - error(oh, "encountered invalid value in find");
  718 + error(tree_root, "encountered invalid value in find");
719 719 }
720 720 }
721 721 return result;
... ... @@ -741,13 +741,13 @@ NNTreeImpl::insertFirst(QPDFObjectHandle const&amp; key, QPDFObjectHandle const&amp; val
741 741 auto iter = begin();
742 742 Array items = iter.node[items_key];
743 743 if (!items) {
744   - error(oh, "unable to find a valid items node");
  744 + error(tree_root, "unable to find a valid items node");
745 745 }
746 746 if (!(key && value)) {
747   - error(oh, "unable to insert null key or value");
  747 + error(tree_root, "unable to insert null key or value");
748 748 }
749 749 if (!value_valid(value)) {
750   - error(oh, "attempting to insert an invalid value");
  750 + error(tree_root, "attempting to insert an invalid value");
751 751 }
752 752 items.insert(0, key);
753 753 items.insert(1, value);
... ... @@ -795,21 +795,21 @@ NNTreeImpl::validate(bool a_repair)
795 795 try {
796 796 for (auto const& [key, value]: *this) {
797 797 if (!keyValid(key)) {
798   - error(oh, "invalid key in validate");
  798 + error(tree_root, "invalid key in validate");
799 799 }
800 800 if (!value_valid(value)) {
801   - error(oh, "invalid value in validate");
  801 + error(tree_root, "invalid value in validate");
802 802 }
803 803 if (first) {
804 804 first = false;
805 805 } else if (last_key && compareKeys(last_key, key) != -1) {
806   - error(oh, "keys are not sorted in validate");
  806 + error(tree_root, "keys are not sorted in validate");
807 807 }
808 808 last_key = key;
809 809 }
810 810 } catch (QPDFExc& e) {
811 811 if (a_repair) {
812   - warn(oh, std::string("attempting to repair after error: ") + e.what());
  812 + warn(tree_root, std::string("attempting to repair after error: ") + e.what());
813 813 repair();
814 814 }
815 815 return false;
... ...
libqpdf/qpdf/NNTree.hh
... ... @@ -128,12 +128,12 @@ class NNTreeImpl final
128 128  
129 129 NNTreeImpl(
130 130 QPDF& qpdf,
131   - QPDFObjectHandle oh,
  131 + qpdf::Dictionary tree_root,
132 132 qpdf_object_type_e key_type,
133 133 std::function<bool(QPDFObjectHandle const&)> value_validator,
134 134 bool auto_repair) :
135 135 qpdf(qpdf),
136   - oh(std::move(oh)),
  136 + tree_root(std::move(tree_root)),
137 137 key_type(key_type),
138 138 items_key(key_type == ::ot_string ? "/Names" : "/Nums"),
139 139 value_valid(value_validator),
... ... @@ -190,7 +190,7 @@ class NNTreeImpl final
190 190  
191 191 QPDF& qpdf;
192 192 int split_threshold{32};
193   - QPDFObjectHandle oh;
  193 + qpdf::Dictionary tree_root;
194 194 const qpdf_object_type_e key_type;
195 195 const std::string items_key;
196 196 const std::function<bool(QPDFObjectHandle const&)> value_valid;
... ...