KUNAI Static Analyzer
Kunai is a library for doing static binary analysis of Dalvik.
Loading...
Searching...
No Matches
protos.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 protos.hpp
7// @brief Manage all the prototypes used in a DEX file, the prototypes
8// include parameters return types, etc.
9
10/*
11 * Each proto structure is:
12 * ProtoID {
13 * uint shorty_idx, # OFFSET to a string with return type
14 * uint return_type_idx, # OFFSET to a type which points to string with return type
15 * uint parameters_off # ---------| Offset to parameters in type list type
16 * } |
17 * |
18 * ------------------------------------
19 * |
20 * v
21 * type_list {
22 * uint size, # Size of the list in entries
23 * list type_item[size] # ---------|
24 * } |
25 * |
26 * -------------------------------------
27 * |
28 * v
29 * type_item {
30 * ushort type_idx # type_idx of each member
31 * }
32 *
33 * ProtoID[] protos
34 */
35
36#ifndef KUNAI_DEX_PARSER_PROTOS_HPP
37#define KUNAI_DEX_PARSER_PROTOS_HPP
38
39#include "Kunai/Utils/kunaistream.hpp"
40#include "Kunai/DEX/parser/types.hpp"
41#include "Kunai/DEX/parser/strings.hpp"
42
43#include <vector>
44
45namespace KUNAI
46{
47namespace DEX
48{
49
53 class ProtoID
54 {
56 std::string &shorty_idx;
58 DVMType *return_type;
60 std::vector<DVMType *> parameters;
65 void parse_parameters(
66 stream::KunaiStream *stream,
67 Types *types,
68 std::uint32_t parameters_off);
69
70 public:
78 stream::KunaiStream *stream,
79 Types *types,
80 std::string& shorty_idx,
81 std::uint32_t return_type_idx,
82 std::uint32_t parameters_off)
83 : shorty_idx(shorty_idx),
84 return_type(types->get_type_from_order(return_type_idx))
85 {
86 parse_parameters(stream, types, parameters_off);
87 }
88
91 const std::string& get_shorty_idx() const
92 {
93 return shorty_idx;
94 }
95
98 std::string& get_shorty_idx()
99 {
100 return shorty_idx;
101 }
102
106 {
107 return return_type;
108 }
109
113 {
114 return return_type;
115 }
116
119 const std::vector<DVMType *>& get_parameters() const
120 {
121 return parameters;
122 }
123
126 std::vector<DVMType*>& get_parameters()
127 {
128 return parameters;
129 }
130 };
131
132 using protoid_t = std::unique_ptr<ProtoID>;
133
136 class Protos
137 {
140 std::vector<protoid_t> proto_ids;
142 std::uint32_t number_of_protos;
143
144 public:
146 Protos() = default;
148 ~Protos() = default;
149
157 std::uint32_t number_of_protos,
158 std::uint32_t offset,
159 Strings* strings,
160 Types* types);
161
164 const std::vector<protoid_t>& get_proto_ids() const
165 {
166 return proto_ids;
167 }
168
171 std::vector<protoid_t>& get_proto_ids()
172 {
173 return proto_ids;
174 }
175
178 std::uint32_t get_number_of_protos() const
179 {
180 return number_of_protos;
181 }
182
186 ProtoID* get_proto_by_order(std::uint32_t pos);
187
192 friend std::ostream &operator<<(std::ostream &os, const Protos &entry);
193
196 void to_xml(std::ofstream& xml_file);
197 };
198} // namespace DEX
199} // namespace KUNAI
200
201#endif
Represents the base class of a Type in the DVM we have different types.
Definition types.hpp:26
Store the information of a ProtoID, this is a string with the return type, the list of parameters and...
Definition protos.hpp:54
const DVMType * get_return_type() const
Get a constant reference to the return type.
Definition protos.hpp:105
std::vector< DVMType * > & get_parameters()
Get a reference to the parameters.
Definition protos.hpp:126
const std::string & get_shorty_idx() const
Get constant reference to shorty_idx string.
Definition protos.hpp:91
const std::vector< DVMType * > & get_parameters() const
Get a constant reference to the parameters.
Definition protos.hpp:119
DVMType * get_return_type()
Get a reference to the return type.
Definition protos.hpp:112
std::string & get_shorty_idx()
Get a reference to shorty_idx string.
Definition protos.hpp:98
ProtoID(stream::KunaiStream *stream, Types *types, std::string &shorty_idx, std::uint32_t return_type_idx, std::uint32_t parameters_off)
Constructor of a ProtoID, the ProtoID parses its own parameters.
Definition protos.hpp:77
Class to manage all the ProtoID from the DEX file.
Definition protos.hpp:137
~Protos()=default
Default destructor of protos.
friend std::ostream & operator<<(std::ostream &os, const Protos &entry)
Return a pretty printed version of the proto_ids.
Protos()=default
Default constructor of protos.
ProtoID * get_proto_by_order(std::uint32_t pos)
Given a position in the vector of protos, return a ProtoID.
std::uint32_t get_number_of_protos() const
get the number of proto_ids
Definition protos.hpp:178
std::vector< protoid_t > & get_proto_ids()
Return a reference to proto_ids vector.
Definition protos.hpp:171
void parse_protos(stream::KunaiStream *stream, std::uint32_t number_of_protos, std::uint32_t offset, Strings *strings, Types *types)
Parse all the ProtoIDs from the file.
void to_xml(std::ofstream &xml_file)
Dump the proto_ids to an XML file.
const std::vector< protoid_t > & get_proto_ids() const
Return a constant reference to proto_ids vector.
Definition protos.hpp:164
Storage class for all the strings of the DEX file.
Definition strings.hpp:34
Definition types.hpp:305
Class to manage an input file stream given for the analysis.
Definition kunaistream.hpp:20
utilities
Definition analysis.hpp:23