From b9d06fb2186d7002d6231d1805bcf97fc60144c5 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 28 Oct 2015 12:57:00 -0400 Subject: Add PositionIsInBounds to CPDF_StreamParser This Cl moves all of the m_Size <= m_Pos and m_Pos >= m_Size checks to an PositionIsInBounds() helper. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1412793008 . --- .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 57 ++++++++++++---------- core/src/fpdfapi/fpdf_page/pageint.h | 8 +++ 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index 27d8c24f15..1e435ee958 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -458,13 +458,13 @@ CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { m_WordSize = 0; FX_BOOL bIsNumber = TRUE; - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return EndOfData; int ch = m_pBuf[m_Pos++]; while (1) { while (PDFCharIsWhitespace(ch)) { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return EndOfData; ch = m_pBuf[m_Pos++]; @@ -474,7 +474,7 @@ CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { break; while (1) { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return EndOfData; ch = m_pBuf[m_Pos++]; @@ -496,7 +496,7 @@ CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { if (!PDFCharIsNumeric(ch)) bIsNumber = FALSE; - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) break; ch = m_pBuf[m_Pos++]; @@ -532,13 +532,13 @@ CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { } void CPDF_StreamParser::SkipPathObject() { FX_DWORD command_startpos = m_Pos; - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return; int ch = m_pBuf[m_Pos++]; while (1) { while (PDFCharIsWhitespace(ch)) { - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; } @@ -550,13 +550,13 @@ void CPDF_StreamParser::SkipPathObject() { while (1) { while (!PDFCharIsWhitespace(ch)) { - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; } while (PDFCharIsWhitespace(ch)) { - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; } @@ -566,7 +566,7 @@ void CPDF_StreamParser::SkipPathObject() { FX_DWORD op_startpos = m_Pos - 1; while (!PDFCharIsWhitespace(ch) && !PDFCharIsDelimiter(ch)) { - if (m_Pos >= m_Size) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; } @@ -679,13 +679,13 @@ CPDF_Object* CPDF_StreamParser::ReadNextObject(FX_BOOL bAllowNestedArray, void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { m_WordSize = 0; bIsNumber = TRUE; - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; int ch = m_pBuf[m_Pos++]; while (1) { while (PDFCharIsWhitespace(ch)) { - if (m_Size <= m_Pos) { + if (!PositionIsInBounds()) { return; } ch = m_pBuf[m_Pos++]; @@ -695,7 +695,7 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { break; while (1) { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; if (ch == '\r' || ch == '\n') @@ -708,7 +708,7 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { m_WordBuffer[m_WordSize++] = ch; if (ch == '/') { while (1) { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) { @@ -720,7 +720,7 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { m_WordBuffer[m_WordSize++] = ch; } } else if (ch == '<') { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; if (ch == '<') @@ -728,7 +728,7 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { else m_Pos--; } else if (ch == '>') { - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; if (ch == '>') @@ -745,7 +745,7 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { if (!PDFCharIsNumeric(ch)) bIsNumber = FALSE; - if (m_Size <= m_Pos) + if (!PositionIsInBounds()) return; ch = m_pBuf[m_Pos++]; if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { @@ -756,9 +756,9 @@ void CPDF_StreamParser::GetNextWord(FX_BOOL& bIsNumber) { } CFX_ByteString CPDF_StreamParser::ReadString() { - if (m_Size <= m_Pos) { + if (!PositionIsInBounds()) return CFX_ByteString(); - } + int ch = m_pBuf[m_Pos++]; CFX_ByteTextBuf buf; int parlevel = 0; @@ -837,23 +837,23 @@ CFX_ByteString CPDF_StreamParser::ReadString() { } break; } - if (m_Size <= m_Pos) { + if (!PositionIsInBounds()) break; - } + ch = m_pBuf[m_Pos++]; } - if (m_Size > m_Pos) { + if (PositionIsInBounds()) ch = m_pBuf[m_Pos++]; - } + if (buf.GetLength() > MAX_STRING_LENGTH) { return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH); } return buf.GetByteString(); } CFX_ByteString CPDF_StreamParser::ReadHexString() { - if (m_Size <= m_Pos) { + if (!PositionIsInBounds()) return CFX_ByteString(); - } + int ch = m_pBuf[m_Pos++]; CFX_ByteTextBuf buf; FX_BOOL bFirst = TRUE; @@ -887,9 +887,9 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() { } bFirst = !bFirst; } - if (m_Size <= m_Pos) { + if (!PositionIsInBounds()) break; - } + ch = m_pBuf[m_Pos++]; } if (!bFirst) { @@ -900,6 +900,11 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() { } return buf.GetByteString(); } + +bool CPDF_StreamParser::PositionIsInBounds() const { + return m_Pos < m_Size; +} + #define PAGEPARSE_STAGE_GETCONTENT 1 #define PAGEPARSE_STAGE_PARSE 2 #define PAGEPARSE_STAGE_CHECKCLIP 3 diff --git a/core/src/fpdfapi/fpdf_page/pageint.h b/core/src/fpdfapi/fpdf_page/pageint.h index c85523b833..1b41633346 100644 --- a/core/src/fpdfapi/fpdf_page/pageint.h +++ b/core/src/fpdfapi/fpdf_page/pageint.h @@ -45,11 +45,19 @@ class CPDF_StreamParser { CFX_ByteString ReadString(); CFX_ByteString ReadHexString(); const uint8_t* m_pBuf; + + // Length in bytes of m_pBuf. FX_DWORD m_Size; + + // Current byte position within m_pBuf. FX_DWORD m_Pos; + uint8_t m_WordBuffer[256]; FX_DWORD m_WordSize; CPDF_Object* m_pLastObj; + + private: + bool PositionIsInBounds() const; }; typedef enum { PDFOP_CloseFillStrokePath = 0, -- cgit v1.2.3