OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "public/fpdf_dataavail.h" | |
8 | |
9 #include "core/include/fpdfapi/cpdf_document.h" | |
10 #include "core/include/fpdfapi/ipdf_data_avail.h" | |
11 #include "fpdfsdk/include/fsdk_define.h" | |
12 #include "public/fpdf_formfill.h" | |
13 | |
14 // These checks are here because core/ and public/ cannot depend on each other. | |
15 static_assert(IPDF_DataAvail::DataError == PDF_DATA_ERROR, | |
16 "IPDF_DataAvail::DataError value mismatch"); | |
17 static_assert(IPDF_DataAvail::DataNotAvailable == PDF_DATA_NOTAVAIL, | |
18 "IPDF_DataAvail::DataNotAvailable value mismatch"); | |
19 static_assert(IPDF_DataAvail::DataAvailable == PDF_DATA_AVAIL, | |
20 "IPDF_DataAvail::DataAvailable value mismatch"); | |
21 | |
22 static_assert(IPDF_DataAvail::LinearizationUnknown == PDF_LINEARIZATION_UNKNOWN, | |
23 "IPDF_DataAvail::LinearizationUnknown value mismatch"); | |
24 static_assert(IPDF_DataAvail::NotLinearized == PDF_NOT_LINEARIZED, | |
25 "IPDF_DataAvail::NotLinearized value mismatch"); | |
26 static_assert(IPDF_DataAvail::Linearized == PDF_LINEARIZED, | |
27 "IPDF_DataAvail::Linearized value mismatch"); | |
28 | |
29 static_assert(IPDF_DataAvail::FormError == PDF_FORM_ERROR, | |
30 "IPDF_DataAvail::FormError value mismatch"); | |
31 static_assert(IPDF_DataAvail::FormNotAvailable == PDF_FORM_NOTAVAIL, | |
32 "IPDF_DataAvail::FormNotAvailable value mismatch"); | |
33 static_assert(IPDF_DataAvail::FormAvailable == PDF_FORM_AVAIL, | |
34 "IPDF_DataAvail::FormAvailable value mismatch"); | |
35 static_assert(IPDF_DataAvail::FormNotExist == PDF_FORM_NOTEXIST, | |
36 "IPDF_DataAvail::FormNotExist value mismatch"); | |
37 | |
38 class CFPDF_FileAvailWrap : public IPDF_DataAvail::FileAvail { | |
39 public: | |
40 CFPDF_FileAvailWrap() { m_pfileAvail = NULL; } | |
41 ~CFPDF_FileAvailWrap() override {} | |
42 | |
43 void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; } | |
44 | |
45 // IPDF_DataAvail::FileAvail: | |
46 FX_BOOL IsDataAvail(FX_FILESIZE offset, FX_DWORD size) override { | |
47 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size); | |
48 } | |
49 | |
50 private: | |
51 FX_FILEAVAIL* m_pfileAvail; | |
52 }; | |
53 | |
54 class CFPDF_FileAccessWrap : public IFX_FileRead { | |
55 public: | |
56 CFPDF_FileAccessWrap() { m_pFileAccess = NULL; } | |
57 ~CFPDF_FileAccessWrap() override {} | |
58 | |
59 void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; } | |
60 | |
61 // IFX_FileRead | |
62 FX_FILESIZE GetSize() override { return m_pFileAccess->m_FileLen; } | |
63 | |
64 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { | |
65 return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, | |
66 (uint8_t*)buffer, size); | |
67 } | |
68 | |
69 void Release() override {} | |
70 | |
71 private: | |
72 FPDF_FILEACCESS* m_pFileAccess; | |
73 }; | |
74 | |
75 class CFPDF_DownloadHintsWrap : public IPDF_DataAvail::DownloadHints { | |
76 public: | |
77 explicit CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) { | |
78 m_pDownloadHints = pDownloadHints; | |
79 } | |
80 ~CFPDF_DownloadHintsWrap() override {} | |
81 | |
82 public: | |
83 // IFX_DownloadHints | |
84 void AddSegment(FX_FILESIZE offset, FX_DWORD size) override { | |
85 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size); | |
86 } | |
87 | |
88 private: | |
89 FX_DOWNLOADHINTS* m_pDownloadHints; | |
90 }; | |
91 | |
92 class CFPDF_DataAvail { | |
93 public: | |
94 CFPDF_DataAvail() { m_pDataAvail = NULL; } | |
95 | |
96 ~CFPDF_DataAvail() { delete m_pDataAvail; } | |
97 | |
98 IPDF_DataAvail* m_pDataAvail; | |
99 CFPDF_FileAvailWrap m_FileAvail; | |
100 CFPDF_FileAccessWrap m_FileRead; | |
101 }; | |
102 | |
103 DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail, | |
104 FPDF_FILEACCESS* file) { | |
105 CFPDF_DataAvail* pAvail = new CFPDF_DataAvail; | |
106 pAvail->m_FileAvail.Set(file_avail); | |
107 pAvail->m_FileRead.Set(file); | |
108 pAvail->m_pDataAvail = | |
109 IPDF_DataAvail::Create(&pAvail->m_FileAvail, &pAvail->m_FileRead); | |
110 return pAvail; | |
111 } | |
112 | |
113 DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail) { | |
114 delete (CFPDF_DataAvail*)avail; | |
115 } | |
116 | |
117 DLLEXPORT int STDCALL | |
118 FPDFAvail_IsDocAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints) { | |
119 if (!avail || !hints) | |
120 return PDF_DATA_ERROR; | |
121 CFPDF_DownloadHintsWrap hints_wrap(hints); | |
122 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsDocAvail(&hints_wrap); | |
123 } | |
124 | |
125 DLLEXPORT FPDF_DOCUMENT STDCALL | |
126 FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) { | |
127 CFPDF_DataAvail* pDataAvail = static_cast<CFPDF_DataAvail*>(avail); | |
128 if (!pDataAvail) | |
129 return nullptr; | |
130 | |
131 CPDF_Parser* pParser = new CPDF_Parser; | |
132 pParser->SetPassword(password); | |
133 CPDF_Parser::Error error = | |
134 pParser->StartAsyncParse(pDataAvail->m_pDataAvail->GetFileRead()); | |
135 if (error != CPDF_Parser::SUCCESS) { | |
136 delete pParser; | |
137 ProcessParseError(error); | |
138 return nullptr; | |
139 } | |
140 pDataAvail->m_pDataAvail->SetDocument(pParser->GetDocument()); | |
141 CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS); | |
142 return FPDFDocumentFromCPDFDocument(pParser->GetDocument()); | |
143 } | |
144 | |
145 DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) { | |
146 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); | |
147 return pDoc ? pDoc->GetParser()->GetFirstPageNo() : 0; | |
148 } | |
149 | |
150 DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail, | |
151 int page_index, | |
152 FX_DOWNLOADHINTS* hints) { | |
153 if (!avail || !hints) | |
154 return PDF_DATA_ERROR; | |
155 CFPDF_DownloadHintsWrap hints_wrap(hints); | |
156 return ((CFPDF_DataAvail*)avail) | |
157 ->m_pDataAvail->IsPageAvail(page_index, &hints_wrap); | |
158 } | |
159 | |
160 DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail, | |
161 FX_DOWNLOADHINTS* hints) { | |
162 if (!avail || !hints) | |
163 return PDF_FORM_ERROR; | |
164 CFPDF_DownloadHintsWrap hints_wrap(hints); | |
165 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsFormAvail(&hints_wrap); | |
166 } | |
167 | |
168 DLLEXPORT int STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail) { | |
169 if (!avail) | |
170 return PDF_LINEARIZATION_UNKNOWN; | |
171 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsLinearizedPDF(); | |
172 } | |
OLD | NEW |