<s> import idaapi <EOL> import idc <EOL> import string <EOL> from . import exceptions <EOL> def get_func ( func_ea ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( func_ea , idaapi . func_t ) : <EOL> return func_ea <EOL> func = idaapi . get_func ( func_ea ) <EOL> if func is None : <EOL> raise exceptions . SarkNoFunction ( "<STR_LIT>" . format ( func_ea ) ) <EOL> return func <EOL> def get_ea ( func_ea ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( func_ea , idaapi . func_t ) : <EOL> return func_ea . startEA <EOL> return func_ea <EOL> def is_string_printable ( string_ ) : <EOL> """<STR_LIT>""" <EOL> return set ( string_ ) - set ( string . printable ) <EOL> def string_to_query ( string_ ) : <EOL> if is_string_printable ( string_ ) : <EOL> return '<STR_LIT>' . format ( string_ ) <EOL> return "<STR_LIT:U+0020>" . join ( char . encode ( "<STR_LIT>" ) for char in string_ ) <EOL> def iter_find_string ( query , start = None , end = None , down = True ) : <EOL> query = string_to_query ( query ) <EOL> return iter_find_query ( query , start , end , down ) <EOL> def iter_find_query ( query , start = None , end = None , down = True ) : <EOL> start , end = fix_addresses ( start , end ) <EOL> if down : <EOL> direction = idc . SEARCH_DOWN <EOL> else : <EOL> direction = idc . SEARCH_UP <EOL> current = idc . FindBinary ( start , direction , query ) <EOL> while current < end : <EOL> yield current <EOL> current = idc . FindBinary ( current + <NUM_LIT:1> , direction , query ) <EOL> def fix_addresses ( start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> if start in ( None , idaapi . BADADDR ) : <EOL> start = idaapi . cvar . inf . minEA <EOL> if end in ( None , idaapi . BADADDR ) : <EOL> end = idaapi . cvar . inf . maxEA <EOL> return start , end <EOL> def set_name ( address , name , anyway = False ) : <EOL> """<STR_LIT>""" <EOL> success = idaapi . set_name ( address , name , idaapi . SN_NOWARN | idaapi . SN_NOCHECK ) <EOL> if success : <EOL> return <EOL> if anyway : <EOL> success = idaapi . do_name_anyway ( address , name ) <EOL> if success : <EOL> return <EOL> raise exceptions . SarkSetNameFailed ( "<STR_LIT>" . format ( address , name ) ) <EOL> raise exceptions . SarkErrorNameAlreadyExists ( <EOL> "<STR_LIT>" . format ( address , name ) ) <EOL> def is_same_function ( ea1 , ea2 ) : <EOL> """<STR_LIT>""" <EOL> func1 = idaapi . get_func ( ea1 ) <EOL> func2 = idaapi . get_func ( ea2 ) <EOL> if any ( func is None for func in ( func1 , func2 ) ) : <EOL> return False <EOL> return func1 . startEA == func2 . startEA <EOL> def get_name_or_address ( ea ) : <EOL> name = idc . Name ( ea ) <EOL> if name : <EOL> name = repr ( name ) <EOL> else : <EOL> name = "<STR_LIT>" . format ( ea ) <EOL> return name <EOL> def get_native_size ( ) : <EOL> """<STR_LIT>""" <EOL> info = idaapi . get_inf_structure ( ) <EOL> if info . is_64bit ( ) : <EOL> return <NUM_LIT:8> <EOL> elif info . is_32bit ( ) : <EOL> return <NUM_LIT:4> <EOL> else : <EOL> return <NUM_LIT:2> <EOL> def get_fileregion_offset ( ea ) : <EOL> file_offset = idaapi . get_fileregion_offset ( ea ) <EOL> if file_offset == - <NUM_LIT:1> : <EOL> raise exceptions . NoFileOffset ( "<STR_LIT>" . format ( ea ) ) <EOL> return file_offset <EOL> def is_function ( ea ) : <EOL> try : <EOL> get_func ( ea ) <EOL> return True <EOL> except exceptions . SarkNoFunction : <EOL> return False <EOL> def is_signed ( number , size = None ) : <EOL> if not size : <EOL> size = get_native_size ( ) <EOL> return number & ( <NUM_LIT:1> << ( ( <NUM_LIT:8> * size ) - <NUM_LIT:1> ) ) </s>
