Fix build broken at 506df426d5d6.
[pdfium.git] / fpdfsdk / src / fpdf_ext.cpp
1 // 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../core/include/fxcrt/fx_xml.h"
8 #include "../../public/fpdf_ext.h"
9 #include "../include/fsdk_define.h"
10
11 #define FPDFSDK_UNSUPPORT_CALL 100
12
13 class CFSDK_UnsupportInfo_Adapter {
14  public:
15   CFSDK_UnsupportInfo_Adapter(UNSUPPORT_INFO* unsp_info) {
16     m_unsp_info = unsp_info;
17   }
18   //  FX_BOOL NeedToPauseNow();
19   void ReportError(int nErrorType);
20
21  private:
22   UNSUPPORT_INFO* m_unsp_info;
23 };
24
25 void CFSDK_UnsupportInfo_Adapter::ReportError(int nErrorType) {
26   if (m_unsp_info && m_unsp_info->FSDK_UnSupport_Handler) {
27     m_unsp_info->FSDK_UnSupport_Handler(m_unsp_info, nErrorType);
28   }
29 }
30
31 void FreeUnsupportInfo(void* pData) {
32   CFSDK_UnsupportInfo_Adapter* pAdapter = (CFSDK_UnsupportInfo_Adapter*)pData;
33   delete pAdapter;
34 }
35
36 FX_BOOL FPDF_UnSupportError(int nError) {
37   CFSDK_UnsupportInfo_Adapter* pAdapter =
38       (CFSDK_UnsupportInfo_Adapter*)CPDF_ModuleMgr::Get()->GetPrivateData(
39           (void*)FPDFSDK_UNSUPPORT_CALL);
40
41   if (!pAdapter)
42     return FALSE;
43   pAdapter->ReportError(nError);
44   return TRUE;
45 }
46
47 DLLEXPORT FPDF_BOOL STDCALL
48 FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO* unsp_info) {
49   if (!unsp_info || unsp_info->version != 1)
50     return FALSE;
51   CFSDK_UnsupportInfo_Adapter* pAdapter =
52       new CFSDK_UnsupportInfo_Adapter(unsp_info);
53
54   CPDF_ModuleMgr::Get()->SetPrivateData((void*)FPDFSDK_UNSUPPORT_CALL, pAdapter,
55                                         &FreeUnsupportInfo);
56
57   return TRUE;
58 }
59
60 void CheckUnSupportAnnot(CPDF_Document* pDoc, CPDF_Annot* pPDFAnnot) {
61   CFX_ByteString cbSubType = pPDFAnnot->GetSubType();
62   if (cbSubType.Compare("3D") == 0) {
63     FPDF_UnSupportError(FPDF_UNSP_ANNOT_3DANNOT);
64   } else if (cbSubType.Compare("Screen") == 0) {
65     CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
66     CFX_ByteString cbString;
67     if (pAnnotDict->KeyExist("IT"))
68       cbString = pAnnotDict->GetString("IT");
69     if (cbString.Compare("Img") != 0)
70       FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_MEDIA);
71   } else if (cbSubType.Compare("Movie") == 0) {
72     FPDF_UnSupportError(FPDF_UNSP_ANNOT_MOVIE);
73   } else if (cbSubType.Compare("Sound") == 0) {
74     FPDF_UnSupportError(FPDF_UNSP_ANNOT_SOUND);
75   } else if (cbSubType.Compare("RichMedia") == 0) {
76     FPDF_UnSupportError(FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA);
77   } else if (cbSubType.Compare("FileAttachment") == 0) {
78     FPDF_UnSupportError(FPDF_UNSP_ANNOT_ATTACHMENT);
79   } else if (cbSubType.Compare("Widget") == 0) {
80     CPDF_Dictionary* pAnnotDict = pPDFAnnot->GetAnnotDict();
81     CFX_ByteString cbString;
82     if (pAnnotDict->KeyExist("FT")) {
83       cbString = pAnnotDict->GetString("FT");
84     }
85     if (cbString.Compare("Sig") == 0) {
86       FPDF_UnSupportError(FPDF_UNSP_ANNOT_SIG);
87     }
88   }
89 }
90
91 FX_BOOL CheckSharedForm(const CXML_Element* pElement, CFX_ByteString cbName) {
92   int count = pElement->CountAttrs();
93   int i = 0;
94   for (i = 0; i < count; i++) {
95     CFX_ByteString space, name;
96     CFX_WideString value;
97     pElement->GetAttrByIndex(i, space, name, value);
98     if (space == FX_BSTRC("xmlns") && name == FX_BSTRC("adhocwf") &&
99         value == L"http://ns.adobe.com/AcrobatAdhocWorkflow/1.0/") {
100       CXML_Element* pVersion = pElement->GetElement("adhocwf", cbName);
101       if (!pVersion)
102         continue;
103       CFX_WideString wsContent = pVersion->GetContent(0);  // == 1.1
104       int nType = wsContent.GetInteger();
105       switch (nType) {
106         case 1:
107           FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_ACROBAT);
108           break;
109         case 2:
110           FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM);
111           break;
112         case 0:
113           FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDFORM_EMAIL);
114           break;
115       }
116     }
117   }
118
119   FX_DWORD nCount = pElement->CountChildren();
120   for (i = 0; i < (int)nCount; i++) {
121     CXML_Element::ChildType childType = pElement->GetChildType(i);
122     if (childType == CXML_Element::Element) {
123       CXML_Element* pChild = pElement->GetElement(i);
124       if (CheckSharedForm(pChild, cbName))
125         return TRUE;
126     }
127   }
128   return FALSE;
129 }
130
131 void CheckUnSupportError(CPDF_Document* pDoc, FX_DWORD err_code) {
132   // Security
133   if (err_code == FPDF_ERR_SECURITY) {
134     FPDF_UnSupportError(FPDF_UNSP_DOC_SECURITY);
135     return;
136   }
137   if (!pDoc)
138     return;
139
140   // Portfolios and Packages
141   CPDF_Dictionary* pRootDict = pDoc->GetRoot();
142   if (pRootDict) {
143     CFX_ByteString cbString;
144     if (pRootDict->KeyExist("Collection")) {
145       FPDF_UnSupportError(FPDF_UNSP_DOC_PORTABLECOLLECTION);
146       return;
147     }
148     if (pRootDict->KeyExist("Names")) {
149       CPDF_Dictionary* pNameDict = pRootDict->GetDict("Names");
150       if (pNameDict && pNameDict->KeyExist("EmbeddedFiles")) {
151         FPDF_UnSupportError(FPDF_UNSP_DOC_ATTACHMENT);
152         return;
153       }
154       if (pNameDict && pNameDict->KeyExist("JavaScript")) {
155         CPDF_Dictionary* pJSDict = pNameDict->GetDict("JavaScript");
156         CPDF_Array* pArray = pJSDict ? pJSDict->GetArray("Names") : NULL;
157         if (pArray) {
158           int nCount = pArray->GetCount();
159           for (int i = 0; i < nCount; i++) {
160             CFX_ByteString cbStr = pArray->GetString(i);
161             if (cbStr.Compare("com.adobe.acrobat.SharedReview.Register") == 0) {
162               FPDF_UnSupportError(FPDF_UNSP_DOC_SHAREDREVIEW);
163               return;
164             }
165           }
166         }
167       }
168     }
169   }
170
171   // SharedForm
172   CPDF_Metadata metaData(pDoc);
173   const CXML_Element* pElement = metaData.GetRoot();
174   if (pElement)
175     CheckSharedForm(pElement, "workflowType");
176
177   // XFA Forms
178   CPDF_InterForm* pInterForm = new CPDF_InterForm(pDoc, FALSE);
179   if (pInterForm->HasXFAForm()) {
180     FPDF_UnSupportError(FPDF_UNSP_DOC_XFAFORM);
181   }
182   delete pInterForm;
183 }
184
185 DLLEXPORT int FPDFDoc_GetPageMode(FPDF_DOCUMENT document) {
186   if (!document)
187     return PAGEMODE_UNKNOWN;
188
189   CPDF_Dictionary* pRoot = ((CPDF_Document*)document)->GetRoot();
190   if (!pRoot)
191     return PAGEMODE_UNKNOWN;
192
193   CPDF_Object* pName = pRoot->GetElement("PageMode");
194   if (!pName)
195     return PAGEMODE_USENONE;
196
197   CFX_ByteString strPageMode = pName->GetString();
198   if (strPageMode.IsEmpty() || strPageMode.EqualNoCase("UseNone"))
199     return PAGEMODE_USENONE;
200   if (strPageMode.EqualNoCase("UseOutlines"))
201     return PAGEMODE_USEOUTLINES;
202   if (strPageMode.EqualNoCase("UseThumbs"))
203     return PAGEMODE_USETHUMBS;
204   if (strPageMode.EqualNoCase("FullScreen"))
205     return PAGEMODE_FULLSCREEN;
206   if (strPageMode.EqualNoCase("UseOC"))
207     return PAGEMODE_USEOC;
208   if (strPageMode.EqualNoCase("UseAttachments"))
209     return PAGEMODE_USEATTACHMENTS;
210
211   return PAGEMODE_UNKNOWN;
212 }