-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove_spaces_from_structs.py
96 lines (76 loc) · 2.66 KB
/
remove_spaces_from_structs.py
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
import struct
import ida_struct
import idaapi
def remove_spaces_from_name(sname):
sname = sname.replace(",const ", ',')
sname = sname.replace(" const,", ',')
sname = sname.replace(" const*,", ',')
sname = sname.replace("<const ", '<')
sname = sname.replace(" const>", '>')
sname = sname.replace(" const*>", '>')
# order matters
inttypes_map = (
("long unsigned int", "__uint64_t"),
("long int", "__int64_t"),
("short unsigned int", "__uint16_t"),
("unsigned int", "__uint32_t"),
("signed int", "__int32_t"),
("short int", "__int16_t"),
("unsigned char", "__uint8_t"),
("signed char", "__int8_t"),
("__int128 unsigned", "__uint128_t"),
)
wrappers = (
('<', '>'),
('<', ','),
(',', '>'),
(',', ','),
)
for inttype, new_inttype in inttypes_map:
# for prefix, suffix in wrappers:
# sname = sname.replace(prefix + inttype + suffix, prefix + new_inttype + suffix)
sname = sname.replace(inttype, new_inttype)
while "> >" in sname:
sname = sname.replace("> >", ">>")
sname = sname.replace(", ", ',')
return sname
def remove_spaces_from_structures():
for i in range(ida_struct.get_struc_qty()):
tid = ida_struct.get_struc_by_idx(i)
struct_name = ida_struct.get_struc_name(tid)
if ' ' not in struct_name:
continue
new_struct_name = remove_spaces_from_name(struct_name)
if new_struct_name == struct_name:
print("Failed to remove spaces from", struct_name)
continue
if idaapi.get_struc_id(new_struct_name) != idaapi.BADADDR:
print(new_struct_name, "already exists, cannot rename", struct_name)
continue
if ' ' in new_struct_name:
print("Not all spaces are removed from", struct_name, '\n', "Still trying to rename")
rv = ida_struct.set_struc_name(tid, new_struct_name)
if not rv:
print("Failed to rename", struct_name, '\n', "TO", '\n', new_struct_name, '\n')
continue
def remove_spaces_from_local_types():
til = idaapi.get_idati()
for i in range(idaapi.get_ordinal_qty(til)):
t = idaapi.get_numbered_type(til, i)
if t is None: continue
type_name = idaapi.get_numbered_type_name(til, i)
if ' ' not in type_name: continue
new_name = remove_spaces_from_name(type_name)
if new_name == type_name: continue
if idaapi.get_named_type(idaapi.get_idati(), new_name, idaapi.NTF_TYPE) is not None:
print(new_name, "already exists, cannot rename", type_name)
continue
if ' ' in new_name:
print("Not all spaces are removed from", i, type_name, "still trying to rename")
idaapi.set_numbered_type(til, i, idaapi.NTF_REPLACE, new_name, t[0])
return
remove_spaces_from_structures()
remove_spaces_from_local_types()
if __name__ == "__main__":
remove_spaces_from_structures()
remove_spaces_from_local_types()