blob: f3fd42a3694c7de4afbf5e5a2ebee085547cc3f7 [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.
4
5// 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);
14class CFPDF_FileAvailWrap : public IFX_FileAvail
15{
16public:
17 CFPDF_FileAvailWrap()
18 {
19 m_pfileAvail = NULL;
20 }
21
22 void Set(FX_FILEAVAIL* pfileAvail)
23 {
24 m_pfileAvail = pfileAvail;
25 }
26
27 virtual FX_BOOL IsDataAvail( FX_FILESIZE offset, FX_DWORD size)
28 {
29 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size);
30 }
31
32private:
33 FX_FILEAVAIL* m_pfileAvail;
34};
35
36class CFPDF_FileAccessWrap : public IFX_FileRead
37{
38public:
39 CFPDF_FileAccessWrap()
40 {
41 m_pFileAccess = NULL;
42 }
43
44 void Set(FPDF_FILEACCESS* pFile)
45 {
46 m_pFileAccess = pFile;
47 }
48
49 virtual FX_FILESIZE GetSize()
50 {
51 return m_pFileAccess->m_FileLen;
52 }
53
54 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
55 {
56 return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, (FX_LPBYTE)buffer, size);
57 }
58
59 virtual void Release()
60 {
61 }
62
63private:
64 FPDF_FILEACCESS* m_pFileAccess;
65};
66
67class CFPDF_DownloadHintsWrap : public IFX_DownloadHints
68{
69public:
70 CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints)
71 {
72 m_pDownloadHints = pDownloadHints;
73 }
74public:
75 virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size)
76 {
77 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size);
78 }
79private:
80 FX_DOWNLOADHINTS* m_pDownloadHints;
81};
82
Tom Sepez6fc8cbb2015-04-14 13:50:34 -070083class CFPDF_DataAvail
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084{
85public:
86 CFPDF_DataAvail()
87 {
88 m_pDataAvail = NULL;
89 }
90
91 ~CFPDF_DataAvail()
92 {
93 if (m_pDataAvail) delete m_pDataAvail;
94 }
95
Tom Sepez19c61602015-01-22 17:00:12 -080096 IPDF_DataAvail* m_pDataAvail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097 CFPDF_FileAvailWrap m_FileAvail;
98 CFPDF_FileAccessWrap m_FileRead;
99};
100
101DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail, FPDF_FILEACCESS* file)
102{
103 CFPDF_DataAvail* pAvail = FX_NEW CFPDF_DataAvail;
104 pAvail->m_FileAvail.Set(file_avail);
105 pAvail->m_FileRead.Set(file);
Tom Sepez19c61602015-01-22 17:00:12 -0800106 pAvail->m_pDataAvail = IPDF_DataAvail::Create(&pAvail->m_FileAvail, &pAvail->m_FileRead);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107 return pAvail;
108}
109
110DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail)
111{
112 if (avail == NULL) return;
113 delete (CFPDF_DataAvail*)avail;
114}
115
116DLLEXPORT int STDCALL FPDFAvail_IsDocAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints)
117{
118 if (avail == NULL || hints == NULL) return 0;
119 CFPDF_DownloadHintsWrap hints_wrap(hints);
120 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsDocAvail(&hints_wrap);
121}
122
123extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
124
125DLLEXPORT FPDF_DOCUMENT STDCALL FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password)
126{
127 if (avail == NULL) return NULL;
128 CPDF_Parser* pParser = FX_NEW CPDF_Parser;
129 pParser->SetPassword(password);
130
131 FX_DWORD err_code = pParser->StartAsynParse(((CFPDF_DataAvail*)avail)->m_pDataAvail->GetFileRead());
132 if (err_code) {
133 delete pParser;
134 ProcessParseError(err_code);
135 return NULL;
136 }
137 ((CFPDF_DataAvail*)avail)->m_pDataAvail->SetDocument(pParser->GetDocument());
138 CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS);
Bo Xufdc00a72014-10-28 23:03:33 -0700139 CPDF_Document* pPDFDoc = pParser->GetDocument();
140
141 CPDFXFA_App* pApp = FPDFXFA_GetApp();
142 CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pApp);
143 //pDocument->LoadXFADoc();
144
145 return pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
148DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc)
149{
150 if (doc == NULL) return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700151 CPDF_Document* pDoc = ((CPDFXFA_Document*)doc)->GetPDFDoc();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152 return ((CPDF_Parser*)pDoc->GetParser())->GetFirstPageNo();
153}
154
155DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail, int page_index, FX_DOWNLOADHINTS* hints)
156{
157 if (avail == NULL || hints == NULL) return 0;
158 CFPDF_DownloadHintsWrap hints_wrap(hints);
159 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsPageAvail(page_index, &hints_wrap);
160}
161
162DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints)
163{
164 if (avail == NULL || hints == NULL) return -1;
165 CFPDF_DownloadHintsWrap hints_wrap(hints);
166 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsFormAvail(&hints_wrap);
167}
168
169DLLEXPORT FPDF_BOOL STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail)
170{
171 if (avail == NULL) return -1;
172 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsLinearizedPDF();
173
174}