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 | |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 7 | #include "../../public/fpdf_dataavail.h" |
| 8 | #include "../../public/fpdf_formfill.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | #include "../include/fsdk_define.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 10 | #include "../include/fpdfxfa/fpdfxfa_doc.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 11 | #include "../include/fpdfxfa/fpdfxfa_app.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 12 | |
| 13 | extern void ProcessParseError(FX_DWORD err_code); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 14 | class CFPDF_FileAvailWrap : public IFX_FileAvail { |
| 15 | public: |
| 16 | CFPDF_FileAvailWrap() { m_pfileAvail = NULL; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 17 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 18 | void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 20 | virtual FX_BOOL IsDataAvail(FX_FILESIZE offset, FX_DWORD size) { |
| 21 | return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size); |
| 22 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | private: |
| 25 | FX_FILEAVAIL* m_pfileAvail; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 26 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 28 | class CFPDF_FileAccessWrap : public IFX_FileRead { |
| 29 | public: |
| 30 | CFPDF_FileAccessWrap() { m_pFileAccess = NULL; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 32 | void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | virtual FX_FILESIZE GetSize() { return m_pFileAccess->m_FileLen; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { |
| 37 | return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, |
| 38 | (uint8_t*)buffer, size); |
| 39 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | virtual void Release() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 42 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | private: |
| 44 | FPDF_FILEACCESS* m_pFileAccess; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | class CFPDF_DownloadHintsWrap : public IFX_DownloadHints { |
| 48 | public: |
| 49 | CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) { |
| 50 | m_pDownloadHints = pDownloadHints; |
| 51 | } |
| 52 | |
| 53 | public: |
| 54 | virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size) { |
| 55 | m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | FX_DOWNLOADHINTS* m_pDownloadHints; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | class CFPDF_DataAvail { |
| 63 | public: |
| 64 | CFPDF_DataAvail() { m_pDataAvail = NULL; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 65 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | ~CFPDF_DataAvail() { delete m_pDataAvail; } |
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 | IPDF_DataAvail* m_pDataAvail; |
| 69 | CFPDF_FileAvailWrap m_FileAvail; |
| 70 | CFPDF_FileAccessWrap m_FileRead; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail, |
| 74 | FPDF_FILEACCESS* file) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame^] | 75 | CFPDF_DataAvail* pAvail = new CFPDF_DataAvail; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | pAvail->m_FileAvail.Set(file_avail); |
| 77 | pAvail->m_FileRead.Set(file); |
| 78 | pAvail->m_pDataAvail = |
| 79 | IPDF_DataAvail::Create(&pAvail->m_FileAvail, &pAvail->m_FileRead); |
| 80 | return pAvail; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail) { |
| 84 | delete (CFPDF_DataAvail*)avail; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | DLLEXPORT int STDCALL FPDFAvail_IsDocAvail(FPDF_AVAIL avail, |
| 88 | FX_DOWNLOADHINTS* hints) { |
| 89 | if (avail == NULL || hints == NULL) |
| 90 | return 0; |
| 91 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
| 92 | return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsDocAvail(&hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | extern void CheckUnSupportError(CPDF_Document* pDoc, FX_DWORD err_code); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | DLLEXPORT FPDF_DOCUMENT STDCALL |
| 98 | FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) { |
| 99 | if (avail == NULL) |
| 100 | return NULL; |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame^] | 101 | CPDF_Parser* pParser = new CPDF_Parser; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | pParser->SetPassword(password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 103 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | FX_DWORD err_code = pParser->StartAsynParse( |
| 105 | ((CFPDF_DataAvail*)avail)->m_pDataAvail->GetFileRead()); |
| 106 | if (err_code) { |
| 107 | delete pParser; |
| 108 | ProcessParseError(err_code); |
| 109 | return NULL; |
| 110 | } |
| 111 | ((CFPDF_DataAvail*)avail)->m_pDataAvail->SetDocument(pParser->GetDocument()); |
| 112 | CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS); |
| 113 | CPDF_Document* pPDFDoc = pParser->GetDocument(); |
| 114 | CPDFXFA_App* pApp = CPDFXFA_App::GetInstance(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame^] | 115 | CPDFXFA_Document* pDocument = new CPDFXFA_Document(pPDFDoc, pApp); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | return pDocument; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 119 | DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) { |
| 120 | if (doc == NULL) |
| 121 | return 0; |
| 122 | CPDF_Document* pDoc = ((CPDFXFA_Document*)doc)->GetPDFDoc(); |
| 123 | return ((CPDF_Parser*)pDoc->GetParser())->GetFirstPageNo(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail, |
| 127 | int page_index, |
| 128 | FX_DOWNLOADHINTS* hints) { |
| 129 | if (avail == NULL || hints == NULL) |
| 130 | return 0; |
| 131 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
| 132 | return ((CFPDF_DataAvail*)avail) |
| 133 | ->m_pDataAvail->IsPageAvail(page_index, &hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail, |
| 137 | FX_DOWNLOADHINTS* hints) { |
| 138 | if (avail == NULL || hints == NULL) |
| 139 | return -1; |
| 140 | CFPDF_DownloadHintsWrap hints_wrap(hints); |
| 141 | return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsFormAvail(&hints_wrap); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | DLLEXPORT FPDF_BOOL STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail) { |
| 145 | if (avail == NULL) |
| 146 | return -1; |
| 147 | return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsLinearizedPDF(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 148 | } |