blob: 1a83fdf64b0f7d0ef99feb55108f16338c9f6380 [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
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_dataavail.h"
8
thestig979070b2016-06-14 10:44:01 -07009#include "core/fpdfapi/fpdf_parser/include/cpdf_data_avail.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040010#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/fsdk_define.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080012#include "public/fpdf_formfill.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080014// These checks are here because core/ and public/ cannot depend on each other.
thestig979070b2016-06-14 10:44:01 -070015static_assert(CPDF_DataAvail::DataError == PDF_DATA_ERROR,
16 "CPDF_DataAvail::DataError value mismatch");
17static_assert(CPDF_DataAvail::DataNotAvailable == PDF_DATA_NOTAVAIL,
18 "CPDF_DataAvail::DataNotAvailable value mismatch");
19static_assert(CPDF_DataAvail::DataAvailable == PDF_DATA_AVAIL,
20 "CPDF_DataAvail::DataAvailable value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080021
thestig979070b2016-06-14 10:44:01 -070022static_assert(CPDF_DataAvail::LinearizationUnknown == PDF_LINEARIZATION_UNKNOWN,
23 "CPDF_DataAvail::LinearizationUnknown value mismatch");
24static_assert(CPDF_DataAvail::NotLinearized == PDF_NOT_LINEARIZED,
25 "CPDF_DataAvail::NotLinearized value mismatch");
26static_assert(CPDF_DataAvail::Linearized == PDF_LINEARIZED,
27 "CPDF_DataAvail::Linearized value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080028
thestig979070b2016-06-14 10:44:01 -070029static_assert(CPDF_DataAvail::FormError == PDF_FORM_ERROR,
30 "CPDF_DataAvail::FormError value mismatch");
31static_assert(CPDF_DataAvail::FormNotAvailable == PDF_FORM_NOTAVAIL,
32 "CPDF_DataAvail::FormNotAvailable value mismatch");
33static_assert(CPDF_DataAvail::FormAvailable == PDF_FORM_AVAIL,
34 "CPDF_DataAvail::FormAvailable value mismatch");
35static_assert(CPDF_DataAvail::FormNotExist == PDF_FORM_NOTEXIST,
36 "CPDF_DataAvail::FormNotExist value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080037
thestig979070b2016-06-14 10:44:01 -070038namespace {
39
40class CFPDF_FileAvailWrap : public CPDF_DataAvail::FileAvail {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 public:
thestig1cd352e2016-06-07 17:53:06 -070042 CFPDF_FileAvailWrap() { m_pfileAvail = nullptr; }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070043 ~CFPDF_FileAvailWrap() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
thestig979070b2016-06-14 10:44:01 -070047 // CPDF_DataAvail::FileAvail:
tsepezc3255f52016-03-25 14:52:27 -070048 FX_BOOL IsDataAvail(FX_FILESIZE offset, uint32_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size);
50 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 private:
53 FX_FILEAVAIL* m_pfileAvail;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070054};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056class CFPDF_FileAccessWrap : public IFX_FileRead {
57 public:
thestig1cd352e2016-06-07 17:53:06 -070058 CFPDF_FileAccessWrap() { m_pFileAccess = nullptr; }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070059 ~CFPDF_FileAccessWrap() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Lei Zhang2b1a2d52015-08-14 22:16:22 -070063 // IFX_FileRead
64 FX_FILESIZE GetSize() override { return m_pFileAccess->m_FileLen; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Lei Zhang2b1a2d52015-08-14 22:16:22 -070066 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset,
68 (uint8_t*)buffer, size);
69 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Lei Zhang2b1a2d52015-08-14 22:16:22 -070071 void Release() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 private:
74 FPDF_FILEACCESS* m_pFileAccess;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075};
76
thestig979070b2016-06-14 10:44:01 -070077class CFPDF_DownloadHintsWrap : public CPDF_DataAvail::DownloadHints {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070079 explicit CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 m_pDownloadHints = pDownloadHints;
81 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070082 ~CFPDF_DownloadHintsWrap() override {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083
84 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070085 // IFX_DownloadHints
tsepezc3255f52016-03-25 14:52:27 -070086 void AddSegment(FX_FILESIZE offset, uint32_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size);
88 }
89
90 private:
91 FX_DOWNLOADHINTS* m_pDownloadHints;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092};
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094class CFPDF_DataAvail {
95 public:
thestig979070b2016-06-14 10:44:01 -070096 CFPDF_DataAvail() {}
97 ~CFPDF_DataAvail() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098
thestig979070b2016-06-14 10:44:01 -070099 std::unique_ptr<CPDF_DataAvail> m_pDataAvail;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CFPDF_FileAvailWrap m_FileAvail;
101 CFPDF_FileAccessWrap m_FileRead;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102};
103
thestig979070b2016-06-14 10:44:01 -0700104CFPDF_DataAvail* CFPDFDataAvailFromFPDFAvail(FPDF_AVAIL avail) {
105 return static_cast<CFPDF_DataAvail*>(avail);
106}
107
108} // namespace
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail,
111 FPDF_FILEACCESS* file) {
Tom Sepezae51c812015-08-05 12:34:06 -0700112 CFPDF_DataAvail* pAvail = new CFPDF_DataAvail;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 pAvail->m_FileAvail.Set(file_avail);
114 pAvail->m_FileRead.Set(file);
thestig979070b2016-06-14 10:44:01 -0700115 pAvail->m_pDataAvail.reset(
116 new CPDF_DataAvail(&pAvail->m_FileAvail, &pAvail->m_FileRead, TRUE));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 return pAvail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail) {
121 delete (CFPDF_DataAvail*)avail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Dan Sinclairf766ad22016-03-14 13:51:24 -0400124DLLEXPORT int STDCALL FPDFAvail_IsDocAvail(FPDF_AVAIL avail,
125 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800126 if (!avail || !hints)
127 return PDF_DATA_ERROR;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700129 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsDocAvail(
130 &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133DLLEXPORT FPDF_DOCUMENT STDCALL
134FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800135 CFPDF_DataAvail* pDataAvail = static_cast<CFPDF_DataAvail*>(avail);
136 if (!pDataAvail)
Lei Zhangaa23e702016-01-29 18:03:40 -0800137 return nullptr;
Tom Sepezf10ae632016-01-26 14:19:52 -0800138
tsepez4540fba2016-08-16 11:12:21 -0700139 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 pParser->SetPassword(password);
tsepez4540fba2016-08-16 11:12:21 -0700141
dsinclaircedaa552016-08-24 11:12:19 -0700142 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 Sepezf10ae632016-01-26 14:19:52 -0800146 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800147 ProcessParseError(error);
Lei Zhangaa23e702016-01-29 18:03:40 -0800148 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
dsinclaircedaa552016-08-24 11:12:19 -0700150 pDataAvail->m_pDataAvail->SetDocument(pDocument.get());
151 CheckUnSupportError(pDocument.get(), FPDF_ERR_SUCCESS);
152 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153}
154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700156 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
157 return pDoc ? pDoc->GetParser()->GetFirstPageNo() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158}
159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail,
161 int page_index,
162 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800163 if (!avail || !hints)
164 return PDF_DATA_ERROR;
thestige0cb6872016-09-01 13:41:10 -0700165 if (page_index < 0)
166 return PDF_DATA_NOTAVAIL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700168 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsPageAvail(
169 page_index, &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail,
173 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800174 if (!avail || !hints)
175 return PDF_FORM_ERROR;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700177 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsFormAvail(
178 &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Jun Fangdf7f3662015-11-10 18:29:18 +0800181DLLEXPORT int STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail) {
182 if (!avail)
183 return PDF_LINEARIZATION_UNKNOWN;
thestig979070b2016-06-14 10:44:01 -0700184 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsLinearizedPDF();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}