summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2015-10-29 14:56:26 -0400
committerDan Sinclair <dsinclair@chromium.org>2015-10-29 14:56:26 -0400
commit589f7e0a57675efce9810c15a3e9b7c49bf0bc90 (patch)
tree6839707c05c00f227744ffc3788665cb1cb5b7bd
parent6ed0028f1cf12ee53e9e7f657b7ba186c77a42f3 (diff)
downloadpdfium-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 .
-rw-r--r--BUILD.gn1
-rw-r--r--core/include/fxcrt/fx_ext.h10
-rw-r--r--core/src/fpdfapi/fpdf_font/fpdf_font.cpp41
-rw-r--r--core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp32
-rw-r--r--core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp2
-rw-r--r--core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp31
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp16
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp141
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp16
-rw-r--r--core/src/fpdftext/fpdf_text.cpp4
-rw-r--r--core/src/fpdftext/fpdf_text_int.cpp5
-rw-r--r--core/src/fxcodec/codec/fx_codec.cpp36
-rw-r--r--core/src/fxcrt/fx_basic_bstring.cpp4
-rw-r--r--core/src/fxcrt/fx_basic_gcc.cpp11
-rw-r--r--core/src/fxcrt/fx_basic_util.cpp8
-rw-r--r--core/src/fxcrt/fx_basic_wstring.cpp4
-rw-r--r--core/src/fxcrt/fx_extension.cpp9
-rw-r--r--core/src/fxcrt/fx_extension_unittest.cpp14
-rw-r--r--fpdfsdk/src/fsdk_baseannot.cpp18
-rw-r--r--fpdfsdk/src/javascript/PublicMethods.cpp2
-rw-r--r--fpdfsdk/src/javascript/util.cpp4
-rw-r--r--pdfium.gyp1
22 files changed, 177 insertions, 233 deletions
diff --git a/BUILD.gn b/BUILD.gn
index abd189b8eb..903e901046 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -757,6 +757,7 @@ test("pdfium_unittests") {
"core/src/fxcrt/fx_basic_memmgr_unittest.cpp",
"core/src/fxcrt/fx_basic_wstring_unittest.cpp",
"core/src/fxcrt/fx_bidi_unittest.cpp",
+ "core/src/fxcrt/fx_extension_unittest.cpp",
"core/src/fxcrt/fx_system_unittest.cpp",
"third_party/base/nonstd_unique_ptr_unittest.cpp",
]
diff --git a/core/include/fxcrt/fx_ext.h b/core/include/fxcrt/fx_ext.h
index c24955fb7c..a31d10040a 100644
--- a/core/include/fxcrt/fx_ext.h
+++ b/core/include/fxcrt/fx_ext.h
@@ -13,6 +13,8 @@
extern "C" {
#endif
+int HexCharToDigit(char c);
+
FX_FLOAT FXSYS_tan(FX_FLOAT a);
FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x);
FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr,
@@ -45,13 +47,6 @@ FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr,
int32_t iLength,
FX_BOOL bIgnoreCase = FALSE);
-#ifdef __cplusplus
-}
-#endif
-#ifdef __cplusplus
-extern "C" {
-#endif
-
void* FX_Random_MT_Start(FX_DWORD dwSeed);
FX_DWORD FX_Random_MT_Generate(void* pContext);
@@ -66,6 +61,7 @@ void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount);
#ifdef __cplusplus
}
#endif
+
template <class baseType>
class CFX_SSortTemplate {
public:
diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
index 543816b03a..e5389f8c77 100644
--- a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
+++ b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
@@ -8,6 +8,7 @@
#include "../../../include/fpdfapi/fpdf_page.h"
#include "../../../include/fpdfapi/fpdf_pageobj.h"
#include "../../../include/fpdfapi/fpdf_resource.h"
+#include "../../../include/fxcrt/fx_ext.h"
#include "../../../include/fxge/fx_freetype.h"
#include "../fpdf_page/pageint.h"
#include "font_int.h"
@@ -511,32 +512,25 @@ FX_DWORD CPDF_ToUnicodeMap::ReverseLookup(FX_WCHAR unicode) {
static FX_DWORD _StringToCode(const CFX_ByteStringC& str) {
const FX_CHAR* buf = str.GetCStr();
int len = str.GetLength();
- if (len == 0) {
+ if (len == 0)
return 0;
- }
+
int result = 0;
if (buf[0] == '<') {
for (int i = 1; i < len; i++) {
- int digit;
- if (buf[i] >= '0' && buf[i] <= '9') {
- digit = buf[i] - '0';
- } else if (buf[i] >= 'a' && buf[i] <= 'f') {
- digit = buf[i] - 'a' + 10;
- } else if (buf[i] >= 'A' && buf[i] <= 'F') {
- digit = buf[i] - 'A' + 10;
- } else {
+ if (!std::isxdigit(buf[i]))
break;
- }
- result = result * 16 + digit;
+ result = result * 16 + HexCharToDigit(buf[i]);
}
return result;
}
+
for (int i = 0; i < len; i++) {
- if (buf[i] < '0' || buf[i] > '9') {
+ if (!std::isdigit(buf[i]))
break;
- }
result = result * 10 + buf[i] - '0';
}
+
return result;
}
static CFX_WideString _StringDataAdd(CFX_WideString str) {
@@ -560,25 +554,18 @@ static CFX_WideString _StringDataAdd(CFX_WideString str) {
static CFX_WideString _StringToWideString(const CFX_ByteStringC& str) {
const FX_CHAR* buf = str.GetCStr();
int len = str.GetLength();
- if (len == 0) {
+ if (len == 0)
return CFX_WideString();
- }
+
CFX_WideString result;
if (buf[0] == '<') {
int byte_pos = 0;
FX_WCHAR ch = 0;
for (int i = 1; i < len; i++) {
- int digit;
- if (buf[i] >= '0' && buf[i] <= '9') {
- digit = buf[i] - '0';
- } else if (buf[i] >= 'a' && buf[i] <= 'f') {
- digit = buf[i] - 'a' + 10;
- } else if (buf[i] >= 'A' && buf[i] <= 'F') {
- digit = buf[i] - 'A' + 10;
- } else {
+ if (!std::isxdigit(buf[i]))
break;
- }
- ch = ch * 16 + digit;
+
+ ch = ch * 16 + HexCharToDigit(buf[i]);
byte_pos++;
if (byte_pos == 4) {
result += ch;
@@ -588,8 +575,6 @@ static CFX_WideString _StringToWideString(const CFX_ByteStringC& str) {
}
return result;
}
- if (buf[0] == '(') {
- }
return result;
}
void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) {
diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp
index 02f0933f36..165ef5bad5 100644
--- a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp
+++ b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp
@@ -7,6 +7,7 @@
#include "../../../include/fpdfapi/fpdf_module.h"
#include "../../../include/fpdfapi/fpdf_page.h"
#include "../../../include/fpdfapi/fpdf_resource.h"
+#include "../../../include/fxcrt/fx_ext.h"
#include "../../../include/fxge/fx_freetype.h"
#include "../../../include/fxge/fx_ge.h"
#include "../fpdf_cmaps/cmap_int.h"
@@ -190,22 +191,15 @@ FX_DWORD CMap_GetCode(const CFX_ByteStringC& word) {
if (word.GetAt(0) == '<') {
for (int i = 1; i < word.GetLength(); i++) {
uint8_t digit = word.GetAt(i);
- if (digit >= '0' && digit <= '9') {
- digit = digit - '0';
- } else if (digit >= 'a' && digit <= 'f') {
- digit = digit - 'a' + 10;
- } else if (digit >= 'A' && digit <= 'F') {
- digit = digit - 'A' + 10;
- } else {
+ if (!std::isxdigit(digit))
return num;
- }
- num = num * 16 + digit;
+ num = num * 16 + HexCharToDigit(digit);
}
} else {
for (int i = 0; i < word.GetLength(); i++) {
- if (word.GetAt(i) < '0' || word.GetAt(i) > '9') {
+ if (!std::isdigit(word.GetAt(i)))
return num;
- }
+
num = num * 10 + word.GetAt(i) - '0';
}
}
@@ -231,13 +225,7 @@ bool CMap_GetCodeRange(CMap_CodeRange& range,
for (i = 0; i < range.m_CharSize; ++i) {
uint8_t digit1 = first.GetAt(i * 2 + 1);
uint8_t digit2 = first.GetAt(i * 2 + 2);
- uint8_t byte = (digit1 >= '0' && digit1 <= '9')
- ? (digit1 - '0')
- : ((digit1 & 0xdf) - 'A' + 10);
- byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9')
- ? (digit2 - '0')
- : ((digit2 & 0xdf) - 'A' + 10));
- range.m_Lower[i] = byte;
+ range.m_Lower[i] = HexCharToDigit(digit1) * 16 + HexCharToDigit(digit2);
}
FX_DWORD size = second.GetLength();
@@ -246,13 +234,7 @@ bool CMap_GetCodeRange(CMap_CodeRange& range,
((FX_DWORD)i * 2 + 1 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 1) : 0;
uint8_t digit2 =
((FX_DWORD)i * 2 + 2 < size) ? second.GetAt((FX_STRSIZE)i * 2 + 2) : 0;
- uint8_t byte = (digit1 >= '0' && digit1 <= '9')
- ? (digit1 - '0')
- : ((digit1 & 0xdf) - 'A' + 10);
- byte = byte * 16 + ((digit2 >= '0' && digit2 <= '9')
- ? (digit2 - '0')
- : ((digit2 & 0xdf) - 'A' + 10));
- range.m_Upper[i] = byte;
+ range.m_Upper[i] = HexCharToDigit(digit1) * 16 + HexCharToDigit(digit2);
}
return true;
}
diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
index 3057948959..cc9b0fcbe8 100644
--- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
+++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
@@ -1540,7 +1540,7 @@ CFX_ByteString _FPDF_ByteStringFromHex(CFX_BinaryBuf& src_buf) {
FX_DWORD size = src_buf.GetSize();
for (FX_DWORD i = 0; i < size; i++) {
uint8_t ch = str[i];
- if (ch >= '0' && ch <= '9') {
+ if (std::isdigit(ch)) {
if (bFirst) {
code = (ch - '0') * 16;
} else {
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 1fa27e3805..88e2269c7a 100644
--- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp
+++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp
@@ -7,6 +7,7 @@
#include "../../../include/fpdfapi/fpdf_page.h"
#include "../../../include/fpdfapi/fpdf_module.h"
#include "../../../include/fxcodec/fx_codec.h"
+#include "../../../include/fxcrt/fx_ext.h"
#include "pageint.h"
#include <limits.h>
@@ -875,34 +876,20 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() {
FX_BOOL bFirst = TRUE;
int 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.AppendChar((char)code);
- }
- bFirst = !bFirst;
- } else if (ch >= 'A' && ch <= 'F') {
- if (bFirst) {
- code = (ch - 'A' + 10) * 16;
- } else {
- code += ch - 'A' + 10;
- buf.AppendChar((char)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;
- buf.AppendChar((char)code);
+ code += val;
+ buf.AppendByte((uint8_t)code);
}
bFirst = !bFirst;
}
+
if (!PositionIsInBounds())
break;
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];
diff --git a/core/src/fpdftext/fpdf_text.cpp b/core/src/fpdftext/fpdf_text.cpp
index 9ecbc21bda..e9ad338025 100644
--- a/core/src/fpdftext/fpdf_text.cpp
+++ b/core/src/fpdftext/fpdf_text.cpp
@@ -436,10 +436,8 @@ void NormalizeString(CFX_WideString& str) {
static FX_BOOL IsNumber(CFX_WideString& str) {
for (int i = 0; i < str.GetLength(); i++) {
FX_WCHAR ch = str[i];
- if ((ch < '0' || ch > '9') && ch != '-' && ch != '+' && ch != '.' &&
- ch != ' ') {
+ if (!std::isdigit(ch) && ch != '-' && ch != '+' && ch != '.' && ch != ' ')
return FALSE;
- }
}
return TRUE;
}
diff --git a/core/src/fpdftext/fpdf_text_int.cpp b/core/src/fpdftext/fpdf_text_int.cpp
index 9ab09e19e4..4459552c84 100644
--- a/core/src/fpdftext/fpdf_text_int.cpp
+++ b/core/src/fpdftext/fpdf_text_int.cpp
@@ -2433,12 +2433,11 @@ FX_BOOL CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText,
}
if ((char_left > 'A' && char_left < 'a') ||
(char_left > 'a' && char_left < 'z') ||
- (char_left > 0xfb00 && char_left < 0xfb06) ||
- (char_left >= '0' && char_left <= '9') ||
+ (char_left > 0xfb00 && char_left < 0xfb06) || std::isdigit(char_left) ||
(char_right > 'A' && char_right < 'a') ||
(char_right > 'a' && char_right < 'z') ||
(char_right > 0xfb00 && char_right < 0xfb06) ||
- (char_right >= '0' && char_right <= '9')) {
+ std::isdigit(char_right)) {
return FALSE;
}
if (!(('A' > char_left || char_left > 'Z') &&
diff --git a/core/src/fxcodec/codec/fx_codec.cpp b/core/src/fxcodec/codec/fx_codec.cpp
index 46f479e0b1..eca6505d12 100644
--- a/core/src/fxcodec/codec/fx_codec.cpp
+++ b/core/src/fxcodec/codec/fx_codec.cpp
@@ -157,13 +157,13 @@ extern "C" double FXstrtod(const char* nptr, char** endptr) {
return 0.0;
}
for (;; ptr++) {
- if (!e_number && !e_point && (*ptr == '\t' || *ptr == ' ')) {
+ if (!e_number && !e_point && (*ptr == '\t' || *ptr == ' '))
continue;
- }
- if (*ptr >= '0' && *ptr <= '9') {
- if (!e_number) {
+
+ if (std::isdigit(*ptr)) {
+ if (!e_number)
e_number = 1;
- }
+
if (!e_point) {
ret *= 10;
ret += (*ptr - '0');
@@ -188,29 +188,29 @@ extern "C" double FXstrtod(const char* nptr, char** endptr) {
}
}
if (e_number && (*ptr == 'e' || *ptr == 'E')) {
-#define EXPONENT_DETECT(ptr) \
- for (;; ptr++) { \
- if (*ptr < '0' || *ptr > '9') { \
- if (endptr) \
- *endptr = (char*)ptr; \
- break; \
- } else { \
- exp_ret *= 10; \
- exp_ret += (*ptr - '0'); \
- continue; \
- } \
+#define EXPONENT_DETECT(ptr) \
+ for (;; ptr++) { \
+ if (!std::isdigit(*ptr)) { \
+ if (endptr) \
+ *endptr = (char*)ptr; \
+ break; \
+ } else { \
+ exp_ret *= 10; \
+ exp_ret += (*ptr - '0'); \
+ continue; \
+ } \
}
exp_ptr = ptr++;
if (*ptr == '+' || *ptr == '-') {
exp_sig = (*ptr++ == '+') ? 1 : -1;
- if (*ptr < '0' || *ptr > '9') {
+ if (!std::isdigit(*ptr)) {
if (endptr) {
*endptr = (char*)exp_ptr;
}
break;
}
EXPONENT_DETECT(ptr);
- } else if (*ptr >= '0' && *ptr <= '9') {
+ } else if (std::isdigit(*ptr)) {
EXPONENT_DETECT(ptr);
} else {
if (endptr) {
diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp
index c706912d9d..d5ffcddb6d 100644
--- a/core/src/fxcrt/fx_basic_bstring.cpp
+++ b/core/src/fxcrt/fx_basic_bstring.cpp
@@ -493,7 +493,7 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) {
}
if (nWidth == 0) {
nWidth = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
+ for (; std::isdigit(*lpsz); lpsz++)
;
}
if (nWidth < 0 || nWidth > 128 * 1024) {
@@ -509,7 +509,7 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) {
lpsz++;
} else {
nPrecision = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
+ for (; std::isdigit(*lpsz); lpsz++)
;
}
}
diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp
index 6f17482156..71f5fe322f 100644
--- a/core/src/fxcrt/fx_basic_gcc.cpp
+++ b/core/src/fxcrt/fx_basic_gcc.cpp
@@ -12,21 +12,20 @@
template <class T, class STR_T>
T FXSYS_StrToInt(STR_T str) {
FX_BOOL neg = FALSE;
- if (str == NULL) {
+ if (!str)
return 0;
- }
+
if (*str == '-') {
neg = TRUE;
str++;
}
T num = 0;
while (*str) {
- if ((*str) < '0' || (*str) > '9') {
+ if (!std::isdigit(*str))
break;
- }
- if (num > (std::numeric_limits<T>::max() - 9) / 10) {
+ if (num > (std::numeric_limits<T>::max() - 9) / 10)
break;
- }
+
num = num * 10 + (*str) - '0';
str++;
}
diff --git a/core/src/fxcrt/fx_basic_util.cpp b/core/src/fxcrt/fx_basic_util.cpp
index 46a0dec1e5..71119082a2 100644
--- a/core/src/fxcrt/fx_basic_util.cpp
+++ b/core/src/fxcrt/fx_basic_util.cpp
@@ -101,13 +101,13 @@ void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) {
cc++;
}
while (cc < len) {
- if (str[cc] < '0' || str[cc] > '9') {
+ if (!std::isdigit(str[cc]))
break;
- }
+
integer = integer * 10 + str[cc] - '0';
- if (integer < 0) {
+ if (integer < 0)
break;
- }
+
cc++;
}
if (bNegative) {
diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp
index c097e1fc09..3310df7062 100644
--- a/core/src/fxcrt/fx_basic_wstring.cpp
+++ b/core/src/fxcrt/fx_basic_wstring.cpp
@@ -765,7 +765,7 @@ void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
}
if (nWidth == 0) {
nWidth = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz++)
+ for (; *lpsz != 0 && std::isdigit(*lpsz); lpsz++)
;
}
if (nWidth < 0 || nWidth > 128 * 1024) {
@@ -781,7 +781,7 @@ void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
lpsz++;
} else {
nPrecision = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
+ for (; *lpsz != 0 && std::isdigit(*lpsz); lpsz++)
;
}
}
diff --git a/core/src/fxcrt/fx_extension.cpp b/core/src/fxcrt/fx_extension.cpp
index d64a06d08b..a75cfb48df 100644
--- a/core/src/fxcrt/fx_extension.cpp
+++ b/core/src/fxcrt/fx_extension.cpp
@@ -13,6 +13,15 @@
#include <ctime>
#endif
+#include <cctype>
+
+int HexCharToDigit(char c) {
+ if (!std::isxdigit(c))
+ return 0;
+ char upchar = std::toupper(c);
+ return upchar > '9' ? upchar - 'A' + 10 : upchar - '0';
+}
+
IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) {
IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create();
if (!pFA) {
diff --git a/core/src/fxcrt/fx_extension_unittest.cpp b/core/src/fxcrt/fx_extension_unittest.cpp
new file mode 100644
index 0000000000..e2017fc3ed
--- /dev/null
+++ b/core/src/fxcrt/fx_extension_unittest.cpp
@@ -0,0 +1,14 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "../../include/fxcrt/fx_ext.h"
+
+TEST(fxcrt, HexCharToDigit) {
+ EXPECT_EQ(10, HexCharToDigit('a'));
+ EXPECT_EQ(10, HexCharToDigit('A'));
+ EXPECT_EQ(7, HexCharToDigit('7'));
+ EXPECT_EQ(0, HexCharToDigit('i'));
+}
diff --git a/fpdfsdk/src/fsdk_baseannot.cpp b/fpdfsdk/src/fsdk_baseannot.cpp
index c6731e6f56..ebf56ad293 100644
--- a/fpdfsdk/src/fsdk_baseannot.cpp
+++ b/fpdfsdk/src/fsdk_baseannot.cpp
@@ -221,7 +221,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
FX_CHAR ch;
while (i < strLength) {
ch = dtStr[i];
- if (ch >= '0' && ch <= '9')
+ if (std::isdigit(ch))
break;
i++;
}
@@ -234,7 +234,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -248,7 +248,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -262,7 +262,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -276,7 +276,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -290,7 +290,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -304,7 +304,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -325,7 +325,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
@@ -342,7 +342,7 @@ CPDFSDK_DateTime& CPDFSDK_DateTime::FromPDFDateTimeString(
ch = dtStr[i];
k = k * 10 + ch - '0';
j++;
- if (ch < '0' || ch > '9')
+ if (!std::isdigit(ch))
break;
i++;
}
diff --git a/fpdfsdk/src/javascript/PublicMethods.cpp b/fpdfsdk/src/javascript/PublicMethods.cpp
index e898214cd8..13239aeb1d 100644
--- a/fpdfsdk/src/javascript/PublicMethods.cpp
+++ b/fpdfsdk/src/javascript/PublicMethods.cpp
@@ -116,7 +116,7 @@ FX_BOOL CJS_PublicMethods::IsDigit(wchar_t ch) {
}
FX_BOOL CJS_PublicMethods::IsDigit(char ch) {
- return (ch >= '0' && ch <= '9');
+ return std::isdigit(ch);
}
FX_BOOL CJS_PublicMethods::IsAlphabetic(wchar_t ch) {
diff --git a/fpdfsdk/src/javascript/util.cpp b/fpdfsdk/src/javascript/util.cpp
index 14e15c1b6f..cf10bbd7b4 100644
--- a/fpdfsdk/src/javascript/util.cpp
+++ b/fpdfsdk/src/javascript/util.cpp
@@ -425,7 +425,7 @@ void util::printx(const std::string& cFormat,
break;
case 'X': {
while (itSource < iSize) {
- if ((cSource[itSource] >= '0' && cSource[itSource] <= '9') ||
+ if (std::isdigit(cSource[itSource]) ||
(cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
(cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
cPurpose += cSource[itSource];
@@ -450,7 +450,7 @@ void util::printx(const std::string& cFormat,
} break;
case '9': {
while (itSource < iSize) {
- if (cSource[itSource] >= '0' && cSource[itSource] <= '9') {
+ if (std::isdigit(cSource[itSource])) {
cPurpose += cSource[itSource];
itSource++;
break;
diff --git a/pdfium.gyp b/pdfium.gyp
index e98a4eee33..250e4e2584 100644
--- a/pdfium.gyp
+++ b/pdfium.gyp
@@ -721,6 +721,7 @@
'core/src/fxcrt/fx_basic_memmgr_unittest.cpp',
'core/src/fxcrt/fx_basic_wstring_unittest.cpp',
'core/src/fxcrt/fx_bidi_unittest.cpp',
+ 'core/src/fxcrt/fx_extension_unittest.cpp',
'core/src/fxcrt/fx_system_unittest.cpp',
'testing/fx_string_testhelpers.h',
'testing/fx_string_testhelpers.cpp',