diff options
Diffstat (limited to 'python/binaryview.py')
| -rw-r--r-- | python/binaryview.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index 53cc308a..4c57131e 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -705,10 +705,11 @@ class _BinaryViewTypeMetaclass(type): class BinaryViewType(metaclass=_BinaryViewTypeMetaclass): - def __init__(self, handle:core.BNBinaryViewType): + def __init__(self, handle:core.BNBinaryViewTypeHandle): # Used to force Python callback objects to not get garbage collected _platform_recognizers = {} - self.handle = core.handle_of_type(handle, core.BNBinaryViewType) + _handle = core.BNBinaryViewTypeHandle + self.handle = ctypes.cast(handle, _handle) def __repr__(self): return f"<view type: '{self.name}'>" @@ -1340,10 +1341,9 @@ class BinaryView: registered_view_type = None _associated_data = {} _registered_instances = [] - def __init__(self, file_metadata:'filemetadata.FileMetadata'=None, parent_view:'BinaryView'=None, handle:'ctypes.pointer[core.BNBinaryView]'=None): + def __init__(self, file_metadata:'filemetadata.FileMetadata'=None, parent_view:'BinaryView'=None, handle:core.BNBinaryViewHandle=None): if handle is not None: - self.handle = core.handle_of_type(handle, core.BNBinaryView) - assert self.handle is not None, "core.handle_of_type returned None" + _handle = handle if file_metadata is None: self._file = filemetadata.FileMetadata(handle=core.BNGetFileForView(handle)) else: @@ -1352,8 +1352,7 @@ class BinaryView: binaryninja._init_plugins() if file_metadata is None: file_metadata = filemetadata.FileMetadata() - self.handle = core.BNCreateBinaryDataView(file_metadata.handle) - assert self.handle is not None, "core.BNCreateBinaryDataView returned None" + _handle = core.BNCreateBinaryDataView(file_metadata.handle) self._file = filemetadata.FileMetadata(handle=core.BNNewFileReference(file_metadata.handle)) else: binaryninja._init_plugins() @@ -1388,9 +1387,10 @@ class BinaryView: _parent_view = None if parent_view is not None: _parent_view = parent_view.handle - self.handle = core.BNCreateCustomBinaryView(self.__class__.name, file_metadata.handle, _parent_view, self._cb) - assert self.handle is not None, "core.BNCreateCustomBinaryView returned None" - assert self.handle is not None + _handle = core.BNCreateCustomBinaryView(self.__class__.name, file_metadata.handle, _parent_view, self._cb) + + assert _handle is not None + self.handle = _handle self._notifications = {} self._parse_only = False @@ -1423,8 +1423,6 @@ class BinaryView: def __eq__(self, other): if not isinstance(other, self.__class__): return NotImplemented - assert self.handle is not None - assert other.handle is not None return ctypes.addressof(self.handle.contents) == ctypes.addressof(other.handle.contents) def __ne__(self, other): @@ -1433,7 +1431,6 @@ class BinaryView: return not (self == other) def __hash__(self): - assert self.handle is not None return hash(ctypes.addressof(self.handle.contents)) def __iter__(self) -> Generator['_function.Function', None, None]: @@ -2052,7 +2049,7 @@ class BinaryView: """Dictionary object where plugins can store arbitrary data associated with the view""" # TODO: # assert isinstance(self.handle, int), "session_data called with no BinaryView.handle" - handle = ctypes.cast(self.handle, ctypes.c_void_p) + handle = ctypes.cast(self.handle, ctypes.c_void_p) # type: ignore if handle.value not in BinaryView._associated_data: obj = _BinaryViewAssociatedDataStore() BinaryView._associated_data[handle.value] = obj @@ -2086,7 +2083,6 @@ class BinaryView: @property def relocation_ranges(self): """List of relocation range tuples (read-only)""" - count = ctypes.c_ulonglong() ranges = core.BNGetRelocationRanges(self.handle, count) assert ranges is not None, "core.BNGetRelocationRanges returned None" @@ -3215,7 +3211,7 @@ class BinaryView: result = [func for func in result if func.platform == plat] return result - def get_functions_by_name(self, name, plat=None, ordered_filter=[SymbolType.FunctionSymbol, SymbolType.ImportedFunctionSymbol, SymbolType.LibraryFunctionSymbol]): + def get_functions_by_name(self, name, plat=None, ordered_filter=None): """``get_functions_by_name`` returns a list of Function objects function with a Symbol of ``name``. @@ -3230,6 +3226,10 @@ class BinaryView: <func: x86_64@0x1587> >>> """ + if ordered_filter is not None: + ordered_filter = [SymbolType.FunctionSymbol, + SymbolType.ImportedFunctionSymbol, + SymbolType.LibraryFunctionSymbol] if plat == None: plat = self.platform syms = self.get_symbols_by_name(name, ordered_filter=ordered_filter) @@ -4019,7 +4019,7 @@ class BinaryView: return None return _types.Symbol(None, None, None, handle = sym) - def get_symbols_by_name(self, name, namespace=None, ordered_filter=[SymbolType.FunctionSymbol, SymbolType.ImportedFunctionSymbol, SymbolType.LibraryFunctionSymbol, SymbolType.DataSymbol, SymbolType.ImportedDataSymbol, SymbolType.ImportAddressSymbol, SymbolType.ExternalSymbol]): + def get_symbols_by_name(self, name, namespace=None, ordered_filter=None): """ ``get_symbols_by_name`` retrieves a list of Symbol objects for the given symbol name and ordered filter @@ -4035,6 +4035,14 @@ class BinaryView: [<FunctionSymbol: "public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)" @ 0x10001100>] >>> """ + if ordered_filter is None: + ordered_filter = [SymbolType.FunctionSymbol, + SymbolType.ImportedFunctionSymbol, + SymbolType.LibraryFunctionSymbol, + SymbolType.DataSymbol, + SymbolType.ImportedDataSymbol, + SymbolType.ImportAddressSymbol, + SymbolType.ExternalSymbol] if isinstance(namespace, str): namespace = _types.NameSpace(namespace) if isinstance(namespace, _types.NameSpace): @@ -5523,9 +5531,9 @@ class BinaryView: core.free_string(errors) raise SyntaxError(error_str) - type_dict = {} - variables = {} - functions = {} + type_dict:Mapping[_types.QualifiedName, _types.Type] = {} + variables:Mapping[_types.QualifiedName, _types.Type] = {} + functions:Mapping[_types.QualifiedName, _types.Type] = {} for i in range(0, parse.typeCount): name = _types.QualifiedName._from_core_struct(parse.types[i].name) type_dict[name] = _types.Type(core.BNNewTypeReference(parse.types[i].type), platform = self.platform) @@ -7267,7 +7275,7 @@ class StructuredDataValue: return ' '.join(["{:02x}".format(x) for x in struct.unpack(decode_str, self._value)]) def __repr__(self): - return "<StructuredDataValue type:{} value:{}>".format(str(self._type), str(self)) + return f"<StructuredDataValue type:{self._type} value:{self}>" def __int__(self): if self._type.width == 1: |
