• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 #ifndef FPDFSDK_CPDFSDK_HELPERS_H_
8 #define FPDFSDK_CPDFSDK_HELPERS_H_
9 
10 #include <vector>
11 
12 #include "build/build_config.h"
13 #include "core/fpdfapi/page/cpdf_page.h"
14 #include "core/fpdfapi/parser/cpdf_parser.h"
15 #include "core/fxcrt/compiler_specific.h"
16 #include "core/fxcrt/numerics/safe_conversions.h"
17 #include "core/fxcrt/retain_ptr.h"
18 #include "core/fxcrt/span.h"
19 #include "core/fxge/cfx_path.h"
20 #include "core/fxge/dib/fx_dib.h"
21 #include "public/fpdf_doc.h"
22 #include "public/fpdf_ext.h"
23 #include "public/fpdfview.h"
24 
25 #ifdef PDF_ENABLE_XFA
26 #include "core/fxcrt/fx_stream.h"
27 #endif  // PDF_ENABLE_XFA
28 
29 class CFX_DIBitmap;
30 class CPDF_Annot;
31 class CPDF_AnnotContext;
32 class CPDF_ClipPath;
33 class CPDF_ContentMarkItem;
34 class CPDF_Object;
35 class CPDF_Font;
36 class CPDF_LinkExtract;
37 class CPDF_PageObject;
38 class CPDF_RenderOptions;
39 class CPDF_Stream;
40 class CPDF_StructElement;
41 class CPDF_StructTree;
42 class CPDF_TextPage;
43 class CPDF_TextPageFind;
44 class CPDFSDK_FormFillEnvironment;
45 class CPDFSDK_InteractiveForm;
46 struct CPDF_JavaScript;
47 struct XObjectContext;
48 
49 #if defined(PDF_USE_SKIA)
50 class SkCanvas;
51 #endif
52 
53 // Conversions to/from underlying types.
54 IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page);
55 FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page);
56 CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page);
57 FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc);
58 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
59 
60 // Conversions to/from incomplete FPDF_ API types.
FPDFActionFromCPDFDictionary(const CPDF_Dictionary * action)61 inline FPDF_ACTION FPDFActionFromCPDFDictionary(const CPDF_Dictionary* action) {
62   return reinterpret_cast<FPDF_ACTION>(const_cast<CPDF_Dictionary*>(action));
63 }
CPDFDictionaryFromFPDFAction(FPDF_ACTION action)64 inline CPDF_Dictionary* CPDFDictionaryFromFPDFAction(FPDF_ACTION action) {
65   return reinterpret_cast<CPDF_Dictionary*>(action);
66 }
67 
FPDFAnnotationFromCPDFAnnotContext(CPDF_AnnotContext * annot)68 inline FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(
69     CPDF_AnnotContext* annot) {
70   return reinterpret_cast<FPDF_ANNOTATION>(annot);
71 }
CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot)72 inline CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(
73     FPDF_ANNOTATION annot) {
74   return reinterpret_cast<CPDF_AnnotContext*>(annot);
75 }
76 
FPDFAttachmentFromCPDFObject(CPDF_Object * attachment)77 inline FPDF_ATTACHMENT FPDFAttachmentFromCPDFObject(CPDF_Object* attachment) {
78   return reinterpret_cast<FPDF_ATTACHMENT>(attachment);
79 }
CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment)80 inline CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) {
81   return reinterpret_cast<CPDF_Object*>(attachment);
82 }
83 
FPDFBitmapFromCFXDIBitmap(CFX_DIBitmap * bitmap)84 inline FPDF_BITMAP FPDFBitmapFromCFXDIBitmap(CFX_DIBitmap* bitmap) {
85   return reinterpret_cast<FPDF_BITMAP>(bitmap);
86 }
CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap)87 inline CFX_DIBitmap* CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
88   return reinterpret_cast<CFX_DIBitmap*>(bitmap);
89 }
90 
91 #if defined(PDF_USE_SKIA)
FPDFSkiaCanvasFromSkCanvas(SkCanvas * canvas)92 inline FPDF_SKIA_CANVAS FPDFSkiaCanvasFromSkCanvas(SkCanvas* canvas) {
93   return reinterpret_cast<FPDF_SKIA_CANVAS>(canvas);
94 }
SkCanvasFromFPDFSkiaCanvas(FPDF_SKIA_CANVAS canvas)95 inline SkCanvas* SkCanvasFromFPDFSkiaCanvas(FPDF_SKIA_CANVAS canvas) {
96   return reinterpret_cast<SkCanvas*>(canvas);
97 }
98 #endif
99 
FPDFBookmarkFromCPDFDictionary(const CPDF_Dictionary * bookmark)100 inline FPDF_BOOKMARK FPDFBookmarkFromCPDFDictionary(
101     const CPDF_Dictionary* bookmark) {
102   return reinterpret_cast<FPDF_BOOKMARK>(
103       const_cast<CPDF_Dictionary*>(bookmark));
104 }
CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark)105 inline CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark) {
106   return reinterpret_cast<CPDF_Dictionary*>(bookmark);
107 }
108 
FPDFClipPathFromCPDFClipPath(CPDF_ClipPath * path)109 inline FPDF_CLIPPATH FPDFClipPathFromCPDFClipPath(CPDF_ClipPath* path) {
110   return reinterpret_cast<FPDF_CLIPPATH>(path);
111 }
CPDFClipPathFromFPDFClipPath(FPDF_CLIPPATH path)112 inline CPDF_ClipPath* CPDFClipPathFromFPDFClipPath(FPDF_CLIPPATH path) {
113   return reinterpret_cast<CPDF_ClipPath*>(path);
114 }
115 
FPDFDestFromCPDFArray(const CPDF_Array * dest)116 inline FPDF_DEST FPDFDestFromCPDFArray(const CPDF_Array* dest) {
117   return reinterpret_cast<FPDF_DEST>(const_cast<CPDF_Array*>(dest));
118 }
CPDFArrayFromFPDFDest(FPDF_DEST dest)119 inline CPDF_Array* CPDFArrayFromFPDFDest(FPDF_DEST dest) {
120   return reinterpret_cast<CPDF_Array*>(dest);
121 }
122 
FPDFFontFromCPDFFont(CPDF_Font * font)123 inline FPDF_FONT FPDFFontFromCPDFFont(CPDF_Font* font) {
124   return reinterpret_cast<FPDF_FONT>(font);
125 }
CPDFFontFromFPDFFont(FPDF_FONT font)126 inline CPDF_Font* CPDFFontFromFPDFFont(FPDF_FONT font) {
127   return reinterpret_cast<CPDF_Font*>(font);
128 }
129 
FPDFJavaScriptActionFromCPDFJavaScriptAction(CPDF_JavaScript * javascript)130 inline FPDF_JAVASCRIPT_ACTION FPDFJavaScriptActionFromCPDFJavaScriptAction(
131     CPDF_JavaScript* javascript) {
132   return reinterpret_cast<FPDF_JAVASCRIPT_ACTION>(javascript);
133 }
CPDFJavaScriptActionFromFPDFJavaScriptAction(FPDF_JAVASCRIPT_ACTION javascript)134 inline CPDF_JavaScript* CPDFJavaScriptActionFromFPDFJavaScriptAction(
135     FPDF_JAVASCRIPT_ACTION javascript) {
136   return reinterpret_cast<CPDF_JavaScript*>(javascript);
137 }
138 
FPDFLinkFromCPDFDictionary(CPDF_Dictionary * link)139 inline FPDF_LINK FPDFLinkFromCPDFDictionary(CPDF_Dictionary* link) {
140   return reinterpret_cast<FPDF_LINK>(link);
141 }
CPDFDictionaryFromFPDFLink(FPDF_LINK link)142 inline CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link) {
143   return reinterpret_cast<CPDF_Dictionary*>(link);
144 }
145 
FPDFPageLinkFromCPDFLinkExtract(CPDF_LinkExtract * link)146 inline FPDF_PAGELINK FPDFPageLinkFromCPDFLinkExtract(CPDF_LinkExtract* link) {
147   return reinterpret_cast<FPDF_PAGELINK>(link);
148 }
CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link)149 inline CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) {
150   return reinterpret_cast<CPDF_LinkExtract*>(link);
151 }
152 
FPDFPageObjectFromCPDFPageObject(CPDF_PageObject * page_object)153 inline FPDF_PAGEOBJECT FPDFPageObjectFromCPDFPageObject(
154     CPDF_PageObject* page_object) {
155   return reinterpret_cast<FPDF_PAGEOBJECT>(page_object);
156 }
CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object)157 inline CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(
158     FPDF_PAGEOBJECT page_object) {
159   return reinterpret_cast<CPDF_PageObject*>(page_object);
160 }
161 
FPDFPageObjectMarkFromCPDFContentMarkItem(CPDF_ContentMarkItem * mark)162 inline FPDF_PAGEOBJECTMARK FPDFPageObjectMarkFromCPDFContentMarkItem(
163     CPDF_ContentMarkItem* mark) {
164   return reinterpret_cast<FPDF_PAGEOBJECTMARK>(mark);
165 }
CPDFContentMarkItemFromFPDFPageObjectMark(FPDF_PAGEOBJECTMARK mark)166 inline CPDF_ContentMarkItem* CPDFContentMarkItemFromFPDFPageObjectMark(
167     FPDF_PAGEOBJECTMARK mark) {
168   return reinterpret_cast<CPDF_ContentMarkItem*>(mark);
169 }
170 
FPDFPageRangeFromCPDFArray(const CPDF_Array * range)171 inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(const CPDF_Array* range) {
172   return reinterpret_cast<FPDF_PAGERANGE>(range);
173 }
CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range)174 inline const CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) {
175   return reinterpret_cast<const CPDF_Array*>(range);
176 }
177 
FPDFPathSegmentFromFXPathPoint(const CFX_Path::Point * segment)178 inline FPDF_PATHSEGMENT FPDFPathSegmentFromFXPathPoint(
179     const CFX_Path::Point* segment) {
180   return reinterpret_cast<FPDF_PATHSEGMENT>(segment);
181 }
FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment)182 inline const CFX_Path::Point* FXPathPointFromFPDFPathSegment(
183     FPDF_PATHSEGMENT segment) {
184   return reinterpret_cast<const CFX_Path::Point*>(segment);
185 }
186 
FPDFStructTreeFromCPDFStructTree(CPDF_StructTree * struct_tree)187 inline FPDF_STRUCTTREE FPDFStructTreeFromCPDFStructTree(
188     CPDF_StructTree* struct_tree) {
189   return reinterpret_cast<FPDF_STRUCTTREE>(struct_tree);
190 }
CPDFStructTreeFromFPDFStructTree(FPDF_STRUCTTREE struct_tree)191 inline CPDF_StructTree* CPDFStructTreeFromFPDFStructTree(
192     FPDF_STRUCTTREE struct_tree) {
193   return reinterpret_cast<CPDF_StructTree*>(struct_tree);
194 }
195 
FPDFStructElementFromCPDFStructElement(CPDF_StructElement * struct_element)196 inline FPDF_STRUCTELEMENT FPDFStructElementFromCPDFStructElement(
197     CPDF_StructElement* struct_element) {
198   return reinterpret_cast<FPDF_STRUCTELEMENT>(struct_element);
199 }
CPDFStructElementFromFPDFStructElement(FPDF_STRUCTELEMENT struct_element)200 inline CPDF_StructElement* CPDFStructElementFromFPDFStructElement(
201     FPDF_STRUCTELEMENT struct_element) {
202   return reinterpret_cast<CPDF_StructElement*>(struct_element);
203 }
204 
FPDFStructElementAttrFromCPDFDictionary(const CPDF_Dictionary * dictionary)205 inline FPDF_STRUCTELEMENT_ATTR FPDFStructElementAttrFromCPDFDictionary(
206     const CPDF_Dictionary* dictionary) {
207   return reinterpret_cast<FPDF_STRUCTELEMENT_ATTR>(dictionary);
208 }
CPDFDictionaryFromFPDFStructElementAttr(FPDF_STRUCTELEMENT_ATTR struct_element_attr)209 inline const CPDF_Dictionary* CPDFDictionaryFromFPDFStructElementAttr(
210     FPDF_STRUCTELEMENT_ATTR struct_element_attr) {
211   return reinterpret_cast<const CPDF_Dictionary*>(struct_element_attr);
212 }
213 
FPDFStructElementAttrValueFromCPDFObject(const CPDF_Object * object)214 inline FPDF_STRUCTELEMENT_ATTR_VALUE FPDFStructElementAttrValueFromCPDFObject(
215     const CPDF_Object* object) {
216   return reinterpret_cast<FPDF_STRUCTELEMENT_ATTR_VALUE>(object);
217 }
CPDFObjectFromFPDFStructElementAttrValue(FPDF_STRUCTELEMENT_ATTR_VALUE struct_element_attr_value)218 inline const CPDF_Object* CPDFObjectFromFPDFStructElementAttrValue(
219     FPDF_STRUCTELEMENT_ATTR_VALUE struct_element_attr_value) {
220   return reinterpret_cast<const CPDF_Object*>(struct_element_attr_value);
221 }
222 
FPDFTextPageFromCPDFTextPage(CPDF_TextPage * page)223 inline FPDF_TEXTPAGE FPDFTextPageFromCPDFTextPage(CPDF_TextPage* page) {
224   return reinterpret_cast<FPDF_TEXTPAGE>(page);
225 }
CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page)226 inline CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page) {
227   return reinterpret_cast<CPDF_TextPage*>(page);
228 }
229 
FPDFSchHandleFromCPDFTextPageFind(CPDF_TextPageFind * handle)230 inline FPDF_SCHHANDLE FPDFSchHandleFromCPDFTextPageFind(
231     CPDF_TextPageFind* handle) {
232   return reinterpret_cast<FPDF_SCHHANDLE>(handle);
233 }
CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle)234 inline CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(
235     FPDF_SCHHANDLE handle) {
236   return reinterpret_cast<CPDF_TextPageFind*>(handle);
237 }
238 
FPDFFormHandleFromCPDFSDKFormFillEnvironment(CPDFSDK_FormFillEnvironment * handle)239 inline FPDF_FORMHANDLE FPDFFormHandleFromCPDFSDKFormFillEnvironment(
240     CPDFSDK_FormFillEnvironment* handle) {
241   return reinterpret_cast<FPDF_FORMHANDLE>(handle);
242 }
243 inline CPDFSDK_FormFillEnvironment*
CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle)244 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle) {
245   return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
246 }
247 
FPDFSignatureFromCPDFDictionary(const CPDF_Dictionary * dictionary)248 inline FPDF_SIGNATURE FPDFSignatureFromCPDFDictionary(
249     const CPDF_Dictionary* dictionary) {
250   return reinterpret_cast<FPDF_SIGNATURE>(dictionary);
251 }
CPDFDictionaryFromFPDFSignature(FPDF_SIGNATURE signature)252 inline const CPDF_Dictionary* CPDFDictionaryFromFPDFSignature(
253     FPDF_SIGNATURE signature) {
254   return reinterpret_cast<const CPDF_Dictionary*>(signature);
255 }
256 
FPDFXObjectFromXObjectContext(XObjectContext * xobject)257 inline FPDF_XOBJECT FPDFXObjectFromXObjectContext(XObjectContext* xobject) {
258   return reinterpret_cast<FPDF_XOBJECT>(xobject);
259 }
260 
XObjectContextFromFPDFXObject(FPDF_XOBJECT xobject)261 inline XObjectContext* XObjectContextFromFPDFXObject(FPDF_XOBJECT xobject) {
262   return reinterpret_cast<XObjectContext*>(xobject);
263 }
264 
265 FXDIB_Format FXDIBFormatFromFPDFFormat(int format);
266 
267 CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
268 
269 UNSAFE_BUFFER_USAGE ByteString
270 ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
271 
272 UNSAFE_BUFFER_USAGE WideString
273 WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
274 
275 // Public APIs are not consistent w.r.t. the type used to represent buffer
276 // length, while internal code generally expects size_t. Use StrictNumeric here
277 // to make sure the length types fit in a size_t.
278 UNSAFE_BUFFER_USAGE pdfium::span<char> SpanFromFPDFApiArgs(
279     void* buffer,
280     pdfium::StrictNumeric<size_t> buflen);
281 
282 #ifdef PDF_ENABLE_XFA
283 // Layering prevents fxcrt from knowing about FPDF_FILEHANDLER, so this can't
284 // be a static method of IFX_SeekableStream.
285 RetainPtr<IFX_SeekableStream> MakeSeekableStream(
286     FPDF_FILEHANDLER* pFileHandler);
287 #endif  // PDF_ENABLE_XFA
288 
289 RetainPtr<const CPDF_Array> GetQuadPointsArrayFromDictionary(
290     const CPDF_Dictionary* dict);
291 RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
292     CPDF_Dictionary* dict);
293 RetainPtr<CPDF_Array> AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
294 bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
295 bool GetQuadPointsAtIndex(RetainPtr<const CPDF_Array> array,
296                           size_t quad_index,
297                           FS_QUADPOINTSF* quad_points);
298 
299 CFX_PointF CFXPointFFromFSPointF(const FS_POINTF& point);
300 
301 CFX_FloatRect CFXFloatRectFromFSRectF(const FS_RECTF& rect);
302 FS_RECTF FSRectFFromCFXFloatRect(const CFX_FloatRect& rect);
303 
304 CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix);
305 FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix& matrix);
306 
307 unsigned long NulTerminateMaybeCopyAndReturnLength(
308     const ByteString& text,
309     pdfium::span<char> result_span);
310 
311 unsigned long Utf16EncodeMaybeCopyAndReturnLength(
312     const WideString& text,
313     pdfium::span<char> result_span);
314 
315 // Returns the length of the raw stream data from |stream|. The raw data is the
316 // stream's data as stored in the PDF without applying any filters. If |buffer|
317 // is non-empty and its length is large enough to contain the raw data, then
318 // the raw data is copied into |buffer|.
319 unsigned long GetRawStreamMaybeCopyAndReturnLength(
320     RetainPtr<const CPDF_Stream> stream,
321     pdfium::span<uint8_t> buffer);
322 
323 // Return the length of the decoded stream data of |stream|. The decoded data is
324 // the uncompressed stream data, i.e. the raw stream data after having all
325 // filters applied. If |buffer| is non-empty and its length is large enough to
326 // contain the decoded data, then the decoded data is copied into |buffer|.
327 unsigned long DecodeStreamMaybeCopyAndReturnLength(
328     RetainPtr<const CPDF_Stream> stream,
329     pdfium::span<uint8_t> buffer);
330 
331 void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable);
332 FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy);
333 
334 void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info);
335 void ReportUnsupportedFeatures(const CPDF_Document* pDoc);
336 void ReportUnsupportedXFA(const CPDF_Document* pDoc);
337 void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot);
338 void ProcessParseError(CPDF_Parser::Error err);
339 void SetColorFromScheme(const FPDF_COLORSCHEME* pColorScheme,
340                         CPDF_RenderOptions* pRenderOptions);
341 
342 // Returns a vector of page indices given a page range string.
343 std::vector<uint32_t> ParsePageRangeString(const ByteString& bsPageRange,
344                                            uint32_t nCount);
345 
346 #endif  // FPDFSDK_CPDFSDK_HELPERS_H_
347