1 /**
2 * Copyright (c) 2024 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 "name_mapping.h"
17
18 #include "utils/logger.h"
19
20 #include "configs/guard_context.h"
21 #include "order_name_generator.h"
22 #include "util/assert_util.h"
23 #include "util/string_util.h"
24
25 namespace {
26 constexpr std::string_view TAG = "[Name_Mapping]";
27 constexpr std::string_view PATH_DELIMITER = "/";
28 } // namespace
29
NameMapping(const std::shared_ptr<NameCache> & nameCache,std::shared_ptr<GuardOptions> options)30 panda::guard::NameMapping::NameMapping(const std::shared_ptr<NameCache> &nameCache,
31 std::shared_ptr<GuardOptions> options)
32 : options_(std::move(options))
33 {
34 fileNameGenerator_ = std::make_unique<OrderNameGenerator>(nameCache->GetHistoryUsedNames());
35 nameGenerator_ = std::make_unique<OrderNameGenerator>(nameCache->GetHistoryUsedNames());
36 fileNameMapping_ = nameCache->GetHistoryFileNameMapping();
37 nameMapping_ = nameCache->GetHistoryNameMapping();
38 }
39
GetName(const std::string & origin,bool generateNew)40 std::string panda::guard::NameMapping::GetName(const std::string &origin, bool generateNew)
41 {
42 std::string obfName;
43 if (origin.empty()) {
44 LOG(WARNING, PANDAGUARD) << TAG << "origin name is empty";
45 return obfName;
46 }
47 const auto &[prefix, suffix] = StringUtil::SplitAnonymousName(origin);
48 do {
49 auto item = nameMapping_.find(prefix);
50 if (item != nameMapping_.end()) {
51 obfName = item->second;
52 break;
53 }
54
55 if (!generateNew) {
56 obfName = prefix;
57 break;
58 }
59
60 if (StringUtil::IsNumber(prefix)) {
61 obfName = prefix;
62 break;
63 }
64
65 if (StringUtil::IsAnonymousNameSpaceName(prefix)) {
66 obfName = prefix;
67 break;
68 }
69
70 if (options_ && (options_->IsReservedNames(prefix) || options_->IsReservedProperties(prefix) ||
71 options_->IsReservedToplevelNames(prefix))) {
72 obfName = prefix;
73 break;
74 }
75
76 obfName = nameGenerator_->GetName();
77 } while (false);
78 PANDA_GUARD_ASSERT_PRINT(obfName.empty(), TAG, ErrorCode::GENERIC_ERROR,
79 "GetName name generator failed, name is empty");
80 nameMapping_.emplace(origin, obfName);
81 obfName.append(suffix);
82 LOG(INFO, PANDAGUARD) << TAG << "NameMapping:" << origin << " --> " << obfName;
83 return obfName;
84 }
85
GetFileName(const std::string & origin,bool generateNew)86 std::string panda::guard::NameMapping::GetFileName(const std::string &origin, bool generateNew)
87 {
88 std::string obfName;
89 if (origin.empty()) {
90 LOG(WARNING, PANDAGUARD) << TAG << "origin file name is empty";
91 return obfName;
92 }
93 do {
94 auto item = fileNameMapping_.find(origin);
95 if (item != fileNameMapping_.end()) {
96 obfName = item->second;
97 break;
98 }
99
100 if (!generateNew) {
101 obfName = origin;
102 break;
103 }
104
105 if (options_ && options_->IsReservedFileNames(origin)) {
106 obfName = origin;
107 break;
108 }
109
110 obfName = fileNameGenerator_->GetName();
111 GuardContext::GetInstance()->GetNameCache()->AddNewNameCacheObfFileName(origin, obfName);
112 } while (false);
113 PANDA_GUARD_ASSERT_PRINT(obfName.empty(), TAG, ErrorCode::GENERIC_ERROR,
114 "GetFileName name generator failed, name is empty");
115 fileNameMapping_.emplace(origin, obfName);
116 LOG(INFO, PANDAGUARD) << TAG << "FileNameMapping:" << origin << " --> " << obfName;
117 return obfName;
118 }
119
GetFilePath(const std::string & origin,bool generateNew)120 std::string panda::guard::NameMapping::GetFilePath(const std::string &origin, bool generateNew)
121 {
122 auto parts = StringUtil::StrictSplit(origin, PATH_DELIMITER.data());
123 PANDA_GUARD_ASSERT_PRINT(parts.empty() || parts[0].empty(), TAG, ErrorCode::GENERIC_ERROR, "invalid file path");
124
125 std::string obfPath = GetFileName(parts[0], generateNew);
126 for (size_t i = 1; i < parts.size(); i++) {
127 obfPath += PATH_DELIMITER.data() + GetFileName(parts[i], generateNew);
128 }
129 return obfPath;
130 }
131
AddReservedNames(const std::set<std::string> & nameList) const132 void panda::guard::NameMapping::AddReservedNames(const std::set<std::string> &nameList) const
133 {
134 nameGenerator_->AddReservedNames(nameList);
135 fileNameGenerator_->AddReservedNames(nameList);
136 }
137
AddNameMapping(const std::string & name)138 void panda::guard::NameMapping::AddNameMapping(const std::string &name)
139 {
140 if (name.empty()) {
141 return;
142 }
143 LOG(INFO, PANDAGUARD) << TAG << "NameMapping[Add]:" << name << " --> " << name;
144 nameMapping_.emplace(name, name);
145 }
146
AddNameMapping(const std::string & origin,const std::string & name)147 void panda::guard::NameMapping::AddNameMapping(const std::string &origin, const std::string &name)
148 {
149 LOG(INFO, PANDAGUARD) << TAG << "NameMapping[Add]:" << origin << " --> " << name;
150 nameMapping_.emplace(origin, name);
151 }
152
AddNameMapping(const std::set<std::string> & nameList)153 void panda::guard::NameMapping::AddNameMapping(const std::set<std::string> &nameList)
154 {
155 for (const auto &name : nameList) {
156 AddNameMapping(name);
157 }
158 }
159
AddFileNameMapping(const std::string & name)160 void panda::guard::NameMapping::AddFileNameMapping(const std::string &name)
161 {
162 if (name.empty()) {
163 return;
164 }
165 LOG(INFO, PANDAGUARD) << TAG << "AddFileNameMapping:" << name << " --> " << name;
166 fileNameMapping_.emplace(name, name);
167 }
168
AddFileNameMapping(const std::set<std::string> & nameList)169 void panda::guard::NameMapping::AddFileNameMapping(const std::set<std::string> &nameList)
170 {
171 for (const auto &name : nameList) {
172 AddFileNameMapping(name);
173 }
174 }
175