Commit 7d7a7d087c82937f8b42229a4dd2d095f5bf2824
1 parent
5d3d583b
Refactor `NNTreeImpl`: replace `QPDFObjectHandle oh` with `qpdf::Dictionary tree_root`.
Showing
2 changed files
with
25 additions
and
25 deletions
libqpdf/NNTree.cc
| @@ -99,7 +99,7 @@ void | @@ -99,7 +99,7 @@ void | ||
| 99 | NNTreeIterator::increment(bool backward) | 99 | NNTreeIterator::increment(bool backward) |
| 100 | { | 100 | { |
| 101 | if (item_number < 0) { | 101 | if (item_number < 0) { |
| 102 | - deepen(impl.oh, !backward, true); | 102 | + deepen(impl.tree_root, !backward, true); |
| 103 | return; | 103 | return; |
| 104 | } | 104 | } |
| 105 | 105 | ||
| @@ -345,7 +345,7 @@ NNTreeIterator::insertAfter(QPDFObjectHandle const& key, QPDFObjectHandle const& | @@ -345,7 +345,7 @@ NNTreeIterator::insertAfter(QPDFObjectHandle const& key, QPDFObjectHandle const& | ||
| 345 | { | 345 | { |
| 346 | if (!valid()) { | 346 | if (!valid()) { |
| 347 | impl.insertFirst(key, value); | 347 | impl.insertFirst(key, value); |
| 348 | - deepen(impl.oh, true, false); | 348 | + deepen(impl.tree_root, true, false); |
| 349 | return; | 349 | return; |
| 350 | } | 350 | } |
| 351 | 351 | ||
| @@ -415,7 +415,7 @@ NNTreeIterator::remove() | @@ -415,7 +415,7 @@ NNTreeIterator::remove() | ||
| 415 | 415 | ||
| 416 | if (path.empty()) { | 416 | if (path.empty()) { |
| 417 | // Special case: if this is the root node, we can leave it empty. | 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 | return; | 419 | return; |
| 420 | } | 420 | } |
| 421 | 421 | ||
| @@ -454,7 +454,7 @@ NNTreeIterator::remove() | @@ -454,7 +454,7 @@ NNTreeIterator::remove() | ||
| 454 | element->node.removeKey("/Kids"); | 454 | element->node.removeKey("/Kids"); |
| 455 | element->node.replaceKey(impl.itemsKey(), Array::empty()); | 455 | element->node.replaceKey(impl.itemsKey(), Array::empty()); |
| 456 | path.clear(); | 456 | path.clear(); |
| 457 | - setItemNumber(impl.oh, -1); | 457 | + setItemNumber(impl.tree_root, -1); |
| 458 | return; | 458 | return; |
| 459 | } | 459 | } |
| 460 | 460 | ||
| @@ -559,7 +559,7 @@ NNTreeImpl::iterator | @@ -559,7 +559,7 @@ NNTreeImpl::iterator | ||
| 559 | NNTreeImpl::begin() | 559 | NNTreeImpl::begin() |
| 560 | { | 560 | { |
| 561 | iterator result(*this); | 561 | iterator result(*this); |
| 562 | - result.deepen(oh, true, true); | 562 | + result.deepen(tree_root, true, true); |
| 563 | return result; | 563 | return result; |
| 564 | } | 564 | } |
| 565 | 565 | ||
| @@ -567,7 +567,7 @@ NNTreeImpl::iterator | @@ -567,7 +567,7 @@ NNTreeImpl::iterator | ||
| 567 | NNTreeImpl::last() | 567 | NNTreeImpl::last() |
| 568 | { | 568 | { |
| 569 | iterator result(*this); | 569 | iterator result(*this); |
| 570 | - result.deepen(oh, false, true); | 570 | + result.deepen(tree_root, false, true); |
| 571 | return result; | 571 | return result; |
| 572 | } | 572 | } |
| 573 | 573 | ||
| @@ -623,7 +623,7 @@ int | @@ -623,7 +623,7 @@ int | ||
| 623 | NNTreeImpl::compareKeyItem(QPDFObjectHandle const& key, Array const& items, int idx) const | 623 | NNTreeImpl::compareKeyItem(QPDFObjectHandle const& key, Array const& items, int idx) const |
| 624 | { | 624 | { |
| 625 | if (!keyValid(items[2 * idx])) { | 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 | return compareKeys(key, items[2 * idx]); | 628 | return compareKeys(key, items[2 * idx]); |
| 629 | } | 629 | } |
| @@ -633,7 +633,7 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle const& key, Array const& kids, int id | @@ -633,7 +633,7 @@ NNTreeImpl::compareKeyKid(QPDFObjectHandle const& key, Array const& kids, int id | ||
| 633 | { | 633 | { |
| 634 | Dictionary kid = kids[idx]; | 634 | Dictionary kid = kids[idx]; |
| 635 | if (!kid) { | 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 | Array limits = kid["/Limits"]; | 638 | Array limits = kid["/Limits"]; |
| 639 | if (!(keyValid(limits[0]) && keyValid(limits[1]))) { | 639 | if (!(keyValid(limits[0]) && keyValid(limits[1]))) { |
| @@ -658,8 +658,8 @@ NNTreeImpl::repair() | @@ -658,8 +658,8 @@ NNTreeImpl::repair() | ||
| 658 | repl.insert(key, value); | 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 | NNTreeImpl::iterator | 665 | NNTreeImpl::iterator |
| @@ -669,7 +669,7 @@ NNTreeImpl::find(QPDFObjectHandle const& key, bool return_prev_if_not_found) | @@ -669,7 +669,7 @@ NNTreeImpl::find(QPDFObjectHandle const& key, bool return_prev_if_not_found) | ||
| 669 | return findInternal(key, return_prev_if_not_found); | 669 | return findInternal(key, return_prev_if_not_found); |
| 670 | } catch (QPDFExc& e) { | 670 | } catch (QPDFExc& e) { |
| 671 | if (auto_repair) { | 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 | repair(); | 673 | repair(); |
| 674 | return findInternal(key, return_prev_if_not_found); | 674 | return findInternal(key, return_prev_if_not_found); |
| 675 | } else { | 675 | } else { |
| @@ -686,10 +686,10 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | @@ -686,10 +686,10 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | ||
| 686 | return end(); | 686 | return end(); |
| 687 | } | 687 | } |
| 688 | if (!keyValid(first_item->first)) { | 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 | if (!value_valid(first_item->second)) { | 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 | if (compareKeys(key, first_item->first) < 0) { | 694 | if (compareKeys(key, first_item->first) < 0) { |
| 695 | // Before the first key | 695 | // Before the first key |
| @@ -697,7 +697,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | @@ -697,7 +697,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | ||
| 697 | } | 697 | } |
| 698 | 698 | ||
| 699 | QPDFObjGen::set seen; | 699 | QPDFObjGen::set seen; |
| 700 | - auto node = oh; | 700 | + auto node = tree_root; |
| 701 | iterator result(*this); | 701 | iterator result(*this); |
| 702 | 702 | ||
| 703 | while (true) { | 703 | while (true) { |
| @@ -715,7 +715,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | @@ -715,7 +715,7 @@ NNTreeImpl::findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_fo | ||
| 715 | error(node, "encountered invalid key in find"); | 715 | error(node, "encountered invalid key in find"); |
| 716 | } | 716 | } |
| 717 | if (!result.impl.value_valid(result.ivalue.second)) { | 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 | return result; | 721 | return result; |
| @@ -741,13 +741,13 @@ NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& val | @@ -741,13 +741,13 @@ NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& val | ||
| 741 | auto iter = begin(); | 741 | auto iter = begin(); |
| 742 | Array items = iter.node[items_key]; | 742 | Array items = iter.node[items_key]; |
| 743 | if (!items) { | 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 | if (!(key && value)) { | 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 | if (!value_valid(value)) { | 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 | items.insert(0, key); | 752 | items.insert(0, key); |
| 753 | items.insert(1, value); | 753 | items.insert(1, value); |
| @@ -795,21 +795,21 @@ NNTreeImpl::validate(bool a_repair) | @@ -795,21 +795,21 @@ NNTreeImpl::validate(bool a_repair) | ||
| 795 | try { | 795 | try { |
| 796 | for (auto const& [key, value]: *this) { | 796 | for (auto const& [key, value]: *this) { |
| 797 | if (!keyValid(key)) { | 797 | if (!keyValid(key)) { |
| 798 | - error(oh, "invalid key in validate"); | 798 | + error(tree_root, "invalid key in validate"); |
| 799 | } | 799 | } |
| 800 | if (!value_valid(value)) { | 800 | if (!value_valid(value)) { |
| 801 | - error(oh, "invalid value in validate"); | 801 | + error(tree_root, "invalid value in validate"); |
| 802 | } | 802 | } |
| 803 | if (first) { | 803 | if (first) { |
| 804 | first = false; | 804 | first = false; |
| 805 | } else if (last_key && compareKeys(last_key, key) != -1) { | 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 | last_key = key; | 808 | last_key = key; |
| 809 | } | 809 | } |
| 810 | } catch (QPDFExc& e) { | 810 | } catch (QPDFExc& e) { |
| 811 | if (a_repair) { | 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 | repair(); | 813 | repair(); |
| 814 | } | 814 | } |
| 815 | return false; | 815 | return false; |
libqpdf/qpdf/NNTree.hh
| @@ -128,12 +128,12 @@ class NNTreeImpl final | @@ -128,12 +128,12 @@ class NNTreeImpl final | ||
| 128 | 128 | ||
| 129 | NNTreeImpl( | 129 | NNTreeImpl( |
| 130 | QPDF& qpdf, | 130 | QPDF& qpdf, |
| 131 | - QPDFObjectHandle oh, | 131 | + qpdf::Dictionary tree_root, |
| 132 | qpdf_object_type_e key_type, | 132 | qpdf_object_type_e key_type, |
| 133 | std::function<bool(QPDFObjectHandle const&)> value_validator, | 133 | std::function<bool(QPDFObjectHandle const&)> value_validator, |
| 134 | bool auto_repair) : | 134 | bool auto_repair) : |
| 135 | qpdf(qpdf), | 135 | qpdf(qpdf), |
| 136 | - oh(std::move(oh)), | 136 | + tree_root(std::move(tree_root)), |
| 137 | key_type(key_type), | 137 | key_type(key_type), |
| 138 | items_key(key_type == ::ot_string ? "/Names" : "/Nums"), | 138 | items_key(key_type == ::ot_string ? "/Names" : "/Nums"), |
| 139 | value_valid(value_validator), | 139 | value_valid(value_validator), |
| @@ -190,7 +190,7 @@ class NNTreeImpl final | @@ -190,7 +190,7 @@ class NNTreeImpl final | ||
| 190 | 190 | ||
| 191 | QPDF& qpdf; | 191 | QPDF& qpdf; |
| 192 | int split_threshold{32}; | 192 | int split_threshold{32}; |
| 193 | - QPDFObjectHandle oh; | 193 | + qpdf::Dictionary tree_root; |
| 194 | const qpdf_object_type_e key_type; | 194 | const qpdf_object_type_e key_type; |
| 195 | const std::string items_key; | 195 | const std::string items_key; |
| 196 | const std::function<bool(QPDFObjectHandle const&)> value_valid; | 196 | const std::function<bool(QPDFObjectHandle const&)> value_valid; |