• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fpdfapi/parser/cpdf_dictionary.h"
8 
9 #include <set>
10 #include <utility>
11 
12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_boolean.h"
14 #include "core/fpdfapi/parser/cpdf_crypto_handler.h"
15 #include "core/fpdfapi/parser/cpdf_name.h"
16 #include "core/fpdfapi/parser/cpdf_number.h"
17 #include "core/fpdfapi/parser/cpdf_reference.h"
18 #include "core/fpdfapi/parser/cpdf_stream.h"
19 #include "core/fpdfapi/parser/cpdf_string.h"
20 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
21 #include "core/fxcrt/fx_stream.h"
22 #include "third_party/base/logging.h"
23 #include "third_party/base/ptr_util.h"
24 #include "third_party/base/stl_util.h"
25 
CPDF_Dictionary()26 CPDF_Dictionary::CPDF_Dictionary()
27     : CPDF_Dictionary(WeakPtr<ByteStringPool>()) {}
28 
CPDF_Dictionary(const WeakPtr<ByteStringPool> & pPool)29 CPDF_Dictionary::CPDF_Dictionary(const WeakPtr<ByteStringPool>& pPool)
30     : m_pPool(pPool) {}
31 
~CPDF_Dictionary()32 CPDF_Dictionary::~CPDF_Dictionary() {
33   // Mark the object as deleted so that it will not be deleted again,
34   // and break cyclic references.
35   m_ObjNum = kInvalidObjNum;
36   for (auto& it : m_Map) {
37     if (it.second && it.second->GetObjNum() == kInvalidObjNum)
38       it.second.Leak();
39   }
40 }
41 
GetType() const42 CPDF_Object::Type CPDF_Dictionary::GetType() const {
43   return kDictionary;
44 }
45 
GetDict()46 CPDF_Dictionary* CPDF_Dictionary::GetDict() {
47   return this;
48 }
49 
GetDict() const50 const CPDF_Dictionary* CPDF_Dictionary::GetDict() const {
51   return this;
52 }
53 
IsDictionary() const54 bool CPDF_Dictionary::IsDictionary() const {
55   return true;
56 }
57 
AsDictionary()58 CPDF_Dictionary* CPDF_Dictionary::AsDictionary() {
59   return this;
60 }
61 
AsDictionary() const62 const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const {
63   return this;
64 }
65 
Clone() const66 RetainPtr<CPDF_Object> CPDF_Dictionary::Clone() const {
67   return CloneObjectNonCyclic(false);
68 }
69 
CloneNonCyclic(bool bDirect,std::set<const CPDF_Object * > * pVisited) const70 RetainPtr<CPDF_Object> CPDF_Dictionary::CloneNonCyclic(
71     bool bDirect,
72     std::set<const CPDF_Object*>* pVisited) const {
73   pVisited->insert(this);
74   auto pCopy = pdfium::MakeRetain<CPDF_Dictionary>(m_pPool);
75   CPDF_DictionaryLocker locker(this);
76   for (const auto& it : locker) {
77     if (!pdfium::ContainsKey(*pVisited, it.second.Get())) {
78       std::set<const CPDF_Object*> visited(*pVisited);
79       if (auto obj = it.second->CloneNonCyclic(bDirect, &visited))
80         pCopy->m_Map.insert(std::make_pair(it.first, std::move(obj)));
81     }
82   }
83   return pCopy;
84 }
85 
GetObjectFor(const ByteString & key) const86 const CPDF_Object* CPDF_Dictionary::GetObjectFor(const ByteString& key) const {
87   auto it = m_Map.find(key);
88   return it != m_Map.end() ? it->second.Get() : nullptr;
89 }
90 
GetObjectFor(const ByteString & key)91 CPDF_Object* CPDF_Dictionary::GetObjectFor(const ByteString& key) {
92   return const_cast<CPDF_Object*>(
93       static_cast<const CPDF_Dictionary*>(this)->GetObjectFor(key));
94 }
95 
GetDirectObjectFor(const ByteString & key) const96 const CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(
97     const ByteString& key) const {
98   const CPDF_Object* p = GetObjectFor(key);
99   return p ? p->GetDirect() : nullptr;
100 }
101 
GetDirectObjectFor(const ByteString & key)102 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(const ByteString& key) {
103   return const_cast<CPDF_Object*>(
104       static_cast<const CPDF_Dictionary*>(this)->GetDirectObjectFor(key));
105 }
106 
GetStringFor(const ByteString & key) const107 ByteString CPDF_Dictionary::GetStringFor(const ByteString& key) const {
108   const CPDF_Object* p = GetObjectFor(key);
109   return p ? p->GetString() : ByteString();
110 }
111 
GetUnicodeTextFor(const ByteString & key) const112 WideString CPDF_Dictionary::GetUnicodeTextFor(const ByteString& key) const {
113   const CPDF_Object* p = GetObjectFor(key);
114   if (const CPDF_Reference* pRef = ToReference(p))
115     p = pRef->GetDirect();
116   return p ? p->GetUnicodeText() : WideString();
117 }
118 
GetStringFor(const ByteString & key,const ByteString & def) const119 ByteString CPDF_Dictionary::GetStringFor(const ByteString& key,
120                                          const ByteString& def) const {
121   const CPDF_Object* p = GetObjectFor(key);
122   return p ? p->GetString() : ByteString(def);
123 }
124 
GetIntegerFor(const ByteString & key) const125 int CPDF_Dictionary::GetIntegerFor(const ByteString& key) const {
126   const CPDF_Object* p = GetObjectFor(key);
127   return p ? p->GetInteger() : 0;
128 }
129 
GetIntegerFor(const ByteString & key,int def) const130 int CPDF_Dictionary::GetIntegerFor(const ByteString& key, int def) const {
131   const CPDF_Object* p = GetObjectFor(key);
132   return p ? p->GetInteger() : def;
133 }
134 
GetNumberFor(const ByteString & key) const135 float CPDF_Dictionary::GetNumberFor(const ByteString& key) const {
136   const CPDF_Object* p = GetObjectFor(key);
137   return p ? p->GetNumber() : 0;
138 }
139 
GetBooleanFor(const ByteString & key,bool bDefault) const140 bool CPDF_Dictionary::GetBooleanFor(const ByteString& key,
141                                     bool bDefault) const {
142   const CPDF_Object* p = GetObjectFor(key);
143   return ToBoolean(p) ? p->GetInteger() != 0 : bDefault;
144 }
145 
GetDictFor(const ByteString & key) const146 const CPDF_Dictionary* CPDF_Dictionary::GetDictFor(
147     const ByteString& key) const {
148   const CPDF_Object* p = GetDirectObjectFor(key);
149   if (!p)
150     return nullptr;
151   if (const CPDF_Dictionary* pDict = p->AsDictionary())
152     return pDict;
153   if (const CPDF_Stream* pStream = p->AsStream())
154     return pStream->GetDict();
155   return nullptr;
156 }
157 
GetDictFor(const ByteString & key)158 CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const ByteString& key) {
159   return const_cast<CPDF_Dictionary*>(
160       static_cast<const CPDF_Dictionary*>(this)->GetDictFor(key));
161 }
162 
GetArrayFor(const ByteString & key) const163 const CPDF_Array* CPDF_Dictionary::GetArrayFor(const ByteString& key) const {
164   return ToArray(GetDirectObjectFor(key));
165 }
166 
GetArrayFor(const ByteString & key)167 CPDF_Array* CPDF_Dictionary::GetArrayFor(const ByteString& key) {
168   return ToArray(GetDirectObjectFor(key));
169 }
170 
GetStreamFor(const ByteString & key) const171 const CPDF_Stream* CPDF_Dictionary::GetStreamFor(const ByteString& key) const {
172   return ToStream(GetDirectObjectFor(key));
173 }
174 
GetStreamFor(const ByteString & key)175 CPDF_Stream* CPDF_Dictionary::GetStreamFor(const ByteString& key) {
176   return ToStream(GetDirectObjectFor(key));
177 }
178 
GetRectFor(const ByteString & key) const179 CFX_FloatRect CPDF_Dictionary::GetRectFor(const ByteString& key) const {
180   CFX_FloatRect rect;
181   const CPDF_Array* pArray = GetArrayFor(key);
182   if (pArray)
183     rect = pArray->GetRect();
184   return rect;
185 }
186 
GetMatrixFor(const ByteString & key) const187 CFX_Matrix CPDF_Dictionary::GetMatrixFor(const ByteString& key) const {
188   CFX_Matrix matrix;
189   const CPDF_Array* pArray = GetArrayFor(key);
190   if (pArray)
191     matrix = pArray->GetMatrix();
192   return matrix;
193 }
194 
KeyExist(const ByteString & key) const195 bool CPDF_Dictionary::KeyExist(const ByteString& key) const {
196   return pdfium::ContainsKey(m_Map, key);
197 }
198 
GetKeys() const199 std::vector<ByteString> CPDF_Dictionary::GetKeys() const {
200   std::vector<ByteString> result;
201   CPDF_DictionaryLocker locker(this);
202   for (const auto& item : locker)
203     result.push_back(item.first);
204   return result;
205 }
206 
SetFor(const ByteString & key,RetainPtr<CPDF_Object> pObj)207 CPDF_Object* CPDF_Dictionary::SetFor(const ByteString& key,
208                                      RetainPtr<CPDF_Object> pObj) {
209   CHECK(!IsLocked());
210   if (!pObj) {
211     m_Map.erase(key);
212     return nullptr;
213   }
214   ASSERT(pObj->IsInline());
215   CPDF_Object* pRet = pObj.Get();
216   m_Map[MaybeIntern(key)] = std::move(pObj);
217   return pRet;
218 }
219 
ConvertToIndirectObjectFor(const ByteString & key,CPDF_IndirectObjectHolder * pHolder)220 void CPDF_Dictionary::ConvertToIndirectObjectFor(
221     const ByteString& key,
222     CPDF_IndirectObjectHolder* pHolder) {
223   CHECK(!IsLocked());
224   auto it = m_Map.find(key);
225   if (it == m_Map.end() || it->second->IsReference())
226     return;
227 
228   CPDF_Object* pObj = pHolder->AddIndirectObject(std::move(it->second));
229   it->second = pObj->MakeReference(pHolder);
230 }
231 
RemoveFor(const ByteString & key)232 RetainPtr<CPDF_Object> CPDF_Dictionary::RemoveFor(const ByteString& key) {
233   CHECK(!IsLocked());
234   RetainPtr<CPDF_Object> result;
235   auto it = m_Map.find(key);
236   if (it != m_Map.end()) {
237     result = std::move(it->second);
238     m_Map.erase(it);
239   }
240   return result;
241 }
242 
ReplaceKey(const ByteString & oldkey,const ByteString & newkey)243 void CPDF_Dictionary::ReplaceKey(const ByteString& oldkey,
244                                  const ByteString& newkey) {
245   CHECK(!IsLocked());
246   auto old_it = m_Map.find(oldkey);
247   if (old_it == m_Map.end())
248     return;
249 
250   auto new_it = m_Map.find(newkey);
251   if (new_it == old_it)
252     return;
253 
254   m_Map[MaybeIntern(newkey)] = std::move(old_it->second);
255   m_Map.erase(old_it);
256 }
257 
SetRectFor(const ByteString & key,const CFX_FloatRect & rect)258 void CPDF_Dictionary::SetRectFor(const ByteString& key,
259                                  const CFX_FloatRect& rect) {
260   CPDF_Array* pArray = SetNewFor<CPDF_Array>(key);
261   pArray->AddNew<CPDF_Number>(rect.left);
262   pArray->AddNew<CPDF_Number>(rect.bottom);
263   pArray->AddNew<CPDF_Number>(rect.right);
264   pArray->AddNew<CPDF_Number>(rect.top);
265 }
266 
SetMatrixFor(const ByteString & key,const CFX_Matrix & matrix)267 void CPDF_Dictionary::SetMatrixFor(const ByteString& key,
268                                    const CFX_Matrix& matrix) {
269   CPDF_Array* pArray = SetNewFor<CPDF_Array>(key);
270   pArray->AddNew<CPDF_Number>(matrix.a);
271   pArray->AddNew<CPDF_Number>(matrix.b);
272   pArray->AddNew<CPDF_Number>(matrix.c);
273   pArray->AddNew<CPDF_Number>(matrix.d);
274   pArray->AddNew<CPDF_Number>(matrix.e);
275   pArray->AddNew<CPDF_Number>(matrix.f);
276 }
277 
MaybeIntern(const ByteString & str)278 ByteString CPDF_Dictionary::MaybeIntern(const ByteString& str) {
279   return m_pPool ? m_pPool->Intern(str) : str;
280 }
281 
WriteTo(IFX_ArchiveStream * archive,const CPDF_Encryptor * encryptor) const282 bool CPDF_Dictionary::WriteTo(IFX_ArchiveStream* archive,
283                               const CPDF_Encryptor* encryptor) const {
284   if (!archive->WriteString("<<"))
285     return false;
286 
287   const bool is_signature = CPDF_CryptoHandler::IsSignatureDictionary(this);
288 
289   CPDF_DictionaryLocker locker(this);
290   for (const auto& it : locker) {
291     const ByteString& key = it.first;
292     CPDF_Object* pValue = it.second.Get();
293     if (!archive->WriteString("/") ||
294         !archive->WriteString(PDF_NameEncode(key).AsStringView())) {
295       return false;
296     }
297     if (!pValue->WriteTo(archive, !is_signature || key != "Contents"
298                                       ? encryptor
299                                       : nullptr)) {
300       return false;
301     }
302   }
303   return archive->WriteString(">>");
304 }
305 
CPDF_DictionaryLocker(const CPDF_Dictionary * pDictionary)306 CPDF_DictionaryLocker::CPDF_DictionaryLocker(const CPDF_Dictionary* pDictionary)
307     : m_pDictionary(pDictionary) {
308   m_pDictionary->m_LockCount++;
309 }
310 
~CPDF_DictionaryLocker()311 CPDF_DictionaryLocker::~CPDF_DictionaryLocker() {
312   m_pDictionary->m_LockCount--;
313 }
314