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 "abc_string_table.h"
17 #include "abc_file_utils.h"
18
19 namespace ark::abc2program {
20
GetStringById(uint32_t stringId) const21 std::string AbcStringTable::GetStringById(uint32_t stringId) const
22 {
23 panda_file::File::EntityId entityId(stringId);
24 return GetStringById(entityId);
25 }
26
GetStringById(panda_file::File::EntityId entityId) const27 std::string AbcStringTable::GetStringById(panda_file::File::EntityId entityId) const
28 {
29 std::string str = StringDataToString(file_.GetStringData(entityId));
30 return str;
31 }
32
AddStringId(uint32_t stringId)33 void AbcStringTable::AddStringId(uint32_t stringId)
34 {
35 auto it = stingIdSet_.find(stringId);
36 if (it != stingIdSet_.end()) {
37 return;
38 }
39 stingIdSet_.insert(stringId);
40 }
41
AddStringId(panda_file::File::EntityId entityId)42 void AbcStringTable::AddStringId(panda_file::File::EntityId entityId)
43 {
44 AddStringId(entityId.GetOffset());
45 }
46
GetStringSet() const47 std::set<std::string> AbcStringTable::GetStringSet() const
48 {
49 std::set<std::string> stringSet;
50 for (uint32_t stringId : stingIdSet_) {
51 stringSet.insert(GetStringById(stringId));
52 }
53 return stringSet;
54 }
55
Dump(std::ostream & os) const56 void AbcStringTable::Dump(std::ostream &os) const
57 {
58 for (uint32_t stringId : stingIdSet_) {
59 DumpStringById(os, stringId);
60 }
61 }
62
DumpStringById(std::ostream & os,uint32_t stringId) const63 void AbcStringTable::DumpStringById(std::ostream &os, uint32_t stringId) const
64 {
65 os << "[offset:0x" << std::hex << stringId << ", name_value:" << GetStringById(stringId) << "]" << std::endl;
66 }
67
StringDataToString(panda_file::File::StringData sd) const68 std::string AbcStringTable::StringDataToString(panda_file::File::StringData sd) const
69 {
70 std::string str = std::string(utf::Mutf8AsCString(sd.data));
71 std::replace(str.begin(), str.end(), '#', '_');
72 size_t symPos = 0;
73 while (symPos = str.find_first_of("\a\b\f\n\r\t\v\'\\\"", symPos), symPos != std::string::npos) {
74 std::string sym;
75 switch (str[symPos]) {
76 case '\a':
77 sym = R"(\a)";
78 break;
79 case '\b':
80 sym = R"(\b)";
81 break;
82 case '\f':
83 sym = R"(\f)";
84 break;
85 case '\n':
86 sym = R"(\n)";
87 break;
88 case '\r':
89 sym = R"(\r)";
90 break;
91 case '\t':
92 sym = R"(\t)";
93 break;
94 case '\v':
95 sym = R"(\v)";
96 break;
97 case '\"':
98 sym = R"(\")";
99 break;
100 case '\'':
101 sym = R"(\')";
102 break;
103 case '\\':
104 sym = R"(\\)";
105 break;
106 default:
107 UNREACHABLE();
108 }
109 str = str.replace(symPos, 1, sym);
110 ASSERT(sym.size() == 2U);
111 symPos += 2U;
112 }
113 return str;
114 }
115
116 } // namespace ark::abc2program
117