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 "public/fpdf_doc.h"
8
9 #include <memory>
10 #include <set>
11
12 #include "core/fpdfapi/page/cpdf_page.h"
13 #include "core/fpdfapi/parser/cpdf_array.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fpdfdoc/cpdf_bookmark.h"
16 #include "core/fpdfdoc/cpdf_bookmarktree.h"
17 #include "core/fpdfdoc/cpdf_dest.h"
18 #include "core/fpdfdoc/cpdf_pagelabel.h"
19 #include "fpdfsdk/fsdk_define.h"
20 #include "third_party/base/ptr_util.h"
21 #include "third_party/base/stl_util.h"
22
23 namespace {
24
FindBookmark(const CPDF_BookmarkTree & tree,CPDF_Bookmark bookmark,const CFX_WideString & title,std::set<CPDF_Dictionary * > * visited)25 CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree,
26 CPDF_Bookmark bookmark,
27 const CFX_WideString& title,
28 std::set<CPDF_Dictionary*>* visited) {
29 // Return if already checked to avoid circular calling.
30 if (pdfium::ContainsKey(*visited, bookmark.GetDict()))
31 return CPDF_Bookmark();
32 visited->insert(bookmark.GetDict());
33
34 if (bookmark.GetDict() &&
35 bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) {
36 // First check this item.
37 return bookmark;
38 }
39
40 // Go into children items.
41 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
42 while (child.GetDict() && !pdfium::ContainsKey(*visited, child.GetDict())) {
43 // Check this item and its children.
44 CPDF_Bookmark found = FindBookmark(tree, child, title, visited);
45 if (found.GetDict())
46 return found;
47 child = tree.GetNextSibling(child);
48 }
49 return CPDF_Bookmark();
50 }
51
GetLinkList(CPDF_Page * page)52 CPDF_LinkList* GetLinkList(CPDF_Page* page) {
53 if (!page)
54 return nullptr;
55
56 CPDF_Document* pDoc = page->m_pDocument;
57 std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext();
58 if (!pHolder->get())
59 *pHolder = pdfium::MakeUnique<CPDF_LinkList>();
60 return pHolder->get();
61 }
62
Utf16EncodeMaybeCopyAndReturnLength(const CFX_WideString & text,void * buffer,unsigned long buflen)63 unsigned long Utf16EncodeMaybeCopyAndReturnLength(const CFX_WideString& text,
64 void* buffer,
65 unsigned long buflen) {
66 CFX_ByteString encodedText = text.UTF16LE_Encode();
67 unsigned long len = encodedText.GetLength();
68 if (buffer && len <= buflen)
69 FXSYS_memcpy(buffer, encodedText.c_str(), len);
70 return len;
71 }
72
73 } // namespace
74
75 DLLEXPORT FPDF_BOOKMARK STDCALL
FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document,FPDF_BOOKMARK pDict)76 FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
77 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
78 if (!pDoc)
79 return nullptr;
80 CPDF_BookmarkTree tree(pDoc);
81 CPDF_Bookmark bookmark =
82 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
83 return tree.GetFirstChild(bookmark).GetDict();
84 }
85
86 DLLEXPORT FPDF_BOOKMARK STDCALL
FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document,FPDF_BOOKMARK pDict)87 FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
88 if (!pDict)
89 return nullptr;
90 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
91 if (!pDoc)
92 return nullptr;
93 CPDF_BookmarkTree tree(pDoc);
94 CPDF_Bookmark bookmark =
95 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
96 return tree.GetNextSibling(bookmark).GetDict();
97 }
98
FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,void * buffer,unsigned long buflen)99 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,
100 void* buffer,
101 unsigned long buflen) {
102 if (!pDict)
103 return 0;
104 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
105 CFX_WideString title = bookmark.GetTitle();
106 return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen);
107 }
108
FPDFBookmark_Find(FPDF_DOCUMENT document,FPDF_WIDESTRING title)109 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document,
110 FPDF_WIDESTRING title) {
111 if (!title || title[0] == 0)
112 return nullptr;
113 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
114 if (!pDoc)
115 return nullptr;
116 CPDF_BookmarkTree tree(pDoc);
117 FX_STRSIZE len = CFX_WideString::WStringLength(title);
118 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
119 std::set<CPDF_Dictionary*> visited;
120 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict();
121 }
122
FPDFBookmark_GetDest(FPDF_DOCUMENT document,FPDF_BOOKMARK pDict)123 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document,
124 FPDF_BOOKMARK pDict) {
125 if (!pDict)
126 return nullptr;
127 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
128 if (!pDoc)
129 return nullptr;
130 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
131 CPDF_Dest dest = bookmark.GetDest(pDoc);
132 if (dest.GetObject())
133 return dest.GetObject();
134 // If this bookmark is not directly associated with a dest, we try to get
135 // action
136 CPDF_Action action = bookmark.GetAction();
137 if (!action.GetDict())
138 return nullptr;
139 return action.GetDest(pDoc).GetObject();
140 }
141
FPDFBookmark_GetAction(FPDF_BOOKMARK pDict)142 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) {
143 if (!pDict)
144 return nullptr;
145 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
146 return bookmark.GetAction().GetDict();
147 }
148
FPDFAction_GetType(FPDF_ACTION pDict)149 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) {
150 if (!pDict)
151 return PDFACTION_UNSUPPORTED;
152
153 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
154 CPDF_Action::ActionType type = action.GetType();
155 switch (type) {
156 case CPDF_Action::GoTo:
157 return PDFACTION_GOTO;
158 case CPDF_Action::GoToR:
159 return PDFACTION_REMOTEGOTO;
160 case CPDF_Action::URI:
161 return PDFACTION_URI;
162 case CPDF_Action::Launch:
163 return PDFACTION_LAUNCH;
164 default:
165 return PDFACTION_UNSUPPORTED;
166 }
167 }
168
FPDFAction_GetDest(FPDF_DOCUMENT document,FPDF_ACTION pDict)169 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document,
170 FPDF_ACTION pDict) {
171 if (!pDict)
172 return nullptr;
173 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
174 if (!pDoc)
175 return nullptr;
176 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
177 return action.GetDest(pDoc).GetObject();
178 }
179
FPDFAction_GetFilePath(FPDF_ACTION pDict,void * buffer,unsigned long buflen)180 DLLEXPORT unsigned long STDCALL FPDFAction_GetFilePath(FPDF_ACTION pDict,
181 void* buffer,
182 unsigned long buflen) {
183 unsigned long type = FPDFAction_GetType(pDict);
184 if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
185 return 0;
186
187 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
188 CFX_ByteString path = action.GetFilePath().UTF8Encode();
189 unsigned long len = path.GetLength() + 1;
190 if (buffer && len <= buflen)
191 FXSYS_memcpy(buffer, path.c_str(), len);
192 return len;
193 }
194
FPDFAction_GetURIPath(FPDF_DOCUMENT document,FPDF_ACTION pDict,void * buffer,unsigned long buflen)195 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document,
196 FPDF_ACTION pDict,
197 void* buffer,
198 unsigned long buflen) {
199 if (!pDict)
200 return 0;
201 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
202 if (!pDoc)
203 return 0;
204 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
205 CFX_ByteString path = action.GetURI(pDoc);
206 unsigned long len = path.GetLength() + 1;
207 if (buffer && len <= buflen)
208 FXSYS_memcpy(buffer, path.c_str(), len);
209 return len;
210 }
211
FPDFDest_GetPageIndex(FPDF_DOCUMENT document,FPDF_DEST pDict)212 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
213 FPDF_DEST pDict) {
214 if (!pDict)
215 return 0;
216 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
217 if (!pDoc)
218 return 0;
219 CPDF_Dest dest(static_cast<CPDF_Array*>(pDict));
220 return dest.GetPageIndex(pDoc);
221 }
222
FPDFDest_GetLocationInPage(FPDF_DEST pDict,FPDF_BOOL * hasXVal,FPDF_BOOL * hasYVal,FPDF_BOOL * hasZoomVal,FS_FLOAT * x,FS_FLOAT * y,FS_FLOAT * zoom)223 DLLEXPORT FPDF_BOOL STDCALL FPDFDest_GetLocationInPage(FPDF_DEST pDict,
224 FPDF_BOOL* hasXVal,
225 FPDF_BOOL* hasYVal,
226 FPDF_BOOL* hasZoomVal,
227 FS_FLOAT* x,
228 FS_FLOAT* y,
229 FS_FLOAT* zoom) {
230 if (!pDict)
231 return false;
232
233 std::unique_ptr<CPDF_Dest> dest(
234 new CPDF_Dest(static_cast<CPDF_Object*>(pDict)));
235
236 // FPDF_BOOL is an int, GetXYZ expects bools.
237 bool bHasX;
238 bool bHasY;
239 bool bHasZoom;
240 if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom))
241 return false;
242
243 *hasXVal = bHasX;
244 *hasYVal = bHasY;
245 *hasZoomVal = bHasZoom;
246 return true;
247 }
248
FPDFLink_GetLinkAtPoint(FPDF_PAGE page,double x,double y)249 DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page,
250 double x,
251 double y) {
252 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
253 if (!pPage)
254 return nullptr;
255
256 CPDF_LinkList* pLinkList = GetLinkList(pPage);
257 if (!pLinkList)
258 return nullptr;
259
260 return pLinkList
261 ->GetLinkAtPoint(
262 pPage, CFX_PointF(static_cast<FX_FLOAT>(x), static_cast<FX_FLOAT>(y)),
263 nullptr)
264 .GetDict();
265 }
266
FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page,double x,double y)267 DLLEXPORT int STDCALL FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page,
268 double x,
269 double y) {
270 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
271 if (!pPage)
272 return -1;
273
274 CPDF_LinkList* pLinkList = GetLinkList(pPage);
275 if (!pLinkList)
276 return -1;
277
278 int z_order = -1;
279 pLinkList->GetLinkAtPoint(
280 pPage, CFX_PointF(static_cast<FX_FLOAT>(x), static_cast<FX_FLOAT>(y)),
281 &z_order);
282 return z_order;
283 }
284
FPDFLink_GetDest(FPDF_DOCUMENT document,FPDF_LINK pDict)285 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document,
286 FPDF_LINK pDict) {
287 if (!pDict)
288 return nullptr;
289 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
290 if (!pDoc)
291 return nullptr;
292 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
293 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
294 if (dest)
295 return dest;
296 // If this link is not directly associated with a dest, we try to get action
297 CPDF_Action action = link.GetAction();
298 if (!action.GetDict())
299 return nullptr;
300 return action.GetDest(pDoc).GetObject();
301 }
302
FPDFLink_GetAction(FPDF_LINK pDict)303 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
304 if (!pDict)
305 return nullptr;
306
307 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
308 return link.GetAction().GetDict();
309 }
310
FPDFLink_Enumerate(FPDF_PAGE page,int * startPos,FPDF_LINK * linkAnnot)311 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
312 int* startPos,
313 FPDF_LINK* linkAnnot) {
314 if (!startPos || !linkAnnot)
315 return false;
316 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
317 if (!pPage || !pPage->m_pFormDict)
318 return false;
319 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
320 if (!pAnnots)
321 return false;
322 for (size_t i = *startPos; i < pAnnots->GetCount(); i++) {
323 CPDF_Dictionary* pDict =
324 ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i)));
325 if (!pDict)
326 continue;
327 if (pDict->GetStringFor("Subtype") == "Link") {
328 *startPos = static_cast<int>(i + 1);
329 *linkAnnot = static_cast<FPDF_LINK>(pDict);
330 return true;
331 }
332 }
333 return false;
334 }
335
FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,FS_RECTF * rect)336 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
337 FS_RECTF* rect) {
338 if (!linkAnnot || !rect)
339 return false;
340 CPDF_Dictionary* pAnnotDict =
341 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
342 CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
343 rect->left = rt.left;
344 rect->bottom = rt.bottom;
345 rect->right = rt.right;
346 rect->top = rt.top;
347 return true;
348 }
349
FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)350 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
351 if (!linkAnnot)
352 return 0;
353 CPDF_Dictionary* pAnnotDict =
354 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
355 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
356 if (!pArray)
357 return 0;
358 return static_cast<int>(pArray->GetCount() / 8);
359 }
360
FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,int quadIndex,FS_QUADPOINTSF * quadPoints)361 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
362 int quadIndex,
363 FS_QUADPOINTSF* quadPoints) {
364 if (!linkAnnot || !quadPoints)
365 return false;
366 CPDF_Dictionary* pAnnotDict =
367 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
368 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
369 if (!pArray)
370 return false;
371
372 if (quadIndex < 0 ||
373 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 ||
374 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) {
375 return false;
376 }
377
378 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8);
379 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1);
380 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
381 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
382 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
383 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
384 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
385 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
386 return true;
387 }
388
FPDF_GetMetaText(FPDF_DOCUMENT document,FPDF_BYTESTRING tag,void * buffer,unsigned long buflen)389 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT document,
390 FPDF_BYTESTRING tag,
391 void* buffer,
392 unsigned long buflen) {
393 if (!tag)
394 return 0;
395 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
396 if (!pDoc)
397 return 0;
398 CPDF_Dictionary* pInfo = pDoc->GetInfo();
399 if (!pInfo)
400 return 0;
401 CFX_WideString text = pInfo->GetUnicodeTextFor(tag);
402 return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen);
403 }
404
FPDF_GetPageLabel(FPDF_DOCUMENT document,int page_index,void * buffer,unsigned long buflen)405 DLLEXPORT unsigned long STDCALL FPDF_GetPageLabel(FPDF_DOCUMENT document,
406 int page_index,
407 void* buffer,
408 unsigned long buflen) {
409 if (page_index < 0)
410 return 0;
411
412 // CPDF_PageLabel can deal with NULL |document|.
413 CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document));
414 CFX_WideString str;
415 if (!label.GetLabel(page_index, &str))
416 return 0;
417 return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen);
418 }
419