| 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() : m_LastObjNum(0) {} | 12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} |
| 13 | 13 |
| 14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() {} | 14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { |
| 15 for (const auto& pair : m_IndirectObjs) |
| 16 delete pair.second; |
| 17 } |
| 15 | 18 |
| 16 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( | 19 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( |
| 17 uint32_t objnum) const { | 20 uint32_t objnum) const { |
| 18 auto it = m_IndirectObjs.find(objnum); | 21 auto it = m_IndirectObjs.find(objnum); |
| 19 return it != m_IndirectObjs.end() ? it->second.get() : nullptr; | 22 return it != m_IndirectObjs.end() ? it->second : nullptr; |
| 20 } | 23 } |
| 21 | 24 |
| 22 CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( | 25 CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( |
| 23 uint32_t objnum) { | 26 uint32_t objnum) { |
| 24 if (objnum == 0) | 27 if (objnum == 0) |
| 25 return nullptr; | 28 return nullptr; |
| 26 | 29 |
| 27 CPDF_Object* pObj = GetIndirectObject(objnum); | 30 CPDF_Object* pObj = GetIndirectObject(objnum); |
| 28 if (pObj) | 31 if (pObj) |
| 29 return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; | 32 return pObj->GetObjNum() != CPDF_Object::kInvalidObjNum ? pObj : nullptr; |
| 30 | 33 |
| 31 pObj = ParseIndirectObject(objnum); | 34 pObj = ParseIndirectObject(objnum); |
| 32 if (!pObj) | 35 if (!pObj) |
| 33 return nullptr; | 36 return nullptr; |
| 34 | 37 |
| 35 pObj->m_ObjNum = objnum; | 38 pObj->m_ObjNum = objnum; |
| 36 m_LastObjNum = std::max(m_LastObjNum, objnum); | 39 m_LastObjNum = std::max(m_LastObjNum, objnum); |
| 37 m_IndirectObjs[objnum].reset(pObj); | 40 if (m_IndirectObjs[objnum]) |
| 41 delete m_IndirectObjs[objnum]; |
| 42 |
| 43 m_IndirectObjs[objnum] = pObj; |
| 38 return pObj; | 44 return pObj; |
| 39 } | 45 } |
| 40 | 46 |
| 41 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { | 47 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { |
| 42 return nullptr; | 48 return nullptr; |
| 43 } | 49 } |
| 44 | 50 |
| 45 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { | 51 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { |
| 46 if (pObj->m_ObjNum) | 52 if (pObj->m_ObjNum) |
| 47 return pObj->m_ObjNum; | 53 return pObj->m_ObjNum; |
| 48 | 54 |
| 49 m_LastObjNum++; | 55 m_LastObjNum++; |
| 50 m_IndirectObjs[m_LastObjNum].reset(pObj); | 56 m_IndirectObjs[m_LastObjNum] = pObj; |
| 51 pObj->m_ObjNum = m_LastObjNum; | 57 pObj->m_ObjNum = m_LastObjNum; |
| 52 return m_LastObjNum; | 58 return m_LastObjNum; |
| 53 } | 59 } |
| 54 | 60 |
| 55 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( | 61 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( |
| 56 uint32_t objnum, | 62 uint32_t objnum, |
| 57 CPDF_Object* pObj) { | 63 CPDF_Object* pObj) { |
| 58 if (!objnum || !pObj) | 64 if (!objnum || !pObj) |
| 59 return false; | 65 return false; |
| 60 | 66 |
| 61 CPDF_Object* pOldObj = GetIndirectObject(objnum); | 67 CPDF_Object* pOldObj = GetIndirectObject(objnum); |
| 62 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { | 68 if (pOldObj) { |
| 63 delete pObj; | 69 if (pObj->GetGenNum() <= pOldObj->GetGenNum()) { |
| 64 return false; | 70 delete pObj; |
| 71 return false; |
| 72 } |
| 73 delete pOldObj; |
| 65 } | 74 } |
| 66 pObj->m_ObjNum = objnum; | 75 pObj->m_ObjNum = objnum; |
| 67 m_IndirectObjs[objnum].reset(pObj); | 76 m_IndirectObjs[objnum] = pObj; |
| 68 m_LastObjNum = std::max(m_LastObjNum, objnum); | 77 m_LastObjNum = std::max(m_LastObjNum, objnum); |
| 69 return true; | 78 return true; |
| 70 } | 79 } |
| 71 | 80 |
| 72 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { | 81 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { |
| 73 CPDF_Object* pObj = GetIndirectObject(objnum); | 82 CPDF_Object* pObj = GetIndirectObject(objnum); |
| 74 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) | 83 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) |
| 75 return; | 84 return; |
| 76 | 85 |
| 86 delete pObj; |
| 77 m_IndirectObjs.erase(objnum); | 87 m_IndirectObjs.erase(objnum); |
| 78 } | 88 } |
| OLD | NEW |