blob: 4100fe2db918e2da84fd59e66ae78a7ca697370a [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_dataavail.h"
8#include "../../public/fpdf_formfill.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
Bo Xufdc00a72014-10-28 23:03:33 -070011#include "../include/fpdfxfa/fpdfxfa_app.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
13extern void ProcessParseError(FX_DWORD err_code);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014class CFPDF_FileAvailWrap : public IFX_FileAvail {
15 public:
16 CFPDF_FileAvailWrap() { m_pfileAvail = NULL; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018 void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020 virtual FX_BOOL IsDataAvail(FX_FILESIZE offset, FX_DWORD size) {
21 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size);
22 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 private:
25 FX_FILEAVAIL* m_pfileAvail;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070026};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028class CFPDF_FileAccessWrap : public IFX_FileRead {
29 public:
30 CFPDF_FileAccessWrap() { m_pFileAccess = NULL; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 virtual FX_FILESIZE GetSize() { return m_pFileAccess->m_FileLen; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 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-Malek3f3b45c2014-05-23 17:28:10 -070040
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 virtual void Release() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 private:
44 FPDF_FILEACCESS* m_pFileAccess;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045};
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047class 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-Malek3f3b45c2014-05-23 17:28:10 -070060};
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062class CFPDF_DataAvail {
63 public:
64 CFPDF_DataAvail() { m_pDataAvail = NULL; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 ~CFPDF_DataAvail() { delete m_pDataAvail; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 IPDF_DataAvail* m_pDataAvail;
69 CFPDF_FileAvailWrap m_FileAvail;
70 CFPDF_FileAccessWrap m_FileRead;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071};
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail,
74 FPDF_FILEACCESS* file) {
Tom Sepezae51c812015-08-05 12:34:06 -070075 CFPDF_DataAvail* pAvail = new CFPDF_DataAvail;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 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-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail) {
84 delete (CFPDF_DataAvail*)avail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085}
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095extern void CheckUnSupportError(CPDF_Document* pDoc, FX_DWORD err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097DLLEXPORT FPDF_DOCUMENT STDCALL
98FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) {
99 if (avail == NULL)
100 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -0700101 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 pParser->SetPassword(password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 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 Sepezae51c812015-08-05 12:34:06 -0700115 CPDFXFA_Document* pDocument = new CPDFXFA_Document(pPDFDoc, pApp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 return pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700148}