summaryrefslogtreecommitdiff
path: root/core/src/fpdfapi/fpdf_parser
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2015-11-03 15:08:31 -0500
committerDan Sinclair <dsinclair@chromium.org>2015-11-03 15:08:31 -0500
commitda06e60fb5a095a91c9a4f509466667878624cb3 (patch)
treecf9af9e6cc40e05cdbcf3ee7271ccc684e805f20 /core/src/fpdfapi/fpdf_parser
parente0e922db5fb77df9a5a9cc802096f484ed21da1c (diff)
downloadpdfium-da06e60fb5a095a91c9a4f509466667878624cb3.tar.xz
Revert "Revert "Revert "Cleanup some numeric code."""
This reverts commit e0e922db5fb77df9a5a9cc802096f484ed21da1c. Broke Windows build. FAILED: ninja -t msvc -e environment.x86 -- "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe" /nologo /showIncludes /FC @obj\core\src\fxcrt\fxcrt.fx_basic_gcc.obj.rsp /c ..\..\core\src\fxcrt\fx_basic_gcc.cpp /Foobj\core\src\fxcrt\fxcrt.fx_basic_gcc.obj /Fdobj\fxcrt.cc.pdb e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_gcc.cpp(28) : error C2220: warning treated as error - no 'object' file generated e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_gcc.cpp(75) : see reference to function template instantiation 'T FXSYS_StrToInt<int32_t,const FX_WCHAR*>(STR_T)' being compiled with [ T=int32_t , STR_T=const FX_WCHAR * ] e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_gcc.cpp(28) : warning C4244: 'argument' : conversion from 'const FX_WCHAR' to 'char', possible loss of data FAILED: ninja -t msvc -e environment.x86 -- "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe" /nologo /showIncludes /FC @obj\core\src\fpdftext\fpdftext.fpdf_text.obj.rsp /c ..\..\core\src\fpdftext\fpdf_text.cpp /Foobj\core\src\fpdftext\fpdftext.fpdf_text.obj /Fdobj\fpdftext.cc.pdb e:\b\build\slave\windows\build\pdfium\core\src\fpdftext\fpdf_text.cpp(439) : error C2039: 'isdigit' : is not a member of 'std' FAILED: ninja -t msvc -e environment.x86 -- "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe" /nologo /showIncludes /FC @obj\core\src\fxcrt\fxcrt.fx_basic_wstring.obj.rsp /c ..\..\core\src\fxcrt\fx_basic_wstring.cpp /Foobj\core\src\fxcrt\fxcrt.fx_basic_wstring.obj /Fdobj\fxcrt.cc.pdb e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_wstring.cpp(973) : error C2220: warning treated as error - no 'object' file generated e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_wstring.cpp(973) : warning C4244: 'argument' : conversion from 'const FX_WCHAR' to 'char', possible loss of data e:\b\build\slave\windows\build\pdfium\core\src\fxcrt\fx_basic_wstring.cpp(981) : warning C4244: 'argument' : conversion from 'const FX_WCHAR' to 'char', possible loss of data FAILED: ninja -t msvc -e environment.x86 -- "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\cl.exe" /nologo /showIncludes /FC @obj\core\src\fpdftext\fpdftext.fpdf_text_int.obj.rsp /c ..\..\core\src\fpdftext\fpdf_text_int.cpp /Foobj\core\src\fpdftext\fpdftext.fpdf_text_int.obj /Fdobj\fpdftext.cc.pdb e:\b\build\slave\windows\build\pdfium\core\src\fpdftext\fpdf_text_int.cpp(2436) : error C2039: 'isdigit' : is not a member of 'std' e:\b\build\slave\windows\build\pdfium\core\src\fpdftext\fpdf_text_int.cpp(2440) : error C2039: 'isdigit' : is not a member of 'std' ninja: build stopped: subcommand failed. TBR=tsepez@chromium.org Review URL: https://codereview.chromium.org/1431683008 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-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
3 files changed, 120 insertions, 92 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
index 3ea554b2d8..e68fcb6800 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
@@ -8,7 +8,6 @@
#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
@@ -129,32 +128,37 @@ FX_DWORD HexDecode(const uint8_t* src_buf,
}
dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
dest_size = 0;
- bool bFirst = true;
+ FX_BOOL bFirstDigit = TRUE;
for (i = 0; i < src_size; i++) {
uint8_t ch = src_buf[i];
if (PDFCharIsLineEnding(ch) || ch == ' ' || ch == '\t')
continue;
- if (ch == '>') {
- ++i;
+ 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++;
break;
- }
- if (!std::isxdigit(ch))
+ } else {
continue;
-
- int digit = FXSYS_toHexDigit(ch);
- if (bFirst)
+ }
+ if (bFirstDigit) {
dest_buf[dest_size] = digit * 16;
- else
+ } else {
dest_buf[dest_size++] += digit;
-
- bFirst = !bFirst;
+ }
+ bFirstDigit = !bFirstDigit;
}
- if (!bFirst)
+ if (!bFirstDigit) {
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 949c3a4f6f..f5ada7c6eb 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
@@ -13,7 +13,6 @@
#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"
@@ -164,83 +163,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);
@@ -249,12 +250,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 =
@@ -458,8 +460,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);
@@ -558,8 +561,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);
@@ -627,32 +631,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;
@@ -661,10 +661,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;
@@ -677,8 +677,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;
@@ -689,10 +689,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') {
@@ -704,8 +704,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;
@@ -720,9 +720,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;
@@ -1863,7 +1863,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() {
break;
case 1:
if (ch >= '0' && ch <= '7') {
- iEscCode = FXSYS_toDecimalDigit(ch);
+ iEscCode = ch - '0';
status = 2;
break;
}
@@ -1888,7 +1888,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);
@@ -1898,7 +1898,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 {
@@ -1923,33 +1923,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() {
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
index 202f02ac77..335101e85b 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp
@@ -5,7 +5,6 @@
// 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
@@ -280,7 +279,18 @@ 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();
@@ -292,8 +302,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++ =
- FXSYS_toHexDigit(pSrc[i + 1]) * 16 + FXSYS_toHexDigit(pSrc[i + 2]);
+ *pDest++ = _hex2dec(pSrc[i + 1]) * 16 + _hex2dec(pSrc[i + 2]);
i += 2;
} else {
*pDest++ = pSrc[i];