-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSTLTypes-ForDistribution.py
More file actions
206 lines (173 loc) · 6.6 KB
/
STLTypes-ForDistribution.py
File metadata and controls
206 lines (173 loc) · 6.6 KB
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
# (C) Rolf Rolles, Mobius Strip Reverse Engineering, 9/21/2021.
import idaapi
from functools import reduce
stl_map_keyvalue_fmt = ("struct {2}_{3}_keyvalue_t"
"{{"
"{0} key;"
"{1} value;"
"}};")
stl_map_node_fmt = ("struct _Tree_node_{2}_{3};" # FORWARD DECLARATION
"struct _Tree_node_{2}_{3}"
"{{ "
" _Tree_node_{2}_{3} *_Left;"
" _Tree_node_{2}_{3} *_Parent;"
" _Tree_node_{2}_{3} *_Right;"
" bool _Color;"
" bool _IsNil;"
" {2}_{3}_keyvalue_t KeyValue;"
"}};")
stl_map_fmt = ("struct __cppobj map_{2}_{3}"
"{{ "
" _Tree_node_{2}_{3} *_Myhead;"
" unsigned __int64 _Mysize;"
"}};")
stl_map_pairib = ("struct __cppobj map_{2}_{3}_iterator_pairib"
"{{ "
" _Tree_node_{2}_{3} *_Myhead;"
" bool _Second;"
"}};")
stl_map_templates = [stl_map_keyvalue_fmt, stl_map_node_fmt, stl_map_fmt, stl_map_pairib]
stl_map_func_insert_at = "_Tree_node_{2}_{3} **__fastcall map_{2}_{3}_insert_at(map_{2}_{3} *map, _Tree_node_{2}_{3} **it_out, bool IsLeft, _Tree_node_{2}_{3} *it1_in, {2}_{3}_keyvalue_t *keyValue, _Tree_node_{2}_{3} *it2_in);"
stl_map_func_insert_nohint = "map_{2}_{3}_iterator_pairib *__fastcall map_{2}_{3}_insert_nohint(map_{2}_{3} *map, map_{2}_{3}_iterator_pairib *itp_out, bool IsLeft, {2}_{3}_keyvalue_t *keyValue, _Tree_node_{2}_{3} *it2_in);"
stl_map_func_insert_hint = "_Tree_node_{2}_{3} **__fastcall map_{2}_{3}_insert_hint(map_{2}_{3} *map, _Tree_node_{2}_{3} **it_out, _Tree_node_{2}_{3} *it1_in, {2}_{3}_keyvalue_t *keyValue, _Tree_node_{2}_{3} *it2_in);"
stl_map_funcsigs = [stl_map_func_insert_at, stl_map_func_insert_nohint, stl_map_func_insert_hint]
def ParseOneDecl(declWithSemi):
retVal = idaapi.parse_decls(None, declWithSemi, None, idaapi.convert_pt_flags_to_hti(idaapi.PT_TYP))
return retVal is not None
def MakeMapTypes(sKeyType, sValType, sKeyName=None, sValName=None):
if sKeyName is None:
sKeyName = sKeyType
if sValName is None:
sValName = sValType
stl_types_reified = map(lambda s:s.format(sKeyType,sValType,sKeyName,sValName),stl_map_templates)
stl_types_reduced = reduce(lambda x,y: x+y, stl_types_reified)
if not ParseOneDecl(stl_types_reduced):
print("Could not parse declaration: %s" % stl_types_reduced)
return False
stl_funcs_reified = map(lambda s:s.format(sKeyType,sValType,sKeyName,sValName),stl_map_funcsigs)
stl_funcs_reduced = reduce(lambda x,y: x+"\n"+y, stl_funcs_reified)
return True
stl_set_node_fmt = ("struct _Tree_node_{1};" # FORWARD DECLARATION
"struct _Tree_node_{1}"
"{{ "
" _Tree_node_{1} *_Left;"
" _Tree_node_{1} *_Parent;"
" _Tree_node_{1} *_Right;"
" bool _Color;"
" bool _IsNil;"
" {0} _Key;"
"}};")
stl_set_fmt = ("struct __cppobj set_{1}"
"{{ "
" _Tree_node_{1} *_Myhead;"
" unsigned __int64 _Mysize;"
"}};")
stl_set_pairib = ("struct __cppobj set_{1}_iterator_pairib"
"{{ "
" _Tree_node_{1} *_Myhead;"
" bool _Second;"
"}};")
stl_set_templates = [stl_set_node_fmt, stl_set_fmt, stl_set_pairib]
def MakeSetTypes(sKeyType, sKeyName=None):
if sKeyName is None:
sKeyName = sKeyType
stl_types_reified = map(lambda s:s.format(sKeyType,sKeyName),stl_set_templates)
stl_types_reduced = reduce(lambda x,y: x+y, stl_types_reified)
if not ParseOneDecl(stl_types_reduced):
print("Could not parse declaration: %s" % stl_types_reduced)
return False
return True
stl_deque_cont = ("struct deque_{1} {{"
" void *_Myproxy;"
" {0} **_Map;"
" size_t _Mapsize;"
" _QWORD _Myoff;"
" _QWORD _Mysize;"
"}};")
def MakeDequeType(sEltType, sEltName=None):
if sEltName is None:
sEltName = sEltType
deque_type_reified = stl_deque_cont.format(sEltType, sEltName)
if not ParseOneDecl(deque_type_reified):
print("Could not parse declaration: %s" % deque_type_reified)
return False
return True
stl_vector_cont = ("struct vector_{1} {{"
" {0} *_Myfirst;"
" {0} *_Mylast;"
" {0} *_Myend;"
"}};")
def MakeVectorType(sEltType, sEltName=None):
if sEltName is None:
sEltName = sEltType
vector_type_reified = stl_vector_cont.format(sEltType, sEltName)
if not ParseOneDecl(vector_type_reified):
print("Could not parse declaration: %s" % vector_type_reified)
return False
return True
stl_list_node = ("struct _List_node_{1};" # FORWARD DECLARATION
"struct _List_node_{1}"
"{{ "
" _List_node_{1} *_Next;"
" _List_node_{1} *_Prev;"
" {0} _Myval;"
"}};")
stl_list_cont = ("struct list_{1}"
"{{ "
" _List_node_{1} *_Myhead;"
" size_t _Mysize;"
"}};")
stl_list_templates = [stl_list_node, stl_list_cont]
def MakeListTypes(sEltType, sEltName=None):
if sEltName is None:
sEltName = sEltType
stl_types_reified = map(lambda s:s.format(sEltType,sEltName),stl_list_templates)
stl_types_reduced = reduce(lambda x,y: x+y, stl_types_reified)
if not ParseOneDecl(stl_types_reduced):
print("Could not parse declaration: %s" % stl_types_reduced)
return False
return True
stl_ref_count_base_vtbl_fmt = ("struct _Ref_count_base_{1};"
"struct _Ref_count_base_{1}_vtbl {{"
" void (__fastcall *_Destroy)(_Ref_count_base_{1} *this);"
" void (__fastcall *_Delete_this)(_Ref_count_base_{1} *this);"
" void (__fastcall *Destructor)(_Ref_count_base_{1} *this);"
" void *(__fastcall *_Get_deleter)(_Ref_count_base_{1} *this);"
"}};")
stl_ref_count_base_fmt = ("struct _Ref_count_base_{1}_vtbl;"
"struct _Ref_count_base_{1} {{"
" _Ref_count_base_{1}_vtbl *__vftable;"
" volatile int _Uses;"
" volatile int _Weaks;"
"}};")
stl_ref_count_fmt = ("struct _Ref_count_base_{1};"
"struct _Ref_count_{1} : _Ref_count_base_{1} {{"
" {0} *_Ptr;"
"}};")
stl_ref_count_obj_fmt = ("struct _Ref_count_base_{1};"
"struct _Ref_count_obj_{1} : _Ref_count_base_{1} {{"
" {0} _Storage;"
"}};")
# Changed _Ref_count to _Ref_count_base
stl_shared_ptr_fmt = ("struct _Ref_count_base_{1};"
"struct shared_ptr_{1} {{"
" {0} *_Ptr;"
" _Ref_count_base_{1} *_Rep;"
"}};")
# FOR 64-BIT TYPES BECAUSE OF __shifted(X,16)
# OTHERWISE WOULD BE __shifted(X,12)
stl_shared_ptr_obj_fmt = ("struct _Ref_count_obj_{1};"
"struct shared_ptr_obj_{1} {{"
" {0} *__shifted(_Ref_count_obj_{1},16) _Ptr;"
" _Ref_count_obj_{1} *_Rep;"
"}};")
stl_shared_ptr_templates = [stl_ref_count_base_vtbl_fmt, stl_ref_count_base_fmt, stl_ref_count_fmt, stl_ref_count_obj_fmt, stl_shared_ptr_fmt, stl_shared_ptr_obj_fmt]
def MakeSharedPtrTypes(sEltType, sEltName=None):
if sEltName is None:
sEltName = sEltType
stl_types_reified = map(lambda s:s.format(sEltType,sEltName),stl_shared_ptr_templates)
stl_types_reduced = reduce(lambda x,y: x+y, stl_types_reified)
if not ParseOneDecl(stl_types_reduced):
print("Could not parse declaration:")
return False
return True