7#include "core/fpdfapi/parser/cpdf_array.h"
12#include "core/fpdfapi/parser/cpdf_boolean.h"
13#include "core/fpdfapi/parser/cpdf_dictionary.h"
14#include "core/fpdfapi/parser/cpdf_name.h"
15#include "core/fpdfapi/parser/cpdf_number.h"
16#include "core/fpdfapi/parser/cpdf_reference.h"
17#include "core/fpdfapi/parser/cpdf_stream.h"
18#include "core/fpdfapi/parser/cpdf_string.h"
19#include "core/fxcrt/fx_stream.h"
20#include "third_party/base/check.h"
21#include "third_party/base/containers/contains.h"
22#include "third_party/base/notreached.h"
24CPDF_Array::CPDF_Array() =
default;
26CPDF_Array::CPDF_Array(
const WeakPtr<ByteStringPool>& pPool) : m_pPool(pPool) {}
31 for (
auto& it : m_Objects) {
32 if (it->GetObjNum() == kInvalidObjNum)
51 std::set<
const CPDF_Object*>* pVisited)
const {
52 pVisited->insert(
this);
53 auto pCopy = pdfium::MakeRetain<CPDF_Array>();
54 for (
const auto& pValue : m_Objects) {
55 if (!pdfium::Contains(*pVisited, pValue.Get())) {
56 std::set<
const CPDF_Object*> visited(*pVisited);
57 if (
auto obj = pValue->CloneNonCyclic(bDirect, &visited))
58 pCopy->m_Objects.push_back(std::move(obj));
66 if (m_Objects.size() != 4)
69 rect.left = GetFloatAt(0);
70 rect.bottom = GetFloatAt(1);
71 rect.right = GetFloatAt(2);
72 rect.top = GetFloatAt(3);
77 if (m_Objects.size() != 6)
80 return CFX_Matrix(GetFloatAt(0), GetFloatAt(1), GetFloatAt(2), GetFloatAt(3),
81 GetFloatAt(4), GetFloatAt(5));
85 for (size_t i = 0; i < size(); ++i) {
86 if (GetDirectObjectAt(i) == pThat)
93 return Find(pThat).has_value();
96CPDF_Object* CPDF_Array::GetMutableObjectAtInternal(size_t index) {
97 return index < m_Objects.size() ? m_Objects[index].Get() :
nullptr;
100const CPDF_Object* CPDF_Array::GetObjectAtInternal(size_t index)
const {
101 return const_cast<CPDF_Array*>(
this)->GetMutableObjectAtInternal(index);
105 return pdfium::WrapRetain(GetMutableObjectAtInternal(index));
109 return pdfium::WrapRetain(GetObjectAtInternal(index));
113 return const_cast<CPDF_Array*>(
this)->GetMutableDirectObjectAt(index);
118 return pObj ? pObj->GetMutableDirect() :
nullptr;
122 if (index >= m_Objects.size())
124 return m_Objects[index]->GetString();
128 if (index >= m_Objects.size())
130 return m_Objects[index]->GetUnicodeText();
134 if (index >= m_Objects.size())
141 if (index >= m_Objects.size())
143 return m_Objects[index]->GetInteger();
147 if (index >= m_Objects.size())
149 return m_Objects[index]->GetNumber();
156 CPDF_Dictionary* pDict = p->AsMutableDictionary();
158 return pdfium::WrapRetain(pDict);
159 CPDF_Stream* pStream = p->AsMutableStream();
166 return const_cast<CPDF_Array*>(
this)->GetMutableDictAt(index);
170 return ToStream(GetMutableDirectObjectAt(index));
174 return const_cast<CPDF_Array*>(
this)->GetMutableStreamAt(index);
178 return ToArray(GetMutableDirectObjectAt(index));
182 return const_cast<CPDF_Array*>(
this)->GetMutableArrayAt(index);
186 return ToNumber(GetObjectAt(index));
190 return ToString(GetObjectAt(index));
200 if (index < m_Objects.size())
201 m_Objects.erase(m_Objects.begin() + index);
207 if (index >= m_Objects.size())
210 if (!m_Objects[index] || m_Objects[index]->IsReference())
213 pHolder->AddIndirectObject(m_Objects[index]);
214 m_Objects[index] = m_Objects[index]->MakeReference(pHolder);
218 (
void)SetAtInternal(index, std::move(object));
222 (
void)InsertAtInternal(index, std::move(object));
226 (
void)AppendInternal(
std::move(object));
229CPDF_Object* CPDF_Array::SetAtInternal(size_t index,
233 CHECK(pObj->IsInline());
234 if (index >= m_Objects.size())
238 m_Objects[index] = std::move(pObj);
242CPDF_Object* CPDF_Array::InsertAtInternal(size_t index,
246 CHECK(pObj->IsInline());
247 if (index > m_Objects.size())
251 m_Objects.insert(m_Objects.begin() + index, std::move(pObj));
258 CHECK(pObj->IsInline());
260 m_Objects.push_back(std::move(pObj));
269 for (size_t i = 0; i < size(); ++i) {
270 if (!GetObjectAt(i)->WriteTo(archive, encryptor))
278 m_pArray->m_LockCount++;
283 m_pArray->m_LockCount++;
288 m_pArray->m_LockCount++;
292 m_pArray->m_LockCount--;
CPDF_ArrayLocker(RetainPtr< CPDF_Array > pArray)
CPDF_ArrayLocker(const CPDF_Array *pArray)
CPDF_ArrayLocker(RetainPtr< const CPDF_Array > pArray)
bool GetBooleanAt(size_t index, bool bDefault) const
void SetAt(size_t index, RetainPtr< CPDF_Object > object)
RetainPtr< CPDF_Dictionary > GetMutableDictAt(size_t index)
RetainPtr< CPDF_Object > CloneNonCyclic(bool bDirect, std::set< const CPDF_Object * > *pVisited) const override
RetainPtr< const CPDF_Object > GetDirectObjectAt(size_t index) const
RetainPtr< const CPDF_Stream > GetStreamAt(size_t index) const
Type GetType() const override
absl::optional< size_t > Find(const CPDF_Object *pThat) const
CPDF_Array * AsMutableArray() override
RetainPtr< const CPDF_Object > GetObjectAt(size_t index) const
RetainPtr< CPDF_Object > GetMutableObjectAt(size_t index)
WideString GetUnicodeTextAt(size_t index) const
RetainPtr< CPDF_Array > GetMutableArrayAt(size_t index)
RetainPtr< CPDF_Object > Clone() const override
RetainPtr< const CPDF_Dictionary > GetDictAt(size_t index) const
RetainPtr< CPDF_Stream > GetMutableStreamAt(size_t index)
bool Contains(const CPDF_Object *pThat) const
void ConvertToIndirectObjectAt(size_t index, CPDF_IndirectObjectHolder *pHolder)
bool WriteTo(IFX_ArchiveStream *archive, const CPDF_Encryptor *encryptor) const override
RetainPtr< CPDF_Object > GetMutableDirectObjectAt(size_t index)
CFX_Matrix GetMatrix() const
int GetIntegerAt(size_t index) const
void InsertAt(size_t index, RetainPtr< CPDF_Object > object)
ByteString GetByteStringAt(size_t index) const
void Append(RetainPtr< CPDF_Object > object)
RetainPtr< const CPDF_String > GetStringAt(size_t index) const
RetainPtr< const CPDF_Array > GetArrayAt(size_t index) const
RetainPtr< const CPDF_Number > GetNumberAt(size_t index) const
CFX_FloatRect GetRect() const
void RemoveAt(size_t index)
float GetFloatAt(size_t index) const
virtual int GetInteger() const
static constexpr uint32_t kInvalidObjNum
RetainPtr< CPDF_Dictionary > GetMutableDict()
RetainPtr< CPDF_Object > CloneObjectNonCyclic(bool bDirect) const
bool WriteString(ByteStringView str)
const CPDF_Boolean * ToBoolean(const CPDF_Object *obj)