LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DEX/Parser.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
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#ifndef LIEF_DEX_PARSER_H
16#define LIEF_DEX_PARSER_H
17
18#include <string_view>
19#include <unordered_map>
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "LIEF/DEX/types.hpp"
25#include "LIEF/path.hpp"
26#include "LIEF/visibility.h"
27
28namespace LIEF {
29class VectorStream;
30
31namespace DEX {
32class Class;
33class Method;
34class Field;
35class File;
36class Type;
37
40 public:
42 static std::unique_ptr<File> parse(std::string_view file);
43
46 template<class PathT, enable_if_path_t<PathT> = 0>
47 static std::unique_ptr<File> parse(const PathT& file) {
48 return parse(file.string());
49 }
50
51 static std::unique_ptr<File> parse(std::vector<uint8_t> data,
52 std::string_view name = "");
53
54 Parser& operator=(const Parser& copy) = delete;
55 Parser(const Parser& copy) = delete;
56
57 private:
58 Parser();
59 Parser(std::string_view file);
60 Parser(std::vector<uint8_t> data);
61 ~Parser();
62
63 void init(std::string_view name, dex_version_t version);
64
65 template<typename DEX_T>
66 void parse_file();
67
68 template<typename DEX_T>
69 void parse_header();
70
71 template<typename DEX_T>
72 void parse_map();
73
74 template<typename DEX_T>
75 void parse_strings();
76
77 template<typename DEX_T>
78 void parse_types();
79
80 template<typename DEX_T>
81 void parse_fields();
82
83 template<typename DEX_T>
84 void parse_prototypes();
85
86 template<typename DEX_T>
87 void parse_methods();
88
89 template<typename DEX_T>
90 void parse_classes();
91
92 template<typename DEX_T>
93 void parse_class_data(uint32_t offset, Class& cls);
94
95 template<typename DEX_T>
96 void parse_field(size_t index, Class& cls, bool is_static);
97
98 template<typename DEX_T>
99 void parse_method(size_t index, Class& cls, bool is_virtual);
100
101 template<typename DEX_T>
102 void parse_code_info(uint32_t offset, Method& method);
103
104 void resolve_inheritance();
105
106 void resolve_external_methods();
107
108 void resolve_external_fields();
109
110 void resolve_types();
111
112 std::unique_ptr<File> file_;
113
114 // Map of inheritance relationship when parsing classes ('parse_classes')
115 // The key is the parent class name of the value
116 std::unordered_multimap<std::string, Class*> inheritance_;
117
118 // Map of method/class relationship when parsing methods ('parse_methods')
119 // The key is the Class name in which the method is defined
120 std::unordered_multimap<std::string, Method*> class_method_map_;
121
122 // Map of field/class relationship when parsing fields ('parse_fields')
123 // The key is the Class name in which the field is defined
124 std::unordered_multimap<std::string, Field*> class_field_map_;
125
126 std::unordered_multimap<std::string, Type*> class_type_map_;
127
128 std::unique_ptr<VectorStream> stream_;
129};
130
131}
132}
133#endif
Class which represents a DEX Class (i.e. a Java/Kotlin class).
Definition DEX/Class.hpp:36
Class which represents a DEX Field.
Definition Field.hpp:35
Class that represents a DEX file.
Definition DEX/File.hpp:39
Class which represents a DEX::Method.
Definition DEX/Method.hpp:36
Parser(const Parser &copy)=delete
static std::unique_ptr< File > parse(std::vector< uint8_t > data, std::string_view name="")
static std::unique_ptr< File > parse(const PathT &file)
Same as parse(std::string_view) but the file is given as a std::filesystem::path.
Definition DEX/Parser.hpp:47
static std::unique_ptr< File > parse(std::string_view file)
Parse the DEX file from the file path given in parameter.
Parser & operator=(const Parser &copy)=delete
Class which represents a DEX type as described in the format specifications: https://source....
Definition DEX/Type.hpp:34
Definition VectorStream.hpp:28
Definition DEX/Class.hpp:30
dex_version_t version(std::string_view file)
Return the DEX version of the given file.
uint32_t dex_version_t
Definition DEX/types.hpp:22
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:44