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