blob: 9f3ca0f9e8c434fcff8a96ee8d7de3e219a7fcf6 [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"
dsinclaircac704d2016-07-28 12:59:09 -070014#include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h"
weili9f515bc2016-07-24 08:08:24 -070015#include "core/fpdfapi/fpdf_render/include/cpdf_progressiverenderer.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040016#include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h"
17#include "core/fpdfapi/include/cpdf_modulemgr.h"
weili9f515bc2016-07-24 08:08:24 -070018#include "core/fpdfapi/include/cpdf_pagerendercontext.h"
dsinclaircac704d2016-07-28 12:59:09 -070019#include "core/fpdfdoc/include/cpdf_annotlist.h"
20#include "core/fpdfdoc/include/cpdf_nametree.h"
dsinclairf34518b2016-09-13 12:03:48 -070021#include "core/fpdfdoc/include/cpdf_occontext.h"
dsinclaircac704d2016-07-28 12:59:09 -070022#include "core/fpdfdoc/include/cpdf_viewerpreferences.h"
dsinclair86e54742016-04-06 12:30:31 -070023#include "core/fxcodec/include/fx_codec.h"
weili9f515bc2016-07-24 08:08:24 -070024#include "core/fxcrt/include/fx_memory.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040025#include "core/fxcrt/include/fx_safe_types.h"
npm9ada2d82016-08-10 07:51:38 -070026#include "core/fxge/include/cfx_fxgedevice.h"
npm26b86e62016-08-04 17:22:14 -070027#include "core/fxge/include/cfx_gemodule.h"
dsinclairf34518b2016-09-13 12:03:48 -070028#include "fpdfsdk/include/cpdfsdk_pageview.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080029#include "fpdfsdk/include/fsdk_define.h"
weili9f515bc2016-07-24 08:08:24 -070030#include "fpdfsdk/include/fsdk_pauseadapter.h"
dsinclair64376be2016-03-31 20:03:24 -070031#include "fpdfsdk/javascript/ijs_runtime.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080032#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080033#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080034#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070035
Tom Sepez40e9ff32015-11-30 12:39:54 -080036#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070037#include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
38#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
39#include "fpdfsdk/fpdfxfa/include/fpdfxfa_page.h"
40#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080041#include "public/fpdf_formfill.h"
42#endif // PDF_ENABLE_XFA
43
thestig25fa42f2016-05-25 21:39:46 -070044#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
npm74847622016-07-29 15:20:25 -070045#include "core/fxge/include/cfx_windowsdevice.h"
thestig25fa42f2016-05-25 21:39:46 -070046#endif
47
Tom Sepez50d12ad2015-11-24 09:50:51 -080048UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
49 return static_cast<UnderlyingDocumentType*>(doc);
50}
51
52FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
53 return static_cast<FPDF_DOCUMENT>(doc);
54}
55
56UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
57 return static_cast<UnderlyingPageType*>(page);
58}
59
Tom Sepez471a1032015-10-15 16:17:18 -070060CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080061#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080062 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080063#else // PDF_ENABLE_XFA
64 return UnderlyingFromFPDFDocument(doc);
65#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070066}
67
Tom Sepezbf59a072015-10-21 14:07:23 -070068FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080069#ifdef PDF_ENABLE_XFA
dsinclaircedaa552016-08-24 11:12:19 -070070 return doc ? FPDFDocumentFromUnderlying(new CPDFXFA_Document(
71 WrapUnique(doc), CPDFXFA_App::GetInstance()))
Tom Sepez50d12ad2015-11-24 09:50:51 -080072 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080073#else // PDF_ENABLE_XFA
74 return FPDFDocumentFromUnderlying(doc);
75#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070076}
77
Tom Sepezdb0be962015-10-16 14:00:21 -070078CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080079#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080080 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080081#else // PDF_ENABLE_XFA
82 return UnderlyingFromFPDFPage(page);
83#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070084}
85
thestigbefa4502016-05-26 20:15:19 -070086CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
87 return static_cast<CFX_DIBitmap*>(bitmap);
88}
89
Tom Sepez40e9ff32015-11-30 12:39:54 -080090#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
92 m_pFS = pFS;
93 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096IFX_FileStream* CFPDF_FileStream::Retain() {
97 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100void CFPDF_FileStream::Release() {
101 if (m_pFS && m_pFS->Release)
102 m_pFS->Release(m_pFS->clientData);
103 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -0700104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106FX_FILESIZE CFPDF_FileStream::GetSize() {
107 if (m_pFS && m_pFS->GetSize)
108 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
109 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700110}
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112FX_BOOL CFPDF_FileStream::IsEOF() {
113 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700114}
115
weili625ad662016-06-15 11:21:33 -0700116FX_FILESIZE CFPDF_FileStream::GetPosition() {
117 return m_nCurPos;
118}
119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
121 FX_FILESIZE offset,
122 size_t size) {
123 if (!buffer || !size || !m_pFS->ReadBlock)
124 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
127 (FPDF_DWORD)size) == 0) {
128 m_nCurPos = offset + size;
129 return TRUE;
130 }
131 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
135 if (!buffer || !size || !m_pFS->ReadBlock)
136 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 FX_FILESIZE nSize = GetSize();
139 if (m_nCurPos >= nSize)
140 return 0;
141 FX_FILESIZE dwAvail = nSize - m_nCurPos;
142 if (dwAvail < (FX_FILESIZE)size)
143 size = (size_t)dwAvail;
144 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
145 (FPDF_DWORD)size) == 0) {
146 m_nCurPos += size;
147 return size;
148 }
Bo Xufdc00a72014-10-28 23:03:33 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700151}
152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
154 FX_FILESIZE offset,
155 size_t size) {
156 if (!m_pFS || !m_pFS->WriteBlock)
157 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
160 (FPDF_DWORD)size) == 0) {
161 m_nCurPos = offset + size;
162 return TRUE;
163 }
164 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167FX_BOOL CFPDF_FileStream::Flush() {
168 if (!m_pFS || !m_pFS->Flush)
169 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700172}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800173#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
176 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800177#ifdef PDF_ENABLE_XFA
tsepezc3255f52016-03-25 14:52:27 -0700178 m_BufferOffset = (uint32_t)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800179#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700180}
181
Tom Sepez40e9ff32015-11-30 12:39:54 -0800182#ifdef PDF_ENABLE_XFA
weili625ad662016-06-15 11:21:33 -0700183CFX_ByteString CPDF_CustomAccess::GetFullPath() {
184 return "";
185}
186
tsepezc3255f52016-03-25 14:52:27 -0700187FX_BOOL CPDF_CustomAccess::GetByte(uint32_t pos, uint8_t& ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (pos >= m_FileAccess.m_FileLen)
189 return FALSE;
tsepezc3255f52016-03-25 14:52:27 -0700190 if (m_BufferOffset == (uint32_t)-1 || pos < m_BufferOffset ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 pos >= m_BufferOffset + 512) {
192 // Need to read from file access
193 m_BufferOffset = pos;
194 int size = 512;
195 if (pos + 512 > m_FileAccess.m_FileLen)
196 size = m_FileAccess.m_FileLen - pos;
197 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
198 size))
199 return FALSE;
200 }
201 ch = m_Buffer[pos - m_BufferOffset];
202 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700203}
204
tsepezc3255f52016-03-25 14:52:27 -0700205FX_BOOL CPDF_CustomAccess::GetBlock(uint32_t pos,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 uint8_t* pBuf,
tsepezc3255f52016-03-25 14:52:27 -0700207 uint32_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (pos + size > m_FileAccess.m_FileLen)
209 return FALSE;
210 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800212#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213
weili625ad662016-06-15 11:21:33 -0700214FX_FILESIZE CPDF_CustomAccess::GetSize() {
215 return m_FileAccess.m_FileLen;
216}
217
218void CPDF_CustomAccess::Release() {
219 delete this;
220}
221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
223 FX_FILESIZE offset,
224 size_t size) {
225 if (offset < 0) {
226 return FALSE;
227 }
228 FX_SAFE_FILESIZE newPos =
229 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
230 newPos += offset;
Wei Li05d53f02016-03-29 16:42:53 -0700231 if (!newPos.IsValid() ||
232 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 return FALSE;
234 }
235 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
236 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700240static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
243 switch (policy) {
244 case FPDF_POLICY_MACHINETIME_ACCESS: {
245 if (enable)
246 foxit_sandbox_policy |= 0x01;
247 else
248 foxit_sandbox_policy &= 0xFFFFFFFE;
249 } break;
250 default:
251 break;
252 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
256 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700257 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700258 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700260 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Tom Sepez2c286192015-06-18 12:47:11 -0700264CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700267 FPDF_InitLibraryWithConfig(nullptr);
268}
269
Dan Sinclairf766ad22016-03-14 13:51:24 -0400270DLLEXPORT void STDCALL
271FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
weili47228ac2016-07-20 10:35:31 -0700274 CFX_GEModule* pModule = CFX_GEModule::Get();
275 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
Tom Sepez1b246282015-11-25 15:15:31 -0800276 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
277 pModuleMgr->SetCodecModule(g_pCodecModule);
278 pModuleMgr->InitPageModule();
dsinclairab5b60e2016-06-23 08:18:36 -0700279 pModuleMgr->LoadEmbeddedGB1CMaps();
280 pModuleMgr->LoadEmbeddedJapan1CMaps();
281 pModuleMgr->LoadEmbeddedCNS1CMaps();
282 pModuleMgr->LoadEmbeddedKorea1CMaps();
dsinclaird647a6b2016-04-26 13:13:20 -0700283
Tom Sepez40e9ff32015-11-30 12:39:54 -0800284#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +0800285 CPDFXFA_App::GetInstance()->Initialize(
dsinclairec3da5b2016-05-25 16:42:05 -0700286 (cfg && cfg->version >= 2) ? static_cast<v8::Isolate*>(cfg->m_pIsolate)
287 : nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800288#endif // PDF_ENABLE_XFA
dsinclaird647a6b2016-04-26 13:13:20 -0700289
Tom Sepez452b4f32015-10-13 09:27:27 -0700290 if (cfg && cfg->version >= 2)
291 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292}
293
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800295#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800297#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 CPDF_ModuleMgr::Destroy();
299 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 delete g_pCodecModule;
302 g_pCodecModule = nullptr;
thestig2d6dda12016-06-28 07:39:09 -0700303
304 IJS_Runtime::Destroy();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305}
306
307#ifndef _WIN32
308int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309void SetLastError(int err) {
310 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313int GetLastError() {
314 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800316#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317
Tom Sepezf10ae632016-01-26 14:19:52 -0800318void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700319 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800321 switch (err) {
322 case CPDF_Parser::SUCCESS:
323 err_code = FPDF_ERR_SUCCESS;
324 break;
325 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 err_code = FPDF_ERR_FILE;
327 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800328 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 err_code = FPDF_ERR_FORMAT;
330 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800331 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 err_code = FPDF_ERR_PASSWORD;
333 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800334 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 err_code = FPDF_ERR_SECURITY;
336 break;
337 }
338 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339}
340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
342 FPDF_BOOL enable) {
343 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700344}
345
thestigfdb35ff2016-07-18 13:45:44 -0700346#if defined(_WIN32) && defined(PDFIUM_PRINT_TEXT_WITH_GDI)
347DLLEXPORT void STDCALL
348FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
349 g_pdfium_typeface_accessible_func = func;
350}
351
352DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
353 g_pdfium_print_text_with_gdi = !!use_gdi;
354}
355#endif
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
358 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700359 // NOTE: the creation of the file needs to be by the embedder on the
360 // other side of this API.
361 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
362 if (!pFileAccess) {
363 return nullptr;
364 }
365
tsepez4540fba2016-08-16 11:12:21 -0700366 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700368
dsinclaircedaa552016-08-24 11:12:19 -0700369 std::unique_ptr<CPDF_Document> pDocument(
370 new CPDF_Document(std::move(pParser)));
dsinclair156de022016-08-24 11:58:24 -0700371 CPDF_Parser::Error error =
372 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800373 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800374 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700375 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800377#ifdef PDF_ENABLE_XFA
dsinclaircedaa552016-08-24 11:12:19 -0700378 return new CPDFXFA_Document(std::move(pDocument), CPDFXFA_App::GetInstance());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800379#else // PDF_ENABLE_XFA
dsinclaircedaa552016-08-24 11:12:19 -0700380 return pDocument.release();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800381#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382}
Jun Fange118ce92015-02-17 06:50:08 -0800383
Tom Sepez40e9ff32015-11-30 12:39:54 -0800384#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
386 int* docType) {
387 if (!document)
388 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 CPDF_Document* pdfDoc =
391 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
392 if (!pdfDoc)
393 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
396 if (!pRoot)
397 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800398
dsinclair38fd8442016-09-15 10:15:32 -0700399 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 if (!pAcroForm)
401 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800402
dsinclair38fd8442016-09-15 10:15:32 -0700403 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 if (!pXFA)
405 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800406
dsinclair38fd8442016-09-15 10:15:32 -0700407 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700408 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800410}
411
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
413 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700414}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800415#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417class CMemFile final : public IFX_FileRead {
418 public:
419 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420
Lei Zhang3884dba2015-10-19 17:27:53 -0700421 void Release() override { delete this; }
422 FX_FILESIZE GetSize() override { return m_size; }
423 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 if (offset < 0) {
425 return FALSE;
426 }
427 FX_SAFE_FILESIZE newPos =
428 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
429 newPos += offset;
weili47ca6922016-03-31 15:08:27 -0700430 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 return FALSE;
432 }
433 FXSYS_memcpy(buffer, m_pBuf + offset, size);
434 return TRUE;
435 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700438 ~CMemFile() override {}
439
Lei Zhang3884dba2015-10-19 17:27:53 -0700440 uint8_t* const m_pBuf;
441 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442};
Lei Zhang3884dba2015-10-19 17:27:53 -0700443
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
445 int size,
446 FPDF_BYTESTRING password) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
tsepez4540fba2016-08-16 11:12:21 -0700448 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
449 pParser->SetPassword(password);
450
dsinclaircedaa552016-08-24 11:12:19 -0700451 std::unique_ptr<CPDF_Document> pDocument(
452 new CPDF_Document(std::move(pParser)));
tsepez4540fba2016-08-16 11:12:21 -0700453 CPDF_Parser::Error error =
dsinclaircedaa552016-08-24 11:12:19 -0700454 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800455 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800456 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700457 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 }
dsinclaircedaa552016-08-24 11:12:19 -0700459 CheckUnSupportError(pDocument.get(), error);
460 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461}
462
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463DLLEXPORT FPDF_DOCUMENT STDCALL
464FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
465 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700466 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
tsepez4540fba2016-08-16 11:12:21 -0700467 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
468 pParser->SetPassword(password);
469
dsinclaircedaa552016-08-24 11:12:19 -0700470 std::unique_ptr<CPDF_Document> pDocument(
471 new CPDF_Document(std::move(pParser)));
472 CPDF_Parser::Error error =
473 pDocument->GetParser()->StartParse(pFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800474 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800475 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700476 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 }
dsinclaircedaa552016-08-24 11:12:19 -0700478 CheckUnSupportError(pDocument.get(), error);
479 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480}
481
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
483 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700484 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700486
Tom Sepez471a1032015-10-15 16:17:18 -0700487 *fileVersion = 0;
488 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
489 if (!pDoc)
490 return FALSE;
491
492 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 if (!pParser)
494 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700495
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 *fileVersion = pParser->GetFileVersion();
497 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498}
499
tsepezc3255f52016-03-25 14:52:27 -0700500// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501// header).
502DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700503 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700504 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
505 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800506#ifndef PDF_ENABLE_XFA
507 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800508#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700509 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800510#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700511 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512
thestig27ddf162016-05-23 15:06:59 -0700513 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700514}
515
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700517 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700518 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700520
Tom Sepez471a1032015-10-15 16:17:18 -0700521 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700522 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700523}
524
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800526 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700527 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700528}
529
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
531 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800532 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
533 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700534 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800535
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700536 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700537 return nullptr;
538
Tom Sepez40e9ff32015-11-30 12:39:54 -0800539#ifdef PDF_ENABLE_XFA
540 return pDoc->GetPage(page_index);
541#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800542 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800543 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700544 return nullptr;
545
546 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
547 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800548 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800549#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550}
551
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800553 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700554 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555}
556
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800558 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700559 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700560}
561
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700562#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
564 FPDF_PAGE page,
565 int start_x,
566 int start_y,
567 int size_x,
568 int size_y,
569 int rotate,
570 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700571 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 if (!pPage)
573 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700574
weili9f515bc2016-07-24 08:08:24 -0700575 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
576 pPage->SetRenderContext(WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700577
Jun Fang1aeeceb2015-12-29 10:27:44 +0800578 CFX_DIBitmap* pBitmap = nullptr;
579 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
580 FX_BOOL bHasImageMask = pPage->HasImageMask();
581 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700582 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 pBitmap->Create(size_x, size_y, FXDIB_Argb);
584 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700585 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700586 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700587 pDevice->Attach(pBitmap, false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800588 } else {
weili9f515bc2016-07-24 08:08:24 -0700589 pContext->m_pDevice.reset(new CFX_WindowsDevice(dc));
Jun Fang1aeeceb2015-12-29 10:27:44 +0800590 }
Bo Xud4e406e2014-08-13 11:03:19 -0700591
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
thestigbefa4502016-05-26 20:15:19 -0700593 rotate, flags, TRUE, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700594
Jun Fang1aeeceb2015-12-29 10:27:44 +0800595 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 if (pBitmap) {
597 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700598
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700600 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 int pitch = pBitmap->GetPitch();
602 pDst->Create(size_x, size_y, FXDIB_Rgb32);
603 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
604 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
thestig1cd352e2016-06-07 17:53:06 -0700605 FXDIB_BLEND_NORMAL, nullptr, FALSE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
607 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800608 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800610 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700611 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800613 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800615
weili9f515bc2016-07-24 08:08:24 -0700616 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617}
Lei Zhangae85f872016-02-19 14:57:07 -0800618#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619
620DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
621 FPDF_PAGE page,
622 int start_x,
623 int start_y,
624 int size_x,
625 int size_y,
626 int rotate,
627 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700628 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 return;
tsepez1e2c5572016-05-25 14:58:09 -0700630
Tom Sepezdb0be962015-10-16 14:00:21 -0700631 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 if (!pPage)
633 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634
weili9f515bc2016-07-24 08:08:24 -0700635 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
636 pPage->SetRenderContext(WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -0700637 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700638 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700639 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
640 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641
642 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez1e2c5572016-05-25 14:58:09 -0700643 rotate, flags, TRUE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644
weili9f515bc2016-07-24 08:08:24 -0700645 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700646}
647
Cary Clark399be5b2016-03-14 16:51:29 -0400648#ifdef _SKIA_SUPPORT_
649DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
650 int size_x,
651 int size_y) {
652 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
653 if (!pPage)
654 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700655
weili9f515bc2016-07-24 08:08:24 -0700656 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
657 pPage->SetRenderContext(WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700658 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400659 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700660 pContext->m_pDevice.reset(skDevice);
thestig4d1311b2016-05-25 18:31:25 -0700661 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, TRUE,
tsepez1e2c5572016-05-25 14:58:09 -0700662 nullptr);
weili9f515bc2016-07-24 08:08:24 -0700663 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
Cary Clark399be5b2016-03-14 16:51:29 -0400664 return recorder;
665}
666#endif
667
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700669 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 if (!page)
671 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800672#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800673 pPage->Release();
674#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800675 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700676 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -0700677 if (pPageView) {
678 if (pPageView->IsLocked()) {
679 pPageView->TakePageOwnership();
680 return;
681 }
682
683 bool owned = pPageView->OwnsPage();
684 // This will delete the |pPageView| object. We must cleanup the PageView
685 // first because it will attempt to reset the View on the |pPage| during
686 // destruction.
687 pPageView->GetSDKDocument()->RemovePageView(pPage);
688 // If the page was owned then the pageview will have deleted the page.
689 if (owned)
690 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800691 }
tsepez1e2c5572016-05-25 14:58:09 -0700692 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800693#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700694}
695
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -0800697 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700698}
699
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
701 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700702}
703
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
705 int start_x,
706 int start_y,
707 int size_x,
708 int size_y,
709 int rotate,
710 int device_x,
711 int device_y,
712 double* page_x,
713 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800714 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800716 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800717#ifdef PDF_ENABLE_XFA
718 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
719 device_y, page_x, page_y);
720#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800721 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800722 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
723 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800724 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800725 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800726 FX_FLOAT page_x_f, page_y_f;
727 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
728 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800729 *page_x = (page_x_f);
730 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800731#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700732}
733
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
735 int start_x,
736 int start_y,
737 int size_x,
738 int size_y,
739 int rotate,
740 double page_x,
741 double page_y,
742 int* device_x,
743 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700744 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800746 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700747 if (!pPage)
748 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800749#ifdef PDF_ENABLE_XFA
750 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
751 device_x, device_y);
752#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800753 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800754 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
755 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800756 FX_FLOAT device_x_f, device_y_f;
757 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
758 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800759 *device_x = FXSYS_round(device_x_f);
760 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800761#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700762}
763
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
765 int height,
766 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800767 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
thestig1cd352e2016-06-07 17:53:06 -0700769 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 }
771 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700772}
773
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
775 int height,
776 int format,
777 void* first_scan,
778 int stride) {
779 FXDIB_Format fx_format;
780 switch (format) {
781 case FPDFBitmap_Gray:
782 fx_format = FXDIB_8bppRgb;
783 break;
784 case FPDFBitmap_BGR:
785 fx_format = FXDIB_Rgb;
786 break;
787 case FPDFBitmap_BGRx:
788 fx_format = FXDIB_Rgb32;
789 break;
790 case FPDFBitmap_BGRA:
791 fx_format = FXDIB_Argb;
792 break;
793 default:
thestig1cd352e2016-06-07 17:53:06 -0700794 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 }
796 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
797 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
798 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700799}
800
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
802 int left,
803 int top,
804 int width,
805 int height,
806 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800807 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808 return;
thestigbefa4502016-05-26 20:15:19 -0700809
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700810 CFX_FxgeDevice device;
thestigbefa4502016-05-26 20:15:19 -0700811 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
812 device.Attach(pBitmap, false, nullptr, false);
813 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700814 color |= 0xFF000000;
815 FX_RECT rect(left, top, left + width, top + height);
816 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700817}
818
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700820 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700821}
822
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700824 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700825}
826
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700828 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829}
830
831DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700832 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833}
834
835DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700836 delete CFXBitmapFromFPDFBitmap(bitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837}
838
weili9f515bc2016-07-24 08:08:24 -0700839void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 FPDF_PAGE page,
841 int start_x,
842 int start_y,
843 int size_x,
844 int size_y,
845 int rotate,
846 int flags,
847 FX_BOOL bNeedToRestore,
848 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700849 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
850 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 return;
852
853 if (!pContext->m_pOptions)
weili9f515bc2016-07-24 08:08:24 -0700854 pContext->m_pOptions.reset(new CPDF_RenderOptions);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855
856 if (flags & FPDF_LCD_TEXT)
857 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
858 else
859 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
860 if (flags & FPDF_NO_NATIVETEXT)
861 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
862 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
863 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
864 if (flags & FPDF_RENDER_FORCEHALFTONE)
865 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800866#ifndef PDF_ENABLE_XFA
867 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
868 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
869 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
870 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
871 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
872 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800873#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 // Grayscale output
875 if (flags & FPDF_GRAYSCALE) {
876 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
877 pContext->m_pOptions->m_ForeColor = 0;
878 pContext->m_pOptions->m_BackColor = 0xffffff;
879 }
880 const CPDF_OCContext::UsageType usage =
881 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
882 pContext->m_pOptions->m_AddFlags = flags >> 8;
883 pContext->m_pOptions->m_pOCContext =
884 new CPDF_OCContext(pPage->m_pDocument, usage);
885
Tom Sepez60d909e2015-12-10 15:34:55 -0800886 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
888
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 pContext->m_pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800890 pContext->m_pDevice->SetClip_Rect(
891 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892
weili9f515bc2016-07-24 08:08:24 -0700893 pContext->m_pContext.reset(new CPDF_RenderContext(pPage));
Tom Sepez71fdc342016-01-22 12:06:32 -0800894 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895
896 if (flags & FPDF_ANNOT) {
weili9f515bc2016-07-24 08:08:24 -0700897 pContext->m_pAnnots.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
weili9f515bc2016-07-24 08:08:24 -0700899 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
jaepark75f84a52016-09-09 15:39:09 -0700900 bPrinting, &matrix, FALSE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 }
902
weili9f515bc2016-07-24 08:08:24 -0700903 pContext->m_pRenderer.reset(new CPDF_ProgressiveRenderer(
904 pContext->m_pContext.get(), pContext->m_pDevice.get(),
905 pContext->m_pOptions.get()));
Tom Sepezb3b67622015-10-19 16:20:03 -0700906 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800907 if (bNeedToRestore)
thestig41846a02016-05-26 10:45:30 -0700908 pContext->m_pDevice->RestoreState(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909}
910
911DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
912 int page_index,
913 double* width,
914 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800915 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
916 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 return FALSE;
918
Tom Sepez40e9ff32015-11-30 12:39:54 -0800919#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920 int count = pDoc->GetPageCount();
921 if (page_index < 0 || page_index >= count)
922 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
924 if (!pPage)
925 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800926 *width = pPage->GetPageWidth();
927 *height = pPage->GetPageHeight();
928#else // PDF_ENABLE_XFA
929 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
930 if (!pDict)
931 return FALSE;
thestig5cc24652016-04-26 11:46:02 -0700932
933 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -0800934 *width = page.GetPageWidth();
935 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800936#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937
938 return TRUE;
939}
940
941DLLEXPORT FPDF_BOOL STDCALL
942FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700943 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (!pDoc)
945 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700946 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 return viewRef.PrintScaling();
948}
949
950DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700951 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 if (!pDoc)
953 return 1;
954 CPDF_ViewerPreferences viewRef(pDoc);
955 return viewRef.NumCopies();
956}
957
958DLLEXPORT FPDF_PAGERANGE STDCALL
959FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700960 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700962 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 CPDF_ViewerPreferences viewRef(pDoc);
964 return viewRef.PrintPageRange();
965}
966
967DLLEXPORT FPDF_DUPLEXTYPE STDCALL
968FPDF_VIEWERREF_GetDuplex(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)
Bo Xu9114e832014-07-14 13:22:47 -0700971 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 CPDF_ViewerPreferences viewRef(pDoc);
973 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -0800974 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -0800976 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -0800978 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 return DuplexFlipLongEdge;
980 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -0700981}
982
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700984 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
985 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800987
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700988 CPDF_Dictionary* pRoot = pDoc->GetRoot();
989 if (!pRoot)
990 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800991
Lei Zhangd983b092015-12-14 16:58:33 -0800992 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800993 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -0700994 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 if (pDest)
996 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800997
998 if (!count.IsValid())
999 return 0;
1000
1001 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002}
1003
1004DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1005 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001007 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008
Tom Sepez471a1032015-10-15 16:17:18 -07001009 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1010 if (!pDoc)
1011 return nullptr;
1012
Lei Zhangd983b092015-12-14 16:58:33 -08001013 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001014 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015}
1016
Tom Sepez51da0932015-11-25 16:05:49 -08001017#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001018DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 if (!str)
1020 return -1;
1021
1022 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1023 return 0;
1024}
1025
thestig77d148d2016-04-06 06:28:31 -07001026DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1027 FPDF_LPCSTR bstr,
1028 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 if (!str)
1030 return -1;
1031 if (!bstr || !length)
1032 return -1;
1033 if (length == -1)
1034 length = FXSYS_strlen(bstr);
1035
1036 if (length == 0) {
1037 if (str->str) {
1038 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001039 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 }
1041 str->len = 0;
1042 return 0;
1043 }
1044
1045 if (str->str && str->len < length)
1046 str->str = FX_Realloc(char, str->str, length + 1);
1047 else if (!str->str)
1048 str->str = FX_Alloc(char, length + 1);
1049
1050 str->str[length] = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 FXSYS_memcpy(str->str, bstr, length);
1052 str->len = length;
1053
1054 return 0;
1055}
1056
thestig77d148d2016-04-06 06:28:31 -07001057DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 if (!str)
1059 return -1;
1060
1061 if (str->str) {
1062 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001063 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 }
1065 str->len = 0;
1066 return 0;
1067}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001068#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069
1070DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1071 int index,
1072 void* buffer,
1073 long* buflen) {
1074 if (!buffer)
1075 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001076
1077 if (index < 0)
1078 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079
Tom Sepezbf59a072015-10-21 14:07:23 -07001080 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001081 if (!pDoc)
1082 return nullptr;
1083
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1085 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001086 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001088 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001090 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 int count = nameTree.GetCount();
1092 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001093 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001095 return nullptr;
1096
1097 pdfium::base::CheckedNumeric<int> checked_count = count;
1098 checked_count += pDest->GetCount();
1099 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1100 return nullptr;
1101
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001104 for (const auto& it : *pDest) {
1105 bsName = it.first;
1106 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 if (!pDestObj)
1108 continue;
1109 if (i == index)
1110 break;
1111 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001112 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001113 } else {
1114 pDestObj = nameTree.LookupValue(index, bsName);
1115 }
1116 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001117 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001118 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001119 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001121 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001123 if (!pDestObj->IsArray())
1124 return nullptr;
1125
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 CFX_WideString wsName = PDF_DecodeText(bsName);
1127 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001128 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001129 if (!buffer) {
1130 *buflen = len;
1131 } else if (*buflen >= len) {
1132 memcpy(buffer, utf16Name.c_str(), len);
1133 *buflen = len;
1134 } else {
1135 *buflen = -1;
1136 }
1137 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001138}