From 03bd7c78ff0411f0db033b5e6b5bf00c7fe2fb87 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Tue, 23 Aug 2016 20:13:41 -0700 Subject: IndirectObjectHolder API updates This Cl updates the names of the methods in the indirect object holder to better reflect their usage. The m_LastObjNum is made private and a setter added. Review-Url: https://codereview.chromium.org/2275593002 --- core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 4 ++-- core/fpdfapi/fpdf_parser/cfdf_document.cpp | 2 +- core/fpdfapi/fpdf_parser/cpdf_array_unittest.cpp | 2 +- core/fpdfapi/fpdf_parser/cpdf_data_avail.cpp | 6 +++-- core/fpdfapi/fpdf_parser/cpdf_document.cpp | 10 ++++---- .../fpdf_parser/cpdf_indirect_object_holder.cpp | 28 ++++++++++++---------- core/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp | 5 ++-- core/fpdfapi/fpdf_parser/cpdf_parser.cpp | 9 ++++--- core/fpdfapi/fpdf_parser/cpdf_reference.cpp | 3 ++- .../include/cpdf_indirect_object_holder.h | 15 ++++++++---- 10 files changed, 49 insertions(+), 35 deletions(-) diff --git a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index c20a483764..9ad1d060cc 100644 --- a/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -1239,7 +1239,7 @@ int32_t CPDF_Creator::WriteOldIndirectObject(uint32_t objnum) { bool bObjStm = (object_type == 2) && m_pEncryptDict && !m_pXRefStream; if (m_pParser->IsVersionUpdated() || m_bSecurityChanged || bExistInMap || bObjStm) { - CPDF_Object* pObj = m_pDocument->GetIndirectObject(objnum); + CPDF_Object* pObj = m_pDocument->GetOrParseIndirectObject(objnum); if (!pObj) { m_ObjectOffset[objnum] = 0; return 0; @@ -1703,7 +1703,7 @@ int32_t CPDF_Creator::WriteDoc_Stage4(IFX_Pause* pPause) { return -1; } } else { - if (m_File.AppendDWord(m_pDocument->m_LastObjNum + 1) < 0) { + if (m_File.AppendDWord(m_pDocument->GetLastObjNum() + 1) < 0) { return -1; } if (m_File.AppendString(" 0 obj <<") < 0) { diff --git a/core/fpdfapi/fpdf_parser/cfdf_document.cpp b/core/fpdfapi/fpdf_parser/cfdf_document.cpp index c039871401..c268503715 100644 --- a/core/fpdfapi/fpdf_parser/cfdf_document.cpp +++ b/core/fpdfapi/fpdf_parser/cfdf_document.cpp @@ -65,7 +65,7 @@ void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { if (!pObj) break; - InsertIndirectObject(objnum, pObj); + ReplaceIndirectObjectIfHigherGeneration(objnum, pObj); word = parser.GetNextWord(nullptr); if (word != "endobj") break; diff --git a/core/fpdfapi/fpdf_parser/cpdf_array_unittest.cpp b/core/fpdfapi/fpdf_parser/cpdf_array_unittest.cpp index 12a36d921a..efad3c38f6 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_array_unittest.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_array_unittest.cpp @@ -116,7 +116,7 @@ TEST(cpdf_array, Clone) { CPDF_Number* obj = new CPDF_Number(elems[i][j]); // Starts object number from 1. int obj_num = i * kNumOfRowElems + j + 1; - obj_holder->InsertIndirectObject(obj_num, obj); + obj_holder->ReplaceIndirectObjectIfHigherGeneration(obj_num, obj); arr_elem->InsertAt(j, new CPDF_Reference(obj_holder.get(), obj_num), obj_holder.get()); } diff --git a/core/fpdfapi/fpdf_parser/cpdf_data_avail.cpp b/core/fpdfapi/fpdf_parser/cpdf_data_avail.cpp index 94795fd60e..58b69257ad 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_data_avail.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_data_avail.cpp @@ -176,7 +176,7 @@ FX_BOOL CPDF_DataAvail::IsObjectsAvail( } else if (!pdfium::ContainsKey(m_ObjectSet, dwNum)) { m_ObjectSet.insert(dwNum); CPDF_Object* pReferred = - m_pDocument->GetIndirectObject(pRef->GetRefObjNum()); + m_pDocument->GetOrParseIndirectObject(pRef->GetRefObjNum()); if (pReferred) new_obj_array.Add(pReferred); } @@ -1788,8 +1788,10 @@ CPDF_Dictionary* CPDF_DataAvail::GetPage(int index) { if (!pPageDict) return nullptr; - if (!m_pDocument->InsertIndirectObject(dwObjNum, pPageDict)) + if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration(dwObjNum, + pPageDict)) { return nullptr; + } return pPageDict->GetDict(); } } diff --git a/core/fpdfapi/fpdf_parser/cpdf_document.cpp b/core/fpdfapi/fpdf_parser/cpdf_document.cpp index 8560660703..a05a24e07f 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_document.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_document.cpp @@ -499,9 +499,9 @@ CPDF_Document::~CPDF_Document() { } void CPDF_Document::LoadDocInternal() { - m_LastObjNum = m_pParser->GetLastObjNum(); + SetLastObjNum(m_pParser->GetLastObjNum()); - CPDF_Object* pRootObj = GetIndirectObject(m_pParser->GetRootObjNum()); + CPDF_Object* pRootObj = GetOrParseIndirectObject(m_pParser->GetRootObjNum()); if (!pRootObj) return; @@ -509,7 +509,7 @@ void CPDF_Document::LoadDocInternal() { if (!m_pRootDict) return; - CPDF_Object* pInfoObj = GetIndirectObject(m_pParser->GetInfoObjNum()); + CPDF_Object* pInfoObj = GetOrParseIndirectObject(m_pParser->GetInfoObjNum()); if (pInfoObj) m_pInfoDict = pInfoObj->GetDict(); if (CPDF_Array* pIDArray = m_pParser->GetIDArray()) { @@ -589,14 +589,14 @@ CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { if (m_bLinearized && (iPage == m_iFirstPageNo)) { if (CPDF_Dictionary* pDict = - ToDictionary(GetIndirectObject(m_dwFirstPageObjNum))) { + ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { return pDict; } } int objnum = m_PageList.GetAt(iPage); if (objnum) { - if (CPDF_Dictionary* pDict = ToDictionary(GetIndirectObject(objnum))) + if (CPDF_Dictionary* pDict = ToDictionary(GetOrParseIndirectObject(objnum))) return pDict; } diff --git a/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp b/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp index 33f45a53bf..bb29da5052 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp @@ -20,7 +20,8 @@ CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { pair.second->Destroy(); } -CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject(uint32_t objnum) { +CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( + uint32_t objnum) { if (objnum == 0) return nullptr; @@ -55,18 +56,9 @@ uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { return m_LastObjNum; } -void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { - auto it = m_IndirectObjs.find(objnum); - if (it == m_IndirectObjs.end() || - it->second->GetObjNum() == CPDF_Object::kInvalidObjNum) { - return; - } - it->second->Destroy(); - m_IndirectObjs.erase(it); -} - -bool CPDF_IndirectObjectHolder::InsertIndirectObject(uint32_t objnum, - CPDF_Object* pObj) { +bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( + uint32_t objnum, + CPDF_Object* pObj) { if (!objnum || !pObj) return false; @@ -83,3 +75,13 @@ bool CPDF_IndirectObjectHolder::InsertIndirectObject(uint32_t objnum, m_LastObjNum = std::max(m_LastObjNum, objnum); return true; } + +void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { + auto it = m_IndirectObjs.find(objnum); + if (it == m_IndirectObjs.end() || + it->second->GetObjNum() == CPDF_Object::kInvalidObjNum) { + return; + } + it->second->Destroy(); + m_IndirectObjs.erase(it); +} diff --git a/core/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp b/core/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp index a77f022c40..52b01fe3b4 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp @@ -725,13 +725,14 @@ TEST(PDFArrayTest, AddReferenceAndGetObjectAt) { // Create two arrays of references by different AddReference() APIs. for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) { // All the indirect objects inserted will be owned by holder. - holder->InsertIndirectObject(obj_nums[i], indirect_objs[i]); + holder->ReplaceIndirectObjectIfHigherGeneration(obj_nums[i], + indirect_objs[i]); arr->AddReference(holder.get(), obj_nums[i]); arr1->AddReference(holder.get(), indirect_objs[i]); } // Check indirect objects. for (size_t i = 0; i < FX_ArraySize(obj_nums); ++i) - EXPECT_EQ(indirect_objs[i], holder->GetIndirectObject(obj_nums[i])); + EXPECT_EQ(indirect_objs[i], holder->GetOrParseIndirectObject(obj_nums[i])); // Check arrays. EXPECT_EQ(arr->GetCount(), arr1->GetCount()); for (size_t i = 0; i < arr->GetCount(); ++i) { diff --git a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp index b2f1a4be3f..26adf7bb13 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp @@ -267,7 +267,7 @@ CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { if (CPDF_Dictionary* pEncryptDict = pEncryptObj->AsDictionary()) { SetEncryptDictionary(pEncryptDict); } else if (CPDF_Reference* pRef = pEncryptObj->AsReference()) { - pEncryptObj = m_pDocument->GetIndirectObject(pRef->GetRefObjNum()); + pEncryptObj = m_pDocument->GetOrParseIndirectObject(pRef->GetRefObjNum()); if (pEncryptObj) SetEncryptDictionary(pEncryptObj->GetDict()); } @@ -986,8 +986,10 @@ FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE* pos, FX_BOOL bMainXRef) { pObject->Release(); return FALSE; } - if (!m_pDocument->InsertIndirectObject(pObject->m_ObjNum, pObject)) + if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration(pObject->m_ObjNum, + pObject)) { return FALSE; + } } CPDF_Stream* pStream = pObject->AsStream(); @@ -1230,7 +1232,8 @@ CPDF_StreamAcc* CPDF_Parser::GetObjectStream(uint32_t objnum) { if (!m_pDocument) return nullptr; - const CPDF_Stream* pStream = ToStream(m_pDocument->GetIndirectObject(objnum)); + const CPDF_Stream* pStream = + ToStream(m_pDocument->GetOrParseIndirectObject(objnum)); if (!pStream) return nullptr; diff --git a/core/fpdfapi/fpdf_parser/cpdf_reference.cpp b/core/fpdfapi/fpdf_parser/cpdf_reference.cpp index 007423e5fb..afda50c324 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_reference.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_reference.cpp @@ -63,5 +63,6 @@ void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum) { } CPDF_Object* CPDF_Reference::GetDirect() const { - return m_pObjList ? m_pObjList->GetIndirectObject(m_RefObjNum) : nullptr; + return m_pObjList ? m_pObjList->GetOrParseIndirectObject(m_RefObjNum) + : nullptr; } diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h b/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h index c29746fdd7..31e0aba5ea 100644 --- a/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h @@ -22,14 +22,17 @@ class CPDF_IndirectObjectHolder { explicit CPDF_IndirectObjectHolder(CPDF_Parser* pParser); ~CPDF_IndirectObjectHolder(); - CPDF_Object* GetIndirectObject(uint32_t objnum); - uint32_t AddIndirectObject(CPDF_Object* pObj); + CPDF_Object* GetOrParseIndirectObject(uint32_t objnum); void ReleaseIndirectObject(uint32_t objnum); - // Takes ownership of |pObj|. - bool InsertIndirectObject(uint32_t objnum, CPDF_Object* pObj); + // Take ownership of |pObj|. + uint32_t AddIndirectObject(CPDF_Object* pObj); + bool ReplaceIndirectObjectIfHigherGeneration(uint32_t objnum, + CPDF_Object* pObj); uint32_t GetLastObjNum() const { return m_LastObjNum; } + void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } + iterator begin() { return m_IndirectObjs.begin(); } const_iterator begin() const { return m_IndirectObjs.begin(); } iterator end() { return m_IndirectObjs.end(); } @@ -37,8 +40,10 @@ class CPDF_IndirectObjectHolder { protected: CPDF_Parser* m_pParser; - uint32_t m_LastObjNum; std::map m_IndirectObjs; + + private: + uint32_t m_LastObjNum; }; #endif // CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_INDIRECT_OBJECT_HOLDER_H_ -- cgit v1.2.3