Re-land else-after-returns
[pdfium.git] / fpdfsdk / src / fpdfppo.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 "../../public/fpdf_ppo.h"
8 #include "../../third_party/base/nonstd_unique_ptr.h"
9 #include "../include/fsdk_define.h"
10
11 class CPDF_PageOrganizer
12 {
13 public:
14     using ObjectNumberMap = std::map<FX_DWORD, FX_DWORD>;
15     CPDF_PageOrganizer();
16     ~CPDF_PageOrganizer();
17
18     FX_BOOL PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Document* pSrcPDFDoc);
19     FX_BOOL ExportPage(CPDF_Document* pSrcPDFDoc,
20                        CFX_WordArray* nPageNum,
21                        CPDF_Document* pDestPDFDoc,
22                        int nIndex);
23     CPDF_Object* PageDictGetInheritableTag(CPDF_Dictionary* pDict,
24                                            CFX_ByteString nSrctag);
25     FX_BOOL UpdateReference(CPDF_Object* pObj,
26                             CPDF_Document* pDoc,
27                             ObjectNumberMap* pObjNumberMap);
28     FX_DWORD GetNewObjId(CPDF_Document* pDoc,
29                          ObjectNumberMap* pObjNumberMap,
30                          CPDF_Reference* pRef);
31 };
32
33
34 CPDF_PageOrganizer::CPDF_PageOrganizer()
35 {
36 }
37
38 CPDF_PageOrganizer::~CPDF_PageOrganizer()
39 {
40 }
41
42 FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc,
43                                        CPDF_Document* pSrcPDFDoc)
44 {
45     if (!pDestPDFDoc || !pSrcPDFDoc)
46         return FALSE;
47
48     CPDF_Dictionary* pNewRoot = pDestPDFDoc->GetRoot();
49     if (!pNewRoot)
50         return FALSE;
51
52     //Set the document information////////////////////////////////////////////
53
54     CPDF_Dictionary* DInfoDict = pDestPDFDoc->GetInfo();
55     if (!DInfoDict)
56         return FALSE;
57
58     CFX_ByteString producerstr;
59     producerstr.Format("PDFium");
60     DInfoDict->SetAt("Producer", new CPDF_String(producerstr));
61
62     //Set type////////////////////////////////////////////////////////////////
63     CFX_ByteString cbRootType = pNewRoot->GetString("Type", "");
64     if (cbRootType.Equal("") ) {
65         pNewRoot->SetAt("Type", new CPDF_Name("Catalog"));
66     }
67
68     CPDF_Object* pElement = pNewRoot->GetElement("Pages");
69     CPDF_Dictionary* pNewPages = pElement ?
70         (CPDF_Dictionary*)pElement->GetDirect() : nullptr;
71     if (!pNewPages) {
72         pNewPages = new CPDF_Dictionary;
73         FX_DWORD NewPagesON = pDestPDFDoc->AddIndirectObject(pNewPages);
74         pNewRoot->SetAt("Pages", new CPDF_Reference(pDestPDFDoc, NewPagesON));
75     }
76
77     CFX_ByteString cbPageType = pNewPages->GetString("Type", "");
78     if (cbPageType.Equal("")) {
79         pNewPages->SetAt("Type", new CPDF_Name("Pages"));
80     }
81
82     CPDF_Array* pKeysArray = pNewPages->GetArray("Kids");
83     if (!pKeysArray) {
84         CPDF_Array* pNewKids = new CPDF_Array;
85         FX_DWORD Kidsobjnum = -1;
86         Kidsobjnum = pDestPDFDoc->AddIndirectObject(pNewKids);
87
88         pNewPages->SetAt("Kids", new CPDF_Reference(pDestPDFDoc, Kidsobjnum));
89         pNewPages->SetAt("Count", new CPDF_Number(0));
90     }
91
92     return TRUE;
93 }
94
95 FX_BOOL CPDF_PageOrganizer::ExportPage(CPDF_Document* pSrcPDFDoc,
96                                        CFX_WordArray* nPageNum,
97                                        CPDF_Document* pDestPDFDoc,
98                                        int nIndex)
99 {
100     int curpage = nIndex;
101
102     nonstd::unique_ptr<ObjectNumberMap> pObjNumberMap(new ObjectNumberMap);
103
104     for (int i = 0; i < nPageNum->GetSize(); ++i) {
105         CPDF_Dictionary* pCurPageDict = pDestPDFDoc->CreateNewPage(curpage);
106         CPDF_Dictionary* pSrcPageDict =
107             pSrcPDFDoc->GetPage(nPageNum->GetAt(i) - 1);
108         if (!pSrcPageDict || !pCurPageDict)
109             return FALSE;
110
111         // Clone the page dictionary///////////
112         FX_POSITION SrcPos = pSrcPageDict->GetStartPos();
113         while (SrcPos) {
114             CFX_ByteString cbSrcKeyStr;
115             CPDF_Object* pObj = pSrcPageDict->GetNextElement(SrcPos,
116                                                              cbSrcKeyStr);
117             if (cbSrcKeyStr.Compare(("Type")) &&
118                 cbSrcKeyStr.Compare(("Parent"))) {
119                 if (pCurPageDict->KeyExist(cbSrcKeyStr))
120                     pCurPageDict->RemoveAt(cbSrcKeyStr);
121                 pCurPageDict->SetAt(cbSrcKeyStr, pObj->Clone());
122             }
123         }
124
125         //inheritable item///////////////////////
126         CPDF_Object* pInheritable = nullptr;
127         //1 MediaBox  //required
128         if (!pCurPageDict->KeyExist("MediaBox")) {
129             pInheritable = PageDictGetInheritableTag(pSrcPageDict, "MediaBox");
130             if (!pInheritable) {
131                 // Search the "CropBox" from source page dictionary,
132                 // if not exists,we take the letter size.
133                 pInheritable = PageDictGetInheritableTag(pSrcPageDict,
134                                                          "CropBox");
135                 if (pInheritable) {
136                     pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
137                 } else {
138                     // Make the default size to be letter size (8.5'x11')
139                     CPDF_Array* pArray = new CPDF_Array;
140                     pArray->AddNumber(0);
141                     pArray->AddNumber(0);
142                     pArray->AddNumber(612);
143                     pArray->AddNumber(792);
144                     pCurPageDict->SetAt("MediaBox", pArray);
145                 }
146             } else {
147                 pCurPageDict->SetAt("MediaBox", pInheritable->Clone());
148             }
149         }
150         //2 Resources //required
151         if (!pCurPageDict->KeyExist("Resources")) {
152             pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Resources");
153             if (!pInheritable)
154                 return FALSE;
155             pCurPageDict->SetAt("Resources", pInheritable->Clone());
156         }
157         //3 CropBox  //Optional
158         if (!pCurPageDict->KeyExist("CropBox")) {
159             pInheritable = PageDictGetInheritableTag(pSrcPageDict, "CropBox");
160             if (pInheritable)
161                 pCurPageDict->SetAt("CropBox", pInheritable->Clone());
162         }
163         //4 Rotate  //Optional
164         if (!pCurPageDict->KeyExist("Rotate")) {
165             pInheritable = PageDictGetInheritableTag(pSrcPageDict, "Rotate");
166             if (pInheritable)
167                 pCurPageDict->SetAt("Rotate", pInheritable->Clone());
168         }
169
170         /////////////////////////////////////////////
171         //Update the reference
172         FX_DWORD dwOldPageObj = pSrcPageDict->GetObjNum();
173         FX_DWORD dwNewPageObj = pCurPageDict->GetObjNum();
174
175         (*pObjNumberMap)[dwOldPageObj] = dwNewPageObj;
176
177         UpdateReference(pCurPageDict, pDestPDFDoc, pObjNumberMap.get());
178         ++curpage;
179     }
180
181     return TRUE;
182 }
183
184 CPDF_Object* CPDF_PageOrganizer::PageDictGetInheritableTag(
185     CPDF_Dictionary* pDict,
186     CFX_ByteString nSrctag)
187 {
188     if (!pDict || nSrctag.IsEmpty())
189         return nullptr;
190     if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
191         return nullptr;
192
193     CPDF_Object* pType = pDict->GetElement("Type")->GetDirect();
194     if (!pType || pType->GetType() != PDFOBJ_NAME)
195         return nullptr;
196     if (pType->GetString().Compare("Page"))
197         return nullptr;
198
199     CPDF_Object* pParent = pDict->GetElement("Parent")->GetDirect();
200     if (!pParent || pParent->GetType() != PDFOBJ_DICTIONARY)
201         return nullptr;
202
203     if (pDict->KeyExist((const char*)nSrctag))
204         return pDict->GetElement((const char*)nSrctag);
205
206     CPDF_Dictionary* pp = (CPDF_Dictionary*)pParent;
207     while (pp) {
208         if (pp->KeyExist((const char*)nSrctag)) {
209             return pp->GetElement((const char*)nSrctag);
210         }
211         if (!pp->KeyExist("Parent")) {
212             break;
213         }
214         pp = (CPDF_Dictionary*)pp->GetElement("Parent")->GetDirect();
215         if (pp->GetType() == PDFOBJ_NULL) {
216             break;
217         }
218     }
219
220     return nullptr;
221 }
222
223 FX_BOOL CPDF_PageOrganizer::UpdateReference(CPDF_Object* pObj,
224                                             CPDF_Document* pDoc,
225                                             ObjectNumberMap* pObjNumberMap)
226 {
227     switch (pObj->GetType()) {
228         case PDFOBJ_REFERENCE: {
229             CPDF_Reference* pReference = (CPDF_Reference*)pObj;
230             FX_DWORD newobjnum = GetNewObjId(pDoc, pObjNumberMap, pReference);
231             if (newobjnum == 0)
232                 return FALSE;
233             pReference->SetRef(pDoc, newobjnum);
234             break;
235         }
236         case PDFOBJ_DICTIONARY: {
237             CPDF_Dictionary* pDict = (CPDF_Dictionary*)pObj;
238
239             FX_POSITION pos = pDict->GetStartPos();
240             while (pos) {
241                 CFX_ByteString key("");
242                 CPDF_Object* pNextObj = pDict->GetNextElement(pos, key);
243                 if (!FXSYS_strcmp(key, "Parent") ||
244                     !FXSYS_strcmp(key, "Prev") ||
245                     !FXSYS_strcmp(key, "First")) {
246                     continue;
247                 }
248                 if (pNextObj) {
249                     if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
250                       pDict->RemoveAt(key);
251                 } else {
252                     return FALSE;
253                 }
254           }
255           break;
256         }
257         case PDFOBJ_ARRAY: {
258             CPDF_Array* pArray = (CPDF_Array*)pObj;
259             FX_DWORD count = pArray->GetCount();
260             for (FX_DWORD i = 0; i < count; ++i) {
261                 CPDF_Object* pNextObj = pArray->GetElement(i);
262                 if (!pNextObj)
263                     return FALSE;
264                 if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
265                     return FALSE;
266             }
267             break;
268         }
269         case PDFOBJ_STREAM: {
270             CPDF_Stream* pStream = (CPDF_Stream*)pObj;
271             CPDF_Dictionary* pDict = pStream->GetDict();
272             if (pDict) {
273                 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
274                     return FALSE;
275             } else {
276                 return FALSE;
277             }
278             break;
279         }
280         default:
281             break;
282     }
283
284     return TRUE;
285 }
286
287 FX_DWORD CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
288                                          ObjectNumberMap* pObjNumberMap,
289                                          CPDF_Reference* pRef)
290 {
291     if (!pRef)
292         return 0;
293
294     FX_DWORD dwObjnum = pRef->GetRefObjNum();
295     FX_DWORD dwNewObjNum = 0;
296     const auto it = pObjNumberMap->find(dwObjnum);
297     if (it != pObjNumberMap->end())
298         dwNewObjNum = it->second;
299     if (dwNewObjNum)
300         return dwNewObjNum;
301
302     CPDF_Object* pDirect = pRef->GetDirect();
303     if (!pDirect)
304         return 0;
305
306     CPDF_Object* pClone = pDirect->Clone();
307     if (!pClone)
308         return 0;
309
310     if (pClone->GetType() == PDFOBJ_DICTIONARY) {
311         CPDF_Dictionary* pDictClone = (CPDF_Dictionary*)pClone;
312         if (pDictClone->KeyExist("Type")) {
313             CFX_ByteString strType = pDictClone->GetString("Type");
314             if (!FXSYS_stricmp(strType, "Pages")) {
315                 pDictClone->Release();
316                 return 4;
317             }
318             if (!FXSYS_stricmp(strType, "Page")) {
319                 pDictClone->Release();
320                 return  0;
321             }
322         }
323     }
324     dwNewObjNum = pDoc->AddIndirectObject(pClone);
325     (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
326
327     if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
328         pClone->Release();
329         return 0;
330     }
331     return dwNewObjNum;
332 }
333
334 FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
335                                 CFX_WordArray* pageArray,
336                                 int nCount)
337 {
338     if (rangstring.GetLength() != 0) {
339         rangstring.Remove(' ');
340         int nLength = rangstring.GetLength();
341         CFX_ByteString cbCompareString("0123456789-,");
342         for (int i = 0; i < nLength; ++i) {
343             if (cbCompareString.Find(rangstring[i]) == -1)
344                 return FALSE;
345         }
346         CFX_ByteString cbMidRange;
347         int nStringFrom = 0;
348         int nStringTo = 0;
349         while (nStringTo < nLength) {
350             nStringTo = rangstring.Find(',', nStringFrom);
351             if (nStringTo == -1)
352                 nStringTo = nLength;
353             cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
354             int nMid = cbMidRange.Find('-');
355             if (nMid == -1) {
356                 long lPageNum = atol(cbMidRange);
357                 if (lPageNum <= 0 || lPageNum > nCount)
358                     return FALSE;
359                 pageArray->Add((FX_WORD)lPageNum);
360             } else {
361                 int nStartPageNum = atol(cbMidRange.Mid(0, nMid));
362                 if (nStartPageNum == 0)
363                     return FALSE;
364
365                 ++nMid;
366                 int nEnd = cbMidRange.GetLength() - nMid;
367                 if (nEnd == 0)
368                     return FALSE;
369
370                 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd));
371                 if (nStartPageNum < 0 ||
372                     nStartPageNum >nEndPageNum ||
373                     nEndPageNum > nCount) {
374                     return FALSE;
375                 }
376                 for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
377                     pageArray->Add(i);
378                 }
379             }
380             nStringFrom = nStringTo + 1;
381         }
382     }
383     return TRUE;
384 }
385
386 DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
387                                              FPDF_DOCUMENT src_doc,
388                                              FPDF_BYTESTRING pagerange,
389                                              int index)
390 {
391     if (!dest_doc || !src_doc)
392         return FALSE;
393
394     CFX_WordArray pageArray;
395     CPDF_Document* pSrcDoc = (CPDF_Document*)src_doc;
396     int nCount = pSrcDoc->GetPageCount();
397     if (pagerange) {
398         if (!ParserPageRangeString(pagerange,&pageArray,nCount))
399             return FALSE;
400     } else {
401         for (int i = 1; i <= nCount; ++i) {
402             pageArray.Add(i);
403         }
404     }
405
406     CPDF_Document* pDestDoc = (CPDF_Document*)dest_doc;
407     CPDF_PageOrganizer pageOrg;
408
409     pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
410
411     return pageOrg.ExportPage(pSrcDoc,&pageArray,pDestDoc,index);
412 }
413
414 DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
415                                                        FPDF_DOCUMENT src_doc)
416 {
417     if (!src_doc || !dest_doc)
418         return false;
419
420     CPDF_Document* pSrcDoc = (CPDF_Document*)src_doc;
421     CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
422     pSrcDict = pSrcDict->GetDict(FX_BSTRC("ViewerPreferences"));;
423     if (!pSrcDict)
424         return FALSE;
425
426     CPDF_Document* pDstDoc = (CPDF_Document*)dest_doc;
427     CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
428     if (!pDstDict)
429         return FALSE;
430     pDstDict->SetAt(FX_BSTRC("ViewerPreferences"), pSrcDict->Clone(TRUE));
431     return TRUE;
432 }