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