From 2d868435f4c0d47ded1ea405ad2af6b9d83b6651 Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Tue, 26 Jun 2018 16:38:49 +0000 Subject: Unify CPDF_Document loading methods. Change-Id: Ibf7aee942027adace7ec0831aefe0fe8c28e41cc Reviewed-on: https://pdfium-review.googlesource.com/35610 Commit-Queue: Art Snake Reviewed-by: dsinclair --- core/fpdfapi/parser/cpdf_document.cpp | 37 +++++++++++--------------- core/fpdfapi/parser/cpdf_document.h | 4 --- core/fpdfapi/parser/cpdf_document_unittest.cpp | 8 +++--- core/fpdfapi/parser/cpdf_parser.cpp | 9 +++++-- core/fpdfapi/parser/cpdf_parser.h | 5 ++++ 5 files changed, 33 insertions(+), 30 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index 393a6f6785..8f727ed14a 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -191,9 +191,6 @@ CPDF_Document::CPDF_Document(std::unique_ptr pParser) m_pRootDict(nullptr), m_iNextPageToTraverse(0), m_bReachedMaxPageLevel(false), - m_bLinearized(false), - m_iFirstPageNo(0), - m_dwFirstPageObjNum(0), m_pDocPage(pdfium::MakeUnique(this)), m_pDocRender(pdfium::MakeUnique(this)) { if (pParser) @@ -226,17 +223,18 @@ void CPDF_Document::LoadDoc() { LoadPages(); } -void CPDF_Document::LoadLinearizedDoc( - const CPDF_LinearizedHeader* pLinearizationParams) { - m_bLinearized = true; - LoadDocInternal(); - m_PageList.resize(pLinearizationParams->GetPageCount()); - m_iFirstPageNo = pLinearizationParams->GetFirstPageNo(); - m_dwFirstPageObjNum = pLinearizationParams->GetFirstPageObjNum(); -} - void CPDF_Document::LoadPages() { - m_PageList.resize(RetrievePageCount()); + const CPDF_LinearizedHeader* linearized_header = + m_pParser->GetLinearizedHeader(); + if (!linearized_header) { + m_PageList.resize(RetrievePageCount()); + return; + } + + m_PageList.resize(linearized_header->GetPageCount()); + DCHECK(linearized_header->GetFirstPageNo() < m_PageList.size()); + m_PageList[linearized_header->GetFirstPageNo()] = + linearized_header->GetFirstPageObjNum(); } CPDF_Dictionary* CPDF_Document::TraversePDFPages(int iPage, @@ -329,15 +327,12 @@ CPDF_Dictionary* CPDF_Document::GetPageDictionary(int iPage) { if (!pdfium::IndexInBounds(m_PageList, iPage)) return nullptr; - if (m_bLinearized && iPage == m_iFirstPageNo) { - if (CPDF_Dictionary* pDict = - ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { - return pDict; - } + const uint32_t objnum = m_PageList[iPage]; + if (objnum) { + CPDF_Dictionary* result = ToDictionary(GetOrParseIndirectObject(objnum)); + if (result) + return result; } - uint32_t objnum = m_PageList[iPage]; - if (objnum) - return ToDictionary(GetOrParseIndirectObject(objnum)); CPDF_Dictionary* pPages = GetPagesDict(); if (!pPages) diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h index db659b38c8..8d0aa08009 100644 --- a/core/fpdfapi/parser/cpdf_document.h +++ b/core/fpdfapi/parser/cpdf_document.h @@ -97,7 +97,6 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { RetainPtr LoadIccProfile(const CPDF_Stream* pStream); void LoadDoc(); - void LoadLinearizedDoc(const CPDF_LinearizedHeader* pLinearizationParams); void LoadPages(); void CreateNewDoc(); @@ -159,9 +158,6 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { // Index of the next page that will be traversed from the page tree. int m_iNextPageToTraverse; bool m_bReachedMaxPageLevel; - bool m_bLinearized; - int m_iFirstPageNo; - uint32_t m_dwFirstPageObjNum; uint32_t m_ParsedPageCount = 0; std::unique_ptr m_pDocPage; std::unique_ptr m_pDocRender; diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp index fa52d3bc7f..522aa65fc9 100644 --- a/core/fpdfapi/parser/cpdf_document_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp @@ -215,13 +215,15 @@ 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()); auto dict = pdfium::MakeUnique(); dict->SetNewFor("Linearized", true); const int page_count = 100; dict->SetNewFor("N", page_count); - TestLinearized linearized(dict.get()); - document.LoadLinearizedDoc(&linearized); + auto linearized = pdfium::MakeUnique(dict.get()); + auto parser = pdfium::MakeUnique(); + parser->SetLinearizedHeader(std::move(linearized)); + CPDF_Document document(std::move(parser)); + document.LoadDoc(); ASSERT_EQ(page_count, document.GetPageCount()); CPDF_Object* page_stub = document.NewIndirect(); const uint32_t obj_num = page_stub->GetObjNum(); diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index 6cdd6ab789..599b04199d 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -1287,6 +1287,11 @@ uint32_t CPDF_Parser::GetFirstPageNo() const { return m_pLinearized ? m_pLinearized->GetFirstPageNo() : 0; } +void CPDF_Parser::SetLinearizedHeader( + std::unique_ptr pLinearized) { + m_pLinearized = std::move(pLinearized); +} + std::unique_ptr CPDF_Parser::LoadTrailerV4() { if (m_pSyntax->GetKeyword() != "trailer") return nullptr; @@ -1354,7 +1359,7 @@ CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( if (eRet != SUCCESS) return eRet; - m_pDocument->LoadLinearizedDoc(m_pLinearized.get()); + m_pDocument->LoadDoc(); if (!m_pDocument->GetRoot() || m_pDocument->GetPageCount() == 0) { if (bXRefRebuilt) return FORMAT_ERROR; @@ -1367,7 +1372,7 @@ CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( if (eRet != SUCCESS) return eRet; - m_pDocument->LoadLinearizedDoc(m_pLinearized.get()); + m_pDocument->LoadDoc(); if (!m_pDocument->GetRoot()) return FORMAT_ERROR; } diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 705fad9bdb..b74f1d52ab 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -106,6 +106,11 @@ class CPDF_Parser { FX_FILESIZE* pResultPos); uint32_t GetFirstPageNo() const; + const CPDF_LinearizedHeader* GetLinearizedHeader() const { + return m_pLinearized.get(); + } + + void SetLinearizedHeader(std::unique_ptr pLinearized); protected: enum class ObjectType : uint8_t { -- cgit v1.2.3