diff options
| author | Peter LaFosse <peter@vector35.com> | 2022-03-04 14:53:33 -0500 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2022-03-04 15:43:58 -0500 |
| commit | 1107abf9d0dc949928e573aadf11dac24133390a (patch) | |
| tree | 889e37ae778b9473397572ed5848623165af6260 /python | |
| parent | 19c48bcbd20f3247bb273829df6a13580bf06e2c (diff) | |
Add a few optimizations to the python API for querying platform
Diffstat (limited to 'python')
| -rw-r--r-- | python/binaryview.py | 6 | ||||
| -rw-r--r-- | python/metadata.py | 2 | ||||
| -rw-r--r-- | python/platform.py | 26 |
3 files changed, 20 insertions, 14 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index e94aff30..7a62aa57 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -1706,6 +1706,7 @@ class BinaryView: self._notifications = {} self._parse_only = False self._preload_limit = 5 + self._platform = None def __enter__(self): return self @@ -2101,7 +2102,9 @@ class BinaryView: plat = core.BNGetDefaultPlatform(self.handle) if plat is None: return None - return _platform.Platform(self.arch, handle=plat) + if self._platform is None: + self._platform = _platform.Platform(self.arch, handle=plat) + return self._platform @platform.setter def platform(self, value: Optional['_platform.Platform']) -> None: @@ -2109,6 +2112,7 @@ class BinaryView: core.BNSetDefaultPlatform(self.handle, None) else: core.BNSetDefaultPlatform(self.handle, value.handle) + self._platform = None @property def endianness(self) -> Endianness: diff --git a/python/metadata.py b/python/metadata.py index a522e5ce..4cd9d55b 100644 --- a/python/metadata.py +++ b/python/metadata.py @@ -157,7 +157,7 @@ class Metadata: length.value = 0 native_list = core.BNMetadataGetRaw(self.handle, ctypes.byref(length)) assert native_list is not None, "core.BNMetadataGetRaw returned None" - return bytes(bytearray(native_list[i] for i in range(length.value))) + return ctypes.string_at(native_list, length.value) finally: core.BNFreeMetadataRaw(native_list) diff --git a/python/platform.py b/python/platform.py index 28a2a5f2..fc46882b 100644 --- a/python/platform.py +++ b/python/platform.py @@ -83,9 +83,9 @@ class Platform(metaclass=_PlatformMetaClass): _arch = architecture.CoreArchitecture._from_cache(core.BNGetPlatformArchitecture(_handle)) assert _handle is not None assert _arch is not None - self.handle = _handle + self.handle:ctypes.POINTER(BNPlatform) = _handle self._arch = _arch - self.name = core.BNGetPlatformName(self.handle) + self._name = None def __del__(self): if core is not None: @@ -97,12 +97,12 @@ class Platform(metaclass=_PlatformMetaClass): def __str__(self): return self.name - def __eq__(self, other): + def __eq__(self, other: 'Platform'): if not isinstance(other, self.__class__): return NotImplemented return ctypes.addressof(self.handle.contents) == ctypes.addressof(other.handle.contents) - def __ne__(self, other): + def __ne__(self, other: 'Platform'): if not isinstance(other, self.__class__): return NotImplemented return not (self == other) @@ -111,14 +111,20 @@ class Platform(metaclass=_PlatformMetaClass): return hash(ctypes.addressof(self.handle.contents)) @property + def name(self) -> str: + if self._name is None: + self._name = core.BNGetPlatformName(self.handle) + return self._name + + @property @classmethod - def os_list(cls): + def os_list(cls) -> List[str]: binaryninja._init_plugins() count = ctypes.c_ulonglong() platforms = core.BNGetPlatformOSList(count) assert platforms is not None, "core.BNGetPlatformOSList returned None" - result = [] - for i in range(0, count.value): + result:List[str] = [] + for i in range(count.value): result.append(str(platforms[i])) core.BNFreePlatformOSList(platforms, count.value) return result @@ -504,8 +510,4 @@ class Platform(metaclass=_PlatformMetaClass): @property def arch(self): - return self._arch - - @arch.setter - def arch(self, value): - self._arch = value + return self._arch
\ No newline at end of file |
