Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 1 | // Copyright 2018 PDFium Authors. All rights reserved. |
| 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 | |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 8 | |
Lei Zhang | bc10648 | 2019-05-30 23:55:19 +0000 | [diff] [blame] | 9 | #include "build/build_config.h" |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 10 | #include "constants/form_fields.h" |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame] | 11 | #include "constants/stream_dict_common.h" |
Tom Sepez | a1d3442 | 2018-04-24 20:54:41 +0000 | [diff] [blame] | 12 | #include "core/fpdfapi/page/cpdf_page.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_document.h" |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
Gourab Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 17 | #include "core/fpdfapi/render/cpdf_renderoptions.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_annot.h" |
Lei Zhang | c345065 | 2018-10-11 16:54:42 +0000 | [diff] [blame] | 19 | #include "core/fpdfdoc/cpdf_interactiveform.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 20 | #include "core/fpdfdoc/cpdf_metadata.h" |
Tom Sepez | 5bfc5f5 | 2021-03-31 20:26:42 +0000 | [diff] [blame] | 21 | #include "core/fxcrt/unowned_ptr.h" |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 22 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
Tom Sepez | 9c78c0d | 2021-01-27 20:13:14 +0000 | [diff] [blame] | 23 | #include "third_party/base/check.h" |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 24 | #include "third_party/base/numerics/safe_conversions.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 28 | constexpr char kQuadPoints[] = "QuadPoints"; |
| 29 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 30 | // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS |
Lei Zhang | bc10648 | 2019-05-30 23:55:19 +0000 | [diff] [blame] | 31 | uint32_t g_sandbox_policy = 0xFFFFFFFF; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 32 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 33 | UNSUPPORT_INFO* g_unsupport_info = nullptr; |
| 34 | |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 35 | bool RaiseUnsupportedError(int nError) { |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 36 | if (!g_unsupport_info) |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 37 | return false; |
| 38 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 39 | if (g_unsupport_info->FSDK_UnSupport_Handler) |
| 40 | g_unsupport_info->FSDK_UnSupport_Handler(g_unsupport_info, nError); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | |
Lei Zhang | 85e09df | 2020-02-27 14:48:07 +0000 | [diff] [blame] | 44 | // Use the existence of the XFA array as a signal for XFA forms. |
| 45 | bool DocHasXFA(const CPDF_Document* doc) { |
| 46 | const CPDF_Dictionary* root = doc->GetRoot(); |
| 47 | if (!root) |
| 48 | return false; |
| 49 | |
| 50 | const CPDF_Dictionary* form = root->GetDictFor("AcroForm"); |
| 51 | return form && form->GetArrayFor("XFA"); |
| 52 | } |
| 53 | |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 54 | unsigned long GetStreamMaybeCopyAndReturnLengthImpl(const CPDF_Stream* stream, |
| 55 | void* buffer, |
| 56 | unsigned long buflen, |
| 57 | bool decode) { |
Tom Sepez | 9c78c0d | 2021-01-27 20:13:14 +0000 | [diff] [blame] | 58 | DCHECK(stream); |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 59 | auto stream_acc = pdfium::MakeRetain<CPDF_StreamAcc>(stream); |
| 60 | |
| 61 | if (decode) |
| 62 | stream_acc->LoadAllDataFiltered(); |
| 63 | else |
| 64 | stream_acc->LoadAllDataRaw(); |
| 65 | |
| 66 | const auto stream_data_size = stream_acc->GetSize(); |
| 67 | if (!buffer || buflen < stream_data_size) |
| 68 | return stream_data_size; |
| 69 | |
| 70 | memcpy(buffer, stream_acc->GetData(), stream_data_size); |
| 71 | return stream_data_size; |
| 72 | } |
| 73 | |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 74 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 75 | class FPDF_FileHandlerContext final : public IFX_SeekableStream { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 76 | public: |
Tom Sepez | b9d9b61 | 2020-06-17 20:47:22 +0000 | [diff] [blame] | 77 | CONSTRUCT_VIA_MAKE_RETAIN; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 78 | |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 79 | // IFX_SeekableStream: |
| 80 | FX_FILESIZE GetSize() override; |
| 81 | bool IsEOF() override; |
| 82 | FX_FILESIZE GetPosition() override; |
Lei Zhang | f6a7921 | 2018-11-15 20:17:49 +0000 | [diff] [blame] | 83 | bool ReadBlockAtOffset(void* buffer, |
| 84 | FX_FILESIZE offset, |
| 85 | size_t size) override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 86 | size_t ReadBlock(void* buffer, size_t size) override; |
Lei Zhang | 59f7623 | 2018-11-15 20:22:59 +0000 | [diff] [blame] | 87 | bool WriteBlockAtOffset(const void* buffer, |
| 88 | FX_FILESIZE offset, |
| 89 | size_t size) override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 90 | bool Flush() override; |
| 91 | |
| 92 | void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } |
| 93 | |
Tom Sepez | cb79825 | 2018-09-17 18:25:32 +0000 | [diff] [blame] | 94 | private: |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 95 | explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS); |
Lei Zhang | 86688de | 2018-05-22 22:06:49 +0000 | [diff] [blame] | 96 | ~FPDF_FileHandlerContext() override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 97 | |
Tom Sepez | 5bfc5f5 | 2021-03-31 20:26:42 +0000 | [diff] [blame] | 98 | UnownedPtr<FPDF_FILEHANDLER> const m_pFS; |
| 99 | FX_FILESIZE m_nCurPos = 0; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Tom Sepez | 5bfc5f5 | 2021-03-31 20:26:42 +0000 | [diff] [blame] | 102 | FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) |
| 103 | : m_pFS(pFS) {} |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 104 | |
| 105 | FPDF_FileHandlerContext::~FPDF_FileHandlerContext() { |
| 106 | if (m_pFS && m_pFS->Release) |
| 107 | m_pFS->Release(m_pFS->clientData); |
| 108 | } |
| 109 | |
| 110 | FX_FILESIZE FPDF_FileHandlerContext::GetSize() { |
| 111 | if (m_pFS && m_pFS->GetSize) |
Tom Sepez | 5bfc5f5 | 2021-03-31 20:26:42 +0000 | [diff] [blame] | 112 | return static_cast<FX_FILESIZE>(m_pFS->GetSize(m_pFS->clientData)); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | bool FPDF_FileHandlerContext::IsEOF() { |
| 117 | return m_nCurPos >= GetSize(); |
| 118 | } |
| 119 | |
| 120 | FX_FILESIZE FPDF_FileHandlerContext::GetPosition() { |
| 121 | return m_nCurPos; |
| 122 | } |
| 123 | |
Lei Zhang | f6a7921 | 2018-11-15 20:17:49 +0000 | [diff] [blame] | 124 | bool FPDF_FileHandlerContext::ReadBlockAtOffset(void* buffer, |
| 125 | FX_FILESIZE offset, |
| 126 | size_t size) { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 127 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 128 | return false; |
| 129 | |
| 130 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 131 | (FPDF_DWORD)size) == 0) { |
| 132 | m_nCurPos = offset + size; |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) { |
| 139 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 140 | return 0; |
| 141 | |
| 142 | FX_FILESIZE nSize = GetSize(); |
| 143 | if (m_nCurPos >= nSize) |
| 144 | return 0; |
| 145 | FX_FILESIZE dwAvail = nSize - m_nCurPos; |
| 146 | if (dwAvail < (FX_FILESIZE)size) |
| 147 | size = static_cast<size_t>(dwAvail); |
| 148 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer, |
| 149 | (FPDF_DWORD)size) == 0) { |
| 150 | m_nCurPos += size; |
| 151 | return size; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Lei Zhang | 59f7623 | 2018-11-15 20:22:59 +0000 | [diff] [blame] | 157 | bool FPDF_FileHandlerContext::WriteBlockAtOffset(const void* buffer, |
| 158 | FX_FILESIZE offset, |
| 159 | size_t size) { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 160 | if (!m_pFS || !m_pFS->WriteBlock) |
| 161 | return false; |
| 162 | |
| 163 | if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 164 | (FPDF_DWORD)size) == 0) { |
| 165 | m_nCurPos = offset + size; |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | bool FPDF_FileHandlerContext::Flush() { |
| 172 | if (!m_pFS || !m_pFS->Flush) |
| 173 | return true; |
| 174 | |
| 175 | return m_pFS->Flush(m_pFS->clientData) == 0; |
| 176 | } |
| 177 | #endif // PDF_ENABLE_XFA |
| 178 | |
| 179 | } // namespace |
| 180 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 181 | IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page) { |
| 182 | return reinterpret_cast<IPDF_Page*>(page); |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 185 | FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page) { |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 186 | return reinterpret_cast<FPDF_PAGE>(page); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { |
Tom Sepez | fe06d51 | 2018-05-01 17:25:25 +0000 | [diff] [blame] | 190 | return reinterpret_cast<CPDF_Document*>(doc); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { |
Tom Sepez | fe06d51 | 2018-05-01 17:25:25 +0000 | [diff] [blame] | 194 | return reinterpret_cast<FPDF_DOCUMENT>(doc); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 198 | return page ? IPDFPageFromFPDFPage(page)->AsPDFPage() : nullptr; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 201 | CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) { |
| 202 | CPDFSDK_FormFillEnvironment* pFormFillEnv = |
| 203 | CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle); |
| 204 | return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr; |
| 205 | } |
| 206 | |
Lei Zhang | b46a763 | 2019-01-09 02:56:16 +0000 | [diff] [blame] | 207 | ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 208 | return WideStringFromFPDFWideString(wide_string).ToUTF8(); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 211 | WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 212 | return WideString::FromUTF16LE(wide_string, |
| 213 | WideString::WStringLength(wide_string)); |
| 214 | } |
| 215 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 216 | #ifdef PDF_ENABLE_XFA |
| 217 | RetainPtr<IFX_SeekableStream> MakeSeekableStream( |
| 218 | FPDF_FILEHANDLER* pFilehandler) { |
| 219 | return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 220 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 221 | #endif // PDF_ENABLE_XFA |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 222 | |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 223 | const CPDF_Array* GetQuadPointsArrayFromDictionary( |
| 224 | const CPDF_Dictionary* dict) { |
Lei Zhang | d934c64 | 2019-03-04 19:42:00 +0000 | [diff] [blame] | 225 | return dict->GetArrayFor("QuadPoints"); |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Tom Sepez | 3d64afa | 2022-06-24 16:40:17 +0000 | [diff] [blame^] | 228 | RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary( |
| 229 | CPDF_Dictionary* dict) { |
| 230 | return pdfium::WrapRetain( |
| 231 | const_cast<CPDF_Array*>(GetQuadPointsArrayFromDictionary(dict))); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 234 | CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 235 | return dict->SetNewFor<CPDF_Array>(kQuadPoints); |
| 236 | } |
| 237 | |
| 238 | bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index) { |
Lei Zhang | f40380f | 2018-10-12 18:31:51 +0000 | [diff] [blame] | 239 | return array && index < array->size() / 8; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool GetQuadPointsAtIndex(const CPDF_Array* array, |
| 243 | size_t quad_index, |
| 244 | FS_QUADPOINTSF* quad_points) { |
Tom Sepez | 9c78c0d | 2021-01-27 20:13:14 +0000 | [diff] [blame] | 245 | DCHECK(quad_points); |
| 246 | DCHECK(array); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 247 | |
| 248 | if (!IsValidQuadPointsIndex(array, quad_index)) |
| 249 | return false; |
| 250 | |
| 251 | quad_index *= 8; |
| 252 | quad_points->x1 = array->GetNumberAt(quad_index); |
| 253 | quad_points->y1 = array->GetNumberAt(quad_index + 1); |
| 254 | quad_points->x2 = array->GetNumberAt(quad_index + 2); |
| 255 | quad_points->y2 = array->GetNumberAt(quad_index + 3); |
| 256 | quad_points->x3 = array->GetNumberAt(quad_index + 4); |
| 257 | quad_points->y3 = array->GetNumberAt(quad_index + 5); |
| 258 | quad_points->x4 = array->GetNumberAt(quad_index + 6); |
| 259 | quad_points->y4 = array->GetNumberAt(quad_index + 7); |
| 260 | return true; |
| 261 | } |
| 262 | |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 263 | CFX_PointF CFXPointFFromFSPointF(const FS_POINTF& point) { |
| 264 | return CFX_PointF(point.x, point.y); |
| 265 | } |
| 266 | |
| 267 | CFX_FloatRect CFXFloatRectFromFSRectF(const FS_RECTF& rect) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 268 | return CFX_FloatRect(rect.left, rect.bottom, rect.right, rect.top); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 269 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 270 | |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 271 | FS_RECTF FSRectFFromCFXFloatRect(const CFX_FloatRect& rect) { |
Lei Zhang | 3567c61 | 2019-11-18 18:10:02 +0000 | [diff] [blame] | 272 | return {rect.left, rect.top, rect.right, rect.bottom}; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Lei Zhang | 6fef1e4 | 2018-12-20 19:14:02 +0000 | [diff] [blame] | 275 | CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix) { |
| 276 | return CFX_Matrix(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f); |
| 277 | } |
| 278 | |
Lei Zhang | c89c582 | 2020-01-21 20:23:56 +0000 | [diff] [blame] | 279 | FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix& matrix) { |
| 280 | return {matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f}; |
Lei Zhang | 8da9823 | 2019-12-11 23:29:33 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Daniel Hosseinian | 5bc1f98 | 2020-04-14 01:05:27 +0000 | [diff] [blame] | 283 | unsigned long NulTerminateMaybeCopyAndReturnLength(const ByteString& text, |
| 284 | void* buffer, |
| 285 | unsigned long buflen) { |
Tom Sepez | 8ef63b9 | 2022-03-08 19:53:34 +0000 | [diff] [blame] | 286 | const unsigned long len = |
| 287 | pdfium::base::checked_cast<unsigned long>(text.GetLength() + 1); |
Daniel Hosseinian | 5bc1f98 | 2020-04-14 01:05:27 +0000 | [diff] [blame] | 288 | if (buffer && len <= buflen) |
| 289 | memcpy(buffer, text.c_str(), len); |
| 290 | return len; |
| 291 | } |
| 292 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 293 | unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text, |
| 294 | void* buffer, |
| 295 | unsigned long buflen) { |
| 296 | ByteString encoded_text = text.ToUTF16LE(); |
Tom Sepez | 8ef63b9 | 2022-03-08 19:53:34 +0000 | [diff] [blame] | 297 | const unsigned long len = |
| 298 | pdfium::base::checked_cast<unsigned long>(encoded_text.GetLength()); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 299 | if (buffer && len <= buflen) |
| 300 | memcpy(buffer, encoded_text.c_str(), len); |
| 301 | return len; |
| 302 | } |
| 303 | |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 304 | unsigned long GetRawStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream, |
| 305 | void* buffer, |
| 306 | unsigned long buflen) { |
| 307 | return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen, |
| 308 | /*decode=*/false); |
| 309 | } |
| 310 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 311 | unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream, |
| 312 | void* buffer, |
| 313 | unsigned long buflen) { |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 314 | return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen, |
| 315 | /*decode=*/true); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Tom Sepez | 69a4a70 | 2019-07-31 17:59:49 +0000 | [diff] [blame] | 318 | void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 319 | switch (policy) { |
| 320 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame] | 321 | uint32_t mask = 1 << policy; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 322 | if (enable) |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame] | 323 | g_sandbox_policy |= mask; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 324 | else |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame] | 325 | g_sandbox_policy &= ~mask; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 326 | } break; |
| 327 | default: |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
Tom Sepez | 69a4a70 | 2019-07-31 17:59:49 +0000 | [diff] [blame] | 332 | FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 333 | switch (policy) { |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame] | 334 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
| 335 | uint32_t mask = 1 << policy; |
| 336 | return !!(g_sandbox_policy & mask); |
| 337 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 338 | default: |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 343 | void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info) { |
| 344 | g_unsupport_info = unsp_info; |
| 345 | } |
| 346 | |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 347 | void ReportUnsupportedFeatures(const CPDF_Document* pDoc) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 348 | const CPDF_Dictionary* pRootDict = pDoc->GetRoot(); |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 349 | if (!pRootDict) |
| 350 | return; |
| 351 | |
| 352 | // Portfolios and Packages |
Lei Zhang | 6e8b20f | 2020-02-27 20:02:49 +0000 | [diff] [blame] | 353 | if (pRootDict->KeyExist("Collection")) |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 354 | RaiseUnsupportedError(FPDF_UNSP_DOC_PORTABLECOLLECTION); |
Lei Zhang | 6e8b20f | 2020-02-27 20:02:49 +0000 | [diff] [blame] | 355 | |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 356 | const CPDF_Dictionary* pNameDict = pRootDict->GetDictFor("Names"); |
| 357 | if (pNameDict) { |
Lei Zhang | 6e8b20f | 2020-02-27 20:02:49 +0000 | [diff] [blame] | 358 | if (pNameDict->KeyExist("EmbeddedFiles")) |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 359 | RaiseUnsupportedError(FPDF_UNSP_DOC_ATTACHMENT); |
Lei Zhang | 6e8b20f | 2020-02-27 20:02:49 +0000 | [diff] [blame] | 360 | |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 361 | const CPDF_Dictionary* pJSDict = pNameDict->GetDictFor("JavaScript"); |
| 362 | if (pJSDict) { |
| 363 | const CPDF_Array* pArray = pJSDict->GetArrayFor("Names"); |
| 364 | if (pArray) { |
| 365 | for (size_t i = 0; i < pArray->size(); i++) { |
| 366 | ByteString cbStr = pArray->GetStringAt(i); |
Lei Zhang | 814e3bf | 2021-04-24 01:45:51 +0000 | [diff] [blame] | 367 | if (cbStr == "com.adobe.acrobat.SharedReview.Register") { |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 368 | RaiseUnsupportedError(FPDF_UNSP_DOC_SHAREDREVIEW); |
Lei Zhang | 6e8b20f | 2020-02-27 20:02:49 +0000 | [diff] [blame] | 369 | break; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 374 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 375 | |
Lei Zhang | 4745a47 | 2020-02-27 18:24:58 +0000 | [diff] [blame] | 376 | // SharedForm |
| 377 | const CPDF_Stream* pStream = pRootDict->GetStreamFor("Metadata"); |
| 378 | if (pStream) { |
| 379 | CPDF_Metadata metadata(pStream); |
| 380 | for (const UnsupportedFeature& feature : metadata.CheckForSharedForm()) |
| 381 | RaiseUnsupportedError(static_cast<int>(feature)); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 382 | } |
Daniel Hosseinian | a793e97 | 2020-01-24 02:51:31 +0000 | [diff] [blame] | 383 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 384 | |
Lei Zhang | 85e09df | 2020-02-27 14:48:07 +0000 | [diff] [blame] | 385 | void ReportUnsupportedXFA(const CPDF_Document* pDoc) { |
| 386 | if (!pDoc->GetExtension() && DocHasXFA(pDoc)) |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 387 | RaiseUnsupportedError(FPDF_UNSP_DOC_XFAFORM); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 390 | void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot) { |
| 391 | switch (pAnnot->GetSubtype()) { |
| 392 | case CPDF_Annot::Subtype::FILEATTACHMENT: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 393 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_ATTACHMENT); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 394 | break; |
| 395 | case CPDF_Annot::Subtype::MOVIE: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 396 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_MOVIE); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 397 | break; |
| 398 | case CPDF_Annot::Subtype::RICHMEDIA: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 399 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 400 | break; |
| 401 | case CPDF_Annot::Subtype::SCREEN: { |
| 402 | const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict(); |
Lei Zhang | 6c71502 | 2019-02-26 20:16:09 +0000 | [diff] [blame] | 403 | ByteString cbString = pAnnotDict->GetStringFor("IT"); |
| 404 | if (cbString != "Img") |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 405 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_MEDIA); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | case CPDF_Annot::Subtype::SOUND: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 409 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SOUND); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 410 | break; |
| 411 | case CPDF_Annot::Subtype::THREED: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 412 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_3DANNOT); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 413 | break; |
| 414 | case CPDF_Annot::Subtype::WIDGET: { |
| 415 | const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict(); |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 416 | ByteString cbString = pAnnotDict->GetStringFor(pdfium::form_fields::kFT); |
Lei Zhang | f496e25 | 2019-02-26 20:20:19 +0000 | [diff] [blame] | 417 | if (cbString == pdfium::form_fields::kSig) |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 418 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SIG); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | default: |
| 422 | break; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 426 | void ProcessParseError(CPDF_Parser::Error err) { |
| 427 | uint32_t err_code = FPDF_ERR_SUCCESS; |
| 428 | // Translate FPDFAPI error code to FPDFVIEW error code |
| 429 | switch (err) { |
| 430 | case CPDF_Parser::SUCCESS: |
| 431 | err_code = FPDF_ERR_SUCCESS; |
| 432 | break; |
| 433 | case CPDF_Parser::FILE_ERROR: |
| 434 | err_code = FPDF_ERR_FILE; |
| 435 | break; |
| 436 | case CPDF_Parser::FORMAT_ERROR: |
| 437 | err_code = FPDF_ERR_FORMAT; |
| 438 | break; |
| 439 | case CPDF_Parser::PASSWORD_ERROR: |
| 440 | err_code = FPDF_ERR_PASSWORD; |
| 441 | break; |
| 442 | case CPDF_Parser::HANDLER_ERROR: |
| 443 | err_code = FPDF_ERR_SECURITY; |
| 444 | break; |
| 445 | } |
Tom Sepez | 04e3af8 | 2019-08-05 23:41:06 +0000 | [diff] [blame] | 446 | FXSYS_SetLastError(err_code); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 447 | } |
Gourab Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 448 | |
| 449 | void SetColorFromScheme(const FPDF_COLORSCHEME* pColorScheme, |
| 450 | CPDF_RenderOptions* pRenderOptions) { |
| 451 | CPDF_RenderOptions::ColorScheme color_scheme; |
Tom Sepez | 5ea5fa9 | 2022-03-08 00:15:23 +0000 | [diff] [blame] | 452 | color_scheme.path_fill_color = |
| 453 | static_cast<FX_ARGB>(pColorScheme->path_fill_color); |
| 454 | color_scheme.path_stroke_color = |
| 455 | static_cast<FX_ARGB>(pColorScheme->path_stroke_color); |
| 456 | color_scheme.text_fill_color = |
| 457 | static_cast<FX_ARGB>(pColorScheme->text_fill_color); |
| 458 | color_scheme.text_stroke_color = |
| 459 | static_cast<FX_ARGB>(pColorScheme->text_stroke_color); |
Gourab Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 460 | pRenderOptions->SetColorScheme(color_scheme); |
| 461 | } |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 462 | |
| 463 | std::vector<uint32_t> ParsePageRangeString(const ByteString& bsPageRange, |
| 464 | uint32_t nCount) { |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 465 | ByteStringView alphabet(" 0123456789-,"); |
| 466 | for (const auto& ch : bsPageRange) { |
| 467 | if (!alphabet.Contains(ch)) |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 468 | return std::vector<uint32_t>(); |
| 469 | } |
| 470 | |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 471 | ByteString bsStrippedPageRange = bsPageRange; |
| 472 | bsStrippedPageRange.Remove(' '); |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 473 | |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 474 | std::vector<uint32_t> results; |
| 475 | for (const auto& entry : fxcrt::Split(bsStrippedPageRange, ',')) { |
| 476 | std::vector<ByteString> args = fxcrt::Split(entry, '-'); |
| 477 | if (args.size() == 1) { |
| 478 | uint32_t page_num = |
| 479 | pdfium::base::checked_cast<uint32_t>(atoi(args[0].c_str())); |
| 480 | if (page_num == 0 || page_num > nCount) |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 481 | return std::vector<uint32_t>(); |
Daniel Hosseinian | c7cb405 | 2021-06-11 21:00:47 +0000 | [diff] [blame] | 482 | results.push_back(page_num - 1); |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 483 | } else if (args.size() == 2) { |
| 484 | uint32_t first_num = |
| 485 | pdfium::base::checked_cast<uint32_t>(atoi(args[0].c_str())); |
| 486 | if (first_num == 0) |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 487 | return std::vector<uint32_t>(); |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 488 | uint32_t last_num = |
| 489 | pdfium::base::checked_cast<uint32_t>(atoi(args[1].c_str())); |
| 490 | if (last_num == 0 || first_num > last_num || last_num > nCount) |
| 491 | return std::vector<uint32_t>(); |
| 492 | for (uint32_t i = first_num; i <= last_num; ++i) |
Daniel Hosseinian | c7cb405 | 2021-06-11 21:00:47 +0000 | [diff] [blame] | 493 | results.push_back(i - 1); |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 494 | } else { |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 495 | return std::vector<uint32_t>(); |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 496 | } |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 497 | } |
Tom Sepez | 1e2d7df | 2021-04-07 19:26:06 +0000 | [diff] [blame] | 498 | return results; |
Tom Sepez | c524b1f | 2021-04-06 00:09:39 +0000 | [diff] [blame] | 499 | } |