7#include "core/fpdfapi/parser/cpdf_dictionary.h"
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/check.h"
23#include "third_party/base/containers/contains.h"
25CPDF_Dictionary::CPDF_Dictionary()
26 : CPDF_Dictionary(WeakPtr<ByteStringPool>()) {}
28CPDF_Dictionary::CPDF_Dictionary(
const WeakPtr<ByteStringPool>& pPool)
35 for (
auto& it : m_Map) {
36 if (it.second->GetObjNum() == kInvalidObjNum)
55 std::set<
const CPDF_Object*>* pVisited)
const {
56 pVisited->insert(
this);
57 auto pCopy = pdfium::MakeRetain<CPDF_Dictionary>(m_pPool);
59 for (
const auto& it : locker) {
60 if (!pdfium::Contains(*pVisited, it.second.Get())) {
61 std::set<
const CPDF_Object*> visited(*pVisited);
62 auto obj = it.second->CloneNonCyclic(bDirect, &visited);
64 pCopy->m_Map.insert(std::make_pair(it.first, std::move(obj)));
70const CPDF_Object* CPDF_Dictionary::GetObjectForInternal(
71 const ByteString& key)
const {
72 auto it = m_Map.find(key);
73 return it != m_Map.end() ? it->second.Get() :
nullptr;
77 const ByteString& key)
const {
78 return pdfium::WrapRetain(GetObjectForInternal(key));
82 const ByteString& key) {
83 return pdfium::WrapRetain(
84 const_cast<
CPDF_Object*>(GetObjectForInternal(key)));
87const CPDF_Object* CPDF_Dictionary::GetDirectObjectForInternal(
88 const ByteString& key)
const {
94 const ByteString& key)
const {
95 return pdfium::WrapRetain(GetDirectObjectForInternal(key));
99 const ByteString& key) {
100 return pdfium::WrapRetain(
101 const_cast<
CPDF_Object*>(GetDirectObjectForInternal(key)));
110 const ByteString& def)
const {
122ByteString CPDF_Dictionary::
GetNameFor(
const ByteString& key)
const {
123 const CPDF_Name* p =
ToName(GetObjectForInternal(key)
);
128 bool bDefault)
const {
144 const CPDF_Number* p =
ToNumber(GetObjectForInternal(key)
);
157const CPDF_Dictionary* CPDF_Dictionary::GetDictForInternal(
158 const ByteString& key)
const {
159 const CPDF_Object* p = GetDirectObjectForInternal(key);
164 const ByteString& key)
const {
165 return pdfium::WrapRetain(GetDictForInternal(key));
169 const ByteString& key) {
170 return pdfium::WrapRetain(
171 const_cast<CPDF_Dictionary*>(GetDictForInternal(key)));
175 const ByteString& key) {
176 RetainPtr<CPDF_Dictionary> result = GetMutableDictFor(key);
179 return SetNewFor<CPDF_Dictionary>(key);
182const CPDF_Array* CPDF_Dictionary::GetArrayForInternal(
183 const ByteString& key)
const {
184 return ToArray(GetDirectObjectForInternal(key)
);
188 const ByteString& key)
const {
189 return pdfium::WrapRetain(GetArrayForInternal(key));
193 const ByteString& key) {
194 return pdfium::WrapRetain(
const_cast<CPDF_Array*>(GetArrayForInternal(key)));
198 const ByteString& key) {
199 RetainPtr<CPDF_Array> result = GetMutableArrayFor(key);
202 return SetNewFor<CPDF_Array>(key);
205const CPDF_Stream* CPDF_Dictionary::GetStreamForInternal(
206 const ByteString& key)
const {
207 return ToStream(GetDirectObjectForInternal(key)
);
211 const ByteString& key)
const {
212 return pdfium::WrapRetain(GetStreamForInternal(key));
216 const ByteString& key) {
217 return pdfium::WrapRetain(
218 const_cast<CPDF_Stream*>(GetStreamForInternal(key)));
221const CPDF_Number* CPDF_Dictionary::GetNumberForInternal(
222 const ByteString& key)
const {
227 const ByteString& key)
const {
228 return pdfium::WrapRetain(GetNumberForInternal(key));
231const CPDF_String* CPDF_Dictionary::GetStringForInternal(
232 const ByteString& key)
const {
237 const ByteString& key)
const {
238 return pdfium::WrapRetain(GetStringForInternal(key));
242 const CPDF_Array* pArray = GetArrayForInternal(key);
249 const CPDF_Array* pArray = GetArrayForInternal(key);
255bool CPDF_Dictionary::
KeyExist(
const ByteString& key)
const {
256 return pdfium::Contains(m_Map, key);
260 std::vector<ByteString> result;
262 for (
const auto& item : locker)
263 result.push_back(item.first);
267void CPDF_Dictionary::
SetFor(
const ByteString& key,
269 (
void)SetForInternal(key,
std::move(object));
272CPDF_Object* CPDF_Dictionary::SetForInternal(
const ByteString& key,
279 CHECK(pObj->IsInline());
280 CHECK(!pObj->IsStream());
282 m_Map[MaybeIntern(key)] = std::move(pObj);
287 const ByteString& key,
290 auto it = m_Map.find(key);
291 if (it == m_Map.end() || it->second->IsReference())
295 it->second = it->second->MakeReference(pHolder);
301 auto it = m_Map.find(key);
302 if (it != m_Map.end()) {
303 result =
std::move(it->second);
310 const ByteString& newkey) {
312 auto old_it = m_Map.find(oldkey);
313 if (old_it == m_Map.end())
316 auto new_it = m_Map.find(newkey);
317 if (new_it == old_it)
320 m_Map[MaybeIntern(newkey)] = std::move(old_it->second);
326 auto pArray = SetNewFor<CPDF_Array>(key);
327 pArray->AppendNew<CPDF_Number>(rect
.left);
328 pArray->AppendNew<CPDF_Number>(rect
.bottom);
329 pArray->AppendNew<CPDF_Number>(rect
.right);
330 pArray->AppendNew<CPDF_Number>(rect
.top);
335 auto pArray = SetNewFor<CPDF_Array>(key);
336 pArray->AppendNew<CPDF_Number>(matrix
.a);
337 pArray->AppendNew<CPDF_Number>(matrix
.b);
338 pArray->AppendNew<CPDF_Number>(matrix
.c);
339 pArray->AppendNew<CPDF_Number>(matrix
.d);
340 pArray->AppendNew<CPDF_Number>(matrix
.e);
341 pArray->AppendNew<CPDF_Number>(matrix
.f);
344ByteString CPDF_Dictionary::MaybeIntern(
const ByteString& str) {
345 return m_pPool ? m_pPool->Intern(str) : str;
356 for (
const auto& it : locker) {
357 const ByteString& key = it.first;
358 const RetainPtr<CPDF_Object>& pValue = it.second;
359 if (!archive->WriteString(
"/") ||
360 !archive->WriteString(PDF_NameEncode(key).AsStringView())) {
363 if (!pValue->WriteTo(archive, !is_signature || key !=
"Contents"
374 m_pDictionary->m_LockCount++;
380 m_pDictionary->m_LockCount++;
384 RetainPtr<
const CPDF_Dictionary> pDictionary)
386 m_pDictionary->m_LockCount++;
390 m_pDictionary->m_LockCount--;
constexpr CFX_FloatRect()=default
CFX_Matrix GetMatrix() const
CFX_FloatRect GetRect() const
static bool IsSignatureDictionary(const CPDF_Dictionary *dictionary)
CPDF_DictionaryLocker(const CPDF_Dictionary *pDictionary)
CPDF_DictionaryLocker(RetainPtr< const CPDF_Dictionary > pDictionary)
CPDF_DictionaryLocker(RetainPtr< CPDF_Dictionary > pDictionary)
Type GetType() const override
bool KeyExist(const ByteString &key) const
int GetIntegerFor(const ByteString &key) const
RetainPtr< const CPDF_Stream > GetStreamFor(const ByteString &key) const
RetainPtr< const CPDF_Number > GetNumberFor(const ByteString &key) const
void SetFor(const ByteString &key, RetainPtr< CPDF_Object > object)
ByteString GetByteStringFor(const ByteString &key, const ByteString &default_str) const
RetainPtr< CPDF_Object > RemoveFor(ByteStringView key)
RetainPtr< CPDF_Array > GetOrCreateArrayFor(const ByteString &key)
void SetMatrixFor(const ByteString &key, const CFX_Matrix &matrix)
RetainPtr< CPDF_Stream > GetMutableStreamFor(const ByteString &key)
RetainPtr< CPDF_Object > GetMutableDirectObjectFor(const ByteString &key)
RetainPtr< const CPDF_Dictionary > GetDictFor(const ByteString &key) const
RetainPtr< CPDF_Array > GetMutableArrayFor(const ByteString &key)
float GetFloatFor(const ByteString &key) const
RetainPtr< CPDF_Dictionary > GetOrCreateDictFor(const ByteString &key)
void ConvertToIndirectObjectFor(const ByteString &key, CPDF_IndirectObjectHolder *pHolder)
WideString GetUnicodeTextFor(const ByteString &key) const
RetainPtr< CPDF_Object > Clone() const override
const CPDF_Dictionary * GetDictInternal() const override
RetainPtr< CPDF_Object > CloneNonCyclic(bool bDirect, std::set< const CPDF_Object * > *visited) const override
CPDF_Dictionary * AsMutableDictionary() override
std::vector< ByteString > GetKeys() const
bool GetBooleanFor(const ByteString &key, bool bDefault) const
ByteString GetByteStringFor(const ByteString &key) const
int GetDirectIntegerFor(const ByteString &key) const
RetainPtr< const CPDF_String > GetStringFor(const ByteString &key) const
void SetRectFor(const ByteString &key, const CFX_FloatRect &rect)
RetainPtr< CPDF_Dictionary > GetMutableDictFor(const ByteString &key)
RetainPtr< const CPDF_Object > GetDirectObjectFor(const ByteString &key) const
bool WriteTo(IFX_ArchiveStream *archive, const CPDF_Encryptor *encryptor) const override
RetainPtr< const CPDF_Array > GetArrayFor(const ByteString &key) const
RetainPtr< const CPDF_Object > GetObjectFor(const ByteString &key) const
int GetIntegerFor(const ByteString &key, int default_int) const
~CPDF_Dictionary() override
CFX_FloatRect GetRectFor(const ByteString &key) const
ByteString GetNameFor(const ByteString &key) const
CFX_Matrix GetMatrixFor(const ByteString &key) const
RetainPtr< CPDF_Object > GetMutableObjectFor(const ByteString &key)
void ReplaceKey(const ByteString &oldkey, const ByteString &newkey)
uint32_t AddIndirectObject(RetainPtr< CPDF_Object > pObj)
ByteString GetString() const override
int GetInteger() const override
virtual WideString GetUnicodeText() const
virtual ByteString GetString() const
virtual int GetInteger() const
virtual float GetNumber() const
static constexpr uint32_t kInvalidObjNum
virtual const CPDF_Object * GetDirectInternal() const
RetainPtr< CPDF_Object > CloneObjectNonCyclic(bool bDirect) const
virtual const CPDF_Dictionary * GetDictInternal() const
const CPDF_Object * GetDirectInternal() const override
bool WriteString(ByteStringView str)
ByteString(const ByteString &other)
const CPDF_Array * ToArray(const CPDF_Object *obj)
const CPDF_Boolean * ToBoolean(const CPDF_Object *obj)
const CPDF_Name * ToName(const CPDF_Object *obj)
const CPDF_Number * ToNumber(const CPDF_Object *obj)
const CPDF_Reference * ToReference(const CPDF_Object *obj)
const CPDF_Stream * ToStream(const CPDF_Object *obj)
const CPDF_String * ToString(const CPDF_Object *obj)