blob: 1e39f2b2c936719d2bd646b105b0e5348e9391a0 [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_memory.h"
Tom Sepez8a6fdad2017-05-09 15:03:33 -070018#include "core/fxcrt/xml/cxml_content.h"
Dan Sinclair908c8482017-03-30 14:33:28 -040019#include "core/fxcrt/xml/cxml_element.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
dsinclair521b7502016-11-02 13:02:28 -070024#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080025#endif // PDF_ENABLE_XFA
26
tsepez4cf55152016-11-02 14:37:54 -070027bool FPDF_UnSupportError(int nError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 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)
tsepez4cf55152016-11-02 14:37:54 -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);
tsepez4cf55152016-11-02 14:37:54 -070036 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037}
38
Dan Sinclair00d2ad12017-08-10 14:13:02 -040039FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
41 if (!unsp_info || unsp_info->version != 1)
tsepez4cf55152016-11-02 14:37:54 -070042 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));
tsepez4cf55152016-11-02 14:37:54 -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();
Ryan Harrison275e2602017-09-18 14:23:18 -040055 ByteString cbString;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 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();
Ryan Harrison275e2602017-09-18 14:23:18 -040070 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
Ryan Harrison275e2602017-09-18 14:23:18 -040078bool CheckSharedForm(const CXML_Element* pElement, 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++) {
Ryan Harrison275e2602017-09-18 14:23:18 -040082 ByteString space;
83 ByteString name;
84 WideString value;
Tom Sepezec8ff7d2017-04-07 16:58:00 -070085 pElement->GetAttrByIndex(i, &space, &name, &value);
Lei Zhangd983b092015-12-14 16:58:33 -080086 if (space == "xmlns" && name == "adhocwf" &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
tsepez28f97ff2016-04-04 16:41:35 -070088 CXML_Element* pVersion =
Ryan Harrison275e2602017-09-18 14:23:18 -040089 pElement->GetElement("adhocwf", cbName.AsStringView(), 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 if (!pVersion)
91 continue;
Tom Sepez8a6fdad2017-05-09 15:03:33 -070092 CXML_Content* pContent = ToContent(pVersion->GetChild(0));
93 if (!pContent)
94 continue;
95 switch (pContent->m_Content.GetInteger()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 case 1:
97 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
98 break;
99 case 2:
100 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
101 break;
102 case 0:
103 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
104 break;
105 }
106 }
107 }
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700108
tsepezc3255f52016-03-25 14:52:27 -0700109 uint32_t nCount = pElement->CountChildren();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 for (i = 0; i < (int)nCount; i++) {
Tom Sepez8a6fdad2017-05-09 15:03:33 -0700111 CXML_Element* pChild = ToElement(pElement->GetChild(i));
112 if (pChild && CheckSharedForm(pChild, cbName))
113 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
tsepez4cf55152016-11-02 14:37:54 -0700115 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116}
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700117
tsepezc3255f52016-03-25 14:52:27 -0700118void CheckUnSupportError(CPDF_Document* pDoc, uint32_t err_code) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 // Security
120 if (err_code == FPDF_ERR_SECURITY) {
121 FPDF_UnSupportError(FPDF_UNSP_DOC_SECURITY);
122 return;
123 }
124 if (!pDoc)
125 return;
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 // Portfolios and Packages
Lei Zhang01581062017-08-30 14:19:26 -0700128 const CPDF_Dictionary* pRootDict = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 if (pRootDict) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400130 ByteString cbString;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (pRootDict->KeyExist("Collection")) {
132 FPDF_UnSupportError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
133 return;
134 }
135 if (pRootDict->KeyExist("Names")) {
dsinclair38fd8442016-09-15 10:15:32 -0700136 CPDF_Dictionary* pNameDict = pRootDict->GetDictFor("Names");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) {
138 FPDF_UnSupportError(FPDF_UNSP_DOC_ATTACHMENT);
139 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700140 }
141 if (pNameDict && pNameDict->KeyExist("JavaScript")) {
dsinclair38fd8442016-09-15 10:15:32 -0700142 CPDF_Dictionary* pJSDict = pNameDict->GetDictFor("JavaScript");
143 CPDF_Array* pArray = pJSDict ? pJSDict->GetArrayFor("Names") : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 if (pArray) {
Wei Lie1aebd42016-04-11 10:02:09 -0700145 for (size_t i = 0; i < pArray->GetCount(); i++) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400146 ByteString cbStr = pArray->GetStringAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) {
148 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDREVIEW);
149 return;
150 }
151 }
152 }
153 }
154 }
155 }
156
157 // SharedForm
Lei Zhang119dc642015-08-11 14:08:47 -0700158 CPDF_Metadata metaData(pDoc);
159 const CXML_Element* pElement = metaData.GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 if (pElement)
161 CheckSharedForm(pElement, "workflowType");
Tom Sepez51da0932015-11-25 16:05:49 -0800162
Tom Sepez40e9ff32015-11-30 12:39:54 -0800163#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800164 // XFA Forms
thestigdb1a24e2016-05-23 16:55:09 -0700165 CPDF_InterForm interform(pDoc);
166 if (interform.HasXFAForm())
Tom Sepez51da0932015-11-25 16:05:49 -0800167 FPDF_UnSupportError(FPDF_UNSP_DOC_XFAFORM);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800168#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169}
170
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400171FPDF_EXPORT int FPDF_CALLCONV FPDFDoc_GetPageMode(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700172 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
173 if (!pDoc)
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700174 return PAGEMODE_UNKNOWN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175
Lei Zhang01581062017-08-30 14:19:26 -0700176 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 if (!pRoot)
178 return PAGEMODE_UNKNOWN;
179
dsinclair38fd8442016-09-15 10:15:32 -0700180 CPDF_Object* pName = pRoot->GetObjectFor("PageMode");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (!pName)
182 return PAGEMODE_USENONE;
183
Ryan Harrison275e2602017-09-18 14:23:18 -0400184 ByteString strPageMode = pName->GetString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 if (strPageMode.IsEmpty() || strPageMode.EqualNoCase("UseNone"))
186 return PAGEMODE_USENONE;
187 if (strPageMode.EqualNoCase("UseOutlines"))
188 return PAGEMODE_USEOUTLINES;
189 if (strPageMode.EqualNoCase("UseThumbs"))
190 return PAGEMODE_USETHUMBS;
191 if (strPageMode.EqualNoCase("FullScreen"))
192 return PAGEMODE_FULLSCREEN;
193 if (strPageMode.EqualNoCase("UseOC"))
194 return PAGEMODE_USEOC;
195 if (strPageMode.EqualNoCase("UseAttachments"))
196 return PAGEMODE_USEATTACHMENTS;
197
198 return PAGEMODE_UNKNOWN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}