• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_GUARD_OBFUSCATE_MODULE_RECORD_H
17 #define PANDA_GUARD_OBFUSCATE_MODULE_RECORD_H
18 
19 #include "entity.h"
20 #include "file_path.h"
21 
22 namespace panda::guard {
23 
24 class FilePathItem final : public Entity, public IExtractNames {
25 public:
FilePathItem(Program * program,std::string literalArrayIdx)26     FilePathItem(Program *program, std::string literalArrayIdx)
27         : Entity(program), refFilePath_(program), literalArrayIdx_(std::move(literalArrayIdx))
28     {
29     }
30 
31     void Build() override;
32 
33     void RefreshNeedUpdate() override;
34 
35     void Update() override;
36 
37     void ExtractNames(std::set<std::string> &strings) const override;
38 
39     ReferenceFilePath refFilePath_;
40     std::string literalArrayIdx_;
41 };
42 
43 class RegularImportItem final : public Entity, public IExtractNames {
44 public:
RegularImportItem(Program * program,std::string literalArrayIdx)45     RegularImportItem(Program *program, std::string literalArrayIdx)
46         : Entity(program), literalArrayIdx_(std::move(literalArrayIdx))
47     {
48     }
49 
50     void RefreshNeedUpdate() override;
51 
52     void Update() override;
53 
54     void ExtractNames(std::set<std::string> &strings) const override;
55 
56     std::string literalArrayIdx_;
57 
58     uint32_t localNameIndex_ = 0;
59     std::string localName_;
60     std::string obfLocalName_;
61 
62     uint32_t importNameIndex_ = 0;
63     std::string importName_;
64     std::string obfImportName_;
65 
66     bool remoteFile_ = false;
67 
68 protected:
69     void WriteFileCache(const std::string &filePath) override;
70 
71     void WritePropertyCache() override;
72 };
73 
74 class NameSpaceImportItem final : public Entity, public IExtractNames {
75 public:
NameSpaceImportItem(Program * program,std::string literalArrayIdx)76     NameSpaceImportItem(Program *program, std::string literalArrayIdx)
77         : Entity(program), literalArrayIdx_(std::move(literalArrayIdx))
78     {
79     }
80 
81     void RefreshNeedUpdate() override;
82 
83     void Update() override;
84 
85     void ExtractNames(std::set<std::string> &strings) const override;
86 
87     std::string literalArrayIdx_;
88 
89     uint32_t localNameIndex_ = 0;
90     std::string localName_;
91     std::string obfLocalName_;
92 
93     bool remoteFile_ = false;
94 
95 protected:
96     void WriteFileCache(const std::string &filePath) override;
97 
98     void WritePropertyCache() override;
99 };
100 
101 class LocalExportItem final : public Entity, public IExtractNames {
102 public:
LocalExportItem(Program * program,std::string literalArrayIdx)103     LocalExportItem(Program *program, std::string literalArrayIdx)
104         : Entity(program), literalArrayIdx_(std::move(literalArrayIdx))
105     {
106     }
107 
108     void RefreshNeedUpdate() override;
109 
110     void Update() override;
111 
112     void ExtractNames(std::set<std::string> &strings) const override;
113 
114     std::string literalArrayIdx_;
115 
116     uint32_t localNameIndex_ = 0;
117     std::string localName_;
118     std::string obfLocalName_;
119 
120     uint32_t exportNameIndex_ = 0;
121     std::string exportName_;
122     std::string obfExportName_;
123 
124 protected:
125     void WriteFileCache(const std::string &filePath) override;
126 
127     void WritePropertyCache() override;
128 };
129 
130 class IndirectExportItem final : public Entity, public IExtractNames {
131 public:
IndirectExportItem(Program * program,std::string literalArrayIdx)132     IndirectExportItem(Program *program, std::string literalArrayIdx)
133         : Entity(program), literalArrayIdx_(std::move(literalArrayIdx))
134     {
135     }
136 
137     void RefreshNeedUpdate() override;
138 
139     void Update() override;
140 
141     void ExtractNames(std::set<std::string> &strings) const override;
142 
143     std::string literalArrayIdx_;
144 
145     uint32_t exportNameIndex_ = 0;
146     std::string exportName_;
147     std::string obfExportName_;
148 
149     uint32_t importNameIndex_ = 0;
150     std::string importName_;
151     std::string obfImportName_;
152 
153     bool remoteFile_ = false;
154 
155 protected:
156     void WriteFileCache(const std::string &filePath) override;
157 
158     void WritePropertyCache() override;
159 };
160 
161 class ModuleRecord final : public Entity, public IExtractNames {
162 public:
ModuleRecord(Program * program,const std::string & name)163     ModuleRecord(Program *program, const std::string &name) : Entity(program, name) {}
164 
165     void Build() override;
166 
167     void ExtractNames(std::set<std::string> &strings) const override;
168 
169     bool IsExportVar(const std::string &var);
170 
171     void RefreshNeedUpdate() override;
172 
173     void Update() override;
174 
175     void WriteNameCache(const std::string &filePath) override;
176 
177     /**
178      * get local var export name by index
179      * @return export name
180      */
181     std::string GetLocalExportName(uint32_t index);
182 
183     /**
184      * Update file name references in this node
185      */
186     void UpdateFileNameReferences();
187 
188 private:
189     void CreateModuleVar(const pandasm::LiteralArray &literalArray);
190 
191     void CreateFilePathList(const std::vector<pandasm::LiteralArray::Literal> &literals, uint32_t &offset);
192 
193     void CreateRegularImportList(const std::vector<pandasm::LiteralArray::Literal> &literals, uint32_t &offset);
194 
195     void CreateNameSpaceImportList(const std::vector<pandasm::LiteralArray::Literal> &literals, uint32_t &offset);
196 
197     void CreateLocalExportList(const std::vector<pandasm::LiteralArray::Literal> &literals, uint32_t &offset);
198 
199     void CreateIndirectExportList(const std::vector<pandasm::LiteralArray::Literal> &literals, uint32_t &offset);
200 
201     void Print();
202 
203 public:
204     std::string literalArrayIdx_;
205     std::vector<FilePathItem> filePathList_ {};
206     std::vector<RegularImportItem> regularImportList_ {};
207     std::vector<NameSpaceImportItem> nameSpaceImportList_ {};
208     std::vector<LocalExportItem> localExportList_ {};
209     std::vector<IndirectExportItem> indirectExportList_ {};
210 };
211 
212 }  // namespace panda::guard
213 
214 #endif  // PANDA_GUARD_OBFUSCATE_MODULE_RECORD_H
215