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/fpdf_parser/cpdf_syntax_parser.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_syntax_parser.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" | 11 #include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" |
12 #include "core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h" | 12 #include "core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h" |
13 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" | 13 #include "core/fpdfapi/fpdf_parser/cpdf_null.h" |
14 #include "core/fpdfapi/fpdf_parser/fpdf_parser_utility.h" | 14 #include "core/fpdfapi/fpdf_parser/fpdf_parser_utility.h" |
15 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 15 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
16 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 16 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
17 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" | 17 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" |
18 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" | 18 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
19 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" | 19 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" |
20 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" | 20 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
21 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | 21 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
22 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 22 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
23 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 23 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
24 #include "core/fxcrt/include/fx_ext.h" | 24 #include "core/fxcrt/include/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 const char* m_pTag; | 30 CFX_ByteStringC m_bsTag; |
31 uint32_t m_Len; | |
32 uint32_t m_Offset; | 31 uint32_t m_Offset; |
33 }; | 32 }; |
34 | 33 |
35 } // namespace | 34 } // namespace |
36 | 35 |
37 // static | 36 // static |
38 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; | 37 int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; |
39 | 38 |
40 CPDF_SyntaxParser::CPDF_SyntaxParser() | 39 CPDF_SyntaxParser::CPDF_SyntaxParser() |
41 : m_MetadataObjnum(0), | 40 : m_MetadataObjnum(0), |
(...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 | 905 |
907 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, | 906 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, |
908 FX_BOOL bWholeWord, | 907 FX_BOOL bWholeWord, |
909 FX_FILESIZE limit) { | 908 FX_FILESIZE limit) { |
910 int32_t ntags = 1; | 909 int32_t ntags = 1; |
911 for (int i = 0; i < tags.GetLength(); ++i) { | 910 for (int i = 0; i < tags.GetLength(); ++i) { |
912 if (tags[i] == 0) | 911 if (tags[i] == 0) |
913 ++ntags; | 912 ++ntags; |
914 } | 913 } |
915 | 914 |
| 915 // Ensure that the input byte string happens to be nul-terminated. This |
| 916 // need not be the case, but the loop below uses this guarantee to put |
| 917 // the last pattern into the vector. |
| 918 ASSERT(tags[tags.GetLength()] == 0); |
916 std::vector<SearchTagRecord> patterns(ntags); | 919 std::vector<SearchTagRecord> patterns(ntags); |
917 uint32_t start = 0; | 920 uint32_t start = 0; |
918 uint32_t itag = 0; | 921 uint32_t itag = 0; |
919 uint32_t max_len = 0; | 922 uint32_t max_len = 0; |
920 for (int i = 0; i <= tags.GetLength(); ++i) { | 923 for (int i = 0; i <= tags.GetLength(); ++i) { |
921 if (tags[i] == 0) { | 924 if (tags[i] == 0) { |
922 uint32_t len = i - start; | 925 uint32_t len = i - start; |
923 max_len = std::max(len, max_len); | 926 max_len = std::max(len, max_len); |
924 patterns[itag].m_pTag = tags.c_str() + start; | 927 patterns[itag].m_bsTag = tags.Mid(start, len); |
925 patterns[itag].m_Len = len; | |
926 patterns[itag].m_Offset = 0; | 928 patterns[itag].m_Offset = 0; |
927 start = i + 1; | 929 start = i + 1; |
928 ++itag; | 930 ++itag; |
929 } | 931 } |
930 } | 932 } |
931 | 933 |
932 const FX_FILESIZE pos_limit = m_Pos + limit; | 934 const FX_FILESIZE pos_limit = m_Pos + limit; |
933 for (FX_FILESIZE pos = m_Pos; !limit || pos < pos_limit; ++pos) { | 935 for (FX_FILESIZE pos = m_Pos; !limit || pos < pos_limit; ++pos) { |
934 uint8_t byte; | 936 uint8_t byte; |
935 if (!GetCharAt(pos, byte)) | 937 if (!GetCharAt(pos, byte)) |
936 break; | 938 break; |
937 | 939 |
938 for (int i = 0; i < ntags; ++i) { | 940 for (int i = 0; i < ntags; ++i) { |
939 SearchTagRecord& pat = patterns[i]; | 941 SearchTagRecord& pat = patterns[i]; |
940 if (pat.m_pTag[pat.m_Offset] != byte) { | 942 if (pat.m_bsTag[pat.m_Offset] != byte) { |
941 pat.m_Offset = (pat.m_pTag[0] == byte) ? 1 : 0; | 943 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
942 continue; | 944 continue; |
943 } | 945 } |
944 | 946 |
945 ++pat.m_Offset; | 947 ++pat.m_Offset; |
946 if (pat.m_Offset != pat.m_Len) | 948 if (pat.m_Offset != pat.m_bsTag.GetLength()) |
947 continue; | 949 continue; |
948 | 950 |
949 if (!bWholeWord || | 951 if (!bWholeWord || IsWholeWord(pos - pat.m_bsTag.GetLength(), limit, |
950 IsWholeWord(pos - pat.m_Len, limit, | 952 pat.m_bsTag, FALSE)) { |
951 CFX_ByteStringC(pat.m_pTag, pat.m_Len), FALSE)) { | |
952 return i; | 953 return i; |
953 } | 954 } |
954 | 955 |
955 pat.m_Offset = (pat.m_pTag[0] == byte) ? 1 : 0; | 956 pat.m_Offset = (pat.m_bsTag[0] == byte) ? 1 : 0; |
956 } | 957 } |
957 } | 958 } |
958 return -1; | 959 return -1; |
959 } | 960 } |
960 | 961 |
961 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, | 962 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, |
962 FX_FILESIZE limit) { | 963 FX_FILESIZE limit) { |
963 int32_t taglen = tag.GetLength(); | 964 int32_t taglen = tag.GetLength(); |
964 int32_t match = 0; | 965 int32_t match = 0; |
965 limit += m_Pos; | 966 limit += m_Pos; |
(...skipping 15 matching lines...) Expand all Loading... |
981 if (limit && m_Pos == limit) | 982 if (limit && m_Pos == limit) |
982 return -1; | 983 return -1; |
983 } | 984 } |
984 return -1; | 985 return -1; |
985 } | 986 } |
986 | 987 |
987 void CPDF_SyntaxParser::SetEncrypt( | 988 void CPDF_SyntaxParser::SetEncrypt( |
988 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { | 989 std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler) { |
989 m_pCryptoHandler = std::move(pCryptoHandler); | 990 m_pCryptoHandler = std::move(pCryptoHandler); |
990 } | 991 } |
OLD | NEW |