forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.cpp
More file actions
188 lines (141 loc) · 4.18 KB
/
Copy pathBinary.cpp
File metadata and controls
188 lines (141 loc) · 4.18 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
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 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 <fstream>
#include "LIEF/VDEX/File.hpp"
#include "LIEF/DEX/File.hpp"
#include "LIEF/DEX/Class.hpp"
#include "LIEF/OAT/Binary.hpp"
#include "LIEF/OAT/hash.hpp"
#include "LIEF/OAT/Class.hpp"
#include "LIEF/OAT/Method.hpp"
#include "LIEF/OAT/DexFile.hpp"
#if defined(LIEF_JSON_SUPPORT)
#include "visitors/json.hpp"
#endif
namespace LIEF {
namespace OAT {
Binary::Binary() {
format_ = LIEF::Binary::FORMATS::OAT;
}
Binary::~Binary() = default;
const Header& Binary::header() const {
return header_;
}
Header& Binary::header() {
return const_cast<Header&>(static_cast<const Binary*>(this)->header());
}
Binary::it_dex_files Binary::dex_files() {
if (vdex_ != nullptr) {
return vdex_->dex_files_;
}
return dex_files_;
}
Binary::it_const_dex_files Binary::dex_files() const {
if (vdex_ != nullptr) {
return vdex_->dex_files_;
}
return dex_files_;
}
Binary::it_oat_dex_files Binary::oat_dex_files() {
return oat_dex_files_;
}
Binary::it_const_oat_dex_files Binary::oat_dex_files() const {
return oat_dex_files_;
}
Binary::it_const_classes Binary::classes() const {
return classes_list_;
}
Binary::it_classes Binary::classes() {
return classes_list_;
}
bool Binary::has_class(const std::string& class_name) const {
return classes_.find(DEX::Class::fullname_normalized(class_name)) != std::end(classes_);
}
const Class* Binary::get_class(const std::string& class_name) const {
auto it = classes_.find(DEX::Class::fullname_normalized(class_name));
if (it == std::end(classes_)) {
return nullptr;
}
return it->second;
}
Class* Binary::get_class(const std::string& class_name) {
return const_cast<Class*>(static_cast<const Binary*>(this)->get_class(class_name));
}
const Class* Binary::get_class(size_t index) const {
if (index >= classes_.size()) {
return nullptr;
}
const auto it = std::find_if(std::begin(classes_), std::end(classes_),
[index] (const std::pair<std::string, Class*>& p) {
return p.second->index() == index;
});
if (it == std::end(classes_)) {
return nullptr;
}
return it->second;
}
Class* Binary::get_class(size_t index) {
return const_cast<Class*>(static_cast<const Binary*>(this)->get_class(index));
}
Binary::it_const_methods Binary::methods() const {
return methods_;
}
Binary::it_methods Binary::methods() {
return methods_;
}
Binary::dex2dex_info_t Binary::dex2dex_info() const {
dex2dex_info_t info;
for (const DEX::File& dex_file : dex_files()) {
info.emplace(&dex_file, dex_file.dex2dex_info());
}
return info;
}
std::string Binary::dex2dex_json_info() {
#if defined(LIEF_JSON_SUPPORT)
json mapping = json::object();
for (const DEX::File& dex_file : dex_files()) {
json dex2dex = json::parse(dex_file.dex2dex_json_info());
mapping[dex_file.name()] = dex2dex;
}
return mapping.dump();
#else
return "";
#endif
}
void Binary::add_class(std::unique_ptr<Class> cls) {
classes_.emplace(cls->fullname(), cls.get());
classes_list_.push_back(std::move(cls));
}
void Binary::accept(Visitor& visitor) const {
visitor.visit(*this);
}
std::ostream& operator<<(std::ostream& os, const Binary& binary) {
os << "Header" << '\n';
os << "======" << '\n';
os << binary.header() << '\n';
if (binary.oat_dex_files().size() > 0) {
os << "Dex Files" << '\n';
os << "=========" << '\n';
for (const DexFile& dex : binary.oat_dex_files()) {
os << dex << '\n';
}
}
os << "Number of classes: " << std::dec << binary.classes().size() << '\n';
os << "Number of methods: " << std::dec << binary.methods().size() << '\n';
return os;
}
}
}