blob: 810650fb7d828da6cf194e4ca8f65545d350e999 [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/fpdf_ext.h"
8
Dan Sinclairaa403d32016-03-15 14:57:22 -04009#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
10#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
11#include "core/fpdfapi/include/cpdf_modulemgr.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040012#include "core/fxcrt/include/fx_xml.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080013#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Tom Sepez40e9ff32015-11-30 12:39:54 -080015#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080016#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080017#endif // PDF_ENABLE_XFA
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019#define FPDFSDK_UNSUPPORT_CALL 100
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021class CFSDK_UnsupportInfo_Adapter {
22 public:
23 CFSDK_UnsupportInfo_Adapter(UNSUPPORT_INFO* unsp_info) {
24 m_unsp_info = unsp_info;
25 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 void ReportError(int nErrorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 private:
29 UNSUPPORT_INFO* m_unsp_info;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030};
31
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032void CFSDK_UnsupportInfo_Adapter::ReportError(int nErrorType) {
33 if (m_unsp_info && m_unsp_info->FSDK_UnSupport_Handler) {
34 m_unsp_info->FSDK_UnSupport_Handler(m_unsp_info, nErrorType);
35 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036}
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038void FreeUnsupportInfo(void* pData) {
39 CFSDK_UnsupportInfo_Adapter* pAdapter = (CFSDK_UnsupportInfo_Adapter*)pData;
40 delete pAdapter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041}
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043FX_BOOL FPDF_UnSupportError(int nError) {
44 CFSDK_UnsupportInfo_Adapter* pAdapter =
45 (CFSDK_UnsupportInfo_Adapter*)CPDF_ModuleMgr::Get()->GetPrivateData(
46 (void*)FPDFSDK_UNSUPPORT_CALL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 if (!pAdapter)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070049 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 pAdapter->ReportError(nError);
51 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052}
53
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054DLLEXPORT FPDF_BOOL STDCALL
55FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
56 if (!unsp_info || unsp_info->version != 1)
57 return FALSE;
58 CFSDK_UnsupportInfo_Adapter* pAdapter =
59 new CFSDK_UnsupportInfo_Adapter(unsp_info);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 CPDF_ModuleMgr::Get()->SetPrivateData((void*)FPDFSDK_UNSUPPORT_CALL, pAdapter,
62 &FreeUnsupportInfo);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
Lei Zhang1b700c32015-10-30 23:55:35 -070067void CheckUnSupportAnnot(CPDF_Document* pDoc, const CPDF_Annot* pPDFAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 CFX_ByteString cbSubType = pPDFAnnot->GetSubType();
69 if (cbSubType.Compare("3D") == 0) {
70 FPDF_UnSupportError(FPDF_UNSP_ANNOT_3DANNOT);
71 } else if (cbSubType.Compare("Screen") == 0) {
Lei Zhang1b700c32015-10-30 23:55:35 -070072 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 CFX_ByteString cbString;
74 if (pAnnotDict->KeyExist("IT"))
Wei Li9b761132016-01-29 15:44:20 -080075 cbString = pAnnotDict->GetStringBy("IT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 if (cbString.Compare("Img") != 0)
77 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
78 } else if (cbSubType.Compare("Movie") == 0) {
79 FPDF_UnSupportError(FPDF_UNSP_ANNOT_MOVIE);
80 } else if (cbSubType.Compare("Sound") == 0) {
81 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SOUND);
82 } else if (cbSubType.Compare("RichMedia") == 0) {
83 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
84 } else if (cbSubType.Compare("FileAttachment") == 0) {
85 FPDF_UnSupportError(FPDF_UNSP_ANNOT_ATTACHMENT);
86 } else if (cbSubType.Compare("Widget") == 0) {
Lei Zhang1b700c32015-10-30 23:55:35 -070087 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CFX_ByteString cbString;
89 if (pAnnotDict->KeyExist("FT")) {
Wei Li9b761132016-01-29 15:44:20 -080090 cbString = pAnnotDict->GetStringBy("FT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 }
92 if (cbString.Compare("Sig") == 0) {
93 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SIG);
94 }
95 }
96}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097
Lei Zhang119dc642015-08-11 14:08:47 -070098FX_BOOL CheckSharedForm(const CXML_Element* pElement, CFX_ByteString cbName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 int count = pElement->CountAttrs();
100 int i = 0;
101 for (i = 0; i < count; i++) {
102 CFX_ByteString space, name;
103 CFX_WideString value;
104 pElement->GetAttrByIndex(i, space, name, value);
Lei Zhangd983b092015-12-14 16:58:33 -0800105 if (space == "xmlns" && name == "adhocwf" &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
107 CXML_Element* pVersion = pElement->GetElement("adhocwf", cbName);
108 if (!pVersion)
109 continue;
110 CFX_WideString wsContent = pVersion->GetContent(0); // == 1.1
111 int nType = wsContent.GetInteger();
112 switch (nType) {
113 case 1:
114 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
115 break;
116 case 2:
117 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
118 break;
119 case 0:
120 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
121 break;
122 }
123 }
124 }
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 FX_DWORD nCount = pElement->CountChildren();
127 for (i = 0; i < (int)nCount; i++) {
128 CXML_Element::ChildType childType = pElement->GetChildType(i);
129 if (childType == CXML_Element::Element) {
130 CXML_Element* pChild = pElement->GetElement(i);
131 if (CheckSharedForm(pChild, cbName))
132 return TRUE;
133 }
134 }
135 return FALSE;
136}
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138void CheckUnSupportError(CPDF_Document* pDoc, FX_DWORD err_code) {
139 // Security
140 if (err_code == FPDF_ERR_SECURITY) {
141 FPDF_UnSupportError(FPDF_UNSP_DOC_SECURITY);
142 return;
143 }
144 if (!pDoc)
145 return;
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 // Portfolios and Packages
148 CPDF_Dictionary* pRootDict = pDoc->GetRoot();
149 if (pRootDict) {
150 CFX_ByteString cbString;
151 if (pRootDict->KeyExist("Collection")) {
152 FPDF_UnSupportError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
153 return;
154 }
155 if (pRootDict->KeyExist("Names")) {
Wei Li9b761132016-01-29 15:44:20 -0800156 CPDF_Dictionary* pNameDict = pRootDict->GetDictBy("Names");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) {
158 FPDF_UnSupportError(FPDF_UNSP_DOC_ATTACHMENT);
159 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700160 }
161 if (pNameDict && pNameDict->KeyExist("JavaScript")) {
Wei Li9b761132016-01-29 15:44:20 -0800162 CPDF_Dictionary* pJSDict = pNameDict->GetDictBy("JavaScript");
163 CPDF_Array* pArray = pJSDict ? pJSDict->GetArrayBy("Names") : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 if (pArray) {
165 int nCount = pArray->GetCount();
166 for (int i = 0; i < nCount; i++) {
Wei Li9b761132016-01-29 15:44:20 -0800167 CFX_ByteString cbStr = pArray->GetStringAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) {
169 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDREVIEW);
170 return;
171 }
172 }
173 }
174 }
175 }
176 }
177
178 // SharedForm
Lei Zhang119dc642015-08-11 14:08:47 -0700179 CPDF_Metadata metaData(pDoc);
180 const CXML_Element* pElement = metaData.GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (pElement)
182 CheckSharedForm(pElement, "workflowType");
Tom Sepez51da0932015-11-25 16:05:49 -0800183
Tom Sepez40e9ff32015-11-30 12:39:54 -0800184#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800185 // XFA Forms
186 CPDF_InterForm* pInterForm = new CPDF_InterForm(pDoc, FALSE);
187 if (pInterForm->HasXFAForm()) {
188 FPDF_UnSupportError(FPDF_UNSP_DOC_XFAFORM);
189 }
190 delete pInterForm;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800191#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192}
193
194DLLEXPORT int FPDFDoc_GetPageMode(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700195 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
196 if (!pDoc)
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700197 return PAGEMODE_UNKNOWN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198
Tom Sepez471a1032015-10-15 16:17:18 -0700199 CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (!pRoot)
201 return PAGEMODE_UNKNOWN;
202
203 CPDF_Object* pName = pRoot->GetElement("PageMode");
204 if (!pName)
205 return PAGEMODE_USENONE;
206
207 CFX_ByteString strPageMode = pName->GetString();
208 if (strPageMode.IsEmpty() || strPageMode.EqualNoCase("UseNone"))
209 return PAGEMODE_USENONE;
210 if (strPageMode.EqualNoCase("UseOutlines"))
211 return PAGEMODE_USEOUTLINES;
212 if (strPageMode.EqualNoCase("UseThumbs"))
213 return PAGEMODE_USETHUMBS;
214 if (strPageMode.EqualNoCase("FullScreen"))
215 return PAGEMODE_FULLSCREEN;
216 if (strPageMode.EqualNoCase("UseOC"))
217 return PAGEMODE_USEOC;
218 if (strPageMode.EqualNoCase("UseAttachments"))
219 return PAGEMODE_USEATTACHMENTS;
220
221 return PAGEMODE_UNKNOWN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}