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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../include/fsdk_define.h"
8 #include "../include/fpdf_dataavail.h"
10 extern void ProcessParseError(FX_DWORD err_code);
11 class CFPDF_FileAvailWrap : public IFX_FileAvail
19 void Set(FX_FILEAVAIL* pfileAvail)
21 m_pfileAvail = pfileAvail;
24 virtual FX_BOOL IsDataAvail( FX_FILESIZE offset, FX_DWORD size)
26 return m_pfileAvail->IsDataAvail(m_pfileAvail, offset, size);
30 FX_FILEAVAIL* m_pfileAvail;
33 class CFPDF_FileAccessWrap : public IFX_FileRead
36 CFPDF_FileAccessWrap()
41 void Set(FPDF_FILEACCESS* pFile)
43 m_pFileAccess = pFile;
46 virtual FX_FILESIZE GetSize()
48 return m_pFileAccess->m_FileLen;
51 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
53 return m_pFileAccess->m_GetBlock(m_pFileAccess->m_Param, offset, (FX_LPBYTE)buffer, size);
56 virtual void Release()
61 FPDF_FILEACCESS* m_pFileAccess;
64 class CFPDF_DownloadHintsWrap : public IFX_DownloadHints
67 CFPDF_DownloadHintsWrap(FX_DOWNLOADHINTS* pDownloadHints)
69 m_pDownloadHints = pDownloadHints;
72 virtual void AddSegment(FX_FILESIZE offset, FX_DWORD size)
74 m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size);
77 FX_DOWNLOADHINTS* m_pDownloadHints;
80 class CFPDF_DataAvail : public CFX_Object
90 if (m_pDataAvail) delete m_pDataAvail;
93 IPDF_DataAvail* m_pDataAvail;
94 CFPDF_FileAvailWrap m_FileAvail;
95 CFPDF_FileAccessWrap m_FileRead;
98 DLLEXPORT FPDF_AVAIL STDCALL FPDFAvail_Create(FX_FILEAVAIL* file_avail, FPDF_FILEACCESS* file)
100 CFPDF_DataAvail* pAvail = FX_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);
107 DLLEXPORT void STDCALL FPDFAvail_Destroy(FPDF_AVAIL avail)
109 if (avail == NULL) return;
110 delete (CFPDF_DataAvail*)avail;
113 DLLEXPORT int STDCALL FPDFAvail_IsDocAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints)
115 if (avail == NULL || hints == NULL) return 0;
116 CFPDF_DownloadHintsWrap hints_wrap(hints);
117 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsDocAvail(&hints_wrap);
120 extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
122 DLLEXPORT FPDF_DOCUMENT STDCALL FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password)
124 if (avail == NULL) return NULL;
125 CPDF_Parser* pParser = FX_NEW CPDF_Parser;
126 pParser->SetPassword(password);
128 FX_DWORD err_code = pParser->StartAsynParse(((CFPDF_DataAvail*)avail)->m_pDataAvail->GetFileRead());
131 ProcessParseError(err_code);
134 ((CFPDF_DataAvail*)avail)->m_pDataAvail->SetDocument(pParser->GetDocument());
135 CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS);
136 return pParser->GetDocument();
139 DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc)
141 if (doc == NULL) return 0;
142 CPDF_Document* pDoc = (CPDF_Document*)doc;
143 return ((CPDF_Parser*)pDoc->GetParser())->GetFirstPageNo();
146 DLLEXPORT int STDCALL FPDFAvail_IsPageAvail(FPDF_AVAIL avail, int page_index, FX_DOWNLOADHINTS* hints)
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);
153 DLLEXPORT int STDCALL FPDFAvail_IsFormAvail(FPDF_AVAIL avail, FX_DOWNLOADHINTS* hints)
155 if (avail == NULL || hints == NULL) return -1;
156 CFPDF_DownloadHintsWrap hints_wrap(hints);
157 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsFormAvail(&hints_wrap);
160 DLLEXPORT FPDF_BOOL STDCALL FPDFAvail_IsLinearized(FPDF_AVAIL avail)
162 if (avail == NULL) return -1;
163 return ((CFPDF_DataAvail*)avail)->m_pDataAvail->IsLinearizedPDF();