This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.hpp
278 lines (219 loc) · 7.1 KB
/
main.hpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef MAIN_HPP
#define MAIN_HPP 1
#pragma once
#pragma warning ( push, 0 )
#include <boost/cstdint.hpp>
#pragma warning ( pop )
// -----------------------------------------------------------------------------
class CTerminator
{
};
class CQueryBuilder
{
public:
explicit CQueryBuilder( std::stringstream & basequery, size_t querySize = 0 );
void execute();
private:
std::string m_BaseQuery;
std::vector< std::string > m_Queries;
std::stringstream m_CurrentQuery;
size_t m_QuerySize;
friend CQueryBuilder & operator << ( CQueryBuilder &, const std::string & );
friend CQueryBuilder & operator << ( CQueryBuilder &, ulong );
friend CQueryBuilder & operator << ( CQueryBuilder &, const CTerminator & );
};
// -----------------------------------------------------------------------------
class CExpressionTreeNode
{
public:
enum
{
TYPE_MNEMONIC = 0,
TYPE_SYMBOL = 1,
TYPE_IMM_INT = 2,
TYPE_IMM_FLOAT = 3,
TYPE_OPERATOR = 4,
TYPE_REGISTER = 5,
TYPE_SIZEPREFIX = 6,
TYPE_DEREFERENCE= 7
};
CExpressionTreeNode();
CExpressionTreeNode( unsigned id, int type, const std::string & symbol, boost::int64_t immediate, int position, unsigned parent );
typedef std::map< std::string, int > TTypeTable;
typedef std::map< std::string, std::string > TTypeTranslation;
static TTypeTable initTypeTable();
static TTypeTranslation initTypeTranslation();
unsigned m_Id;
int m_Type;
std::string m_Symbol;
boost::int64_t m_Immediate;
int m_Position;
unsigned m_Parent;
static TTypeTable ms_TypeTable;
static TTypeTranslation ms_TypeTranslation;
};
// -----------------------------------------------------------------------------
class CBasicBlock
{
public:
CBasicBlock();
explicit CBasicBlock( ulong startAddress );
void nextId();
ulong m_Address;
ulong m_Id;
static ulong ms_NextId;
};
// -----------------------------------------------------------------------------
class CLine
{
public:
CLine( ulong address, const std::string & mnemonic, const std::string & binaryDump, bool isFlow )
: m_Address( address )
, m_Mnemonic( mnemonic )
, m_BinaryDump( binaryDump )
, m_IsFlow( isFlow )
{
}
ulong m_Address;
std::string m_Mnemonic;
std::string m_BinaryDump;
bool m_IsFlow;
};
// -----------------------------------------------------------------------------
class CEdge
{
public:
typedef enum TEdgeType
{
TYPE_TRUE = 0,
TYPE_FALSE = 1,
TYPE_UNCONDITIONAL = 2,
TYPE_SWITCH = 3,
CALL_DIRECT = 4,
CALL_INDIRECT = 5,
CALL_INDIRECT_VIRTUAL = 6,
DATA = 7,
DATA_STRING = 8
};
CEdge( ulong source, ulong operandId, ulong expressionId, ulong dest, unsigned type )
: m_Source( source )
, m_SourceOperand( operandId )
, m_SourceExpression( expressionId )
, m_Dest( dest )
, m_Type( type )
{
}
ulong m_Source;
ulong m_SourceOperand;
ulong m_SourceExpression;
ulong m_Dest;
unsigned m_Type;
};
// -----------------------------------------------------------------------------
class CExporter;
class CFunction
{
public:
enum
{
TYPE_STANDARD = 0,
TYPE_LIBRARY = 1,
TYPE_IMPORTED = 2,
TYPE_THUNK = 3
};
CFunction( ulong startAddress, ulong endAddress,
const std::string & name, const std::string & module, int moduleId, CExporter * exporter,
CQueryBuilder & instructionQuery,
CQueryBuilder & basicBlockQuery,
CQueryBuilder & flowgraphQuery,
unsigned type = TYPE_STANDARD );
bool addressInFunction( ulong address ) const;
std::string getName() const;
std::string getModule() const;
bool hasRealName() const;
ulong getAddress() const;
ulong getEndAddress() const;
unsigned getType() const;
void addBasicBlock( const CBasicBlock & basicBlock );
void addEdge( ulong source, ulong sourceOperand, ulong sourceExpression, ulong dest, CEdge::TEdgeType type );
void write();
ulong decodeLine( const t_disasm & assemblyLine, CQueryBuilder & tupleQuery, CQueryBuilder & stringQuery, ulong & operandId );
void resolveAddressesToIds();
unsigned getNrOfBasicBlocks() const;
void writeStub();
const CBasicBlock * getBasicBlockForAddress( ulong address ) const;
private:
typedef std::map< ulong, CBasicBlock > TBasicBlocks;
typedef std::vector< CEdge > TEdges;
typedef std::vector< CLine > TLines;
std::string m_Name;
std::string m_Module;
ulong m_StartAddress;
ulong m_EndAddress;
unsigned m_Type;
TBasicBlocks m_BasicBlocks;
TEdges m_Edges;
int m_ModuleId;
TLines m_Lines;
CExporter * m_Exporter;
CQueryBuilder & m_InstructionQuery;
CQueryBuilder & m_BasicBlockQuery;
CQueryBuilder & m_FlowGraphQuery;
void resolveEdges();
CFunction & operator = ( const CFunction & );
};
// -----------------------------------------------------------------------------
class CExporter
{
public:
CExporter();
~CExporter();
void showAbout() const;
void setWindowHandle( HWND window );
HWND getWindowHandle() const;
void setInstanceHandle( HINSTANCE instance );
void execute();
void reset();
ulong decodeOperand( const std::string & operand, CExpressionTreeNode * parent, ulong operandId, const t_disasm & assemblyLine, ulong position = 0 );
void setDatabaseName( const std::string & name );
private:
typedef std::map< ulong, CFunction * > TFunctions;
typedef std::vector< CEdge > TEdges;
typedef std::map< std::string, CExpressionTreeNode > TExpressionTreeNodes;
typedef std::map< ulong, std::string > TAddressComments;
HINSTANCE m_DllInstance;
HWND m_WindowHandle;
TFunctions m_Functions;
TEdges m_CallGraphEdges;
TEdges m_AllEdges;
TExpressionTreeNodes m_ExpressionTreeNodes;
int m_ModuleId;
unsigned m_ExpresssionTreeNodeId;
CQueryBuilder * m_OperandTupleQuery;
CQueryBuilder * m_OperandStringQuery;
CQueryBuilder * m_OperandExpressionQuery;
CQueryBuilder * m_InstructionQuery;
CQueryBuilder * m_BasicBlockQuery;
CQueryBuilder * m_FlowGraphQuery;
CQueryBuilder * m_ExpressionSubstitutionQuery;
ulong m_OperandId;
TAddressComments m_AddressComments;
std::string m_DatabaseName;
ulong getFunctionForAddress( ulong address ) const;
void decodeLine( const t_disasm & assemblyLine, CFunction & function, CBasicBlock & basicBlock, ulong nextAddress );
void decodeFunction( ulong startAddress, ulong endAddress, CFunction & function );
void executeSqlFromFile( std::istream & source, const std::string & replacement ) const;
bool prepareDatabase();
void writeFunctions();
void writeCallGraph();
void writeFlowGraphs();
void writeExpressionTree();
void writeOperands();
void writeModule();
void writeAddressReferences();
void writeAddressComments();
CExpressionTreeNode * insertSizePrefixIfNeeded( CExpressionTreeNode * parent, ulong operandId, ulong & position );
CExpressionTreeNode * getId( CExpressionTreeNode & node );
};
// -----------------------------------------------------------------------------
#endif