diff options
author | dan sinclair <dsinclair@chromium.org> | 2015-11-10 13:33:04 -0500 |
---|---|---|
committer | dan sinclair <dsinclair@chromium.org> | 2015-11-10 13:33:04 -0500 |
commit | 0569ab0b11b723d9bca4ddd642b0cf8828c4bdd1 (patch) | |
tree | d0f6ba295574785c773536be7b5526680ed60f04 /core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | |
parent | e6e16954f0cdc324849fca9da883be8f131b3834 (diff) | |
download | pdfium-0569ab0b11b723d9bca4ddd642b0cf8828c4bdd1.tar.xz |
Revert x4 "Cleanup some numeric code."
This reverts commit b27902b8995bb3e003daed6b0811ed746763c68d.
Cleanup some numeric code.
This changes the various comparisons of char >= '0' && char <= '9' and
char < '0' || char > '9' to use std::isdigit checks. It also cleans up
a handful of hex to digit conversions to call one common method.
TBR=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1432973003 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp')
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 163 |
1 files changed, 89 insertions, 74 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 9d958521bd..2fa2762906 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -14,7 +14,6 @@ #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_parser.h" -#include "core/include/fxcrt/fx_ext.h" #include "core/include/fxcrt/fx_safe_types.h" #include "third_party/base/nonstd_unique_ptr.h" #include "third_party/base/stl_util.h" @@ -165,83 +164,85 @@ FX_DWORD CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, m_bXRefStream = FALSE; m_LastXRefOffset = 0; m_bOwnFileRead = bOwnFileRead; - int32_t offset = GetHeaderOffset(pFileAccess); if (offset == -1) { - if (bOwnFileRead && pFileAccess) + if (bOwnFileRead && pFileAccess) { pFileAccess->Release(); + } return PDFPARSE_ERROR_FORMAT; } m_Syntax.InitParser(pFileAccess, offset); - uint8_t ch; - if (!m_Syntax.GetCharAt(5, ch)) + if (!m_Syntax.GetCharAt(5, ch)) { return PDFPARSE_ERROR_FORMAT; - if (std::isdigit(ch)) - m_FileVersion = FXSYS_toDecimalDigit(ch) * 10; - - if (!m_Syntax.GetCharAt(7, ch)) + } + if (ch >= '0' && ch <= '9') { + m_FileVersion = (ch - '0') * 10; + } + if (!m_Syntax.GetCharAt(7, ch)) { return PDFPARSE_ERROR_FORMAT; - if (std::isdigit(ch)) - m_FileVersion += FXSYS_toDecimalDigit(ch); - - if (m_Syntax.m_FileLen < m_Syntax.m_HeaderOffset + 9) + } + if (ch >= '0' && ch <= '9') { + m_FileVersion += ch - '0'; + } + if (m_Syntax.m_FileLen < m_Syntax.m_HeaderOffset + 9) { return PDFPARSE_ERROR_FORMAT; - + } m_Syntax.RestorePos(m_Syntax.m_FileLen - m_Syntax.m_HeaderOffset - 9); - if (!bReParse) + if (!bReParse) { m_pDocument = new CPDF_Document(this); - + } FX_BOOL bXRefRebuilt = FALSE; if (m_Syntax.SearchWord(FX_BSTRC("startxref"), TRUE, FALSE, 4096)) { FX_FILESIZE startxref_offset = m_Syntax.SavePos(); void* pResult = FXSYS_bsearch(&startxref_offset, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), CompareFileSize); - if (!pResult) + if (pResult == NULL) { m_SortedOffset.Add(startxref_offset); - + } m_Syntax.GetKeyword(); FX_BOOL bNumber; CFX_ByteString xrefpos_str = m_Syntax.GetNextWord(bNumber); - if (!bNumber) + if (!bNumber) { return PDFPARSE_ERROR_FORMAT; - + } m_LastXRefOffset = (FX_FILESIZE)FXSYS_atoi64(xrefpos_str); if (!LoadAllCrossRefV4(m_LastXRefOffset) && !LoadAllCrossRefV5(m_LastXRefOffset)) { - if (!RebuildCrossRef()) + if (!RebuildCrossRef()) { return PDFPARSE_ERROR_FORMAT; - + } bXRefRebuilt = TRUE; m_LastXRefOffset = 0; } } else { - if (!RebuildCrossRef()) + if (!RebuildCrossRef()) { return PDFPARSE_ERROR_FORMAT; - + } bXRefRebuilt = TRUE; } FX_DWORD dwRet = SetEncryptHandler(); - if (dwRet != PDFPARSE_ERROR_SUCCESS) + if (dwRet != PDFPARSE_ERROR_SUCCESS) { return dwRet; - + } m_pDocument->LoadDoc(); - if (!m_pDocument->GetRoot() || m_pDocument->GetPageCount() == 0) { - if (bXRefRebuilt) + if (m_pDocument->GetRoot() == NULL || m_pDocument->GetPageCount() == 0) { + if (bXRefRebuilt) { return PDFPARSE_ERROR_FORMAT; - + } ReleaseEncryptHandler(); - if (!RebuildCrossRef()) + if (!RebuildCrossRef()) { return PDFPARSE_ERROR_FORMAT; - + } dwRet = SetEncryptHandler(); - if (dwRet != PDFPARSE_ERROR_SUCCESS) + if (dwRet != PDFPARSE_ERROR_SUCCESS) { return dwRet; - + } m_pDocument->LoadDoc(); - if (!m_pDocument->GetRoot()) + if (m_pDocument->GetRoot() == NULL) { return PDFPARSE_ERROR_FORMAT; + } } FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), CompareFileSize); @@ -250,12 +251,13 @@ FX_DWORD CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, ReleaseEncryptHandler(); RebuildCrossRef(); RootObjNum = GetRootObjNum(); - if (RootObjNum == 0) + if (RootObjNum == 0) { return PDFPARSE_ERROR_FORMAT; - + } dwRet = SetEncryptHandler(); - if (dwRet != PDFPARSE_ERROR_SUCCESS) + if (dwRet != PDFPARSE_ERROR_SUCCESS) { return dwRet; + } } if (m_pSecurityHandler && !m_pSecurityHandler->IsMetadataEncrypted()) { CPDF_Reference* pMetadata = @@ -459,8 +461,9 @@ FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos, int32_t offset = FXSYS_atoi(pEntry); if (offset == 0) { for (int32_t c = 0; c < 10; c++) { - if (!std::isdigit(pEntry[c])) + if (pEntry[c] < '0' || pEntry[c] > '9') { return FALSE; + } } } m_CrossRef.SetAtGrow(objnum, offset); @@ -559,8 +562,9 @@ bool CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, FX_FILESIZE offset = (FX_FILESIZE)FXSYS_atoi64(pEntry); if (offset == 0) { for (int32_t c = 0; c < 10; c++) { - if (!std::isdigit(pEntry[c])) + if (pEntry[c] < '0' || pEntry[c] > '9') { return false; + } } } m_CrossRef.SetAtGrow(objnum, offset); @@ -628,32 +632,28 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { uint8_t byte = buffer[i]; switch (status) { case 0: - if (PDFCharIsWhitespace(byte)) + if (PDFCharIsWhitespace(byte)) { status = 1; - - if (std::isdigit(byte)) { + } + if (byte <= '9' && byte >= '0') { --i; status = 1; } - if (byte == '%') { inside_index = 0; status = 9; } - if (byte == '(') { status = 10; depth = 1; } - if (byte == '<') { inside_index = 1; status = 11; } - - if (byte == '\\') + if (byte == '\\') { status = 13; - + } if (byte == 't') { status = 7; inside_index = 1; @@ -662,10 +662,10 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { case 1: if (PDFCharIsWhitespace(byte)) { break; - } else if (std::isdigit(byte)) { + } else if (byte <= '9' && byte >= '0') { start_pos = pos + i; status = 2; - objnum = FXSYS_toDecimalDigit(byte); + objnum = byte - '0'; } else if (byte == 't') { status = 7; inside_index = 1; @@ -678,8 +678,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 2: - if (std::isdigit(byte)) { - objnum = objnum * 10 + FXSYS_toDecimalDigit(byte); + if (byte <= '9' && byte >= '0') { + objnum = objnum * 10 + byte - '0'; break; } else if (PDFCharIsWhitespace(byte)) { status = 3; @@ -690,10 +690,10 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 3: - if (std::isdigit(byte)) { + if (byte <= '9' && byte >= '0') { start_pos1 = pos + i; status = 4; - gennum = FXSYS_toDecimalDigit(byte); + gennum = byte - '0'; } else if (PDFCharIsWhitespace(byte)) { break; } else if (byte == 't') { @@ -705,8 +705,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 4: - if (std::isdigit(byte)) { - gennum = gennum * 10 + FXSYS_toDecimalDigit(byte); + if (byte <= '9' && byte >= '0') { + gennum = gennum * 10 + byte - '0'; break; } else if (PDFCharIsWhitespace(byte)) { status = 5; @@ -721,9 +721,9 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { inside_index = 1; } else if (PDFCharIsWhitespace(byte)) { break; - } else if (std::isdigit(byte)) { + } else if (byte <= '9' && byte >= '0') { objnum = gennum; - gennum = FXSYS_toDecimalDigit(byte); + gennum = byte - '0'; start_pos = start_pos1; start_pos1 = pos + i; status = 4; @@ -1858,7 +1858,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 1: if (ch >= '0' && ch <= '7') { - iEscCode = FXSYS_toDecimalDigit(ch); + iEscCode = ch - '0'; status = 2; break; } @@ -1883,7 +1883,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 2: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = iEscCode * 8 + ch - '0'; status = 3; } else { buf.AppendChar(iEscCode); @@ -1893,7 +1893,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 3: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = iEscCode * 8 + ch - '0'; buf.AppendChar(iEscCode); status = 0; } else { @@ -1918,33 +1918,48 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { } CFX_ByteString CPDF_SyntaxParser::ReadHexString() { uint8_t ch; - if (!GetNextChar(ch)) + if (!GetNextChar(ch)) { return CFX_ByteString(); - + } CFX_BinaryBuf buf; - bool bFirst = true; + FX_BOOL bFirst = TRUE; uint8_t code = 0; while (1) { - if (ch == '>') + if (ch == '>') { break; - - if (std::isxdigit(ch)) { - int val = FXSYS_toHexDigit(ch); + } + if (ch >= '0' && ch <= '9') { + if (bFirst) { + code = (ch - '0') * 16; + } else { + code += ch - '0'; + buf.AppendByte((uint8_t)code); + } + bFirst = !bFirst; + } else if (ch >= 'A' && ch <= 'F') { if (bFirst) { - code = val * 16; + code = (ch - 'A' + 10) * 16; } else { - code += val; + code += ch - 'A' + 10; + buf.AppendByte((uint8_t)code); + } + bFirst = !bFirst; + } else if (ch >= 'a' && ch <= 'f') { + if (bFirst) { + code = (ch - 'a' + 10) * 16; + } else { + code += ch - 'a' + 10; buf.AppendByte((uint8_t)code); } bFirst = !bFirst; } - - if (!GetNextChar(ch)) + if (!GetNextChar(ch)) { break; + } } - if (!bFirst) + if (!bFirst) { buf.AppendByte((uint8_t)code); - + } return buf.GetByteString(); } void CPDF_SyntaxParser::ToNextLine() { |