blob: f598835e3cf74cbbee97334100a22856d7f774f0 [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"
Tom Sepez8a6fdad2017-05-09 15:03:33 -070019#include "core/fxcrt/xml/cxml_content.h"
Dan Sinclair908c8482017-03-30 14:33:28 -040020#include "core/fxcrt/xml/cxml_element.h"
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/fsdk_define.h"
tsepez36eb4bd2016-10-03 15:24:27 -070022#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Tom Sepez40e9ff32015-11-30 12:39:54 -080024#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070025#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080026#endif // PDF_ENABLE_XFA
27
tsepez4cf55152016-11-02 14:37:54 -070028bool FPDF_UnSupportError(int nError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 CFSDK_UnsupportInfo_Adapter* pAdapter =
weili9f515bc2016-07-24 08:08:24 -070030 CPDF_ModuleMgr::Get()->GetUnsupportInfoAdapter();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 if (!pAdapter)
tsepez4cf55152016-11-02 14:37:54 -070032 return false;
tsepezddffb572016-05-24 16:20:29 -070033
weili9f515bc2016-07-24 08:08:24 -070034 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(pAdapter->GetUnspInfo());
35 if (info && info->FSDK_UnSupport_Handler)
36 info->FSDK_UnSupport_Handler(info, nError);
tsepez4cf55152016-11-02 14:37:54 -070037 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038}
39
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040DLLEXPORT FPDF_BOOL STDCALL
41FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
42 if (!unsp_info || unsp_info->version != 1)
tsepez4cf55152016-11-02 14:37:54 -070043 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
weili9f515bc2016-07-24 08:08:24 -070045 CPDF_ModuleMgr::Get()->SetUnsupportInfoAdapter(
tsepez36eb4bd2016-10-03 15:24:27 -070046 pdfium::MakeUnique<CFSDK_UnsupportInfo_Adapter>(unsp_info));
tsepez4cf55152016-11-02 14:37:54 -070047 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Lei Zhang1b700c32015-10-30 23:55:35 -070050void CheckUnSupportAnnot(CPDF_Document* pDoc, const CPDF_Annot* pPDFAnnot) {
jaepark956553e2016-08-31 06:49:27 -070051 CPDF_Annot::Subtype nAnnotSubtype = pPDFAnnot->GetSubtype();
52 if (nAnnotSubtype == CPDF_Annot::Subtype::THREED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 FPDF_UnSupportError(FPDF_UNSP_ANNOT_3DANNOT);
jaepark956553e2016-08-31 06:49:27 -070054 } else if (nAnnotSubtype == CPDF_Annot::Subtype::SCREEN) {
Lei Zhang1b700c32015-10-30 23:55:35 -070055 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 CFX_ByteString cbString;
57 if (pAnnotDict->KeyExist("IT"))
dsinclair38fd8442016-09-15 10:15:32 -070058 cbString = pAnnotDict->GetStringFor("IT");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 if (cbString.Compare("Img") != 0)
60 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
jaepark956553e2016-08-31 06:49:27 -070061 } else if (nAnnotSubtype == CPDF_Annot::Subtype::MOVIE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 FPDF_UnSupportError(FPDF_UNSP_ANNOT_MOVIE);
jaepark956553e2016-08-31 06:49:27 -070063 } else if (nAnnotSubtype == CPDF_Annot::Subtype::SOUND) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SOUND);
jaepark956553e2016-08-31 06:49:27 -070065 } else if (nAnnotSubtype == CPDF_Annot::Subtype::RICHMEDIA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
jaepark956553e2016-08-31 06:49:27 -070067 } else if (nAnnotSubtype == CPDF_Annot::Subtype::FILEATTACHMENT) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 FPDF_UnSupportError(FPDF_UNSP_ANNOT_ATTACHMENT);
jaepark956553e2016-08-31 06:49:27 -070069 } else if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET) {
Lei Zhang1b700c32015-10-30 23:55:35 -070070 const CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 CFX_ByteString cbString;
jaepark956553e2016-08-31 06:49:27 -070072 if (pAnnotDict->KeyExist("FT"))
dsinclair38fd8442016-09-15 10:15:32 -070073 cbString = pAnnotDict->GetStringFor("FT");
jaepark956553e2016-08-31 06:49:27 -070074 if (cbString.Compare("Sig") == 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 FPDF_UnSupportError(FPDF_UNSP_ANNOT_SIG);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 }
77}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
tsepez4cf55152016-11-02 14:37:54 -070079bool CheckSharedForm(const CXML_Element* pElement, CFX_ByteString cbName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 int count = pElement->CountAttrs();
81 int i = 0;
82 for (i = 0; i < count; i++) {
Tom Sepezec8ff7d2017-04-07 16:58:00 -070083 CFX_ByteString space;
84 CFX_ByteString name;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 CFX_WideString value;
Tom Sepezec8ff7d2017-04-07 16:58:00 -070086 pElement->GetAttrByIndex(i, &space, &name, &value);
Lei Zhangd983b092015-12-14 16:58:33 -080087 if (space == "xmlns" && name == "adhocwf" &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
tsepez28f97ff2016-04-04 16:41:35 -070089 CXML_Element* pVersion =
Tom Sepez8a6fdad2017-05-09 15:03:33 -070090 pElement->GetElement("adhocwf", cbName.AsStringC(), 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (!pVersion)
92 continue;
Tom Sepez8a6fdad2017-05-09 15:03:33 -070093 CXML_Content* pContent = ToContent(pVersion->GetChild(0));
94 if (!pContent)
95 continue;
96 switch (pContent->m_Content.GetInteger()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 case 1:
98 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
99 break;
100 case 2:
101 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
102 break;
103 case 0:
104 FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
105 break;
106 }
107 }
108 }
Tom Sepezca3ac5e2015-06-10 17:38:11 -0700109
tsepezc3255f52016-03-25 14:52:27 -0700110 uint32_t nCount = pElement->CountChildren();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 for (i = 0; i < (int)nCount; i++) {
Tom Sepez8a6fdad2017-05-09 15:03:33 -0700112 CXML_Element* pChild = ToElement(pElement->GetChild(i));
113 if (pChild && CheckSharedForm(pChild, cbName))
114 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 }
tsepez4cf55152016-11-02 14:37:54 -0700116 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117}
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}