From 4012849bb60f483c71ddf854e5a08ff6f4f0b8ef Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Thu, 2 Nov 2017 15:01:18 +0000 Subject: Improve readability for CPDF_Parser::StartParseInternal Change-Id: Id250aeeb131d344d79f41c93d4c71b42e266195d Reviewed-on: https://pdfium-review.googlesource.com/13930 Commit-Queue: Art Snake Reviewed-by: dsinclair --- core/fpdfapi/parser/cpdf_parser.cpp | 34 +++++++++++++++++++++++++--------- core/fpdfapi/parser/cpdf_parser.h | 2 +- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index 7a8f4f9ce7..d02989000c 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -214,21 +214,14 @@ CPDF_Parser::Error CPDF_Parser::StartParseInternal(CPDF_Document* pDocument) { ASSERT(!m_bHasParsed); m_bHasParsed = true; m_bXRefStream = false; - m_LastXRefOffset = 0; - m_pSyntax->SetPos(m_pSyntax->m_FileLen - m_pSyntax->m_HeaderOffset - 9); m_pDocument = pDocument; bool bXRefRebuilt = false; - if (m_pSyntax->BackwardsSearchToWord("startxref", 4096)) { - m_pSyntax->GetKeyword(); - bool bNumber; - ByteString xrefpos_str = m_pSyntax->GetNextWord(&bNumber); - if (!bNumber) - return FORMAT_ERROR; + m_LastXRefOffset = ParseStartXRef(); - m_LastXRefOffset = (FX_FILESIZE)FXSYS_atoi64(xrefpos_str.c_str()); + if (m_LastXRefOffset > 0) { if (!LoadAllCrossRefV4(m_LastXRefOffset) && !LoadAllCrossRefV5(m_LastXRefOffset)) { if (!RebuildCrossRef()) @@ -282,6 +275,29 @@ CPDF_Parser::Error CPDF_Parser::StartParseInternal(CPDF_Document* pDocument) { return SUCCESS; } +FX_FILESIZE CPDF_Parser::ParseStartXRef() { + static constexpr char kStartXRefKeyword[] = "startxref"; + m_pSyntax->SetPos(m_pSyntax->m_FileLen - m_pSyntax->m_HeaderOffset - + strlen(kStartXRefKeyword)); + if (!m_pSyntax->BackwardsSearchToWord(kStartXRefKeyword, 4096)) + return 0; + + // Skip "startxref" keyword. + m_pSyntax->GetKeyword(); + + // Read XRef offset. + bool bNumber; + const ByteString xrefpos_str = m_pSyntax->GetNextWord(&bNumber); + if (!bNumber || xrefpos_str.IsEmpty()) + return 0; + + const FX_SAFE_FILESIZE result = FXSYS_atoi64(xrefpos_str.c_str()); + if (!result.IsValid() || result.ValueOrDie() >= GetFileAccess()->GetSize()) + return 0; + + return result.ValueOrDie(); +} + CPDF_Parser::Error CPDF_Parser::SetEncryptHandler() { ReleaseEncryptHandler(); if (!GetTrailer()) diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 6c8cfbd0f8..375bc01621 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -155,7 +155,7 @@ class CPDF_Parser { }; Error StartParseInternal(CPDF_Document* pDocument); - CPDF_Object* ParseDirect(CPDF_Object* pObj); + FX_FILESIZE ParseStartXRef(); bool LoadAllCrossRefV4(FX_FILESIZE pos); bool LoadAllCrossRefV5(FX_FILESIZE pos); bool LoadCrossRefV5(FX_FILESIZE* pos, bool bMainXRef); -- cgit v1.2.3