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" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 17 | #include "core/fpdfdoc/cpdf_annot.h" |
Lei Zhang | c345065 | 2018-10-11 16:54:42 +0000 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_interactiveform.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 19 | #include "core/fpdfdoc/cpdf_metadata.h" |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 20 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 24 | constexpr char kQuadPoints[] = "QuadPoints"; |
| 25 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 26 | // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS |
Lei Zhang | bc10648 | 2019-05-30 23:55:19 +0000 | [diff] [blame] | 27 | uint32_t g_sandbox_policy = 0xFFFFFFFF; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 28 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 29 | UNSUPPORT_INFO* g_unsupport_info = nullptr; |
| 30 | |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 31 | bool RaiseUnsupportedError(int nError) { |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 32 | if (!g_unsupport_info) |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 33 | return false; |
| 34 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 35 | if (g_unsupport_info->FSDK_UnSupport_Handler) |
| 36 | g_unsupport_info->FSDK_UnSupport_Handler(g_unsupport_info, nError); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 37 | return true; |
| 38 | } |
| 39 | |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 40 | unsigned long GetStreamMaybeCopyAndReturnLengthImpl(const CPDF_Stream* stream, |
| 41 | void* buffer, |
| 42 | unsigned long buflen, |
| 43 | bool decode) { |
| 44 | ASSERT(stream); |
| 45 | auto stream_acc = pdfium::MakeRetain<CPDF_StreamAcc>(stream); |
| 46 | |
| 47 | if (decode) |
| 48 | stream_acc->LoadAllDataFiltered(); |
| 49 | else |
| 50 | stream_acc->LoadAllDataRaw(); |
| 51 | |
| 52 | const auto stream_data_size = stream_acc->GetSize(); |
| 53 | if (!buffer || buflen < stream_data_size) |
| 54 | return stream_data_size; |
| 55 | |
| 56 | memcpy(buffer, stream_acc->GetData(), stream_data_size); |
| 57 | return stream_data_size; |
| 58 | } |
| 59 | |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 60 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 61 | class FPDF_FileHandlerContext final : public IFX_SeekableStream { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 62 | public: |
| 63 | template <typename T, typename... Args> |
| 64 | friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
| 65 | |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 66 | // IFX_SeekableStream: |
| 67 | FX_FILESIZE GetSize() override; |
| 68 | bool IsEOF() override; |
| 69 | FX_FILESIZE GetPosition() override; |
Lei Zhang | f6a7921 | 2018-11-15 20:17:49 +0000 | [diff] [blame] | 70 | bool ReadBlockAtOffset(void* buffer, |
| 71 | FX_FILESIZE offset, |
| 72 | size_t size) override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 73 | size_t ReadBlock(void* buffer, size_t size) override; |
Lei Zhang | 59f7623 | 2018-11-15 20:22:59 +0000 | [diff] [blame] | 74 | bool WriteBlockAtOffset(const void* buffer, |
| 75 | FX_FILESIZE offset, |
| 76 | size_t size) override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 77 | bool Flush() override; |
| 78 | |
| 79 | void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } |
| 80 | |
Tom Sepez | cb79825 | 2018-09-17 18:25:32 +0000 | [diff] [blame] | 81 | private: |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 82 | explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS); |
Lei Zhang | 86688de | 2018-05-22 22:06:49 +0000 | [diff] [blame] | 83 | ~FPDF_FileHandlerContext() override; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 84 | |
| 85 | FPDF_FILEHANDLER* m_pFS; |
| 86 | FX_FILESIZE m_nCurPos; |
| 87 | }; |
| 88 | |
| 89 | FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) { |
| 90 | m_pFS = pFS; |
| 91 | m_nCurPos = 0; |
| 92 | } |
| 93 | |
| 94 | FPDF_FileHandlerContext::~FPDF_FileHandlerContext() { |
| 95 | if (m_pFS && m_pFS->Release) |
| 96 | m_pFS->Release(m_pFS->clientData); |
| 97 | } |
| 98 | |
| 99 | FX_FILESIZE FPDF_FileHandlerContext::GetSize() { |
| 100 | if (m_pFS && m_pFS->GetSize) |
| 101 | return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | bool FPDF_FileHandlerContext::IsEOF() { |
| 106 | return m_nCurPos >= GetSize(); |
| 107 | } |
| 108 | |
| 109 | FX_FILESIZE FPDF_FileHandlerContext::GetPosition() { |
| 110 | return m_nCurPos; |
| 111 | } |
| 112 | |
Lei Zhang | f6a7921 | 2018-11-15 20:17:49 +0000 | [diff] [blame] | 113 | bool FPDF_FileHandlerContext::ReadBlockAtOffset(void* buffer, |
| 114 | FX_FILESIZE offset, |
| 115 | size_t size) { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 116 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 117 | return false; |
| 118 | |
| 119 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 120 | (FPDF_DWORD)size) == 0) { |
| 121 | m_nCurPos = offset + size; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) { |
| 128 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 129 | return 0; |
| 130 | |
| 131 | FX_FILESIZE nSize = GetSize(); |
| 132 | if (m_nCurPos >= nSize) |
| 133 | return 0; |
| 134 | FX_FILESIZE dwAvail = nSize - m_nCurPos; |
| 135 | if (dwAvail < (FX_FILESIZE)size) |
| 136 | size = static_cast<size_t>(dwAvail); |
| 137 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer, |
| 138 | (FPDF_DWORD)size) == 0) { |
| 139 | m_nCurPos += size; |
| 140 | return size; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Lei Zhang | 59f7623 | 2018-11-15 20:22:59 +0000 | [diff] [blame] | 146 | bool FPDF_FileHandlerContext::WriteBlockAtOffset(const void* buffer, |
| 147 | FX_FILESIZE offset, |
| 148 | size_t size) { |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 149 | if (!m_pFS || !m_pFS->WriteBlock) |
| 150 | return false; |
| 151 | |
| 152 | if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 153 | (FPDF_DWORD)size) == 0) { |
| 154 | m_nCurPos = offset + size; |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | bool FPDF_FileHandlerContext::Flush() { |
| 161 | if (!m_pFS || !m_pFS->Flush) |
| 162 | return true; |
| 163 | |
| 164 | return m_pFS->Flush(m_pFS->clientData) == 0; |
| 165 | } |
| 166 | #endif // PDF_ENABLE_XFA |
| 167 | |
| 168 | } // namespace |
| 169 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 170 | IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page) { |
| 171 | return reinterpret_cast<IPDF_Page*>(page); |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 174 | FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page) { |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 175 | return reinterpret_cast<FPDF_PAGE>(page); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { |
Tom Sepez | fe06d51 | 2018-05-01 17:25:25 +0000 | [diff] [blame] | 179 | return reinterpret_cast<CPDF_Document*>(doc); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { |
Tom Sepez | fe06d51 | 2018-05-01 17:25:25 +0000 | [diff] [blame] | 183 | return reinterpret_cast<FPDF_DOCUMENT>(doc); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 187 | return page ? IPDFPageFromFPDFPage(page)->AsPDFPage() : nullptr; |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 190 | CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) { |
| 191 | CPDFSDK_FormFillEnvironment* pFormFillEnv = |
| 192 | CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle); |
| 193 | return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr; |
| 194 | } |
| 195 | |
Lei Zhang | b46a763 | 2019-01-09 02:56:16 +0000 | [diff] [blame] | 196 | ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 197 | return WideStringFromFPDFWideString(wide_string).ToUTF8(); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 200 | WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 201 | return WideString::FromUTF16LE(wide_string, |
| 202 | WideString::WStringLength(wide_string)); |
| 203 | } |
| 204 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 205 | #ifdef PDF_ENABLE_XFA |
| 206 | RetainPtr<IFX_SeekableStream> MakeSeekableStream( |
| 207 | FPDF_FILEHANDLER* pFilehandler) { |
| 208 | return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 209 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 210 | #endif // PDF_ENABLE_XFA |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 211 | |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 212 | const CPDF_Array* GetQuadPointsArrayFromDictionary( |
| 213 | const CPDF_Dictionary* dict) { |
Lei Zhang | d934c64 | 2019-03-04 19:42:00 +0000 | [diff] [blame] | 214 | return dict->GetArrayFor("QuadPoints"); |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict) { |
Lei Zhang | d934c64 | 2019-03-04 19:42:00 +0000 | [diff] [blame] | 218 | return dict->GetArrayFor("QuadPoints"); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 221 | CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 222 | return dict->SetNewFor<CPDF_Array>(kQuadPoints); |
| 223 | } |
| 224 | |
| 225 | bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index) { |
Lei Zhang | f40380f | 2018-10-12 18:31:51 +0000 | [diff] [blame] | 226 | return array && index < array->size() / 8; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool GetQuadPointsAtIndex(const CPDF_Array* array, |
| 230 | size_t quad_index, |
| 231 | FS_QUADPOINTSF* quad_points) { |
| 232 | ASSERT(quad_points); |
| 233 | ASSERT(array); |
| 234 | |
| 235 | if (!IsValidQuadPointsIndex(array, quad_index)) |
| 236 | return false; |
| 237 | |
| 238 | quad_index *= 8; |
| 239 | quad_points->x1 = array->GetNumberAt(quad_index); |
| 240 | quad_points->y1 = array->GetNumberAt(quad_index + 1); |
| 241 | quad_points->x2 = array->GetNumberAt(quad_index + 2); |
| 242 | quad_points->y2 = array->GetNumberAt(quad_index + 3); |
| 243 | quad_points->x3 = array->GetNumberAt(quad_index + 4); |
| 244 | quad_points->y3 = array->GetNumberAt(quad_index + 5); |
| 245 | quad_points->x4 = array->GetNumberAt(quad_index + 6); |
| 246 | quad_points->y4 = array->GetNumberAt(quad_index + 7); |
| 247 | return true; |
| 248 | } |
| 249 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 250 | CFX_FloatRect CFXFloatRectFromFSRECTF(const FS_RECTF& rect) { |
| 251 | return CFX_FloatRect(rect.left, rect.bottom, rect.right, rect.top); |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 252 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 253 | |
Lei Zhang | 3567c61 | 2019-11-18 18:10:02 +0000 | [diff] [blame] | 254 | FS_RECTF FSRECTFFromCFXFloatRect(const CFX_FloatRect& rect) { |
| 255 | return {rect.left, rect.top, rect.right, rect.bottom}; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Lei Zhang | 6fef1e4 | 2018-12-20 19:14:02 +0000 | [diff] [blame] | 258 | CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix) { |
| 259 | return CFX_Matrix(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f); |
| 260 | } |
| 261 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 262 | unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text, |
| 263 | void* buffer, |
| 264 | unsigned long buflen) { |
| 265 | ByteString encoded_text = text.ToUTF16LE(); |
| 266 | unsigned long len = encoded_text.GetLength(); |
| 267 | if (buffer && len <= buflen) |
| 268 | memcpy(buffer, encoded_text.c_str(), len); |
| 269 | return len; |
| 270 | } |
| 271 | |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 272 | unsigned long GetRawStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream, |
| 273 | void* buffer, |
| 274 | unsigned long buflen) { |
| 275 | return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen, |
| 276 | /*decode=*/false); |
| 277 | } |
| 278 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 279 | unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream, |
| 280 | void* buffer, |
| 281 | unsigned long buflen) { |
Jeremy Chinsen | 617a2e8 | 2019-06-20 00:11:12 +0000 | [diff] [blame] | 282 | return GetStreamMaybeCopyAndReturnLengthImpl(stream, buffer, buflen, |
| 283 | /*decode=*/true); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Tom Sepez | 69a4a70 | 2019-07-31 17:59:49 +0000 | [diff] [blame] | 286 | void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 287 | switch (policy) { |
| 288 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame^] | 289 | uint32_t mask = 1 << policy; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 290 | if (enable) |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame^] | 291 | g_sandbox_policy |= mask; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 292 | else |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame^] | 293 | g_sandbox_policy &= ~mask; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 294 | } break; |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
Tom Sepez | 69a4a70 | 2019-07-31 17:59:49 +0000 | [diff] [blame] | 300 | FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy) { |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 301 | switch (policy) { |
Tom Sepez | fe285c3 | 2019-12-04 18:38:03 +0000 | [diff] [blame^] | 302 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
| 303 | uint32_t mask = 1 << policy; |
| 304 | return !!(g_sandbox_policy & mask); |
| 305 | } |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 306 | default: |
| 307 | return false; |
| 308 | } |
| 309 | } |
| 310 | |
Tom Sepez | 20c946f | 2019-07-31 19:33:21 +0000 | [diff] [blame] | 311 | void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info) { |
| 312 | g_unsupport_info = unsp_info; |
| 313 | } |
| 314 | |
| 315 | UNSUPPORT_INFO* GetPDFUnssuportInto() { |
| 316 | return g_unsupport_info; |
| 317 | } |
| 318 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 319 | void ReportUnsupportedFeatures(CPDF_Document* pDoc) { |
| 320 | const CPDF_Dictionary* pRootDict = pDoc->GetRoot(); |
| 321 | if (pRootDict) { |
| 322 | // Portfolios and Packages |
| 323 | if (pRootDict->KeyExist("Collection")) { |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 324 | RaiseUnsupportedError(FPDF_UNSP_DOC_PORTABLECOLLECTION); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | if (pRootDict->KeyExist("Names")) { |
| 328 | const CPDF_Dictionary* pNameDict = pRootDict->GetDictFor("Names"); |
| 329 | if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) { |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 330 | RaiseUnsupportedError(FPDF_UNSP_DOC_ATTACHMENT); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | if (pNameDict && pNameDict->KeyExist("JavaScript")) { |
| 334 | const CPDF_Dictionary* pJSDict = pNameDict->GetDictFor("JavaScript"); |
| 335 | const CPDF_Array* pArray = |
| 336 | pJSDict ? pJSDict->GetArrayFor("Names") : nullptr; |
| 337 | if (pArray) { |
| 338 | for (size_t i = 0; i < pArray->size(); i++) { |
| 339 | ByteString cbStr = pArray->GetStringAt(i); |
| 340 | if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) { |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 341 | RaiseUnsupportedError(FPDF_UNSP_DOC_SHAREDREVIEW); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // SharedForm |
| 350 | const CPDF_Stream* pStream = pRootDict->GetStreamFor("Metadata"); |
| 351 | if (pStream) { |
| 352 | CPDF_Metadata metaData(pStream); |
| 353 | for (const auto& err : metaData.CheckForSharedForm()) |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 354 | RaiseUnsupportedError(static_cast<int>(err)); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | // XFA Forms |
| 359 | if (!pDoc->GetExtension() && CPDF_InteractiveForm(pDoc).HasXFAForm()) |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 360 | RaiseUnsupportedError(FPDF_UNSP_DOC_XFAFORM); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 363 | void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot) { |
| 364 | switch (pAnnot->GetSubtype()) { |
| 365 | case CPDF_Annot::Subtype::FILEATTACHMENT: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 366 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_ATTACHMENT); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 367 | break; |
| 368 | case CPDF_Annot::Subtype::MOVIE: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 369 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_MOVIE); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 370 | break; |
| 371 | case CPDF_Annot::Subtype::RICHMEDIA: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 372 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 373 | break; |
| 374 | case CPDF_Annot::Subtype::SCREEN: { |
| 375 | const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict(); |
Lei Zhang | 6c71502 | 2019-02-26 20:16:09 +0000 | [diff] [blame] | 376 | ByteString cbString = pAnnotDict->GetStringFor("IT"); |
| 377 | if (cbString != "Img") |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 378 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SCREEN_MEDIA); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | case CPDF_Annot::Subtype::SOUND: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 382 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SOUND); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 383 | break; |
| 384 | case CPDF_Annot::Subtype::THREED: |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 385 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_3DANNOT); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 386 | break; |
| 387 | case CPDF_Annot::Subtype::WIDGET: { |
| 388 | const CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict(); |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 389 | ByteString cbString = pAnnotDict->GetStringFor(pdfium::form_fields::kFT); |
Lei Zhang | f496e25 | 2019-02-26 20:20:19 +0000 | [diff] [blame] | 390 | if (cbString == pdfium::form_fields::kSig) |
Lei Zhang | b7d09ca | 2019-02-27 23:50:44 +0000 | [diff] [blame] | 391 | RaiseUnsupportedError(FPDF_UNSP_ANNOT_SIG); |
Lei Zhang | 4efdb51 | 2019-02-26 19:48:39 +0000 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | default: |
| 395 | break; |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 399 | void ProcessParseError(CPDF_Parser::Error err) { |
| 400 | uint32_t err_code = FPDF_ERR_SUCCESS; |
| 401 | // Translate FPDFAPI error code to FPDFVIEW error code |
| 402 | switch (err) { |
| 403 | case CPDF_Parser::SUCCESS: |
| 404 | err_code = FPDF_ERR_SUCCESS; |
| 405 | break; |
| 406 | case CPDF_Parser::FILE_ERROR: |
| 407 | err_code = FPDF_ERR_FILE; |
| 408 | break; |
| 409 | case CPDF_Parser::FORMAT_ERROR: |
| 410 | err_code = FPDF_ERR_FORMAT; |
| 411 | break; |
| 412 | case CPDF_Parser::PASSWORD_ERROR: |
| 413 | err_code = FPDF_ERR_PASSWORD; |
| 414 | break; |
| 415 | case CPDF_Parser::HANDLER_ERROR: |
| 416 | err_code = FPDF_ERR_SECURITY; |
| 417 | break; |
| 418 | } |
Tom Sepez | 04e3af8 | 2019-08-05 23:41:06 +0000 | [diff] [blame] | 419 | FXSYS_SetLastError(err_code); |
Lei Zhang | 65a8d5e | 2018-12-20 19:13:21 +0000 | [diff] [blame] | 420 | } |