Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_document.cpp

Issue 1585823003: Correct the way to count pages and to avoid infinite loop (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: remove a function Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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*>* counting_pages) {
Lei Zhang 2016/01/14 19:22:39 counted_pages or encountered_pages?
Wei Li 2016/01/14 19:46:00 Those pages are not counted yet. Chose a textbook
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(*counting_pages, pKid)) {
30 continue;
31 }
32 if (!pKid->KeyExist("Kids")) {
Lei Zhang 2016/01/14 19:22:39 Do you want to remove the '!' and flip the if/else
Wei Li 2016/01/14 19:45:59 Done.
33 count++;
34 } else {
35 // Use |counting_pages| to help detect circular references of pages.
36 ScopedSetInsertion<CPDF_Dictionary*> local_add(counting_pages, pKid);
37 count += CountPages(pKid, counting_pages);
38 }
39 }
40 pPages->SetAtInteger("Count", count);
41 return count;
42 }
43
44 } // namespace
10 45
11 CPDF_Document::CPDF_Document(CPDF_Parser* pParser) 46 CPDF_Document::CPDF_Document(CPDF_Parser* pParser)
12 : CPDF_IndirectObjectHolder(pParser) { 47 : CPDF_IndirectObjectHolder(pParser) {
13 ASSERT(pParser); 48 ASSERT(pParser);
14 m_pRootDict = NULL; 49 m_pRootDict = NULL;
15 m_pInfoDict = NULL; 50 m_pInfoDict = NULL;
16 m_bLinearized = FALSE; 51 m_bLinearized = FALSE;
17 m_dwFirstPageNo = 0; 52 m_dwFirstPageNo = 0;
18 m_dwFirstPageObjNum = 0; 53 m_dwFirstPageObjNum = 0;
19 m_pDocPage = CPDF_ModuleMgr::Get()->GetPageModule()->CreateDocData(this); 54 m_pDocPage = CPDF_ModuleMgr::Get()->GetPageModule()->CreateDocData(this);
(...skipping 27 matching lines...) Expand all
47 CPDF_Object* pInfoObj = 82 CPDF_Object* pInfoObj =
48 GetIndirectObject(m_pParser->GetInfoObjNum(), nullptr); 83 GetIndirectObject(m_pParser->GetInfoObjNum(), nullptr);
49 if (pInfoObj) { 84 if (pInfoObj) {
50 m_pInfoDict = pInfoObj->GetDict(); 85 m_pInfoDict = pInfoObj->GetDict();
51 } 86 }
52 CPDF_Array* pIDArray = m_pParser->GetIDArray(); 87 CPDF_Array* pIDArray = m_pParser->GetIDArray();
53 if (pIDArray) { 88 if (pIDArray) {
54 m_ID1 = pIDArray->GetString(0); 89 m_ID1 = pIDArray->GetString(0);
55 m_ID2 = pIDArray->GetString(1); 90 m_ID2 = pIDArray->GetString(1);
56 } 91 }
57 m_PageList.SetSize(_GetPageCount()); 92 m_PageList.SetSize(RetrievePageCount());
58 } 93 }
59 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { 94 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) {
60 m_bLinearized = TRUE; 95 m_bLinearized = TRUE;
61 m_LastObjNum = m_pParser->GetLastObjNum(); 96 m_LastObjNum = m_pParser->GetLastObjNum();
62 CPDF_Object* pIndirectObj = 97 CPDF_Object* pIndirectObj =
63 GetIndirectObject(m_pParser->GetRootObjNum(), nullptr); 98 GetIndirectObject(m_pParser->GetRootObjNum(), nullptr);
64 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; 99 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr;
65 if (!m_pRootDict) { 100 if (!m_pRootDict) {
66 return; 101 return;
67 } 102 }
(...skipping 12 matching lines...) Expand all
80 m_PageList.SetSize(dwPageCount); 115 m_PageList.SetSize(dwPageCount);
81 CPDF_Object* pNo = pLinearized->GetElement("P"); 116 CPDF_Object* pNo = pLinearized->GetElement("P");
82 if (ToNumber(pNo)) 117 if (ToNumber(pNo))
83 m_dwFirstPageNo = pNo->GetInteger(); 118 m_dwFirstPageNo = pNo->GetInteger();
84 119
85 CPDF_Object* pObjNum = pLinearized->GetElement("O"); 120 CPDF_Object* pObjNum = pLinearized->GetElement("O");
86 if (ToNumber(pObjNum)) 121 if (ToNumber(pObjNum))
87 m_dwFirstPageObjNum = pObjNum->GetInteger(); 122 m_dwFirstPageObjNum = pObjNum->GetInteger();
88 } 123 }
89 void CPDF_Document::LoadPages() { 124 void CPDF_Document::LoadPages() {
90 m_PageList.SetSize(_GetPageCount()); 125 m_PageList.SetSize(RetrievePageCount());
91 } 126 }
92 CPDF_Document::~CPDF_Document() { 127 CPDF_Document::~CPDF_Document() {
93 if (m_pDocPage) { 128 if (m_pDocPage) {
94 CPDF_ModuleMgr::Get()->GetPageModule()->ReleaseDoc(this); 129 CPDF_ModuleMgr::Get()->GetPageModule()->ReleaseDoc(this);
95 CPDF_ModuleMgr::Get()->GetPageModule()->ClearStockFont(this); 130 CPDF_ModuleMgr::Get()->GetPageModule()->ClearStockFont(this);
96 } 131 }
97 if (m_pDocRender) { 132 if (m_pDocRender) {
98 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender); 133 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender);
99 } 134 }
100 } 135 }
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); 284 CPDF_Dictionary* pPages = pRoot->GetDict("Pages");
250 if (!pPages) { 285 if (!pPages) {
251 return -1; 286 return -1;
252 } 287 }
253 int index = 0; 288 int index = 0;
254 return _FindPageIndex(pPages, skip_count, objnum, index); 289 return _FindPageIndex(pPages, skip_count, objnum, index);
255 } 290 }
256 int CPDF_Document::GetPageCount() const { 291 int CPDF_Document::GetPageCount() const {
257 return m_PageList.GetSize(); 292 return m_PageList.GetSize();
258 } 293 }
259 static int _CountPages(CPDF_Dictionary* pPages, int level) { 294
260 if (level > 128) { 295 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(); 296 CPDF_Dictionary* pRoot = GetRoot();
288 if (!pRoot) { 297 if (!pRoot) {
289 return 0; 298 return 0;
290 } 299 }
291 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); 300 CPDF_Dictionary* pPages = pRoot->GetDict("Pages");
292 if (!pPages) { 301 if (!pPages) {
293 return 0; 302 return 0;
294 } 303 }
295 if (!pPages->KeyExist("Kids")) { 304 if (!pPages->KeyExist("Kids")) {
296 return 1; 305 return 1;
297 } 306 }
298 return _CountPages(pPages, 0); 307 std::set<CPDF_Dictionary*> counting_pages;
308 counting_pages.insert(pPages);
309 return CountPages(pPages, &counting_pages);
299 } 310 }
311
300 FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum, 312 FX_BOOL CPDF_Document::IsContentUsedElsewhere(FX_DWORD objnum,
301 CPDF_Dictionary* pThisPageDict) { 313 CPDF_Dictionary* pThisPageDict) {
302 for (int i = 0; i < m_PageList.GetSize(); i++) { 314 for (int i = 0; i < m_PageList.GetSize(); i++) {
303 CPDF_Dictionary* pPageDict = GetPage(i); 315 CPDF_Dictionary* pPageDict = GetPage(i);
304 if (pPageDict == pThisPageDict) { 316 if (pPageDict == pThisPageDict) {
305 continue; 317 continue;
306 } 318 }
307 CPDF_Object* pContents = 319 CPDF_Object* pContents =
308 pPageDict ? pPageDict->GetElement("Contents") : NULL; 320 pPageDict ? pPageDict->GetElement("Contents") : NULL;
309 if (!pContents) { 321 if (!pContents) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 void CPDF_Document::ClearPageData() { 359 void CPDF_Document::ClearPageData() {
348 if (m_pDocPage) { 360 if (m_pDocPage) {
349 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this); 361 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this);
350 } 362 }
351 } 363 }
352 void CPDF_Document::ClearRenderData() { 364 void CPDF_Document::ClearRenderData() {
353 if (m_pDocRender) { 365 if (m_pDocRender) {
354 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender); 366 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender);
355 } 367 }
356 } 368 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698