From 461b1d93f717e248ceb3c1e1bbb8285ba3258f8c Mon Sep 17 00:00:00 2001 From: art-snake Date: Mon, 31 Oct 2016 12:25:30 -0700 Subject: Fix loading page using hint tables. When linearized document have hint table, The FPDFAvail_IsPageAvail return true, but FPDF_LoadPage return nullptr, for non first pages. This happens, bacause document not use hint tables, to load page. To fix this, I force save the page's ObjNum in document. This is restoring of original fix: https://codereview.chromium.org/2437773003/ Review-Url: https://codereview.chromium.org/2444903002 --- core/fpdfapi/parser/cpdf_data_avail.cpp | 52 +++++++++++++------------- core/fpdfapi/parser/cpdf_document_unittest.cpp | 44 +++++++++++++++++++--- 2 files changed, 65 insertions(+), 31 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp index c6a434be5d..318f2cf54d 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.cpp +++ b/core/fpdfapi/parser/cpdf_data_avail.cpp @@ -1626,7 +1626,7 @@ CPDF_DataAvail::DocAvailStatus CPDF_DataAvail::IsPageAvail( if (nResult != DataAvailable) return nResult; m_pagesLoadState.insert(dwPage); - return DataAvailable; + return GetPage(dwPage) ? DataAvailable : DataError; } if (m_bMainXRefLoadedOK) { @@ -1751,31 +1751,33 @@ int CPDF_DataAvail::GetPageCount() const { CPDF_Dictionary* CPDF_DataAvail::GetPage(int index) { if (!m_pDocument || index < 0 || index >= GetPageCount()) return nullptr; + CPDF_Dictionary* page = m_pDocument->GetPage(index); + if (page) + return page; + if (!m_pLinearized || !m_pHintTables) + return nullptr; - if (m_pLinearized) { - CPDF_Dictionary* pDict = m_pLinearized->GetDict(); - CPDF_Object* pObj = pDict ? pDict->GetDirectObjectFor("P") : nullptr; - - int pageNum = pObj ? pObj->GetInteger() : 0; - if (m_pHintTables && index != pageNum) { - FX_FILESIZE szPageStartPos = 0; - FX_FILESIZE szPageLength = 0; - uint32_t dwObjNum = 0; - bool bPagePosGot = m_pHintTables->GetPagePos(index, &szPageStartPos, - &szPageLength, &dwObjNum); - if (!bPagePosGot) - return nullptr; - - m_syntaxParser.InitParser(m_pFileRead, (uint32_t)szPageStartPos); - CPDF_Object* pPageDict = ParseIndirectObjectAt(0, dwObjNum, m_pDocument); - if (!pPageDict) - return nullptr; - - if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration(dwObjNum, - pPageDict)) { - return nullptr; - } - return pPageDict->GetDict(); + CPDF_Dictionary* pDict = m_pLinearized->GetDict(); + CPDF_Object* pObj = pDict ? pDict->GetDirectObjectFor("P") : nullptr; + int firstPageNum = pObj ? pObj->GetInteger() : 0; + if (index == firstPageNum) + return nullptr; + FX_FILESIZE szPageStartPos = 0; + FX_FILESIZE szPageLength = 0; + uint32_t dwObjNum = 0; + const bool bPagePosGot = m_pHintTables->GetPagePos(index, &szPageStartPos, + &szPageLength, &dwObjNum); + if (!bPagePosGot || !dwObjNum) + return nullptr; + // We should say to the document, which object is the page. + m_pDocument->SetPageObjNum(index, dwObjNum); + // Page object already can be parsed in document. + CPDF_Object* pPageDict = m_pDocument->GetIndirectObject(dwObjNum); + if (!pPageDict) { + m_syntaxParser.InitParser(m_pFileRead, (uint32_t)szPageStartPos); + pPageDict = ParseIndirectObjectAt(0, dwObjNum, m_pDocument); + if (pPageDict) { + m_pDocument->ReplaceIndirectObjectIfHigherGeneration(dwObjNum, pPageDict); } } return m_pDocument->GetPage(index); diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp index 799ecc694e..9336626f45 100644 --- a/core/fpdfapi/parser/cpdf_document_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp @@ -15,6 +15,9 @@ namespace { +using ScopedDictionary = + std::unique_ptr>; + CPDF_Dictionary* CreatePageTreeNode(CPDF_Array* kids, CPDF_Document* pDoc, int count) { @@ -35,13 +38,9 @@ CPDF_Dictionary* CreateNumberedPage(size_t number) { return page; } -} // namespace - class CPDF_TestDocumentForPages : public CPDF_Document { public: CPDF_TestDocumentForPages() : CPDF_Document(nullptr) { - CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); - module_mgr->InitPageModule(); // Set up test CPDF_Array* zeroToTwo = new CPDF_Array(); zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); @@ -80,8 +79,18 @@ class CPDF_TestDocumentForPages : public CPDF_Document { std::unique_ptr> m_pOwnedRootDict; }; +} // namespace + +class cpdf_document_test : public testing::Test { + public: + void SetUp() override { + CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); + module_mgr->InitPageModule(); + } + void TearDown() override {} +}; -TEST(cpdf_document, GetPages) { +TEST_F(cpdf_document_test, GetPages) { std::unique_ptr document = pdfium::MakeUnique(); for (int i = 0; i < 7; i++) { @@ -94,7 +103,7 @@ TEST(cpdf_document, GetPages) { EXPECT_FALSE(page); } -TEST(cpdf_document, GetPagesReverseOrder) { +TEST_F(cpdf_document_test, GetPagesReverseOrder) { std::unique_ptr document = pdfium::MakeUnique(); for (int i = 6; i >= 0; i--) { @@ -106,3 +115,26 @@ TEST(cpdf_document, GetPagesReverseOrder) { CPDF_Dictionary* page = document->GetPage(7); EXPECT_FALSE(page); } + +TEST_F(cpdf_document_test, UseCachedPageObjNumIfHaveNotPagesDict) { + // ObjNum can be added in CPDF_DataAvail::IsPageAvail, and PagesDict + // can be not exists in this case. + // (case, when hint table is used to page check in CPDF_DataAvail). + CPDF_Document document(pdfium::MakeUnique()); + ScopedDictionary dict(new CPDF_Dictionary()); + const int page_count = 100; + dict->SetIntegerFor("N", page_count); + document.LoadLinearizedDoc(dict.get()); + ASSERT_EQ(page_count, document.GetPageCount()); + CPDF_Object* page_stub = new CPDF_Dictionary(); + const uint32_t obj_num = document.AddIndirectObject(page_stub); + const int test_page_num = 33; + + EXPECT_FALSE(document.IsPageLoaded(test_page_num)); + EXPECT_EQ(nullptr, document.GetPage(test_page_num)); + + document.SetPageObjNum(test_page_num, obj_num); + + EXPECT_TRUE(document.IsPageLoaded(test_page_num)); + EXPECT_EQ(page_stub, document.GetPage(test_page_num)); +} -- cgit v1.2.3