diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2015-10-29 14:56:26 -0400 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2015-10-29 14:56:26 -0400 |
commit | 589f7e0a57675efce9810c15a3e9b7c49bf0bc90 (patch) | |
tree | 6839707c05c00f227744ffc3788665cb1cb5b7bd /core/src/fpdfapi/fpdf_parser | |
parent | 6ed0028f1cf12ee53e9e7f657b7ba186c77a42f3 (diff) | |
download | pdfium-589f7e0a57675efce9810c15a3e9b7c49bf0bc90.tar.xz |
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.
R=thestig@chromium.org
Review URL: https://codereview.chromium.org/1415933005 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 16 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 141 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 16 |
3 files changed, 73 insertions, 100 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 255d0ce29c..eaff29d812 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -8,6 +8,7 @@ #include "../../../include/fpdfapi/fpdf_parser.h" #include "../../../include/fpdfapi/fpdf_module.h" #include "../../../include/fxcodec/fx_codec.h" +#include "../../../include/fxcrt/fx_ext.h" #define _STREAM_MAX_SIZE_ 20 * 1024 * 1024 @@ -135,23 +136,20 @@ FX_DWORD _HexDecode(const uint8_t* src_buf, continue; int digit; - if (ch <= '9' && ch >= '0') { - digit = ch - '0'; - } else if (ch <= 'f' && ch >= 'a') { - digit = ch - 'a' + 10; - } else if (ch <= 'F' && ch >= 'A') { - digit = ch - 'A' + 10; + if (std::isxdigit(ch)) { + digit = HexCharToDigit(ch); } else if (ch == '>') { i++; break; } else { continue; } - if (bFirstDigit) { + + if (bFirstDigit) dest_buf[dest_size] = digit * 16; - } else { + else dest_buf[dest_size++] += digit; - } + bFirstDigit = !bFirstDigit; } if (!bFirstDigit) { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 4ce196e90d..fa114f9cfb 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -13,6 +13,7 @@ #include "../../../include/fpdfapi/fpdf_module.h" #include "../../../include/fpdfapi/fpdf_page.h" #include "../../../include/fpdfapi/fpdf_parser.h" +#include "../../../include/fxcrt/fx_ext.h" #include "../../../include/fxcrt/fx_safe_types.h" #include "../fpdf_page/pageint.h" @@ -162,85 +163,83 @@ 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 (ch >= '0' && ch <= '9') { + if (std::isdigit(ch)) m_FileVersion = (ch - '0') * 10; - } - if (!m_Syntax.GetCharAt(7, ch)) { + + if (!m_Syntax.GetCharAt(7, ch)) return PDFPARSE_ERROR_FORMAT; - } - if (ch >= '0' && ch <= '9') { + if (std::isdigit(ch)) m_FileVersion += ch - '0'; - } - if (m_Syntax.m_FileLen < m_Syntax.m_HeaderOffset + 9) { + + 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 == NULL) { + if (!pResult) 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() == NULL || m_pDocument->GetPageCount() == 0) { - if (bXRefRebuilt) { + if (!m_pDocument->GetRoot() || 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() == NULL) { + if (!m_pDocument->GetRoot()) return PDFPARSE_ERROR_FORMAT; - } } FXSYS_qsort(m_SortedOffset.GetData(), m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), CompareFileSize); @@ -249,13 +248,12 @@ 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,9 +457,8 @@ 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 (pEntry[c] < '0' || pEntry[c] > '9') { + if (!std::isdigit(pEntry[c])) return FALSE; - } } } m_CrossRef.SetAtGrow(objnum, offset); @@ -560,9 +557,8 @@ 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 (pEntry[c] < '0' || pEntry[c] > '9') { + if (!std::isdigit(pEntry[c])) return false; - } } } m_CrossRef.SetAtGrow(objnum, offset); @@ -630,28 +626,32 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { uint8_t byte = buffer[i]; switch (status) { case 0: - if (PDFCharIsWhitespace(byte)) { + if (PDFCharIsWhitespace(byte)) status = 1; - } - if (byte <= '9' && byte >= '0') { + + if (std::isdigit(byte)) { --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; @@ -660,7 +660,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { case 1: if (PDFCharIsWhitespace(byte)) { break; - } else if (byte <= '9' && byte >= '0') { + } else if (std::isdigit(byte)) { start_pos = pos + i; status = 2; objnum = byte - '0'; @@ -676,7 +676,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 2: - if (byte <= '9' && byte >= '0') { + if (std::isdigit(byte)) { objnum = objnum * 10 + byte - '0'; break; } else if (PDFCharIsWhitespace(byte)) { @@ -688,7 +688,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 3: - if (byte <= '9' && byte >= '0') { + if (std::isdigit(byte)) { start_pos1 = pos + i; status = 4; gennum = byte - '0'; @@ -703,7 +703,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } break; case 4: - if (byte <= '9' && byte >= '0') { + if (std::isdigit(byte)) { gennum = gennum * 10 + byte - '0'; break; } else if (PDFCharIsWhitespace(byte)) { @@ -719,7 +719,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { inside_index = 1; } else if (PDFCharIsWhitespace(byte)) { break; - } else if (byte <= '9' && byte >= '0') { + } else if (std::isdigit(byte)) { objnum = gennum; gennum = byte - '0'; start_pos = start_pos1; @@ -1922,48 +1922,33 @@ 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; FX_BOOL bFirst = TRUE; uint8_t code = 0; while (1) { - if (ch == '>') { + if (ch == '>') break; - } - 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 = (ch - 'A' + 10) * 16; - } else { - code += ch - 'A' + 10; - buf.AppendByte((uint8_t)code); - } - bFirst = !bFirst; - } else if (ch >= 'a' && ch <= 'f') { + + if (std::isxdigit(ch)) { + int val = HexCharToDigit(ch); if (bFirst) { - code = (ch - 'a' + 10) * 16; + code = val * 16; } else { - code += ch - 'a' + 10; + code += val; 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() { diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index 335101e85b..d7c4136447 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -5,6 +5,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com #include "../../../include/fpdfapi/fpdf_parser.h" +#include "../../../include/fxcrt/fx_ext.h" // Indexed by 8-bit character code, contains either: // 'W' - for whitespace: NUL, TAB, CR, LF, FF, 0x80, 0xff @@ -279,18 +280,7 @@ FX_BOOL CPDF_SimpleParser::FindTagParam(const CFX_ByteStringC& token, } return FALSE; } -static int _hex2dec(char ch) { - if (ch >= '0' && ch <= '9') { - return ch - '0'; - } - if (ch >= 'a' && ch <= 'f') { - return ch - 'a' + 10; - } - if (ch >= 'A' && ch <= 'F') { - return ch - 'A' + 10; - } - return 0; -} + CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) { int size = bstr.GetLength(); const FX_CHAR* pSrc = bstr.GetCStr(); @@ -302,7 +292,7 @@ CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) { FX_CHAR* pDest = pDestStart; for (int i = 0; i < size; i++) { if (pSrc[i] == '#' && i < size - 2) { - *pDest++ = _hex2dec(pSrc[i + 1]) * 16 + _hex2dec(pSrc[i + 2]); + *pDest++ = HexCharToDigit(pSrc[i + 1]) * 16 + HexCharToDigit(pSrc[i + 2]); i += 2; } else { *pDest++ = pSrc[i]; |