Revert "Fix else-after-returns throughout pdfium."
[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 || !pDict->KeyExist("Type") || nSrctag.IsEmpty())
189         return nullptr;
190     if (!pDict->KeyExist("Parent"))
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     CPDF_Dictionary* pp = (CPDF_Dictionary*)pParent;
204
205     if (pDict->KeyExist((const char*)nSrctag))
206         return pDict->GetElement((const char*)nSrctag);
207
208     while (pp) {
209         if (pp->KeyExist((const char*)nSrctag)) {
210             return pp->GetElement((const char*)nSrctag);
211         } else if (pp->KeyExist("Parent")) {
212             pp = (CPDF_Dictionary*)pp->GetElement("Parent")->GetDirect();
213             if (pp->GetType() == PDFOBJ_NULL)
214                 break;
215         } else {
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                     if (!UpdateReference(pNextObj, pDoc, pObjNumberMap))
264                         return FALSE;
265                 } else {
266                     return FALSE;
267                 }
268             }
269             break;
270         }
271         case PDFOBJ_STREAM: {
272             CPDF_Stream* pStream = (CPDF_Stream*)pObj;
273             CPDF_Dictionary* pDict = pStream->GetDict();
274             if (pDict) {
275                 if (!UpdateReference(pDict, pDoc, pObjNumberMap))
276                     return FALSE;
277             } else {
278                 return FALSE;
279             }
280             break;
281         }
282         default:
283             break;
284     }
285
286     return TRUE;
287 }
288
289 FX_DWORD CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc,
290                                          ObjectNumberMap* pObjNumberMap,
291                                          CPDF_Reference* pRef)
292 {
293     if (!pRef)
294         return 0;
295
296     FX_DWORD dwObjnum = pRef->GetRefObjNum();
297     FX_DWORD dwNewObjNum = 0;
298     const auto it = pObjNumberMap->find(dwObjnum);
299     if (it != pObjNumberMap->end())
300         dwNewObjNum = it->second;
301     if (dwNewObjNum)
302         return dwNewObjNum;
303
304     CPDF_Object* pDirect = pRef->GetDirect();
305     if (!pDirect)
306         return 0;
307
308     CPDF_Object* pClone = pDirect->Clone();
309     if (!pClone)
310         return 0;
311
312     if (pClone->GetType() == PDFOBJ_DICTIONARY) {
313         CPDF_Dictionary* pDictClone = (CPDF_Dictionary*)pClone;
314         if (pDictClone->KeyExist("Type")) {
315             CFX_ByteString strType = pDictClone->GetString("Type");
316             if (!FXSYS_stricmp(strType, "Pages")) {
317                 pDictClone->Release();
318                 return 4;
319             } else if (!FXSYS_stricmp(strType, "Page")) {
320                 pDictClone->Release();
321                 return  0;
322             }
323         }
324     }
325     dwNewObjNum = pDoc->AddIndirectObject(pClone);
326     (*pObjNumberMap)[dwObjnum] = dwNewObjNum;
327
328     if (!UpdateReference(pClone, pDoc, pObjNumberMap)) {
329         pClone->Release();
330         return 0;
331     }
332     return dwNewObjNum;
333 }
334
335 FPDF_BOOL ParserPageRangeString(CFX_ByteString rangstring,
336                                 CFX_WordArray* pageArray,
337                                 int nCount)
338 {
339     if (rangstring.GetLength() != 0) {
340         rangstring.Remove(' ');
341         int nLength = rangstring.GetLength();
342         CFX_ByteString cbCompareString("0123456789-,");
343         for (int i = 0; i < nLength; ++i) {
344             if (cbCompareString.Find(rangstring[i]) == -1)
345                 return FALSE;
346         }
347         CFX_ByteString cbMidRange;
348         int nStringFrom = 0;
349         int nStringTo = 0;
350         while (nStringTo < nLength) {
351             nStringTo = rangstring.Find(',', nStringFrom);
352             if (nStringTo == -1)
353                 nStringTo = nLength;
354             cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom);
355             int nMid = cbMidRange.Find('-');
356             if (nMid == -1) {
357                 long lPageNum = atol(cbMidRange);
358                 if (lPageNum <= 0 || lPageNum > nCount)
359                     return FALSE;
360                 pageArray->Add((FX_WORD)lPageNum);
361             } else {
362                 int nStartPageNum = atol(cbMidRange.Mid(0, nMid));
363                 if (nStartPageNum == 0)
364                     return FALSE;
365
366                 ++nMid;
367                 int nEnd = cbMidRange.GetLength() - nMid;
368                 if (nEnd == 0)
369                     return FALSE;
370
371                 int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd));
372                 if (nStartPageNum < 0 ||
373                     nStartPageNum >nEndPageNum ||
374                     nEndPageNum > nCount) {
375                     return FALSE;
376                 } else {
377                     for (int i = nStartPageNum; i <= nEndPageNum; ++i) {
378                         pageArray->Add(i);
379                     }
380                 }
381             }
382             nStringFrom = nStringTo + 1;
383         }
384     }
385     return TRUE;
386 }
387
388 DLLEXPORT FPDF_BOOL STDCALL FPDF_ImportPages(FPDF_DOCUMENT dest_doc,
389                                              FPDF_DOCUMENT src_doc,
390                                              FPDF_BYTESTRING pagerange,
391                                              int index)
392 {
393     if (!dest_doc || !src_doc)
394         return FALSE;
395
396     CFX_WordArray pageArray;
397     CPDF_Document* pSrcDoc = (CPDF_Document*)src_doc;
398     int nCount = pSrcDoc->GetPageCount();
399     if (pagerange) {
400         if (!ParserPageRangeString(pagerange,&pageArray,nCount))
401             return FALSE;
402     } else {
403         for (int i = 1; i <= nCount; ++i) {
404             pageArray.Add(i);
405         }
406     }
407
408     CPDF_Document* pDestDoc = (CPDF_Document*)dest_doc;
409     CPDF_PageOrganizer pageOrg;
410
411     pageOrg.PDFDocInit(pDestDoc, pSrcDoc);
412
413     return pageOrg.ExportPage(pSrcDoc,&pageArray,pDestDoc,index);
414 }
415
416 DLLEXPORT FPDF_BOOL STDCALL FPDF_CopyViewerPreferences(FPDF_DOCUMENT dest_doc,
417                                                        FPDF_DOCUMENT src_doc)
418 {
419     if (!src_doc || !dest_doc)
420         return false;
421
422     CPDF_Document* pSrcDoc = (CPDF_Document*)src_doc;
423     CPDF_Dictionary* pSrcDict = pSrcDoc->GetRoot();
424     pSrcDict = pSrcDict->GetDict(FX_BSTRC("ViewerPreferences"));;
425     if (!pSrcDict)
426         return FALSE;
427
428     CPDF_Document* pDstDoc = (CPDF_Document*)dest_doc;
429     CPDF_Dictionary* pDstDict = pDstDoc->GetRoot();
430     if (!pDstDict)
431         return FALSE;
432     pDstDict->SetAt(FX_BSTRC("ViewerPreferences"), pSrcDict->Clone(TRUE));
433     return TRUE;
434 }