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 do {
48 auto item = nameMapping_.find(origin);
49 if (item != nameMapping_.end()) {
50 obfName = item->second;
51 break;
52 }
53
54 if (!generateNew) {
55 obfName = origin;
56 break;
57 }
58
59 if (StringUtil::IsNumber(origin)) {
60 obfName = origin;
61 break;
62 }
63
64 if (StringUtil::IsAnonymousNameSpaceName(origin)) {
65 obfName = origin;
66 break;
67 }
68
69 if (options_ && (options_->IsReservedNames(origin) || options_->IsReservedProperties(origin) ||
70 options_->IsReservedToplevelNames(origin))) {
71 obfName = origin;
72 break;
73 }
74
75 obfName = nameGenerator_->GetName();
76 } while (false);
77 PANDA_GUARD_ASSERT_PRINT(obfName.empty(), TAG, ErrorCode::GENERIC_ERROR,
78 "GetName name generator failed, name is empty");
79 nameMapping_.emplace(origin, obfName);
80 LOG(INFO, PANDAGUARD) << TAG << "NameMapping:" << origin << " --> " << obfName;
81 return obfName;
82 }
83
GetFileName(const std::string & origin,bool generateNew)84 std::string panda::guard::NameMapping::GetFileName(const std::string &origin, bool generateNew)
85 {
86 std::string obfName;
87 if (origin.empty()) {
88 LOG(WARNING, PANDAGUARD) << TAG << "origin file name is empty";
89 return obfName;
90 }
91 do {
92 auto item = fileNameMapping_.find(origin);
93 if (item != fileNameMapping_.end()) {
94 obfName = item->second;
95 break;
96 }
97
98 if (!generateNew) {
99 obfName = origin;
100 break;
101 }
102
103 if (options_ && options_->IsReservedFileNames(origin)) {
104 obfName = origin;
105 break;
106 }
107
108 obfName = fileNameGenerator_->GetName();
109 GuardContext::GetInstance()->GetNameCache()->AddNewNameCacheObfFileName(origin, obfName);
110 } while (false);
111 PANDA_GUARD_ASSERT_PRINT(obfName.empty(), TAG, ErrorCode::GENERIC_ERROR,
112 "GetFileName name generator failed, name is empty");
113 fileNameMapping_.emplace(origin, obfName);
114 LOG(INFO, PANDAGUARD) << TAG << "FileNameMapping:" << origin << " --> " << obfName;
115 return obfName;
116 }
117
GetFilePath(const std::string & origin,bool generateNew)118 std::string panda::guard::NameMapping::GetFilePath(const std::string &origin, bool generateNew)
119 {
120 auto parts = StringUtil::StrictSplit(origin, PATH_DELIMITER.data());
121 PANDA_GUARD_ASSERT_PRINT(parts.empty() || parts[0].empty(), TAG, ErrorCode::GENERIC_ERROR, "invalid file path");
122
123 std::string obfPath = GetFileName(parts[0], generateNew);
124 for (size_t i = 1; i < parts.size(); i++) {
125 obfPath += PATH_DELIMITER.data() + GetFileName(parts[i], generateNew);
126 }
127 return obfPath;
128 }
129
AddReservedNames(const std::set<std::string> & nameList) const130 void panda::guard::NameMapping::AddReservedNames(const std::set<std::string> &nameList) const
131 {
132 nameGenerator_->AddReservedNames(nameList);
133 fileNameGenerator_->AddReservedNames(nameList);
134 }
135
AddNameMapping(const std::string & name)136 void panda::guard::NameMapping::AddNameMapping(const std::string &name)
137 {
138 if (name.empty()) {
139 return;
140 }
141 LOG(INFO, PANDAGUARD) << TAG << "NameMapping[Add]:" << name << " --> " << name;
142 nameMapping_.emplace(name, name);
143 }
144
AddNameMapping(const std::set<std::string> & nameList)145 void panda::guard::NameMapping::AddNameMapping(const std::set<std::string> &nameList)
146 {
147 for (const auto &name : nameList) {
148 AddNameMapping(name);
149 }
150 }
151
AddFileNameMapping(const std::string & name)152 void panda::guard::NameMapping::AddFileNameMapping(const std::string &name)
153 {
154 if (name.empty()) {
155 return;
156 }
157 LOG(INFO, PANDAGUARD) << TAG << "AddFileNameMapping:" << name << " --> " << name;
158 fileNameMapping_.emplace(name, name);
159 }
160
AddFileNameMapping(const std::set<std::string> & nameList)161 void panda::guard::NameMapping::AddFileNameMapping(const std::set<std::string> &nameList)
162 {
163 for (const auto &name : nameList) {
164 AddFileNameMapping(name);
165 }
166 }
167