Chromium Code Reviews| 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 if (it == m_IndirectObjs.end()) |
|
Tom Sepez
2016/08/17 16:46:45
nit: maybe ?-operator here as in
return it != m_
dsinclair
2016/08/17 16:59:25
Done.
| |
| 29 return it->second->GetObjNum() != CPDF_Object::kInvalidObjNum ? it->second | 26 return nullptr; |
| 30 : nullptr; | 27 return it->second; |
| 28 } | |
| 31 | 29 |
| 32 if (!m_pParser) | 30 CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObjectIfValid( |
| 31 uint32_t objnum) { | |
| 32 CPDF_Object* pObj = GetIndirectObject(objnum); | |
| 33 if (pObj && pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) | |
| 33 return nullptr; | 34 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; | 35 return pObj; |
| 46 } | 36 } |
| 47 | 37 |
| 48 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { | 38 uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { |
|
Tom Sepez
2016/08/17 16:46:44
nit: I wish this name suggested that the obj gets
dsinclair
2016/08/17 16:59:25
Acknowledged. Couldn't come up with anything bette
| |
| 49 if (pObj->m_ObjNum) | 39 if (pObj->m_ObjNum) |
| 50 return pObj->m_ObjNum; | 40 return pObj->m_ObjNum; |
| 51 | 41 |
| 52 m_LastObjNum++; | 42 m_LastObjNum++; |
| 53 m_IndirectObjs[m_LastObjNum] = pObj; | |
| 54 pObj->m_ObjNum = m_LastObjNum; | 43 pObj->m_ObjNum = m_LastObjNum; |
| 44 EmplaceIndirectObject(pObj); | |
| 55 return m_LastObjNum; | 45 return m_LastObjNum; |
| 56 } | 46 } |
| 57 | 47 |
| 58 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { | 48 void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { |
| 59 auto it = m_IndirectObjs.find(objnum); | 49 CPDF_Object* pObj = GetIndirectObjectIfValid(objnum); |
| 60 if (it == m_IndirectObjs.end() || | 50 if (!pObj) |
| 61 it->second->GetObjNum() == CPDF_Object::kInvalidObjNum) { | |
| 62 return; | 51 return; |
| 63 } | 52 |
| 64 it->second->Destroy(); | 53 pObj->Destroy(); |
| 65 m_IndirectObjs.erase(it); | 54 m_IndirectObjs.erase(objnum); |
| 66 } | 55 } |
| 67 | 56 |
| 68 bool CPDF_IndirectObjectHolder::InsertIndirectObject(uint32_t objnum, | 57 bool CPDF_IndirectObjectHolder::InsertIndirectObjectIfHigherGeneration( |
|
Tom Sepez
2016/08/17 16:46:45
nit: More like ReplaceIndirectObjectIfHigherGenera
dsinclair
2016/08/17 16:59:25
Done.
| |
| 69 CPDF_Object* pObj) { | 58 uint32_t objnum, |
| 59 CPDF_Object* pObj) { | |
| 70 if (!objnum || !pObj) | 60 if (!objnum || !pObj) |
| 71 return false; | 61 return false; |
| 72 | 62 |
| 73 auto it = m_IndirectObjs.find(objnum); | 63 CPDF_Object* pOldObj = GetIndirectObject(objnum); |
| 74 if (it != m_IndirectObjs.end()) { | 64 if (pOldObj) { |
| 75 if (pObj->GetGenNum() <= it->second->GetGenNum()) { | 65 if (pObj->GetGenNum() <= pOldObj->GetGenNum()) { |
|
Tom Sepez
2016/08/17 16:46:44
nit: combine with &&
dsinclair
2016/08/17 16:59:25
Done.
| |
| 76 pObj->Destroy(); | 66 pObj->Destroy(); |
| 77 return false; | 67 return false; |
| 78 } | 68 } |
| 79 it->second->Destroy(); | |
| 80 } | 69 } |
| 70 | |
| 81 pObj->m_ObjNum = objnum; | 71 pObj->m_ObjNum = objnum; |
| 82 m_IndirectObjs[objnum] = pObj; | 72 EmplaceIndirectObject(pObj); |
| 83 m_LastObjNum = std::max(m_LastObjNum, objnum); | |
| 84 return true; | 73 return true; |
| 85 } | 74 } |
| 75 | |
| 76 void CPDF_IndirectObjectHolder::EmplaceIndirectObject(CPDF_Object* pObj) { | |
|
Tom Sepez
2016/08/17 16:46:45
nit: more like replace rather than emplace, Emplac
dsinclair
2016/08/17 16:59:25
Done.
| |
| 77 m_LastObjNum = std::max(m_LastObjNum, pObj->m_ObjNum); | |
| 78 if (m_IndirectObjs[pObj->m_ObjNum]) | |
| 79 m_IndirectObjs[pObj->m_ObjNum]->Destroy(); | |
| 80 | |
| 81 m_IndirectObjs[pObj->m_ObjNum] = pObj; | |
| 82 } | |
| OLD | NEW |