blob: dec1059e1ab3aa04bce4e424b62c08079d28305b [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"
dsinclair86e54742016-04-06 12:30:31 -070016#include "core/fxcodec/include/fx_codec.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040017#include "core/fxcrt/include/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"
dsinclair64376be2016-03-31 20:03:24 -070021#include "fpdfsdk/javascript/ijs_runtime.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
dsinclair89bdd082016-04-06 10:47:54 -070027#include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
28#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
29#include "fpdfsdk/fpdfxfa/include/fpdfxfa_page.h"
30#include "fpdfsdk/fpdfxfa/include/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
tsepezc3255f52016-03-25 14:52:27 -0700156 m_BufferOffset = (uint32_t)-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
tsepezc3255f52016-03-25 14:52:27 -0700161FX_BOOL CPDF_CustomAccess::GetByte(uint32_t pos, uint8_t& ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 if (pos >= m_FileAccess.m_FileLen)
163 return FALSE;
tsepezc3255f52016-03-25 14:52:27 -0700164 if (m_BufferOffset == (uint32_t)-1 || pos < m_BufferOffset ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 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
tsepezc3255f52016-03-25 14:52:27 -0700179FX_BOOL CPDF_CustomAccess::GetBlock(uint32_t pos,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 uint8_t* pBuf,
tsepezc3255f52016-03-25 14:52:27 -0700181 uint32_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 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;
Wei Li05d53f02016-03-29 16:42:53 -0700197 if (!newPos.IsValid() ||
198 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return FALSE;
200 }
201 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
202 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700206static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
209 switch (policy) {
210 case FPDF_POLICY_MACHINETIME_ACCESS: {
211 if (enable)
212 foxit_sandbox_policy |= 0x01;
213 else
214 foxit_sandbox_policy &= 0xFFFFFFFE;
215 } break;
216 default:
217 break;
218 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219}
220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
222 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700223 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700224 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700226 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228}
229
Tom Sepez2c286192015-06-18 12:47:11 -0700230CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700233 FPDF_InitLibraryWithConfig(nullptr);
234}
235
Dan Sinclairf766ad22016-03-14 13:51:24 -0400236DLLEXPORT void STDCALL
237FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239
Lei Zhang6f62d532015-09-23 15:31:44 -0700240 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 CPDF_ModuleMgr::Create();
Tom Sepez1b246282015-11-25 15:15:31 -0800244 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
245 pModuleMgr->SetCodecModule(g_pCodecModule);
246 pModuleMgr->InitPageModule();
247 pModuleMgr->InitRenderModule();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800248#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +0800249 CPDFXFA_App::GetInstance()->Initialize(
250 (cfg && cfg->version >= 2)
251 ? reinterpret_cast<FXJSE_HRUNTIME>(cfg->m_pIsolate)
252 : nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800253#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800254 pModuleMgr->LoadEmbeddedGB1CMaps();
255 pModuleMgr->LoadEmbeddedJapan1CMaps();
256 pModuleMgr->LoadEmbeddedCNS1CMaps();
257 pModuleMgr->LoadEmbeddedKorea1CMaps();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800258#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700259 if (cfg && cfg->version >= 2)
260 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261}
262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800264#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800266#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 CPDF_ModuleMgr::Destroy();
268 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 delete g_pCodecModule;
271 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
274#ifndef _WIN32
275int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276void SetLastError(int err) {
277 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280int GetLastError() {
281 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800283#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284
Tom Sepezf10ae632016-01-26 14:19:52 -0800285void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700286 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800288 switch (err) {
289 case CPDF_Parser::SUCCESS:
290 err_code = FPDF_ERR_SUCCESS;
291 break;
292 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 err_code = FPDF_ERR_FILE;
294 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800295 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 err_code = FPDF_ERR_FORMAT;
297 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800298 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 err_code = FPDF_ERR_PASSWORD;
300 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800301 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 err_code = FPDF_ERR_SECURITY;
303 break;
304 }
305 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306}
307
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
309 FPDF_BOOL enable) {
310 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
314 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700315 // NOTE: the creation of the file needs to be by the embedder on the
316 // other side of this API.
317 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
318 if (!pFileAccess) {
319 return nullptr;
320 }
321
322 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700324
Tom Sepezf10ae632016-01-26 14:19:52 -0800325 CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
326 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800328 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 return NULL;
330 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800331#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 CPDF_Document* pPDFDoc = pParser->GetDocument();
333 if (!pPDFDoc)
334 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700337 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800338#else // PDF_ENABLE_XFA
339 return pParser->GetDocument();
340#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341}
Jun Fange118ce92015-02-17 06:50:08 -0800342
Tom Sepez40e9ff32015-11-30 12:39:54 -0800343#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
345 int* docType) {
346 if (!document)
347 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 CPDF_Document* pdfDoc =
350 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
351 if (!pdfDoc)
352 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
355 if (!pRoot)
356 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800357
Wei Li9b761132016-01-29 15:44:20 -0800358 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 if (!pAcroForm)
360 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800361
tsepezbd567552016-03-29 14:51:50 -0700362 CPDF_Object* pXFA = pAcroForm->GetObjectBy("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 if (!pXFA)
364 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800365
Wei Li9b761132016-01-29 15:44:20 -0800366 FX_BOOL bDynamicXFA = pRoot->GetBooleanBy("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 if (bDynamicXFA)
Tom Sepezd3116dc2015-11-24 15:58:06 -0800369 *docType = DOCTYPE_DYNAMIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 else
371 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800374}
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
377 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700378}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800379#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381class CMemFile final : public IFX_FileRead {
382 public:
383 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384
Lei Zhang3884dba2015-10-19 17:27:53 -0700385 void Release() override { delete this; }
386 FX_FILESIZE GetSize() override { return m_size; }
387 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 if (offset < 0) {
389 return FALSE;
390 }
391 FX_SAFE_FILESIZE newPos =
392 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
393 newPos += offset;
weili47ca6922016-03-31 15:08:27 -0700394 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 return FALSE;
396 }
397 FXSYS_memcpy(buffer, m_pBuf + offset, size);
398 return TRUE;
399 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700402 ~CMemFile() override {}
403
Lei Zhang3884dba2015-10-19 17:27:53 -0700404 uint8_t* const m_pBuf;
405 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406};
Lei Zhang3884dba2015-10-19 17:27:53 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
409 int size,
410 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700411 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 pParser->SetPassword(password);
413 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
Tom Sepezf10ae632016-01-26 14:19:52 -0800414 CPDF_Parser::Error error = pParser->StartParse(pMemFile);
415 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800417 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 return NULL;
419 }
420 CPDF_Document* pDoc = NULL;
421 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800422 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700423 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424}
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426DLLEXPORT FPDF_DOCUMENT STDCALL
427FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
428 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700429 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700431 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Tom Sepezf10ae632016-01-26 14:19:52 -0800432 CPDF_Parser::Error error = pParser->StartParse(pFile);
433 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800435 ProcessParseError(error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 return NULL;
437 }
438 CPDF_Document* pDoc = NULL;
439 pDoc = pParser ? pParser->GetDocument() : NULL;
Tom Sepezf10ae632016-01-26 14:19:52 -0800440 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700441 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442}
443
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
445 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700446 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700448
Tom Sepez471a1032015-10-15 16:17:18 -0700449 *fileVersion = 0;
450 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
451 if (!pDoc)
452 return FALSE;
453
454 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 if (!pParser)
456 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700457
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 *fileVersion = pParser->GetFileVersion();
459 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460}
461
tsepezc3255f52016-03-25 14:52:27 -0700462// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463// header).
464DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700465 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700466 if (!pDoc || !pDoc->GetParser())
Tom Sepez51da0932015-11-25 16:05:49 -0800467#ifndef PDF_ENABLE_XFA
468 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800469#else // PDF_ENABLE_XFA
tsepezc3255f52016-03-25 14:52:27 -0700470 return (uint32_t)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800471#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472
Tom Sepez471a1032015-10-15 16:17:18 -0700473 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
tsepezc3255f52016-03-25 14:52:27 -0700474 return pDict ? pDict->GetIntegerBy("P") : (uint32_t)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475}
476
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700478 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700479 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700481
Tom Sepez471a1032015-10-15 16:17:18 -0700482 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800483 return pDict ? pDict->GetIntegerBy("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700484}
485
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800487 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700488 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489}
490
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
492 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800493 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
494 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700495 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800496
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700497 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700498 return nullptr;
499
Tom Sepez40e9ff32015-11-30 12:39:54 -0800500#ifdef PDF_ENABLE_XFA
501 return pDoc->GetPage(page_index);
502#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800503 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800504 if (!pDict)
Tom Sepez51da0932015-11-25 16:05:49 -0800505 return NULL;
506 CPDF_Page* pPage = new CPDF_Page;
507 pPage->Load(pDoc, pDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -0800508 pPage->ParseContent(nullptr);
Tom Sepez51da0932015-11-25 16:05:49 -0800509 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800510#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800514 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700515 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700516}
517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800519 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700520 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700521}
522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523void DropContext(void* data) {
524 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525}
526
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700527#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
529 FPDF_PAGE page,
530 int start_x,
531 int start_y,
532 int size_x,
533 int size_y,
534 int rotate,
535 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700536 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 if (!pPage)
538 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700539
Tom Sepezae51c812015-08-05 12:34:06 -0700540 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542
Lei Zhangae85f872016-02-19 14:57:07 -0800543#if !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800544 CFX_DIBitmap* pBitmap = nullptr;
545 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
546 FX_BOOL bHasImageMask = pPage->HasImageMask();
547 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700548 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 pBitmap->Create(size_x, size_y, FXDIB_Argb);
550 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700552 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554#else
Tom Sepezae51c812015-08-05 12:34:06 -0700555 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700557#endif
Jun Fang1aeeceb2015-12-29 10:27:44 +0800558 } else {
Tom Sepezae51c812015-08-05 12:34:06 -0700559 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800560 }
Bo Xud4e406e2014-08-13 11:03:19 -0700561
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
563 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700564
Jun Fang1aeeceb2015-12-29 10:27:44 +0800565 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 if (pBitmap) {
567 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700568
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700570 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 int pitch = pBitmap->GetPitch();
572 pDst->Create(size_x, size_y, FXDIB_Rgb32);
573 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
574 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
575 FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
576 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
577 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800578 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800580 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700581 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700583#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 // get clip region
585 RECT rect, cliprect;
586 rect.left = start_x;
587 rect.top = start_y;
588 rect.right = start_x + size_x;
589 rect.bottom = start_y + size_y;
590 GetClipBox(dc, &cliprect);
591 IntersectRect(&rect, &rect, &cliprect);
592 int width = rect.right - rect.left;
593 int height = rect.bottom - rect.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595 // Create a DIB section
596 LPVOID pBuffer;
597 BITMAPINFOHEADER bmih;
598 FXSYS_memset(&bmih, 0, sizeof bmih);
599 bmih.biSize = sizeof bmih;
600 bmih.biBitCount = 24;
601 bmih.biHeight = -height;
602 bmih.biPlanes = 1;
603 bmih.biWidth = width;
604 pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
605 &pBuffer, NULL, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
607
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 // Create a device with this external buffer
609 pContext->m_pBitmap = new CFX_DIBitmap;
610 pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
611 pContext->m_pDevice = new CPDF_FxgeDevice;
612 ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
613
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 // output to bitmap device
615 FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
616 start_y - rect.top, size_x, size_y, rotate, flags);
617
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 // Now output to real device
619 HDC hMemDC = CreateCompatibleDC(dc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
621
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
623 SelectObject(hMemDC, hOldBitmap);
624 DeleteDC(hMemDC);
625
Lei Zhangae85f872016-02-19 14:57:07 -0800626#endif // !defined(_WIN32_WCE)
Jun Fang1aeeceb2015-12-29 10:27:44 +0800627 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800629
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 delete pContext;
631 pPage->RemovePrivateData((void*)1);
632}
Lei Zhangae85f872016-02-19 14:57:07 -0800633#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634
635DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
636 FPDF_PAGE page,
637 int start_x,
638 int start_y,
639 int size_x,
640 int size_y,
641 int rotate,
642 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700643 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700645 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 if (!pPage)
647 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700648 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 pPage->SetPrivateData((void*)1, pContext, DropContext);
650#ifdef _SKIA_SUPPORT_
Cary Clark399be5b2016-03-14 16:51:29 -0400651 pContext->m_pDevice = new CFX_SkiaDevice();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652
653 if (flags & FPDF_REVERSE_BYTE_ORDER)
654 ((CFX_SkiaDevice*)pContext->m_pDevice)
655 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
656 else
657 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
658#else
Tom Sepezae51c812015-08-05 12:34:06 -0700659 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660
661 if (flags & FPDF_REVERSE_BYTE_ORDER)
662 ((CFX_FxgeDevice*)pContext->m_pDevice)
663 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
664 else
665 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
666#endif
667
668 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
669 rotate, flags, TRUE, NULL);
670
671 delete pContext;
672 pPage->RemovePrivateData((void*)1);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Cary Clark399be5b2016-03-14 16:51:29 -0400675#ifdef _SKIA_SUPPORT_
676DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
677 int size_x,
678 int size_y) {
679 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
680 if (!pPage)
681 return nullptr;
682 std::unique_ptr<CRenderContext> pContext(new CRenderContext);
683 pPage->SetPrivateData((void*)1, pContext.get(), DropContext);
684 CFX_SkiaDevice* skDevice = new CFX_SkiaDevice();
685 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
686 pContext->m_pDevice = skDevice;
687
688 FPDF_RenderPage_Retail(pContext.get(), page, 0, 0, size_x, size_y, 0, 0, TRUE,
689 NULL);
690 pPage->RemovePrivateData((void*)1);
691 return recorder;
692}
693#endif
694
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
696 if (!page)
697 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800698#ifdef PDF_ENABLE_XFA
699 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
700 pPage->Release();
701#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800702 CPDFSDK_PageView* pPageView =
703 (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
704 if (pPageView && pPageView->IsLocked()) {
705 pPageView->TakeOverPage();
706 return;
707 }
708 delete (CPDF_Page*)page;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800709#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700710}
711
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800713#ifdef PDF_ENABLE_XFA
Jun Fangfc751362015-12-16 21:23:39 -0800714 delete UnderlyingFromFPDFDocument(document);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800715#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800716 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
717 if (!pDoc)
718 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800719 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800720 if (!pParser) {
721 delete pDoc;
722 return;
723 }
724 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800725#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700726}
727
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
729 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700730}
731
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
733 int start_x,
734 int start_y,
735 int size_x,
736 int size_y,
737 int rotate,
738 int device_x,
739 int device_y,
740 double* page_x,
741 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800742 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800744 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800745#ifdef PDF_ENABLE_XFA
746 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
747 device_y, page_x, page_y);
748#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800749 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800750 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
751 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800752 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800753 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800754 FX_FLOAT page_x_f, page_y_f;
755 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
756 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800757 *page_x = (page_x_f);
758 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800759#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700760}
761
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
763 int start_x,
764 int start_y,
765 int size_x,
766 int size_y,
767 int rotate,
768 double page_x,
769 double page_y,
770 int* device_x,
771 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700772 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800774 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700775 if (!pPage)
776 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800777#ifdef PDF_ENABLE_XFA
778 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
779 device_x, device_y);
780#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800781 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800782 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
783 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800784 FX_FLOAT device_x_f, device_y_f;
785 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
786 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800787 *device_x = FXSYS_round(device_x_f);
788 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800789#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700790}
791
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
793 int height,
794 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800795 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
797 return NULL;
798 }
799 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700800}
801
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
803 int height,
804 int format,
805 void* first_scan,
806 int stride) {
807 FXDIB_Format fx_format;
808 switch (format) {
809 case FPDFBitmap_Gray:
810 fx_format = FXDIB_8bppRgb;
811 break;
812 case FPDFBitmap_BGR:
813 fx_format = FXDIB_Rgb;
814 break;
815 case FPDFBitmap_BGRx:
816 fx_format = FXDIB_Rgb32;
817 break;
818 case FPDFBitmap_BGRA:
819 fx_format = FXDIB_Argb;
820 break;
821 default:
822 return NULL;
823 }
824 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
825 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
826 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700827}
828
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
830 int left,
831 int top,
832 int width,
833 int height,
834 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800835 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 return;
837#ifdef _SKIA_SUPPORT_
838 CFX_SkiaDevice device;
839#else
840 CFX_FxgeDevice device;
841#endif
842 device.Attach((CFX_DIBitmap*)bitmap);
843 if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
844 color |= 0xFF000000;
845 FX_RECT rect(left, top, left + width, top + height);
846 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700847}
848
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800850 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700851}
852
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800854 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700855}
856
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800858 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859}
860
861DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800862 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863}
864
865DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
866 delete (CFX_DIBitmap*)bitmap;
867}
868
869void FPDF_RenderPage_Retail(CRenderContext* pContext,
870 FPDF_PAGE page,
871 int start_x,
872 int start_y,
873 int size_x,
874 int size_y,
875 int rotate,
876 int flags,
877 FX_BOOL bNeedToRestore,
878 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700879 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
880 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 return;
882
883 if (!pContext->m_pOptions)
884 pContext->m_pOptions = new CPDF_RenderOptions;
885
886 if (flags & FPDF_LCD_TEXT)
887 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
888 else
889 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
890 if (flags & FPDF_NO_NATIVETEXT)
891 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
892 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
893 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
894 if (flags & FPDF_RENDER_FORCEHALFTONE)
895 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800896#ifndef PDF_ENABLE_XFA
897 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
898 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
899 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
900 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
901 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
902 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800903#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904 // Grayscale output
905 if (flags & FPDF_GRAYSCALE) {
906 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
907 pContext->m_pOptions->m_ForeColor = 0;
908 pContext->m_pOptions->m_BackColor = 0xffffff;
909 }
910 const CPDF_OCContext::UsageType usage =
911 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
912 pContext->m_pOptions->m_AddFlags = flags >> 8;
913 pContext->m_pOptions->m_pOCContext =
914 new CPDF_OCContext(pPage->m_pDocument, usage);
915
Tom Sepez60d909e2015-12-10 15:34:55 -0800916 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
918
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919 pContext->m_pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800920 pContext->m_pDevice->SetClip_Rect(
921 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922
Tom Sepez979ddd82015-12-17 13:41:13 -0800923 pContext->m_pContext = new CPDF_RenderContext(pPage);
Tom Sepez71fdc342016-01-22 12:06:32 -0800924 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925
926 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700927 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
929 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
930 &matrix, TRUE, NULL);
931 }
932
Tom Sepezb3b67622015-10-19 16:20:03 -0700933 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
934 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
935 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800936 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938}
939
940DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
941 int page_index,
942 double* width,
943 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800944 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
945 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 return FALSE;
947
Tom Sepez40e9ff32015-11-30 12:39:54 -0800948#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 int count = pDoc->GetPageCount();
950 if (page_index < 0 || page_index >= count)
951 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
953 if (!pPage)
954 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800955 *width = pPage->GetPageWidth();
956 *height = pPage->GetPageHeight();
957#else // PDF_ENABLE_XFA
958 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
959 if (!pDict)
960 return FALSE;
Tom Sepez51da0932015-11-25 16:05:49 -0800961 CPDF_Page page;
962 page.Load(pDoc, pDict);
963 *width = page.GetPageWidth();
964 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800965#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966
967 return TRUE;
968}
969
970DLLEXPORT FPDF_BOOL STDCALL
971FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700972 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 if (!pDoc)
974 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700975 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 return viewRef.PrintScaling();
977}
978
979DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700980 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 if (!pDoc)
982 return 1;
983 CPDF_ViewerPreferences viewRef(pDoc);
984 return viewRef.NumCopies();
985}
986
987DLLEXPORT FPDF_PAGERANGE STDCALL
988FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700989 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 if (!pDoc)
991 return NULL;
992 CPDF_ViewerPreferences viewRef(pDoc);
993 return viewRef.PrintPageRange();
994}
995
996DLLEXPORT FPDF_DUPLEXTYPE STDCALL
997FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700998 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001000 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 CPDF_ViewerPreferences viewRef(pDoc);
1002 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001003 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001005 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001007 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 return DuplexFlipLongEdge;
1009 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001010}
1011
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001013 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1014 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001016
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1018 if (!pRoot)
1019 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001020
Lei Zhangd983b092015-12-14 16:58:33 -08001021 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001022 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
Wei Li9b761132016-01-29 15:44:20 -08001023 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (pDest)
1025 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001026
1027 if (!count.IsValid())
1028 return 0;
1029
1030 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031}
1032
1033DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1034 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001036 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037
Tom Sepez471a1032015-10-15 16:17:18 -07001038 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1039 if (!pDoc)
1040 return nullptr;
1041
Lei Zhangd983b092015-12-14 16:58:33 -08001042 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001043 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044}
1045
Tom Sepez51da0932015-11-25 16:05:49 -08001046#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001047DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048 if (!str)
1049 return -1;
1050
1051 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1052 return 0;
1053}
1054
thestig77d148d2016-04-06 06:28:31 -07001055DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1056 FPDF_LPCSTR bstr,
1057 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 if (!str)
1059 return -1;
1060 if (!bstr || !length)
1061 return -1;
1062 if (length == -1)
1063 length = FXSYS_strlen(bstr);
1064
1065 if (length == 0) {
1066 if (str->str) {
1067 FX_Free(str->str);
1068 str->str = NULL;
1069 }
1070 str->len = 0;
1071 return 0;
1072 }
1073
1074 if (str->str && str->len < length)
1075 str->str = FX_Realloc(char, str->str, length + 1);
1076 else if (!str->str)
1077 str->str = FX_Alloc(char, length + 1);
1078
1079 str->str[length] = 0;
1080 if (str->str == NULL)
1081 return -1;
1082
1083 FXSYS_memcpy(str->str, bstr, length);
1084 str->len = length;
1085
1086 return 0;
1087}
1088
thestig77d148d2016-04-06 06:28:31 -07001089DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090 if (!str)
1091 return -1;
1092
1093 if (str->str) {
1094 FX_Free(str->str);
1095 str->str = NULL;
1096 }
1097 str->len = 0;
1098 return 0;
1099}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001100#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101
1102DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1103 int index,
1104 void* buffer,
1105 long* buflen) {
1106 if (!buffer)
1107 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001108
1109 if (index < 0)
1110 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
Tom Sepezbf59a072015-10-21 14:07:23 -07001112 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001113 if (!pDoc)
1114 return nullptr;
1115
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001116 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1117 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001118 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001119
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001120 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001122 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001123 int count = nameTree.GetCount();
1124 if (index >= count) {
Wei Li9b761132016-01-29 15:44:20 -08001125 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001127 return nullptr;
1128
1129 pdfium::base::CheckedNumeric<int> checked_count = count;
1130 checked_count += pDest->GetCount();
1131 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1132 return nullptr;
1133
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001136 for (const auto& it : *pDest) {
1137 bsName = it.first;
1138 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 if (!pDestObj)
1140 continue;
1141 if (i == index)
1142 break;
1143 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001144 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145 } else {
1146 pDestObj = nameTree.LookupValue(index, bsName);
1147 }
1148 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001149 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001150 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -08001151 pDestObj = pDict->GetArrayBy("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001153 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001155 if (!pDestObj->IsArray())
1156 return nullptr;
1157
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 CFX_WideString wsName = PDF_DecodeText(bsName);
1159 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001160 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 if (!buffer) {
1162 *buflen = len;
1163 } else if (*buflen >= len) {
1164 memcpy(buffer, utf16Name.c_str(), len);
1165 *buflen = len;
1166 } else {
1167 *buflen = -1;
1168 }
1169 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001170}