From 38afdcea7b08226aa6cc12a8fcda93a6f35a0a18 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 2 Apr 2020 17:52:21 -0400 Subject: Add QPDFObjectHandle::unsafeShallowCopy --- libqpdf/QPDFObjectHandle.cc | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index e7a6223f..4c19b377 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2460,6 +2460,23 @@ QPDFObjectHandle::hasObjectDescription() QPDFObjectHandle QPDFObjectHandle::shallowCopy() +{ + QPDFObjectHandle result; + shallowCopyInternal(result, false); + return result; +} + +QPDFObjectHandle +QPDFObjectHandle::unsafeShallowCopy() +{ + QPDFObjectHandle result; + shallowCopyInternal(result, true); + return result; +} + +void +QPDFObjectHandle::shallowCopyInternal(QPDFObjectHandle& new_obj, + bool first_level_only) { assertInitialized(); @@ -2470,7 +2487,6 @@ QPDFObjectHandle::shallowCopy() "attempt to make a shallow copy of a stream"); } - QPDFObjectHandle new_obj; if (isArray()) { QTC::TC("qpdf", "QPDFObjectHandle shallow copy array"); @@ -2491,13 +2507,12 @@ QPDFObjectHandle::shallowCopy() } std::set visited; - new_obj.copyObject(visited, false); - return new_obj; + new_obj.copyObject(visited, false, first_level_only); } void QPDFObjectHandle::copyObject(std::set& visited, - bool cross_indirect) + bool cross_indirect, bool first_level_only) { assertInitialized(); @@ -2573,9 +2588,11 @@ QPDFObjectHandle::copyObject(std::set& visited, for (int i = 0; i < n; ++i) { items.push_back(getArrayItem(i)); - if (cross_indirect || (! items.back().isIndirect())) + if ((! first_level_only) && + (cross_indirect || (! items.back().isIndirect()))) { - items.back().copyObject(visited, cross_indirect); + items.back().copyObject( + visited, cross_indirect, first_level_only); } } new_obj = new QPDF_Array(items); @@ -2589,9 +2606,11 @@ QPDFObjectHandle::copyObject(std::set& visited, iter != keys.end(); ++iter) { items[*iter] = getKey(*iter); - if (cross_indirect || (! items[*iter].isIndirect())) + if ((! first_level_only) && + (cross_indirect || (! items[*iter].isIndirect()))) { - items[*iter].copyObject(visited, cross_indirect); + items[*iter].copyObject( + visited, cross_indirect, first_level_only); } } new_obj = new QPDF_Dictionary(items); @@ -2614,7 +2633,7 @@ void QPDFObjectHandle::makeDirect() { std::set visited; - copyObject(visited, true); + copyObject(visited, true, false); } void -- cgit v1.2.3-70-g09d2