blob: 1c9024d8369a464e3153ccf8d3043fb804b417a5 [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
Dan Sinclair455a4192016-03-16 09:48:56 -040011#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040012#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
13#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
14#include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h"
15#include "core/fpdfapi/include/cpdf_modulemgr.h"
Lei Zhanga688a042015-11-09 13:57:49 -080016#include "core/include/fxcodec/fx_codec.h"
17#include "core/include/fxcrt/fx_safe_types.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080018#include "fpdfsdk/include/fsdk_define.h"
19#include "fpdfsdk/include/fsdk_mgr.h"
20#include "fpdfsdk/include/fsdk_rendercontext.h"
21#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080022#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080023#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080024#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070025
Tom Sepez40e9ff32015-11-30 12:39:54 -080026#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080027#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
28#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
29#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
30#include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080031#include "public/fpdf_formfill.h"
32#endif // PDF_ENABLE_XFA
33
Tom Sepez50d12ad2015-11-24 09:50:51 -080034UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
35 return static_cast<UnderlyingDocumentType*>(doc);
36}
37
38FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
39 return static_cast<FPDF_DOCUMENT>(doc);
40}
41
42UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
43 return static_cast<UnderlyingPageType*>(page);
44}
45
Tom Sepez471a1032015-10-15 16:17:18 -070046CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080047#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080048 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080049#else // PDF_ENABLE_XFA
50 return UnderlyingFromFPDFDocument(doc);
51#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070052}
53
Tom Sepezbf59a072015-10-21 14:07:23 -070054FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080055#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080056 return doc ? FPDFDocumentFromUnderlying(
57 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
58 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080059#else // PDF_ENABLE_XFA
60 return FPDFDocumentFromUnderlying(doc);
61#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070062}
63
Tom Sepezdb0be962015-10-16 14:00:21 -070064CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080065#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080066 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080067#else // PDF_ENABLE_XFA
68 return UnderlyingFromFPDFPage(page);
69#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070070}
71
Tom Sepez40e9ff32015-11-30 12:39:54 -080072#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
74 m_pFS = pFS;
75 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078IFX_FileStream* CFPDF_FileStream::Retain() {
79 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082void CFPDF_FileStream::Release() {
83 if (m_pFS && m_pFS->Release)
84 m_pFS->Release(m_pFS->clientData);
85 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088FX_FILESIZE CFPDF_FileStream::GetSize() {
89 if (m_pFS && m_pFS->GetSize)
90 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
91 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094FX_BOOL CFPDF_FileStream::IsEOF() {
95 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
99 FX_FILESIZE offset,
100 size_t size) {
101 if (!buffer || !size || !m_pFS->ReadBlock)
102 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
105 (FPDF_DWORD)size) == 0) {
106 m_nCurPos = offset + size;
107 return TRUE;
108 }
109 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700110}
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
113 if (!buffer || !size || !m_pFS->ReadBlock)
114 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 FX_FILESIZE nSize = GetSize();
117 if (m_nCurPos >= nSize)
118 return 0;
119 FX_FILESIZE dwAvail = nSize - m_nCurPos;
120 if (dwAvail < (FX_FILESIZE)size)
121 size = (size_t)dwAvail;
122 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
123 (FPDF_DWORD)size) == 0) {
124 m_nCurPos += size;
125 return size;
126 }
Bo Xufdc00a72014-10-28 23:03:33 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
132 FX_FILESIZE offset,
133 size_t size) {
134 if (!m_pFS || !m_pFS->WriteBlock)
135 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
138 (FPDF_DWORD)size) == 0) {
139 m_nCurPos = offset + size;
140 return TRUE;
141 }
142 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145FX_BOOL CFPDF_FileStream::Flush() {
146 if (!m_pFS || !m_pFS->Flush)
147 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700150}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800151#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
154 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800155#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 m_BufferOffset = (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800157#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700158}
159
Tom Sepez40e9ff32015-11-30 12:39:54 -0800160#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch) {
162 if (pos >= m_FileAccess.m_FileLen)
163 return FALSE;
164 if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset ||
165 pos >= m_BufferOffset + 512) {
166 // Need to read from file access
167 m_BufferOffset = pos;
168 int size = 512;
169 if (pos + 512 > m_FileAccess.m_FileLen)
170 size = m_FileAccess.m_FileLen - pos;
171 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
172 size))
173 return FALSE;
174 }
175 ch = m_Buffer[pos - m_BufferOffset];
176 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700177}
178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos,
180 uint8_t* pBuf,
181 FX_DWORD size) {
182 if (pos + size > m_FileAccess.m_FileLen)
183 return FALSE;
184 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800186#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
189 FX_FILESIZE offset,
190 size_t size) {
191 if (offset < 0) {
192 return FALSE;
193 }
194 FX_SAFE_FILESIZE newPos =
195 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
196 newPos += offset;
197 if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
198 return FALSE;
199 }
200 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
201 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
208 switch (policy) {
209 case FPDF_POLICY_MACHINETIME_ACCESS: {
210 if (enable)
211 foxit_sandbox_policy |= 0x01;
212 else
213 foxit_sandbox_policy &= 0xFFFFFFFE;
214 } break;
215 default:
216 break;
217 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
221 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700222 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700223 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700225 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227}
228
Tom Sepez2c286192015-06-18 12:47:11 -0700229CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700232 FPDF_InitLibraryWithConfig(nullptr);
233}
234
Dan Sinclairf766ad22016-03-14 13:51:24 -0400235DLLEXPORT void STDCALL
236FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238
Lei Zhang6f62d532015-09-23 15:31:44 -0700239 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 CPDF_ModuleMgr::Create();
Tom Sepez1b246282015-11-25 15:15:31 -0800243 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
244 pModuleMgr->SetCodecModule(g_pCodecModule);
245 pModuleMgr->InitPageModule();
246 pModuleMgr->InitRenderModule();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800247#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +0800248 CPDFXFA_App::GetInstance()->Initialize(
249 (cfg && cfg->version >= 2)
250 ? reinterpret_cast<FXJSE_HRUNTIME>(cfg->m_pIsolate)
251 : nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800252#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800253 pModuleMgr->LoadEmbeddedGB1CMaps();
254 pModuleMgr->LoadEmbeddedJapan1CMaps();
255 pModuleMgr->LoadEmbeddedCNS1CMaps();
256 pModuleMgr->LoadEmbeddedKorea1CMaps();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800257#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700258 if (cfg && cfg->version >= 2)
259 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800263#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800265#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 CPDF_ModuleMgr::Destroy();
267 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 delete g_pCodecModule;
270 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
273#ifndef _WIN32
274int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275void SetLastError(int err) {
276 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277}
278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279int GetLastError() {
280 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800282#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Tom Sepezf10ae632016-01-26 14:19:52 -0800284void ProcessParseError(CPDF_Parser::Error err) {
285 FX_DWORD err_code;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800287 switch (err) {
288 case CPDF_Parser::SUCCESS:
289 err_code = FPDF_ERR_SUCCESS;
290 break;
291 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 err_code = FPDF_ERR_FILE;
293 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800294 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 err_code = FPDF_ERR_FORMAT;
296 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800297 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 err_code = FPDF_ERR_PASSWORD;
299 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800300 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 err_code = FPDF_ERR_SECURITY;
302 break;
303 }
304 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305}
306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
308 FPDF_BOOL enable) {
309 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
313 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700314 // NOTE: the creation of the file needs to be by the embedder on the
315 // other side of this API.
316 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
317 if (!pFileAccess) {
318 return nullptr;
319 }
320
321 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700323
Tom Sepezf10ae632016-01-26 14:19:52 -0800324 CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
325 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800327 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 return NULL;
329 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800330#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 CPDF_Document* pPDFDoc = pParser->GetDocument();
332 if (!pPDFDoc)
333 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700336 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800337#else // PDF_ENABLE_XFA
338 return pParser->GetDocument();
339#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
Jun Fange118ce92015-02-17 06:50:08 -0800341
Tom Sepez40e9ff32015-11-30 12:39:54 -0800342#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
344 int* docType) {
345 if (!document)
346 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 CPDF_Document* pdfDoc =
349 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
350 if (!pdfDoc)
351 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
354 if (!pRoot)
355 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800356
Wei Li9b761132016-01-29 15:44:20 -0800357 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 if (!pAcroForm)
359 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800360
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
362 if (!pXFA)
363 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800364
Wei Li9b761132016-01-29 15:44:20 -0800365 FX_BOOL bDynamicXFA = pRoot->GetBooleanBy("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 if (bDynamicXFA)
Tom Sepezd3116dc2015-11-24 15:58:06 -0800368 *docType = DOCTYPE_DYNAMIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 else
370 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800371
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
376 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700377}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800378#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380class CMemFile final : public IFX_FileRead {
381 public:
382 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383
Lei Zhang3884dba2015-10-19 17:27:53 -0700384 void Release() override { delete this; }
385 FX_FILESIZE GetSize() override { return m_size; }
386 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 if (offset < 0) {
388 return FALSE;
389 }
390 FX_SAFE_FILESIZE newPos =
391 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
392 newPos += offset;
393 if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
394 return FALSE;
395 }
396 FXSYS_memcpy(buffer, m_pBuf + offset, size);
397 return TRUE;
398 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700401 ~CMemFile() override {}
402
Lei Zhang3884dba2015-10-19 17:27:53 -0700403 uint8_t* const m_pBuf;
404 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405};
Lei Zhang3884dba2015-10-19 17:27:53 -0700406
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
408 int size,
409 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700410 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 pParser->SetPassword(password);
412 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
Tom Sepezf10ae632016-01-26 14:19:52 -0800413 CPDF_Parser::Error error = pParser->StartParse(pMemFile);
414 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800416 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 return NULL;
418 }
419 CPDF_Document* pDoc = NULL;
420 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800421 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700422 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700423}
424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425DLLEXPORT FPDF_DOCUMENT STDCALL
426FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
427 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700428 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700430 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Tom Sepezf10ae632016-01-26 14:19:52 -0800431 CPDF_Parser::Error error = pParser->StartParse(pFile);
432 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800434 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 return NULL;
436 }
437 CPDF_Document* pDoc = NULL;
438 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800439 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700440 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441}
442
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
444 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700445 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700447
Tom Sepez471a1032015-10-15 16:17:18 -0700448 *fileVersion = 0;
449 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
450 if (!pDoc)
451 return FALSE;
452
453 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 if (!pParser)
455 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700456
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 *fileVersion = pParser->GetFileVersion();
458 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459}
460
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
462// header).
463DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700464 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
465 if (!pDoc)
Tom Sepez51da0932015-11-25 16:05:49 -0800466#ifndef PDF_ENABLE_XFA
467 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800468#else // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 return (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800470#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700471
Tom Sepez471a1032015-10-15 16:17:18 -0700472 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800473 return pDict ? pDict->GetIntegerBy("P") : (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700474}
475
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700477 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
478 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700480
Tom Sepez471a1032015-10-15 16:17:18 -0700481 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800482 return pDict ? pDict->GetIntegerBy("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700483}
484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800486 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700487 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700488}
489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
491 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800492 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
493 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700494 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800495
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700496 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700497 return nullptr;
498
Tom Sepez40e9ff32015-11-30 12:39:54 -0800499#ifdef PDF_ENABLE_XFA
500 return pDoc->GetPage(page_index);
501#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800502 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800503 if (!pDict)
Tom Sepez51da0932015-11-25 16:05:49 -0800504 return NULL;
505 CPDF_Page* pPage = new CPDF_Page;
506 pPage->Load(pDoc, pDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -0800507 pPage->ParseContent(nullptr);
Tom Sepez51da0932015-11-25 16:05:49 -0800508 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800509#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700510}
511
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800513 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700514 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515}
516
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800518 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700519 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700520}
521
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522void DropContext(void* data) {
523 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700524}
525
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700526#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
528 FPDF_PAGE page,
529 int start_x,
530 int start_y,
531 int size_x,
532 int size_y,
533 int rotate,
534 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700535 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 if (!pPage)
537 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538
Tom Sepezae51c812015-08-05 12:34:06 -0700539 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700541
Lei Zhangae85f872016-02-19 14:57:07 -0800542#if !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800543 CFX_DIBitmap* pBitmap = nullptr;
544 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
545 FX_BOOL bHasImageMask = pPage->HasImageMask();
546 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700547 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548 pBitmap->Create(size_x, size_y, FXDIB_Argb);
549 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700551 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700553#else
Tom Sepezae51c812015-08-05 12:34:06 -0700554 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700556#endif
Jun Fang1aeeceb2015-12-29 10:27:44 +0800557 } else {
Tom Sepezae51c812015-08-05 12:34:06 -0700558 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800559 }
Bo Xud4e406e2014-08-13 11:03:19 -0700560
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
562 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700563
Jun Fang1aeeceb2015-12-29 10:27:44 +0800564 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 if (pBitmap) {
566 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700569 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 int pitch = pBitmap->GetPitch();
571 pDst->Create(size_x, size_y, FXDIB_Rgb32);
572 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
573 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
574 FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
575 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
576 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800577 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800579 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700580 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700582#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 // get clip region
584 RECT rect, cliprect;
585 rect.left = start_x;
586 rect.top = start_y;
587 rect.right = start_x + size_x;
588 rect.bottom = start_y + size_y;
589 GetClipBox(dc, &cliprect);
590 IntersectRect(&rect, &rect, &cliprect);
591 int width = rect.right - rect.left;
592 int height = rect.bottom - rect.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700593
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 // Create a DIB section
595 LPVOID pBuffer;
596 BITMAPINFOHEADER bmih;
597 FXSYS_memset(&bmih, 0, sizeof bmih);
598 bmih.biSize = sizeof bmih;
599 bmih.biBitCount = 24;
600 bmih.biHeight = -height;
601 bmih.biPlanes = 1;
602 bmih.biWidth = width;
603 pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
604 &pBuffer, NULL, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
606
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 // Create a device with this external buffer
608 pContext->m_pBitmap = new CFX_DIBitmap;
609 pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
610 pContext->m_pDevice = new CPDF_FxgeDevice;
611 ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
612
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 // output to bitmap device
614 FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
615 start_y - rect.top, size_x, size_y, rotate, flags);
616
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 // Now output to real device
618 HDC hMemDC = CreateCompatibleDC(dc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
620
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
622 SelectObject(hMemDC, hOldBitmap);
623 DeleteDC(hMemDC);
624
Lei Zhangae85f872016-02-19 14:57:07 -0800625#endif // !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800626 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800628
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 delete pContext;
630 pPage->RemovePrivateData((void*)1);
631}
Lei Zhangae85f872016-02-19 14:57:07 -0800632#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633
634DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
635 FPDF_PAGE page,
636 int start_x,
637 int start_y,
638 int size_x,
639 int size_y,
640 int rotate,
641 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700642 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700644 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 if (!pPage)
646 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700647 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 pPage->SetPrivateData((void*)1, pContext, DropContext);
649#ifdef _SKIA_SUPPORT_
Cary Clark399be5b2016-03-14 16:51:29 -0400650 pContext->m_pDevice = new CFX_SkiaDevice();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651
652 if (flags & FPDF_REVERSE_BYTE_ORDER)
653 ((CFX_SkiaDevice*)pContext->m_pDevice)
654 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
655 else
656 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
657#else
Tom Sepezae51c812015-08-05 12:34:06 -0700658 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659
660 if (flags & FPDF_REVERSE_BYTE_ORDER)
661 ((CFX_FxgeDevice*)pContext->m_pDevice)
662 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
663 else
664 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
665#endif
666
667 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
668 rotate, flags, TRUE, NULL);
669
670 delete pContext;
671 pPage->RemovePrivateData((void*)1);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
Cary Clark399be5b2016-03-14 16:51:29 -0400674#ifdef _SKIA_SUPPORT_
675DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
676 int size_x,
677 int size_y) {
678 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
679 if (!pPage)
680 return nullptr;
681 std::unique_ptr<CRenderContext> pContext(new CRenderContext);
682 pPage->SetPrivateData((void*)1, pContext.get(), DropContext);
683 CFX_SkiaDevice* skDevice = new CFX_SkiaDevice();
684 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
685 pContext->m_pDevice = skDevice;
686
687 FPDF_RenderPage_Retail(pContext.get(), page, 0, 0, size_x, size_y, 0, 0, TRUE,
688 NULL);
689 pPage->RemovePrivateData((void*)1);
690 return recorder;
691}
692#endif
693
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
695 if (!page)
696 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800697#ifdef PDF_ENABLE_XFA
698 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
699 pPage->Release();
700#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800701 CPDFSDK_PageView* pPageView =
702 (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
703 if (pPageView && pPageView->IsLocked()) {
704 pPageView->TakeOverPage();
705 return;
706 }
707 delete (CPDF_Page*)page;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800708#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700709}
710
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800712#ifdef PDF_ENABLE_XFA
Jun Fangfc751362015-12-16 21:23:39 -0800713 delete UnderlyingFromFPDFDocument(document);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800714#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800715 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
716 if (!pDoc)
717 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800718 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800719 if (!pParser) {
720 delete pDoc;
721 return;
722 }
723 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800724#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700725}
726
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
728 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700729}
730
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
732 int start_x,
733 int start_y,
734 int size_x,
735 int size_y,
736 int rotate,
737 int device_x,
738 int device_y,
739 double* page_x,
740 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800741 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800743 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800744#ifdef PDF_ENABLE_XFA
745 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
746 device_y, page_x, page_y);
747#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800748 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800749 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
750 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800751 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800752 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800753 FX_FLOAT page_x_f, page_y_f;
754 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
755 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800756 *page_x = (page_x_f);
757 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800758#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700759}
760
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
762 int start_x,
763 int start_y,
764 int size_x,
765 int size_y,
766 int rotate,
767 double page_x,
768 double page_y,
769 int* device_x,
770 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700771 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800773 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700774 if (!pPage)
775 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800776#ifdef PDF_ENABLE_XFA
777 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
778 device_x, device_y);
779#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800780 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800781 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
782 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800783 FX_FLOAT device_x_f, device_y_f;
784 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
785 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800786 *device_x = FXSYS_round(device_x_f);
787 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800788#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700789}
790
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700791DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
792 int height,
793 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800794 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
796 return NULL;
797 }
798 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700799}
800
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
802 int height,
803 int format,
804 void* first_scan,
805 int stride) {
806 FXDIB_Format fx_format;
807 switch (format) {
808 case FPDFBitmap_Gray:
809 fx_format = FXDIB_8bppRgb;
810 break;
811 case FPDFBitmap_BGR:
812 fx_format = FXDIB_Rgb;
813 break;
814 case FPDFBitmap_BGRx:
815 fx_format = FXDIB_Rgb32;
816 break;
817 case FPDFBitmap_BGRA:
818 fx_format = FXDIB_Argb;
819 break;
820 default:
821 return NULL;
822 }
823 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
824 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
825 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700826}
827
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
829 int left,
830 int top,
831 int width,
832 int height,
833 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800834 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 return;
836#ifdef _SKIA_SUPPORT_
837 CFX_SkiaDevice device;
838#else
839 CFX_FxgeDevice device;
840#endif
841 device.Attach((CFX_DIBitmap*)bitmap);
842 if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
843 color |= 0xFF000000;
844 FX_RECT rect(left, top, left + width, top + height);
845 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700846}
847
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800849 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700850}
851
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800853 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700854}
855
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800857 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858}
859
860DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800861 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862}
863
864DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
865 delete (CFX_DIBitmap*)bitmap;
866}
867
868void FPDF_RenderPage_Retail(CRenderContext* pContext,
869 FPDF_PAGE page,
870 int start_x,
871 int start_y,
872 int size_x,
873 int size_y,
874 int rotate,
875 int flags,
876 FX_BOOL bNeedToRestore,
877 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700878 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
879 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880 return;
881
882 if (!pContext->m_pOptions)
883 pContext->m_pOptions = new CPDF_RenderOptions;
884
885 if (flags & FPDF_LCD_TEXT)
886 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
887 else
888 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
889 if (flags & FPDF_NO_NATIVETEXT)
890 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
891 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
892 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
893 if (flags & FPDF_RENDER_FORCEHALFTONE)
894 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800895#ifndef PDF_ENABLE_XFA
896 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
897 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
898 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
899 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
900 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
901 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800902#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 // Grayscale output
904 if (flags & FPDF_GRAYSCALE) {
905 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
906 pContext->m_pOptions->m_ForeColor = 0;
907 pContext->m_pOptions->m_BackColor = 0xffffff;
908 }
909 const CPDF_OCContext::UsageType usage =
910 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
911 pContext->m_pOptions->m_AddFlags = flags >> 8;
912 pContext->m_pOptions->m_pOCContext =
913 new CPDF_OCContext(pPage->m_pDocument, usage);
914
Tom Sepez60d909e2015-12-10 15:34:55 -0800915 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
917
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 pContext->m_pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800919 pContext->m_pDevice->SetClip_Rect(
920 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921
Tom Sepez979ddd82015-12-17 13:41:13 -0800922 pContext->m_pContext = new CPDF_RenderContext(pPage);
Tom Sepez71fdc342016-01-22 12:06:32 -0800923 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924
925 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700926 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
928 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
929 &matrix, TRUE, NULL);
930 }
931
Tom Sepezb3b67622015-10-19 16:20:03 -0700932 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
933 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
934 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800935 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937}
938
939DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
940 int page_index,
941 double* width,
942 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800943 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
944 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 return FALSE;
946
Tom Sepez40e9ff32015-11-30 12:39:54 -0800947#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948 int count = pDoc->GetPageCount();
949 if (page_index < 0 || page_index >= count)
950 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
952 if (!pPage)
953 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800954 *width = pPage->GetPageWidth();
955 *height = pPage->GetPageHeight();
956#else // PDF_ENABLE_XFA
957 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
958 if (!pDict)
959 return FALSE;
Tom Sepez51da0932015-11-25 16:05:49 -0800960 CPDF_Page page;
961 page.Load(pDoc, pDict);
962 *width = page.GetPageWidth();
963 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800964#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965
966 return TRUE;
967}
968
969DLLEXPORT FPDF_BOOL STDCALL
970FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700971 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 if (!pDoc)
973 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700974 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 return viewRef.PrintScaling();
976}
977
978DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700979 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 if (!pDoc)
981 return 1;
982 CPDF_ViewerPreferences viewRef(pDoc);
983 return viewRef.NumCopies();
984}
985
986DLLEXPORT FPDF_PAGERANGE STDCALL
987FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700988 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 if (!pDoc)
990 return NULL;
991 CPDF_ViewerPreferences viewRef(pDoc);
992 return viewRef.PrintPageRange();
993}
994
995DLLEXPORT FPDF_DUPLEXTYPE STDCALL
996FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700997 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -0700999 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 CPDF_ViewerPreferences viewRef(pDoc);
1001 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001002 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001004 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001006 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 return DuplexFlipLongEdge;
1008 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001009}
1010
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001012 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1013 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001015
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1017 if (!pRoot)
1018 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001019
Lei Zhangd983b092015-12-14 16:58:33 -08001020 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001021 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
Wei Li9b761132016-01-29 15:44:20 -08001022 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 if (pDest)
1024 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001025
1026 if (!count.IsValid())
1027 return 0;
1028
1029 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030}
1031
1032DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1033 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001035 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036
Tom Sepez471a1032015-10-15 16:17:18 -07001037 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1038 if (!pDoc)
1039 return nullptr;
1040
Lei Zhangd983b092015-12-14 16:58:33 -08001041 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001042 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043}
1044
Tom Sepez51da0932015-11-25 16:05:49 -08001045#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046FPDF_RESULT FPDF_BStr_Init(FPDF_BSTR* str) {
1047 if (!str)
1048 return -1;
1049
1050 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1051 return 0;
1052}
1053
1054FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length) {
1055 if (!str)
1056 return -1;
1057 if (!bstr || !length)
1058 return -1;
1059 if (length == -1)
1060 length = FXSYS_strlen(bstr);
1061
1062 if (length == 0) {
1063 if (str->str) {
1064 FX_Free(str->str);
1065 str->str = NULL;
1066 }
1067 str->len = 0;
1068 return 0;
1069 }
1070
1071 if (str->str && str->len < length)
1072 str->str = FX_Realloc(char, str->str, length + 1);
1073 else if (!str->str)
1074 str->str = FX_Alloc(char, length + 1);
1075
1076 str->str[length] = 0;
1077 if (str->str == NULL)
1078 return -1;
1079
1080 FXSYS_memcpy(str->str, bstr, length);
1081 str->len = length;
1082
1083 return 0;
1084}
1085
1086FPDF_RESULT FPDF_BStr_Clear(FPDF_BSTR* str) {
1087 if (!str)
1088 return -1;
1089
1090 if (str->str) {
1091 FX_Free(str->str);
1092 str->str = NULL;
1093 }
1094 str->len = 0;
1095 return 0;
1096}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001097#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098
1099DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1100 int index,
1101 void* buffer,
1102 long* buflen) {
1103 if (!buffer)
1104 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001105
1106 if (index < 0)
1107 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108
Tom Sepezbf59a072015-10-21 14:07:23 -07001109 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001110 if (!pDoc)
1111 return nullptr;
1112
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1114 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001115 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001117 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001119 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 int count = nameTree.GetCount();
1121 if (index >= count) {
Wei Li9b761132016-01-29 15:44:20 -08001122 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001123 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001124 return nullptr;
1125
1126 pdfium::base::CheckedNumeric<int> checked_count = count;
1127 checked_count += pDest->GetCount();
1128 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1129 return nullptr;
1130
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001131 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001132 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001133 for (const auto& it : *pDest) {
1134 bsName = it.first;
1135 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 if (!pDestObj)
1137 continue;
1138 if (i == index)
1139 break;
1140 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001141 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142 } else {
1143 pDestObj = nameTree.LookupValue(index, bsName);
1144 }
1145 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001146 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001147 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -08001148 pDestObj = pDict->GetArrayBy("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001150 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001152 if (!pDestObj->IsArray())
1153 return nullptr;
1154
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 CFX_WideString wsName = PDF_DecodeText(bsName);
1156 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
1157 unsigned int len = utf16Name.GetLength();
1158 if (!buffer) {
1159 *buflen = len;
1160 } else if (*buflen >= len) {
1161 memcpy(buffer, utf16Name.c_str(), len);
1162 *buflen = len;
1163 } else {
1164 *buflen = -1;
1165 }
1166 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001167}