1 // Copyright 2016 The PDFium Authors
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_array.h"
8
9 #include <set>
10 #include <utility>
11
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"
23
24 CPDF_Array::CPDF_Array() = default;
25
CPDF_Array(const WeakPtr<ByteStringPool> & pPool)26 CPDF_Array::CPDF_Array(const WeakPtr<ByteStringPool>& pPool) : m_pPool(pPool) {}
27
~CPDF_Array()28 CPDF_Array::~CPDF_Array() {
29 // Break cycles for cyclic references.
30 m_ObjNum = kInvalidObjNum;
31 for (auto& it : m_Objects) {
32 if (it->GetObjNum() == kInvalidObjNum)
33 it.Leak();
34 }
35 }
36
GetType() const37 CPDF_Object::Type CPDF_Array::GetType() const {
38 return kArray;
39 }
40
AsMutableArray()41 CPDF_Array* CPDF_Array::AsMutableArray() {
42 return this;
43 }
44
Clone() const45 RetainPtr<CPDF_Object> CPDF_Array::Clone() const {
46 return CloneObjectNonCyclic(false);
47 }
48
CloneNonCyclic(bool bDirect,std::set<const CPDF_Object * > * pVisited) const49 RetainPtr<CPDF_Object> CPDF_Array::CloneNonCyclic(
50 bool bDirect,
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));
59 }
60 }
61 return pCopy;
62 }
63
GetRect() const64 CFX_FloatRect CPDF_Array::GetRect() const {
65 CFX_FloatRect rect;
66 if (m_Objects.size() != 4)
67 return rect;
68
69 rect.left = GetFloatAt(0);
70 rect.bottom = GetFloatAt(1);
71 rect.right = GetFloatAt(2);
72 rect.top = GetFloatAt(3);
73 return rect;
74 }
75
GetMatrix() const76 CFX_Matrix CPDF_Array::GetMatrix() const {
77 if (m_Objects.size() != 6)
78 return CFX_Matrix();
79
80 return CFX_Matrix(GetFloatAt(0), GetFloatAt(1), GetFloatAt(2), GetFloatAt(3),
81 GetFloatAt(4), GetFloatAt(5));
82 }
83
Find(const CPDF_Object * pThat) const84 absl::optional<size_t> CPDF_Array::Find(const CPDF_Object* pThat) const {
85 for (size_t i = 0; i < size(); ++i) {
86 if (GetDirectObjectAt(i) == pThat)
87 return i;
88 }
89 return absl::nullopt;
90 }
91
Contains(const CPDF_Object * pThat) const92 bool CPDF_Array::Contains(const CPDF_Object* pThat) const {
93 return Find(pThat).has_value();
94 }
95
GetMutableObjectAtInternal(size_t index)96 CPDF_Object* CPDF_Array::GetMutableObjectAtInternal(size_t index) {
97 return index < m_Objects.size() ? m_Objects[index].Get() : nullptr;
98 }
99
GetObjectAtInternal(size_t index) const100 const CPDF_Object* CPDF_Array::GetObjectAtInternal(size_t index) const {
101 return const_cast<CPDF_Array*>(this)->GetMutableObjectAtInternal(index);
102 }
103
GetMutableObjectAt(size_t index)104 RetainPtr<CPDF_Object> CPDF_Array::GetMutableObjectAt(size_t index) {
105 return pdfium::WrapRetain(GetMutableObjectAtInternal(index));
106 }
107
GetObjectAt(size_t index) const108 RetainPtr<const CPDF_Object> CPDF_Array::GetObjectAt(size_t index) const {
109 return pdfium::WrapRetain(GetObjectAtInternal(index));
110 }
111
GetDirectObjectAt(size_t index) const112 RetainPtr<const CPDF_Object> CPDF_Array::GetDirectObjectAt(size_t index) const {
113 return const_cast<CPDF_Array*>(this)->GetMutableDirectObjectAt(index);
114 }
115
GetMutableDirectObjectAt(size_t index)116 RetainPtr<CPDF_Object> CPDF_Array::GetMutableDirectObjectAt(size_t index) {
117 RetainPtr<CPDF_Object> pObj = GetMutableObjectAt(index);
118 return pObj ? pObj->GetMutableDirect() : nullptr;
119 }
120
GetByteStringAt(size_t index) const121 ByteString CPDF_Array::GetByteStringAt(size_t index) const {
122 if (index >= m_Objects.size())
123 return ByteString();
124 return m_Objects[index]->GetString();
125 }
126
GetUnicodeTextAt(size_t index) const127 WideString CPDF_Array::GetUnicodeTextAt(size_t index) const {
128 if (index >= m_Objects.size())
129 return WideString();
130 return m_Objects[index]->GetUnicodeText();
131 }
132
GetBooleanAt(size_t index,bool bDefault) const133 bool CPDF_Array::GetBooleanAt(size_t index, bool bDefault) const {
134 if (index >= m_Objects.size())
135 return bDefault;
136 const CPDF_Object* p = m_Objects[index].Get();
137 return ToBoolean(p) ? p->GetInteger() != 0 : bDefault;
138 }
139
GetIntegerAt(size_t index) const140 int CPDF_Array::GetIntegerAt(size_t index) const {
141 if (index >= m_Objects.size())
142 return 0;
143 return m_Objects[index]->GetInteger();
144 }
145
GetFloatAt(size_t index) const146 float CPDF_Array::GetFloatAt(size_t index) const {
147 if (index >= m_Objects.size())
148 return 0;
149 return m_Objects[index]->GetNumber();
150 }
151
GetMutableDictAt(size_t index)152 RetainPtr<CPDF_Dictionary> CPDF_Array::GetMutableDictAt(size_t index) {
153 RetainPtr<CPDF_Object> p = GetMutableDirectObjectAt(index);
154 if (!p)
155 return nullptr;
156 CPDF_Dictionary* pDict = p->AsMutableDictionary();
157 if (pDict)
158 return pdfium::WrapRetain(pDict);
159 CPDF_Stream* pStream = p->AsMutableStream();
160 if (pStream)
161 return pStream->GetMutableDict();
162 return nullptr;
163 }
164
GetDictAt(size_t index) const165 RetainPtr<const CPDF_Dictionary> CPDF_Array::GetDictAt(size_t index) const {
166 return const_cast<CPDF_Array*>(this)->GetMutableDictAt(index);
167 }
168
GetMutableStreamAt(size_t index)169 RetainPtr<CPDF_Stream> CPDF_Array::GetMutableStreamAt(size_t index) {
170 return ToStream(GetMutableDirectObjectAt(index));
171 }
172
GetStreamAt(size_t index) const173 RetainPtr<const CPDF_Stream> CPDF_Array::GetStreamAt(size_t index) const {
174 return const_cast<CPDF_Array*>(this)->GetMutableStreamAt(index);
175 }
176
GetMutableArrayAt(size_t index)177 RetainPtr<CPDF_Array> CPDF_Array::GetMutableArrayAt(size_t index) {
178 return ToArray(GetMutableDirectObjectAt(index));
179 }
180
GetArrayAt(size_t index) const181 RetainPtr<const CPDF_Array> CPDF_Array::GetArrayAt(size_t index) const {
182 return const_cast<CPDF_Array*>(this)->GetMutableArrayAt(index);
183 }
184
GetNumberAt(size_t index) const185 RetainPtr<const CPDF_Number> CPDF_Array::GetNumberAt(size_t index) const {
186 return ToNumber(GetObjectAt(index));
187 }
188
GetStringAt(size_t index) const189 RetainPtr<const CPDF_String> CPDF_Array::GetStringAt(size_t index) const {
190 return ToString(GetObjectAt(index));
191 }
192
Clear()193 void CPDF_Array::Clear() {
194 CHECK(!IsLocked());
195 m_Objects.clear();
196 }
197
RemoveAt(size_t index)198 void CPDF_Array::RemoveAt(size_t index) {
199 CHECK(!IsLocked());
200 if (index < m_Objects.size())
201 m_Objects.erase(m_Objects.begin() + index);
202 }
203
ConvertToIndirectObjectAt(size_t index,CPDF_IndirectObjectHolder * pHolder)204 void CPDF_Array::ConvertToIndirectObjectAt(size_t index,
205 CPDF_IndirectObjectHolder* pHolder) {
206 CHECK(!IsLocked());
207 if (index >= m_Objects.size())
208 return;
209
210 if (!m_Objects[index] || m_Objects[index]->IsReference())
211 return;
212
213 pHolder->AddIndirectObject(m_Objects[index]);
214 m_Objects[index] = m_Objects[index]->MakeReference(pHolder);
215 }
216
SetAt(size_t index,RetainPtr<CPDF_Object> pObj)217 void CPDF_Array::SetAt(size_t index, RetainPtr<CPDF_Object> pObj) {
218 (void)SetAtInternal(index, std::move(pObj));
219 }
220
InsertAt(size_t index,RetainPtr<CPDF_Object> pObj)221 void CPDF_Array::InsertAt(size_t index, RetainPtr<CPDF_Object> pObj) {
222 (void)InsertAtInternal(index, std::move(pObj));
223 }
224
Append(RetainPtr<CPDF_Object> pObj)225 void CPDF_Array::Append(RetainPtr<CPDF_Object> pObj) {
226 (void)AppendInternal(std::move(pObj));
227 }
228
SetAtInternal(size_t index,RetainPtr<CPDF_Object> pObj)229 CPDF_Object* CPDF_Array::SetAtInternal(size_t index,
230 RetainPtr<CPDF_Object> pObj) {
231 CHECK(!IsLocked());
232 CHECK(pObj);
233 CHECK(pObj->IsInline());
234 if (index >= m_Objects.size())
235 return nullptr;
236
237 CPDF_Object* pRet = pObj.Get();
238 m_Objects[index] = std::move(pObj);
239 return pRet;
240 }
241
InsertAtInternal(size_t index,RetainPtr<CPDF_Object> pObj)242 CPDF_Object* CPDF_Array::InsertAtInternal(size_t index,
243 RetainPtr<CPDF_Object> pObj) {
244 CHECK(!IsLocked());
245 CHECK(pObj);
246 CHECK(pObj->IsInline());
247 if (index > m_Objects.size())
248 return nullptr;
249
250 CPDF_Object* pRet = pObj.Get();
251 m_Objects.insert(m_Objects.begin() + index, std::move(pObj));
252 return pRet;
253 }
254
AppendInternal(RetainPtr<CPDF_Object> pObj)255 CPDF_Object* CPDF_Array::AppendInternal(RetainPtr<CPDF_Object> pObj) {
256 CHECK(!IsLocked());
257 CHECK(pObj);
258 CHECK(pObj->IsInline());
259 CPDF_Object* pRet = pObj.Get();
260 m_Objects.push_back(std::move(pObj));
261 return pRet;
262 }
263
WriteTo(IFX_ArchiveStream * archive,const CPDF_Encryptor * encryptor) const264 bool CPDF_Array::WriteTo(IFX_ArchiveStream* archive,
265 const CPDF_Encryptor* encryptor) const {
266 if (!archive->WriteString("["))
267 return false;
268
269 for (size_t i = 0; i < size(); ++i) {
270 if (!GetObjectAt(i)->WriteTo(archive, encryptor))
271 return false;
272 }
273 return archive->WriteString("]");
274 }
275
CPDF_ArrayLocker(const CPDF_Array * pArray)276 CPDF_ArrayLocker::CPDF_ArrayLocker(const CPDF_Array* pArray)
277 : m_pArray(pArray) {
278 m_pArray->m_LockCount++;
279 }
280
CPDF_ArrayLocker(RetainPtr<CPDF_Array> pArray)281 CPDF_ArrayLocker::CPDF_ArrayLocker(RetainPtr<CPDF_Array> pArray)
282 : m_pArray(std::move(pArray)) {
283 m_pArray->m_LockCount++;
284 }
285
CPDF_ArrayLocker(RetainPtr<const CPDF_Array> pArray)286 CPDF_ArrayLocker::CPDF_ArrayLocker(RetainPtr<const CPDF_Array> pArray)
287 : m_pArray(std::move(pArray)) {
288 m_pArray->m_LockCount++;
289 }
290
~CPDF_ArrayLocker()291 CPDF_ArrayLocker::~CPDF_ArrayLocker() {
292 m_pArray->m_LockCount--;
293 }
294