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