Chromium Code Reviews| Index: core/fpdfapi/parser/cpdf_syntax_parser.cpp |
| diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp |
| index 3b1df94503372795840e022d651e763b3fb7625f..afe386b19ed08c3f7795a9cde5f0f1c979b77b14 100644 |
| --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp |
| +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp |
| @@ -31,6 +31,14 @@ struct SearchTagRecord { |
| FX_STRSIZE m_Offset; |
| }; |
| +enum SyntaxParser_ReadStatus { |
|
dsinclair
2016/11/01 19:14:42
Make this an enum class then you can drop the Synt
npm
2016/11/01 20:09:10
Done.
|
| + SyntaxParser_ReadNormal, |
| + SyntaxParser_ReadBackslash, |
| + SyntaxParser_ReadOctal, |
| + SyntaxParser_ReadFinishOctal, |
| + SyntaxParser_ReadCarriageReturn |
| +}; |
| + |
| } // namespace |
| // static |
| @@ -57,30 +65,36 @@ FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { |
| return GetNextChar(ch); |
| } |
| +FX_BOOL CPDF_SyntaxParser::ReadChar(FX_FILESIZE read_pos, uint32_t read_size) { |
|
dsinclair
2016/11/01 19:14:42
Should this be ReadChars? It will potentially read
npm
2016/11/01 20:09:10
But it still represents a single char, as it is ca
|
| + if (static_cast<FX_FILESIZE>(read_pos + read_size) > m_FileLen) { |
| + if (m_FileLen < static_cast<FX_FILESIZE>(read_size)) { |
| + read_pos = 0; |
| + read_size = static_cast<uint32_t>(m_FileLen); |
| + } else { |
| + read_pos = m_FileLen - read_size; |
| + } |
| + } |
| + if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) |
| + return FALSE; |
| + |
| + m_BufOffset = read_pos; |
| + return TRUE; |
| +} |
| + |
| FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) { |
| FX_FILESIZE pos = m_Pos + m_HeaderOffset; |
| if (pos >= m_FileLen) |
| return FALSE; |
| - if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { |
| + if (m_BufOffset >= pos || |
| + static_cast<FX_FILESIZE>(m_BufOffset + m_BufSize) <= pos) { |
| FX_FILESIZE read_pos = pos; |
| uint32_t read_size = m_BufSize; |
| - if ((FX_FILESIZE)read_size > m_FileLen) |
| - read_size = (uint32_t)m_FileLen; |
| - |
| - if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { |
| - if (m_FileLen < (FX_FILESIZE)read_size) { |
| - read_pos = 0; |
| - read_size = (uint32_t)m_FileLen; |
| - } else { |
| - read_pos = m_FileLen - read_size; |
| - } |
| - } |
| - |
| - if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) |
| + if (static_cast<FX_FILESIZE>(read_size) > m_FileLen) |
| + read_size = static_cast<uint32_t>(m_FileLen); |
| + if (!ReadChar(read_pos, read_size)) |
| return FALSE; |
| - m_BufOffset = read_pos; |
| } |
| ch = m_pFileBuf[pos - m_BufOffset]; |
| m_Pos++; |
| @@ -92,27 +106,17 @@ FX_BOOL CPDF_SyntaxParser::GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch) { |
| if (pos >= m_FileLen) |
| return FALSE; |
| - if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { |
| + if (m_BufOffset >= pos || |
| + static_cast<FX_FILESIZE>(m_BufOffset + m_BufSize) <= pos) { |
| FX_FILESIZE read_pos; |
| - if (pos < (FX_FILESIZE)m_BufSize) |
| + if (pos < static_cast<FX_FILESIZE>(m_BufSize)) |
| read_pos = 0; |
| else |
| read_pos = pos - m_BufSize + 1; |
| - |
| uint32_t read_size = m_BufSize; |
| - if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { |
| - if (m_FileLen < (FX_FILESIZE)read_size) { |
| - read_pos = 0; |
| - read_size = (uint32_t)m_FileLen; |
| - } else { |
| - read_pos = m_FileLen - read_size; |
| - } |
| - } |
| - |
| - if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) |
| + if (!ReadChar(read_pos, read_size)) |
| return FALSE; |
| - m_BufOffset = read_pos; |
| } |
| ch = m_pFileBuf[pos - m_BufOffset]; |
| return TRUE; |
| @@ -215,30 +219,27 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { |
| CFX_ByteTextBuf buf; |
| int32_t parlevel = 0; |
| - int32_t status = 0; |
| + int32_t status = SyntaxParser_ReadNormal; |
| int32_t iEscCode = 0; |
| while (1) { |
| switch (status) { |
| - case 0: |
| + case SyntaxParser_ReadNormal: |
| if (ch == ')') { |
| - if (parlevel == 0) { |
| + if (parlevel == 0) |
| return buf.MakeString(); |
| - } |
| parlevel--; |
| - buf.AppendChar(')'); |
| } else if (ch == '(') { |
| parlevel++; |
| - buf.AppendChar('('); |
| - } else if (ch == '\\') { |
| - status = 1; |
| - } else { |
| - buf.AppendChar(ch); |
| } |
| + if (ch == '\\') |
| + status = SyntaxParser_ReadBackslash; |
| + else |
| + buf.AppendChar(ch); |
| break; |
| - case 1: |
| + case SyntaxParser_ReadBackslash: |
| if (ch >= '0' && ch <= '7') { |
| iEscCode = FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); |
| - status = 2; |
| + status = SyntaxParser_ReadOctal; |
| break; |
| } |
| @@ -253,38 +254,38 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { |
| } else if (ch == 'f') { |
| buf.AppendChar('\f'); |
| } else if (ch == '\r') { |
| - status = 4; |
| + status = SyntaxParser_ReadCarriageReturn; |
| break; |
| } else if (ch != '\n') { |
| buf.AppendChar(ch); |
| } |
| - status = 0; |
| + status = SyntaxParser_ReadNormal; |
| break; |
| - case 2: |
| + case SyntaxParser_ReadOctal: |
| if (ch >= '0' && ch <= '7') { |
| iEscCode = |
| iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); |
| - status = 3; |
| + status = SyntaxParser_ReadFinishOctal; |
| } else { |
| buf.AppendChar(iEscCode); |
| - status = 0; |
| + status = SyntaxParser_ReadNormal; |
| continue; |
| } |
| break; |
| - case 3: |
| + case SyntaxParser_ReadFinishOctal: |
| if (ch >= '0' && ch <= '7') { |
| iEscCode = |
| iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); |
| buf.AppendChar(iEscCode); |
| - status = 0; |
| + status = SyntaxParser_ReadNormal; |
|
dsinclair
2016/11/01 19:14:42
Move this before the if() as it happens in both br
npm
2016/11/01 20:09:10
Done.
|
| } else { |
| buf.AppendChar(iEscCode); |
| - status = 0; |
| + status = SyntaxParser_ReadNormal; |
| continue; |
| } |
| break; |
| - case 4: |
| - status = 0; |
| + case SyntaxParser_ReadCarriageReturn: |
| + status = SyntaxParser_ReadNormal; |
| if (ch != '\n') |
| continue; |
| break; |
| @@ -648,7 +649,9 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, |
| const CFX_ByteStringC kEndObjStr("endobj"); |
| CPDF_CryptoHandler* pCryptoHandler = |
| - objnum == (uint32_t)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); |
| + objnum == static_cast<uint32_t>(m_MetadataObjnum) |
| + ? nullptr |
| + : m_pCryptoHandler.get(); |
| if (!pCryptoHandler) { |
| FX_BOOL bSearchForKeyword = TRUE; |
| if (len >= 0) { |
| @@ -794,7 +797,9 @@ void CPDF_SyntaxParser::InitParser(IFX_SeekableReadStream* pFileAccess, |
| m_BufOffset = 0; |
| pFileAccess->ReadBlock( |
| m_pFileBuf, 0, |
| - (size_t)((FX_FILESIZE)m_BufSize > m_FileLen ? m_FileLen : m_BufSize)); |
| + static_cast<size_t>(static_cast<FX_FILESIZE>(m_BufSize) > m_FileLen |
| + ? m_FileLen |
| + : m_BufSize)); |
| } |
| uint32_t CPDF_SyntaxParser::GetDirectNum() { |
| @@ -905,62 +910,6 @@ FX_BOOL CPDF_SyntaxParser::SearchWord(const CFX_ByteStringC& tag, |
| return FALSE; |
| } |
| -int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, |
| - FX_BOOL bWholeWord, |
| - FX_FILESIZE limit) { |
| - int32_t ntags = 1; |
| - for (int i = 0; i < tags.GetLength(); ++i) { |
| - if (tags[i] == 0) |
| - ++ntags; |
| - } |
| - |
| - // Ensure that the input byte string happens to be nul-terminated. This |
| - // need not be the case, but the loop below uses this guarantee to put |
| - // the last pattern into the vector. |
| - ASSERT(tags[tags.GetLength()] == 0); |
| - std::vector<SearchTagRecord> patterns(ntags); |
| - uint32_t start = 0; |
| - uint32_t itag = 0; |
| - uint32_t max_len = 0; |
| - for (int i = 0; i <= tags.GetLength(); ++i) { |
| - if (tags[i] == 0) { |
| - uint32_t len = i - start; |
| - max_len = std::max(len, max_len); |
| - patterns[itag].m_bsTag = tags.Mid(start, len); |
| - patterns[itag].m_Offset = 0; |
| - start = i + 1; |
| - ++itag; |
| - } |
| - } |
| - |
| - const FX_FILESIZE pos_limit = m_Pos + limit; |
| - for (FX_FILESIZE pos = m_Pos; !limit || pos < pos_limit; ++pos) { |
| - uint8_t byte; |
| - if (!GetCharAt(pos, byte)) |
| - break; |
| - |
| - for (int i = 0; i < ntags; ++i) { |
| - SearchTagRecord& pat = patterns[i]; |
| - if (pat.m_bsTag[pat.m_Offset] != byte) { |
| - pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
| - continue; |
| - } |
| - |
| - ++pat.m_Offset; |
| - if (pat.m_Offset != pat.m_bsTag.GetLength()) |
| - continue; |
| - |
| - if (!bWholeWord || IsWholeWord(pos - pat.m_bsTag.GetLength(), limit, |
| - pat.m_bsTag, FALSE)) { |
| - return i; |
| - } |
| - |
| - pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
| - } |
| - } |
| - return -1; |
| -} |
| - |
| FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, |
| FX_FILESIZE limit) { |
| int32_t taglen = tag.GetLength(); |