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