blob: 350945e3287e594f73d65c24f9f5435aae53a9c4 [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
thestigdb1a24e2016-05-23 16:55:09 -07009#include <memory>
10
Dan Sinclairaa403d32016-03-15 14:57:22 -040011#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
12#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
13#include "core/fpdfapi/include/cpdf_modulemgr.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040014#include "core/fxcrt/include/fx_xml.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080015#include "fpdfsdk/include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Tom Sepez40e9ff32015-11-30 12:39:54 -080017#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070018#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080019#endif // PDF_ENABLE_XFA
20
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021#define FPDFSDK_UNSUPPORT_CALL 100
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023class CFSDK_UnsupportInfo_Adapter {
24 public:
thestigdb1a24e2016-05-23 16:55:09 -070025 explicit CFSDK_UnsupportInfo_Adapter(UNSUPPORT_INFO* unsp_info)
26 : m_unsp_info(unsp_info) {}
27
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 void ReportError(int nErrorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 private:
thestigdb1a24e2016-05-23 16:55:09 -070031 UNSUPPORT_INFO* const m_unsp_info;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032};
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034void CFSDK_UnsupportInfo_Adapter::ReportError(int nErrorType) {
35 if (m_unsp_info && m_unsp_info->FSDK_UnSupport_Handler) {
36 m_unsp_info->FSDK_UnSupport_Handler(m_unsp_info, nErrorType);
37 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038}
39
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040void FreeUnsupportInfo(void* pData) {
41 CFSDK_UnsupportInfo_Adapter* pAdapter = (CFSDK_UnsupportInfo_Adapter*)pData;
42 delete pAdapter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045FX_BOOL FPDF_UnSupportError(int nError) {
46 CFSDK_UnsupportInfo_Adapter* pAdapter =
47 (CFSDK_UnsupportInfo_Adapter*)CPDF_ModuleMgr::Get()->GetPrivateData(
48 (void*)FPDFSDK_UNSUPPORT_CALL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 if (!pAdapter)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070051 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 pAdapter->ReportError(nError);
53 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054}
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056DLLEXPORT FPDF_BOOL STDCALL
57FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
58 if (!unsp_info || unsp_info->version != 1)
59 return FALSE;
60 CFSDK_UnsupportInfo_Adapter* pAdapter =
61 new CFSDK_UnsupportInfo_Adapter(unsp_info);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 CPDF_ModuleMgr::Get()->SetPrivateData((void*)FPDFSDK_UNSUPPORT_CALL, pAdapter,
64 &FreeUnsupportInfo);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Lei Zhang1b700c32015-10-30 23:55:35 -070069void CheckUnSupportAnnot(CPDF_Document* pDoc, const CPDF_Annot* pPDFAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 CFX_ByteString cbSubType = pPDFAnnot->GetSubType();
71 if (cbSubType.Compare("3D") == 0) {
72 FPDF_UnSupportError(FPDF_UNSP_ANNOT_3DANNOT);
73 } else if (cbSubType.Compare("Screen") == 0) {
Lei Zhang1b700c32015-10-30 23:55:35 -070074 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 CFX_ByteString cbString;
76 if (pAnnotDict->KeyExist("IT"))
Wei Li9b761132016-01-29 15:44:20 -080077 cbString = pAnnotDict->GetStringBy("IT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 if (cbString.Compare("Img") != 0)
79 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
80 } else if (cbSubType.Compare("Movie") == 0) {
81 FPDF_UnSupportError(FPDF_UNSP_ANNOT_MOVIE);
82 } else if (cbSubType.Compare("Sound") == 0) {
83 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SOUND);
84 } else if (cbSubType.Compare("RichMedia") == 0) {
85 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
86 } else if (cbSubType.Compare("FileAttachment") == 0) {
87 FPDF_UnSupportError(FPDF_UNSP_ANNOT_ATTACHMENT);
88 } else if (cbSubType.Compare("Widget") == 0) {
Lei Zhang1b700c32015-10-30 23:55:35 -070089 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 CFX_ByteString cbString;
91 if (pAnnotDict->KeyExist("FT")) {
Wei Li9b761132016-01-29 15:44:20 -080092 cbString = pAnnotDict->GetStringBy("FT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 }
94 if (cbString.Compare("Sig") == 0) {
95 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SIG);
96 }
97 }
98}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Lei Zhang119dc642015-08-11 14:08:47 -0700100FX_BOOL CheckSharedForm(const CXML_Element* pElement, CFX_ByteString cbName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 int count = pElement->CountAttrs();
102 int i = 0;
103 for (i = 0; i < count; i++) {
104 CFX_ByteString space, name;
105 CFX_WideString value;
106 pElement->GetAttrByIndex(i, space, name, value);
Lei Zhangd983b092015-12-14 16:58:33 -0800107 if (space == "xmlns" && name == "adhocwf" &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
tsepez28f97ff2016-04-04 16:41:35 -0700109 CXML_Element* pVersion =
tsepez4c3debb2016-04-08 12:20:38 -0700110 pElement->GetElement("adhocwf", cbName.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 if (!pVersion)
112 continue;
tsepez28f97ff2016-04-04 16:41:35 -0700113 CFX_WideString wsContent = pVersion->GetContent(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 int nType = wsContent.GetInteger();
115 switch (nType) {
116 case 1:
117 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
118 break;
119 case 2:
120 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
121 break;
122 case 0:
123 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
124 break;
125 }
126 }
127 }
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700128
tsepezc3255f52016-03-25 14:52:27 -0700129 uint32_t nCount = pElement->CountChildren();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 for (i = 0; i < (int)nCount; i++) {
131 CXML_Element::ChildType childType = pElement->GetChildType(i);
132 if (childType == CXML_Element::Element) {
133 CXML_Element* pChild = pElement->GetElement(i);
134 if (CheckSharedForm(pChild, cbName))
135 return TRUE;
136 }
137 }
138 return FALSE;
139}
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700140
tsepezc3255f52016-03-25 14:52:27 -0700141void CheckUnSupportError(CPDF_Document* pDoc, uint32_t err_code) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 // Security
143 if (err_code == FPDF_ERR_SECURITY) {
144 FPDF_UnSupportError(FPDF_UNSP_DOC_SECURITY);
145 return;
146 }
147 if (!pDoc)
148 return;
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 // Portfolios and Packages
151 CPDF_Dictionary* pRootDict = pDoc->GetRoot();
152 if (pRootDict) {
153 CFX_ByteString cbString;
154 if (pRootDict->KeyExist("Collection")) {
155 FPDF_UnSupportError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
156 return;
157 }
158 if (pRootDict->KeyExist("Names")) {
Wei Li9b761132016-01-29 15:44:20 -0800159 CPDF_Dictionary* pNameDict = pRootDict->GetDictBy("Names");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) {
161 FPDF_UnSupportError(FPDF_UNSP_DOC_ATTACHMENT);
162 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700163 }
164 if (pNameDict && pNameDict->KeyExist("JavaScript")) {
Wei Li9b761132016-01-29 15:44:20 -0800165 CPDF_Dictionary* pJSDict = pNameDict->GetDictBy("JavaScript");
166 CPDF_Array* pArray = pJSDict ? pJSDict->GetArrayBy("Names") : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 if (pArray) {
Wei Lie1aebd42016-04-11 10:02:09 -0700168 for (size_t i = 0; i < pArray->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -0800169 CFX_ByteString cbStr = pArray->GetStringAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) {
171 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDREVIEW);
172 return;
173 }
174 }
175 }
176 }
177 }
178 }
179
180 // SharedForm
Lei Zhang119dc642015-08-11 14:08:47 -0700181 CPDF_Metadata metaData(pDoc);
182 const CXML_Element* pElement = metaData.GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 if (pElement)
184 CheckSharedForm(pElement, "workflowType");
Tom Sepez51da0932015-11-25 16:05:49 -0800185
Tom Sepez40e9ff32015-11-30 12:39:54 -0800186#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800187 // XFA Forms
thestigdb1a24e2016-05-23 16:55:09 -0700188 CPDF_InterForm interform(pDoc);
189 if (interform.HasXFAForm())
Tom Sepez51da0932015-11-25 16:05:49 -0800190 FPDF_UnSupportError(FPDF_UNSP_DOC_XFAFORM);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800191#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192}
193
thestiga55880d2016-05-23 09:11:54 -0700194DLLEXPORT int STDCALL 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
tsepezbd567552016-03-29 14:51:50 -0700203 CPDF_Object* pName = pRoot->GetObjectBy("PageMode");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 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}