summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser
diff options
context:
space:
mode:
authorArtem Strygin <art-snake@yandex-team.ru>2017-11-02 15:01:18 +0000
committerChromium commit bot <commit-bot@chromium.org>2017-11-02 15:01:18 +0000
commit4012849bb60f483c71ddf854e5a08ff6f4f0b8ef (patch)
treee7dc0b38d2ed88bf0997e846e9e99a9126767ba5 /core/fpdfapi/parser
parent1beb4a9c5ff7ac58450310493783ef7869f4de71 (diff)
downloadpdfium-4012849bb60f483c71ddf854e5a08ff6f4f0b8ef.tar.xz
Improve readability for CPDF_Parser::StartParseInternal
Change-Id: Id250aeeb131d344d79f41c93d4c71b42e266195d Reviewed-on: https://pdfium-review.googlesource.com/13930 Commit-Queue: Art Snake <art-snake@yandex-team.ru> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdfapi/parser')
-rw-r--r--core/fpdfapi/parser/cpdf_parser.cpp34
-rw-r--r--core/fpdfapi/parser/cpdf_parser.h2
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);