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/parser/cpdf_indirect_object_holder.h" | 7 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" |
8 | 8 |
9 #include "core/fpdfapi/parser/cpdf_array.h" | |
10 #include "core/fpdfapi/parser/cpdf_dictionary.h" | |
11 #include "core/fpdfapi/parser/cpdf_object.h" | 9 #include "core/fpdfapi/parser/cpdf_object.h" |
12 #include "core/fpdfapi/parser/cpdf_parser.h" | 10 #include "core/fpdfapi/parser/cpdf_parser.h" |
13 #include "core/fpdfapi/parser/cpdf_stream.h" | |
14 | 11 |
15 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} | 12 CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} |
16 | 13 |
17 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() {} | 14 CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() {} |
18 | 15 |
19 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( | 16 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( |
20 uint32_t objnum) const { | 17 uint32_t objnum) const { |
21 auto it = m_IndirectObjs.find(objnum); | 18 auto it = m_IndirectObjs.find(objnum); |
22 return it != m_IndirectObjs.end() ? it->second.get() : nullptr; | 19 return it != m_IndirectObjs.end() ? it->second.get() : nullptr; |
23 } | 20 } |
(...skipping 14 matching lines...) Expand all Loading... |
38 pObj->m_ObjNum = objnum; | 35 pObj->m_ObjNum = objnum; |
39 m_LastObjNum = std::max(m_LastObjNum, objnum); | 36 m_LastObjNum = std::max(m_LastObjNum, objnum); |
40 m_IndirectObjs[objnum].reset(pObj); | 37 m_IndirectObjs[objnum].reset(pObj); |
41 return pObj; | 38 return pObj; |
42 } | 39 } |
43 | 40 |
44 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { | 41 CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { |
45 return nullptr; | 42 return nullptr; |
46 } | 43 } |
47 | 44 |
48 CPDF_Object* CPDF_IndirectObjectHolder::AddIndirectObject(UniqueObject pObj) { | 45 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { |
49 if (pObj->m_ObjNum) | 46 if (pObj->m_ObjNum) |
50 return pObj.release(); // TODO(tsepez): shouldn't happen, stop this leak. | 47 return pObj->m_ObjNum; |
51 | 48 |
52 pObj->m_ObjNum = ++m_LastObjNum; | 49 m_LastObjNum++; |
53 m_IndirectObjs[m_LastObjNum].release(); // TODO(tsepez): stop this leak. | 50 m_IndirectObjs[m_LastObjNum].release(); // TODO(tsepez): stop this leak. |
54 m_IndirectObjs[m_LastObjNum].reset(pObj.release()); // Changes deleters. | 51 m_IndirectObjs[m_LastObjNum].reset(pObj); |
55 return m_IndirectObjs[m_LastObjNum].get(); | 52 pObj->m_ObjNum = m_LastObjNum; |
56 } | 53 return m_LastObjNum; |
57 | |
58 CPDF_Array* CPDF_IndirectObjectHolder::AddIndirectArray() { | |
59 return ToArray(AddIndirectObject(UniqueObject(new CPDF_Array()))); | |
60 } | |
61 | |
62 CPDF_Dictionary* CPDF_IndirectObjectHolder::AddIndirectDictionary() { | |
63 return ToDictionary(AddIndirectObject(UniqueObject(new CPDF_Dictionary()))); | |
64 } | |
65 | |
66 CPDF_Dictionary* CPDF_IndirectObjectHolder::AddIndirectDictionary( | |
67 const CFX_WeakPtr<CFX_ByteStringPool>& pPool) { | |
68 return ToDictionary( | |
69 AddIndirectObject(UniqueObject(new CPDF_Dictionary(pPool)))); | |
70 } | |
71 | |
72 CPDF_Stream* CPDF_IndirectObjectHolder::AddIndirectStream() { | |
73 return ToStream(AddIndirectObject(UniqueObject(new CPDF_Stream()))); | |
74 } | |
75 | |
76 CPDF_Stream* CPDF_IndirectObjectHolder::AddIndirectStream( | |
77 uint8_t* pData, | |
78 uint32_t size, | |
79 CPDF_Dictionary* pDict) { | |
80 return ToStream( | |
81 AddIndirectObject(UniqueObject(new CPDF_Stream(pData, size, pDict)))); | |
82 } | 54 } |
83 | 55 |
84 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( | 56 bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( |
85 uint32_t objnum, | 57 uint32_t objnum, |
86 UniqueObject pObj) { | 58 CPDF_Object* pObj) { |
87 if (!objnum || !pObj) | 59 if (!objnum || !pObj) |
88 return false; | 60 return false; |
89 | 61 |
90 CPDF_Object* pOldObj = GetIndirectObject(objnum); | 62 CPDF_Object* pOldObj = GetIndirectObject(objnum); |
91 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) | 63 if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { |
| 64 delete pObj; |
92 return false; | 65 return false; |
93 | 66 } |
94 pObj->m_ObjNum = objnum; | 67 pObj->m_ObjNum = objnum; |
95 m_IndirectObjs[objnum].reset(pObj.release()); // Changes deleters. | 68 m_IndirectObjs[objnum].reset(pObj); |
96 m_LastObjNum = std::max(m_LastObjNum, objnum); | 69 m_LastObjNum = std::max(m_LastObjNum, objnum); |
97 return true; | 70 return true; |
98 } | 71 } |
99 | 72 |
100 void CPDF_IndirectObjectHolder::DeleteIndirectObject(uint32_t objnum) { | 73 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { |
101 CPDF_Object* pObj = GetIndirectObject(objnum); | 74 CPDF_Object* pObj = GetIndirectObject(objnum); |
102 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) | 75 if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) |
103 return; | 76 return; |
104 | 77 |
105 m_IndirectObjs.erase(objnum); | 78 m_IndirectObjs.erase(objnum); |
106 } | 79 } |
OLD | NEW |