• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 <memory>
10 #include <utility>
11 
12 #include "core/fpdfapi/page/cpdf_docpagedata.h"
13 #include "core/fpdfapi/parser/cpdf_data_avail.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fpdfapi/render/cpdf_docrenderdata.h"
16 #include "core/fxcrt/fx_safe_types.h"
17 #include "core/fxcrt/fx_stream.h"
18 #include "core/fxcrt/retain_ptr.h"
19 #include "core/fxcrt/unowned_ptr.h"
20 #include "fpdfsdk/cpdfsdk_helpers.h"
21 #include "public/fpdf_formfill.h"
22 #include "third_party/base/numerics/safe_conversions.h"
23 
24 #ifdef PDF_ENABLE_XFA
25 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
26 #endif  // PDF_ENABLE_XFA
27 
28 // These checks are here because core/ and public/ cannot depend on each other.
29 static_assert(CPDF_DataAvail::kDataError == PDF_DATA_ERROR,
30               "CPDF_DataAvail::kDataError value mismatch");
31 static_assert(CPDF_DataAvail::kDataNotAvailable == PDF_DATA_NOTAVAIL,
32               "CPDF_DataAvail::kDataNotAvailable value mismatch");
33 static_assert(CPDF_DataAvail::kDataAvailable == PDF_DATA_AVAIL,
34               "CPDF_DataAvail::kDataAvailable value mismatch");
35 
36 static_assert(CPDF_DataAvail::kLinearizationUnknown ==
37                   PDF_LINEARIZATION_UNKNOWN,
38               "CPDF_DataAvail::kLinearizationUnknown value mismatch");
39 static_assert(CPDF_DataAvail::kNotLinearized == PDF_NOT_LINEARIZED,
40               "CPDF_DataAvail::kNotLinearized value mismatch");
41 static_assert(CPDF_DataAvail::kLinearized == PDF_LINEARIZED,
42               "CPDF_DataAvail::kLinearized value mismatch");
43 
44 static_assert(CPDF_DataAvail::kFormError == PDF_FORM_ERROR,
45               "CPDF_DataAvail::kFormError value mismatch");
46 static_assert(CPDF_DataAvail::kFormNotAvailable == PDF_FORM_NOTAVAIL,
47               "CPDF_DataAvail::kFormNotAvailable value mismatch");
48 static_assert(CPDF_DataAvail::kFormAvailable == PDF_FORM_AVAIL,
49               "CPDF_DataAvail::kFormAvailable value mismatch");
50 static_assert(CPDF_DataAvail::kFormNotExist == PDF_FORM_NOTEXIST,
51               "CPDF_DataAvail::kFormNotExist value mismatch");
52 
53 namespace {
54 
55 class FPDF_FileAvailContext final : public CPDF_DataAvail::FileAvail {
56  public:
FPDF_FileAvailContext(FX_FILEAVAIL * avail)57   explicit FPDF_FileAvailContext(FX_FILEAVAIL* avail) : avail_(avail) {}
58   ~FPDF_FileAvailContext() override = default;
59 
60   // CPDF_DataAvail::FileAvail:
IsDataAvail(FX_FILESIZE offset,size_t size)61   bool IsDataAvail(FX_FILESIZE offset, size_t size) override {
62     return !!avail_->IsDataAvail(
63         avail_, pdfium::base::checked_cast<size_t>(offset), size);
64   }
65 
66  private:
67   FX_FILEAVAIL* const avail_;
68 };
69 
70 class FPDF_FileAccessContext final : public IFX_SeekableReadStream {
71  public:
72   CONSTRUCT_VIA_MAKE_RETAIN;
73 
74   // IFX_SeekableReadStream:
GetSize()75   FX_FILESIZE GetSize() override { return file_->m_FileLen; }
76 
ReadBlockAtOffset(pdfium::span<uint8_t> buffer,FX_FILESIZE offset)77   bool ReadBlockAtOffset(pdfium::span<uint8_t> buffer,
78                          FX_FILESIZE offset) override {
79     if (buffer.empty() || offset < 0)
80       return false;
81 
82     if (!pdfium::base::IsValueInRangeForNumericType<FX_FILESIZE>(buffer.size()))
83       return false;
84 
85     FX_SAFE_FILESIZE new_pos = buffer.size();
86     new_pos += offset;
87     return new_pos.IsValid() && new_pos.ValueOrDie() <= GetSize() &&
88            file_->m_GetBlock(
89                file_->m_Param,
90                pdfium::base::checked_cast<unsigned long>(offset), buffer.data(),
91                pdfium::base::checked_cast<unsigned long>(buffer.size()));
92   }
93 
94  private:
FPDF_FileAccessContext(FPDF_FILEACCESS * file)95   explicit FPDF_FileAccessContext(FPDF_FILEACCESS* file) : file_(file) {}
96   ~FPDF_FileAccessContext() override = default;
97 
98   FPDF_FILEACCESS* const file_;
99 };
100 
101 class FPDF_DownloadHintsContext final : public CPDF_DataAvail::DownloadHints {
102  public:
FPDF_DownloadHintsContext(FX_DOWNLOADHINTS * pDownloadHints)103   explicit FPDF_DownloadHintsContext(FX_DOWNLOADHINTS* pDownloadHints)
104       : m_pDownloadHints(pDownloadHints) {}
105   ~FPDF_DownloadHintsContext() override = default;
106 
107   // IFX_DownloadHints
AddSegment(FX_FILESIZE offset,size_t size)108   void AddSegment(FX_FILESIZE offset, size_t size) override {
109     if (m_pDownloadHints) {
110       m_pDownloadHints->AddSegment(m_pDownloadHints,
111                                    static_cast<size_t>(offset), size);
112     }
113   }
114 
115  private:
116   UnownedPtr<FX_DOWNLOADHINTS> m_pDownloadHints;
117 };
118 
119 class FPDF_AvailContext {
120  public:
FPDF_AvailContext(FX_FILEAVAIL * file_avail,FPDF_FILEACCESS * file)121   FPDF_AvailContext(FX_FILEAVAIL* file_avail, FPDF_FILEACCESS* file)
122       : file_avail_(std::make_unique<FPDF_FileAvailContext>(file_avail)),
123         file_read_(pdfium::MakeRetain<FPDF_FileAccessContext>(file)),
124         data_avail_(
125             std::make_unique<CPDF_DataAvail>(file_avail_.get(), file_read_)) {}
126   ~FPDF_AvailContext() = default;
127 
data_avail()128   CPDF_DataAvail* data_avail() { return data_avail_.get(); }
129 
130  private:
131   std::unique_ptr<FPDF_FileAvailContext> const file_avail_;
132   RetainPtr<FPDF_FileAccessContext> const file_read_;
133   std::unique_ptr<CPDF_DataAvail> const data_avail_;
134 };
135 
FPDFAvailContextFromFPDFAvail(FPDF_AVAIL avail)136 FPDF_AvailContext* FPDFAvailContextFromFPDFAvail(FPDF_AVAIL avail) {
137   return reinterpret_cast<FPDF_AvailContext*>(avail);
138 }
139 
FPDFAvailFromFPDFAvailContext(FPDF_AvailContext * pAvailContext)140 FPDF_AVAIL FPDFAvailFromFPDFAvailContext(FPDF_AvailContext* pAvailContext) {
141   return reinterpret_cast<FPDF_AVAIL>(pAvailContext);
142 }
143 
144 }  // namespace
145 
FPDFAvail_Create(FX_FILEAVAIL * file_avail,FPDF_FILEACCESS * file)146 FPDF_EXPORT FPDF_AVAIL FPDF_CALLCONV FPDFAvail_Create(FX_FILEAVAIL* file_avail,
147                                                       FPDF_FILEACCESS* file) {
148   auto pAvail = std::make_unique<FPDF_AvailContext>(file_avail, file);
149 
150   // Caller takes ownership.
151   return FPDFAvailFromFPDFAvailContext(pAvail.release());
152 }
153 
FPDFAvail_Destroy(FPDF_AVAIL avail)154 FPDF_EXPORT void FPDF_CALLCONV FPDFAvail_Destroy(FPDF_AVAIL avail) {
155   // Take ownership back from caller and destroy.
156   std::unique_ptr<FPDF_AvailContext>(FPDFAvailContextFromFPDFAvail(avail));
157 }
158 
FPDFAvail_IsDocAvail(FPDF_AVAIL avail,FX_DOWNLOADHINTS * hints)159 FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsDocAvail(FPDF_AVAIL avail,
160                                                    FX_DOWNLOADHINTS* hints) {
161   auto* avail_context = FPDFAvailContextFromFPDFAvail(avail);
162   if (!avail_context)
163     return PDF_DATA_ERROR;
164   FPDF_DownloadHintsContext hints_context(hints);
165   return avail_context->data_avail()->IsDocAvail(&hints_context);
166 }
167 
168 FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
FPDFAvail_GetDocument(FPDF_AVAIL avail,FPDF_BYTESTRING password)169 FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) {
170   auto* avail_context = FPDFAvailContextFromFPDFAvail(avail);
171   if (!avail_context)
172     return nullptr;
173   CPDF_Parser::Error error;
174   std::unique_ptr<CPDF_Document> document;
175   std::tie(error, document) = avail_context->data_avail()->ParseDocument(
176       std::make_unique<CPDF_DocRenderData>(),
177       std::make_unique<CPDF_DocPageData>(), password);
178   if (error != CPDF_Parser::SUCCESS) {
179     ProcessParseError(error);
180     return nullptr;
181   }
182 
183   ReportUnsupportedFeatures(document.get());
184   return FPDFDocumentFromCPDFDocument(document.release());
185 }
186 
FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc)187 FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) {
188   CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
189   return pDoc ? pDoc->GetParser()->GetFirstPageNo() : 0;
190 }
191 
FPDFAvail_IsPageAvail(FPDF_AVAIL avail,int page_index,FX_DOWNLOADHINTS * hints)192 FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsPageAvail(FPDF_AVAIL avail,
193                                                     int page_index,
194                                                     FX_DOWNLOADHINTS* hints) {
195   auto* avail_context = FPDFAvailContextFromFPDFAvail(avail);
196   if (!avail_context)
197     return PDF_DATA_ERROR;
198   if (page_index < 0)
199     return PDF_DATA_NOTAVAIL;
200   FPDF_DownloadHintsContext hints_context(hints);
201   return avail_context->data_avail()->IsPageAvail(page_index, &hints_context);
202 }
203 
FPDFAvail_IsFormAvail(FPDF_AVAIL avail,FX_DOWNLOADHINTS * hints)204 FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsFormAvail(FPDF_AVAIL avail,
205                                                     FX_DOWNLOADHINTS* hints) {
206   auto* avail_context = FPDFAvailContextFromFPDFAvail(avail);
207   if (!avail_context)
208     return PDF_FORM_ERROR;
209   FPDF_DownloadHintsContext hints_context(hints);
210   return avail_context->data_avail()->IsFormAvail(&hints_context);
211 }
212 
FPDFAvail_IsLinearized(FPDF_AVAIL avail)213 FPDF_EXPORT int FPDF_CALLCONV FPDFAvail_IsLinearized(FPDF_AVAIL avail) {
214   auto* avail_context = FPDFAvailContextFromFPDFAvail(avail);
215   if (!avail_context)
216     return PDF_LINEARIZATION_UNKNOWN;
217   return avail_context->data_avail()->IsLinearizedPDF();
218 }
219