blob: ba647093eb95b9f7046c69dbe84f44f37c93a29b [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/fpdfview.h"
8
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
10
Lei Zhanga688a042015-11-09 13:57:49 -080011#include "core/include/fxcodec/fx_codec.h"
12#include "core/include/fxcrt/fx_safe_types.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080013#include "fpdfsdk/include/fsdk_define.h"
14#include "fpdfsdk/include/fsdk_mgr.h"
15#include "fpdfsdk/include/fsdk_rendercontext.h"
16#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080019#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070020
Tom Sepez40e9ff32015-11-30 12:39:54 -080021#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -080022#include "core/include/fpdfapi/fpdf_module.h"
Lei Zhang875b9c92016-01-08 13:51:10 -080023#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
24#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
25#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
26#include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080027#include "public/fpdf_formfill.h"
28#endif // PDF_ENABLE_XFA
29
Tom Sepez50d12ad2015-11-24 09:50:51 -080030UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
31 return static_cast<UnderlyingDocumentType*>(doc);
32}
33
34FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
35 return static_cast<FPDF_DOCUMENT>(doc);
36}
37
38UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
39 return static_cast<UnderlyingPageType*>(page);
40}
41
Tom Sepez471a1032015-10-15 16:17:18 -070042CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080043#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080044 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080045#else // PDF_ENABLE_XFA
46 return UnderlyingFromFPDFDocument(doc);
47#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070048}
49
Tom Sepezbf59a072015-10-21 14:07:23 -070050FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080051#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080052 return doc ? FPDFDocumentFromUnderlying(
53 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
54 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080055#else // PDF_ENABLE_XFA
56 return FPDFDocumentFromUnderlying(doc);
57#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070058}
59
Tom Sepezdb0be962015-10-16 14:00:21 -070060CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080061#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080062 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080063#else // PDF_ENABLE_XFA
64 return UnderlyingFromFPDFPage(page);
65#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070066}
67
Tom Sepez40e9ff32015-11-30 12:39:54 -080068#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
70 m_pFS = pFS;
71 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070072}
73
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074IFX_FileStream* CFPDF_FileStream::Retain() {
75 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078void CFPDF_FileStream::Release() {
79 if (m_pFS && m_pFS->Release)
80 m_pFS->Release(m_pFS->clientData);
81 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084FX_FILESIZE CFPDF_FileStream::GetSize() {
85 if (m_pFS && m_pFS->GetSize)
86 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
87 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090FX_BOOL CFPDF_FileStream::IsEOF() {
91 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
95 FX_FILESIZE offset,
96 size_t size) {
97 if (!buffer || !size || !m_pFS->ReadBlock)
98 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
101 (FPDF_DWORD)size) == 0) {
102 m_nCurPos = offset + size;
103 return TRUE;
104 }
105 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
109 if (!buffer || !size || !m_pFS->ReadBlock)
110 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 FX_FILESIZE nSize = GetSize();
113 if (m_nCurPos >= nSize)
114 return 0;
115 FX_FILESIZE dwAvail = nSize - m_nCurPos;
116 if (dwAvail < (FX_FILESIZE)size)
117 size = (size_t)dwAvail;
118 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
119 (FPDF_DWORD)size) == 0) {
120 m_nCurPos += size;
121 return size;
122 }
Bo Xufdc00a72014-10-28 23:03:33 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
128 FX_FILESIZE offset,
129 size_t size) {
130 if (!m_pFS || !m_pFS->WriteBlock)
131 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
134 (FPDF_DWORD)size) == 0) {
135 m_nCurPos = offset + size;
136 return TRUE;
137 }
138 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700139}
140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141FX_BOOL CFPDF_FileStream::Flush() {
142 if (!m_pFS || !m_pFS->Flush)
143 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700146}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800147#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
150 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800151#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 m_BufferOffset = (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800153#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700154}
155
Tom Sepez40e9ff32015-11-30 12:39:54 -0800156#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch) {
158 if (pos >= m_FileAccess.m_FileLen)
159 return FALSE;
160 if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset ||
161 pos >= m_BufferOffset + 512) {
162 // Need to read from file access
163 m_BufferOffset = pos;
164 int size = 512;
165 if (pos + 512 > m_FileAccess.m_FileLen)
166 size = m_FileAccess.m_FileLen - pos;
167 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
168 size))
169 return FALSE;
170 }
171 ch = m_Buffer[pos - m_BufferOffset];
172 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700173}
174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos,
176 uint8_t* pBuf,
177 FX_DWORD size) {
178 if (pos + size > m_FileAccess.m_FileLen)
179 return FALSE;
180 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800182#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
185 FX_FILESIZE offset,
186 size_t size) {
187 if (offset < 0) {
188 return FALSE;
189 }
190 FX_SAFE_FILESIZE newPos =
191 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
192 newPos += offset;
193 if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
194 return FALSE;
195 }
196 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
197 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
204 switch (policy) {
205 case FPDF_POLICY_MACHINETIME_ACCESS: {
206 if (enable)
207 foxit_sandbox_policy |= 0x01;
208 else
209 foxit_sandbox_policy &= 0xFFFFFFFE;
210 } break;
211 default:
212 break;
213 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214}
215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
217 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700218 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700219 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700221 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Tom Sepez2c286192015-06-18 12:47:11 -0700225CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700228 FPDF_InitLibraryWithConfig(nullptr);
229}
230
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800231DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig(
232 const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
Lei Zhang6f62d532015-09-23 15:31:44 -0700235 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 CPDF_ModuleMgr::Create();
Tom Sepez1b246282015-11-25 15:15:31 -0800239 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
240 pModuleMgr->SetCodecModule(g_pCodecModule);
241 pModuleMgr->InitPageModule();
242 pModuleMgr->InitRenderModule();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800243#ifdef PDF_ENABLE_XFA
244 CPDFXFA_App::GetInstance()->Initialize();
245#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800246 pModuleMgr->LoadEmbeddedGB1CMaps();
247 pModuleMgr->LoadEmbeddedJapan1CMaps();
248 pModuleMgr->LoadEmbeddedCNS1CMaps();
249 pModuleMgr->LoadEmbeddedKorea1CMaps();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800250#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700251 if (cfg && cfg->version >= 2)
252 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800256#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800258#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 CPDF_ModuleMgr::Destroy();
260 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 delete g_pCodecModule;
263 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
266#ifndef _WIN32
267int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268void SetLastError(int err) {
269 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270}
271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272int GetLastError() {
273 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800275#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Tom Sepezf10ae632016-01-26 14:19:52 -0800277void ProcessParseError(CPDF_Parser::Error err) {
278 FX_DWORD err_code;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800280 switch (err) {
281 case CPDF_Parser::SUCCESS:
282 err_code = FPDF_ERR_SUCCESS;
283 break;
284 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 err_code = FPDF_ERR_FILE;
286 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800287 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 err_code = FPDF_ERR_FORMAT;
289 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800290 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 err_code = FPDF_ERR_PASSWORD;
292 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800293 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 err_code = FPDF_ERR_SECURITY;
295 break;
296 }
297 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
301 FPDF_BOOL enable) {
302 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
306 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700307 // NOTE: the creation of the file needs to be by the embedder on the
308 // other side of this API.
309 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
310 if (!pFileAccess) {
311 return nullptr;
312 }
313
314 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700316
Tom Sepezf10ae632016-01-26 14:19:52 -0800317 CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
318 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800320 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 return NULL;
322 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800323#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 CPDF_Document* pPDFDoc = pParser->GetDocument();
325 if (!pPDFDoc)
326 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700327
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700329 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800330#else // PDF_ENABLE_XFA
331 return pParser->GetDocument();
332#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333}
Jun Fange118ce92015-02-17 06:50:08 -0800334
Tom Sepez40e9ff32015-11-30 12:39:54 -0800335#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
337 int* docType) {
338 if (!document)
339 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 CPDF_Document* pdfDoc =
342 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
343 if (!pdfDoc)
344 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800345
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
347 if (!pRoot)
348 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800349
Wei Li9b761132016-01-29 15:44:20 -0800350 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 if (!pAcroForm)
352 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
355 if (!pXFA)
356 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800357
Wei Li9b761132016-01-29 15:44:20 -0800358 FX_BOOL bDynamicXFA = pRoot->GetBooleanBy("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 if (bDynamicXFA)
Tom Sepezd3116dc2015-11-24 15:58:06 -0800361 *docType = DOCTYPE_DYNAMIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 else
363 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800366}
367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
369 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700370}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800371#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373class CMemFile final : public IFX_FileRead {
374 public:
375 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376
Lei Zhang3884dba2015-10-19 17:27:53 -0700377 void Release() override { delete this; }
378 FX_FILESIZE GetSize() override { return m_size; }
379 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 if (offset < 0) {
381 return FALSE;
382 }
383 FX_SAFE_FILESIZE newPos =
384 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
385 newPos += offset;
386 if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
387 return FALSE;
388 }
389 FXSYS_memcpy(buffer, m_pBuf + offset, size);
390 return TRUE;
391 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700394 ~CMemFile() override {}
395
Lei Zhang3884dba2015-10-19 17:27:53 -0700396 uint8_t* const m_pBuf;
397 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398};
Lei Zhang3884dba2015-10-19 17:27:53 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
401 int size,
402 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700403 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 pParser->SetPassword(password);
405 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
Tom Sepezf10ae632016-01-26 14:19:52 -0800406 CPDF_Parser::Error error = pParser->StartParse(pMemFile);
407 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800409 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 return NULL;
411 }
412 CPDF_Document* pDoc = NULL;
413 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800414 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700415 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700416}
417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418DLLEXPORT FPDF_DOCUMENT STDCALL
419FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
420 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700421 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700423 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Tom Sepezf10ae632016-01-26 14:19:52 -0800424 CPDF_Parser::Error error = pParser->StartParse(pFile);
425 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800427 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 return NULL;
429 }
430 CPDF_Document* pDoc = NULL;
431 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800432 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700433 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434}
435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
437 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700438 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700440
Tom Sepez471a1032015-10-15 16:17:18 -0700441 *fileVersion = 0;
442 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
443 if (!pDoc)
444 return FALSE;
445
446 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 if (!pParser)
448 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700449
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 *fileVersion = pParser->GetFileVersion();
451 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700452}
453
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
455// header).
456DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700457 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
458 if (!pDoc)
Tom Sepez51da0932015-11-25 16:05:49 -0800459#ifndef PDF_ENABLE_XFA
460 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800461#else // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 return (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800463#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464
Tom Sepez471a1032015-10-15 16:17:18 -0700465 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800466 return pDict ? pDict->GetIntegerBy("P") : (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467}
468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700470 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
471 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700473
Tom Sepez471a1032015-10-15 16:17:18 -0700474 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800475 return pDict ? pDict->GetIntegerBy("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700476}
477
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800479 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700480 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700481}
482
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
484 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800485 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
486 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700487 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800488
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700489 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700490 return nullptr;
491
Tom Sepez40e9ff32015-11-30 12:39:54 -0800492#ifdef PDF_ENABLE_XFA
493 return pDoc->GetPage(page_index);
494#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800495 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800496 if (!pDict)
Tom Sepez51da0932015-11-25 16:05:49 -0800497 return NULL;
498 CPDF_Page* pPage = new CPDF_Page;
499 pPage->Load(pDoc, pDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -0800500 pPage->ParseContent(nullptr);
Tom Sepez51da0932015-11-25 16:05:49 -0800501 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800502#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503}
504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800506 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700507 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508}
509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800511 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700512 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700513}
514
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515void DropContext(void* data) {
516 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700517}
518
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519#if defined(_DEBUG) || defined(DEBUG)
520#define DEBUG_TRACE
521#endif
522
523#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
525 FPDF_PAGE page,
526 int start_x,
527 int start_y,
528 int size_x,
529 int size_y,
530 int rotate,
531 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700532 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 if (!pPage)
534 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700535
Tom Sepezae51c812015-08-05 12:34:06 -0700536 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538
539#ifndef _WIN32_WCE
Jun Fang1aeeceb2015-12-29 10:27:44 +0800540 CFX_DIBitmap* pBitmap = nullptr;
541 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
542 FX_BOOL bHasImageMask = pPage->HasImageMask();
543 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700544 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545 pBitmap->Create(size_x, size_y, FXDIB_Argb);
546 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700547#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700548 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550#else
Tom Sepezae51c812015-08-05 12:34:06 -0700551 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700553#endif
Jun Fang1aeeceb2015-12-29 10:27:44 +0800554 } else {
Tom Sepezae51c812015-08-05 12:34:06 -0700555 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800556 }
Bo Xud4e406e2014-08-13 11:03:19 -0700557
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
559 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700560
Jun Fang1aeeceb2015-12-29 10:27:44 +0800561 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 if (pBitmap) {
563 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700566 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567 int pitch = pBitmap->GetPitch();
568 pDst->Create(size_x, size_y, FXDIB_Rgb32);
569 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
570 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
571 FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
572 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
573 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800574 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800576 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700577 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700579#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 // get clip region
581 RECT rect, cliprect;
582 rect.left = start_x;
583 rect.top = start_y;
584 rect.right = start_x + size_x;
585 rect.bottom = start_y + size_y;
586 GetClipBox(dc, &cliprect);
587 IntersectRect(&rect, &rect, &cliprect);
588 int width = rect.right - rect.left;
589 int height = rect.bottom - rect.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700590
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591#ifdef DEBUG_TRACE
592 {
593 char str[128];
594 memset(str, 0, sizeof(str));
595 FXSYS_snprintf(str, sizeof(str) - 1, "Rendering DIB %d x %d", width,
596 height);
597 CPDF_ModuleMgr::Get()->ReportError(999, str);
598 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700599#endif
Bo Xud4e406e2014-08-13 11:03:19 -0700600
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 // Create a DIB section
602 LPVOID pBuffer;
603 BITMAPINFOHEADER bmih;
604 FXSYS_memset(&bmih, 0, sizeof bmih);
605 bmih.biSize = sizeof bmih;
606 bmih.biBitCount = 24;
607 bmih.biHeight = -height;
608 bmih.biPlanes = 1;
609 bmih.biWidth = width;
610 pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
611 &pBuffer, NULL, 0);
Lei Zhang412e9082015-12-14 18:34:00 -0800612 if (!pContext->m_hBitmap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613#if defined(DEBUG) || defined(_DEBUG)
614 char str[128];
615 memset(str, 0, sizeof(str));
616 FXSYS_snprintf(str, sizeof(str) - 1,
617 "Error CreateDIBSection: %d x %d, error code = %d", width,
618 height, GetLastError());
619 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700620#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700622#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 }
624 FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
625
626#ifdef DEBUG_TRACE
627 { CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created"); }
628#endif
629
630 // Create a device with this external buffer
631 pContext->m_pBitmap = new CFX_DIBitmap;
632 pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
633 pContext->m_pDevice = new CPDF_FxgeDevice;
634 ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
635
636#ifdef DEBUG_TRACE
637 CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");
638#endif
639
640 // output to bitmap device
641 FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
642 start_y - rect.top, size_x, size_y, rotate, flags);
643
644#ifdef DEBUG_TRACE
645 CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
646#endif
647
648 // Now output to real device
649 HDC hMemDC = CreateCompatibleDC(dc);
Lei Zhang412e9082015-12-14 18:34:00 -0800650 if (!hMemDC) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651#if defined(DEBUG) || defined(_DEBUG)
652 char str[128];
653 memset(str, 0, sizeof(str));
654 FXSYS_snprintf(str, sizeof(str) - 1,
655 "Error CreateCompatibleDC. Error code = %d", GetLastError());
656 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
657#else
658 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
659#endif
660 }
661
662 HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
663
664#ifdef DEBUG_TRACE
665 CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");
666#endif
667
668 BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
669 SelectObject(hMemDC, hOldBitmap);
670 DeleteDC(hMemDC);
671
672#ifdef DEBUG_TRACE
673 CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");
674#endif
675
676#endif
Jun Fang1aeeceb2015-12-29 10:27:44 +0800677 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800679
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 delete pContext;
681 pPage->RemovePrivateData((void*)1);
682}
683#endif
684
685DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
686 FPDF_PAGE page,
687 int start_x,
688 int start_y,
689 int size_x,
690 int size_y,
691 int rotate,
692 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700693 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700695 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 if (!pPage)
697 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700698 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699 pPage->SetPrivateData((void*)1, pContext, DropContext);
700#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700701 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702
703 if (flags & FPDF_REVERSE_BYTE_ORDER)
704 ((CFX_SkiaDevice*)pContext->m_pDevice)
705 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
706 else
707 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
708#else
Tom Sepezae51c812015-08-05 12:34:06 -0700709 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710
711 if (flags & FPDF_REVERSE_BYTE_ORDER)
712 ((CFX_FxgeDevice*)pContext->m_pDevice)
713 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
714 else
715 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
716#endif
717
718 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
719 rotate, flags, TRUE, NULL);
720
721 delete pContext;
722 pPage->RemovePrivateData((void*)1);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700723}
724
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
726 if (!page)
727 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800728#ifdef PDF_ENABLE_XFA
729 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
730 pPage->Release();
731#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800732 CPDFSDK_PageView* pPageView =
733 (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
734 if (pPageView && pPageView->IsLocked()) {
735 pPageView->TakeOverPage();
736 return;
737 }
738 delete (CPDF_Page*)page;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800739#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700740}
741
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800743#ifdef PDF_ENABLE_XFA
Jun Fangfc751362015-12-16 21:23:39 -0800744 delete UnderlyingFromFPDFDocument(document);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800745#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800746 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
747 if (!pDoc)
748 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800749 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800750 if (!pParser) {
751 delete pDoc;
752 return;
753 }
754 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800755#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700756}
757
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
759 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700760}
761
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
763 int start_x,
764 int start_y,
765 int size_x,
766 int size_y,
767 int rotate,
768 int device_x,
769 int device_y,
770 double* page_x,
771 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800772 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800774 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800775#ifdef PDF_ENABLE_XFA
776 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
777 device_y, page_x, page_y);
778#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800779 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800780 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
781 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800782 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800783 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800784 FX_FLOAT page_x_f, page_y_f;
785 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
786 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800787 *page_x = (page_x_f);
788 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800789#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700790}
791
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
793 int start_x,
794 int start_y,
795 int size_x,
796 int size_y,
797 int rotate,
798 double page_x,
799 double page_y,
800 int* device_x,
801 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700802 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800804 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700805 if (!pPage)
806 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800807#ifdef PDF_ENABLE_XFA
808 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
809 device_x, device_y);
810#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800811 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800812 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
813 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800814 FX_FLOAT device_x_f, device_y_f;
815 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
816 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800817 *device_x = FXSYS_round(device_x_f);
818 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800819#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700820}
821
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
823 int height,
824 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800825 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
827 return NULL;
828 }
829 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700830}
831
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
833 int height,
834 int format,
835 void* first_scan,
836 int stride) {
837 FXDIB_Format fx_format;
838 switch (format) {
839 case FPDFBitmap_Gray:
840 fx_format = FXDIB_8bppRgb;
841 break;
842 case FPDFBitmap_BGR:
843 fx_format = FXDIB_Rgb;
844 break;
845 case FPDFBitmap_BGRx:
846 fx_format = FXDIB_Rgb32;
847 break;
848 case FPDFBitmap_BGRA:
849 fx_format = FXDIB_Argb;
850 break;
851 default:
852 return NULL;
853 }
854 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
855 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
856 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700857}
858
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
860 int left,
861 int top,
862 int width,
863 int height,
864 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800865 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 return;
867#ifdef _SKIA_SUPPORT_
868 CFX_SkiaDevice device;
869#else
870 CFX_FxgeDevice device;
871#endif
872 device.Attach((CFX_DIBitmap*)bitmap);
873 if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
874 color |= 0xFF000000;
875 FX_RECT rect(left, top, left + width, top + height);
876 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700877}
878
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800880 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700881}
882
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800884 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700885}
886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800888 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889}
890
891DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800892 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893}
894
895DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
896 delete (CFX_DIBitmap*)bitmap;
897}
898
899void FPDF_RenderPage_Retail(CRenderContext* pContext,
900 FPDF_PAGE page,
901 int start_x,
902 int start_y,
903 int size_x,
904 int size_y,
905 int rotate,
906 int flags,
907 FX_BOOL bNeedToRestore,
908 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700909 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
910 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911 return;
912
913 if (!pContext->m_pOptions)
914 pContext->m_pOptions = new CPDF_RenderOptions;
915
916 if (flags & FPDF_LCD_TEXT)
917 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
918 else
919 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
920 if (flags & FPDF_NO_NATIVETEXT)
921 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
922 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
923 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
924 if (flags & FPDF_RENDER_FORCEHALFTONE)
925 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800926#ifndef PDF_ENABLE_XFA
927 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
928 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
929 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
930 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
931 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
932 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800933#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 // Grayscale output
935 if (flags & FPDF_GRAYSCALE) {
936 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
937 pContext->m_pOptions->m_ForeColor = 0;
938 pContext->m_pOptions->m_BackColor = 0xffffff;
939 }
940 const CPDF_OCContext::UsageType usage =
941 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
942 pContext->m_pOptions->m_AddFlags = flags >> 8;
943 pContext->m_pOptions->m_pOCContext =
944 new CPDF_OCContext(pPage->m_pDocument, usage);
945
Tom Sepez60d909e2015-12-10 15:34:55 -0800946 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
948
949 FX_RECT clip;
950 clip.left = start_x;
951 clip.right = start_x + size_x;
952 clip.top = start_y;
953 clip.bottom = start_y + size_y;
954 pContext->m_pDevice->SaveState();
955 pContext->m_pDevice->SetClip_Rect(&clip);
956
Tom Sepez979ddd82015-12-17 13:41:13 -0800957 pContext->m_pContext = new CPDF_RenderContext(pPage);
Tom Sepez71fdc342016-01-22 12:06:32 -0800958 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959
960 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700961 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
963 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
964 &matrix, TRUE, NULL);
965 }
966
Tom Sepezb3b67622015-10-19 16:20:03 -0700967 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
968 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
969 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800970 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972}
973
974DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
975 int page_index,
976 double* width,
977 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800978 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
979 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 return FALSE;
981
Tom Sepez40e9ff32015-11-30 12:39:54 -0800982#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 int count = pDoc->GetPageCount();
984 if (page_index < 0 || page_index >= count)
985 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
987 if (!pPage)
988 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800989 *width = pPage->GetPageWidth();
990 *height = pPage->GetPageHeight();
991#else // PDF_ENABLE_XFA
992 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
993 if (!pDict)
994 return FALSE;
Tom Sepez51da0932015-11-25 16:05:49 -0800995 CPDF_Page page;
996 page.Load(pDoc, pDict);
997 *width = page.GetPageWidth();
998 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800999#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000
1001 return TRUE;
1002}
1003
1004DLLEXPORT FPDF_BOOL STDCALL
1005FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001006 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 if (!pDoc)
1008 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -07001009 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 return viewRef.PrintScaling();
1011}
1012
1013DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001014 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 if (!pDoc)
1016 return 1;
1017 CPDF_ViewerPreferences viewRef(pDoc);
1018 return viewRef.NumCopies();
1019}
1020
1021DLLEXPORT FPDF_PAGERANGE STDCALL
1022FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001023 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (!pDoc)
1025 return NULL;
1026 CPDF_ViewerPreferences viewRef(pDoc);
1027 return viewRef.PrintPageRange();
1028}
1029
1030DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1031FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001032 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001034 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 CPDF_ViewerPreferences viewRef(pDoc);
1036 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001037 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001039 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001041 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 return DuplexFlipLongEdge;
1043 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001044}
1045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001047 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1048 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001050
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1052 if (!pRoot)
1053 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001054
Lei Zhangd983b092015-12-14 16:58:33 -08001055 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001056 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
Wei Li9b761132016-01-29 15:44:20 -08001057 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 if (pDest)
1059 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001060
1061 if (!count.IsValid())
1062 return 0;
1063
1064 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065}
1066
1067DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1068 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001070 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071
Tom Sepez471a1032015-10-15 16:17:18 -07001072 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1073 if (!pDoc)
1074 return nullptr;
1075
Lei Zhangd983b092015-12-14 16:58:33 -08001076 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001077 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078}
1079
Tom Sepez51da0932015-11-25 16:05:49 -08001080#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081FPDF_RESULT FPDF_BStr_Init(FPDF_BSTR* str) {
1082 if (!str)
1083 return -1;
1084
1085 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1086 return 0;
1087}
1088
1089FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length) {
1090 if (!str)
1091 return -1;
1092 if (!bstr || !length)
1093 return -1;
1094 if (length == -1)
1095 length = FXSYS_strlen(bstr);
1096
1097 if (length == 0) {
1098 if (str->str) {
1099 FX_Free(str->str);
1100 str->str = NULL;
1101 }
1102 str->len = 0;
1103 return 0;
1104 }
1105
1106 if (str->str && str->len < length)
1107 str->str = FX_Realloc(char, str->str, length + 1);
1108 else if (!str->str)
1109 str->str = FX_Alloc(char, length + 1);
1110
1111 str->str[length] = 0;
1112 if (str->str == NULL)
1113 return -1;
1114
1115 FXSYS_memcpy(str->str, bstr, length);
1116 str->len = length;
1117
1118 return 0;
1119}
1120
1121FPDF_RESULT FPDF_BStr_Clear(FPDF_BSTR* str) {
1122 if (!str)
1123 return -1;
1124
1125 if (str->str) {
1126 FX_Free(str->str);
1127 str->str = NULL;
1128 }
1129 str->len = 0;
1130 return 0;
1131}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001132#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133
1134DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1135 int index,
1136 void* buffer,
1137 long* buflen) {
1138 if (!buffer)
1139 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001140
1141 if (index < 0)
1142 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143
Tom Sepezbf59a072015-10-21 14:07:23 -07001144 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001145 if (!pDoc)
1146 return nullptr;
1147
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001148 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1149 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001150 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001152 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001154 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 int count = nameTree.GetCount();
1156 if (index >= count) {
Wei Li9b761132016-01-29 15:44:20 -08001157 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001159 return nullptr;
1160
1161 pdfium::base::CheckedNumeric<int> checked_count = count;
1162 checked_count += pDest->GetCount();
1163 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1164 return nullptr;
1165
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001168 for (const auto& it : *pDest) {
1169 bsName = it.first;
1170 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 if (!pDestObj)
1172 continue;
1173 if (i == index)
1174 break;
1175 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001176 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 } else {
1178 pDestObj = nameTree.LookupValue(index, bsName);
1179 }
1180 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001181 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001182 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -08001183 pDestObj = pDict->GetArrayBy("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001185 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001187 if (!pDestObj->IsArray())
1188 return nullptr;
1189
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 CFX_WideString wsName = PDF_DecodeText(bsName);
1191 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
1192 unsigned int len = utf16Name.GetLength();
1193 if (!buffer) {
1194 *buflen = len;
1195 } else if (*buflen >= len) {
1196 memcpy(buffer, utf16Name.c_str(), len);
1197 *buflen = len;
1198 } else {
1199 *buflen = -1;
1200 }
1201 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001202}