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