OLD | NEW |
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 : m_MetadataObjnum(0), | 44 : m_MetadataObjnum(0), |
45 m_pFileAccess(nullptr), | 45 m_pFileAccess(nullptr), |
46 m_pFileBuf(nullptr), | 46 m_pFileBuf(nullptr), |
47 m_BufSize(CPDF_ModuleMgr::kFileBufSize), | 47 m_BufSize(CPDF_ModuleMgr::kFileBufSize), |
48 m_pPool(pPool) {} | 48 m_pPool(pPool) {} |
49 | 49 |
50 CPDF_SyntaxParser::~CPDF_SyntaxParser() { | 50 CPDF_SyntaxParser::~CPDF_SyntaxParser() { |
51 FX_Free(m_pFileBuf); | 51 FX_Free(m_pFileBuf); |
52 } | 52 } |
53 | 53 |
54 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { | 54 bool CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { |
55 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos); | 55 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos); |
56 m_Pos = pos; | 56 m_Pos = pos; |
57 return GetNextChar(ch); | 57 return GetNextChar(ch); |
58 } | 58 } |
59 | 59 |
60 FX_BOOL CPDF_SyntaxParser::GetNextChar(uint8_t& ch) { | 60 bool CPDF_SyntaxParser::GetNextChar(uint8_t& ch) { |
61 FX_FILESIZE pos = m_Pos + m_HeaderOffset; | 61 FX_FILESIZE pos = m_Pos + m_HeaderOffset; |
62 if (pos >= m_FileLen) | 62 if (pos >= m_FileLen) |
63 return FALSE; | 63 return false; |
64 | 64 |
65 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { | 65 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { |
66 FX_FILESIZE read_pos = pos; | 66 FX_FILESIZE read_pos = pos; |
67 uint32_t read_size = m_BufSize; | 67 uint32_t read_size = m_BufSize; |
68 if ((FX_FILESIZE)read_size > m_FileLen) | 68 if ((FX_FILESIZE)read_size > m_FileLen) |
69 read_size = (uint32_t)m_FileLen; | 69 read_size = (uint32_t)m_FileLen; |
70 | 70 |
71 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { | 71 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { |
72 if (m_FileLen < (FX_FILESIZE)read_size) { | 72 if (m_FileLen < (FX_FILESIZE)read_size) { |
73 read_pos = 0; | 73 read_pos = 0; |
74 read_size = (uint32_t)m_FileLen; | 74 read_size = (uint32_t)m_FileLen; |
75 } else { | 75 } else { |
76 read_pos = m_FileLen - read_size; | 76 read_pos = m_FileLen - read_size; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) | 80 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) |
81 return FALSE; | 81 return false; |
82 | 82 |
83 m_BufOffset = read_pos; | 83 m_BufOffset = read_pos; |
84 } | 84 } |
85 ch = m_pFileBuf[pos - m_BufOffset]; | 85 ch = m_pFileBuf[pos - m_BufOffset]; |
86 m_Pos++; | 86 m_Pos++; |
87 return TRUE; | 87 return true; |
88 } | 88 } |
89 | 89 |
90 FX_BOOL CPDF_SyntaxParser::GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch) { | 90 bool CPDF_SyntaxParser::GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch) { |
91 pos += m_HeaderOffset; | 91 pos += m_HeaderOffset; |
92 if (pos >= m_FileLen) | 92 if (pos >= m_FileLen) |
93 return FALSE; | 93 return false; |
94 | 94 |
95 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { | 95 if (m_BufOffset >= pos || (FX_FILESIZE)(m_BufOffset + m_BufSize) <= pos) { |
96 FX_FILESIZE read_pos; | 96 FX_FILESIZE read_pos; |
97 if (pos < (FX_FILESIZE)m_BufSize) | 97 if (pos < (FX_FILESIZE)m_BufSize) |
98 read_pos = 0; | 98 read_pos = 0; |
99 else | 99 else |
100 read_pos = pos - m_BufSize + 1; | 100 read_pos = pos - m_BufSize + 1; |
101 | 101 |
102 uint32_t read_size = m_BufSize; | 102 uint32_t read_size = m_BufSize; |
103 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { | 103 if ((FX_FILESIZE)(read_pos + read_size) > m_FileLen) { |
104 if (m_FileLen < (FX_FILESIZE)read_size) { | 104 if (m_FileLen < (FX_FILESIZE)read_size) { |
105 read_pos = 0; | 105 read_pos = 0; |
106 read_size = (uint32_t)m_FileLen; | 106 read_size = (uint32_t)m_FileLen; |
107 } else { | 107 } else { |
108 read_pos = m_FileLen - read_size; | 108 read_pos = m_FileLen - read_size; |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) | 112 if (!m_pFileAccess->ReadBlock(m_pFileBuf, read_pos, read_size)) |
113 return FALSE; | 113 return false; |
114 | 114 |
115 m_BufOffset = read_pos; | 115 m_BufOffset = read_pos; |
116 } | 116 } |
117 ch = m_pFileBuf[pos - m_BufOffset]; | 117 ch = m_pFileBuf[pos - m_BufOffset]; |
118 return TRUE; | 118 return true; |
119 } | 119 } |
120 | 120 |
121 FX_BOOL CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, uint32_t size) { | 121 bool CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, uint32_t size) { |
122 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size)) | 122 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size)) |
123 return FALSE; | 123 return false; |
124 m_Pos += size; | 124 m_Pos += size; |
125 return TRUE; | 125 return true; |
126 } | 126 } |
127 | 127 |
128 void CPDF_SyntaxParser::GetNextWordInternal(bool* bIsNumber) { | 128 void CPDF_SyntaxParser::GetNextWordInternal(bool* bIsNumber) { |
129 m_WordSize = 0; | 129 m_WordSize = 0; |
130 if (bIsNumber) | 130 if (bIsNumber) |
131 *bIsNumber = true; | 131 *bIsNumber = true; |
132 | 132 |
133 uint8_t ch; | 133 uint8_t ch; |
134 if (!GetNextChar(ch)) | 134 if (!GetNextChar(ch)) |
135 return; | 135 return; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 return CFX_ByteString((const FX_CHAR*)m_WordBuffer, m_WordSize); | 374 return CFX_ByteString((const FX_CHAR*)m_WordBuffer, m_WordSize); |
375 } | 375 } |
376 | 376 |
377 CFX_ByteString CPDF_SyntaxParser::GetKeyword() { | 377 CFX_ByteString CPDF_SyntaxParser::GetKeyword() { |
378 return GetNextWord(nullptr); | 378 return GetNextWord(nullptr); |
379 } | 379 } |
380 | 380 |
381 CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjectHolder* pObjList, | 381 CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjectHolder* pObjList, |
382 uint32_t objnum, | 382 uint32_t objnum, |
383 uint32_t gennum, | 383 uint32_t gennum, |
384 FX_BOOL bDecrypt) { | 384 bool bDecrypt) { |
385 CFX_AutoRestorer<int> restorer(&s_CurrentRecursionDepth); | 385 CFX_AutoRestorer<int> restorer(&s_CurrentRecursionDepth); |
386 if (++s_CurrentRecursionDepth > kParserMaxRecursionDepth) | 386 if (++s_CurrentRecursionDepth > kParserMaxRecursionDepth) |
387 return nullptr; | 387 return nullptr; |
388 | 388 |
389 FX_FILESIZE SavedObjPos = m_Pos; | 389 FX_FILESIZE SavedObjPos = m_Pos; |
390 bool bIsNumber; | 390 bool bIsNumber; |
391 CFX_ByteString word = GetNextWord(&bIsNumber); | 391 CFX_ByteString word = GetNextWord(&bIsNumber); |
392 if (word.GetLength() == 0) | 392 if (word.GetLength() == 0) |
393 return nullptr; | 393 return nullptr; |
394 | 394 |
(...skipping 12 matching lines...) Expand all Loading... |
407 if (word == "true" || word == "false") | 407 if (word == "true" || word == "false") |
408 return new CPDF_Boolean(word == "true"); | 408 return new CPDF_Boolean(word == "true"); |
409 | 409 |
410 if (word == "null") | 410 if (word == "null") |
411 return new CPDF_Null; | 411 return new CPDF_Null; |
412 | 412 |
413 if (word == "(") { | 413 if (word == "(") { |
414 CFX_ByteString str = ReadString(); | 414 CFX_ByteString str = ReadString(); |
415 if (m_pCryptoHandler && bDecrypt) | 415 if (m_pCryptoHandler && bDecrypt) |
416 m_pCryptoHandler->Decrypt(objnum, gennum, str); | 416 m_pCryptoHandler->Decrypt(objnum, gennum, str); |
417 return new CPDF_String(MaybeIntern(str), FALSE); | 417 return new CPDF_String(MaybeIntern(str), false); |
418 } | 418 } |
419 | 419 |
420 if (word == "<") { | 420 if (word == "<") { |
421 CFX_ByteString str = ReadHexString(); | 421 CFX_ByteString str = ReadHexString(); |
422 if (m_pCryptoHandler && bDecrypt) | 422 if (m_pCryptoHandler && bDecrypt) |
423 m_pCryptoHandler->Decrypt(objnum, gennum, str); | 423 m_pCryptoHandler->Decrypt(objnum, gennum, str); |
424 return new CPDF_String(MaybeIntern(str), TRUE); | 424 return new CPDF_String(MaybeIntern(str), true); |
425 } | 425 } |
426 | 426 |
427 if (word == "[") { | 427 if (word == "[") { |
428 CPDF_Array* pArray = new CPDF_Array; | 428 CPDF_Array* pArray = new CPDF_Array; |
429 while (CPDF_Object* pObj = GetObject(pObjList, objnum, gennum, true)) | 429 while (CPDF_Object* pObj = GetObject(pObjList, objnum, gennum, true)) |
430 pArray->Add(pObj); | 430 pArray->Add(pObj); |
431 | 431 |
432 return pArray; | 432 return pArray; |
433 } | 433 } |
434 | 434 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 if (word == "true" || word == "false") | 528 if (word == "true" || word == "false") |
529 return new CPDF_Boolean(word == "true"); | 529 return new CPDF_Boolean(word == "true"); |
530 | 530 |
531 if (word == "null") | 531 if (word == "null") |
532 return new CPDF_Null; | 532 return new CPDF_Null; |
533 | 533 |
534 if (word == "(") { | 534 if (word == "(") { |
535 CFX_ByteString str = ReadString(); | 535 CFX_ByteString str = ReadString(); |
536 if (m_pCryptoHandler) | 536 if (m_pCryptoHandler) |
537 m_pCryptoHandler->Decrypt(objnum, gennum, str); | 537 m_pCryptoHandler->Decrypt(objnum, gennum, str); |
538 return new CPDF_String(MaybeIntern(str), FALSE); | 538 return new CPDF_String(MaybeIntern(str), false); |
539 } | 539 } |
540 | 540 |
541 if (word == "<") { | 541 if (word == "<") { |
542 CFX_ByteString str = ReadHexString(); | 542 CFX_ByteString str = ReadHexString(); |
543 if (m_pCryptoHandler) | 543 if (m_pCryptoHandler) |
544 m_pCryptoHandler->Decrypt(objnum, gennum, str); | 544 m_pCryptoHandler->Decrypt(objnum, gennum, str); |
545 return new CPDF_String(MaybeIntern(str), TRUE); | 545 return new CPDF_String(MaybeIntern(str), true); |
546 } | 546 } |
547 | 547 |
548 if (word == "[") { | 548 if (word == "[") { |
549 std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>> pArray( | 549 std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>> pArray( |
550 new CPDF_Array); | 550 new CPDF_Array); |
551 while (CPDF_Object* pObj = GetObject(pObjList, objnum, gennum, true)) | 551 while (CPDF_Object* pObj = GetObject(pObjList, objnum, gennum, true)) |
552 pArray->Add(pObj); | 552 pArray->Add(pObj); |
553 | 553 |
554 return m_WordBuffer[0] == ']' ? pArray.release() : nullptr; | 554 return m_WordBuffer[0] == ']' ? pArray.release() : nullptr; |
555 } | 555 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 // Locate the start of stream. | 643 // Locate the start of stream. |
644 ToNextLine(); | 644 ToNextLine(); |
645 FX_FILESIZE streamStartPos = m_Pos; | 645 FX_FILESIZE streamStartPos = m_Pos; |
646 | 646 |
647 const CFX_ByteStringC kEndStreamStr("endstream"); | 647 const CFX_ByteStringC kEndStreamStr("endstream"); |
648 const CFX_ByteStringC kEndObjStr("endobj"); | 648 const CFX_ByteStringC kEndObjStr("endobj"); |
649 | 649 |
650 CPDF_CryptoHandler* pCryptoHandler = | 650 CPDF_CryptoHandler* pCryptoHandler = |
651 objnum == (uint32_t)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); | 651 objnum == (uint32_t)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); |
652 if (!pCryptoHandler) { | 652 if (!pCryptoHandler) { |
653 FX_BOOL bSearchForKeyword = TRUE; | 653 bool bSearchForKeyword = true; |
654 if (len >= 0) { | 654 if (len >= 0) { |
655 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos; | 655 pdfium::base::CheckedNumeric<FX_FILESIZE> pos = m_Pos; |
656 pos += len; | 656 pos += len; |
657 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen) | 657 if (pos.IsValid() && pos.ValueOrDie() < m_FileLen) |
658 m_Pos = pos.ValueOrDie(); | 658 m_Pos = pos.ValueOrDie(); |
659 | 659 |
660 m_Pos += ReadEOLMarkers(m_Pos); | 660 m_Pos += ReadEOLMarkers(m_Pos); |
661 FXSYS_memset(m_WordBuffer, 0, kEndStreamStr.GetLength() + 1); | 661 FXSYS_memset(m_WordBuffer, 0, kEndStreamStr.GetLength() + 1); |
662 GetNextWordInternal(nullptr); | 662 GetNextWordInternal(nullptr); |
663 // Earlier version of PDF specification doesn't require EOL marker before | 663 // Earlier version of PDF specification doesn't require EOL marker before |
664 // 'endstream' keyword. If keyword 'endstream' follows the bytes in | 664 // 'endstream' keyword. If keyword 'endstream' follows the bytes in |
665 // specified length, it signals the end of stream. | 665 // specified length, it signals the end of stream. |
666 if (FXSYS_memcmp(m_WordBuffer, kEndStreamStr.raw_str(), | 666 if (FXSYS_memcmp(m_WordBuffer, kEndStreamStr.raw_str(), |
667 kEndStreamStr.GetLength()) == 0) { | 667 kEndStreamStr.GetLength()) == 0) { |
668 bSearchForKeyword = FALSE; | 668 bSearchForKeyword = false; |
669 } | 669 } |
670 } | 670 } |
671 | 671 |
672 if (bSearchForKeyword) { | 672 if (bSearchForKeyword) { |
673 // If len is not available, len needs to be calculated | 673 // If len is not available, len needs to be calculated |
674 // by searching the keywords "endstream" or "endobj". | 674 // by searching the keywords "endstream" or "endobj". |
675 m_Pos = streamStartPos; | 675 m_Pos = streamStartPos; |
676 FX_FILESIZE endStreamOffset = 0; | 676 FX_FILESIZE endStreamOffset = 0; |
677 while (endStreamOffset >= 0) { | 677 while (endStreamOffset >= 0) { |
678 endStreamOffset = FindTag(kEndStreamStr, 0); | 678 endStreamOffset = FindTag(kEndStreamStr, 0); |
679 | 679 |
680 // Can't find "endstream". | 680 // Can't find "endstream". |
681 if (endStreamOffset < 0) | 681 if (endStreamOffset < 0) |
682 break; | 682 break; |
683 | 683 |
684 // Stop searching when "endstream" is found. | 684 // Stop searching when "endstream" is found. |
685 if (IsWholeWord(m_Pos - kEndStreamStr.GetLength(), m_FileLen, | 685 if (IsWholeWord(m_Pos - kEndStreamStr.GetLength(), m_FileLen, |
686 kEndStreamStr, TRUE)) { | 686 kEndStreamStr, true)) { |
687 endStreamOffset = m_Pos - streamStartPos - kEndStreamStr.GetLength(); | 687 endStreamOffset = m_Pos - streamStartPos - kEndStreamStr.GetLength(); |
688 break; | 688 break; |
689 } | 689 } |
690 } | 690 } |
691 | 691 |
692 m_Pos = streamStartPos; | 692 m_Pos = streamStartPos; |
693 FX_FILESIZE endObjOffset = 0; | 693 FX_FILESIZE endObjOffset = 0; |
694 while (endObjOffset >= 0) { | 694 while (endObjOffset >= 0) { |
695 endObjOffset = FindTag(kEndObjStr, 0); | 695 endObjOffset = FindTag(kEndObjStr, 0); |
696 | 696 |
697 // Can't find "endobj". | 697 // Can't find "endobj". |
698 if (endObjOffset < 0) | 698 if (endObjOffset < 0) |
699 break; | 699 break; |
700 | 700 |
701 // Stop searching when "endobj" is found. | 701 // Stop searching when "endobj" is found. |
702 if (IsWholeWord(m_Pos - kEndObjStr.GetLength(), m_FileLen, kEndObjStr, | 702 if (IsWholeWord(m_Pos - kEndObjStr.GetLength(), m_FileLen, kEndObjStr, |
703 TRUE)) { | 703 true)) { |
704 endObjOffset = m_Pos - streamStartPos - kEndObjStr.GetLength(); | 704 endObjOffset = m_Pos - streamStartPos - kEndObjStr.GetLength(); |
705 break; | 705 break; |
706 } | 706 } |
707 } | 707 } |
708 | 708 |
709 // Can't find "endstream" or "endobj". | 709 // Can't find "endstream" or "endobj". |
710 if (endStreamOffset < 0 && endObjOffset < 0) { | 710 if (endStreamOffset < 0 && endObjOffset < 0) { |
711 pDict->Release(); | 711 pDict->Release(); |
712 return nullptr; | 712 return nullptr; |
713 } | 713 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 if (!bIsNumber) | 803 if (!bIsNumber) |
804 return 0; | 804 return 0; |
805 | 805 |
806 m_WordBuffer[m_WordSize] = 0; | 806 m_WordBuffer[m_WordSize] = 0; |
807 return FXSYS_atoui(reinterpret_cast<const FX_CHAR*>(m_WordBuffer)); | 807 return FXSYS_atoui(reinterpret_cast<const FX_CHAR*>(m_WordBuffer)); |
808 } | 808 } |
809 | 809 |
810 bool CPDF_SyntaxParser::IsWholeWord(FX_FILESIZE startpos, | 810 bool CPDF_SyntaxParser::IsWholeWord(FX_FILESIZE startpos, |
811 FX_FILESIZE limit, | 811 FX_FILESIZE limit, |
812 const CFX_ByteStringC& tag, | 812 const CFX_ByteStringC& tag, |
813 FX_BOOL checkKeyword) { | 813 bool checkKeyword) { |
814 const uint32_t taglen = tag.GetLength(); | 814 const uint32_t taglen = tag.GetLength(); |
815 | 815 |
816 bool bCheckLeft = !PDFCharIsDelimiter(tag[0]) && !PDFCharIsWhitespace(tag[0]); | 816 bool bCheckLeft = !PDFCharIsDelimiter(tag[0]) && !PDFCharIsWhitespace(tag[0]); |
817 bool bCheckRight = !PDFCharIsDelimiter(tag[taglen - 1]) && | 817 bool bCheckRight = !PDFCharIsDelimiter(tag[taglen - 1]) && |
818 !PDFCharIsWhitespace(tag[taglen - 1]); | 818 !PDFCharIsWhitespace(tag[taglen - 1]); |
819 | 819 |
820 uint8_t ch; | 820 uint8_t ch; |
821 if (bCheckRight && startpos + (int32_t)taglen <= limit && | 821 if (bCheckRight && startpos + (int32_t)taglen <= limit && |
822 GetCharAt(startpos + (int32_t)taglen, ch)) { | 822 GetCharAt(startpos + (int32_t)taglen, ch)) { |
823 if (PDFCharIsNumeric(ch) || PDFCharIsOther(ch) || | 823 if (PDFCharIsNumeric(ch) || PDFCharIsOther(ch) || |
824 (checkKeyword && PDFCharIsDelimiter(ch))) { | 824 (checkKeyword && PDFCharIsDelimiter(ch))) { |
825 return false; | 825 return false; |
826 } | 826 } |
827 } | 827 } |
828 | 828 |
829 if (bCheckLeft && startpos > 0 && GetCharAt(startpos - 1, ch)) { | 829 if (bCheckLeft && startpos > 0 && GetCharAt(startpos - 1, ch)) { |
830 if (PDFCharIsNumeric(ch) || PDFCharIsOther(ch) || | 830 if (PDFCharIsNumeric(ch) || PDFCharIsOther(ch) || |
831 (checkKeyword && PDFCharIsDelimiter(ch))) { | 831 (checkKeyword && PDFCharIsDelimiter(ch))) { |
832 return false; | 832 return false; |
833 } | 833 } |
834 } | 834 } |
835 return true; | 835 return true; |
836 } | 836 } |
837 | 837 |
838 // TODO(dsinclair): Split into a SearchWordForward and SearchWordBackwards | 838 // TODO(dsinclair): Split into a SearchWordForward and SearchWordBackwards |
839 // and drop the bool. | 839 // and drop the bool. |
840 FX_BOOL CPDF_SyntaxParser::SearchWord(const CFX_ByteStringC& tag, | 840 bool CPDF_SyntaxParser::SearchWord(const CFX_ByteStringC& tag, |
841 FX_BOOL bWholeWord, | 841 bool bWholeWord, |
842 FX_BOOL bForward, | 842 bool bForward, |
843 FX_FILESIZE limit) { | 843 FX_FILESIZE limit) { |
844 int32_t taglen = tag.GetLength(); | 844 int32_t taglen = tag.GetLength(); |
845 if (taglen == 0) | 845 if (taglen == 0) |
846 return FALSE; | 846 return false; |
847 | 847 |
848 FX_FILESIZE pos = m_Pos; | 848 FX_FILESIZE pos = m_Pos; |
849 int32_t offset = 0; | 849 int32_t offset = 0; |
850 if (!bForward) | 850 if (!bForward) |
851 offset = taglen - 1; | 851 offset = taglen - 1; |
852 | 852 |
853 const uint8_t* tag_data = tag.raw_str(); | 853 const uint8_t* tag_data = tag.raw_str(); |
854 uint8_t byte; | 854 uint8_t byte; |
855 while (1) { | 855 while (1) { |
856 if (bForward) { | 856 if (bForward) { |
857 if (limit && pos >= m_Pos + limit) | 857 if (limit && pos >= m_Pos + limit) |
858 return FALSE; | 858 return false; |
859 | 859 |
860 if (!GetCharAt(pos, byte)) | 860 if (!GetCharAt(pos, byte)) |
861 return FALSE; | 861 return false; |
862 | 862 |
863 } else { | 863 } else { |
864 if (limit && pos <= m_Pos - limit) | 864 if (limit && pos <= m_Pos - limit) |
865 return FALSE; | 865 return false; |
866 | 866 |
867 if (!GetCharAtBackward(pos, byte)) | 867 if (!GetCharAtBackward(pos, byte)) |
868 return FALSE; | 868 return false; |
869 } | 869 } |
870 | 870 |
871 if (byte == tag_data[offset]) { | 871 if (byte == tag_data[offset]) { |
872 if (bForward) { | 872 if (bForward) { |
873 offset++; | 873 offset++; |
874 if (offset < taglen) { | 874 if (offset < taglen) { |
875 pos++; | 875 pos++; |
876 continue; | 876 continue; |
877 } | 877 } |
878 } else { | 878 } else { |
879 offset--; | 879 offset--; |
880 if (offset >= 0) { | 880 if (offset >= 0) { |
881 pos--; | 881 pos--; |
882 continue; | 882 continue; |
883 } | 883 } |
884 } | 884 } |
885 | 885 |
886 FX_FILESIZE startpos = bForward ? pos - taglen + 1 : pos; | 886 FX_FILESIZE startpos = bForward ? pos - taglen + 1 : pos; |
887 if (!bWholeWord || IsWholeWord(startpos, limit, tag, FALSE)) { | 887 if (!bWholeWord || IsWholeWord(startpos, limit, tag, false)) { |
888 m_Pos = startpos; | 888 m_Pos = startpos; |
889 return TRUE; | 889 return true; |
890 } | 890 } |
891 } | 891 } |
892 | 892 |
893 if (bForward) { | 893 if (bForward) { |
894 offset = byte == tag_data[0] ? 1 : 0; | 894 offset = byte == tag_data[0] ? 1 : 0; |
895 pos++; | 895 pos++; |
896 } else { | 896 } else { |
897 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1; | 897 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1; |
898 pos--; | 898 pos--; |
899 } | 899 } |
900 | 900 |
901 if (pos < 0) | 901 if (pos < 0) |
902 return FALSE; | 902 return false; |
903 } | 903 } |
904 | 904 |
905 return FALSE; | 905 return false; |
906 } | 906 } |
907 | 907 |
908 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, | 908 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, |
909 FX_BOOL bWholeWord, | 909 bool bWholeWord, |
910 FX_FILESIZE limit) { | 910 FX_FILESIZE limit) { |
911 int32_t ntags = 1; | 911 int32_t ntags = 1; |
912 for (int i = 0; i < tags.GetLength(); ++i) { | 912 for (int i = 0; i < tags.GetLength(); ++i) { |
913 if (tags[i] == 0) | 913 if (tags[i] == 0) |
914 ++ntags; | 914 ++ntags; |
915 } | 915 } |
916 | 916 |
917 // Ensure that the input byte string happens to be nul-terminated. This | 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 | 918 // need not be the case, but the loop below uses this guarantee to put |
919 // the last pattern into the vector. | 919 // the last pattern into the vector. |
(...skipping 24 matching lines...) Expand all Loading... |
944 if (pat.m_bsTag[pat.m_Offset] != byte) { | 944 if (pat.m_bsTag[pat.m_Offset] != byte) { |
945 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; | 945 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
946 continue; | 946 continue; |
947 } | 947 } |
948 | 948 |
949 ++pat.m_Offset; | 949 ++pat.m_Offset; |
950 if (pat.m_Offset != pat.m_bsTag.GetLength()) | 950 if (pat.m_Offset != pat.m_bsTag.GetLength()) |
951 continue; | 951 continue; |
952 | 952 |
953 if (!bWholeWord || IsWholeWord(pos - pat.m_bsTag.GetLength(), limit, | 953 if (!bWholeWord || IsWholeWord(pos - pat.m_bsTag.GetLength(), limit, |
954 pat.m_bsTag, FALSE)) { | 954 pat.m_bsTag, false)) { |
955 return i; | 955 return i; |
956 } | 956 } |
957 | 957 |
958 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; | 958 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
959 } | 959 } |
960 } | 960 } |
961 return -1; | 961 return -1; |
962 } | 962 } |
963 | 963 |
964 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, | 964 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, |
(...skipping 23 matching lines...) Expand all Loading... |
988 } | 988 } |
989 | 989 |
990 void CPDF_SyntaxParser::SetEncrypt( | 990 void CPDF_SyntaxParser::SetEncrypt( |
991 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { | 991 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { |
992 m_pCryptoHandler = std::move(pCryptoHandler); | 992 m_pCryptoHandler = std::move(pCryptoHandler); |
993 } | 993 } |
994 | 994 |
995 CFX_ByteString CPDF_SyntaxParser::MaybeIntern(const CFX_ByteString& str) { | 995 CFX_ByteString CPDF_SyntaxParser::MaybeIntern(const CFX_ByteString& str) { |
996 return m_pPool ? m_pPool->Intern(str) : str; | 996 return m_pPool ? m_pPool->Intern(str) : str; |
997 } | 997 } |
OLD | NEW |