blob: 6b0a3861f9e845cdf48a4cd4709cc56db47697cd [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
dsinclair39c62fd2016-09-29 12:49:17 -070011#include "core/fpdfapi/cpdf_modulemgr.h"
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_array.h"
13#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/cpdf_annot.h"
15#include "core/fpdfdoc/cpdf_interform.h"
16#include "core/fpdfdoc/cpdf_metadata.h"
dsinclaira52ab742016-09-29 13:59:29 -070017#include "core/fxcrt/fx_basic.h"
18#include "core/fxcrt/fx_memory.h"
19#include "core/fxcrt/fx_xml.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/fsdk_define.h"
tsepez36eb4bd2016-10-03 15:24:27 -070021#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Tom Sepez40e9ff32015-11-30 12:39:54 -080023#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070024#include "fpdfsdk/fpdfxfa/cpdfxfa_document.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080025#endif // PDF_ENABLE_XFA
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027FX_BOOL FPDF_UnSupportError(int nError) {
28 CFSDK_UnsupportInfo_Adapter* pAdapter =
weili9f515bc2016-07-24 08:08:24 -070029 CPDF_ModuleMgr::Get()->GetUnsupportInfoAdapter();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 if (!pAdapter)
Tom Sepez2f2ffec2015-07-23 14:42:09 -070031 return FALSE;
tsepezddffb572016-05-24 16:20:29 -070032
weili9f515bc2016-07-24 08:08:24 -070033 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(pAdapter->GetUnspInfo());
34 if (info && info->FSDK_UnSupport_Handler)
35 info->FSDK_UnSupport_Handler(info, nError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037}
38
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039DLLEXPORT FPDF_BOOL STDCALL
40FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
41 if (!unsp_info || unsp_info->version != 1)
42 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
weili9f515bc2016-07-24 08:08:24 -070044 CPDF_ModuleMgr::Get()->SetUnsupportInfoAdapter(
tsepez36eb4bd2016-10-03 15:24:27 -070045 pdfium::MakeUnique<CFSDK_UnsupportInfo_Adapter>(unsp_info));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Lei Zhang1b700c32015-10-30 23:55:35 -070049void CheckUnSupportAnnot(CPDF_Document* pDoc, const CPDF_Annot* pPDFAnnot) {
jaepark956553e2016-08-31 06:49:27 -070050 CPDF_Annot::Subtype nAnnotSubtype = pPDFAnnot->GetSubtype();
51 if (nAnnotSubtype == CPDF_Annot::Subtype::THREED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 FPDF_UnSupportError(FPDF_UNSP_ANNOT_3DANNOT);
jaepark956553e2016-08-31 06:49:27 -070053 } else if (nAnnotSubtype == CPDF_Annot::Subtype::SCREEN) {
Lei Zhang1b700c32015-10-30 23:55:35 -070054 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 CFX_ByteString cbString;
56 if (pAnnotDict->KeyExist("IT"))
dsinclair38fd8442016-09-15 10:15:32 -070057 cbString = pAnnotDict->GetStringFor("IT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 if (cbString.Compare("Img") != 0)
59 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
jaepark956553e2016-08-31 06:49:27 -070060 } else if (nAnnotSubtype == CPDF_Annot::Subtype::MOVIE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 FPDF_UnSupportError(FPDF_UNSP_ANNOT_MOVIE);
jaepark956553e2016-08-31 06:49:27 -070062 } else if (nAnnotSubtype == CPDF_Annot::Subtype::SOUND) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SOUND);
jaepark956553e2016-08-31 06:49:27 -070064 } else if (nAnnotSubtype == CPDF_Annot::Subtype::RICHMEDIA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
jaepark956553e2016-08-31 06:49:27 -070066 } else if (nAnnotSubtype == CPDF_Annot::Subtype::FILEATTACHMENT) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 FPDF_UnSupportError(FPDF_UNSP_ANNOT_ATTACHMENT);
jaepark956553e2016-08-31 06:49:27 -070068 } else if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET) {
Lei Zhang1b700c32015-10-30 23:55:35 -070069 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 CFX_ByteString cbString;
jaepark956553e2016-08-31 06:49:27 -070071 if (pAnnotDict->KeyExist("FT"))
dsinclair38fd8442016-09-15 10:15:32 -070072 cbString = pAnnotDict->GetStringFor("FT");
jaepark956553e2016-08-31 06:49:27 -070073 if (cbString.Compare("Sig") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SIG);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 }
76}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Lei Zhang119dc642015-08-11 14:08:47 -070078FX_BOOL CheckSharedForm(const CXML_Element* pElement, CFX_ByteString cbName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 int count = pElement->CountAttrs();
80 int i = 0;
81 for (i = 0; i < count; i++) {
82 CFX_ByteString space, name;
83 CFX_WideString value;
84 pElement->GetAttrByIndex(i, space, name, value);
Lei Zhangd983b092015-12-14 16:58:33 -080085 if (space == "xmlns" && name == "adhocwf" &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
tsepez28f97ff2016-04-04 16:41:35 -070087 CXML_Element* pVersion =
tsepez4c3debb2016-04-08 12:20:38 -070088 pElement->GetElement("adhocwf", cbName.AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 if (!pVersion)
90 continue;
tsepez28f97ff2016-04-04 16:41:35 -070091 CFX_WideString wsContent = pVersion->GetContent(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 int nType = wsContent.GetInteger();
93 switch (nType) {
94 case 1:
95 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
96 break;
97 case 2:
98 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
99 break;
100 case 0:
101 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
102 break;
103 }
104 }
105 }
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700106
tsepezc3255f52016-03-25 14:52:27 -0700107 uint32_t nCount = pElement->CountChildren();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 for (i = 0; i < (int)nCount; i++) {
109 CXML_Element::ChildType childType = pElement->GetChildType(i);
110 if (childType == CXML_Element::Element) {
111 CXML_Element* pChild = pElement->GetElement(i);
112 if (CheckSharedForm(pChild, cbName))
113 return TRUE;
114 }
115 }
116 return FALSE;
117}
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700118
tsepezc3255f52016-03-25 14:52:27 -0700119void CheckUnSupportError(CPDF_Document* pDoc, uint32_t err_code) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 // Security
121 if (err_code == FPDF_ERR_SECURITY) {
122 FPDF_UnSupportError(FPDF_UNSP_DOC_SECURITY);
123 return;
124 }
125 if (!pDoc)
126 return;
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 // Portfolios and Packages
129 CPDF_Dictionary* pRootDict = pDoc->GetRoot();
130 if (pRootDict) {
131 CFX_ByteString cbString;
132 if (pRootDict->KeyExist("Collection")) {
133 FPDF_UnSupportError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
134 return;
135 }
136 if (pRootDict->KeyExist("Names")) {
dsinclair38fd8442016-09-15 10:15:32 -0700137 CPDF_Dictionary* pNameDict = pRootDict->GetDictFor("Names");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) {
139 FPDF_UnSupportError(FPDF_UNSP_DOC_ATTACHMENT);
140 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700141 }
142 if (pNameDict && pNameDict->KeyExist("JavaScript")) {
dsinclair38fd8442016-09-15 10:15:32 -0700143 CPDF_Dictionary* pJSDict = pNameDict->GetDictFor("JavaScript");
144 CPDF_Array* pArray = pJSDict ? pJSDict->GetArrayFor("Names") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 if (pArray) {
Wei Lie1aebd42016-04-11 10:02:09 -0700146 for (size_t i = 0; i < pArray->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -0800147 CFX_ByteString cbStr = pArray->GetStringAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) {
149 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDREVIEW);
150 return;
151 }
152 }
153 }
154 }
155 }
156 }
157
158 // SharedForm
Lei Zhang119dc642015-08-11 14:08:47 -0700159 CPDF_Metadata metaData(pDoc);
160 const CXML_Element* pElement = metaData.GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 if (pElement)
162 CheckSharedForm(pElement, "workflowType");
Tom Sepez51da0932015-11-25 16:05:49 -0800163
Tom Sepez40e9ff32015-11-30 12:39:54 -0800164#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800165 // XFA Forms
thestigdb1a24e2016-05-23 16:55:09 -0700166 CPDF_InterForm interform(pDoc);
167 if (interform.HasXFAForm())
Tom Sepez51da0932015-11-25 16:05:49 -0800168 FPDF_UnSupportError(FPDF_UNSP_DOC_XFAFORM);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800169#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170}
171
thestiga55880d2016-05-23 09:11:54 -0700172DLLEXPORT int STDCALL FPDFDoc_GetPageMode(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700173 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
174 if (!pDoc)
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700175 return PAGEMODE_UNKNOWN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176
Tom Sepez471a1032015-10-15 16:17:18 -0700177 CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 if (!pRoot)
179 return PAGEMODE_UNKNOWN;
180
dsinclair38fd8442016-09-15 10:15:32 -0700181 CPDF_Object* pName = pRoot->GetObjectFor("PageMode");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 if (!pName)
183 return PAGEMODE_USENONE;
184
185 CFX_ByteString strPageMode = pName->GetString();
186 if (strPageMode.IsEmpty() || strPageMode.EqualNoCase("UseNone"))
187 return PAGEMODE_USENONE;
188 if (strPageMode.EqualNoCase("UseOutlines"))
189 return PAGEMODE_USEOUTLINES;
190 if (strPageMode.EqualNoCase("UseThumbs"))
191 return PAGEMODE_USETHUMBS;
192 if (strPageMode.EqualNoCase("FullScreen"))
193 return PAGEMODE_FULLSCREEN;
194 if (strPageMode.EqualNoCase("UseOC"))
195 return PAGEMODE_USEOC;
196 if (strPageMode.EqualNoCase("UseAttachments"))
197 return PAGEMODE_USEATTACHMENTS;
198
199 return PAGEMODE_UNKNOWN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}