1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "stringfilter.h"
17
18 #include <iosfwd>
19 #include <istream>
20 #include <ostream>
21 #include <sstream>
22 #include <streambuf>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 char StringFilter::charTab_[StringFilter::CHAR_RANGE][StringFilter::MAP_STR_LEN];
30 int StringFilter::statTab_[StringFilter::STATE_NUM][StringFilter::CHAR_RANGE];
31 StringFilter StringFilter::filter_;
32
StringFilter()33 StringFilter::StringFilter()
34 {
35 // build mapping table for special chars
36 std::vector<std::pair<char, std::string>> mapping = {
37 {'\\', "\\\\"}, {'\"', "\\\""}, {'\b', "\\b"},
38 {'\f', "\\f"}, {'\n', "\\n"}, {'\r', "\\r"}, {'\t', "\\t"}
39 };
40 std::stringstream ss;
41 for (int i = 0; i < CHAR_RANGE; ++i) {
42 ss.clear();
43 ss << static_cast<char>(i);
44 ss >> charTab_[i];
45 }
46 for (auto e : mapping) {
47 ss.clear();
48 ss << e.second;
49 ss >> charTab_[static_cast<int>(e.first)];
50 }
51
52 int i = 0;
53 // build state transition table
54 for (i = 0; i < CHAR_RANGE; ++i) {
55 statTab_[STATE_BEGIN][i] = STATE_STOP;
56 }
57 for (i = 'a'; i <= 'z'; ++i) {
58 statTab_[STATE_BEGIN][i] = STATE_RUN;
59 }
60 for (i = 'A'; i <= 'Z'; ++i) {
61 statTab_[STATE_BEGIN][i] = STATE_RUN;
62 }
63
64 for (i = 0; i < CHAR_RANGE; ++i) {
65 statTab_[STATE_RUN][i] = STATE_STOP;
66 }
67 for (i = '0'; i <= '9'; ++i) {
68 statTab_[STATE_RUN][i] = STATE_RUN;
69 }
70 for (i = 'a'; i <= 'z'; ++i) {
71 statTab_[STATE_RUN][i] = STATE_RUN;
72 }
73 for (i = 'A'; i <= 'Z'; ++i) {
74 statTab_[STATE_RUN][i] = STATE_RUN;
75 }
76 statTab_[STATE_RUN][static_cast<int>('_')] = STATE_RUN;
77 }
78
EscapeToRaw(const std::string & text)79 std::string StringFilter::EscapeToRaw(const std::string &text)
80 {
81 std::string rawText = "";
82 for (auto c : text) {
83 unsigned int ic = static_cast<unsigned int>(c);
84 if (ic < CHAR_RANGE && charTab_[ic][1]) {
85 rawText.append(charTab_[ic]);
86 } else {
87 rawText.push_back(c);
88 }
89 }
90 return rawText;
91 }
92
IsValidName(const std::string & text,unsigned int maxSize)93 bool StringFilter::IsValidName(const std::string &text, unsigned int maxSize)
94 {
95 if (text.empty()) {
96 return false;
97 }
98 if (text.length() > maxSize) {
99 return false;
100 }
101 int state = STATE_BEGIN;
102 for (auto c : text) {
103 unsigned int ic = static_cast<unsigned int>(c);
104 if ((ic >= CHAR_RANGE) || (state < 0) || (state >= StringFilter::STATE_NUM)) {
105 return false;
106 }
107 state = statTab_[state][ic];
108 if (state == STATE_STOP) {
109 return false;
110 }
111 }
112 return true;
113 }
114
GetInstance()115 StringFilter& StringFilter::GetInstance()
116 {
117 return filter_;
118 }
119 } // namespace HiviewDFX
120 } // namespace OHOS
121
122