summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/fpdfapi')
-rw-r--r--core/src/fpdfapi/fpdf_font/fpdf_font.cpp51
-rw-r--r--core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp44
-rw-r--r--core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp1
-rw-r--r--core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp64
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp32
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp163
-rw-r--r--core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp17
7 files changed, 140 insertions, 232 deletions
diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp
index 0529d3d35b..2d5e9780ad 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"
@@ -513,32 +514,19 @@ FX_DWORD CPDF_ToUnicodeMap::ReverseLookup(FX_WCHAR unicode) {
FX_DWORD CPDF_ToUnicodeMap::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 {
- break;
- }
- result = result * 16 + digit;
- }
+ for (int i = 1; i < len && std::isxdigit(buf[i]); ++i)
+ result = result * 16 + FXSYS_toHexDigit(buf[i]);
return result;
}
- for (int i = 0; i < len; i++) {
- if (buf[i] < '0' || buf[i] > '9') {
- break;
- }
- result = result * 10 + buf[i] - '0';
- }
+
+ for (int i = 0; i < len && std::isdigit(buf[i]); ++i)
+ result = result * 10 + FXSYS_toDecimalDigit(buf[i]);
+
return result;
}
static CFX_WideString StringDataAdd(CFX_WideString str) {
@@ -565,26 +553,15 @@ CFX_WideString CPDF_ToUnicodeMap::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 {
- break;
- }
- ch = ch * 16 + digit;
-
+ for (int i = 1; i < len && std::isxdigit(buf[i]); ++i) {
+ ch = ch * 16 + FXSYS_toHexDigit(buf[i]);
byte_pos++;
if (byte_pos == 4) {
result += ch;
@@ -594,8 +571,6 @@ CFX_WideString CPDF_ToUnicodeMap::StringToWideString(
}
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 43125d717a..2d1834c096 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"
@@ -698,30 +699,17 @@ void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) {
m_LastWord = word;
}
+// Static.
FX_DWORD CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) {
int num = 0;
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 {
- return num;
- }
- num = num * 16 + digit;
- }
- } else {
- for (int i = 0; i < word.GetLength(); i++) {
- if (word.GetAt(i) < '0' || word.GetAt(i) > '9') {
- return num;
- }
- num = num * 10 + word.GetAt(i) - '0';
- }
+ for (int i = 1; i < word.GetLength() && std::isxdigit(word.GetAt(i)); ++i)
+ num = num * 16 + FXSYS_toHexDigit(word.GetAt(i));
+ return num;
}
+
+ for (int i = 0; i < word.GetLength() && std::isdigit(word.GetAt(i)); ++i)
+ num = num * 10 + FXSYS_toDecimalDigit(word.GetAt(i));
return num;
}
@@ -745,13 +733,7 @@ bool CPDF_CMapParser::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] = FXSYS_toHexDigit(digit1) * 16 + FXSYS_toHexDigit(digit2);
}
FX_DWORD size = second.GetLength();
@@ -762,13 +744,7 @@ bool CPDF_CMapParser::CMap_GetCodeRange(CMap_CodeRange& range,
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] = FXSYS_toHexDigit(digit1) * 16 + FXSYS_toHexDigit(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 e095414ffe..72c999e9fc 100644
--- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
+++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
@@ -7,6 +7,7 @@
#include "../../../include/fpdfapi/fpdf_page.h"
#include "../../../include/fpdfapi/fpdf_module.h"
#include "../../../include/fpdfapi/fpdf_serial.h"
+#include "../../../include/fxcrt/fx_ext.h"
#include "pageint.h"
#define REQUIRE_PARAMS(count) \
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 187d6152e9..5e312e3eec 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>
@@ -788,7 +789,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() {
break;
case 1:
if (ch >= '0' && ch <= '7') {
- iEscCode = ch - '0';
+ iEscCode = FXSYS_toDecimalDigit(ch);
status = 2;
break;
}
@@ -813,7 +814,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() {
break;
case 2:
if (ch >= '0' && ch <= '7') {
- iEscCode = iEscCode * 8 + ch - '0';
+ iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch);
status = 3;
} else {
buf.AppendChar(iEscCode);
@@ -823,7 +824,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() {
break;
case 3:
if (ch >= '0' && ch <= '7') {
- iEscCode = iEscCode * 8 + ch - '0';
+ iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch);
buf.AppendChar(iEscCode);
status = 0;
} else {
@@ -856,50 +857,33 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() {
if (!PositionIsInBounds())
return CFX_ByteString();
- int ch = m_pBuf[m_Pos++];
CFX_ByteTextBuf buf;
- FX_BOOL bFirst = TRUE;
+ bool bFirst = true;
int code = 0;
- while (1) {
- 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 (bFirst) {
- code = (ch - 'a' + 10) * 16;
- } else {
- code += ch - 'a' + 10;
- buf.AppendChar((char)code);
- }
- bFirst = !bFirst;
- }
- if (!PositionIsInBounds())
+ while (PositionIsInBounds()) {
+ int ch = m_pBuf[m_Pos++];
+
+ if (ch == '>')
break;
- ch = m_pBuf[m_Pos++];
+ if (!std::isxdigit(ch))
+ continue;
+
+ int val = FXSYS_toHexDigit(ch);
+ if (bFirst) {
+ code = val * 16;
+ } else {
+ code += val;
+ buf.AppendByte((uint8_t)code);
+ }
+ bFirst = !bFirst;
}
- if (!bFirst) {
+ if (!bFirst)
buf.AppendChar((char)code);
- }
- if (buf.GetLength() > MAX_STRING_LENGTH) {
+
+ if (buf.GetLength() > MAX_STRING_LENGTH)
return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH);
- }
+
return buf.GetByteString();
}
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
index e68fcb6800..3ea554b2d8 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
@@ -128,37 +129,32 @@ FX_DWORD HexDecode(const uint8_t* src_buf,
}
dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
dest_size = 0;
- FX_BOOL bFirstDigit = TRUE;
+ bool bFirst = true;
for (i = 0; i < src_size; i++) {
uint8_t ch = src_buf[i];
if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
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;
- } else if (ch == '>') {
- i++;
+ if (ch == '>') {
+ ++i;
break;
- } else {
- continue;
}
- if (bFirstDigit) {
+ if (!std::isxdigit(ch))
+ continue;
+
+ int digit = FXSYS_toHexDigit(ch);
+ if (bFirst)
dest_buf[dest_size] = digit * 16;
- } else {
+ else
dest_buf[dest_size++] += digit;
- }
- bFirstDigit = !bFirstDigit;
+
+ bFirst = !bFirst;
}
- if (!bFirstDigit) {
+ if (!bFirst)
dest_size++;
- }
return i;
}
+
FX_DWORD RunLengthDecode(const uint8_t* src_buf,
FX_DWORD src_size,
uint8_t*& dest_buf,
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
index f5ada7c6eb..949c3a4f6f 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"
#include "parser_int.h"
@@ -163,85 +164,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') {
- m_FileVersion = (ch - '0') * 10;
- }
- if (!m_Syntax.GetCharAt(7, ch)) {
+ if (std::isdigit(ch))
+ m_FileVersion = FXSYS_toDecimalDigit(ch) * 10;
+
+ if (!m_Syntax.GetCharAt(7, ch))
return PDFPARSE_ERROR_FORMAT;
- }
- if (ch >= '0' && ch <= '9') {
- m_FileVersion += ch - '0';
- }
- if (m_Syntax.m_FileLen < m_Syntax.m_HeaderOffset + 9) {
+ if (std::isdigit(ch))
+ m_FileVersion += FXSYS_toDecimalDigit(ch);
+
+ 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);
@@ -250,13 +249,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 =
@@ -460,9 +458,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);
@@ -561,9 +558,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);
@@ -631,28 +627,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;
@@ -661,10 +661,10 @@ 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';
+ objnum = FXSYS_toDecimalDigit(byte);
} else if (byte == 't') {
status = 7;
inside_index = 1;
@@ -677,8 +677,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() {
}
break;
case 2:
- if (byte <= '9' && byte >= '0') {
- objnum = objnum * 10 + byte - '0';
+ if (std::isdigit(byte)) {
+ objnum = objnum * 10 + FXSYS_toDecimalDigit(byte);
break;
} else if (PDFCharIsWhitespace(byte)) {
status = 3;
@@ -689,10 +689,10 @@ 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';
+ gennum = FXSYS_toDecimalDigit(byte);
} else if (PDFCharIsWhitespace(byte)) {
break;
} else if (byte == 't') {
@@ -704,8 +704,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() {
}
break;
case 4:
- if (byte <= '9' && byte >= '0') {
- gennum = gennum * 10 + byte - '0';
+ if (std::isdigit(byte)) {
+ gennum = gennum * 10 + FXSYS_toDecimalDigit(byte);
break;
} else if (PDFCharIsWhitespace(byte)) {
status = 5;
@@ -720,9 +720,9 @@ 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';
+ gennum = FXSYS_toDecimalDigit(byte);
start_pos = start_pos1;
start_pos1 = pos + i;
status = 4;
@@ -1863,7 +1863,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() {
break;
case 1:
if (ch >= '0' && ch <= '7') {
- iEscCode = ch - '0';
+ iEscCode = FXSYS_toDecimalDigit(ch);
status = 2;
break;
}
@@ -1888,7 +1888,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() {
break;
case 2:
if (ch >= '0' && ch <= '7') {
- iEscCode = iEscCode * 8 + ch - '0';
+ iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch);
status = 3;
} else {
buf.AppendChar(iEscCode);
@@ -1898,7 +1898,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() {
break;
case 3:
if (ch >= '0' && ch <= '7') {
- iEscCode = iEscCode * 8 + ch - '0';
+ iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch);
buf.AppendChar(iEscCode);
status = 0;
} else {
@@ -1923,48 +1923,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;
+ 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 = FXSYS_toHexDigit(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..202f02ac77 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,8 @@ 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++ =
+ FXSYS_toHexDigit(pSrc[i + 1]) * 16 + FXSYS_toHexDigit(pSrc[i + 2]);
i += 2;
} else {
*pDest++ = pSrc[i];