blob: e659db7be11748b58835998798e831fc4aa22592 [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
thestigd4c34f22016-09-28 17:04:51 -07009#include <memory>
10#include <utility>
11
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_data_avail.h"
13#include "core/fpdfapi/parser/cpdf_document.h"
tsepez833619b2016-12-07 09:21:17 -080014#include "core/fxcrt/cfx_retain_ptr.h"
Dan Sinclairbcd1e702017-08-31 13:19:18 -040015#include "core/fxcrt/fx_stream.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/fsdk_define.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_formfill.h"
tsepez36eb4bd2016-10-03 15:24:27 -070018#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080020// These checks are here because core/ and public/ cannot depend on each other.
thestig979070b2016-06-14 10:44:01 -070021static_assert(CPDF_DataAvail::DataError == PDF_DATA_ERROR,
22 "CPDF_DataAvail::DataError value mismatch");
23static_assert(CPDF_DataAvail::DataNotAvailable == PDF_DATA_NOTAVAIL,
24 "CPDF_DataAvail::DataNotAvailable value mismatch");
25static_assert(CPDF_DataAvail::DataAvailable == PDF_DATA_AVAIL,
26 "CPDF_DataAvail::DataAvailable value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080027
thestig979070b2016-06-14 10:44:01 -070028static_assert(CPDF_DataAvail::LinearizationUnknown == PDF_LINEARIZATION_UNKNOWN,
29 "CPDF_DataAvail::LinearizationUnknown value mismatch");
30static_assert(CPDF_DataAvail::NotLinearized == PDF_NOT_LINEARIZED,
31 "CPDF_DataAvail::NotLinearized value mismatch");
32static_assert(CPDF_DataAvail::Linearized == PDF_LINEARIZED,
33 "CPDF_DataAvail::Linearized value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080034
thestig979070b2016-06-14 10:44:01 -070035static_assert(CPDF_DataAvail::FormError == PDF_FORM_ERROR,
36 "CPDF_DataAvail::FormError value mismatch");
37static_assert(CPDF_DataAvail::FormNotAvailable == PDF_FORM_NOTAVAIL,
38 "CPDF_DataAvail::FormNotAvailable value mismatch");
39static_assert(CPDF_DataAvail::FormAvailable == PDF_FORM_AVAIL,
40 "CPDF_DataAvail::FormAvailable value mismatch");
41static_assert(CPDF_DataAvail::FormNotExist == PDF_FORM_NOTEXIST,
42 "CPDF_DataAvail::FormNotExist value mismatch");
Tom Sepezc7e4c4f2015-11-20 09:45:24 -080043
thestig979070b2016-06-14 10:44:01 -070044namespace {
45
46class CFPDF_FileAvailWrap : public CPDF_DataAvail::FileAvail {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 public:
tsepez833619b2016-12-07 09:21:17 -080048 CFPDF_FileAvailWrap() : m_pfileAvail(nullptr) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070049 ~CFPDF_FileAvailWrap() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void Set(FX_FILEAVAIL* pfileAvail) { m_pfileAvail = pfileAvail; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
thestig979070b2016-06-14 10:44:01 -070053 // CPDF_DataAvail::FileAvail:
tsepez4cf55152016-11-02 14:37:54 -070054 bool IsDataAvail(FX_FILESIZE offset, uint32_t size) override {
tsepez95e58342016-10-28 11:34:42 -070055 return !!m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 private:
59 FX_FILEAVAIL* m_pfileAvail;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070060};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
tsepezad2441e2016-10-24 10:19:11 -070062class CFPDF_FileAccessWrap : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 public:
Dan Sinclair0bb13332017-03-30 16:12:02 -040064 template <typename T, typename... Args>
65 friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
66
Lei Zhang2b1a2d52015-08-14 22:16:22 -070067 ~CFPDF_FileAccessWrap() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 void Set(FPDF_FILEACCESS* pFile) { m_pFileAccess = pFile; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
tsepezad2441e2016-10-24 10:19:11 -070071 // IFX_SeekableReadStream
Lei Zhang2b1a2d52015-08-14 22:16:22 -070072 FX_FILESIZE GetSize() override { return m_pFileAccess->m_FileLen; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073
tsepezf39074c2016-10-26 15:33:58 -070074 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
75 return !!m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset,
76 (uint8_t*)buffer, size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 private:
tsepez833619b2016-12-07 09:21:17 -080080 CFPDF_FileAccessWrap() : m_pFileAccess(nullptr) {}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 FPDF_FILEACCESS* m_pFileAccess;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083};
84
thestig979070b2016-06-14 10:44:01 -070085class CFPDF_DownloadHintsWrap : public CPDF_DataAvail::DownloadHints {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070087 explicit CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 m_pDownloadHints = pDownloadHints;
89 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070090 ~CFPDF_DownloadHintsWrap() override {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091
92 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070093 // IFX_DownloadHints
tsepezc3255f52016-03-25 14:52:27 -070094 void AddSegment(FX_FILESIZE offset, uint32_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size);
96 }
97
98 private:
99 FX_DOWNLOADHINTS* m_pDownloadHints;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100};
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102class CFPDF_DataAvail {
103 public:
tsepez833619b2016-12-07 09:21:17 -0800104 CFPDF_DataAvail()
Dan Sinclair0bb13332017-03-30 16:12:02 -0400105 : m_FileAvail(pdfium::MakeUnique<CFPDF_FileAvailWrap>()),
106 m_FileRead(pdfium::MakeRetain<CFPDF_FileAccessWrap>()) {}
thestig979070b2016-06-14 10:44:01 -0700107 ~CFPDF_DataAvail() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
tsepez833619b2016-12-07 09:21:17 -0800109 std::unique_ptr<CFPDF_FileAvailWrap> m_FileAvail;
110 CFX_RetainPtr<CFPDF_FileAccessWrap> m_FileRead;
Artem Strygin834ebec2017-07-27 14:01:32 +0300111 std::unique_ptr<CPDF_DataAvail> m_pDataAvail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112};
113
thestig979070b2016-06-14 10:44:01 -0700114CFPDF_DataAvail* CFPDFDataAvailFromFPDFAvail(FPDF_AVAIL avail) {
115 return static_cast<CFPDF_DataAvail*>(avail);
116}
117
118} // namespace
119
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400120FPDF_EXPORT FPDF_AVAIL FPDF_CALLCONV FPDFAvail_Create(FX_FILEAVAIL* file_avail,
121 FPDF_FILEACCESS* file) {
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700122 auto pAvail = pdfium::MakeUnique<CFPDF_DataAvail>();
tsepez833619b2016-12-07 09:21:17 -0800123 pAvail->m_FileAvail->Set(file_avail);
124 pAvail->m_FileRead->Set(file);
tsepez36eb4bd2016-10-03 15:24:27 -0700125 pAvail->m_pDataAvail = pdfium::MakeUnique<CPDF_DataAvail>(
tsepez833619b2016-12-07 09:21:17 -0800126 pAvail->m_FileAvail.get(), pAvail->m_FileRead, true);
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700127 return pAvail.release(); // Caller takes ownership.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400130FPDF_EXPORT void FPDF_CALLCONV FPDFAvail_Destroy(FPDF_AVAIL avail) {
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700131 // Take ownership back from caller and destroy.
132 std::unique_ptr<CFPDF_DataAvail>(static_cast<CFPDF_DataAvail*>(avail));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400135FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsDocAvail(FPDF_AVAIL avail,
136 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800137 if (!avail || !hints)
138 return PDF_DATA_ERROR;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700140 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsDocAvail(
141 &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400144FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800146 CFPDF_DataAvail* pDataAvail = static_cast<CFPDF_DataAvail*>(avail);
147 if (!pDataAvail)
Lei Zhangaa23e702016-01-29 18:03:40 -0800148 return nullptr;
Tom Sepezf10ae632016-01-26 14:19:52 -0800149
Dan Sinclair0bb13332017-03-30 16:12:02 -0400150 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 pParser->SetPassword(password);
tsepez4540fba2016-08-16 11:12:21 -0700152
Dan Sinclair0bb13332017-03-30 16:12:02 -0400153 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclaircedaa552016-08-24 11:12:19 -0700154 CPDF_Parser::Error error = pDocument->GetParser()->StartLinearizedParse(
155 pDataAvail->m_pDataAvail->GetFileRead(), pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800156 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800157 ProcessParseError(error);
Lei Zhangaa23e702016-01-29 18:03:40 -0800158 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
dsinclaircedaa552016-08-24 11:12:19 -0700160 pDataAvail->m_pDataAvail->SetDocument(pDocument.get());
161 CheckUnSupportError(pDocument.get(), FPDF_ERR_SUCCESS);
162 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400165FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) {
Tom Sepez471a1032015-10-15 16:17:18 -0700166 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
167 return pDoc ? pDoc->GetParser()->GetFirstPageNo() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400170FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsPageAvail(FPDF_AVAIL avail,
171 int page_index,
172 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800173 if (!avail || !hints)
174 return PDF_DATA_ERROR;
thestige0cb6872016-09-01 13:41:10 -0700175 if (page_index < 0)
176 return PDF_DATA_NOTAVAIL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700178 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsPageAvail(
179 page_index, &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400182FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsFormAvail(FPDF_AVAIL avail,
183 FX_DOWNLOADHINTS* hints) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800184 if (!avail || !hints)
185 return PDF_FORM_ERROR;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 CFPDF_DownloadHintsWrap hints_wrap(hints);
thestig979070b2016-06-14 10:44:01 -0700187 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsFormAvail(
188 &hints_wrap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400191FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsLinearized(FPDF_AVAIL avail) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800192 if (!avail)
193 return PDF_LINEARIZATION_UNKNOWN;
thestig979070b2016-06-14 10:44:01 -0700194 return CFPDFDataAvailFromFPDFAvail(avail)->m_pDataAvail->IsLinearizedPDF();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}