From 381fc833ac9e6ea58762b7e7ac8b7f2129e8477f Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 10 Oct 2016 14:06:44 -0700 Subject: Add method to convert to an indirect object in a dictionary. Avoid an assert which previously could only be overcome by removing/re-inserting. Back-fill a unit test for the equivalent Array method. BUG=654387 Review-Url: https://codereview.chromium.org/2403143002 --- core/fpdfapi/parser/cpdf_dictionary.cpp | 11 ++++++++++ core/fpdfapi/parser/cpdf_dictionary.h | 3 +++ core/fpdfapi/parser/cpdf_object_unittest.cpp | 30 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) (limited to 'core') diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 7ef5a53551..54aafd43b1 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -192,6 +192,17 @@ void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) { m_Map.erase(it); } +void CPDF_Dictionary::ConvertToIndirectObjectFor( + const CFX_ByteString& key, + CPDF_IndirectObjectHolder* pHolder) { + auto it = m_Map.find(key); + if (it == m_Map.end() || it->second->IsReference()) + return; + + uint32_t objnum = pHolder->AddIndirectObject(it->second); + it->second = new CPDF_Reference(pHolder, objnum); +} + void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { auto it = m_Map.find(key); if (it == m_Map.end()) diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index 23f2e0e3ca..b56e40a6ef 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -70,6 +70,9 @@ class CPDF_Dictionary : public CPDF_Object { void SetMatrixFor(const CFX_ByteString& key, const CFX_Matrix& matrix); void SetBooleanFor(const CFX_ByteString& key, bool bValue); + void ConvertToIndirectObjectFor(const CFX_ByteString& key, + CPDF_IndirectObjectHolder* pHolder); + // Invalidates iterators for the element with the key |key|. void RemoveFor(const CFX_ByteString& key); diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp index b2177afd8a..502d2a06d2 100644 --- a/core/fpdfapi/parser/cpdf_object_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp @@ -766,6 +766,21 @@ TEST(PDFArrayTest, CloneDirectObject) { EXPECT_FALSE(cloned_obj); } +TEST(PDFArrayTest, ConvertIndirect) { + CPDF_IndirectObjectHolder objects_holder; + ScopedArray array(new CPDF_Array); + CPDF_Object* pObj = new CPDF_Number(42); + array->Add(pObj); + array->ConvertToIndirectObjectAt(0, &objects_holder); + CPDF_Object* pRef = array->GetObjectAt(0); + CPDF_Object* pNum = array->GetDirectObjectAt(0); + EXPECT_TRUE(pRef->IsReference()); + EXPECT_TRUE(pNum->IsNumber()); + EXPECT_NE(pObj, pRef); + EXPECT_EQ(pObj, pNum); + EXPECT_EQ(42, array->GetIntegerAt(0)); +} + TEST(PDFDictionaryTest, CloneDirectObject) { CPDF_IndirectObjectHolder objects_holder; ScopedDict dict(new CPDF_Dictionary()); @@ -833,3 +848,18 @@ TEST(PDFObjectTest, CloneCheckLoop) { EXPECT_EQ(nullptr, cloned_arr->AsArray()->GetObjectAt(0)); } } + +TEST(PDFDictionaryTest, ConvertIndirect) { + CPDF_IndirectObjectHolder objects_holder; + ScopedDict dict(new CPDF_Dictionary); + CPDF_Object* pObj = new CPDF_Number(42); + dict->SetFor("clams", pObj); + dict->ConvertToIndirectObjectFor("clams", &objects_holder); + CPDF_Object* pRef = dict->GetObjectFor("clams"); + CPDF_Object* pNum = dict->GetDirectObjectFor("clams"); + EXPECT_TRUE(pRef->IsReference()); + EXPECT_TRUE(pNum->IsNumber()); + EXPECT_NE(pObj, pRef); + EXPECT_EQ(pObj, pNum); + EXPECT_EQ(42, dict->GetIntegerFor("clams")); +} -- cgit v1.2.3