| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" | 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
| 11 | 11 |
| 12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder(CPDF_Parser* pParser) | 12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} |
| 13 : m_pParser(pParser), m_LastObjNum(0) { | |
| 14 if (pParser) | |
| 15 m_LastObjNum = m_pParser->GetLastObjNum(); | |
| 16 } | |
| 17 | 13 |
| 18 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { | 14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { |
| 19 for (const auto& pair : m_IndirectObjs) | 15 for (const auto& pair : m_IndirectObjs) |
| 20 pair.second->Destroy(); | 16 pair.second->Destroy(); |
| 21 } | 17 } |
| 22 | 18 |
| 23 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject(uint32_t objnum) { | 19 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( |
| 20 uint32_t objnum) const { |
| 24 if (objnum == 0) | 21 if (objnum == 0) |
| 25 return nullptr; | 22 return nullptr; |
| 26 | 23 |
| 27 auto it = m_IndirectObjs.find(objnum); | 24 auto it = m_IndirectObjs.find(objnum); |
| 28 if (it != m_IndirectObjs.end()) | 25 return it != m_IndirectObjs.end() ? it->second : nullptr; |
| 29 return it->second->GetObjNum() != CPDF_Object::kInvalidObjNum ? it->second | 26 } |
| 30 : nullptr; | |
| 31 | 27 |
| 32 if (!m_pParser) | 28 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { |
| 33 return nullptr; | 29 return nullptr; |
| 30 } |
| 34 | 31 |
| 35 CPDF_Object* pObj = m_pParser->ParseIndirectObject(this, objnum); | 32 CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( |
| 33 uint32_t objnum) { |
| 34 CPDF_Object* pObj = GetIndirectObject(objnum); |
| 35 if (pObj) |
| 36 return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; |
| 37 |
| 38 pObj = ParseIndirectObject(objnum); |
| 36 if (!pObj) | 39 if (!pObj) |
| 37 return nullptr; | 40 return nullptr; |
| 38 | 41 |
| 39 pObj->m_ObjNum = objnum; | 42 pObj->m_ObjNum = objnum; |
| 40 m_LastObjNum = std::max(m_LastObjNum, objnum); | 43 ReplaceIndirectObject(pObj); |
| 41 if (m_IndirectObjs[objnum]) | |
| 42 m_IndirectObjs[objnum]->Destroy(); | |
| 43 | |
| 44 m_IndirectObjs[objnum] = pObj; | |
| 45 return pObj; | 44 return pObj; |
| 46 } | 45 } |
| 47 | 46 |
| 48 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { | 47 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { |
| 49 if (pObj->m_ObjNum) | 48 if (pObj->m_ObjNum) |
| 50 return pObj->m_ObjNum; | 49 return pObj->m_ObjNum; |
| 51 | 50 |
| 52 m_LastObjNum++; | 51 m_LastObjNum++; |
| 53 m_IndirectObjs[m_LastObjNum] = pObj; | |
| 54 pObj->m_ObjNum = m_LastObjNum; | 52 pObj->m_ObjNum = m_LastObjNum; |
| 53 ReplaceIndirectObject(pObj); |
| 55 return m_LastObjNum; | 54 return m_LastObjNum; |
| 56 } | 55 } |
| 57 | 56 |
| 58 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { | 57 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { |
| 59 auto it = m_IndirectObjs.find(objnum); | 58 CPDF_Object* pObj = GetIndirectObject(objnum); |
| 60 if (it == m_IndirectObjs.end() || | 59 if (!pObj || pObj->m_ObjNum == CPDF_Object::kInvalidObjNum) |
| 61 it->second->GetObjNum() == CPDF_Object::kInvalidObjNum) { | |
| 62 return; | 60 return; |
| 63 } | 61 |
| 64 it->second->Destroy(); | 62 pObj->Destroy(); |
| 65 m_IndirectObjs.erase(it); | 63 m_IndirectObjs.erase(objnum); |
| 66 } | 64 } |
| 67 | 65 |
| 68 bool CPDF_IndirectObjectHolder::InsertIndirectObject(uint32_t objnum, | 66 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( |
| 69 CPDF_Object* pObj) { | 67 uint32_t objnum, |
| 68 CPDF_Object* pObj) { |
| 70 if (!objnum || !pObj) | 69 if (!objnum || !pObj) |
| 71 return false; | 70 return false; |
| 72 | 71 |
| 73 auto it = m_IndirectObjs.find(objnum); | 72 CPDF_Object* pOldObj = GetIndirectObject(objnum); |
| 74 if (it != m_IndirectObjs.end()) { | 73 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { |
| 75 if (pObj->GetGenNum() <= it->second->GetGenNum()) { | 74 pObj->Destroy(); |
| 76 pObj->Destroy(); | 75 return false; |
| 77 return false; | |
| 78 } | |
| 79 it->second->Destroy(); | |
| 80 } | 76 } |
| 77 |
| 81 pObj->m_ObjNum = objnum; | 78 pObj->m_ObjNum = objnum; |
| 82 m_IndirectObjs[objnum] = pObj; | 79 ReplaceIndirectObject(pObj); |
| 83 m_LastObjNum = std::max(m_LastObjNum, objnum); | |
| 84 return true; | 80 return true; |
| 85 } | 81 } |
| 82 |
| 83 void CPDF_IndirectObjectHolder::ReplaceIndirectObject(CPDF_Object* pObj) { |
| 84 m_LastObjNum = std::max(m_LastObjNum, pObj->m_ObjNum); |
| 85 if (m_IndirectObjs[pObj->m_ObjNum]) |
| 86 m_IndirectObjs[pObj->m_ObjNum]->Destroy(); |
| 87 |
| 88 m_IndirectObjs[pObj->m_ObjNum] = pObj; |
| 89 } |
| OLD | NEW |