LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
DEX/File.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_DEX_FILE_H
17#define LIEF_DEX_FILE_H
18#include <memory>
19
20#include "LIEF/visibility.h"
21#include "LIEF/Object.hpp"
22
23#include "LIEF/DEX/Header.hpp"
24#include "LIEF/DEX/MapList.hpp"
26#include "LIEF/DEX/deopt.hpp"
27#include "LIEF/DEX/types.hpp"
28
29namespace LIEF {
30namespace DEX {
31class Parser;
32class Class;
33class Method;
34class Type;
35class Prototype;
36class Field;
37class LIEF_API File : public Object {
40 friend class Parser;
41
42 public:
43 using classes_t = std::unordered_map<std::string, Class*>;
44 using classes_list_t = std::vector<std::unique_ptr<Class>>;
45 using it_classes = ref_iterator<classes_list_t&, Class*>;
46 using it_const_classes = const_ref_iterator<const classes_list_t&, const Class*>;
47
48 using methods_t = std::vector<std::unique_ptr<Method>>;
49 using it_methods = ref_iterator<methods_t&, Method*>;
50 using it_const_methods = const_ref_iterator<const methods_t&, const Method*>;
51
52 using strings_t = std::vector<std::unique_ptr<std::string>>;
53 using it_strings = ref_iterator<strings_t&, std::string*>;
54 using it_const_strings = const_ref_iterator<const strings_t&, const std::string*>;
55
56 using types_t = std::vector<std::unique_ptr<Type>>;
57 using it_types = ref_iterator<types_t&, Type*>;
58 using it_const_types = const_ref_iterator<const types_t&, const Type*>;
59
60 using prototypes_t = std::vector<std::unique_ptr<Prototype>>;
61 using it_prototypes = ref_iterator<prototypes_t&, Prototype*>;
62 using it_const_prototypes = const_ref_iterator<const prototypes_t&, const Prototype*>;
63
64 using fields_t = std::vector<std::unique_ptr<Field>>;
65 using it_fields = ref_iterator<fields_t&, Field*>;
66 using it_const_fields = const_ref_iterator<const fields_t&, const Field*>;
67
68 public:
69 File& operator=(const File& copy) = delete;
70 File(const File& copy) = delete;
71 dex_version_t version() const;
74 const std::string& name() const;
77
78 void name(const std::string& name);
79 const std::string& location() const;
82 void location(const std::string& location);
83 const Header& header() const;
86 Header& header();
87 it_const_classes classes() const;
90 it_classes classes();
91 bool has_class(const std::string& class_name) const;
94 const Class* get_class(const std::string& class_name) const;
97
98 Class* get_class(const std::string& class_name);
99 const Class* get_class(size_t index) const;
102
103 Class* get_class(size_t index);
107 std::string dex2dex_json_info() const;
110 it_const_methods methods() const;
113 it_methods methods();
114 it_const_fields fields() const;
117 it_fields fields();
118 it_const_strings strings() const;
121 it_strings strings();
122 it_const_types types() const;
125 it_types types();
126 it_prototypes prototypes();
129 it_const_prototypes prototypes() const;
130 const MapList& map() const;
133 MapList& map();
134 std::string save(const std::string& path = "", bool deoptimize = true) const;
137
138 std::vector<uint8_t> raw(bool deoptimize = true) const;
139
140 void accept(Visitor& visitor) const override;
141
142
143 ~File() override;
144
145 LIEF_API friend std::ostream& operator<<(std::ostream& os, const File& file);
146
147 private:
148 File();
149
150 void add_class(std::unique_ptr<Class> cls);
151
152 static void deoptimize_nop(uint8_t* inst_ptr, uint32_t value);
153 static void deoptimize_return(uint8_t* inst_ptr, uint32_t value);
154 static void deoptimize_invoke_virtual(uint8_t* inst_ptr, uint32_t value, OPCODES new_inst);
155 static void deoptimize_instance_field_access(uint8_t* inst_ptr, uint32_t value, OPCODES new_inst);
156
157 std::string name_;
158 std::string location_;
159
160 Header header_;
161 classes_t classes_;
162 methods_t methods_;
163 fields_t fields_;
164 strings_t strings_;
165 types_t types_;
166 prototypes_t prototypes_;
167 MapList map_;
168
169 classes_list_t class_list_;
170 std::vector<uint8_t> original_data_;
171};
172
173}
174}
175
176#endif
Class which represents a DEX Class (i.e. a Java/Kotlin class)
Definition DEX/Class.hpp:36
Class which represent a DEX Field.
Definition Field.hpp:34
Class that represents a DEX file.
Definition DEX/File.hpp:39
void accept(Visitor &visitor) const override
MapList & map()
it_const_methods methods() const
Return an iterator over all the DEX::Method used in this DEX file.
void location(const std::string &location)
Class * get_class(const std::string &class_name)
it_fields fields()
std::vector< uint8_t > raw(bool deoptimize=true) const
it_const_fields fields() const
Return an iterator over all the DEX::Field used in this DEX file.
std::string save(const std::string &path="", bool deoptimize=true) const
Extract the current dex file and deoptimize it.
it_types types()
it_const_types types() const
Type pool.
Class * get_class(size_t index)
const Header & header() const
DEX header.
it_const_strings strings() const
String pool.
Header & header()
it_const_classes classes() const
All classes used in the DEX file
dex2dex_info_t dex2dex_info() const
De-optimize information.
const std::string & location() const
Location of this file.
const Class * get_class(const std::string &class_name) const
Return the DEX::Class object associated with the given name.
it_const_prototypes prototypes() const
it_strings strings()
bool has_class(const std::string &class_name) const
Check if the given class name exists.
File(const File &copy)=delete
~File() override
friend std::ostream & operator<<(std::ostream &os, const File &file)
it_classes classes()
const Class * get_class(size_t index) const
Return the DEX::Class object associated with the given index.
const MapList & map() const
DEX Map.
it_prototypes prototypes()
Prototype pool.
File & operator=(const File &copy)=delete
const std::string & name() const
Name of this file.
std::string dex2dex_json_info() const
De-optimize information as JSON.
void name(const std::string &name)
dex_version_t version() const
Version of the current DEX file.
it_methods methods()
Class which represents a DEX::Method.
Definition DEX/Method.hpp:36
Class which parses a DEX file to produce a DEX::File object.
Definition DEX/Parser.hpp:38
Class which represents a DEX method prototype.
Definition Prototype.hpp:29
Class which represents a DEX type as described in the format specifications: https://source....
Definition DEX/Type.hpp:33
Definition DEX/Class.hpp:30
uint32_t dex_version_t
Definition DEX/types.hpp:23
std::unordered_map< Class *, dex2dex_class_info_t > dex2dex_info_t
Definition deopt.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41