blob: 4630ac1e7e56f4325a14ffd3851bea871cbed733 [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
Tom Sepez5fc239a2016-03-10 14:10:38 -080011#include "core/include/fpdfapi/cpdf_array.h"
Tom Sepez310438f2016-03-08 13:10:55 -080012#include "core/include/fpdfapi/cpdf_document.h"
Lei Zhanga688a042015-11-09 13:57:49 -080013#include "core/include/fxcodec/fx_codec.h"
14#include "core/include/fxcrt/fx_safe_types.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080015#include "fpdfsdk/include/fsdk_define.h"
16#include "fpdfsdk/include/fsdk_mgr.h"
17#include "fpdfsdk/include/fsdk_rendercontext.h"
18#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080021#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070022
Tom Sepez40e9ff32015-11-30 12:39:54 -080023#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -080024#include "core/include/fpdfapi/fpdf_module.h"
Lei Zhang875b9c92016-01-08 13:51:10 -080025#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
26#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
27#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
28#include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080029#include "public/fpdf_formfill.h"
30#endif // PDF_ENABLE_XFA
31
Tom Sepez50d12ad2015-11-24 09:50:51 -080032UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
33 return static_cast<UnderlyingDocumentType*>(doc);
34}
35
36FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
37 return static_cast<FPDF_DOCUMENT>(doc);
38}
39
40UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
41 return static_cast<UnderlyingPageType*>(page);
42}
43
Tom Sepez471a1032015-10-15 16:17:18 -070044CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080045#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080046 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080047#else // PDF_ENABLE_XFA
48 return UnderlyingFromFPDFDocument(doc);
49#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070050}
51
Tom Sepezbf59a072015-10-21 14:07:23 -070052FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080053#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080054 return doc ? FPDFDocumentFromUnderlying(
55 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
56 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080057#else // PDF_ENABLE_XFA
58 return FPDFDocumentFromUnderlying(doc);
59#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070060}
61
Tom Sepezdb0be962015-10-16 14:00:21 -070062CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080063#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080064 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080065#else // PDF_ENABLE_XFA
66 return UnderlyingFromFPDFPage(page);
67#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070068}
69
Tom Sepez40e9ff32015-11-30 12:39:54 -080070#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
72 m_pFS = pFS;
73 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076IFX_FileStream* CFPDF_FileStream::Retain() {
77 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080void CFPDF_FileStream::Release() {
81 if (m_pFS && m_pFS->Release)
82 m_pFS->Release(m_pFS->clientData);
83 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086FX_FILESIZE CFPDF_FileStream::GetSize() {
87 if (m_pFS && m_pFS->GetSize)
88 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
89 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092FX_BOOL CFPDF_FileStream::IsEOF() {
93 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
97 FX_FILESIZE offset,
98 size_t size) {
99 if (!buffer || !size || !m_pFS->ReadBlock)
100 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
103 (FPDF_DWORD)size) == 0) {
104 m_nCurPos = offset + size;
105 return TRUE;
106 }
107 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
111 if (!buffer || !size || !m_pFS->ReadBlock)
112 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 FX_FILESIZE nSize = GetSize();
115 if (m_nCurPos >= nSize)
116 return 0;
117 FX_FILESIZE dwAvail = nSize - m_nCurPos;
118 if (dwAvail < (FX_FILESIZE)size)
119 size = (size_t)dwAvail;
120 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
121 (FPDF_DWORD)size) == 0) {
122 m_nCurPos += size;
123 return size;
124 }
Bo Xufdc00a72014-10-28 23:03:33 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700127}
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
130 FX_FILESIZE offset,
131 size_t size) {
132 if (!m_pFS || !m_pFS->WriteBlock)
133 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
136 (FPDF_DWORD)size) == 0) {
137 m_nCurPos = offset + size;
138 return TRUE;
139 }
140 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700141}
142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143FX_BOOL CFPDF_FileStream::Flush() {
144 if (!m_pFS || !m_pFS->Flush)
145 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700148}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800149#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
152 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800153#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 m_BufferOffset = (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800155#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700156}
157
Tom Sepez40e9ff32015-11-30 12:39:54 -0800158#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch) {
160 if (pos >= m_FileAccess.m_FileLen)
161 return FALSE;
162 if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset ||
163 pos >= m_BufferOffset + 512) {
164 // Need to read from file access
165 m_BufferOffset = pos;
166 int size = 512;
167 if (pos + 512 > m_FileAccess.m_FileLen)
168 size = m_FileAccess.m_FileLen - pos;
169 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
170 size))
171 return FALSE;
172 }
173 ch = m_Buffer[pos - m_BufferOffset];
174 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos,
178 uint8_t* pBuf,
179 FX_DWORD size) {
180 if (pos + size > m_FileAccess.m_FileLen)
181 return FALSE;
182 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800184#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
187 FX_FILESIZE offset,
188 size_t size) {
189 if (offset < 0) {
190 return FALSE;
191 }
192 FX_SAFE_FILESIZE newPos =
193 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
194 newPos += offset;
195 if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
196 return FALSE;
197 }
198 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
199 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
206 switch (policy) {
207 case FPDF_POLICY_MACHINETIME_ACCESS: {
208 if (enable)
209 foxit_sandbox_policy |= 0x01;
210 else
211 foxit_sandbox_policy &= 0xFFFFFFFE;
212 } break;
213 default:
214 break;
215 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
219 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700220 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700221 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700223 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Tom Sepez2c286192015-06-18 12:47:11 -0700227CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700230 FPDF_InitLibraryWithConfig(nullptr);
231}
232
Dan Sinclairf766ad22016-03-14 13:51:24 -0400233DLLEXPORT void STDCALL
234FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Lei Zhang6f62d532015-09-23 15:31:44 -0700237 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 CPDF_ModuleMgr::Create();
Tom Sepez1b246282015-11-25 15:15:31 -0800241 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
242 pModuleMgr->SetCodecModule(g_pCodecModule);
243 pModuleMgr->InitPageModule();
244 pModuleMgr->InitRenderModule();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800245#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +0800246 CPDFXFA_App::GetInstance()->Initialize(
247 (cfg && cfg->version >= 2)
248 ? reinterpret_cast<FXJSE_HRUNTIME>(cfg->m_pIsolate)
249 : nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800250#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800251 pModuleMgr->LoadEmbeddedGB1CMaps();
252 pModuleMgr->LoadEmbeddedJapan1CMaps();
253 pModuleMgr->LoadEmbeddedCNS1CMaps();
254 pModuleMgr->LoadEmbeddedKorea1CMaps();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800255#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700256 if (cfg && cfg->version >= 2)
257 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800261#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800263#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CPDF_ModuleMgr::Destroy();
265 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 delete g_pCodecModule;
268 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269}
270
271#ifndef _WIN32
272int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273void SetLastError(int err) {
274 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277int GetLastError() {
278 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800280#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Tom Sepezf10ae632016-01-26 14:19:52 -0800282void ProcessParseError(CPDF_Parser::Error err) {
283 FX_DWORD err_code;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800285 switch (err) {
286 case CPDF_Parser::SUCCESS:
287 err_code = FPDF_ERR_SUCCESS;
288 break;
289 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 err_code = FPDF_ERR_FILE;
291 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800292 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 err_code = FPDF_ERR_FORMAT;
294 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800295 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 err_code = FPDF_ERR_PASSWORD;
297 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800298 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 err_code = FPDF_ERR_SECURITY;
300 break;
301 }
302 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
306 FPDF_BOOL enable) {
307 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
311 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700312 // NOTE: the creation of the file needs to be by the embedder on the
313 // other side of this API.
314 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
315 if (!pFileAccess) {
316 return nullptr;
317 }
318
319 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700321
Tom Sepezf10ae632016-01-26 14:19:52 -0800322 CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
323 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800325 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 return NULL;
327 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800328#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 CPDF_Document* pPDFDoc = pParser->GetDocument();
330 if (!pPDFDoc)
331 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700334 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800335#else // PDF_ENABLE_XFA
336 return pParser->GetDocument();
337#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338}
Jun Fange118ce92015-02-17 06:50:08 -0800339
Tom Sepez40e9ff32015-11-30 12:39:54 -0800340#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
342 int* docType) {
343 if (!document)
344 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800345
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 CPDF_Document* pdfDoc =
347 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
348 if (!pdfDoc)
349 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
352 if (!pRoot)
353 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800354
Wei Li9b761132016-01-29 15:44:20 -0800355 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 if (!pAcroForm)
357 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800358
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
360 if (!pXFA)
361 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800362
Wei Li9b761132016-01-29 15:44:20 -0800363 FX_BOOL bDynamicXFA = pRoot->GetBooleanBy("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 if (bDynamicXFA)
Tom Sepezd3116dc2015-11-24 15:58:06 -0800366 *docType = DOCTYPE_DYNAMIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 else
368 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800371}
372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
374 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700375}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800376#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378class CMemFile final : public IFX_FileRead {
379 public:
380 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381
Lei Zhang3884dba2015-10-19 17:27:53 -0700382 void Release() override { delete this; }
383 FX_FILESIZE GetSize() override { return m_size; }
384 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 if (offset < 0) {
386 return FALSE;
387 }
388 FX_SAFE_FILESIZE newPos =
389 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
390 newPos += offset;
391 if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
392 return FALSE;
393 }
394 FXSYS_memcpy(buffer, m_pBuf + offset, size);
395 return TRUE;
396 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700399 ~CMemFile() override {}
400
Lei Zhang3884dba2015-10-19 17:27:53 -0700401 uint8_t* const m_pBuf;
402 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403};
Lei Zhang3884dba2015-10-19 17:27:53 -0700404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
406 int size,
407 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700408 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 pParser->SetPassword(password);
410 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
Tom Sepezf10ae632016-01-26 14:19:52 -0800411 CPDF_Parser::Error error = pParser->StartParse(pMemFile);
412 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800414 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 return NULL;
416 }
417 CPDF_Document* pDoc = NULL;
418 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800419 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700420 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421}
422
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423DLLEXPORT FPDF_DOCUMENT STDCALL
424FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
425 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700426 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700428 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Tom Sepezf10ae632016-01-26 14:19:52 -0800429 CPDF_Parser::Error error = pParser->StartParse(pFile);
430 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800432 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 return NULL;
434 }
435 CPDF_Document* pDoc = NULL;
436 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800437 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700438 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439}
440
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
442 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700443 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700445
Tom Sepez471a1032015-10-15 16:17:18 -0700446 *fileVersion = 0;
447 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
448 if (!pDoc)
449 return FALSE;
450
451 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (!pParser)
453 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700454
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 *fileVersion = pParser->GetFileVersion();
456 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700457}
458
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
460// header).
461DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700462 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
463 if (!pDoc)
Tom Sepez51da0932015-11-25 16:05:49 -0800464#ifndef PDF_ENABLE_XFA
465 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800466#else // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 return (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800468#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469
Tom Sepez471a1032015-10-15 16:17:18 -0700470 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800471 return pDict ? pDict->GetIntegerBy("P") : (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472}
473
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700475 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
476 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700478
Tom Sepez471a1032015-10-15 16:17:18 -0700479 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800480 return pDict ? pDict->GetIntegerBy("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700481}
482
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800484 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700485 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700486}
487
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
489 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800490 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
491 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700492 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800493
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700494 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700495 return nullptr;
496
Tom Sepez40e9ff32015-11-30 12:39:54 -0800497#ifdef PDF_ENABLE_XFA
498 return pDoc->GetPage(page_index);
499#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800500 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800501 if (!pDict)
Tom Sepez51da0932015-11-25 16:05:49 -0800502 return NULL;
503 CPDF_Page* pPage = new CPDF_Page;
504 pPage->Load(pDoc, pDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -0800505 pPage->ParseContent(nullptr);
Tom Sepez51da0932015-11-25 16:05:49 -0800506 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800507#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508}
509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510DLLEXPORT double STDCALL FPDF_GetPageWidth(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->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700513}
514
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800516 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700517 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700518}
519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520void DropContext(void* data) {
521 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700522}
523
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700524#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
526 FPDF_PAGE page,
527 int start_x,
528 int start_y,
529 int size_x,
530 int size_y,
531 int rotate,
532 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700533 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 if (!pPage)
535 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700536
Tom Sepezae51c812015-08-05 12:34:06 -0700537 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700539
Lei Zhangae85f872016-02-19 14:57:07 -0800540#if !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800541 CFX_DIBitmap* pBitmap = nullptr;
542 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
543 FX_BOOL bHasImageMask = pPage->HasImageMask();
544 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700545 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 pBitmap->Create(size_x, size_y, FXDIB_Argb);
547 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700548#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700549 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551#else
Tom Sepezae51c812015-08-05 12:34:06 -0700552 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554#endif
Jun Fang1aeeceb2015-12-29 10:27:44 +0800555 } else {
Tom Sepezae51c812015-08-05 12:34:06 -0700556 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800557 }
Bo Xud4e406e2014-08-13 11:03:19 -0700558
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
560 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700561
Jun Fang1aeeceb2015-12-29 10:27:44 +0800562 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 if (pBitmap) {
564 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700565
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700567 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 int pitch = pBitmap->GetPitch();
569 pDst->Create(size_x, size_y, FXDIB_Rgb32);
570 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
571 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
572 FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
573 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
574 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800575 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800577 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700578 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700580#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 // get clip region
582 RECT rect, cliprect;
583 rect.left = start_x;
584 rect.top = start_y;
585 rect.right = start_x + size_x;
586 rect.bottom = start_y + size_y;
587 GetClipBox(dc, &cliprect);
588 IntersectRect(&rect, &rect, &cliprect);
589 int width = rect.right - rect.left;
590 int height = rect.bottom - rect.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700591
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 // Create a DIB section
593 LPVOID pBuffer;
594 BITMAPINFOHEADER bmih;
595 FXSYS_memset(&bmih, 0, sizeof bmih);
596 bmih.biSize = sizeof bmih;
597 bmih.biBitCount = 24;
598 bmih.biHeight = -height;
599 bmih.biPlanes = 1;
600 bmih.biWidth = width;
601 pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
602 &pBuffer, NULL, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 // Create a device with this external buffer
606 pContext->m_pBitmap = new CFX_DIBitmap;
607 pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
608 pContext->m_pDevice = new CPDF_FxgeDevice;
609 ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
610
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 // output to bitmap device
612 FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
613 start_y - rect.top, size_x, size_y, rotate, flags);
614
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 // Now output to real device
616 HDC hMemDC = CreateCompatibleDC(dc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
618
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
620 SelectObject(hMemDC, hOldBitmap);
621 DeleteDC(hMemDC);
622
Lei Zhangae85f872016-02-19 14:57:07 -0800623#endif // !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800624 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800626
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 delete pContext;
628 pPage->RemovePrivateData((void*)1);
629}
Lei Zhangae85f872016-02-19 14:57:07 -0800630#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631
632DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
633 FPDF_PAGE page,
634 int start_x,
635 int start_y,
636 int size_x,
637 int size_y,
638 int rotate,
639 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700640 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700642 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 if (!pPage)
644 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700645 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 pPage->SetPrivateData((void*)1, pContext, DropContext);
647#ifdef _SKIA_SUPPORT_
Cary Clark399be5b2016-03-14 16:51:29 -0400648 pContext->m_pDevice = new CFX_SkiaDevice();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649
650 if (flags & FPDF_REVERSE_BYTE_ORDER)
651 ((CFX_SkiaDevice*)pContext->m_pDevice)
652 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
653 else
654 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
655#else
Tom Sepezae51c812015-08-05 12:34:06 -0700656 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700657
658 if (flags & FPDF_REVERSE_BYTE_ORDER)
659 ((CFX_FxgeDevice*)pContext->m_pDevice)
660 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
661 else
662 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
663#endif
664
665 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
666 rotate, flags, TRUE, NULL);
667
668 delete pContext;
669 pPage->RemovePrivateData((void*)1);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700670}
671
Cary Clark399be5b2016-03-14 16:51:29 -0400672#ifdef _SKIA_SUPPORT_
673DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
674 int size_x,
675 int size_y) {
676 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
677 if (!pPage)
678 return nullptr;
679 std::unique_ptr<CRenderContext> pContext(new CRenderContext);
680 pPage->SetPrivateData((void*)1, pContext.get(), DropContext);
681 CFX_SkiaDevice* skDevice = new CFX_SkiaDevice();
682 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
683 pContext->m_pDevice = skDevice;
684
685 FPDF_RenderPage_Retail(pContext.get(), page, 0, 0, size_x, size_y, 0, 0, TRUE,
686 NULL);
687 pPage->RemovePrivateData((void*)1);
688 return recorder;
689}
690#endif
691
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
693 if (!page)
694 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800695#ifdef PDF_ENABLE_XFA
696 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
697 pPage->Release();
698#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800699 CPDFSDK_PageView* pPageView =
700 (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
701 if (pPageView && pPageView->IsLocked()) {
702 pPageView->TakeOverPage();
703 return;
704 }
705 delete (CPDF_Page*)page;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800706#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700707}
708
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800710#ifdef PDF_ENABLE_XFA
Jun Fangfc751362015-12-16 21:23:39 -0800711 delete UnderlyingFromFPDFDocument(document);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800712#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800713 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
714 if (!pDoc)
715 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800716 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800717 if (!pParser) {
718 delete pDoc;
719 return;
720 }
721 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800722#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700723}
724
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
726 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700727}
728
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
730 int start_x,
731 int start_y,
732 int size_x,
733 int size_y,
734 int rotate,
735 int device_x,
736 int device_y,
737 double* page_x,
738 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800739 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700740 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800741 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800742#ifdef PDF_ENABLE_XFA
743 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
744 device_y, page_x, page_y);
745#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800746 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800747 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
748 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800749 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800750 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800751 FX_FLOAT page_x_f, page_y_f;
752 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
753 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800754 *page_x = (page_x_f);
755 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800756#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700757}
758
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
760 int start_x,
761 int start_y,
762 int size_x,
763 int size_y,
764 int rotate,
765 double page_x,
766 double page_y,
767 int* device_x,
768 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700769 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800771 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700772 if (!pPage)
773 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800774#ifdef PDF_ENABLE_XFA
775 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
776 device_x, device_y);
777#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800778 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800779 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
780 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800781 FX_FLOAT device_x_f, device_y_f;
782 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
783 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800784 *device_x = FXSYS_round(device_x_f);
785 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800786#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700787}
788
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700789DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
790 int height,
791 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800792 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
794 return NULL;
795 }
796 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700797}
798
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
800 int height,
801 int format,
802 void* first_scan,
803 int stride) {
804 FXDIB_Format fx_format;
805 switch (format) {
806 case FPDFBitmap_Gray:
807 fx_format = FXDIB_8bppRgb;
808 break;
809 case FPDFBitmap_BGR:
810 fx_format = FXDIB_Rgb;
811 break;
812 case FPDFBitmap_BGRx:
813 fx_format = FXDIB_Rgb32;
814 break;
815 case FPDFBitmap_BGRA:
816 fx_format = FXDIB_Argb;
817 break;
818 default:
819 return NULL;
820 }
821 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
822 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
823 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
827 int left,
828 int top,
829 int width,
830 int height,
831 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800832 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833 return;
834#ifdef _SKIA_SUPPORT_
835 CFX_SkiaDevice device;
836#else
837 CFX_FxgeDevice device;
838#endif
839 device.Attach((CFX_DIBitmap*)bitmap);
840 if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
841 color |= 0xFF000000;
842 FX_RECT rect(left, top, left + width, top + height);
843 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700844}
845
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800847 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700848}
849
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800851 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700852}
853
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800855 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856}
857
858DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800859 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860}
861
862DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
863 delete (CFX_DIBitmap*)bitmap;
864}
865
866void FPDF_RenderPage_Retail(CRenderContext* pContext,
867 FPDF_PAGE page,
868 int start_x,
869 int start_y,
870 int size_x,
871 int size_y,
872 int rotate,
873 int flags,
874 FX_BOOL bNeedToRestore,
875 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700876 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
877 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 return;
879
880 if (!pContext->m_pOptions)
881 pContext->m_pOptions = new CPDF_RenderOptions;
882
883 if (flags & FPDF_LCD_TEXT)
884 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
885 else
886 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
887 if (flags & FPDF_NO_NATIVETEXT)
888 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
889 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
890 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
891 if (flags & FPDF_RENDER_FORCEHALFTONE)
892 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800893#ifndef PDF_ENABLE_XFA
894 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
895 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
896 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
897 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
898 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
899 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800900#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 // Grayscale output
902 if (flags & FPDF_GRAYSCALE) {
903 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
904 pContext->m_pOptions->m_ForeColor = 0;
905 pContext->m_pOptions->m_BackColor = 0xffffff;
906 }
907 const CPDF_OCContext::UsageType usage =
908 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
909 pContext->m_pOptions->m_AddFlags = flags >> 8;
910 pContext->m_pOptions->m_pOCContext =
911 new CPDF_OCContext(pPage->m_pDocument, usage);
912
Tom Sepez60d909e2015-12-10 15:34:55 -0800913 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
915
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 pContext->m_pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800917 pContext->m_pDevice->SetClip_Rect(
918 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919
Tom Sepez979ddd82015-12-17 13:41:13 -0800920 pContext->m_pContext = new CPDF_RenderContext(pPage);
Tom Sepez71fdc342016-01-22 12:06:32 -0800921 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922
923 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700924 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
926 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
927 &matrix, TRUE, NULL);
928 }
929
Tom Sepezb3b67622015-10-19 16:20:03 -0700930 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
931 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
932 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800933 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935}
936
937DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
938 int page_index,
939 double* width,
940 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800941 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
942 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 return FALSE;
944
Tom Sepez40e9ff32015-11-30 12:39:54 -0800945#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 int count = pDoc->GetPageCount();
947 if (page_index < 0 || page_index >= count)
948 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
950 if (!pPage)
951 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800952 *width = pPage->GetPageWidth();
953 *height = pPage->GetPageHeight();
954#else // PDF_ENABLE_XFA
955 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
956 if (!pDict)
957 return FALSE;
Tom Sepez51da0932015-11-25 16:05:49 -0800958 CPDF_Page page;
959 page.Load(pDoc, pDict);
960 *width = page.GetPageWidth();
961 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800962#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963
964 return TRUE;
965}
966
967DLLEXPORT FPDF_BOOL STDCALL
968FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700969 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 if (!pDoc)
971 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700972 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 return viewRef.PrintScaling();
974}
975
976DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700977 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 if (!pDoc)
979 return 1;
980 CPDF_ViewerPreferences viewRef(pDoc);
981 return viewRef.NumCopies();
982}
983
984DLLEXPORT FPDF_PAGERANGE STDCALL
985FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700986 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987 if (!pDoc)
988 return NULL;
989 CPDF_ViewerPreferences viewRef(pDoc);
990 return viewRef.PrintPageRange();
991}
992
993DLLEXPORT FPDF_DUPLEXTYPE STDCALL
994FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700995 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -0700997 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 CPDF_ViewerPreferences viewRef(pDoc);
999 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001000 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001002 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001004 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 return DuplexFlipLongEdge;
1006 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001007}
1008
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001010 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1011 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001013
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1015 if (!pRoot)
1016 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001017
Lei Zhangd983b092015-12-14 16:58:33 -08001018 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001019 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
Wei Li9b761132016-01-29 15:44:20 -08001020 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 if (pDest)
1022 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001023
1024 if (!count.IsValid())
1025 return 0;
1026
1027 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028}
1029
1030DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1031 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001033 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034
Tom Sepez471a1032015-10-15 16:17:18 -07001035 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1036 if (!pDoc)
1037 return nullptr;
1038
Lei Zhangd983b092015-12-14 16:58:33 -08001039 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001040 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041}
1042
Tom Sepez51da0932015-11-25 16:05:49 -08001043#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044FPDF_RESULT FPDF_BStr_Init(FPDF_BSTR* str) {
1045 if (!str)
1046 return -1;
1047
1048 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1049 return 0;
1050}
1051
1052FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length) {
1053 if (!str)
1054 return -1;
1055 if (!bstr || !length)
1056 return -1;
1057 if (length == -1)
1058 length = FXSYS_strlen(bstr);
1059
1060 if (length == 0) {
1061 if (str->str) {
1062 FX_Free(str->str);
1063 str->str = NULL;
1064 }
1065 str->len = 0;
1066 return 0;
1067 }
1068
1069 if (str->str && str->len < length)
1070 str->str = FX_Realloc(char, str->str, length + 1);
1071 else if (!str->str)
1072 str->str = FX_Alloc(char, length + 1);
1073
1074 str->str[length] = 0;
1075 if (str->str == NULL)
1076 return -1;
1077
1078 FXSYS_memcpy(str->str, bstr, length);
1079 str->len = length;
1080
1081 return 0;
1082}
1083
1084FPDF_RESULT FPDF_BStr_Clear(FPDF_BSTR* str) {
1085 if (!str)
1086 return -1;
1087
1088 if (str->str) {
1089 FX_Free(str->str);
1090 str->str = NULL;
1091 }
1092 str->len = 0;
1093 return 0;
1094}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001095#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096
1097DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1098 int index,
1099 void* buffer,
1100 long* buflen) {
1101 if (!buffer)
1102 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001103
1104 if (index < 0)
1105 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
Tom Sepezbf59a072015-10-21 14:07:23 -07001107 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001108 if (!pDoc)
1109 return nullptr;
1110
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1112 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001113 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001114
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001115 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001117 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 int count = nameTree.GetCount();
1119 if (index >= count) {
Wei Li9b761132016-01-29 15:44:20 -08001120 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001122 return nullptr;
1123
1124 pdfium::base::CheckedNumeric<int> checked_count = count;
1125 checked_count += pDest->GetCount();
1126 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1127 return nullptr;
1128
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001129 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001131 for (const auto& it : *pDest) {
1132 bsName = it.first;
1133 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 if (!pDestObj)
1135 continue;
1136 if (i == index)
1137 break;
1138 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001139 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140 } else {
1141 pDestObj = nameTree.LookupValue(index, bsName);
1142 }
1143 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001144 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001145 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -08001146 pDestObj = pDict->GetArrayBy("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001148 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001150 if (!pDestObj->IsArray())
1151 return nullptr;
1152
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153 CFX_WideString wsName = PDF_DecodeText(bsName);
1154 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
1155 unsigned int len = utf16Name.GetLength();
1156 if (!buffer) {
1157 *buflen = len;
1158 } else if (*buflen >= len) {
1159 memcpy(buffer, utf16Name.c_str(), len);
1160 *buflen = len;
1161 } else {
1162 *buflen = -1;
1163 }
1164 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001165}