summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2021-09-06 13:11:51 -0400
committerPeter LaFosse <peter@vector35.com>2021-09-07 08:23:17 -0400
commit2af056bca94120b953629d6c57adc486ad8ffc10 (patch)
tree11de3ddbb71b86e2d11c35c71830e9212347c346 /python
parent7c84dcfbdcb57df0b99257307ab95b18d9bc3821 (diff)
Create NameSpace.get_core_struct()
Diffstat (limited to 'python')
-rw-r--r--python/binaryview.py41
-rw-r--r--python/types.py17
2 files changed, 22 insertions, 36 deletions
diff --git a/python/binaryview.py b/python/binaryview.py
index 4b78dc49..1c568789 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -3952,7 +3952,7 @@ class BinaryView:
core.BNFreeAddressList(refs)
return result
- def get_symbol_at(self, addr:int, namespace:Optional['_types.NameSpace']=None) -> Optional['_types.Symbol']:
+ def get_symbol_at(self, addr:int, namespace:'_types.NameSpaceType'=None) -> Optional['_types.Symbol']:
"""
``get_symbol_at`` returns the Symbol at the provided virtual address.
@@ -3966,18 +3966,13 @@ class BinaryView:
<FunctionSymbol: "_start" @ 0x100001174>
>>>
"""
- _namespace = None
- if isinstance(namespace, str):
- _namespace = _types.NameSpace(namespace)
- if isinstance(namespace, _types.NameSpace):
- _namespace = namespace._to_core_struct()
-
+ _namespace = _types.NameSpace.get_core_struct(namespace)
sym = core.BNGetSymbolByAddress(self.handle, addr, _namespace)
if sym is None:
return None
return _types.Symbol(None, None, None, handle = sym)
- def get_symbol_by_raw_name(self, name:str, namespace:Optional['_types.NameSpace']=None) -> Optional['_types.Symbol']:
+ def get_symbol_by_raw_name(self, name:str, namespace:'_types.NameSpaceType'=None) -> Optional['_types.Symbol']:
"""
``get_symbol_by_raw_name`` retrieves a Symbol object for the given a raw (mangled) name.
@@ -3991,17 +3986,13 @@ class BinaryView:
<FunctionSymbol: "public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)" @ 0x10001100>
>>>
"""
- _namespace = None
- if isinstance(namespace, str):
- _namespace = _types.NameSpace(namespace)
- if isinstance(namespace, _types.NameSpace):
- _namespace = namespace._to_core_struct()
+ _namespace = _types.NameSpace.get_core_struct(namespace)
sym = core.BNGetSymbolByRawName(self.handle, name, _namespace)
if sym is None:
return None
return _types.Symbol(None, None, None, handle = sym)
- def get_symbols_by_name(self, name:str, namespace:Optional['_types.NameSpace']=None, ordered_filter:List[SymbolType]=[]) -> List['_types.Symbol']:
+ def get_symbols_by_name(self, name:str, namespace:'_types.NameSpaceType'=None, ordered_filter:List[SymbolType]=[]) -> List['_types.Symbol']:
"""
``get_symbols_by_name`` retrieves a list of Symbol objects for the given symbol name and ordered filter
@@ -4025,11 +4016,7 @@ class BinaryView:
SymbolType.ImportedDataSymbol,
SymbolType.ImportAddressSymbol,
SymbolType.ExternalSymbol]
- _namespace = namespace
- if isinstance(namespace, str):
- _namespace = _types.NameSpace(namespace)
- if isinstance(namespace, _types.NameSpace):
- _namespace = namespace._to_core_struct()
+ _namespace = _types.NameSpace.get_core_struct(namespace)
count = ctypes.c_ulonglong(0)
syms = core.BNGetSymbolsByName(self.handle, name, count, _namespace)
assert syms is not None, "core.BNGetSymbolsByName returned None"
@@ -4042,7 +4029,7 @@ class BinaryView:
finally:
core.BNFreeSymbolList(syms, count.value)
- def get_symbols(self, start:Optional[int]=None, length:Optional[int]=None, namespace:Optional['_types.NameSpace']=None) -> List['_types.Symbol']:
+ def get_symbols(self, start:Optional[int]=None, length:Optional[int]=None, namespace:'_types.NameSpaceType'=None) -> List['_types.Symbol']:
"""
``get_symbols`` retrieves the list of all Symbol objects in the optionally provided range.
@@ -4057,11 +4044,7 @@ class BinaryView:
>>>
"""
count = ctypes.c_ulonglong(0)
- _namespace = namespace
- if isinstance(namespace, str):
- _namespace = _types.NameSpace(namespace)
- if isinstance(namespace, _types.NameSpace):
- _namespace = namespace._to_core_struct()
+ _namespace = _types.NameSpace.get_core_struct(namespace)
if start is None:
syms = core.BNGetSymbols(self.handle, count, _namespace)
assert syms is not None, "core.BNGetSymbols returned None"
@@ -4077,7 +4060,7 @@ class BinaryView:
core.BNFreeSymbolList(syms, count.value)
def get_symbols_of_type(self, sym_type:SymbolType, start:Optional[int]=None, length:Optional[int]=None,
- namespace:Optional[Union[str, '_types.NameSpace']]=None) -> List['_types.Symbol']:
+ namespace:'_types.NameSpaceType'=None) -> List['_types.Symbol']:
"""
``get_symbols_of_type`` retrieves a list of all Symbol objects of the provided symbol type in the optionally
provided range.
@@ -4095,11 +4078,7 @@ class BinaryView:
"""
if isinstance(sym_type, str):
sym_type = SymbolType[sym_type]
- _namespace = namespace
- if isinstance(namespace, str):
- _namespace = _types.NameSpace(namespace)
- if isinstance(namespace, _types.NameSpace):
- _namespace = namespace._to_core_struct()
+ _namespace = _types.NameSpace.get_core_struct(namespace)
count = ctypes.c_ulonglong(0)
if start is None:
diff --git a/python/types.py b/python/types.py
index 4595876b..d52f2b71 100644
--- a/python/types.py
+++ b/python/types.py
@@ -46,6 +46,7 @@ MembersType = Union[List['StructureMember'], List[Tuple['Type', str]]]
EnumMembersType = Union[List[Tuple[str,int]], List[str], List['EnumerationMember']]
SomeType = Union['TypeBuilder', 'Type']
TypeContainer = Union['binaryview.BinaryView', 'typelibrary.TypeLibrary']
+NameSpaceType = Optional[Union[str, List[str], 'NameSpace']]
# The following are needed to prevent the type checker from getting
# confused as we have member functions in `Type` named the same thing
_int = int
@@ -198,6 +199,15 @@ class NameSpace(QualifiedName):
result.append(name.name[i].decode("utf-8"))
return NameSpace(result)
+ @staticmethod
+ def get_core_struct(name:Optional[Union[str, List[str], 'NameSpace']]) -> Optional[core.BNNameSpace]:
+ if name is None:
+ return None
+ if isinstance(name, NameSpace):
+ return name._to_core_struct()
+ else:
+ return NameSpace(name)._to_core_struct()
+
class Symbol:
"""
@@ -228,11 +238,8 @@ class Symbol:
raw_name = full_name
if binding is None:
binding = SymbolBinding.NoBinding
- if isinstance(namespace, str):
- namespace = NameSpace(namespace)
- if isinstance(namespace, NameSpace):
- namespace = namespace._to_core_struct()
- _handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding, namespace, ordinal)
+ _namespace = NameSpace.get_core_struct(namespace)
+ _handle = core.BNCreateSymbol(sym_type, short_name, full_name, raw_name, addr, binding, _namespace, ordinal)
assert _handle is not None
self._handle = _handle