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_dictionary.h" | 7 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "core/fpdfapi/parser/cpdf_array.h" | 12 #include "core/fpdfapi/parser/cpdf_array.h" |
13 #include "core/fpdfapi/parser/cpdf_boolean.h" | 13 #include "core/fpdfapi/parser/cpdf_boolean.h" |
14 #include "core/fpdfapi/parser/cpdf_name.h" | 14 #include "core/fpdfapi/parser/cpdf_name.h" |
15 #include "core/fpdfapi/parser/cpdf_number.h" | 15 #include "core/fpdfapi/parser/cpdf_number.h" |
16 #include "core/fpdfapi/parser/cpdf_reference.h" | 16 #include "core/fpdfapi/parser/cpdf_reference.h" |
17 #include "core/fpdfapi/parser/cpdf_stream.h" | 17 #include "core/fpdfapi/parser/cpdf_stream.h" |
18 #include "core/fpdfapi/parser/cpdf_string.h" | 18 #include "core/fpdfapi/parser/cpdf_string.h" |
19 #include "third_party/base/logging.h" | 19 #include "third_party/base/logging.h" |
20 #include "third_party/base/stl_util.h" | 20 #include "third_party/base/stl_util.h" |
21 | 21 |
22 CPDF_Dictionary::CPDF_Dictionary() | 22 CPDF_Dictionary::CPDF_Dictionary() |
23 : CPDF_Dictionary(CFX_WeakPtr<CFX_ByteStringPool>()) {} | 23 : CPDF_Dictionary(CFX_WeakPtr<CFX_ByteStringPool>()) {} |
24 | 24 |
25 CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool) | 25 CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool) |
26 : m_pPool(pPool) {} | 26 : m_pPool(pPool) {} |
27 | 27 |
28 CPDF_Dictionary::~CPDF_Dictionary() { | 28 CPDF_Dictionary::~CPDF_Dictionary() { |
29 // Mark the object as deleted so that it will not be deleted again | 29 // Mark the object as deleted so that it will not be deleted again |
30 // in case of cyclic references, then break cycles. | 30 // in case of cyclic references. |
31 m_ObjNum = kInvalidObjNum; | 31 m_ObjNum = kInvalidObjNum; |
32 for (auto& it : m_Map) { | 32 for (const auto& it : m_Map) { |
33 if (it.second && it.second->GetObjNum() == kInvalidObjNum) | 33 if (it.second && it.second->GetObjNum() != kInvalidObjNum) |
34 it.second.release(); | 34 it.second->Release(); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 CPDF_Object::Type CPDF_Dictionary::GetType() const { | 38 CPDF_Object::Type CPDF_Dictionary::GetType() const { |
39 return DICTIONARY; | 39 return DICTIONARY; |
40 } | 40 } |
41 | 41 |
42 CPDF_Dictionary* CPDF_Dictionary::GetDict() const { | 42 CPDF_Dictionary* CPDF_Dictionary::GetDict() const { |
43 // The method should be made non-const if we want to not be const. | 43 // The method should be made non-const if we want to not be const. |
44 // See bug #234. | 44 // See bug #234. |
(...skipping 13 matching lines...) Expand all Loading... |
58 } | 58 } |
59 | 59 |
60 CPDF_Object* CPDF_Dictionary::Clone() const { | 60 CPDF_Object* CPDF_Dictionary::Clone() const { |
61 return CloneObjectNonCyclic(false); | 61 return CloneObjectNonCyclic(false); |
62 } | 62 } |
63 | 63 |
64 CPDF_Object* CPDF_Dictionary::CloneNonCyclic( | 64 CPDF_Object* CPDF_Dictionary::CloneNonCyclic( |
65 bool bDirect, | 65 bool bDirect, |
66 std::set<const CPDF_Object*>* pVisited) const { | 66 std::set<const CPDF_Object*>* pVisited) const { |
67 pVisited->insert(this); | 67 pVisited->insert(this); |
68 UniqueDictionary pCopy = UniqueDictionary(new CPDF_Dictionary(m_pPool)); | 68 CPDF_Dictionary* pCopy = new CPDF_Dictionary(m_pPool); |
69 for (const auto& it : *this) { | 69 for (const auto& it : *this) { |
70 if (!pdfium::ContainsKey(*pVisited, it.second.get())) { | 70 CPDF_Object* value = it.second; |
71 pCopy->m_Map[it.first] = | 71 if (!pdfium::ContainsKey(*pVisited, value)) { |
72 UniqueObject(it.second->CloneNonCyclic(bDirect, pVisited)); | 72 pCopy->m_Map.insert( |
| 73 std::make_pair(it.first, value->CloneNonCyclic(bDirect, pVisited))); |
73 } | 74 } |
74 } | 75 } |
75 return pCopy.release(); | 76 return pCopy; |
76 } | 77 } |
77 | 78 |
78 CPDF_Object* CPDF_Dictionary::GetObjectFor(const CFX_ByteString& key) const { | 79 CPDF_Object* CPDF_Dictionary::GetObjectFor(const CFX_ByteString& key) const { |
79 auto it = m_Map.find(key); | 80 auto it = m_Map.find(key); |
80 return it != m_Map.end() ? it->second.get() : nullptr; | 81 return it != m_Map.end() ? it->second : nullptr; |
81 } | 82 } |
82 | 83 |
83 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor( | 84 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor( |
84 const CFX_ByteString& key) const { | 85 const CFX_ByteString& key) const { |
85 CPDF_Object* p = GetObjectFor(key); | 86 CPDF_Object* p = GetObjectFor(key); |
86 return p ? p->GetDirect() : nullptr; | 87 return p ? p->GetDirect() : nullptr; |
87 } | 88 } |
88 | 89 |
89 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key) const { | 90 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key) const { |
90 CPDF_Object* p = GetObjectFor(key); | 91 CPDF_Object* p = GetObjectFor(key); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 167 } |
167 | 168 |
168 bool CPDF_Dictionary::IsSignatureDict() const { | 169 bool CPDF_Dictionary::IsSignatureDict() const { |
169 CPDF_Object* pType = GetDirectObjectFor("Type"); | 170 CPDF_Object* pType = GetDirectObjectFor("Type"); |
170 if (!pType) | 171 if (!pType) |
171 pType = GetDirectObjectFor("FT"); | 172 pType = GetDirectObjectFor("FT"); |
172 return pType && pType->GetString() == "Sig"; | 173 return pType && pType->GetString() == "Sig"; |
173 } | 174 } |
174 | 175 |
175 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) { | 176 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) { |
176 if (!pObj) { | 177 CHECK(!pObj || pObj->IsInline()); |
177 m_Map.erase(key); | 178 auto it = m_Map.find(key); |
| 179 if (it == m_Map.end()) { |
| 180 if (pObj) |
| 181 m_Map.insert(std::make_pair(MaybeIntern(key), pObj)); |
178 return; | 182 return; |
179 } | 183 } |
180 ASSERT(pObj->IsInline()); | 184 |
181 m_Map[key] = UniqueObject(pObj); | 185 if (it->second == pObj) |
| 186 return; |
| 187 it->second->Release(); |
| 188 |
| 189 if (pObj) |
| 190 it->second = pObj; |
| 191 else |
| 192 m_Map.erase(it); |
182 } | 193 } |
183 | 194 |
184 void CPDF_Dictionary::ConvertToIndirectObjectFor( | 195 void CPDF_Dictionary::ConvertToIndirectObjectFor( |
185 const CFX_ByteString& key, | 196 const CFX_ByteString& key, |
186 CPDF_IndirectObjectHolder* pHolder) { | 197 CPDF_IndirectObjectHolder* pHolder) { |
187 auto it = m_Map.find(key); | 198 auto it = m_Map.find(key); |
188 if (it == m_Map.end() || it->second->IsReference()) | 199 if (it == m_Map.end() || it->second->IsReference()) |
189 return; | 200 return; |
190 | 201 |
191 uint32_t objnum = pHolder->AddIndirectObject(it->second.release()); | 202 uint32_t objnum = pHolder->AddIndirectObject(it->second); |
192 it->second = UniqueReference(new CPDF_Reference(pHolder, objnum)); | 203 it->second = new CPDF_Reference(pHolder, objnum); |
193 } | 204 } |
194 | 205 |
195 void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { | 206 void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { |
196 m_Map.erase(key); | 207 auto it = m_Map.find(key); |
| 208 if (it == m_Map.end()) |
| 209 return; |
| 210 |
| 211 it->second->Release(); |
| 212 m_Map.erase(it); |
197 } | 213 } |
198 | 214 |
199 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey, | 215 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey, |
200 const CFX_ByteString& newkey) { | 216 const CFX_ByteString& newkey) { |
201 if (oldkey == newkey) | |
202 return; | |
203 | |
204 auto old_it = m_Map.find(oldkey); | 217 auto old_it = m_Map.find(oldkey); |
205 if (old_it == m_Map.end()) | 218 if (old_it == m_Map.end()) |
206 return; | 219 return; |
207 | 220 |
208 m_Map[newkey] = std::move(old_it->second); | 221 auto new_it = m_Map.find(newkey); |
| 222 if (new_it == old_it) |
| 223 return; |
| 224 |
| 225 if (new_it != m_Map.end()) { |
| 226 new_it->second->Release(); |
| 227 new_it->second = old_it->second; |
| 228 } else { |
| 229 m_Map.insert(std::make_pair(MaybeIntern(newkey), old_it->second)); |
| 230 } |
209 m_Map.erase(old_it); | 231 m_Map.erase(old_it); |
210 } | 232 } |
211 | 233 |
212 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) { | 234 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) { |
213 SetFor(key, new CPDF_Number(i)); | 235 SetFor(key, new CPDF_Number(i)); |
214 } | 236 } |
215 | 237 |
216 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key, | 238 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key, |
217 const CFX_ByteString& name) { | 239 const CFX_ByteString& name) { |
218 SetFor(key, new CPDF_Name(MaybeIntern(name))); | 240 SetFor(key, new CPDF_Name(MaybeIntern(name))); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 pArray->AddNumber(matrix.c); | 277 pArray->AddNumber(matrix.c); |
256 pArray->AddNumber(matrix.d); | 278 pArray->AddNumber(matrix.d); |
257 pArray->AddNumber(matrix.e); | 279 pArray->AddNumber(matrix.e); |
258 pArray->AddNumber(matrix.f); | 280 pArray->AddNumber(matrix.f); |
259 SetFor(key, pArray); | 281 SetFor(key, pArray); |
260 } | 282 } |
261 | 283 |
262 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) { | 284 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) { |
263 return m_pPool ? m_pPool->Intern(str) : str; | 285 return m_pPool ? m_pPool->Intern(str) : str; |
264 } | 286 } |
OLD | NEW |