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