John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_dataavail.h" |
| 8 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_data_avail.h" |
| 13 | #include "core/fpdfapi/parser/cpdf_document.h" |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 14 | #include "core/fxcrt/cfx_retain_ptr.h" |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 15 | #include "core/fxcrt/fx_stream.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/fsdk_define.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 17 | #include "public/fpdf_formfill.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 18 | #include "third_party/base/ptr_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 20 | // These checks are here because core/ and public/ cannot depend on each other. |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 21 | static_assert(CPDF_DataAvail::DataError == PDF_DATA_ERROR, |
| 22 | "CPDF_DataAvail::DataError value mismatch"); |
| 23 | static_assert(CPDF_DataAvail::DataNotAvailable == PDF_DATA_NOTAVAIL, |
| 24 | "CPDF_DataAvail::DataNotAvailable value mismatch"); |
| 25 | static_assert(CPDF_DataAvail::DataAvailable == PDF_DATA_AVAIL, |
| 26 | "CPDF_DataAvail::DataAvailable value mismatch"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 27 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 28 | static_assert(CPDF_DataAvail::LinearizationUnknown == PDF_LINEARIZATION_UNKNOWN, |
| 29 | "CPDF_DataAvail::LinearizationUnknown value mismatch"); |
| 30 | static_assert(CPDF_DataAvail::NotLinearized == PDF_NOT_LINEARIZED, |
| 31 | "CPDF_DataAvail::NotLinearized value mismatch"); |
| 32 | static_assert(CPDF_DataAvail::Linearized == PDF_LINEARIZED, |
| 33 | "CPDF_DataAvail::Linearized value mismatch"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 34 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 35 | static_assert(CPDF_DataAvail::FormError == PDF_FORM_ERROR, |
| 36 | "CPDF_DataAvail::FormError value mismatch"); |
| 37 | static_assert(CPDF_DataAvail::FormNotAvailable == PDF_FORM_NOTAVAIL, |
| 38 | "CPDF_DataAvail::FormNotAvailable value mismatch"); |
| 39 | static_assert(CPDF_DataAvail::FormAvailable == PDF_FORM_AVAIL, |
| 40 | "CPDF_DataAvail::FormAvailable value mismatch"); |
| 41 | static_assert(CPDF_DataAvail::FormNotExist == PDF_FORM_NOTEXIST, |
| 42 | "CPDF_DataAvail::FormNotExist value mismatch"); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 43 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | class CFPDF_FileAvailWrap : public CPDF_DataAvail::FileAvail { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | public: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 48 | CFPDF_FileAvailWrap() : m_pfileAvail(nullptr) {} |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 49 | ~CFPDF_FileAvailWrap() override {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 53 | // CPDF_DataAvail::FileAvail: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 54 | bool IsDataAvail(FX_FILESIZE offset, uint32_t size) override { |
tsepez | 95e5834 | 2016-10-28 11:34:42 -0700 | [diff] [blame] | 55 | return !!m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 56 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | private: |
| 59 | FX_FILEAVAIL* m_pfileAvail; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 60 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 61 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 62 | class CFPDF_FileAccessWrap : public IFX_SeekableReadStream { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | public: |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 64 | template <typename T, typename... Args> |
| 65 | friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
| 66 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 67 | ~CFPDF_FileAccessWrap() override {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 68 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 70 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 71 | // IFX_SeekableReadStream |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 72 | FX_FILESIZE GetSize() override { return m_pFileAccess->m_FileLen; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 74 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
| 75 | return !!m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, |
| 76 | (uint8_t*)buffer, size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 78 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 80 | CFPDF_FileAccessWrap() : m_pFileAccess(nullptr) {} |
| 81 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | FPDF_FILEACCESS* m_pFileAccess; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 85 | class CFPDF_DownloadHintsWrap : public CPDF_DataAvail::DownloadHints { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | public: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 87 | explicit CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | m_pDownloadHints = pDownloadHints; |
| 89 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 90 | ~CFPDF_DownloadHintsWrap() override {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | |
| 92 | public: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 93 | // IFX_DownloadHints |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 94 | void AddSegment(FX_FILESIZE offset, uint32_t size) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | FX_DOWNLOADHINTS* m_pDownloadHints; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | class CFPDF_DataAvail { |
| 103 | public: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 104 | CFPDF_DataAvail() |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 105 | : m_FileAvail(pdfium::MakeUnique<CFPDF_FileAvailWrap>()), |
| 106 | m_FileRead(pdfium::MakeRetain<CFPDF_FileAccessWrap>()) {} |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 107 | ~CFPDF_DataAvail() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 108 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 109 | std::unique_ptr<CFPDF_FileAvailWrap> m_FileAvail; |
| 110 | CFX_RetainPtr<CFPDF_FileAccessWrap> m_FileRead; |
Artem Strygin | 834ebec | 2017-07-27 14:01:32 +0300 | [diff] [blame] | 111 | std::unique_ptr<CPDF_DataAvail> m_pDataAvail; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 114 | CFPDF_DataAvail* CFPDFDataAvailFromFPDFAvail(FPDF_AVAIL avail) { |
| 115 | return static_cast<CFPDF_DataAvail*>(avail); |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 120 | FPDF_EXPORT FPDF_AVAIL FPDF_CALLCONV FPDFAvail_Create(FX_FILEAVAIL* file_avail, |
| 121 | FPDF_FILEACCESS* file) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 122 | auto pAvail = pdfium::MakeUnique<CFPDF_DataAvail>(); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 123 | pAvail->m_FileAvail->Set(file_avail); |
| 124 | pAvail->m_FileRead->Set(file); |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 125 | pAvail->m_pDataAvail = pdfium::MakeUnique<CPDF_DataAvail>( |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 126 | pAvail->m_FileAvail.get(), pAvail->m_FileRead, true); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 127 | return pAvail.release(); // Caller takes ownership. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 130 | FPDF_EXPORT void FPDF_CALLCONV FPDFAvail_Destroy(FPDF_AVAIL avail) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 131 | // Take ownership back from caller and destroy. |
| 132 | std::unique_ptr<CFPDF_DataAvail>(static_cast<CFPDF_DataAvail*>(avail)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 135 | FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsDocAvail(FPDF_AVAIL avail, |
| 136 | FX_DOWNLOADHINTS* hints) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 137 | if (!avail || !hints) |
| 138 | return PDF_DATA_ERROR; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 140 | return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsDocAvail( |
| 141 | &hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 144 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) { |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 146 | CFPDF_DataAvail* pDataAvail = static_cast<CFPDF_DataAvail*>(avail); |
| 147 | if (!pDataAvail) |
Lei Zhang | aa23e70 | 2016-01-29 18:03:40 -0800 | [diff] [blame] | 148 | return nullptr; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 149 | |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 150 | auto pParser = pdfium::MakeUnique<CPDF_Parser>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 151 | pParser->SetPassword(password); |
tsepez | 4540fba | 2016-08-16 11:12:21 -0700 | [diff] [blame] | 152 | |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 153 | auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 154 | CPDF_Parser::Error error = pDocument->GetParser()->StartLinearizedParse( |
| 155 | pDataAvail->m_pDataAvail->GetFileRead(), pDocument.get()); |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 156 | if (error != CPDF_Parser::SUCCESS) { |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 157 | ProcessParseError(error); |
Lei Zhang | aa23e70 | 2016-01-29 18:03:40 -0800 | [diff] [blame] | 158 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | } |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 160 | pDataAvail->m_pDataAvail->SetDocument(pDocument.get()); |
| 161 | CheckUnSupportError(pDocument.get(), FPDF_ERR_SUCCESS); |
| 162 | return FPDFDocumentFromCPDFDocument(pDocument.release()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 165 | FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 166 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); |
| 167 | return pDoc ? pDoc->GetParser()->GetFirstPageNo() : 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 170 | FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsPageAvail(FPDF_AVAIL avail, |
| 171 | int page_index, |
| 172 | FX_DOWNLOADHINTS* hints) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 173 | if (!avail || !hints) |
| 174 | return PDF_DATA_ERROR; |
thestig | e0cb687 | 2016-09-01 13:41:10 -0700 | [diff] [blame] | 175 | if (page_index < 0) |
| 176 | return PDF_DATA_NOTAVAIL; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 178 | return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsPageAvail( |
| 179 | page_index, &hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 182 | FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsFormAvail(FPDF_AVAIL avail, |
| 183 | FX_DOWNLOADHINTS* hints) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 184 | if (!avail || !hints) |
| 185 | return PDF_FORM_ERROR; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 187 | return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsFormAvail( |
| 188 | &hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 191 | FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsLinearized(FPDF_AVAIL avail) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 192 | if (!avail) |
| 193 | return PDF_LINEARIZATION_UNKNOWN; |
thestig | 979070b | 2016-06-14 10:44:01 -0700 | [diff] [blame] | 194 | return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsLinearizedPDF(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 195 | } |