Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: core/fpdfapi/fpdf_parser/cpdf_dictionary.cpp

Issue 2345063002: Use string pools in some dictionaries (Closed)
Patch Set: more Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_dictionary.h" 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
8 8
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" 12 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h"
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
14 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" 14 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h"
15 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" 15 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
16 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" 16 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h"
17 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" 17 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
18 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" 18 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
19 #include "third_party/base/stl_util.h" 19 #include "third_party/base/stl_util.h"
20 20
21 CPDF_Dictionary::CPDF_Dictionary() {} 21 CPDF_Dictionary::CPDF_Dictionary() : m_pPool(nullptr) {}
22
23 CPDF_Dictionary::CPDF_Dictionary(CFX_ByteStringPool* pPool) : m_pPool(pPool) {}
22 24
23 CPDF_Dictionary::~CPDF_Dictionary() { 25 CPDF_Dictionary::~CPDF_Dictionary() {
24 // Mark the object as deleted so that it will not be deleted again 26 // Mark the object as deleted so that it will not be deleted again
25 // in case of cyclic references. 27 // in case of cyclic references.
26 m_ObjNum = kInvalidObjNum; 28 m_ObjNum = kInvalidObjNum;
27 for (const auto& it : m_Map) { 29 for (const auto& it : m_Map) {
28 if (it.second) 30 if (it.second)
29 it.second->Release(); 31 it.second->Release();
30 } 32 }
31 } 33 }
(...skipping 21 matching lines...) Expand all
53 } 55 }
54 56
55 CPDF_Object* CPDF_Dictionary::Clone() const { 57 CPDF_Object* CPDF_Dictionary::Clone() const {
56 return CloneObjectNonCyclic(false); 58 return CloneObjectNonCyclic(false);
57 } 59 }
58 60
59 CPDF_Object* CPDF_Dictionary::CloneNonCyclic( 61 CPDF_Object* CPDF_Dictionary::CloneNonCyclic(
60 bool bDirect, 62 bool bDirect,
61 std::set<const CPDF_Object*>* pVisited) const { 63 std::set<const CPDF_Object*>* pVisited) const {
62 pVisited->insert(this); 64 pVisited->insert(this);
63 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); 65 CPDF_Dictionary* pCopy = new CPDF_Dictionary(m_pPool);
64 for (const auto& it : *this) { 66 for (const auto& it : *this) {
65 CPDF_Object* value = it.second; 67 CPDF_Object* value = it.second;
66 if (!pdfium::ContainsKey(*pVisited, value)) { 68 if (!pdfium::ContainsKey(*pVisited, value)) {
67 pCopy->m_Map.insert( 69 pCopy->m_Map.insert(
68 std::make_pair(it.first, value->CloneNonCyclic(bDirect, pVisited))); 70 std::make_pair(it.first, value->CloneNonCyclic(bDirect, pVisited)));
69 } 71 }
70 } 72 }
71 return pCopy; 73 return pCopy;
72 } 74 }
73 75
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 CPDF_Object* pType = GetDirectObjectFor("Type"); 167 CPDF_Object* pType = GetDirectObjectFor("Type");
166 if (!pType) 168 if (!pType)
167 pType = GetDirectObjectFor("FT"); 169 pType = GetDirectObjectFor("FT");
168 return pType && pType->GetString() == "Sig"; 170 return pType && pType->GetString() == "Sig";
169 } 171 }
170 172
171 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) { 173 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) {
172 auto it = m_Map.find(key); 174 auto it = m_Map.find(key);
173 if (it == m_Map.end()) { 175 if (it == m_Map.end()) {
174 if (pObj) 176 if (pObj)
175 m_Map.insert(std::make_pair(key, pObj)); 177 m_Map.insert(std::make_pair(MaybeIntern(key), pObj));
176 return; 178 return;
177 } 179 }
178 180
179 if (it->second == pObj) 181 if (it->second == pObj)
180 return; 182 return;
181 it->second->Release(); 183 it->second->Release();
182 184
183 if (pObj) 185 if (pObj)
184 it->second = pObj; 186 it->second = pObj;
185 else 187 else
(...skipping 16 matching lines...) Expand all
202 return; 204 return;
203 205
204 auto new_it = m_Map.find(newkey); 206 auto new_it = m_Map.find(newkey);
205 if (new_it == old_it) 207 if (new_it == old_it)
206 return; 208 return;
207 209
208 if (new_it != m_Map.end()) { 210 if (new_it != m_Map.end()) {
209 new_it->second->Release(); 211 new_it->second->Release();
210 new_it->second = old_it->second; 212 new_it->second = old_it->second;
211 } else { 213 } else {
212 m_Map.insert(std::make_pair(newkey, old_it->second)); 214 m_Map.insert(std::make_pair(MaybeIntern(newkey), old_it->second));
213 } 215 }
214 m_Map.erase(old_it); 216 m_Map.erase(old_it);
215 } 217 }
216 218
217 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) { 219 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) {
218 SetFor(key, new CPDF_Number(i)); 220 SetFor(key, new CPDF_Number(i));
219 } 221 }
220 222
221 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key, 223 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key,
222 const CFX_ByteString& name) { 224 const CFX_ByteString& name) {
223 SetFor(key, new CPDF_Name(name)); 225 SetFor(key, new CPDF_Name(MaybeIntern(name)));
224 } 226 }
225 227
226 void CPDF_Dictionary::SetStringFor(const CFX_ByteString& key, 228 void CPDF_Dictionary::SetStringFor(const CFX_ByteString& key,
227 const CFX_ByteString& str) { 229 const CFX_ByteString& str) {
228 SetFor(key, new CPDF_String(str, FALSE)); 230 SetFor(key, new CPDF_String(MaybeIntern(str), FALSE));
229 } 231 }
230 232
231 void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key, 233 void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key,
232 CPDF_IndirectObjectHolder* pDoc, 234 CPDF_IndirectObjectHolder* pDoc,
233 uint32_t objnum) { 235 uint32_t objnum) {
234 SetFor(key, new CPDF_Reference(pDoc, objnum)); 236 SetFor(key, new CPDF_Reference(pDoc, objnum));
235 } 237 }
236 238
237 void CPDF_Dictionary::SetNumberFor(const CFX_ByteString& key, FX_FLOAT f) { 239 void CPDF_Dictionary::SetNumberFor(const CFX_ByteString& key, FX_FLOAT f) {
238 SetFor(key, new CPDF_Number(f)); 240 SetFor(key, new CPDF_Number(f));
(...skipping 17 matching lines...) Expand all
256 const CFX_Matrix& matrix) { 258 const CFX_Matrix& matrix) {
257 CPDF_Array* pArray = new CPDF_Array; 259 CPDF_Array* pArray = new CPDF_Array;
258 pArray->AddNumber(matrix.a); 260 pArray->AddNumber(matrix.a);
259 pArray->AddNumber(matrix.b); 261 pArray->AddNumber(matrix.b);
260 pArray->AddNumber(matrix.c); 262 pArray->AddNumber(matrix.c);
261 pArray->AddNumber(matrix.d); 263 pArray->AddNumber(matrix.d);
262 pArray->AddNumber(matrix.e); 264 pArray->AddNumber(matrix.e);
263 pArray->AddNumber(matrix.f); 265 pArray->AddNumber(matrix.f);
264 SetFor(key, pArray); 266 SetFor(key, pArray);
265 } 267 }
268
269 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) {
270 return m_pPool ? m_pPool->Intern(str) : str;
271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698