| 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_array.h" | 7 #include "core/fpdfapi/parser/cpdf_array.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "core/fpdfapi/parser/cpdf_name.h" | 11 #include "core/fpdfapi/parser/cpdf_name.h" |
| 12 #include "core/fpdfapi/parser/cpdf_number.h" | 12 #include "core/fpdfapi/parser/cpdf_number.h" |
| 13 #include "core/fpdfapi/parser/cpdf_reference.h" | 13 #include "core/fpdfapi/parser/cpdf_reference.h" |
| 14 #include "core/fpdfapi/parser/cpdf_stream.h" | 14 #include "core/fpdfapi/parser/cpdf_stream.h" |
| 15 #include "core/fpdfapi/parser/cpdf_string.h" | 15 #include "core/fpdfapi/parser/cpdf_string.h" |
| 16 #include "third_party/base/logging.h" | 16 #include "third_party/base/logging.h" |
| 17 #include "third_party/base/stl_util.h" | 17 #include "third_party/base/stl_util.h" |
| 18 | 18 |
| 19 CPDF_Array::CPDF_Array() {} | 19 CPDF_Array::CPDF_Array() {} |
| 20 | 20 |
| 21 CPDF_Array::~CPDF_Array() { | 21 CPDF_Array::~CPDF_Array() { |
| 22 // Mark the object as deleted so that it will not be deleted again | 22 // Mark the object as deleted so that it will not be deleted again |
| 23 // in case of cyclic references. | 23 // in case of cyclic references, then break cycles. |
| 24 m_ObjNum = kInvalidObjNum; | 24 m_ObjNum = kInvalidObjNum; |
| 25 for (auto& it : m_Objects) { | 25 for (auto& it : m_Objects) { |
| 26 if (it && it->GetObjNum() != kInvalidObjNum) | 26 if (it && it->GetObjNum() == kInvalidObjNum) |
| 27 it->Release(); | 27 it.release(); |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 CPDF_Object::Type CPDF_Array::GetType() const { | 31 CPDF_Object::Type CPDF_Array::GetType() const { |
| 32 return ARRAY; | 32 return ARRAY; |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool CPDF_Array::IsArray() const { | 35 bool CPDF_Array::IsArray() const { |
| 36 return true; | 36 return true; |
| 37 } | 37 } |
| 38 | 38 |
| 39 CPDF_Array* CPDF_Array::AsArray() { | 39 CPDF_Array* CPDF_Array::AsArray() { |
| 40 return this; | 40 return this; |
| 41 } | 41 } |
| 42 | 42 |
| 43 const CPDF_Array* CPDF_Array::AsArray() const { | 43 const CPDF_Array* CPDF_Array::AsArray() const { |
| 44 return this; | 44 return this; |
| 45 } | 45 } |
| 46 | 46 |
| 47 CPDF_Object* CPDF_Array::Clone() const { | 47 CPDF_Object* CPDF_Array::Clone() const { |
| 48 return CloneObjectNonCyclic(false); | 48 return CloneObjectNonCyclic(false); |
| 49 } | 49 } |
| 50 | 50 |
| 51 CPDF_Object* CPDF_Array::CloneNonCyclic( | 51 CPDF_Object* CPDF_Array::CloneNonCyclic( |
| 52 bool bDirect, | 52 bool bDirect, |
| 53 std::set<const CPDF_Object*>* pVisited) const { | 53 std::set<const CPDF_Object*>* pVisited) const { |
| 54 pVisited->insert(this); | 54 pVisited->insert(this); |
| 55 CPDF_Array* pCopy = new CPDF_Array(); | 55 CPDF_Array* pCopy = new CPDF_Array(); |
| 56 for (size_t i = 0; i < GetCount(); i++) { | 56 for (const auto& pObj : m_Objects) { |
| 57 CPDF_Object* value = m_Objects[i]; | 57 if (!pdfium::ContainsKey(*pVisited, pObj.get())) { |
| 58 if (!pdfium::ContainsKey(*pVisited, value)) | 58 pCopy->m_Objects.push_back( |
| 59 pCopy->m_Objects.push_back(value->CloneNonCyclic(bDirect, pVisited)); | 59 UniqueObject(pObj->CloneNonCyclic(bDirect, pVisited))); |
| 60 } |
| 60 } | 61 } |
| 61 return pCopy; | 62 return pCopy; |
| 62 } | 63 } |
| 63 | 64 |
| 64 CFX_FloatRect CPDF_Array::GetRect() { | 65 CFX_FloatRect CPDF_Array::GetRect() { |
| 65 CFX_FloatRect rect; | 66 CFX_FloatRect rect; |
| 66 if (!IsArray() || m_Objects.size() != 4) | 67 if (!IsArray() || m_Objects.size() != 4) |
| 67 return rect; | 68 return rect; |
| 68 | 69 |
| 69 rect.left = GetNumberAt(0); | 70 rect.left = GetNumberAt(0); |
| 70 rect.bottom = GetNumberAt(1); | 71 rect.bottom = GetNumberAt(1); |
| 71 rect.right = GetNumberAt(2); | 72 rect.right = GetNumberAt(2); |
| 72 rect.top = GetNumberAt(3); | 73 rect.top = GetNumberAt(3); |
| 73 return rect; | 74 return rect; |
| 74 } | 75 } |
| 75 | 76 |
| 76 CFX_Matrix CPDF_Array::GetMatrix() { | 77 CFX_Matrix CPDF_Array::GetMatrix() { |
| 77 CFX_Matrix matrix; | 78 CFX_Matrix matrix; |
| 78 if (!IsArray() || m_Objects.size() != 6) | 79 if (!IsArray() || m_Objects.size() != 6) |
| 79 return matrix; | 80 return matrix; |
| 80 | 81 |
| 81 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), | 82 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), |
| 82 GetNumberAt(4), GetNumberAt(5)); | 83 GetNumberAt(4), GetNumberAt(5)); |
| 83 return matrix; | 84 return matrix; |
| 84 } | 85 } |
| 85 | 86 |
| 86 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { | 87 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { |
| 87 if (i >= m_Objects.size()) | 88 if (i >= m_Objects.size()) |
| 88 return nullptr; | 89 return nullptr; |
| 89 return m_Objects[i]; | 90 return m_Objects[i].get(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { | 93 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { |
| 93 if (i >= m_Objects.size()) | 94 if (i >= m_Objects.size()) |
| 94 return nullptr; | 95 return nullptr; |
| 95 return m_Objects[i]->GetDirect(); | 96 return m_Objects[i]->GetDirect(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { | 99 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { |
| 99 if (i >= m_Objects.size()) | 100 if (i >= m_Objects.size()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 return ToArray(GetDirectObjectAt(i)); | 133 return ToArray(GetDirectObjectAt(i)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { | 136 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { |
| 136 if (i >= m_Objects.size()) | 137 if (i >= m_Objects.size()) |
| 137 return; | 138 return; |
| 138 | 139 |
| 139 if (nCount <= 0 || nCount > m_Objects.size() - i) | 140 if (nCount <= 0 || nCount > m_Objects.size() - i) |
| 140 return; | 141 return; |
| 141 | 142 |
| 142 for (size_t j = 0; j < nCount; ++j) { | 143 auto it = m_Objects.begin() + i; |
| 143 if (CPDF_Object* p = m_Objects[i + j]) | 144 m_Objects.erase(it, it + nCount); |
| 144 p->Release(); | |
| 145 } | |
| 146 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount); | |
| 147 } | 145 } |
| 148 | 146 |
| 149 void CPDF_Array::ConvertToIndirectObjectAt(size_t i, | 147 void CPDF_Array::ConvertToIndirectObjectAt(size_t i, |
| 150 CPDF_IndirectObjectHolder* pHolder) { | 148 CPDF_IndirectObjectHolder* pHolder) { |
| 151 if (i >= m_Objects.size()) | 149 if (i >= m_Objects.size()) |
| 152 return; | 150 return; |
| 153 | 151 |
| 154 CPDF_Object* pObj = m_Objects[i]; | 152 if (!m_Objects[i] || m_Objects[i]->IsReference()) |
| 155 if (!pObj || pObj->IsReference()) | |
| 156 return; | 153 return; |
| 157 | 154 |
| 158 uint32_t dwObjNum = pHolder->AddIndirectObject(pObj); | 155 uint32_t dwObjNum = pHolder->AddIndirectObject(m_Objects[i].release()); |
| 159 m_Objects[i] = new CPDF_Reference(pHolder, dwObjNum); | 156 m_Objects[i] = UniqueObject(new CPDF_Reference(pHolder, dwObjNum)); |
| 160 } | 157 } |
| 161 | 158 |
| 162 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { | 159 void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { |
| 163 ASSERT(IsArray()); | 160 ASSERT(IsArray()); |
| 164 CHECK(!pObj || pObj->IsInline()); | 161 CHECK(!pObj || pObj->IsInline()); |
| 165 if (i >= m_Objects.size()) { | 162 if (i >= m_Objects.size()) { |
| 166 ASSERT(false); | 163 ASSERT(false); |
| 167 return; | 164 return; |
| 168 } | 165 } |
| 169 if (CPDF_Object* pOld = m_Objects[i]) | 166 m_Objects[i] = UniqueObject(pObj); |
| 170 pOld->Release(); | |
| 171 | |
| 172 m_Objects[i] = pObj; | |
| 173 } | 167 } |
| 174 | 168 |
| 175 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { | 169 void CPDF_Array::InsertAt(size_t index, CPDF_Object* pObj) { |
| 176 ASSERT(IsArray()); | 170 ASSERT(IsArray()); |
| 177 CHECK(!pObj || pObj->IsInline()); | 171 CHECK(!pObj || pObj->IsInline()); |
| 178 if (index >= m_Objects.size()) { | 172 if (index >= m_Objects.size()) { |
| 179 // Allocate space first. | 173 // Allocate space first. |
| 180 m_Objects.resize(index + 1, nullptr); | 174 m_Objects.resize(index + 1); |
| 181 m_Objects[index] = pObj; | 175 m_Objects[index] = UniqueObject(pObj); |
| 182 } else { | 176 } else { |
| 183 // Directly insert. | 177 // Directly insert. |
| 184 m_Objects.insert(m_Objects.begin() + index, pObj); | 178 m_Objects.insert(m_Objects.begin() + index, UniqueObject(pObj)); |
| 185 } | 179 } |
| 186 } | 180 } |
| 187 | 181 |
| 188 void CPDF_Array::Add(CPDF_Object* pObj) { | 182 void CPDF_Array::Add(CPDF_Object* pObj) { |
| 189 ASSERT(IsArray()); | 183 ASSERT(IsArray()); |
| 190 CHECK(!pObj || pObj->IsInline()); | 184 CHECK(!pObj || pObj->IsInline()); |
| 191 m_Objects.push_back(pObj); | 185 m_Objects.push_back(UniqueObject(pObj)); |
| 192 } | 186 } |
| 193 | 187 |
| 194 void CPDF_Array::AddName(const CFX_ByteString& str) { | 188 void CPDF_Array::AddName(const CFX_ByteString& str) { |
| 195 Add(new CPDF_Name(str)); | 189 Add(new CPDF_Name(str)); |
| 196 } | 190 } |
| 197 | 191 |
| 198 void CPDF_Array::AddString(const CFX_ByteString& str) { | 192 void CPDF_Array::AddString(const CFX_ByteString& str) { |
| 199 Add(new CPDF_String(str, FALSE)); | 193 Add(new CPDF_String(str, FALSE)); |
| 200 } | 194 } |
| 201 | 195 |
| 202 void CPDF_Array::AddInteger(int i) { | 196 void CPDF_Array::AddInteger(int i) { |
| 203 Add(new CPDF_Number(i)); | 197 Add(new CPDF_Number(i)); |
| 204 } | 198 } |
| 205 | 199 |
| 206 void CPDF_Array::AddNumber(FX_FLOAT f) { | 200 void CPDF_Array::AddNumber(FX_FLOAT f) { |
| 207 Add(new CPDF_Number(f)); | 201 Add(new CPDF_Number(f)); |
| 208 } | 202 } |
| 209 | 203 |
| 210 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, | 204 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, |
| 211 uint32_t objnum) { | 205 uint32_t objnum) { |
| 212 Add(new CPDF_Reference(pDoc, objnum)); | 206 Add(new CPDF_Reference(pDoc, objnum)); |
| 213 } | 207 } |
| OLD | NEW |