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