forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
175 lines (141 loc) · 4.81 KB
/
Copy pathParser.cpp
File metadata and controls
175 lines (141 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include "logging.hpp"
#include <LIEF/BinaryStream/VectorStream.hpp>
#include "LIEF/DEX/Parser.hpp"
#include "LIEF/DEX/File.hpp"
#include "LIEF/DEX/Type.hpp"
#include "LIEF/DEX/utils.hpp"
#include "DEX/Structures.hpp"
#include "Parser.tcc"
namespace LIEF {
namespace DEX {
Parser::~Parser() = default;
Parser::Parser() = default;
std::unique_ptr<File> Parser::parse(const std::string& filename) {
if (!is_dex(filename)) {
LIEF_ERR("'{}' is not a DEX File", filename);
return nullptr;
}
Parser parser{filename};
dex_version_t version = DEX::version(filename);
parser.init(filename, version);
return std::move(parser.file_);
}
std::unique_ptr<File> Parser::parse(std::vector<uint8_t> data, const std::string& name) {
if (!is_dex(data)) {
LIEF_ERR("'{}' is not a DEX File", name);
return nullptr;
}
dex_version_t version = DEX::version(data);
Parser parser{std::move(data)};
parser.init(name, version);
return std::move(parser.file_);
}
Parser::Parser(std::vector<uint8_t> data) :
file_{new File{}},
stream_{std::make_unique<VectorStream>(std::move(data))}
{}
Parser::Parser(const std::string& file) :
file_{new File{}}
{
auto stream = VectorStream::from_file(file);
if (!stream) {
LIEF_ERR("Can't create the stream");
} else {
stream_ = std::make_unique<VectorStream>(std::move(*stream));
}
}
void Parser::init(const std::string& name, dex_version_t version) {
LIEF_DEBUG("Parsing file: {}", name);
if (version == details::DEX_35::dex_version) {
return parse_file<details::DEX35>();
}
if (version == details::DEX_37::dex_version) {
return parse_file<details::DEX37>();
}
if (version == details::DEX_38::dex_version) {
return parse_file<details::DEX38>();
}
if (version == details::DEX_39::dex_version) {
return parse_file<details::DEX39>();
}
}
void Parser::resolve_inheritance() {
LIEF_DEBUG("Resolving inheritance relationship for #{:d} classes", inheritance_.size());
for (const std::pair<const std::string, Class*>& p : inheritance_) {
const std::string& parent_name = p.first;
Class* child = p.second;
const auto it_inner_class = file_->classes_.find(parent_name);
if (it_inner_class == std::end(file_->classes_)) {
auto external_class = std::make_unique<Class>(parent_name);
child->parent_ = external_class.get();
file_->add_class(std::move(external_class));
} else {
child->parent_ = it_inner_class->second;
}
}
}
void Parser::resolve_external_methods() {
LIEF_DEBUG("Resolving external methods for #{:d} methods", class_method_map_.size());
for (const std::pair<const std::string, Method*>& p : class_method_map_) {
const std::string& clazz = p.first;
Method* method = p.second;
const auto it_inner_class = file_->classes_.find(clazz);
if (it_inner_class == std::end(file_->classes_)) {
auto cls = std::make_unique<Class>(clazz);
cls->methods_.push_back(method);
method->parent_ = cls.get();
file_->add_class(std::move(cls));
} else {
Class* cls = it_inner_class->second;
method->parent_ = cls;
cls->methods_.push_back(method);
}
}
}
void Parser::resolve_external_fields() {
LIEF_DEBUG("Resolving external fields for #{:d} fields", class_field_map_.size());
for (const std::pair<const std::string, Field*>& p : class_field_map_) {
const std::string& clazz = p.first;
Field* field = p.second;
const auto it_inner_class = file_->classes_.find(clazz);
if (it_inner_class == std::end(file_->classes_)) {
auto cls = std::make_unique<Class>(clazz);
cls->fields_.push_back(field);
field->parent_ = cls.get();
file_->add_class(std::move(cls));
} else {
Class* cls = it_inner_class->second;
field->parent_ = cls;
cls->fields_.push_back(field);
}
}
}
void Parser::resolve_types() {
for (const auto& p : class_type_map_) {
if(Class* cls = file_->get_class(p.first)) {
p.second->underlying_array_type().cls_ = cls;
} else {
auto new_cls = std::make_unique<Class>(p.first);
p.second->underlying_array_type().cls_ = new_cls.get();
file_->add_class(std::move(new_cls));
}
}
}
} // namespace DEX
} // namespace LIEF