summaryrefslogtreecommitdiff
path: root/python/function.py
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2019-03-08 13:59:10 -0500
committerPeter LaFosse <peter@vector35.com>2019-03-08 13:59:23 -0500
commit73c6fc68b205fcb8937bb7c7dd4b9df2652eb1ac (patch)
tree57fada9edf92f1cd6a9877e4d609502d32da55ac /python/function.py
parent5ca01c0091667160be5cd38d091177bd4dfe0914 (diff)
Add ability to index into functions and basicblocks
Diffstat (limited to 'python/function.py')
-rw-r--r--python/function.py57
1 files changed, 35 insertions, 22 deletions
diff --git a/python/function.py b/python/function.py
index b7c9229c..14e140c1 100644
--- a/python/function.py
+++ b/python/function.py
@@ -389,6 +389,41 @@ class Function(object):
def __hash__(self):
return hash((self.start, self.arch.name, self.platform.name))
+ def __getitem__(self, i):
+ count = ctypes.c_ulonglong()
+ blocks = core.BNGetFunctionBasicBlockList(self.handle, count)
+ try:
+ if i < 0:
+ i = count.value + i
+ if i < 0 or i >= count.value:
+ raise IndexError("index out of range")
+ block = binaryninja.basicblock.BasicBlock(core.BNNewBasicBlockReference(blocks[i]), self._view)
+ return block
+ finally:
+ core.BNFreeBasicBlockList(blocks, count.value)
+
+ def __iter__(self):
+ count = ctypes.c_ulonglong()
+ blocks = core.BNGetFunctionBasicBlockList(self.handle, count)
+ try:
+ for i in range(0, count.value):
+ yield binaryninja.basicblock.BasicBlock(core.BNNewBasicBlockReference(blocks[i]), self._view)
+ finally:
+ core.BNFreeBasicBlockList(blocks, count.value)
+
+ def __setattr__(self, name, value):
+ try:
+ object.__setattr__(self, name, value)
+ except AttributeError:
+ raise AttributeError("attribute '%s' is read only" % name)
+
+ def __repr__(self):
+ arch = self.arch
+ if arch:
+ return "<func: %s@%#x>" % (arch.name, self.start)
+ else:
+ return "<func: %#x>" % self.start
+
@classmethod
def _unregister(cls, func):
handle = ctypes.cast(func, ctypes.c_void_p)
@@ -870,28 +905,6 @@ class Function(object):
return None
return binaryninja.flowgraph.CoreFlowGraph(graph)
- def __iter__(self):
- count = ctypes.c_ulonglong()
- blocks = core.BNGetFunctionBasicBlockList(self.handle, count)
- try:
- for i in range(0, count.value):
- yield binaryninja.basicblock.BasicBlock(core.BNNewBasicBlockReference(blocks[i]), self._view)
- finally:
- core.BNFreeBasicBlockList(blocks, count.value)
-
- def __setattr__(self, name, value):
- try:
- object.__setattr__(self, name, value)
- except AttributeError:
- raise AttributeError("attribute '%s' is read only" % name)
-
- def __repr__(self):
- arch = self.arch
- if arch:
- return "<func: %s@%#x>" % (arch.name, self.start)
- else:
- return "<func: %#x>" % self.start
-
def mark_recent_use(self):
core.BNMarkFunctionAsRecentlyUsed(self.handle)