OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/include/fpdfapi/fpdf_parser.h" | 7 #include "core/include/fpdfapi/fpdf_parser.h" |
8 | 8 |
| 9 #include <set> |
| 10 |
9 #include "core/include/fpdfapi/fpdf_module.h" | 11 #include "core/include/fpdfapi/fpdf_module.h" |
| 12 #include "third_party/base/stl_util.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 int CountPages(CPDF_Dictionary* pPages, |
| 17 std::set<CPDF_Dictionary*>* visited_pages) { |
| 18 int count = pPages->GetInteger("Count"); |
| 19 if (count > 0 && count < FPDF_PAGE_MAX_NUM) { |
| 20 return count; |
| 21 } |
| 22 CPDF_Array* pKidList = pPages->GetArray("Kids"); |
| 23 if (!pKidList) { |
| 24 return 0; |
| 25 } |
| 26 count = 0; |
| 27 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { |
| 28 CPDF_Dictionary* pKid = pKidList->GetDict(i); |
| 29 if (!pKid || pdfium::ContainsKey(*visited_pages, pKid)) { |
| 30 continue; |
| 31 } |
| 32 if (pKid->KeyExist("Kids")) { |
| 33 // Use |visited_pages| to help detect circular references of pages. |
| 34 ScopedSetInsertion<CPDF_Dictionary*> local_add(visited_pages, pKid); |
| 35 count += CountPages(pKid, visited_pages); |
| 36 } else { |
| 37 // This page is a leaf node. |
| 38 count++; |
| 39 } |
| 40 } |
| 41 pPages->SetAtInteger("Count", count); |
| 42 return count; |
| 43 } |
| 44 |
| 45 } // namespace |
10 | 46 |
11 CPDF_Document::CPDF_Document(CPDF_Parser* pParser) | 47 CPDF_Document::CPDF_Document(CPDF_Parser* pParser) |
12 : CPDF_IndirectObjectHolder(pParser) { | 48 : CPDF_IndirectObjectHolder(pParser) { |
13 ASSERT(pParser); | 49 ASSERT(pParser); |
14 m_pRootDict = NULL; | 50 m_pRootDict = NULL; |
15 m_pInfoDict = NULL; | 51 m_pInfoDict = NULL; |
16 m_bLinearized = FALSE; | 52 m_bLinearized = FALSE; |
17 m_dwFirstPageNo = 0; | 53 m_dwFirstPageNo = 0; |
18 m_dwFirstPageObjNum = 0; | 54 m_dwFirstPageObjNum = 0; |
19 m_pDocPage = CPDF_ModuleMgr::Get()->GetPageModule()->CreateDocData(this); | 55 m_pDocPage = CPDF_ModuleMgr::Get()->GetPageModule()->CreateDocData(this); |
(...skipping 27 matching lines...) Expand all Loading... |
47 CPDF_Object* pInfoObj = | 83 CPDF_Object* pInfoObj = |
48 GetIndirectObject(m_pParser->GetInfoObjNum(), nullptr); | 84 GetIndirectObject(m_pParser->GetInfoObjNum(), nullptr); |
49 if (pInfoObj) { | 85 if (pInfoObj) { |
50 m_pInfoDict = pInfoObj->GetDict(); | 86 m_pInfoDict = pInfoObj->GetDict(); |
51 } | 87 } |
52 CPDF_Array* pIDArray = m_pParser->GetIDArray(); | 88 CPDF_Array* pIDArray = m_pParser->GetIDArray(); |
53 if (pIDArray) { | 89 if (pIDArray) { |
54 m_ID1 = pIDArray->GetString(0); | 90 m_ID1 = pIDArray->GetString(0); |
55 m_ID2 = pIDArray->GetString(1); | 91 m_ID2 = pIDArray->GetString(1); |
56 } | 92 } |
57 m_PageList.SetSize(_GetPageCount()); | 93 m_PageList.SetSize(RetrievePageCount()); |
58 } | 94 } |
59 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { | 95 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { |
60 m_bLinearized = TRUE; | 96 m_bLinearized = TRUE; |
61 m_LastObjNum = m_pParser->GetLastObjNum(); | 97 m_LastObjNum = m_pParser->GetLastObjNum(); |
62 CPDF_Object* pIndirectObj = | 98 CPDF_Object* pIndirectObj = |
63 GetIndirectObject(m_pParser->GetRootObjNum(), nullptr); | 99 GetIndirectObject(m_pParser->GetRootObjNum(), nullptr); |
64 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; | 100 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; |
65 if (!m_pRootDict) { | 101 if (!m_pRootDict) { |
66 return; | 102 return; |
67 } | 103 } |
(...skipping 12 matching lines...) Expand all Loading... |
80 m_PageList.SetSize(dwPageCount); | 116 m_PageList.SetSize(dwPageCount); |
81 CPDF_Object* pNo = pLinearized->GetElement("P"); | 117 CPDF_Object* pNo = pLinearized->GetElement("P"); |
82 if (ToNumber(pNo)) | 118 if (ToNumber(pNo)) |
83 m_dwFirstPageNo = pNo->GetInteger(); | 119 m_dwFirstPageNo = pNo->GetInteger(); |
84 | 120 |
85 CPDF_Object* pObjNum = pLinearized->GetElement("O"); | 121 CPDF_Object* pObjNum = pLinearized->GetElement("O"); |
86 if (ToNumber(pObjNum)) | 122 if (ToNumber(pObjNum)) |
87 m_dwFirstPageObjNum = pObjNum->GetInteger(); | 123 m_dwFirstPageObjNum = pObjNum->GetInteger(); |
88 } | 124 } |
89 void CPDF_Document::LoadPages() { | 125 void CPDF_Document::LoadPages() { |
90 m_PageList.SetSize(_GetPageCount()); | 126 m_PageList.SetSize(RetrievePageCount()); |
91 } | 127 } |
92 CPDF_Document::~CPDF_Document() { | 128 CPDF_Document::~CPDF_Document() { |
93 if (m_pDocPage) { | 129 if (m_pDocPage) { |
94 CPDF_ModuleMgr::Get()->GetPageModule()->ReleaseDoc(this); | 130 CPDF_ModuleMgr::Get()->GetPageModule()->ReleaseDoc(this); |
95 CPDF_ModuleMgr::Get()->GetPageModule()->ClearStockFont(this); | 131 CPDF_ModuleMgr::Get()->GetPageModule()->ClearStockFont(this); |
96 } | 132 } |
97 if (m_pDocRender) { | 133 if (m_pDocRender) { |
98 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender); | 134 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender); |
99 } | 135 } |
100 } | 136 } |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); | 285 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); |
250 if (!pPages) { | 286 if (!pPages) { |
251 return -1; | 287 return -1; |
252 } | 288 } |
253 int index = 0; | 289 int index = 0; |
254 return _FindPageIndex(pPages, skip_count, objnum, index); | 290 return _FindPageIndex(pPages, skip_count, objnum, index); |
255 } | 291 } |
256 int CPDF_Document::GetPageCount() const { | 292 int CPDF_Document::GetPageCount() const { |
257 return m_PageList.GetSize(); | 293 return m_PageList.GetSize(); |
258 } | 294 } |
259 static int _CountPages(CPDF_Dictionary* pPages, int level) { | 295 |
260 if (level > 128) { | 296 int CPDF_Document::RetrievePageCount() const { |
261 return 0; | |
262 } | |
263 int count = pPages->GetInteger("Count"); | |
264 if (count > 0 && count < FPDF_PAGE_MAX_NUM) { | |
265 return count; | |
266 } | |
267 CPDF_Array* pKidList = pPages->GetArray("Kids"); | |
268 if (!pKidList) { | |
269 return 0; | |
270 } | |
271 count = 0; | |
272 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { | |
273 CPDF_Dictionary* pKid = pKidList->GetDict(i); | |
274 if (!pKid) { | |
275 continue; | |
276 } | |
277 if (!pKid->KeyExist("Kids")) { | |
278 count++; | |
279 } else { | |
280 count += _CountPages(pKid, level + 1); | |
281 } | |
282 } | |
283 pPages->SetAtInteger("Count", count); | |
284 return count; | |
285 } | |
286 int CPDF_Document::_GetPageCount() const { | |
287 CPDF_Dictionary* pRoot = GetRoot(); | 297 CPDF_Dictionary* pRoot = GetRoot(); |
288 if (!pRoot) { | 298 if (!pRoot) { |
289 return 0; | 299 return 0; |
290 } | 300 } |
291 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); | 301 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); |
292 if (!pPages) { | 302 if (!pPages) { |
293 return 0; | 303 return 0; |
294 } | 304 } |
295 if (!pPages->KeyExist("Kids")) { | 305 if (!pPages->KeyExist("Kids")) { |
296 return 1; | 306 return 1; |
297 } | 307 } |
298 return _CountPages(pPages, 0); | 308 std::set<CPDF_Dictionary*> visited_pages; |
| 309 visited_pages.insert(pPages); |
| 310 return CountPages(pPages, &visited_pages); |
299 } | 311 } |
| 312 |
300 FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum, | 313 FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum, |
301 CPDF_Dictionary* pThisPageDict) { | 314 CPDF_Dictionary* pThisPageDict) { |
302 for (int i = 0; i < m_PageList.GetSize(); i++) { | 315 for (int i = 0; i < m_PageList.GetSize(); i++) { |
303 CPDF_Dictionary* pPageDict = GetPage(i); | 316 CPDF_Dictionary* pPageDict = GetPage(i); |
304 if (pPageDict == pThisPageDict) { | 317 if (pPageDict == pThisPageDict) { |
305 continue; | 318 continue; |
306 } | 319 } |
307 CPDF_Object* pContents = | 320 CPDF_Object* pContents = |
308 pPageDict ? pPageDict->GetElement("Contents") : NULL; | 321 pPageDict ? pPageDict->GetElement("Contents") : NULL; |
309 if (!pContents) { | 322 if (!pContents) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 void CPDF_Document::ClearPageData() { | 360 void CPDF_Document::ClearPageData() { |
348 if (m_pDocPage) { | 361 if (m_pDocPage) { |
349 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this); | 362 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this); |
350 } | 363 } |
351 } | 364 } |
352 void CPDF_Document::ClearRenderData() { | 365 void CPDF_Document::ClearRenderData() { |
353 if (m_pDocRender) { | 366 if (m_pDocRender) { |
354 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender); | 367 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender); |
355 } | 368 } |
356 } | 369 } |
OLD | NEW |