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 #ifndef CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_
9
10 #include <stddef.h>
11
12 #include <set>
13 #include <type_traits>
14 #include <utility>
15 #include <vector>
16
17 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
18 #include "core/fpdfapi/parser/cpdf_object.h"
19 #include "core/fxcrt/fx_coordinates.h"
20 #include "core/fxcrt/retain_ptr.h"
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22 #include "third_party/base/check.h"
23
24 // Arrays never contain nullptrs for objects within bounds, but some of the
25 // methods will tolerate out-of-bounds indices and return nullptr for those
26 // cases.
27 class CPDF_Array final : public CPDF_Object {
28 public:
29 using const_iterator = std::vector<RetainPtr<CPDF_Object>>::const_iterator;
30
31 CONSTRUCT_VIA_MAKE_RETAIN;
32
33 // CPDF_Object:
34 Type GetType() const override;
35 RetainPtr<CPDF_Object> Clone() const override;
36 CPDF_Array* AsMutableArray() override;
37 bool WriteTo(IFX_ArchiveStream* archive,
38 const CPDF_Encryptor* encryptor) const override;
39
IsEmpty()40 bool IsEmpty() const { return m_Objects.empty(); }
size()41 size_t size() const { return m_Objects.size(); }
42
43 // The Get*ObjectAt() methods tolerate out-of-bounds indices and return
44 // nullptr in those cases. Otherwise, for in-bound indices, the result
45 // is never nullptr.
46 RetainPtr<CPDF_Object> GetMutableObjectAt(size_t index);
47 RetainPtr<const CPDF_Object> GetObjectAt(size_t index) const;
48
49 // The Get*DirectObjectAt() methods tolerate out-of-bounds indices and
50 // return nullptr in those cases. Furthermore, for reference objects that
51 // do not correspond to a valid indirect object, nullptr is returned.
52 RetainPtr<CPDF_Object> GetMutableDirectObjectAt(size_t index);
53 RetainPtr<const CPDF_Object> GetDirectObjectAt(size_t index) const;
54
55 // The Get*At() methods tolerate out-of-bounds indices and return nullptr
56 // in those cases. Furthermore, these safely coerce to the sub-class,
57 // returning nullptr if the object at the location is of a different type.
58 ByteString GetByteStringAt(size_t index) const;
59 WideString GetUnicodeTextAt(size_t index) const;
60 bool GetBooleanAt(size_t index, bool bDefault) const;
61 int GetIntegerAt(size_t index) const;
62 float GetFloatAt(size_t index) const;
63 RetainPtr<CPDF_Dictionary> GetMutableDictAt(size_t index);
64 RetainPtr<const CPDF_Dictionary> GetDictAt(size_t index) const;
65 RetainPtr<CPDF_Stream> GetMutableStreamAt(size_t index);
66 RetainPtr<const CPDF_Stream> GetStreamAt(size_t index) const;
67 RetainPtr<CPDF_Array> GetMutableArrayAt(size_t index);
68 RetainPtr<const CPDF_Array> GetArrayAt(size_t index) const;
69 RetainPtr<const CPDF_Number> GetNumberAt(size_t index) const;
70 RetainPtr<const CPDF_String> GetStringAt(size_t index) const;
71
72 CFX_FloatRect GetRect() const;
73 CFX_Matrix GetMatrix() const;
74
75 absl::optional<size_t> Find(const CPDF_Object* pThat) const;
76 bool Contains(const CPDF_Object* pThat) const;
77
78 // Creates object owned by the array, and returns a retained pointer to it.
79 // We have special cases for objects that can intern strings from
80 // a ByteStringPool. Prefer using these templates over direct calls
81 // to Append()/SetAt()/InsertAt() since by creating a new object with no
82 // previous references, they ensure cycles can not be introduced.
83 template <typename T, typename... Args>
84 typename std::enable_if<!CanInternStrings<T>::value, RetainPtr<T>>::type
AppendNew(Args &&...args)85 AppendNew(Args&&... args) {
86 return pdfium::WrapRetain(static_cast<T*>(
87 AppendInternal(pdfium::MakeRetain<T>(std::forward<Args>(args)...))));
88 }
89 template <typename T, typename... Args>
90 typename std::enable_if<CanInternStrings<T>::value, RetainPtr<T>>::type
AppendNew(Args &&...args)91 AppendNew(Args&&... args) {
92 return pdfium::WrapRetain(static_cast<T*>(AppendInternal(
93 pdfium::MakeRetain<T>(m_pPool, std::forward<Args>(args)...))));
94 }
95 template <typename T, typename... Args>
96 typename std::enable_if<!CanInternStrings<T>::value, RetainPtr<T>>::type
SetNewAt(size_t index,Args &&...args)97 SetNewAt(size_t index, Args&&... args) {
98 return pdfium::WrapRetain(static_cast<T*>(SetAtInternal(
99 index, pdfium::MakeRetain<T>(std::forward<Args>(args)...))));
100 }
101 template <typename T, typename... Args>
102 typename std::enable_if<CanInternStrings<T>::value, RetainPtr<T>>::type
SetNewAt(size_t index,Args &&...args)103 SetNewAt(size_t index, Args&&... args) {
104 return pdfium::WrapRetain(static_cast<T*>(SetAtInternal(
105 index, pdfium::MakeRetain<T>(m_pPool, std::forward<Args>(args)...))));
106 }
107 template <typename T, typename... Args>
108 typename std::enable_if<!CanInternStrings<T>::value, RetainPtr<T>>::type
InsertNewAt(size_t index,Args &&...args)109 InsertNewAt(size_t index, Args&&... args) {
110 return pdfium::WrapRetain(static_cast<T*>(InsertAtInternal(
111 index, pdfium::MakeRetain<T>(std::forward<Args>(args)...))));
112 }
113 template <typename T, typename... Args>
114 typename std::enable_if<CanInternStrings<T>::value, RetainPtr<T>>::type
InsertNewAt(size_t index,Args &&...args)115 InsertNewAt(size_t index, Args&&... args) {
116 return pdfium::WrapRetain(static_cast<T*>(InsertAtInternal(
117 index, pdfium::MakeRetain<T>(m_pPool, std::forward<Args>(args)...))));
118 }
119
120 // Adds non-null `pObj` to the end of the array, growing as appropriate.
121 void Append(RetainPtr<CPDF_Object> pObj);
122
123 // Overwrites the object at `index` with non-null `pObj`, if it is
124 // in bounds. Otherwise, `index` is out of bounds, and `pObj` is
125 // not stored.
126 void SetAt(size_t index, RetainPtr<CPDF_Object> pObj);
127
128 // Inserts non-null `pObj` at `index` and shifts by one position all of the
129 // objects beyond it like std::vector::insert(), if `index` is less than or
130 // equal to the current array size. Otherwise, `index` is out of bounds,
131 // and `pObj` is not stored.
132 void InsertAt(size_t index, RetainPtr<CPDF_Object> pObj);
133
134 void Clear();
135 void RemoveAt(size_t index);
136 void ConvertToIndirectObjectAt(size_t index,
137 CPDF_IndirectObjectHolder* pHolder);
IsLocked()138 bool IsLocked() const { return !!m_LockCount; }
139
140 private:
141 friend class CPDF_ArrayLocker;
142
143 CPDF_Array();
144 explicit CPDF_Array(const WeakPtr<ByteStringPool>& pPool);
145 ~CPDF_Array() override;
146
147 // No guarantees about result lifetime, use with caution.
148 const CPDF_Object* GetObjectAtInternal(size_t index) const;
149 CPDF_Object* GetMutableObjectAtInternal(size_t index);
150 CPDF_Object* AppendInternal(RetainPtr<CPDF_Object> pObj);
151 CPDF_Object* SetAtInternal(size_t index, RetainPtr<CPDF_Object> pObj);
152 CPDF_Object* InsertAtInternal(size_t index, RetainPtr<CPDF_Object> pObj);
153
154 RetainPtr<CPDF_Object> CloneNonCyclic(
155 bool bDirect,
156 std::set<const CPDF_Object*>* pVisited) const override;
157
158 std::vector<RetainPtr<CPDF_Object>> m_Objects;
159 WeakPtr<ByteStringPool> m_pPool;
160 mutable uint32_t m_LockCount = 0;
161 };
162
163 class CPDF_ArrayLocker {
164 public:
165 FX_STACK_ALLOCATED();
166 using const_iterator = CPDF_Array::const_iterator;
167
168 explicit CPDF_ArrayLocker(const CPDF_Array* pArray);
169 explicit CPDF_ArrayLocker(RetainPtr<CPDF_Array> pArray);
170 explicit CPDF_ArrayLocker(RetainPtr<const CPDF_Array> pArray);
171 ~CPDF_ArrayLocker();
172
begin()173 const_iterator begin() const {
174 CHECK(m_pArray->IsLocked());
175 return m_pArray->m_Objects.begin();
176 }
end()177 const_iterator end() const {
178 CHECK(m_pArray->IsLocked());
179 return m_pArray->m_Objects.end();
180 }
181
182 private:
183 RetainPtr<const CPDF_Array> const m_pArray;
184 };
185
ToArray(CPDF_Object * obj)186 inline CPDF_Array* ToArray(CPDF_Object* obj) {
187 return obj ? obj->AsMutableArray() : nullptr;
188 }
189
ToArray(const CPDF_Object * obj)190 inline const CPDF_Array* ToArray(const CPDF_Object* obj) {
191 return obj ? obj->AsArray() : nullptr;
192 }
193
ToArray(RetainPtr<CPDF_Object> obj)194 inline RetainPtr<CPDF_Array> ToArray(RetainPtr<CPDF_Object> obj) {
195 return RetainPtr<CPDF_Array>(ToArray(obj.Get()));
196 }
197
ToArray(RetainPtr<const CPDF_Object> obj)198 inline RetainPtr<const CPDF_Array> ToArray(RetainPtr<const CPDF_Object> obj) {
199 return RetainPtr<const CPDF_Array>(ToArray(obj.Get()));
200 }
201
202 #endif // CORE_FPDFAPI_PARSER_CPDF_ARRAY_H_
203