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::GetIndirectObjectIfValid( |
29 uint32_t objnum) { | |
30 CPDF_Object* pObj = GetIndirectObject(objnum); | |
31 if (pObj && pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) | |
33 return nullptr; | 32 return nullptr; |
34 | |
35 CPDF_Object* pObj = m_pParser->ParseIndirectObject(this, objnum); | |
36 if (!pObj) | |
37 return nullptr; | |
38 | |
39 pObj->m_ObjNum = objnum; | |
40 m_LastObjNum = std::max(m_LastObjNum, objnum); | |
41 if (m_IndirectObjs[objnum]) | |
42 m_IndirectObjs[objnum]->Destroy(); | |
43 | |
44 m_IndirectObjs[objnum] = pObj; | |
45 return pObj; | 33 return pObj; |
46 } | 34 } |
47 | 35 |
48 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { | 36 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { |
49 if (pObj->m_ObjNum) | 37 if (pObj->m_ObjNum) |
50 return pObj->m_ObjNum; | 38 return pObj->m_ObjNum; |
51 | 39 |
52 m_LastObjNum++; | 40 m_LastObjNum++; |
53 m_IndirectObjs[m_LastObjNum] = pObj; | |
54 pObj->m_ObjNum = m_LastObjNum; | 41 pObj->m_ObjNum = m_LastObjNum; |
42 ReplaceIndirectObject(pObj); | |
55 return m_LastObjNum; | 43 return m_LastObjNum; |
56 } | 44 } |
57 | 45 |
58 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { | 46 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { |
59 auto it = m_IndirectObjs.find(objnum); | 47 CPDF_Object* pObj = GetIndirectObjectIfValid(objnum); |
Tom Sepez
2016/08/17 17:04:13
Might this then cause a parse that didn't happen b
dsinclair
2016/08/17 18:16:44
Good catch. Fixed.
| |
60 if (it == m_IndirectObjs.end() || | 48 if (!pObj) |
61 it->second->GetObjNum() == CPDF_Object::kInvalidObjNum) { | |
62 return; | 49 return; |
63 } | 50 |
64 it->second->Destroy(); | 51 pObj->Destroy(); |
65 m_IndirectObjs.erase(it); | 52 m_IndirectObjs.erase(objnum); |
66 } | 53 } |
67 | 54 |
68 bool CPDF_IndirectObjectHolder::InsertIndirectObject(uint32_t objnum, | 55 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( |
69 CPDF_Object* pObj) { | 56 uint32_t objnum, |
57 CPDF_Object* pObj) { | |
70 if (!objnum || !pObj) | 58 if (!objnum || !pObj) |
71 return false; | 59 return false; |
72 | 60 |
73 auto it = m_IndirectObjs.find(objnum); | 61 CPDF_Object* pOldObj = GetIndirectObject(objnum); |
74 if (it != m_IndirectObjs.end()) { | 62 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { |
75 if (pObj->GetGenNum() <= it->second->GetGenNum()) { | 63 pObj->Destroy(); |
76 pObj->Destroy(); | 64 return false; |
77 return false; | |
78 } | |
79 it->second->Destroy(); | |
80 } | 65 } |
66 | |
81 pObj->m_ObjNum = objnum; | 67 pObj->m_ObjNum = objnum; |
82 m_IndirectObjs[objnum] = pObj; | 68 ReplaceIndirectObject(pObj); |
83 m_LastObjNum = std::max(m_LastObjNum, objnum); | |
84 return true; | 69 return true; |
85 } | 70 } |
71 | |
72 void CPDF_IndirectObjectHolder::ReplaceIndirectObject(CPDF_Object* pObj) { | |
73 m_LastObjNum = std::max(m_LastObjNum, pObj->m_ObjNum); | |
74 if (m_IndirectObjs[pObj->m_ObjNum]) | |
75 m_IndirectObjs[pObj->m_ObjNum]->Destroy(); | |
76 | |
77 m_IndirectObjs[pObj->m_ObjNum] = pObj; | |
78 } | |
OLD | NEW |