Created
September 4, 2020 09:01
-
-
Save MoePus/5d880ffdeb0be550dba742491be41995 to your computer and use it in GitHub Desktop.
IDA struct to cppstruct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def serialize_ordinal(n): | |
frame = '=%s#' | |
body = '' | |
def get_chunk(level): | |
if level < 2: | |
return 1 | |
return 64 * (128 ** (level-2)) | |
def get_cap(level): | |
if level < 2: | |
return 64 | |
return 128 | |
def get_level(n): | |
level = 1 | |
base = 0 | |
while True: | |
thunk = get_chunk(level) | |
cap = get_cap(level) | |
base = base + thunk * cap | |
if n < base: | |
return level | |
level += 1 | |
base -= thunk | |
level = get_level(n) | |
for i in range(level,1,-1): | |
thunk = get_chunk(i) | |
m,n = divmod(n, thunk) | |
body += chr(m + 0x80) | |
body += chr(n+ 0x40) | |
type_str = frame % chr(len(body) + 2) + body | |
return (type_str,'') | |
def struct2tinfo(sptr): | |
ti = idaapi.tinfo_t() | |
decl = serialize_ordinal(sptr.ordinal) | |
ti.deserialize(None,*decl) | |
return ti | |
def mod_struct_udt_type_data(sptr, callback): | |
ti = struct2tinfo(sptr) | |
udtt = ida_typeinf.udt_type_data_t() | |
ti.get_udt_details(udtt, idaapi.GTD_NO_LAYOUT) | |
udtt = callback(udtt) | |
name = ti._print() | |
ti.create_udt(udtt, idaapi.BTF_STRUCT) | |
ti.set_named_type(idati, name, idaapi.NTF_REPLACE) | |
def set_struct_inherited(sptr, count = 1): | |
def cbk(udtt): | |
udtt.taudt_bits |= idaapi.TAUDT_CPPOBJ | |
for i in range(min(len(udtt), count)): | |
ut = udtt[i] | |
ut.set_baseclass() | |
ut.name = '' | |
udtt[i] = ut | |
return udtt | |
mod_struct_udt_type_data(sptr, cbk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment