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#include "../include/fsdk_define.h" 9 10extern void ProcessParseError(FX_DWORD err_code); 11class CFPDF_FileAvailWrap : public IFX_FileAvail 12{ 13public: 14 CFPDF_FileAvailWrap() 15 { 16 m_pfileAvail = NULL; 17 } 18 19 void Set(FX_FILEAVAIL* pfileAvail) 20 { 21 m_pfileAvail = pfileAvail; 22 } 23 24 virtual FX_BOOL IsDataAvail( FX_FILESIZE offset, FX_DWORD size) 25 { 26 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size); 27 } 28 29private: 30 FX_FILEAVAIL* m_pfileAvail; 31}; 32 33class CFPDF_FileAccessWrap : public IFX_FileRead 34{ 35public: 36 CFPDF_FileAccessWrap() 37 { 38 m_pFileAccess = NULL; 39 } 40 41 void Set(FPDF_FILEACCESS* pFile) 42 { 43 m_pFileAccess = pFile; 44 } 45 46 virtual FX_FILESIZE GetSize() 47 { 48 return m_pFileAccess->m_FileLen; 49 } 50 51 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) 52 { 53 return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, (FX_LPBYTE)buffer, size); 54 } 55 56 virtual void Release() 57 { 58 } 59 60private: 61 FPDF_FILEACCESS* m_pFileAccess; 62}; 63 64class CFPDF_DownloadHintsWrap : public IFX_DownloadHints 65{ 66public: 67 CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints) 68 { 69 m_pDownloadHints = pDownloadHints; 70 } 71public: 72 virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size) 73 { 74 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size); 75 } 76private: 77 FX_DOWNLOADHINTS* m_pDownloadHints; 78}; 79 80class CFPDF_DataAvail 81{ 82public: 83 CFPDF_DataAvail() 84 { 85 m_pDataAvail = NULL; 86 } 87 88 ~CFPDF_DataAvail() 89 { 90 if (m_pDataAvail) delete m_pDataAvail; 91 } 92 93 IPDF_DataAvail* m_pDataAvail; 94 CFPDF_FileAvailWrap m_FileAvail; 95 CFPDF_FileAccessWrap m_FileRead; 96}; 97 98DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail, FPDF_FILEACCESS* file) 99{ 100 CFPDF_DataAvail* pAvail = new CFPDF_DataAvail; 101 pAvail->m_FileAvail.Set(file_avail); 102 pAvail->m_FileRead.Set(file); 103 pAvail->m_pDataAvail = IPDF_DataAvail::Create(&pAvail->m_FileAvail, &pAvail->m_FileRead); 104 return pAvail; 105} 106 107DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail) 108{ 109 if (avail == NULL) return; 110 delete (CFPDF_DataAvail*)avail; 111} 112 113DLLEXPORT int STDCALL FPDFAvail_IsDocAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints) 114{ 115 if (avail == NULL || hints == NULL) return 0; 116 CFPDF_DownloadHintsWrap hints_wrap(hints); 117 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsDocAvail(&hints_wrap); 118} 119 120extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code); 121 122DLLEXPORT FPDF_DOCUMENT STDCALL FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) 123{ 124 if (avail == NULL) return NULL; 125 CPDF_Parser* pParser = new CPDF_Parser; 126 pParser->SetPassword(password); 127 128 FX_DWORD err_code = pParser->StartAsynParse(((CFPDF_DataAvail*)avail)->m_pDataAvail->GetFileRead()); 129 if (err_code) { 130 delete pParser; 131 ProcessParseError(err_code); 132 return NULL; 133 } 134 ((CFPDF_DataAvail*)avail)->m_pDataAvail->SetDocument(pParser->GetDocument()); 135 CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS); 136 return pParser->GetDocument(); 137} 138 139DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) 140{ 141 if (doc == NULL) return 0; 142 CPDF_Document* pDoc = (CPDF_Document*)doc; 143 return ((CPDF_Parser*)pDoc->GetParser())->GetFirstPageNo(); 144} 145 146DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail, int page_index, FX_DOWNLOADHINTS* hints) 147{ 148 if (avail == NULL || hints == NULL) return 0; 149 CFPDF_DownloadHintsWrap hints_wrap(hints); 150 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsPageAvail(page_index, &hints_wrap); 151} 152 153DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints) 154{ 155 if (avail == NULL || hints == NULL) return -1; 156 CFPDF_DownloadHintsWrap hints_wrap(hints); 157 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsFormAvail(&hints_wrap); 158} 159 160DLLEXPORT FPDF_BOOL STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail) 161{ 162 if (avail == NULL) return -1; 163 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsLinearizedPDF(); 164 165} 166