Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: core/fpdfapi/parser/cpdf_syntax_parser.cpp

Issue 2469833002: Clean up CPDF_SyntaxParser a little bit (Closed)
Patch Set: Replace C-cast with static_casts Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_syntax_parser.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" 7 #include "core/fpdfapi/parser/cpdf_syntax_parser.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "core/fxcrt/fx_ext.h" 24 #include "core/fxcrt/fx_ext.h"
25 #include "third_party/base/numerics/safe_math.h" 25 #include "third_party/base/numerics/safe_math.h"
26 26
27 namespace { 27 namespace {
28 28
29 struct SearchTagRecord { 29 struct SearchTagRecord {
30 CFX_ByteStringC m_bsTag; 30 CFX_ByteStringC m_bsTag;
31 FX_STRSIZE m_Offset; 31 FX_STRSIZE m_Offset;
32 }; 32 };
33 33
34 enum SyntaxParser_ReadStatus {
dsinclair 2016/11/01 19:14:42 Make this an enum class then you can drop the Synt
npm 2016/11/01 20:09:10 Done.
35 SyntaxParser_ReadNormal,
36 SyntaxParser_ReadBackslash,
37 SyntaxParser_ReadOctal,
38 SyntaxParser_ReadFinishOctal,
39 SyntaxParser_ReadCarriageReturn
40 };
41
34 } // namespace 42 } // namespace
35 43
36 // static 44 // static
37 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; 45 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0;
38 46
39 CPDF_SyntaxParser::CPDF_SyntaxParser() 47 CPDF_SyntaxParser::CPDF_SyntaxParser()
40 : CPDF_SyntaxParser(CFX_WeakPtr<CFX_ByteStringPool>()) {} 48 : CPDF_SyntaxParser(CFX_WeakPtr<CFX_ByteStringPool>()) {}
41 49
42 CPDF_SyntaxParser::CPDF_SyntaxParser( 50 CPDF_SyntaxParser::CPDF_SyntaxParser(
43 const CFX_WeakPtr<CFX_ByteStringPool>& pPool) 51 const CFX_WeakPtr<CFX_ByteStringPool>& pPool)
44 : m_MetadataObjnum(0), 52 : m_MetadataObjnum(0),
45 m_pFileAccess(nullptr), 53 m_pFileAccess(nullptr),
46 m_pFileBuf(nullptr), 54 m_pFileBuf(nullptr),
47 m_BufSize(CPDF_ModuleMgr::kFileBufSize), 55 m_BufSize(CPDF_ModuleMgr::kFileBufSize),
48 m_pPool(pPool) {} 56 m_pPool(pPool) {}
49 57
50 CPDF_SyntaxParser::~CPDF_SyntaxParser() { 58 CPDF_SyntaxParser::~CPDF_SyntaxParser() {
51 FX_Free(m_pFileBuf); 59 FX_Free(m_pFileBuf);
52 } 60 }
53 61
54 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { 62 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) {
55 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos); 63 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos);
56 m_Pos = pos; 64 m_Pos = pos;
57 return GetNextChar(ch); 65 return GetNextChar(ch);
58 } 66 }
59 67
68 FX_BOOL CPDF_SyntaxParser::ReadChar(FX_FILESIZE read_pos, uint32_t read_size) {
dsinclair 2016/11/01 19:14:42 Should this be ReadChars? It will potentially read
npm 2016/11/01 20:09:10 But it still represents a single char, as it is ca
69 if (static_cast<FX_FILESIZE>(read_pos + read_size) > m_FileLen) {
70 if (m_FileLen < static_cast<FX_FILESIZE>(read_size)) {
71 read_pos = 0;
72 read_size = static_cast<uint32_t>(m_FileLen);
73 } else {
74 read_pos = m_FileLen - read_size;
75 }
76 }
77 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size))
78 return FALSE;
79
80 m_BufOffset = read_pos;
81 return TRUE;
82 }
83
60 FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) { 84 FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) {
61 FX_FILESIZE pos = m_Pos + m_HeaderOffset; 85 FX_FILESIZE pos = m_Pos + m_HeaderOffset;
62 if (pos >= m_FileLen) 86 if (pos >= m_FileLen)
63 return FALSE; 87 return FALSE;
64 88
65 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { 89 if (m_BufOffset >= pos ||
90 static_cast<FX_FILESIZE>(m_BufOffset + m_BufSize) <= pos) {
66 FX_FILESIZE read_pos = pos; 91 FX_FILESIZE read_pos = pos;
67 uint32_t read_size = m_BufSize; 92 uint32_t read_size = m_BufSize;
68 if ((FX_FILESIZE)read_size > m_FileLen) 93 if (static_cast<FX_FILESIZE>(read_size) > m_FileLen)
69 read_size = (uint32_t)m_FileLen; 94 read_size = static_cast<uint32_t>(m_FileLen);
70 95 if (!ReadChar(read_pos, read_size))
71 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) {
72 if (m_FileLen < (FX_FILESIZE)read_size) {
73 read_pos = 0;
74 read_size = (uint32_t)m_FileLen;
75 } else {
76 read_pos = m_FileLen - read_size;
77 }
78 }
79
80 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size))
81 return FALSE; 96 return FALSE;
82 97
83 m_BufOffset = read_pos;
84 } 98 }
85 ch = m_pFileBuf[pos - m_BufOffset]; 99 ch = m_pFileBuf[pos - m_BufOffset];
86 m_Pos++; 100 m_Pos++;
87 return TRUE; 101 return TRUE;
88 } 102 }
89 103
90 FX_BOOL CPDF_SyntaxParser::GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch) { 104 FX_BOOL CPDF_SyntaxParser::GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch) {
91 pos += m_HeaderOffset; 105 pos += m_HeaderOffset;
92 if (pos >= m_FileLen) 106 if (pos >= m_FileLen)
93 return FALSE; 107 return FALSE;
94 108
95 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { 109 if (m_BufOffset >= pos ||
110 static_cast<FX_FILESIZE>(m_BufOffset + m_BufSize) <= pos) {
96 FX_FILESIZE read_pos; 111 FX_FILESIZE read_pos;
97 if (pos < (FX_FILESIZE)m_BufSize) 112 if (pos < static_cast<FX_FILESIZE>(m_BufSize))
98 read_pos = 0; 113 read_pos = 0;
99 else 114 else
100 read_pos = pos - m_BufSize + 1; 115 read_pos = pos - m_BufSize + 1;
101
102 uint32_t read_size = m_BufSize; 116 uint32_t read_size = m_BufSize;
103 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { 117 if (!ReadChar(read_pos, read_size))
104 if (m_FileLen < (FX_FILESIZE)read_size) {
105 read_pos = 0;
106 read_size = (uint32_t)m_FileLen;
107 } else {
108 read_pos = m_FileLen - read_size;
109 }
110 }
111
112 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size))
113 return FALSE; 118 return FALSE;
114 119
115 m_BufOffset = read_pos;
116 } 120 }
117 ch = m_pFileBuf[pos - m_BufOffset]; 121 ch = m_pFileBuf[pos - m_BufOffset];
118 return TRUE; 122 return TRUE;
119 } 123 }
120 124
121 FX_BOOL CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, uint32_t size) { 125 FX_BOOL CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, uint32_t size) {
122 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size)) 126 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size))
123 return FALSE; 127 return FALSE;
124 m_Pos += size; 128 m_Pos += size;
125 return TRUE; 129 return TRUE;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 } 212 }
209 } 213 }
210 214
211 CFX_ByteString CPDF_SyntaxParser::ReadString() { 215 CFX_ByteString CPDF_SyntaxParser::ReadString() {
212 uint8_t ch; 216 uint8_t ch;
213 if (!GetNextChar(ch)) 217 if (!GetNextChar(ch))
214 return CFX_ByteString(); 218 return CFX_ByteString();
215 219
216 CFX_ByteTextBuf buf; 220 CFX_ByteTextBuf buf;
217 int32_t parlevel = 0; 221 int32_t parlevel = 0;
218 int32_t status = 0; 222 int32_t status = SyntaxParser_ReadNormal;
219 int32_t iEscCode = 0; 223 int32_t iEscCode = 0;
220 while (1) { 224 while (1) {
221 switch (status) { 225 switch (status) {
222 case 0: 226 case SyntaxParser_ReadNormal:
223 if (ch == ')') { 227 if (ch == ')') {
224 if (parlevel == 0) { 228 if (parlevel == 0)
225 return buf.MakeString(); 229 return buf.MakeString();
226 }
227 parlevel--; 230 parlevel--;
228 buf.AppendChar(')');
229 } else if (ch == '(') { 231 } else if (ch == '(') {
230 parlevel++; 232 parlevel++;
231 buf.AppendChar('('); 233 }
232 } else if (ch == '\\') { 234 if (ch == '\\')
233 status = 1; 235 status = SyntaxParser_ReadBackslash;
234 } else { 236 else
235 buf.AppendChar(ch); 237 buf.AppendChar(ch);
236 }
237 break; 238 break;
238 case 1: 239 case SyntaxParser_ReadBackslash:
239 if (ch >= '0' && ch <= '7') { 240 if (ch >= '0' && ch <= '7') {
240 iEscCode = FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); 241 iEscCode = FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch));
241 status = 2; 242 status = SyntaxParser_ReadOctal;
242 break; 243 break;
243 } 244 }
244 245
245 if (ch == 'n') { 246 if (ch == 'n') {
246 buf.AppendChar('\n'); 247 buf.AppendChar('\n');
247 } else if (ch == 'r') { 248 } else if (ch == 'r') {
248 buf.AppendChar('\r'); 249 buf.AppendChar('\r');
249 } else if (ch == 't') { 250 } else if (ch == 't') {
250 buf.AppendChar('\t'); 251 buf.AppendChar('\t');
251 } else if (ch == 'b') { 252 } else if (ch == 'b') {
252 buf.AppendChar('\b'); 253 buf.AppendChar('\b');
253 } else if (ch == 'f') { 254 } else if (ch == 'f') {
254 buf.AppendChar('\f'); 255 buf.AppendChar('\f');
255 } else if (ch == '\r') { 256 } else if (ch == '\r') {
256 status = 4; 257 status = SyntaxParser_ReadCarriageReturn;
257 break; 258 break;
258 } else if (ch != '\n') { 259 } else if (ch != '\n') {
259 buf.AppendChar(ch); 260 buf.AppendChar(ch);
260 } 261 }
261 status = 0; 262 status = SyntaxParser_ReadNormal;
262 break; 263 break;
263 case 2: 264 case SyntaxParser_ReadOctal:
264 if (ch >= '0' && ch <= '7') { 265 if (ch >= '0' && ch <= '7') {
265 iEscCode = 266 iEscCode =
266 iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); 267 iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch));
267 status = 3; 268 status = SyntaxParser_ReadFinishOctal;
268 } else { 269 } else {
269 buf.AppendChar(iEscCode); 270 buf.AppendChar(iEscCode);
270 status = 0; 271 status = SyntaxParser_ReadNormal;
271 continue; 272 continue;
272 } 273 }
273 break; 274 break;
274 case 3: 275 case SyntaxParser_ReadFinishOctal:
275 if (ch >= '0' && ch <= '7') { 276 if (ch >= '0' && ch <= '7') {
276 iEscCode = 277 iEscCode =
277 iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); 278 iEscCode * 8 + FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch));
278 buf.AppendChar(iEscCode); 279 buf.AppendChar(iEscCode);
279 status = 0; 280 status = SyntaxParser_ReadNormal;
dsinclair 2016/11/01 19:14:42 Move this before the if() as it happens in both br
npm 2016/11/01 20:09:10 Done.
280 } else { 281 } else {
281 buf.AppendChar(iEscCode); 282 buf.AppendChar(iEscCode);
282 status = 0; 283 status = SyntaxParser_ReadNormal;
283 continue; 284 continue;
284 } 285 }
285 break; 286 break;
286 case 4: 287 case SyntaxParser_ReadCarriageReturn:
287 status = 0; 288 status = SyntaxParser_ReadNormal;
288 if (ch != '\n') 289 if (ch != '\n')
289 continue; 290 continue;
290 break; 291 break;
291 } 292 }
292 293
293 if (!GetNextChar(ch)) 294 if (!GetNextChar(ch))
294 break; 295 break;
295 } 296 }
296 297
297 GetNextChar(ch); 298 GetNextChar(ch);
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 len = pLenObj->GetInteger(); 642 len = pLenObj->GetInteger();
642 643
643 // Locate the start of stream. 644 // Locate the start of stream.
644 ToNextLine(); 645 ToNextLine();
645 FX_FILESIZE streamStartPos = m_Pos; 646 FX_FILESIZE streamStartPos = m_Pos;
646 647
647 const CFX_ByteStringC kEndStreamStr("endstream"); 648 const CFX_ByteStringC kEndStreamStr("endstream");
648 const CFX_ByteStringC kEndObjStr("endobj"); 649 const CFX_ByteStringC kEndObjStr("endobj");
649 650
650 CPDF_CryptoHandler* pCryptoHandler = 651 CPDF_CryptoHandler* pCryptoHandler =
651 objnum == (uint32_t)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); 652 objnum == static_cast<uint32_t>(m_MetadataObjnum)
653 ? nullptr
654 : m_pCryptoHandler.get();
652 if (!pCryptoHandler) { 655 if (!pCryptoHandler) {
653 FX_BOOL bSearchForKeyword = TRUE; 656 FX_BOOL bSearchForKeyword = TRUE;
654 if (len >= 0) { 657 if (len >= 0) {
655 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos; 658 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos;
656 pos += len; 659 pos += len;
657 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen) 660 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen)
658 m_Pos = pos.ValueOrDie(); 661 m_Pos = pos.ValueOrDie();
659 662
660 m_Pos += ReadEOLMarkers(m_Pos); 663 m_Pos += ReadEOLMarkers(m_Pos);
661 FXSYS_memset(m_WordBuffer, 0, kEndStreamStr.GetLength() + 1); 664 FXSYS_memset(m_WordBuffer, 0, kEndStreamStr.GetLength() + 1);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 FX_Free(m_pFileBuf); 790 FX_Free(m_pFileBuf);
788 791
789 m_pFileBuf = FX_Alloc(uint8_t, m_BufSize); 792 m_pFileBuf = FX_Alloc(uint8_t, m_BufSize);
790 m_HeaderOffset = HeaderOffset; 793 m_HeaderOffset = HeaderOffset;
791 m_FileLen = pFileAccess->GetSize(); 794 m_FileLen = pFileAccess->GetSize();
792 m_Pos = 0; 795 m_Pos = 0;
793 m_pFileAccess = pFileAccess; 796 m_pFileAccess = pFileAccess;
794 m_BufOffset = 0; 797 m_BufOffset = 0;
795 pFileAccess->ReadBlock( 798 pFileAccess->ReadBlock(
796 m_pFileBuf, 0, 799 m_pFileBuf, 0,
797 (size_t)((FX_FILESIZE)m_BufSize > m_FileLen ? m_FileLen : m_BufSize)); 800 static_cast<size_t>(static_cast<FX_FILESIZE>(m_BufSize) > m_FileLen
801 ? m_FileLen
802 : m_BufSize));
798 } 803 }
799 804
800 uint32_t CPDF_SyntaxParser::GetDirectNum() { 805 uint32_t CPDF_SyntaxParser::GetDirectNum() {
801 bool bIsNumber; 806 bool bIsNumber;
802 GetNextWordInternal(&bIsNumber); 807 GetNextWordInternal(&bIsNumber);
803 if (!bIsNumber) 808 if (!bIsNumber)
804 return 0; 809 return 0;
805 810
806 m_WordBuffer[m_WordSize] = 0; 811 m_WordBuffer[m_WordSize] = 0;
807 return FXSYS_atoui(reinterpret_cast<const FX_CHAR*>(m_WordBuffer)); 812 return FXSYS_atoui(reinterpret_cast<const FX_CHAR*>(m_WordBuffer));
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 pos--; 903 pos--;
899 } 904 }
900 905
901 if (pos < 0) 906 if (pos < 0)
902 return FALSE; 907 return FALSE;
903 } 908 }
904 909
905 return FALSE; 910 return FALSE;
906 } 911 }
907 912
908 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags,
909 FX_BOOL bWholeWord,
910 FX_FILESIZE limit) {
911 int32_t ntags = 1;
912 for (int i = 0; i < tags.GetLength(); ++i) {
913 if (tags[i] == 0)
914 ++ntags;
915 }
916
917 // Ensure that the input byte string happens to be nul-terminated. This
918 // need not be the case, but the loop below uses this guarantee to put
919 // the last pattern into the vector.
920 ASSERT(tags[tags.GetLength()] == 0);
921 std::vector<SearchTagRecord> patterns(ntags);
922 uint32_t start = 0;
923 uint32_t itag = 0;
924 uint32_t max_len = 0;
925 for (int i = 0; i <= tags.GetLength(); ++i) {
926 if (tags[i] == 0) {
927 uint32_t len = i - start;
928 max_len = std::max(len, max_len);
929 patterns[itag].m_bsTag = tags.Mid(start, len);
930 patterns[itag].m_Offset = 0;
931 start = i + 1;
932 ++itag;
933 }
934 }
935
936 const FX_FILESIZE pos_limit = m_Pos + limit;
937 for (FX_FILESIZE pos = m_Pos; !limit || pos < pos_limit; ++pos) {
938 uint8_t byte;
939 if (!GetCharAt(pos, byte))
940 break;
941
942 for (int i = 0; i < ntags; ++i) {
943 SearchTagRecord& pat = patterns[i];
944 if (pat.m_bsTag[pat.m_Offset] != byte) {
945 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0;
946 continue;
947 }
948
949 ++pat.m_Offset;
950 if (pat.m_Offset != pat.m_bsTag.GetLength())
951 continue;
952
953 if (!bWholeWord || IsWholeWord(pos - pat.m_bsTag.GetLength(), limit,
954 pat.m_bsTag, FALSE)) {
955 return i;
956 }
957
958 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0;
959 }
960 }
961 return -1;
962 }
963
964 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, 913 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag,
965 FX_FILESIZE limit) { 914 FX_FILESIZE limit) {
966 int32_t taglen = tag.GetLength(); 915 int32_t taglen = tag.GetLength();
967 int32_t match = 0; 916 int32_t match = 0;
968 limit += m_Pos; 917 limit += m_Pos;
969 FX_FILESIZE startpos = m_Pos; 918 FX_FILESIZE startpos = m_Pos;
970 919
971 while (1) { 920 while (1) {
972 uint8_t ch; 921 uint8_t ch;
973 if (!GetNextChar(ch)) 922 if (!GetNextChar(ch))
(...skipping 14 matching lines...) Expand all
988 } 937 }
989 938
990 void CPDF_SyntaxParser::SetEncrypt( 939 void CPDF_SyntaxParser::SetEncrypt(
991 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { 940 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) {
992 m_pCryptoHandler = std::move(pCryptoHandler); 941 m_pCryptoHandler = std::move(pCryptoHandler);
993 } 942 }
994 943
995 CFX_ByteString CPDF_SyntaxParser::MaybeIntern(const CFX_ByteString& str) { 944 CFX_ByteString CPDF_SyntaxParser::MaybeIntern(const CFX_ByteString& str) {
996 return m_pPool ? m_pPool->Intern(str) : str; 945 return m_pPool ? m_pPool->Intern(str) : str;
997 } 946 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_syntax_parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698