KUNAI Static Analyzer
Kunai is a library for doing static binary analysis of Dalvik.
Loading...
Searching...
No Matches
types.hpp
1//--------------------------------------------------------------------*- C++ -*-
2// Kunai-static-analyzer: library for doing analysis of dalvik files
3// @author Farenain <kunai.static.analysis@gmail.com>
4// @author Ernesto Java <javaernesto@gmail.com>
5//
6// @file types.hpp
7// @brief Class for managing all the types from the DEX file
8// the types are simply storing the string with the type.
9#ifndef KUNAI_DEX_PARSER_TYPES_HPP
10#define KUNAI_DEX_PARSER_TYPES_HPP
11
12#include "Kunai/Utils/kunaistream.hpp"
13#include "Kunai/DEX/parser/strings.hpp"
14
15#include <vector>
16#include <unordered_map>
17#include <memory>
18
19namespace KUNAI
20{
21namespace DEX
22{
25 class DVMType
26 {
27 public:
30 enum type_e
31 {
32 FUNDAMENTAL,
35 UNKNOWN
36 };
37
38 private:
40 enum type_e type;
42 std::string raw_type;
43
44 public:
48 DVMType(type_e type, std::string raw_type)
49 : type(type), raw_type(raw_type)
50 {}
51
53 virtual ~DVMType() = default;
54
57 virtual type_e get_type() const = 0;
58
61 virtual std::string print_type() const = 0;
62
65 std::string& get_raw()
66 {
67 return raw_type;
68 }
69
72 virtual const std::string& pretty_print()
73 {
74 return raw_type;
75 }
76 };
77
78 using dvmtype_t = std::unique_ptr<DVMType>;
79
82 class DVMFundamental : public DVMType
83 {
84 public:
87 {
88 BOOLEAN,
89 BYTE,
90 CHAR,
91 DOUBLE,
92 FLOAT,
93 INT,
94 LONG,
95 SHORT,
96 VOID
97 };
98
99 private:
101 enum fundamental_e f_type;
103 std::string name;
104
105 const std::unordered_map<fundamental_e, std::string> fundamental_s =
106 {
107 {BOOLEAN, "boolean"},
108 {BYTE, "byte"},
109 {CHAR, "char"},
110 {DOUBLE, "double"},
111 {FLOAT, "float"},
112 {INT, "int"},
113 {LONG, "long"},
114 {SHORT, "short"},
115 {VOID, "void"}
116 };
117 public:
121 DVMFundamental(fundamental_e f_type, std::string name) :
122 DVMType(FUNDAMENTAL, name), f_type(f_type), name(name)
123 {}
124
126 ~DVMFundamental() = default;
127
130 type_e get_type() const override
131 {
132 return FUNDAMENTAL;
133 }
134
137 std::string print_type() const override
138 {
139 return "Fundamental";
140 }
141
145 {
146 return f_type;
147 }
148
152 const std::string& print_fundamental_type(fundamental_e type) const
153 {
154 return fundamental_s.at(type);
155 }
156
159 const std::string& get_name() const
160 {
161 return name;
162 }
163
166 const std::string& pretty_print() override
167 {
168 return fundamental_s.at(f_type);
169 }
170 };
171
173 class DVMClass : public DVMType
174 {
176 std::string name;
178 std::string pretty_name;
179
180 public:
183 DVMClass(std::string name) :
184 DVMType(CLASS, name), name(name)
185 {}
186
188 ~DVMClass() = default;
189
192 type_e get_type() const override
193 {
194 return CLASS;
195 }
196
199 std::string print_type() const override
200 {
201 return "Class";
202 }
203
206 const std::string& get_name() const
207 {
208 return name;
209 }
210
213 std::string& get_name()
214 {
215 return name;
216 }
217
218 const std::string& pretty_print() override;
219 };
220
222 class DVMArray : public DVMType
223 {
224
227 size_t depth;
229 dvmtype_t array_type;
231 std::string pretty_name;
232 public:
237 DVMArray(std::string raw, size_t depth, dvmtype_t& array) :
238 DVMType(ARRAY, raw), depth(depth), array_type(std::move(array))
239 {}
240
242 ~DVMArray() = default;
243
246 type_e get_type() const override
247 {
248 return ARRAY;
249 }
250
253 std::string print_type() const override
254 {
255 return "Array";
256 }
257
260 const DVMType* get_array_type() const
261 {
262 return array_type.get();
263 }
264
267 size_t get_depth() const
268 {
269 return depth;
270 }
271
272 const std::string& pretty_print() override;
273 };
274
276 class Unknown : public DVMType
277 {
278 public:
282 Unknown(std::string raw) :
283 DVMType(UNKNOWN, raw)
284 {}
285
287 ~Unknown() = default;
288
291 type_e get_type() const override
292 {
293 return UNKNOWN;
294 }
295
298 std::string print_type() const override
299 {
300 return "Unknown";
301 }
302 };
303
304 class Types
305 {
307 std::vector<dvmtype_t> ordered_types;
309 std::unordered_map<std::uint32_t, DVMType*> types_by_id;
311 std::uint32_t number_of_types;
313 std::uint32_t offset;
314 public:
316 Types() = default;
318 ~Types() = default;
319
326 stream::KunaiStream* stream,
327 Strings* strings,
328 std::uint32_t number_of_types,
329 std::uint32_t types_offset
330 );
331
334 const std::vector<dvmtype_t>& get_ordered_types() const
335 {
336 return ordered_types;
337 }
338
341 const std::unordered_map<std::uint32_t, DVMType*>& get_types_by_id() const
342 {
343 return types_by_id;
344 }
345
349 DVMType* get_type_by_id(std::uint32_t type_id);
350
354 DVMType* get_type_from_order(std::uint32_t pos);
355
358 std::uint32_t get_number_of_types() const
359 {
360 return number_of_types;
361 }
362
365 std::uint32_t get_offset() const
366 {
367 return offset;
368 }
369
374 friend std::ostream &operator<<(std::ostream &os, const Types &entry);
375
378 void to_xml(std::ofstream& xml_file);
379 private:
384 dvmtype_t parse_type(std::string& name);
385 };
386} // namespace DEX
387} // namespace KUNAI
388
389
390#endif
Class that represent the array types.
Definition types.hpp:223
DVMArray(std::string raw, size_t depth, dvmtype_t &array)
Constructor of DVMArray.
Definition types.hpp:237
std::string print_type() const override
Return the string representation of the type.
Definition types.hpp:253
type_e get_type() const override
Return the type in this case an Array type.
Definition types.hpp:246
~DVMArray()=default
Destructor of DVMArray.
size_t get_depth() const
Get the depth of the array specified as [[.
Definition types.hpp:267
const DVMType * get_array_type() const
Return a pointer to the type of the array.
Definition types.hpp:260
const std::string & pretty_print() override
Pretty print the name of the type.
Classes of the DVM.
Definition types.hpp:174
type_e get_type() const override
get type_e enum value for Class object.
Definition types.hpp:192
std::string & get_name()
Return a reference to the name.
Definition types.hpp:213
DVMClass(std::string name)
constructor of DVM class with the name of the class
Definition types.hpp:183
std::string print_type() const override
get the type in string format
Definition types.hpp:199
const std::string & pretty_print() override
Pretty print the name of the type.
~DVMClass()=default
default destructor of DVMClass
const std::string & get_name() const
Get the name of the class.
Definition types.hpp:206
Fundamental types from the DVM, these are the common from many other languages.
Definition types.hpp:83
~DVMFundamental()=default
Destructor of the fundamental.
const std::string & print_fundamental_type(fundamental_e type) const
Return a reference to a string with the fundamental type.
Definition types.hpp:152
DVMFundamental(fundamental_e f_type, std::string name)
Constructor of the DVMFundamental.
Definition types.hpp:121
type_e get_type() const override
get the type of the object
Definition types.hpp:130
const std::string & pretty_print() override
Get a pretty printed version of the name.
Definition types.hpp:166
fundamental_e get_fundamental_type() const
get the stored fundamental type
Definition types.hpp:144
fundamental_e
enum with the fundamental types
Definition types.hpp:87
const std::string & get_name() const
Return a reference to the fundamental name.
Definition types.hpp:159
std::string print_type() const override
Return a string with the name of the type.
Definition types.hpp:137
Represents the base class of a Type in the DVM we have different types.
Definition types.hpp:26
virtual type_e get_type() const =0
Virtual method to return the type.
type_e
Types of the DVM we have by default fundamental, classes and array types.
Definition types.hpp:31
@ CLASS
fundamental type (int, float...)
Definition types.hpp:33
@ ARRAY
user defined class
Definition types.hpp:34
@ UNKNOWN
an array type
Definition types.hpp:35
virtual const std::string & pretty_print()
Pretty print the name of the type.
Definition types.hpp:72
DVMType(type_e type, std::string raw_type)
Constructor of DVMType.
Definition types.hpp:48
std::string & get_raw()
get raw string from the type object
Definition types.hpp:65
virtual std::string print_type() const =0
Get the type on its string representation.
virtual ~DVMType()=default
Destructor of DVMType.
Storage class for all the strings of the DEX file.
Definition strings.hpp:34
Definition types.hpp:305
void to_xml(std::ofstream &xml_file)
Dump the types to an xml file.
std::uint32_t get_number_of_types() const
Get the number of types stored.
Definition types.hpp:358
friend std::ostream & operator<<(std::ostream &os, const Types &entry)
Pretty printer for Types.
const std::vector< dvmtype_t > & get_ordered_types() const
Get a reference to the vector with all the types.
Definition types.hpp:334
DVMType * get_type_by_id(std::uint32_t type_id)
Get a type given its string ID.
std::uint32_t get_offset() const
Get the offset where types are stored.
Definition types.hpp:365
DVMType * get_type_from_order(std::uint32_t pos)
Get a type given position.
Types()=default
Constructor of the Types object, nothing for initialization.
~Types()=default
Destructor of Types.
const std::unordered_map< std::uint32_t, DVMType * > & get_types_by_id() const
Get the types with the map of string id - type.
Definition types.hpp:341
void parse_types(stream::KunaiStream *stream, Strings *strings, std::uint32_t number_of_types, std::uint32_t types_offset)
Parse the types and store them in the class.
In case something unknown is found, we categorize it.
Definition types.hpp:277
~Unknown()=default
Destructor of unknown type.
Unknown(std::string raw)
Constructor of unknown type.
Definition types.hpp:282
type_e get_type() const override
Get Unkown type.
Definition types.hpp:291
std::string print_type() const override
Get Unkown type as a string.
Definition types.hpp:298
Class to manage an input file stream given for the analysis.
Definition kunaistream.hpp:20
utilities
Definition analysis.hpp:23