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