blob: c548420b996bd3af061cc365ef56f63f8117f7d5 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2018 The PDFium Authors
Dan Sinclair7aba4722018-03-28 17:04:16 +00002// 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
Dan Sinclair00d47a62018-03-28 18:39:04 +00007#include "fpdfsdk/cpdfsdk_helpers.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +00008
Tom Sepez652d35b2022-09-10 01:42:25 +00009#include <utility>
10
Lei Zhangbc106482019-05-30 23:55:19 +000011#include "build/build_config.h"
Lei Zhang865ffb12019-02-26 20:18:19 +000012#include "constants/form_fields.h"
Lei Zhang26170562018-04-17 17:01:52 +000013#include "constants/stream_dict_common.h"
Tom Sepeza1d34422018-04-24 20:54:41 +000014#include "core/fpdfapi/page/cpdf_page.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +000015#include "core/fpdfapi/parser/cpdf_array.h"
Lei Zhang81535612018-10-09 21:15:17 +000016#include "core/fpdfapi/parser/cpdf_dictionary.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +000017#include "core/fpdfapi/parser/cpdf_document.h"
Artem Strygineababa12018-06-06 12:31:18 +000018#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Gourab Kundud35a62d2020-04-24 10:29:27 +000019#include "core/fpdfapi/render/cpdf_renderoptions.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +000020#include "core/fpdfdoc/cpdf_annot.h"
Lei Zhangc3450652018-10-11 16:54:42 +000021#include "core/fpdfdoc/cpdf_interactiveform.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +000022#include "core/fpdfdoc/cpdf_metadata.h"
Tom Sepez06943a92022-11-10 20:29:34 +000023#include "core/fxcrt/span_util.h"
Tom Sepez5bfc5f52021-03-31 20:26:42 +000024#include "core/fxcrt/unowned_ptr.h"
Lei Zhange6fcdfa2019-02-14 04:07:09 +000025#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Tom Sepez9c78c0d2021-01-27 20:13:14 +000026#include "third_party/base/check.h"
Tom Sepezc524b1f2021-04-06 00:09:39 +000027#include "third_party/base/numerics/safe_conversions.h"
Dan Sinclair7aba4722018-03-28 17:04:16 +000028
29namespace {
30
Ralf Sippl16381792018-04-12 21:20:26 +000031constexpr char kQuadPoints[] = "QuadPoints";
32
Lei Zhang65a8d5e2018-12-20 19:13:21 +000033// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
Lei Zhangbc106482019-05-30 23:55:19 +000034uint32_t g_sandbox_policy = 0xFFFFFFFF;
Lei Zhang65a8d5e2018-12-20 19:13:21 +000035
Tom Sepez20c946f2019-07-31 19:33:21 +000036UNSUPPORT_INFO* g_unsupport_info = nullptr;
37
Lei Zhangb7d09ca2019-02-27 23:50:44 +000038bool RaiseUnsupportedError(int nError) {
Tom Sepez20c946f2019-07-31 19:33:21 +000039 if (!g_unsupport_info)
Dan Sinclair7aba4722018-03-28 17:04:16 +000040 return false;
41
Tom Sepez20c946f2019-07-31 19:33:21 +000042 if (g_unsupport_info->FSDK_UnSupport_Handler)
43 g_unsupport_info->FSDK_UnSupport_Handler(g_unsupport_info, nError);
Dan Sinclair7aba4722018-03-28 17:04:16 +000044 return true;
45}
46
Lei Zhang85e09df2020-02-27 14:48:07 +000047// Use the existence of the XFA array as a signal for XFA forms.
48bool DocHasXFA(const CPDF_Document* doc) {
49 const CPDF_Dictionary* root = doc->GetRoot();
50 if (!root)
51 return false;
52
Tom Sepez5313f922022-09-20 18:17:45 +000053 RetainPtr<const CPDF_Dictionary> form = root->GetDictFor("AcroForm");
Lei Zhang85e09df2020-02-27 14:48:07 +000054 return form && form->GetArrayFor("XFA");
55}
56
Tom Sepez06943a92022-11-10 20:29:34 +000057unsigned long GetStreamMaybeCopyAndReturnLengthImpl(
58 RetainPtr<const CPDF_Stream> stream,
59 pdfium::span<uint8_t> buffer,
60 bool decode) {
Tom Sepez9c78c0d2021-01-27 20:13:14 +000061 DCHECK(stream);
Tom Sepez06943a92022-11-10 20:29:34 +000062 auto stream_acc = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(stream));
Jeremy Chinsen617a2e82019-06-20 00:11:12 +000063 if (decode)
64 stream_acc->LoadAllDataFiltered();
65 else
66 stream_acc->LoadAllDataRaw();
67
Tom Sepez06943a92022-11-10 20:29:34 +000068 pdfium::span<const uint8_t> stream_data_span = stream_acc->GetSpan();
69 if (!buffer.empty() && buffer.size() <= stream_data_span.size())
70 fxcrt::spancpy(buffer, stream_data_span);
Jeremy Chinsen617a2e82019-06-20 00:11:12 +000071
Tom Sepez06943a92022-11-10 20:29:34 +000072 return pdfium::base::checked_cast<unsigned long>(stream_data_span.size());
Jeremy Chinsen617a2e82019-06-20 00:11:12 +000073}
74
Dan Sinclair7aba4722018-03-28 17:04:16 +000075#ifdef PDF_ENABLE_XFA
Tom Sepez55865452018-08-27 20:18:04 +000076class FPDF_FileHandlerContext final : public IFX_SeekableStream {
Dan Sinclair7aba4722018-03-28 17:04:16 +000077 public:
Tom Sepezb9d9b612020-06-17 20:47:22 +000078 CONSTRUCT_VIA_MAKE_RETAIN;
Dan Sinclair7aba4722018-03-28 17:04:16 +000079
Dan Sinclair7aba4722018-03-28 17:04:16 +000080 // IFX_SeekableStream:
81 FX_FILESIZE GetSize() override;
Dan Sinclair7aba4722018-03-28 17:04:16 +000082 FX_FILESIZE GetPosition() override;
Tom Sepeze1c2bcf2022-11-02 19:29:55 +000083 bool IsEOF() override;
84 size_t ReadBlock(pdfium::span<uint8_t> buffer) override;
Tom Sepez6a39df12022-11-01 20:29:35 +000085 bool ReadBlockAtOffset(pdfium::span<uint8_t> buffer,
86 FX_FILESIZE offset) override;
Tom Sepeze1c2bcf2022-11-02 19:29:55 +000087 bool WriteBlockAtOffset(pdfium::span<const uint8_t> buffer,
88 FX_FILESIZE offset) override;
Dan Sinclair7aba4722018-03-28 17:04:16 +000089 bool Flush() override;
90
91 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
92
Tom Sepezcb798252018-09-17 18:25:32 +000093 private:
Dan Sinclair7aba4722018-03-28 17:04:16 +000094 explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS);
Lei Zhang86688de2018-05-22 22:06:49 +000095 ~FPDF_FileHandlerContext() override;
Dan Sinclair7aba4722018-03-28 17:04:16 +000096
Tom Sepez5bfc5f52021-03-31 20:26:42 +000097 UnownedPtr<FPDF_FILEHANDLER> const m_pFS;
98 FX_FILESIZE m_nCurPos = 0;
Dan Sinclair7aba4722018-03-28 17:04:16 +000099};
100
Tom Sepez5bfc5f52021-03-31 20:26:42 +0000101FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS)
102 : m_pFS(pFS) {}
Dan Sinclair7aba4722018-03-28 17:04:16 +0000103
104FPDF_FileHandlerContext::~FPDF_FileHandlerContext() {
105 if (m_pFS && m_pFS->Release)
106 m_pFS->Release(m_pFS->clientData);
107}
108
109FX_FILESIZE FPDF_FileHandlerContext::GetSize() {
110 if (m_pFS && m_pFS->GetSize)
Tom Sepez5bfc5f52021-03-31 20:26:42 +0000111 return static_cast<FX_FILESIZE>(m_pFS->GetSize(m_pFS->clientData));
Dan Sinclair7aba4722018-03-28 17:04:16 +0000112 return 0;
113}
114
115bool FPDF_FileHandlerContext::IsEOF() {
116 return m_nCurPos >= GetSize();
117}
118
119FX_FILESIZE FPDF_FileHandlerContext::GetPosition() {
120 return m_nCurPos;
121}
122
Tom Sepez6a39df12022-11-01 20:29:35 +0000123bool FPDF_FileHandlerContext::ReadBlockAtOffset(pdfium::span<uint8_t> buffer,
124 FX_FILESIZE offset) {
125 if (buffer.empty() || !m_pFS->ReadBlock)
Dan Sinclair7aba4722018-03-28 17:04:16 +0000126 return false;
127
Tom Sepez6a39df12022-11-01 20:29:35 +0000128 if (m_pFS->ReadBlock(m_pFS->clientData, static_cast<FPDF_DWORD>(offset),
129 buffer.data(),
130 static_cast<FPDF_DWORD>(buffer.size())) == 0) {
131 m_nCurPos = offset + buffer.size();
Dan Sinclair7aba4722018-03-28 17:04:16 +0000132 return true;
133 }
134 return false;
135}
136
Tom Sepez1f866362022-11-01 23:54:47 +0000137size_t FPDF_FileHandlerContext::ReadBlock(pdfium::span<uint8_t> buffer) {
138 if (buffer.empty() || !m_pFS->ReadBlock)
Dan Sinclair7aba4722018-03-28 17:04:16 +0000139 return 0;
140
141 FX_FILESIZE nSize = GetSize();
142 if (m_nCurPos >= nSize)
143 return 0;
144 FX_FILESIZE dwAvail = nSize - m_nCurPos;
Tom Sepez1f866362022-11-01 23:54:47 +0000145 if (dwAvail < (FX_FILESIZE)buffer.size())
146 buffer = buffer.first(static_cast<size_t>(dwAvail));
147 if (m_pFS->ReadBlock(m_pFS->clientData, static_cast<FPDF_DWORD>(m_nCurPos),
148 buffer.data(),
149 static_cast<FPDF_DWORD>(buffer.size())) == 0) {
150 m_nCurPos += buffer.size();
151 return buffer.size();
Dan Sinclair7aba4722018-03-28 17:04:16 +0000152 }
153
154 return 0;
155}
156
Tom Sepeze1c2bcf2022-11-02 19:29:55 +0000157bool FPDF_FileHandlerContext::WriteBlockAtOffset(
158 pdfium::span<const uint8_t> buffer,
159 FX_FILESIZE offset) {
Dan Sinclair7aba4722018-03-28 17:04:16 +0000160 if (!m_pFS || !m_pFS->WriteBlock)
161 return false;
162
Tom Sepeze1c2bcf2022-11-02 19:29:55 +0000163 if (m_pFS->WriteBlock(m_pFS->clientData, static_cast<FPDF_DWORD>(offset),
164 buffer.data(),
165 static_cast<FPDF_DWORD>(buffer.size())) == 0) {
166 m_nCurPos = offset + buffer.size();
Dan Sinclair7aba4722018-03-28 17:04:16 +0000167 return true;
168 }
169 return false;
170}
171
172bool FPDF_FileHandlerContext::Flush() {
173 if (!m_pFS || !m_pFS->Flush)
174 return true;
175
176 return m_pFS->Flush(m_pFS->clientData) == 0;
177}
178#endif // PDF_ENABLE_XFA
179
180} // namespace
181
Tom Sepez101535f2018-06-12 13:36:05 +0000182IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page) {
183 return reinterpret_cast<IPDF_Page*>(page);
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000184}
185
Tom Sepez101535f2018-06-12 13:36:05 +0000186FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page) {
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000187 return reinterpret_cast<FPDF_PAGE>(page);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000188}
189
190CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepezfe06d512018-05-01 17:25:25 +0000191 return reinterpret_cast<CPDF_Document*>(doc);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000192}
193
194FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepezfe06d512018-05-01 17:25:25 +0000195 return reinterpret_cast<FPDF_DOCUMENT>(doc);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000196}
197
198CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez101535f2018-06-12 13:36:05 +0000199 return page ? IPDFPageFromFPDFPage(page)->AsPDFPage() : nullptr;
Dan Sinclair7aba4722018-03-28 17:04:16 +0000200}
201
Lei Zhange6fcdfa2019-02-14 04:07:09 +0000202CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) {
203 CPDFSDK_FormFillEnvironment* pFormFillEnv =
204 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
205 return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr;
206}
207
Lei Zhangb46a7632019-01-09 02:56:16 +0000208ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
209 return WideStringFromFPDFWideString(wide_string).ToUTF8();
Dan Sinclair7aba4722018-03-28 17:04:16 +0000210}
211
Lei Zhangf5fcd9e2018-12-23 03:11:50 +0000212WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
213 return WideString::FromUTF16LE(wide_string,
214 WideString::WStringLength(wide_string));
215}
216
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000217#ifdef PDF_ENABLE_XFA
218RetainPtr<IFX_SeekableStream> MakeSeekableStream(
219 FPDF_FILEHANDLER* pFilehandler) {
220 return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000221}
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000222#endif // PDF_ENABLE_XFA
Dan Sinclair7aba4722018-03-28 17:04:16 +0000223
Tom Sepez525a4272022-09-28 21:35:24 +0000224RetainPtr<const CPDF_Array> GetQuadPointsArrayFromDictionary(
Lei Zhang5cee3f22018-05-25 21:48:49 +0000225 const CPDF_Dictionary* dict) {
Tom Sepez525a4272022-09-28 21:35:24 +0000226 return dict->GetArrayFor("QuadPoints");
Lei Zhang5cee3f22018-05-25 21:48:49 +0000227}
228
Tom Sepez3d64afa2022-06-24 16:40:17 +0000229RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
230 CPDF_Dictionary* dict) {
231 return pdfium::WrapRetain(
Tom Sepez525a4272022-09-28 21:35:24 +0000232 const_cast<CPDF_Array*>(GetQuadPointsArrayFromDictionary(dict).Get()));
Dan Sinclair7aba4722018-03-28 17:04:16 +0000233}
234
Tom Sepez8aad22a2022-09-22 18:56:16 +0000235RetainPtr<CPDF_Array> AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) {
Ralf Sippl16381792018-04-12 21:20:26 +0000236 return dict->SetNewFor<CPDF_Array>(kQuadPoints);
237}
238
239bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index) {
Lei Zhangf40380f2018-10-12 18:31:51 +0000240 return array && index < array->size() / 8;
Ralf Sippl16381792018-04-12 21:20:26 +0000241}
242
Tom Sepez525a4272022-09-28 21:35:24 +0000243bool GetQuadPointsAtIndex(RetainPtr<const CPDF_Array> array,
Ralf Sippl16381792018-04-12 21:20:26 +0000244 size_t quad_index,
245 FS_QUADPOINTSF* quad_points) {
Tom Sepez9c78c0d2021-01-27 20:13:14 +0000246 DCHECK(quad_points);
247 DCHECK(array);
Ralf Sippl16381792018-04-12 21:20:26 +0000248
249 if (!IsValidQuadPointsIndex(array, quad_index))
250 return false;
251
252 quad_index *= 8;
Tom Sepez3b886ba2022-09-10 00:00:45 +0000253 quad_points->x1 = array->GetFloatAt(quad_index);
254 quad_points->y1 = array->GetFloatAt(quad_index + 1);
255 quad_points->x2 = array->GetFloatAt(quad_index + 2);
256 quad_points->y2 = array->GetFloatAt(quad_index + 3);
257 quad_points->x3 = array->GetFloatAt(quad_index + 4);
258 quad_points->y3 = array->GetFloatAt(quad_index + 5);
259 quad_points->x4 = array->GetFloatAt(quad_index + 6);
260 quad_points->y4 = array->GetFloatAt(quad_index + 7);
Ralf Sippl16381792018-04-12 21:20:26 +0000261 return true;
262}
263
Lei Zhang8da98232019-12-11 23:29:33 +0000264CFX_PointF CFXPointFFromFSPointF(const FS_POINTF& point) {
265 return CFX_PointF(point.x, point.y);
266}
267
268CFX_FloatRect CFXFloatRectFromFSRectF(const FS_RECTF& rect) {
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000269 return CFX_FloatRect(rect.left, rect.bottom, rect.right, rect.top);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000270}
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000271
Lei Zhang8da98232019-12-11 23:29:33 +0000272FS_RECTF FSRectFFromCFXFloatRect(const CFX_FloatRect& rect) {
Lei Zhang3567c612019-11-18 18:10:02 +0000273 return {rect.left, rect.top, rect.right, rect.bottom};
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000274}
275
Lei Zhang6fef1e42018-12-20 19:14:02 +0000276CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix) {
277 return CFX_Matrix(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f);
278}
279
Lei Zhangc89c5822020-01-21 20:23:56 +0000280FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix& matrix) {
281 return {matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f};
Lei Zhang8da98232019-12-11 23:29:33 +0000282}
283
Daniel Hosseinian5bc1f982020-04-14 01:05:27 +0000284unsigned long NulTerminateMaybeCopyAndReturnLength(const ByteString& text,
285 void* buffer,
286 unsigned long buflen) {
Tom Sepez8ef63b92022-03-08 19:53:34 +0000287 const unsigned long len =
288 pdfium::base::checked_cast<unsigned long>(text.GetLength() + 1);
Daniel Hosseinian5bc1f982020-04-14 01:05:27 +0000289 if (buffer && len <= buflen)
290 memcpy(buffer, text.c_str(), len);
291 return len;
292}
293
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000294unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
295 void* buffer,
296 unsigned long buflen) {
297 ByteString encoded_text = text.ToUTF16LE();
Tom Sepez8ef63b92022-03-08 19:53:34 +0000298 const unsigned long len =
299 pdfium::base::checked_cast<unsigned long>(encoded_text.GetLength());
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000300 if (buffer && len <= buflen)
301 memcpy(buffer, encoded_text.c_str(), len);
302 return len;
303}
304
Tom Sepez06943a92022-11-10 20:29:34 +0000305unsigned long GetRawStreamMaybeCopyAndReturnLength(
306 RetainPtr<const CPDF_Stream> stream,
307 pdfium::span<uint8_t> buffer) {
308 return GetStreamMaybeCopyAndReturnLengthImpl(std::move(stream), buffer,
Jeremy Chinsen617a2e82019-06-20 00:11:12 +0000309 /*decode=*/false);
310}
311
Tom Sepez06943a92022-11-10 20:29:34 +0000312unsigned long DecodeStreamMaybeCopyAndReturnLength(
313 RetainPtr<const CPDF_Stream> stream,
314 pdfium::span<uint8_t> buffer) {
315 return GetStreamMaybeCopyAndReturnLengthImpl(std::move(stream), buffer,
Jeremy Chinsen617a2e82019-06-20 00:11:12 +0000316 /*decode=*/true);
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000317}
318
Tom Sepez69a4a702019-07-31 17:59:49 +0000319void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000320 switch (policy) {
321 case FPDF_POLICY_MACHINETIME_ACCESS: {
Tom Sepezfe285c32019-12-04 18:38:03 +0000322 uint32_t mask = 1 << policy;
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000323 if (enable)
Tom Sepezfe285c32019-12-04 18:38:03 +0000324 g_sandbox_policy |= mask;
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000325 else
Tom Sepezfe285c32019-12-04 18:38:03 +0000326 g_sandbox_policy &= ~mask;
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000327 } break;
328 default:
329 break;
330 }
331}
332
Tom Sepez69a4a702019-07-31 17:59:49 +0000333FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy) {
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000334 switch (policy) {
Tom Sepezfe285c32019-12-04 18:38:03 +0000335 case FPDF_POLICY_MACHINETIME_ACCESS: {
336 uint32_t mask = 1 << policy;
337 return !!(g_sandbox_policy & mask);
338 }
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000339 default:
340 return false;
341 }
342}
343
Tom Sepez20c946f2019-07-31 19:33:21 +0000344void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info) {
345 g_unsupport_info = unsp_info;
346}
347
Lei Zhang4745a472020-02-27 18:24:58 +0000348void ReportUnsupportedFeatures(const CPDF_Document* pDoc) {
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000349 const CPDF_Dictionary* pRootDict = pDoc->GetRoot();
Lei Zhang4745a472020-02-27 18:24:58 +0000350 if (!pRootDict)
351 return;
352
353 // Portfolios and Packages
Lei Zhang6e8b20f2020-02-27 20:02:49 +0000354 if (pRootDict->KeyExist("Collection"))
Lei Zhang4745a472020-02-27 18:24:58 +0000355 RaiseUnsupportedError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
Lei Zhang6e8b20f2020-02-27 20:02:49 +0000356
Tom Sepez5313f922022-09-20 18:17:45 +0000357 RetainPtr<const CPDF_Dictionary> pNameDict = pRootDict->GetDictFor("Names");
Lei Zhang4745a472020-02-27 18:24:58 +0000358 if (pNameDict) {
Lei Zhang6e8b20f2020-02-27 20:02:49 +0000359 if (pNameDict->KeyExist("EmbeddedFiles"))
Lei Zhang4745a472020-02-27 18:24:58 +0000360 RaiseUnsupportedError(FPDF_UNSP_DOC_ATTACHMENT);
Lei Zhang6e8b20f2020-02-27 20:02:49 +0000361
Tom Sepez5313f922022-09-20 18:17:45 +0000362 RetainPtr<const CPDF_Dictionary> pJSDict =
363 pNameDict->GetDictFor("JavaScript");
Lei Zhang4745a472020-02-27 18:24:58 +0000364 if (pJSDict) {
Tom Sepezc84dc1f2022-09-19 23:06:33 +0000365 RetainPtr<const CPDF_Array> pArray = pJSDict->GetArrayFor("Names");
Lei Zhang4745a472020-02-27 18:24:58 +0000366 if (pArray) {
367 for (size_t i = 0; i < pArray->size(); i++) {
Tom Sepez56902472022-09-14 21:37:14 +0000368 ByteString cbStr = pArray->GetByteStringAt(i);
Lei Zhang814e3bf2021-04-24 01:45:51 +0000369 if (cbStr == "com.adobe.acrobat.SharedReview.Register") {
Lei Zhang4745a472020-02-27 18:24:58 +0000370 RaiseUnsupportedError(FPDF_UNSP_DOC_SHAREDREVIEW);
Lei Zhang6e8b20f2020-02-27 20:02:49 +0000371 break;
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000372 }
373 }
374 }
375 }
Lei Zhang4745a472020-02-27 18:24:58 +0000376 }
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000377
Lei Zhang4745a472020-02-27 18:24:58 +0000378 // SharedForm
Tom Sepez652d35b2022-09-10 01:42:25 +0000379 RetainPtr<const CPDF_Stream> pStream = pRootDict->GetStreamFor("Metadata");
Lei Zhang4745a472020-02-27 18:24:58 +0000380 if (pStream) {
Tom Sepez652d35b2022-09-10 01:42:25 +0000381 CPDF_Metadata metadata(std::move(pStream));
Lei Zhang4745a472020-02-27 18:24:58 +0000382 for (const UnsupportedFeature& feature : metadata.CheckForSharedForm())
383 RaiseUnsupportedError(static_cast<int>(feature));
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000384 }
Daniel Hosseiniana793e972020-01-24 02:51:31 +0000385}
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000386
Lei Zhang85e09df2020-02-27 14:48:07 +0000387void ReportUnsupportedXFA(const CPDF_Document* pDoc) {
388 if (!pDoc->GetExtension() && DocHasXFA(pDoc))
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000389 RaiseUnsupportedError(FPDF_UNSP_DOC_XFAFORM);
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000390}
391
Lei Zhang4efdb512019-02-26 19:48:39 +0000392void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot) {
393 switch (pAnnot->GetSubtype()) {
394 case CPDF_Annot::Subtype::FILEATTACHMENT:
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000395 RaiseUnsupportedError(FPDF_UNSP_ANNOT_ATTACHMENT);
Lei Zhang4efdb512019-02-26 19:48:39 +0000396 break;
397 case CPDF_Annot::Subtype::MOVIE:
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000398 RaiseUnsupportedError(FPDF_UNSP_ANNOT_MOVIE);
Lei Zhang4efdb512019-02-26 19:48:39 +0000399 break;
400 case CPDF_Annot::Subtype::RICHMEDIA:
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000401 RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
Lei Zhang4efdb512019-02-26 19:48:39 +0000402 break;
403 case CPDF_Annot::Subtype::SCREEN: {
404 const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
Tom Sepez27151a62022-09-14 19:48:59 +0000405 ByteString cbString = pAnnotDict->GetByteStringFor("IT");
Lei Zhang6c715022019-02-26 20:16:09 +0000406 if (cbString != "Img")
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000407 RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
Lei Zhang4efdb512019-02-26 19:48:39 +0000408 break;
409 }
410 case CPDF_Annot::Subtype::SOUND:
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000411 RaiseUnsupportedError(FPDF_UNSP_ANNOT_SOUND);
Lei Zhang4efdb512019-02-26 19:48:39 +0000412 break;
413 case CPDF_Annot::Subtype::THREED:
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000414 RaiseUnsupportedError(FPDF_UNSP_ANNOT_3DANNOT);
Lei Zhang4efdb512019-02-26 19:48:39 +0000415 break;
416 case CPDF_Annot::Subtype::WIDGET: {
417 const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
Tom Sepez27151a62022-09-14 19:48:59 +0000418 ByteString cbString =
419 pAnnotDict->GetByteStringFor(pdfium::form_fields::kFT);
Lei Zhangf496e252019-02-26 20:20:19 +0000420 if (cbString == pdfium::form_fields::kSig)
Lei Zhangb7d09ca2019-02-27 23:50:44 +0000421 RaiseUnsupportedError(FPDF_UNSP_ANNOT_SIG);
Lei Zhang4efdb512019-02-26 19:48:39 +0000422 break;
423 }
424 default:
425 break;
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000426 }
427}
428
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000429void ProcessParseError(CPDF_Parser::Error err) {
430 uint32_t err_code = FPDF_ERR_SUCCESS;
431 // Translate FPDFAPI error code to FPDFVIEW error code
432 switch (err) {
433 case CPDF_Parser::SUCCESS:
434 err_code = FPDF_ERR_SUCCESS;
435 break;
436 case CPDF_Parser::FILE_ERROR:
437 err_code = FPDF_ERR_FILE;
438 break;
439 case CPDF_Parser::FORMAT_ERROR:
440 err_code = FPDF_ERR_FORMAT;
441 break;
442 case CPDF_Parser::PASSWORD_ERROR:
443 err_code = FPDF_ERR_PASSWORD;
444 break;
445 case CPDF_Parser::HANDLER_ERROR:
446 err_code = FPDF_ERR_SECURITY;
447 break;
448 }
Tom Sepez04e3af82019-08-05 23:41:06 +0000449 FXSYS_SetLastError(err_code);
Lei Zhang65a8d5e2018-12-20 19:13:21 +0000450}
Gourab Kundud35a62d2020-04-24 10:29:27 +0000451
452void SetColorFromScheme(const FPDF_COLORSCHEME* pColorScheme,
453 CPDF_RenderOptions* pRenderOptions) {
454 CPDF_RenderOptions::ColorScheme color_scheme;
Tom Sepez5ea5fa92022-03-08 00:15:23 +0000455 color_scheme.path_fill_color =
456 static_cast<FX_ARGB>(pColorScheme->path_fill_color);
457 color_scheme.path_stroke_color =
458 static_cast<FX_ARGB>(pColorScheme->path_stroke_color);
459 color_scheme.text_fill_color =
460 static_cast<FX_ARGB>(pColorScheme->text_fill_color);
461 color_scheme.text_stroke_color =
462 static_cast<FX_ARGB>(pColorScheme->text_stroke_color);
Gourab Kundud35a62d2020-04-24 10:29:27 +0000463 pRenderOptions->SetColorScheme(color_scheme);
464}
Tom Sepezc524b1f2021-04-06 00:09:39 +0000465
466std::vector<uint32_t> ParsePageRangeString(const ByteString& bsPageRange,
467 uint32_t nCount) {
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000468 ByteStringView alphabet(" 0123456789-,");
469 for (const auto& ch : bsPageRange) {
470 if (!alphabet.Contains(ch))
Tom Sepezc524b1f2021-04-06 00:09:39 +0000471 return std::vector<uint32_t>();
472 }
473
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000474 ByteString bsStrippedPageRange = bsPageRange;
475 bsStrippedPageRange.Remove(' ');
Tom Sepezc524b1f2021-04-06 00:09:39 +0000476
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000477 std::vector<uint32_t> results;
478 for (const auto& entry : fxcrt::Split(bsStrippedPageRange, ',')) {
479 std::vector<ByteString> args = fxcrt::Split(entry, '-');
480 if (args.size() == 1) {
481 uint32_t page_num =
482 pdfium::base::checked_cast<uint32_t>(atoi(args[0].c_str()));
483 if (page_num == 0 || page_num > nCount)
Tom Sepezc524b1f2021-04-06 00:09:39 +0000484 return std::vector<uint32_t>();
Daniel Hosseinianc7cb4052021-06-11 21:00:47 +0000485 results.push_back(page_num - 1);
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000486 } else if (args.size() == 2) {
487 uint32_t first_num =
488 pdfium::base::checked_cast<uint32_t>(atoi(args[0].c_str()));
489 if (first_num == 0)
Tom Sepezc524b1f2021-04-06 00:09:39 +0000490 return std::vector<uint32_t>();
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000491 uint32_t last_num =
492 pdfium::base::checked_cast<uint32_t>(atoi(args[1].c_str()));
493 if (last_num == 0 || first_num > last_num || last_num > nCount)
494 return std::vector<uint32_t>();
495 for (uint32_t i = first_num; i <= last_num; ++i)
Daniel Hosseinianc7cb4052021-06-11 21:00:47 +0000496 results.push_back(i - 1);
Tom Sepezc524b1f2021-04-06 00:09:39 +0000497 } else {
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000498 return std::vector<uint32_t>();
Tom Sepezc524b1f2021-04-06 00:09:39 +0000499 }
Tom Sepezc524b1f2021-04-06 00:09:39 +0000500 }
Tom Sepez1e2d7df2021-04-07 19:26:06 +0000501 return results;
Tom Sepezc524b1f2021-04-06 00:09:39 +0000502}