summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRusty Wagner <rusty@vector35.com>2018-07-26 16:02:26 -0400
committerRusty Wagner <rusty@vector35.com>2018-07-26 16:18:02 -0400
commit6eb3234d924d870641ee30c4263437f1d8a8d5c7 (patch)
treeb64815c5e0a2c3b1a10a3e3dcab4c786fdd85c34 /python
parentc5c93fc82b8929d04f62d241ca50228de60fa5f4 (diff)
parent1f986c2698ff9df6d42429b1b7699842223634e5 (diff)
Merge branch 'dev' into test_stack_adjust
Diffstat (limited to 'python')
-rw-r--r--python/__init__.py148
-rw-r--r--python/architecture.py195
-rw-r--r--python/basicblock.py46
-rw-r--r--python/binaryview.py258
-rw-r--r--python/callingconvention.py68
-rw-r--r--python/databuffer.py30
-rw-r--r--python/demangle.py16
-rw-r--r--python/downloadprovider.py271
-rw-r--r--python/examples/bin_info.py10
-rw-r--r--python/examples/breakpoint.py2
-rwxr-xr-xpython/examples/export_svg.py6
-rw-r--r--python/examples/jump_table.py8
-rw-r--r--python/examples/nds.py5
-rw-r--r--python/examples/nes.py6
-rw-r--r--python/examples/notification_callbacks.py85
-rw-r--r--python/examples/version_switcher.py66
-rw-r--r--python/fileaccessor.py4
-rw-r--r--python/filemetadata.py26
-rw-r--r--python/flowgraph.py47
-rw-r--r--python/function.py142
-rw-r--r--python/functionrecognizer.py12
-rw-r--r--python/generator.cpp154
-rw-r--r--python/highlight.py4
-rw-r--r--python/interaction.py47
-rw-r--r--python/log.py17
-rw-r--r--python/lowlevelil.py113
-rw-r--r--python/mainthread.py4
-rw-r--r--python/mediumlevelil.py68
-rw-r--r--python/metadata.py24
-rw-r--r--python/platform.py83
-rw-r--r--python/plugin.py160
-rw-r--r--python/pluginmanager.py19
-rw-r--r--python/scriptingprovider.py59
-rw-r--r--python/setting.py22
-rw-r--r--python/startup.py22
-rw-r--r--python/transform.py50
-rw-r--r--python/types.py55
-rw-r--r--python/undoaction.py8
-rw-r--r--python/update.py30
39 files changed, 1549 insertions, 841 deletions
diff --git a/python/__init__.py b/python/__init__.py
index 05ba4c2b..aa7c1ed4 100644
--- a/python/__init__.py
+++ b/python/__init__.py
@@ -19,41 +19,115 @@
# IN THE SOFTWARE.
+from __future__ import absolute_import
import atexit
import sys
+import ctypes
from time import gmtime
+
+# 2-3 compatibility
+try:
+ import builtins # __builtins__ for python2
+except ImportError:
+ pass
+def range(*args):
+ """ A Python2 and Python3 Compatible Range Generator """
+ try:
+ return xrange(*args)
+ except NameError:
+ return builtins.range(*args)
+
+
+def with_metaclass(meta, *bases):
+ """Create a base class with a metaclass."""
+ class metaclass(type):
+ def __new__(cls, name, this_bases, d):
+ return meta(name, bases, d)
+
+ @classmethod
+ def __prepare__(cls, name, this_bases):
+ return meta.__prepare__(name, bases)
+ return type.__new__(metaclass, 'temporary_class', (), {})
+
+
+def cstr(arg):
+ if isinstance(arg, bytes) or arg is None:
+ return arg
+ else:
+ return arg.encode('charmap')
+
+
+def pyNativeStr(arg):
+ if isinstance(arg, str):
+ return arg
+ else:
+ return arg.decode('charmap')
+
+
# Binary Ninja components
-import _binaryninjacore as core
-from .enums import *
-from .databuffer import *
-from .filemetadata import *
-from .fileaccessor import *
-from .binaryview import *
-from .transform import *
-from .architecture import *
-from .basicblock import *
-from .function import *
-from .log import *
-from .lowlevelil import *
-from .mediumlevelil import *
-from .types import *
-from .functionrecognizer import *
-from .update import *
-from .plugin import *
-from .callingconvention import *
-from .platform import *
-from .demangle import *
-from .mainthread import *
-from .interaction import *
-from .lineardisassembly import *
-from .undoaction import *
-from .highlight import *
-from .scriptingprovider import *
-from .pluginmanager import *
-from .setting import *
-from .metadata import *
-from .flowgraph import *
+import binaryninja._binaryninjacore as core
+# __all__ = [
+# "enums",
+# "databuffer",
+# "filemetadata",
+# "fileaccessor",
+# "binaryview",
+# "transform",
+# "architecture",
+# "basicblock",
+# "function",
+# "log",
+# "lowlevelil",
+# "mediumlevelil",
+# "types",
+# "functionrecognizer",
+# "update",
+# "plugin",
+# "callingconvention",
+# "platform",
+# "demangle",
+# "mainthread",
+# "interaction",
+# "lineardisassembly",
+# "undoaction",
+# "highlight",
+# "scriptingprovider",
+# "pluginmanager",
+# "setting",
+# "metadata",
+# "flowgraph",
+# ]
+from binaryninja.enums import *
+from binaryninja.databuffer import *
+from binaryninja.filemetadata import *
+from binaryninja.fileaccessor import *
+from binaryninja.binaryview import *
+from binaryninja.transform import *
+from binaryninja.architecture import *
+from binaryninja.basicblock import *
+from binaryninja.function import *
+from binaryninja.log import *
+from binaryninja.lowlevelil import *
+from binaryninja.mediumlevelil import *
+from binaryninja.types import *
+from binaryninja.functionrecognizer import *
+from binaryninja.update import *
+from binaryninja.plugin import *
+from binaryninja.callingconvention import *
+from binaryninja.platform import *
+from binaryninja.demangle import *
+from binaryninja.mainthread import *
+from binaryninja.interaction import *
+from binaryninja.lineardisassembly import *
+from binaryninja.undoaction import *
+from binaryninja.highlight import *
+from binaryninja.scriptingprovider import *
+from binaryninja.downloadprovider import *
+from binaryninja.pluginmanager import *
+from binaryninja.setting import *
+from binaryninja.metadata import *
+from binaryninja.flowgraph import *
def shutdown():
@@ -138,6 +212,20 @@ class _DestructionCallbackHandler(object):
Function._unregister(func)
+_plugin_init = False
+
+
+def _init_plugins():
+ global _plugin_init
+ if not _plugin_init:
+ _plugin_init = True
+ core.BNInitCorePlugins()
+ core.BNInitUserPlugins()
+ core.BNInitRepoPlugins()
+ if not core.BNIsLicenseValidated():
+ raise RuntimeError("License is not valid. Please supply a valid license.")
+
+
_destruct_callbacks = _DestructionCallbackHandler()
bundled_plugin_path = core.BNGetBundledPluginDirectory()
diff --git a/python/architecture.py b/python/architecture.py
index c5f87ec6..aee7c301 100644
--- a/python/architecture.py
+++ b/python/architecture.py
@@ -18,55 +18,60 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+from __future__ import absolute_import
import traceback
import ctypes
import abc
# Binary Ninja components
-import _binaryninjacore as core
-from enums import (Endianness, ImplicitRegisterExtend, BranchType,
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import (Endianness, ImplicitRegisterExtend, BranchType,
InstructionTextTokenType, LowLevelILFlagCondition, FlagRole)
-import startup
-import function
-import lowlevelil
-import callingconvention
-import platform
-import log
-import databuffer
-import types
+import binaryninja
+from binaryninja import log
+from binaryninja import lowlevelil
+from binaryninja import types
+from binaryninja import databuffer
+from binaryninja import platform
+from binaryninja import callingconvention
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class _ArchitectureMetaClass(type):
+
@property
def list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
archs = core.BNGetArchitectureList(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(CoreArchitecture._from_cache(archs[i]))
core.BNFreeArchitectureList(archs)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
archs = core.BNGetArchitectureList(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield CoreArchitecture._from_cache(archs[i])
finally:
core.BNFreeArchitectureList(archs)
def __getitem__(cls, name):
- startup._init_plugins()
+ binaryninja._init_plugins()
arch = core.BNGetArchitectureByName(name)
if arch is None:
raise KeyError("'%s' is not a valid architecture" % str(name))
return CoreArchitecture._from_cache(arch)
def register(cls):
- startup._init_plugins()
+ binaryninja._init_plugins()
if cls.name is None:
raise ValueError("architecture 'name' is not defined")
arch = cls()
@@ -80,12 +85,12 @@ class _ArchitectureMetaClass(type):
raise AttributeError("attribute '%s' is read only" % name)
-class Architecture(object):
+class Architecture(with_metaclass(_ArchitectureMetaClass, object)):
"""
``class Architecture`` is the parent class for all CPU architectures. Subclasses of Architecture implement assembly,
disassembly, IL lifting, and patching.
- ``class Architecture`` has a ``__metaclass__`` with the additional methods ``register``, and supports
+ ``class Architecture`` has a metaclass with the additional methods ``register``, and supports
iteration::
>>> #List the architectures
@@ -130,11 +135,10 @@ class Architecture(object):
semantic_class_for_flag_write_type = {}
reg_stacks = {}
intrinsics = {}
- __metaclass__ = _ArchitectureMetaClass
next_address = 0
def __init__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
if self.__class__.opcode_display_length > self.__class__.max_instr_length:
self.__class__.opcode_display_length = self.__class__.max_instr_length
@@ -365,11 +369,11 @@ class Architecture(object):
for intrinsic in self.__class__.intrinsics.keys():
if intrinsic not in self._intrinsics:
info = self.__class__.intrinsics[intrinsic]
- for i in xrange(0, len(info.inputs)):
+ for i in range(0, len(info.inputs)):
if isinstance(info.inputs[i], types.Type):
- info.inputs[i] = function.IntrinsicInput(info.inputs[i])
+ info.inputs[i] = binaryninja.function.IntrinsicInput(info.inputs[i])
elif isinstance(info.inputs[i], tuple):
- info.inputs[i] = function.IntrinsicInput(info.inputs[i][0], info.inputs[i][1])
+ info.inputs[i] = binaryninja.function.IntrinsicInput(info.inputs[i][0], info.inputs[i][1])
info.index = intrinsic_index
self._intrinsics[intrinsic] = intrinsic_index
self._intrinsics_by_index[intrinsic_index] = (intrinsic, info)
@@ -392,12 +396,17 @@ class Architecture(object):
return ctypes.addressof(self.handle.contents) != ctypes.addressof(value.handle.contents)
@property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
+ @property
def full_width_regs(self):
"""List of full width register strings (read-only)"""
count = ctypes.c_ulonglong()
regs = core.BNGetFullWidthArchitectureRegisters(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(core.BNGetArchitectureRegisterName(self.handle, regs[i]))
core.BNFreeRegisterList(regs)
return result
@@ -408,7 +417,7 @@ class Architecture(object):
count = ctypes.c_ulonglong()
cc = core.BNGetArchitectureCallingConventions(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
obj = callingconvention.CallingConvention(handle=core.BNNewCallingConventionReference(cc[i]))
result[obj.name] = obj
core.BNFreeCallingConventionList(cc, count)
@@ -499,7 +508,7 @@ class Architecture(object):
result[0].archTransitionByTargetAddr = info.arch_transition_by_target_addr
result[0].branchDelay = info.branch_delay
result[0].branchCount = len(info.branches)
- for i in xrange(0, len(info.branches)):
+ for i in range(0, len(info.branches)):
if isinstance(info.branches[i].type, str):
result[0].branchType[i] = BranchType[info.branches[i].type]
else:
@@ -525,7 +534,7 @@ class Architecture(object):
length[0] = info[1]
count[0] = len(tokens)
token_buf = (core.BNInstructionTextToken * len(tokens))()
- for i in xrange(0, len(tokens)):
+ for i in range(0, len(tokens)):
if isinstance(tokens[i].type, str):
token_buf[i].type = InstructionTextTokenType[tokens[i].type]
else:
@@ -615,10 +624,10 @@ class Architecture(object):
def _get_full_width_registers(self, ctxt, count):
try:
- regs = self._full_width_regs.values()
+ regs = list(self._full_width_regs.values())
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = regs[i]
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -630,10 +639,10 @@ class Architecture(object):
def _get_all_registers(self, ctxt, count):
try:
- regs = self._regs_by_index.keys()
+ regs = list(self._regs_by_index.keys())
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = regs[i]
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -645,10 +654,10 @@ class Architecture(object):
def _get_all_flags(self, ctxt, count):
try:
- flags = self._flags_by_index.keys()
+ flags = list(self._flags_by_index.keys())
count[0] = len(flags)
flag_buf = (ctypes.c_uint * len(flags))()
- for i in xrange(0, len(flags)):
+ for i in range(0, len(flags)):
flag_buf[i] = flags[i]
result = ctypes.cast(flag_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, flag_buf)
@@ -660,10 +669,10 @@ class Architecture(object):
def _get_all_flag_write_types(self, ctxt, count):
try:
- write_types = self._flag_write_types_by_index.keys()
+ write_types = list(self._flag_write_types_by_index.keys())
count[0] = len(write_types)
type_buf = (ctypes.c_uint * len(write_types))()
- for i in xrange(0, len(write_types)):
+ for i in range(0, len(write_types)):
type_buf[i] = write_types[i]
result = ctypes.cast(type_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, type_buf)
@@ -675,10 +684,10 @@ class Architecture(object):
def _get_all_semantic_flag_classes(self, ctxt, count):
try:
- sem_classes = self._semantic_flag_classes_by_index.keys()
+ sem_classes = list(self._semantic_flag_classes_by_index.keys())
count[0] = len(sem_classes)
class_buf = (ctypes.c_uint * len(sem_classes))()
- for i in xrange(0, len(sem_classes)):
+ for i in range(0, len(sem_classes)):
class_buf[i] = sem_classes[i]
result = ctypes.cast(class_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, class_buf)
@@ -690,10 +699,10 @@ class Architecture(object):
def _get_all_semantic_flag_groups(self, ctxt, count):
try:
- sem_groups = self._semantic_flag_groups_by_index.keys()
+ sem_groups = list(self._semantic_flag_groups_by_index.keys())
count[0] = len(sem_groups)
group_buf = (ctypes.c_uint * len(sem_groups))()
- for i in xrange(0, len(sem_groups)):
+ for i in range(0, len(sem_groups)):
group_buf[i] = sem_groups[i]
result = ctypes.cast(group_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, group_buf)
@@ -726,7 +735,7 @@ class Architecture(object):
flags.append(self._flags[name])
count[0] = len(flags)
flag_buf = (ctypes.c_uint * len(flags))()
- for i in xrange(0, len(flags)):
+ for i in range(0, len(flags)):
flag_buf[i] = flags[i]
result = ctypes.cast(flag_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, flag_buf)
@@ -744,7 +753,7 @@ class Architecture(object):
flags = []
count[0] = len(flags)
flag_buf = (ctypes.c_uint * len(flags))()
- for i in xrange(0, len(flags)):
+ for i in range(0, len(flags)):
flag_buf[i] = flags[i]
result = ctypes.cast(flag_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, flag_buf)
@@ -792,7 +801,7 @@ class Architecture(object):
flags = []
count[0] = len(flags)
flag_buf = (ctypes.c_uint * len(flags))()
- for i in xrange(0, len(flags)):
+ for i in range(0, len(flags)):
flag_buf[i] = flags[i]
result = ctypes.cast(flag_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, flag_buf)
@@ -819,7 +828,7 @@ class Architecture(object):
write_type_name = self._flag_write_types_by_index[write_type]
flag_name = self._flags_by_index[flag]
operand_list = []
- for i in xrange(operand_count):
+ for i in range(operand_count):
if operands[i].constant:
operand_list.append(operands[i].value)
elif lowlevelil.LLIL_REG_IS_TEMP(operands[i].reg):
@@ -908,7 +917,7 @@ class Architecture(object):
try:
count[0] = len(self.global_regs)
reg_buf = (ctypes.c_uint * len(self.global_regs))()
- for i in xrange(0, len(self.global_regs)):
+ for i in range(0, len(self.global_regs)):
reg_buf[i] = self._all_regs[self.global_regs[i]]
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -929,10 +938,10 @@ class Architecture(object):
def _get_all_register_stacks(self, ctxt, count):
try:
- regs = self._reg_stacks_by_index.keys()
+ regs = list(self._reg_stacks_by_index.keys())
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = regs[i]
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -980,10 +989,10 @@ class Architecture(object):
def _get_all_intrinsics(self, ctxt, count):
try:
- regs = self._intrinsics_by_index.keys()
+ regs = list(self._intrinsics_by_index.keys())
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = regs[i]
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -999,7 +1008,7 @@ class Architecture(object):
inputs = self._intrinsics_by_index[intrinsic][1].inputs
count[0] = len(inputs)
input_buf = (core.BNNameAndType * len(inputs))()
- for i in xrange(0, len(inputs)):
+ for i in range(0, len(inputs)):
input_buf[i].name = inputs[i].name
input_buf[i].type = core.BNNewTypeReference(inputs[i].type.handle)
input_buf[i].typeConfidence = inputs[i].type.confidence
@@ -1020,7 +1029,7 @@ class Architecture(object):
raise ValueError("freeing name and type list that wasn't allocated")
name_and_types = self._pending_name_and_type_lists[buf.value][1]
count = self._pending_name_and_type_lists[buf.value][2]
- for i in xrange(0, count):
+ for i in range(0, count):
core.BNFreeType(name_and_types[i].type)
del self._pending_name_and_type_lists[buf.value]
except (ValueError, KeyError):
@@ -1032,7 +1041,7 @@ class Architecture(object):
outputs = self._intrinsics_by_index[intrinsic][1].outputs
count[0] = len(outputs)
output_buf = (core.BNTypeWithConfidence * len(outputs))()
- for i in xrange(0, len(outputs)):
+ for i in range(0, len(outputs)):
output_buf[i].type = core.BNNewTypeReference(outputs[i].handle)
output_buf[i].confidence = outputs[i].confidence
result = ctypes.cast(output_buf, ctypes.c_void_p)
@@ -1052,7 +1061,7 @@ class Architecture(object):
raise ValueError("freeing type list that wasn't allocated")
types = self._pending_type_lists[buf.value][1]
count = self._pending_type_lists[buf.value][2]
- for i in xrange(0, count):
+ for i in range(0, count):
core.BNFreeType(types[i].type)
del self._pending_type_lists[buf.value]
except (ValueError, KeyError):
@@ -1064,7 +1073,6 @@ class Architecture(object):
errors[0] = core.BNAllocString(str(error_str))
if data is None:
return False
- data = str(data)
buf = ctypes.create_string_buffer(len(data))
ctypes.memmove(buf, data, len(data))
core.BNSetDataBufferContents(result, buf, len(data))
@@ -1701,7 +1709,7 @@ class Architecture(object):
:rtype: LowLevelILExpr index
"""
operand_list = (core.BNRegisterOrConstant * len(operands))()
- for i in xrange(len(operands)):
+ for i in range(len(operands)):
if isinstance(operands[i], str):
operand_list[i].constant = False
operand_list[i].reg = self.regs[operands[i]].index
@@ -1756,7 +1764,7 @@ class Architecture(object):
count = ctypes.c_ulonglong()
regs = core.BNGetModifiedArchitectureRegistersOnWrite(self.handle, reg, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(core.BNGetArchitectureRegisterName(self.handle, regs[i]))
core.BNFreeRegisterList(regs)
return result
@@ -2065,15 +2073,15 @@ class CoreArchitecture(Architecture):
self._regs_by_index = {}
self._full_width_regs = {}
self.__dict__["regs"] = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureRegisterName(self.handle, regs[i])
info = core.BNGetArchitectureRegisterInfo(self.handle, regs[i])
full_width_reg = core.BNGetArchitectureRegisterName(self.handle, info.fullWidthRegister)
- self.regs[name] = function.RegisterInfo(full_width_reg, info.size, info.offset,
+ self.regs[name] = binaryninja.function.RegisterInfo(full_width_reg, info.size, info.offset,
ImplicitRegisterExtend(info.extend), regs[i])
self._all_regs[name] = regs[i]
self._regs_by_index[regs[i]] = name
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
info = core.BNGetArchitectureRegisterInfo(self.handle, regs[i])
full_width_reg = core.BNGetArchitectureRegisterName(self.handle, info.fullWidthRegister)
if full_width_reg not in self._full_width_regs:
@@ -2085,7 +2093,7 @@ class CoreArchitecture(Architecture):
self._flags = {}
self._flags_by_index = {}
self.__dict__["flags"] = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureFlagName(self.handle, flags[i])
self._flags[name] = flags[i]
self._flags_by_index[flags[i]] = name
@@ -2097,7 +2105,7 @@ class CoreArchitecture(Architecture):
self._flag_write_types = {}
self._flag_write_types_by_index = {}
self.__dict__["flag_write_types"] = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureFlagWriteTypeName(self.handle, write_types[i])
self._flag_write_types[name] = write_types[i]
self._flag_write_types_by_index[write_types[i]] = name
@@ -2109,7 +2117,7 @@ class CoreArchitecture(Architecture):
self._semantic_flag_classes = {}
self._semantic_flag_classes_by_index = {}
self.__dict__["semantic_flag_classes"] = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureSemanticFlagClassName(self.handle, sem_classes[i])
self._semantic_flag_classes[name] = sem_classes[i]
self._semantic_flag_classes_by_index[sem_classes[i]] = name
@@ -2121,7 +2129,7 @@ class CoreArchitecture(Architecture):
self._semantic_flag_groups = {}
self._semantic_flag_groups_by_index = {}
self.__dict__["semantic_flag_groups"] = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureSemanticFlagGroupName(self.handle, sem_groups[i])
self._semantic_flag_groups[name] = sem_groups[i]
self._semantic_flag_groups_by_index[sem_groups[i]] = name
@@ -2140,7 +2148,7 @@ class CoreArchitecture(Architecture):
count = ctypes.c_ulonglong()
flags = core.BNGetArchitectureFlagsRequiredForFlagCondition(self.handle, cond, 0, count)
flag_names = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
flag_names.append(self._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
self.__dict__["flags_required_for_flag_condition"][cond] = flag_names
@@ -2153,7 +2161,7 @@ class CoreArchitecture(Architecture):
self._semantic_flag_groups[group], count)
flag_indexes = []
flag_names = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
flag_indexes.append(flags[i])
flag_names.append(self._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
@@ -2168,7 +2176,7 @@ class CoreArchitecture(Architecture):
self._semantic_flag_groups[group], count)
class_index_cond = {}
class_cond = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
class_index_cond[conditions[i].semanticClass] = conditions[i].condition
if conditions[i].semanticClass == 0:
class_cond[None] = conditions[i].condition
@@ -2186,7 +2194,7 @@ class CoreArchitecture(Architecture):
self._flag_write_types[write_type], count)
flag_indexes = []
flag_names = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
flag_indexes.append(flags[i])
flag_names.append(self._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
@@ -2208,7 +2216,7 @@ class CoreArchitecture(Architecture):
count = ctypes.c_ulonglong()
regs = core.BNGetArchitectureGlobalRegisters(self.handle, count)
self.__dict__["global_regs"] = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
self.global_regs.append(core.BNGetArchitectureRegisterName(self.handle, regs[i]))
core.BNFreeRegisterList(regs)
@@ -2217,17 +2225,17 @@ class CoreArchitecture(Architecture):
self._all_reg_stacks = {}
self._reg_stacks_by_index = {}
self.__dict__["reg_stacks"] = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureRegisterStackName(self.handle, regs[i])
info = core.BNGetArchitectureRegisterStackInfo(self.handle, regs[i])
storage = []
- for j in xrange(0, info.storageCount):
+ for j in range(0, info.storageCount):
storage.append(core.BNGetArchitectureRegisterName(self.handle, info.firstStorageReg + j))
top_rel = []
- for j in xrange(0, info.topRelativeCount):
+ for j in range(0, info.topRelativeCount):
top_rel.append(core.BNGetArchitectureRegisterName(self.handle, info.firstTopRelativeReg + j))
top = core.BNGetArchitectureRegisterName(self.handle, info.stackTopReg)
- self.reg_stacks[name] = function.RegisterStackInfo(storage, top_rel, top, regs[i])
+ self.reg_stacks[name] = binaryninja.function.RegisterStackInfo(storage, top_rel, top, regs[i])
self._all_reg_stacks[name] = regs[i]
self._reg_stacks_by_index[regs[i]] = name
core.BNFreeRegisterList(regs)
@@ -2237,23 +2245,23 @@ class CoreArchitecture(Architecture):
self._intrinsics = {}
self._intrinsics_by_index = {}
self.__dict__["intrinsics"] = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = core.BNGetArchitectureIntrinsicName(self.handle, intrinsics[i])
input_count = ctypes.c_ulonglong()
inputs = core.BNGetArchitectureIntrinsicInputs(self.handle, intrinsics[i], input_count)
input_list = []
- for j in xrange(0, input_count.value):
+ for j in range(0, input_count.value):
input_name = inputs[j].name
type_obj = types.Type(core.BNNewTypeReference(inputs[j].type), confidence = inputs[j].typeConfidence)
- input_list.append(function.IntrinsicInput(type_obj, input_name))
+ input_list.append(binaryninja.function.IntrinsicInput(type_obj, input_name))
core.BNFreeNameAndTypeList(inputs, input_count.value)
output_count = ctypes.c_ulonglong()
outputs = core.BNGetArchitectureIntrinsicOutputs(self.handle, intrinsics[i], output_count)
output_list = []
- for j in xrange(0, output_count.value):
+ for j in range(0, output_count.value):
output_list.append(types.Type(core.BNNewTypeReference(outputs[j].type), confidence = outputs[j].confidence))
core.BNFreeOutputTypeList(outputs, output_count.value)
- self.intrinsics[name] = function.IntrinsicInfo(input_list, output_list)
+ self.intrinsics[name] = binaryninja.function.IntrinsicInfo(input_list, output_list)
self._intrinsics[name] = intrinsics[i]
self._intrinsics_by_index[intrinsics[i]] = (name, self.intrinsics[name])
core.BNFreeRegisterList(intrinsics)
@@ -2285,16 +2293,15 @@ class CoreArchitecture(Architecture):
:rtype: InstructionInfo
"""
info = core.BNInstructionInfo()
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
if not core.BNGetInstructionInfo(self.handle, buf, addr, len(data), info):
return None
- result = function.InstructionInfo()
+ result = binaryninja.function.InstructionInfo()
result.length = info.length
result.arch_transition_by_target_addr = info.archTransitionByTargetAddr
result.branch_delay = info.branchDelay
- for i in xrange(0, info.branchCount):
+ for i in range(0, info.branchCount):
target = info.branchTarget[i]
if info.branchArch[i]:
arch = CoreArchitecture._from_cache(info.branchArch[i])
@@ -2313,7 +2320,6 @@ class CoreArchitecture(Architecture):
:return: an InstructionTextToken list for the current instruction
:rtype: list(InstructionTextToken)
"""
- data = str(data)
count = ctypes.c_ulonglong()
length = ctypes.c_ulonglong()
length.value = len(data)
@@ -2323,7 +2329,7 @@ class CoreArchitecture(Architecture):
if not core.BNGetInstructionText(self.handle, buf, addr, length, tokens, count):
return None, 0
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -2332,7 +2338,7 @@ class CoreArchitecture(Architecture):
context = tokens[i].context
confidence = tokens[i].confidence
address = tokens[i].address
- result.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
core.BNFreeInstructionText(tokens, count.value)
return result, length.value
@@ -2350,7 +2356,6 @@ class CoreArchitecture(Architecture):
:return: the length of the current instruction
:rtype: int
"""
- data = str(data)
length = ctypes.c_ulonglong()
length.value = len(data)
buf = (ctypes.c_ubyte * len(data))()
@@ -2370,7 +2375,7 @@ class CoreArchitecture(Architecture):
"""
flag = self.get_flag_index(flag)
operand_list = (core.BNRegisterOrConstant * len(operands))()
- for i in xrange(len(operands)):
+ for i in range(len(operands)):
if isinstance(operands[i], str):
operand_list[i].constant = False
operand_list[i].reg = self.regs[operands[i]].index
@@ -2410,8 +2415,8 @@ class CoreArchitecture(Architecture):
:param str code: string representation of the instructions to be assembled
:param int addr: virtual address that the instructions will be loaded at
- :return: the bytes for the assembled instructions or error string
- :rtype: (a tuple of instructions and empty string) or (or None and error string)
+ :return: the bytes for the assembled instructions
+ :rtype: Python3 - a 'bytes' object; Python2 - a 'str'
:Example:
>>> arch.assemble("je 10")
@@ -2421,8 +2426,11 @@ class CoreArchitecture(Architecture):
result = databuffer.DataBuffer()
errors = ctypes.c_char_p()
if not core.BNAssemble(self.handle, code, addr, result.handle, errors):
- return None, errors.value
- return str(result), errors.value
+ raise ValueError("Could not assemble")
+ if isinstance(str(result), bytes):
+ return str(result)
+ else:
+ return bytes(result)
def is_never_branch_patch_available(self, data, addr):
"""
@@ -2440,7 +2448,6 @@ class CoreArchitecture(Architecture):
False
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
return core.BNIsArchitectureNeverBranchPatchAvailable(self.handle, buf, addr, len(data))
@@ -2462,7 +2469,6 @@ class CoreArchitecture(Architecture):
False
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
return core.BNIsArchitectureAlwaysBranchPatchAvailable(self.handle, buf, addr, len(data))
@@ -2483,7 +2489,6 @@ class CoreArchitecture(Architecture):
False
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
return core.BNIsArchitectureInvertBranchPatchAvailable(self.handle, buf, addr, len(data))
@@ -2507,7 +2512,6 @@ class CoreArchitecture(Architecture):
False
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
return core.BNIsArchitectureSkipAndReturnZeroPatchAvailable(self.handle, buf, addr, len(data))
@@ -2529,7 +2533,6 @@ class CoreArchitecture(Architecture):
False
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
return core.BNIsArchitectureSkipAndReturnValuePatchAvailable(self.handle, buf, addr, len(data))
@@ -2549,7 +2552,6 @@ class CoreArchitecture(Architecture):
'\\x90\\x90'
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
if not core.BNArchitectureConvertToNop(self.handle, buf, addr, len(data)):
@@ -2576,7 +2578,6 @@ class CoreArchitecture(Architecture):
(['jmp ', '0x9'], 5L)
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
if not core.BNArchitectureAlwaysBranch(self.handle, buf, addr, len(data)):
@@ -2604,7 +2605,6 @@ class CoreArchitecture(Architecture):
(['jl ', '0xa'], 6L)
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
if not core.BNArchitectureInvertBranch(self.handle, buf, addr, len(data)):
@@ -2628,7 +2628,6 @@ class CoreArchitecture(Architecture):
(['mov ', 'eax', ', ', '0x0'], 5L)
>>>
"""
- data = str(data)
buf = (ctypes.c_ubyte * len(data))()
ctypes.memmove(buf, data, len(data))
if not core.BNArchitectureSkipAndReturnValue(self.handle, buf, addr, len(data), value):
@@ -2655,7 +2654,7 @@ class CoreArchitecture(Architecture):
count = ctypes.c_ulonglong()
flags = core.BNGetArchitectureFlagsRequiredForFlagCondition(self.handle, cond, sem_class, count)
flag_names = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
flag_names.append(self._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
return flag_names
diff --git a/python/basicblock.py b/python/basicblock.py
index 3f615bba..353771d9 100644
--- a/python/basicblock.py
+++ b/python/basicblock.py
@@ -21,11 +21,13 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import BranchType, HighlightColorStyle, HighlightStandardColor, InstructionTextTokenType
-import architecture
-import highlight
-import function
+import binaryninja
+from binaryninja import highlight
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import BranchType, HighlightColorStyle, HighlightStandardColor, InstructionTextTokenType
+
+# 2-3 compatibility
+from binaryninja import range
class BasicBlockEdge(object):
@@ -87,7 +89,7 @@ class BasicBlock(object):
func = core.BNGetBasicBlockFunction(self.handle)
if func is None:
return None
- self._func = function.Function(self.view, func)
+ self._func =binaryninja.function.Function(self.view, func)
return self._func
@property
@@ -100,7 +102,7 @@ class BasicBlock(object):
arch = core.BNGetBasicBlockArchitecture(self.handle)
if arch is None:
return None
- self._arch = architecture.CoreArchitecture._from_cache(arch)
+ self._arch = binaryninja.architecture.CoreArchitecture._from_cache(arch)
return self._arch
@property
@@ -129,7 +131,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong(0)
edges = core.BNGetBasicBlockOutgoingEdges(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
branch_type = BranchType(edges[i].type)
if edges[i].target:
target = self._create_instance(self.view, core.BNNewBasicBlockReference(edges[i].target))
@@ -145,7 +147,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong(0)
edges = core.BNGetBasicBlockIncomingEdges(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
branch_type = BranchType(edges[i].type)
if edges[i].target:
target = self._create_instance(self.view, core.BNNewBasicBlockReference(edges[i].target))
@@ -171,7 +173,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetBasicBlockDominators(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -182,7 +184,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetBasicBlockStrictDominators(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -201,7 +203,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetBasicBlockDominatorTreeChildren(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -212,7 +214,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetBasicBlockDominanceFrontier(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self._create_instance(self.view, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -225,7 +227,7 @@ class BasicBlock(object):
@property
def disassembly_text(self):
"""
- ``disassembly_text`` property which returns a list of function.DisassemblyTextLine objects for the current basic block.
+ ``disassembly_text`` property which returns a list of binaryninja.function.DisassemblyTextLine objects for the current basic block.
:Example:
>>> current_basic_block.disassembly_text
@@ -269,12 +271,12 @@ class BasicBlock(object):
if len(blocks) == 0:
return []
block_set = (ctypes.POINTER(core.BNBasicBlock) * len(blocks))()
- for i in xrange(len(blocks)):
+ for i in range(len(blocks)):
block_set[i] = blocks[i].handle
count = ctypes.c_ulonglong()
out_blocks = core.BNGetBasicBlockIteratedDominanceFrontier(block_set, len(blocks), count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(BasicBlock(blocks[0].view, core.BNNewBasicBlockReference(out_blocks[i])))
core.BNFreeBasicBlockList(out_blocks, count.value)
return result
@@ -305,6 +307,8 @@ class BasicBlock(object):
inst_info = self.arch.get_instruction_info(data, idx)
inst_text = self.arch.get_instruction_text(data, idx)
+ if inst_info is None:
+ break
yield inst_text
idx += inst_info.length
@@ -313,7 +317,7 @@ class BasicBlock(object):
def get_disassembly_text(self, settings=None):
"""
- ``get_disassembly_text`` returns a list of function.DisassemblyTextLine objects for the current basic block.
+ ``get_disassembly_text`` returns a list of binaryninja.function.DisassemblyTextLine objects for the current basic block.
:param DisassemblySettings settings: (optional) DisassemblySettings object
:Example:
@@ -328,7 +332,7 @@ class BasicBlock(object):
count = ctypes.c_ulonglong()
lines = core.BNGetBasicBlockDisassemblyText(self.handle, settings_obj, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
addr = lines[i].addr
if (lines[i].instrIndex != 0xffffffffffffffff) and hasattr(self, 'il_function'):
il_instr = self.il_function[lines[i].instrIndex]
@@ -336,7 +340,7 @@ class BasicBlock(object):
il_instr = None
color = highlight.HighlightColor._from_core_struct(lines[i].highlight)
tokens = []
- for j in xrange(0, lines[i].count):
+ for j in range(0, lines[i].count):
token_type = InstructionTextTokenType(lines[i].tokens[j].type)
text = lines[i].tokens[j].text
value = lines[i].tokens[j].value
@@ -345,8 +349,8 @@ class BasicBlock(object):
context = lines[i].tokens[j].context
confidence = lines[i].tokens[j].confidence
address = lines[i].tokens[j].address
- tokens.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
- result.append(function.DisassemblyTextLine(tokens, addr, il_instr, color))
+ tokens.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.DisassemblyTextLine(tokens, addr, il_instr, color))
core.BNFreeDisassemblyTextLines(lines, count.value)
return result
diff --git a/python/binaryview.py b/python/binaryview.py
index ede279ad..d84ed0aa 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -22,26 +22,25 @@ import struct
import traceback
import ctypes
import abc
-import threading
# Binary Ninja components
-import _binaryninjacore as core
-from enums import (AnalysisState, SymbolType, InstructionTextTokenType,
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import (AnalysisState, SymbolType, InstructionTextTokenType,
Endianness, ModificationStatus, StringType, SegmentFlag, SectionSemantics)
-import function
-import startup
-import architecture
-import platform
-import associateddatastore
-import fileaccessor
-import filemetadata
-import log
-import databuffer
-import basicblock
-import types
-import lineardisassembly
-import metadata
-import highlight
+import binaryninja
+from binaryninja import associateddatastore # required for _BinaryViewAssociatedDataStore
+from binaryninja import log
+from binaryninja import types
+from binaryninja import fileaccessor
+from binaryninja import databuffer
+from binaryninja import basicblock
+from binaryninja import lineardisassembly
+from binaryninja import metadata
+from binaryninja import highlight
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class BinaryDataNotification(object):
@@ -100,21 +99,23 @@ class StringReference(object):
@property
def value(self):
- return self.view.read(self.start, self.length)
+ return binaryninja.pyNativeStr(self.view.read(self.start, self.length))
def __repr__(self):
return "<%s: %#x, len %#x>" % (self.type, self.start, self.length)
+_pending_analysis_completion_events = {}
class AnalysisCompletionEvent(object):
"""
The ``AnalysisCompletionEvent`` object provides an asynchronous mechanism for receiving
callbacks when analysis is complete. The callback runs once. A completion event must be added
- for each new analysis in order to be notified of each analysis completion.
+ for each new analysis in order to be notified of each analysis completion. The
+ AnalysisCompletionEvent class takes responcibility for keeping track of the object's lifetime.
:Example:
>>> def on_complete(self):
- ... print "Analysis Complete", self.view
+ ... print("Analysis Complete", self.view)
...
>>> evt = AnalysisCompletionEvent(bv, on_complete)
>>>
@@ -124,11 +125,19 @@ class AnalysisCompletionEvent(object):
self.callback = callback
self._cb = ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self._notify)
self.handle = core.BNAddAnalysisCompletionEvent(self.view.handle, None, self._cb)
+ global _pending_analysis_completion_events
+ _pending_analysis_completion_events[id(self)] = self
def __del__(self):
+ global _pending_analysis_completion_events
+ if id(self) in _pending_analysis_completion_events:
+ del _pending_analysis_completion_events[id(self)]
core.BNFreeAnalysisCompletionEvent(self.handle)
def _notify(self, ctxt):
+ global _pending_analysis_completion_events
+ if id(self) in _pending_analysis_completion_events:
+ del _pending_analysis_completion_events[id(self)]
try:
self.callback(self)
except:
@@ -144,6 +153,30 @@ class AnalysisCompletionEvent(object):
"""
self.callback = self._empty_callback
core.BNCancelAnalysisCompletionEvent(self.handle)
+ global _pending_analysis_completion_events
+ if id(self) in _pending_analysis_completion_events:
+ del _pending_analysis_completion_events[id(self)]
+
+
+class ActiveAnalysisInfo(object):
+ def __init__(self, func, analysis_time, update_count, submit_count):
+ self.func = func
+ self.analysis_time = analysis_time
+ self.update_count = update_count
+ self.submit_count = submit_count
+
+ def __repr__(self):
+ return "<ActiveAnalysisInfo %s, analysis_time %d, update_count %d, submit_count %d>" % (self.func, self.analysis_time, self.update_count, self.submit_count)
+
+
+class AnalysisInfo(object):
+ def __init__(self, state, analysis_time, active_info):
+ self.state = AnalysisState(state)
+ self.analysis_time = analysis_time
+ self.active_info = active_info
+
+ def __repr__(self):
+ return "<AnalysisInfo %s, analysis_time %d, active_info %s>" % (self.state, self.analysis_time, self.active_info)
class AnalysisProgress(object):
@@ -157,6 +190,8 @@ class AnalysisProgress(object):
return "Disassembling (%d/%d)" % (self.count, self.total)
if self.state == AnalysisState.AnalyzeState:
return "Analyzing (%d/%d)" % (self.count, self.total)
+ if self.state == AnalysisState.ExtendedAnalyzeState:
+ return "Extended Analysis"
return "Idle"
def __repr__(self):
@@ -220,25 +255,25 @@ class BinaryDataNotificationCallbacks(object):
def _function_added(self, ctxt, view, func):
try:
- self.notify.function_added(self.view, function.Function(self.view, core.BNNewFunctionReference(func)))
+ self.notify.function_added(self.view, binaryninja.function.Function(self.view, core.BNNewFunctionReference(func)))
except:
log.log_error(traceback.format_exc())
def _function_removed(self, ctxt, view, func):
try:
- self.notify.function_removed(self.view, function.Function(self.view, core.BNNewFunctionReference(func)))
+ self.notify.function_removed(self.view, binaryninja.function.Function(self.view, core.BNNewFunctionReference(func)))
except:
log.log_error(traceback.format_exc())
def _function_updated(self, ctxt, view, func):
try:
- self.notify.function_updated(self.view, function.Function(self.view, core.BNNewFunctionReference(func)))
+ self.notify.function_updated(self.view, binaryninja.function.Function(self.view, core.BNNewFunctionReference(func)))
except:
log.log_error(traceback.format_exc())
def _function_update_requested(self, ctxt, view, func):
try:
- self.notify.function_update_requested(self.view, function.Function(self.view, core.BNNewFunctionReference(func)))
+ self.notify.function_update_requested(self.view, binaryninja.function.Function(self.view, core.BNNewFunctionReference(func)))
except:
log.log_error(traceback.format_exc())
@@ -297,38 +332,38 @@ class BinaryDataNotificationCallbacks(object):
class _BinaryViewTypeMetaclass(type):
+
@property
def list(self):
"""List all BinaryView types (read-only)"""
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
types = core.BNGetBinaryViewTypes(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(BinaryViewType(types[i]))
core.BNFreeBinaryViewTypeList(types)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
types = core.BNGetBinaryViewTypes(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield BinaryViewType(types[i])
finally:
core.BNFreeBinaryViewTypeList(types)
def __getitem__(self, value):
- startup._init_plugins()
+ binaryninja._init_plugins()
view_type = core.BNGetBinaryViewTypeByName(str(value))
if view_type is None:
raise KeyError("'%s' is not a valid view type" % str(value))
return BinaryViewType(view_type)
-class BinaryViewType(object):
- __metaclass__ = _BinaryViewTypeMetaclass
+class BinaryViewType(with_metaclass(_BinaryViewTypeMetaclass, object)):
def __init__(self, handle):
self.handle = core.handle_of_type(handle, core.BNBinaryViewType)
@@ -344,6 +379,11 @@ class BinaryViewType(object):
return ctypes.addressof(self.handle.contents) != ctypes.addressof(value.handle.contents)
@property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
+ @property
def name(self):
"""BinaryView name (read-only)"""
return core.BNGetBinaryViewTypeName(self.handle)
@@ -384,7 +424,7 @@ class BinaryViewType(object):
if f is None or f.read(len(sqlite)) != sqlite:
return None
f.close()
- view = filemetadata.FileMetadata().open_existing_database(filename)
+ view = binaryninja.filemetadata.FileMetadata().open_existing_database(filename)
else:
view = BinaryView.open(filename)
@@ -397,6 +437,9 @@ class BinaryViewType(object):
else:
bv = cls[available.name].open(filename)
+ if bv is None:
+ raise Exception("Unknown Architecture/Architecture Not Found (check plugins folder)")
+
if update_analysis:
bv.update_analysis_and_wait()
return bv
@@ -412,7 +455,7 @@ class BinaryViewType(object):
arch = core.BNGetArchitectureForViewType(self.handle, ident, endian)
if arch is None:
return None
- return architecture.CoreArchitecture._from_cache(arch)
+ return binaryninja.architecture.CoreArchitecture._from_cache(arch)
def register_platform(self, ident, arch, plat):
core.BNRegisterPlatformForViewType(self.handle, ident, arch.handle, plat.handle)
@@ -424,7 +467,7 @@ class BinaryViewType(object):
plat = core.BNGetPlatformForViewType(self.handle, ident, arch.handle)
if plat is None:
return None
- return platform.Platform(None, plat)
+ return binaryninja.platform.Platform(None, plat)
class Segment(object):
@@ -566,17 +609,17 @@ class BinaryView(object):
if handle is not None:
self.handle = core.handle_of_type(handle, core.BNBinaryView)
if file_metadata is None:
- self.file = filemetadata.FileMetadata(handle=core.BNGetFileForView(handle))
+ self.file = binaryninja.filemetadata.FileMetadata(handle=core.BNGetFileForView(handle))
else:
self.file = file_metadata
elif self.__class__ is BinaryView:
- startup._init_plugins()
+ binaryninja._init_plugins()
if file_metadata is None:
- file_metadata = filemetadata.FileMetadata()
+ file_metadata = binaryninja.filemetadata.FileMetadata()
self.handle = core.BNCreateBinaryDataView(file_metadata.handle)
- self.file = filemetadata.FileMetadata(handle=core.BNNewFileReference(file_metadata.handle))
+ self.file = binaryninja.filemetadata.FileMetadata(handle=core.BNNewFileReference(file_metadata.handle))
else:
- startup._init_plugins()
+ binaryninja._init_plugins()
if not self.__class__._registered:
raise TypeError("view type not registered")
self._cb = core.BNCustomBinaryView()
@@ -619,7 +662,7 @@ class BinaryView(object):
@classmethod
def register(cls):
- startup._init_plugins()
+ binaryninja._init_plugins()
if cls.name is None:
raise ValueError("view 'name' not defined")
if cls.long_name is None:
@@ -634,7 +677,7 @@ class BinaryView(object):
@classmethod
def _create(cls, ctxt, data):
try:
- file_metadata = filemetadata.FileMetadata(handle=core.BNGetFileForView(data))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle=core.BNGetFileForView(data))
view = cls(BinaryView(file_metadata=file_metadata, handle=core.BNNewViewReference(data)))
if view is None:
return None
@@ -653,14 +696,14 @@ class BinaryView(object):
@classmethod
def open(cls, src, file_metadata=None):
- startup._init_plugins()
+ binaryninja._init_plugins()
if isinstance(src, fileaccessor.FileAccessor):
if file_metadata is None:
- file_metadata = filemetadata.FileMetadata()
+ file_metadata = binaryninja.filemetadata.FileMetadata()
view = core.BNCreateBinaryDataViewFromFile(file_metadata.handle, src._cb)
else:
if file_metadata is None:
- file_metadata = filemetadata.FileMetadata(str(src))
+ file_metadata = binaryninja.filemetadata.FileMetadata(str(src))
view = core.BNCreateBinaryDataViewFromFilename(file_metadata.handle, str(src))
if view is None:
return None
@@ -669,9 +712,9 @@ class BinaryView(object):
@classmethod
def new(cls, data=None, file_metadata=None):
- startup._init_plugins()
+ binaryninja._init_plugins()
if file_metadata is None:
- file_metadata = filemetadata.FileMetadata()
+ file_metadata = binaryninja.filemetadata.FileMetadata()
if data is None:
view = core.BNCreateBinaryDataView(file_metadata.handle)
else:
@@ -755,8 +798,8 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
funcs = core.BNGetAnalysisFunctionList(self.handle, count)
try:
- for i in xrange(0, count.value):
- yield function.Function(self, core.BNNewFunctionReference(funcs[i]))
+ for i in range(0, count.value):
+ yield binaryninja.function.Function(self, core.BNNewFunctionReference(funcs[i]))
finally:
core.BNFreeFunctionList(funcs, count.value)
@@ -824,7 +867,7 @@ class BinaryView(object):
arch = core.BNGetDefaultArchitecture(self.handle)
if arch is None:
return None
- return architecture.CoreArchitecture._from_cache(handle=arch)
+ return binaryninja.architecture.CoreArchitecture._from_cache(handle=arch)
@arch.setter
def arch(self, value):
@@ -839,7 +882,7 @@ class BinaryView(object):
plat = core.BNGetDefaultPlatform(self.handle)
if plat is None:
return None
- return platform.Platform(self.arch, handle=plat)
+ return binaryninja.platform.Platform(self.arch, handle=plat)
@platform.setter
def platform(self, value):
@@ -874,8 +917,8 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
funcs = core.BNGetAnalysisFunctionList(self.handle, count)
result = []
- for i in xrange(0, count.value):
- result.append(function.Function(self, core.BNNewFunctionReference(funcs[i])))
+ for i in range(0, count.value):
+ result.append(binaryninja.function.Function(self, core.BNNewFunctionReference(funcs[i])))
core.BNFreeFunctionList(funcs, count.value)
return result
@@ -890,7 +933,7 @@ class BinaryView(object):
func = core.BNGetAnalysisEntryPoint(self.handle)
if func is None:
return None
- return function.Function(self, func)
+ return binaryninja.function.Function(self, func)
@property
def symbols(self):
@@ -898,7 +941,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
syms = core.BNGetSymbols(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
sym = types.Symbol(None, None, None, handle=core.BNNewSymbolReference(syms[i]))
result[sym.raw_name] = sym
core.BNFreeSymbolList(syms, count.value)
@@ -915,7 +958,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
types = core.BNGetBinaryViewTypesForData(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(BinaryViewType(types[i]))
core.BNFreeBinaryViewTypeList(types)
return result
@@ -935,6 +978,20 @@ class BinaryView(object):
self.file.saved = value
@property
+ def analysis_info(self):
+ """Relevant analysis information with list of functions under active analysis (read-only)"""
+ info_ref = core.BNGetAnalysisInfo(self.handle)
+ info = info_ref[0]
+ active_info_list = []
+ for i in xrange(0, info.count):
+ func = binaryninja.function.Function(self, core.BNNewFunctionReference(info.activeInfo[i].func))
+ active_info = ActiveAnalysisInfo(func, info.activeInfo[i].analysisTime, info.activeInfo[i].updateCount, info.activeInfo[i].submitCount)
+ active_info_list.append(active_info)
+ result = AnalysisInfo(info.state, info.analysisTime, active_info_list)
+ core.BNFreeAnalysisInfo(info_ref)
+ return result
+
+ @property
def analysis_progress(self):
"""Status of current analysis (read-only)"""
result = core.BNGetAnalysisProgress(self.handle)
@@ -951,7 +1008,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
var_list = core.BNGetDataVariables(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
addr = var_list[i].address
var_type = types.Type(core.BNNewTypeReference(var_list[i].type), platform = self.platform, confidence = var_list[i].typeConfidence)
auto_discovered = var_list[i].autoDiscovered
@@ -965,7 +1022,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
type_list = core.BNGetAnalysisTypeList(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = types.QualifiedName._from_core_struct(type_list[i].name)
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type), platform = self.platform)
core.BNFreeTypeList(type_list, count.value)
@@ -977,7 +1034,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
segment_list = core.BNGetSegments(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Segment(segment_list[i].start, segment_list[i].length,
segment_list[i].dataOffset, segment_list[i].dataLength, segment_list[i].flags, segment_list[i].autoDefined))
core.BNFreeSegmentList(segment_list)
@@ -989,7 +1046,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
section_list = core.BNGetSections(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result[section_list[i].name] = Section(section_list[i].name, section_list[i].type, section_list[i].start,
section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection,
section_list[i].infoData, section_list[i].align, section_list[i].entrySize,
@@ -1003,7 +1060,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
range_list = core.BNGetAllocatedRanges(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(AddressRange(range_list[i].start, range_list[i].end))
core.BNFreeAddressRanges(range_list)
return result
@@ -1023,7 +1080,15 @@ class BinaryView(object):
def global_pointer_value(self):
"""Discovered value of the global pointer register, if the binary uses one (read-only)"""
result = core.BNGetGlobalPointerValue(self.handle)
- return function.RegisterValue(self.arch, result.value, confidence = result.confidence)
+ return binaryninja.function.RegisterValue(self.arch, result.value, confidence = result.confidence)
+
+ @property
+ def parameters_for_analysis(self):
+ return core.BNGetParametersForAnalysis(self.handle)
+
+ @parameters_for_analysis.setter
+ def parameters_for_analysis(self, params):
+ core.BNSetParametersForAnalysis(self.handle, params)
@property
def max_function_size_for_analysis(self):
@@ -1680,10 +1745,13 @@ class BinaryView(object):
"""
``read`` returns the data reads at most ``length`` bytes from virtual address ``addr``.
+ Note: Python2 returns a str, but Python3 returns a bytes object. str(DataBufferObject) will
+ still get you a str in either case.
+
:param int addr: virtual address to read from.
:param int length: number of bytes to read.
:return: at most ``length`` bytes from the virtual address ``addr``, empty string on error or no data.
- :rtype: str
+ :rtype: python2 - str; python3 - bytes
:Example:
>>> #Opening a x86_64 Mach-O binary
@@ -1692,7 +1760,7 @@ class BinaryView(object):
\'\\xcf\\xfa\\xed\\xfe\'
"""
buf = databuffer.DataBuffer(handle=core.BNReadViewBuffer(self.handle, addr, length))
- return str(buf)
+ return bytes(buf)
def write(self, addr, data):
"""
@@ -1711,6 +1779,8 @@ class BinaryView(object):
>>> bv.read(0,4)
'AAAA'
"""
+ if not isinstance(data, bytes):
+ raise TypeError("Must be bytes")
buf = databuffer.DataBuffer(data)
return core.BNWriteViewBuffer(self.handle, addr, buf.handle)
@@ -1729,6 +1799,8 @@ class BinaryView(object):
>>> bv.read(0,8)
'BBBBAAAA'
"""
+ if not isinstance(data, bytes):
+ raise TypeError("Must be bytes")
buf = databuffer.DataBuffer(data)
return core.BNInsertViewBuffer(self.handle, addr, buf.handle)
@@ -2121,7 +2193,7 @@ class BinaryView(object):
func = core.BNGetAnalysisFunction(self.handle, plat.handle, addr)
if func is None:
return None
- return function.Function(self, func)
+ return binaryninja.function.Function(self, func)
def get_functions_at(self, addr):
"""
@@ -2137,8 +2209,8 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
funcs = core.BNGetAnalysisFunctionsForAddress(self.handle, addr, count)
result = []
- for i in xrange(0, count.value):
- result.append(function.Function(self, core.BNNewFunctionReference(funcs[i])))
+ for i in range(0, count.value):
+ result.append(binaryninja.function.Function(self, core.BNNewFunctionReference(funcs[i])))
core.BNFreeFunctionList(funcs, count.value)
return result
@@ -2146,7 +2218,7 @@ class BinaryView(object):
func = core.BNGetRecentAnalysisFunctionForAddress(self.handle, addr)
if func is None:
return None
- return function.Function(self, func)
+ return binaryninja.function.Function(self, func)
def get_basic_blocks_at(self, addr):
"""
@@ -2159,7 +2231,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
blocks = core.BNGetBasicBlocksForAddress(self.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(basicblock.BasicBlock(self, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -2175,7 +2247,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
blocks = core.BNGetBasicBlocksStartingAtAddress(self.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(basicblock.BasicBlock(self, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -2206,17 +2278,17 @@ class BinaryView(object):
else:
refs = core.BNGetCodeReferencesInRange(self.handle, addr, length, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
if refs[i].func:
- func = function.Function(self, core.BNNewFunctionReference(refs[i].func))
+ func = binaryninja.function.Function(self, core.BNNewFunctionReference(refs[i].func))
else:
func = None
if refs[i].arch:
- arch = architecture.CoreArchitecture._from_cache(refs[i].arch)
+ arch = binaryninja.architecture.CoreArchitecture._from_cache(refs[i].arch)
else:
arch = None
addr = refs[i].addr
- result.append(architecture.ReferenceSource(func, arch, addr))
+ result.append(binaryninja.architecture.ReferenceSource(func, arch, addr))
core.BNFreeCodeReferences(refs, count.value)
return result
@@ -2272,7 +2344,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
syms = core.BNGetSymbolsByName(self.handle, name, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(types.Symbol(None, None, None, handle = core.BNNewSymbolReference(syms[i])))
core.BNFreeSymbolList(syms, count.value)
return result
@@ -2297,7 +2369,7 @@ class BinaryView(object):
else:
syms = core.BNGetSymbolsInRange(self.handle, start, length, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(types.Symbol(None, None, None, handle = core.BNNewSymbolReference(syms[i])))
core.BNFreeSymbolList(syms, count.value)
return result
@@ -2326,7 +2398,7 @@ class BinaryView(object):
else:
syms = core.BNGetSymbolsOfTypeInRange(self.handle, sym_type, start, length, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(types.Symbol(None, None, None, handle = core.BNNewSymbolReference(syms[i])))
core.BNFreeSymbolList(syms, count.value)
return result
@@ -2721,9 +2793,11 @@ class BinaryView(object):
if start is None:
strings = core.BNGetStrings(self.handle, count)
else:
+ if length is None:
+ length = self.end - start
strings = core.BNGetStringsInRange(self.handle, start, length, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(StringReference(self, StringType(strings[i].type), strings[i].start, strings[i].length))
core.BNFreeStringReferenceList(strings)
return result
@@ -2731,7 +2805,9 @@ class BinaryView(object):
def add_analysis_completion_event(self, callback):
"""
``add_analysis_completion_event`` sets up a call back function to be called when analysis has been completed.
- This is helpful when using asynchronously analysis.
+ This is helpful when using ``update_analysis`` which does not wait for analysis completion before returning.
+
+ The callee of this function is not resposible for maintaining the lifetime of the returned AnalysisCompletionEvent object.
:param callable() callback: A function to be called with no parameters when analysis has completed.
:return: An initialized AnalysisCompletionEvent object.
@@ -2739,7 +2815,7 @@ class BinaryView(object):
:Example:
>>> def completionEvent():
- ... print "done"
+ ... print("done")
...
>>> bv.add_analysis_completion_event(completionEvent)
<binaryninja.AnalysisCompletionEvent object at 0x10a2c9f10>
@@ -2933,7 +3009,7 @@ class BinaryView(object):
func = None
block = None
if pos.function:
- func = function.Function(self, pos.function)
+ func = binaryninja.function.Function(self, pos.function)
if pos.block:
block = basicblock.BasicBlock(self, pos.block)
return lineardisassembly.LinearDisassemblyPosition(func, block, pos.address)
@@ -2955,17 +3031,17 @@ class BinaryView(object):
lines = api(self.handle, pos_obj, settings, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
func = None
block = None
if lines[i].function:
- func = function.Function(self, core.BNNewFunctionReference(lines[i].function))
+ func = binaryninja.function.Function(self, core.BNNewFunctionReference(lines[i].function))
if lines[i].block:
block = basicblock.BasicBlock(self, core.BNNewBasicBlockReference(lines[i].block))
color = highlight.HighlightColor._from_core_struct(lines[i].contents.highlight)
addr = lines[i].contents.addr
tokens = []
- for j in xrange(0, lines[i].contents.count):
+ for j in range(0, lines[i].contents.count):
token_type = InstructionTextTokenType(lines[i].contents.tokens[j].type)
text = lines[i].contents.tokens[j].text
value = lines[i].contents.tokens[j].value
@@ -2974,14 +3050,14 @@ class BinaryView(object):
context = lines[i].contents.tokens[j].context
confidence = lines[i].contents.tokens[j].confidence
address = lines[i].contents.tokens[j].address
- tokens.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
- contents = function.DisassemblyTextLine(tokens, addr, color = color)
+ tokens.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ contents = binaryninja.function.DisassemblyTextLine(tokens, addr, color = color)
result.append(lineardisassembly.LinearDisassemblyLine(lines[i].type, func, block, lines[i].lineOffset, contents))
func = None
block = None
if pos_obj.function:
- func = function.Function(self, pos_obj.function)
+ func = binaryninja.function.Function(self, pos_obj.function)
if pos_obj.block:
block = basicblock.BasicBlock(self, pos_obj.block)
pos.function = func
@@ -3048,7 +3124,7 @@ class BinaryView(object):
>>> settings = DisassemblySettings()
>>> lines = bv.get_linear_disassembly(settings)
>>> for line in lines:
- ... print line
+ ... print(line)
... break
...
cf fa ed fe 07 00 00 01 ........
@@ -3411,7 +3487,7 @@ class BinaryView(object):
count = ctypes.c_ulonglong(0)
section_list = core.BNGetSectionsAt(self.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Section(section_list[i].name, section_list[i].type, section_list[i].start,
section_list[i].length, section_list[i].linkedSection, section_list[i].infoSection,
section_list[i].infoData, section_list[i].align, section_list[i].entrySize,
@@ -3431,11 +3507,11 @@ class BinaryView(object):
def get_unique_section_names(self, name_list):
incoming_names = (ctypes.c_char_p * len(name_list))()
- for i in xrange(0, len(name_list)):
- incoming_names[i] = name_list[i]
+ for i in range(0, len(name_list)):
+ incoming_names[i] = binaryninja.cstr(name_list[i])
outgoing_names = core.BNGetUniqueSectionNames(self.handle, incoming_names, len(name_list))
result = []
- for i in xrange(0, len(name_list)):
+ for i in range(0, len(name_list)):
result.append(str(outgoing_names[i]))
core.BNFreeStringList(outgoing_names, len(name_list))
return result
diff --git a/python/callingconvention.py b/python/callingconvention.py
index e3ec7261..6b906184 100644
--- a/python/callingconvention.py
+++ b/python/callingconvention.py
@@ -22,13 +22,13 @@ import traceback
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-import architecture
-import log
-import types
-import function
-import binaryview
-from enums import VariableSourceType
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja import log
+from binaryninja.enums import VariableSourceType
+
+# 2-3 compatibility
+from binaryninja import range
class CallingConvention(object):
@@ -47,7 +47,7 @@ class CallingConvention(object):
_registered_calling_conventions = []
- def __init__(self, arch=None, name=None, handle=None, confidence=types.max_confidence):
+ def __init__(self, arch=None, name=None, handle=None, confidence=binaryninja.types.max_confidence):
if handle is None:
if arch is None or name is None:
raise ValueError("Must specify either handle or architecture and name")
@@ -75,7 +75,7 @@ class CallingConvention(object):
self.__class__._registered_calling_conventions.append(self)
else:
self.handle = handle
- self.arch = architecture.CoreArchitecture._from_cache(core.BNGetCallingConventionArchitecture(self.handle))
+ self.arch = binaryninja.architecture.CoreArchitecture._from_cache(core.BNGetCallingConventionArchitecture(self.handle))
self.__dict__["name"] = core.BNGetCallingConventionName(self.handle)
self.__dict__["arg_regs_share_index"] = core.BNAreArgumentRegistersSharedIndex(self.handle)
self.__dict__["stack_reserved_for_arg_regs"] = core.BNIsStackReservedForArgumentRegisters(self.handle)
@@ -85,7 +85,7 @@ class CallingConvention(object):
regs = core.BNGetCallerSavedRegisters(self.handle, count)
result = []
arch = self.arch
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs, count.value)
self.__dict__["caller_saved_regs"] = result
@@ -94,7 +94,7 @@ class CallingConvention(object):
regs = core.BNGetIntegerArgumentRegisters(self.handle, count)
result = []
arch = self.arch
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs, count.value)
self.__dict__["int_arg_regs"] = result
@@ -103,7 +103,7 @@ class CallingConvention(object):
regs = core.BNGetFloatArgumentRegisters(self.handle, count)
result = []
arch = self.arch
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs, count.value)
self.__dict__["float_arg_regs"] = result
@@ -136,7 +136,7 @@ class CallingConvention(object):
regs = core.BNGetImplicitlyDefinedRegisters(self.handle, count)
result = []
arch = self.arch
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs, count.value)
self.__dict__["implicitly_defined_regs"] = result
@@ -161,7 +161,7 @@ class CallingConvention(object):
regs = self.__class__.caller_saved_regs
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = self.arch.regs[regs[i]].index
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -176,7 +176,7 @@ class CallingConvention(object):
regs = self.__class__.int_arg_regs
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = self.arch.regs[regs[i]].index
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -191,7 +191,7 @@ class CallingConvention(object):
regs = self.__class__.float_arg_regs
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = self.arch.regs[regs[i]].index
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -270,7 +270,7 @@ class CallingConvention(object):
regs = self.__class__.implicitly_defined_regs
count[0] = len(regs)
reg_buf = (ctypes.c_uint * len(regs))()
- for i in xrange(0, len(regs)):
+ for i in range(0, len(regs)):
reg_buf[i] = self.arch.regs[regs[i]].index
result = ctypes.cast(reg_buf, ctypes.c_void_p)
self._pending_reg_lists[result.value] = (result, reg_buf)
@@ -282,25 +282,25 @@ class CallingConvention(object):
def _get_incoming_reg_value(self, ctxt, reg, func, result):
try:
- func_obj = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
+ func_obj = binaryninja.function.Function(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
core.BNNewFunctionReference(func))
reg_name = self.arch.get_reg_name(reg)
api_obj = self.perform_get_incoming_reg_value(reg_name, func_obj)._to_api_object()
except:
log.log_error(traceback.format_exc())
- api_obj = function.RegisterValue()._to_api_object()
+ api_obj = binaryninja.function.RegisterValue()._to_api_object()
result[0].state = api_obj.state
result[0].value = api_obj.value
def _get_incoming_flag_value(self, ctxt, reg, func, result):
try:
- func_obj = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
+ func_obj = binaryninja.function.Function(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
core.BNNewFunctionReference(func))
reg_name = self.arch.get_reg_name(reg)
api_obj = self.perform_get_incoming_flag_value(reg_name, func_obj)._to_api_object()
except:
log.log_error(traceback.format_exc())
- api_obj = function.RegisterValue()._to_api_object()
+ api_obj = binaryninja.function.RegisterValue()._to_api_object()
result[0].state = api_obj.state
result[0].value = api_obj.value
@@ -309,9 +309,9 @@ class CallingConvention(object):
if func is None:
func_obj = None
else:
- func_obj = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
+ func_obj = binaryninja.function.Function(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
core.BNNewFunctionReference(func))
- in_var_obj = function.Variable(func_obj, in_var[0].type, in_var[0].index, in_var[0].storage)
+ in_var_obj = binaryninja.function.Variable(func_obj, in_var[0].type, in_var[0].index, in_var[0].storage)
out_var = self.perform_get_incoming_var_for_parameter_var(in_var_obj, func_obj)
result[0].type = out_var.source_type
result[0].index = out_var.index
@@ -327,9 +327,9 @@ class CallingConvention(object):
if func is None:
func_obj = None
else:
- func_obj = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
+ func_obj = binaryninja.function.Function(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)),
core.BNNewFunctionReference(func))
- in_var_obj = function.Variable(func_obj, in_var[0].type, in_var[0].index, in_var[0].storage)
+ in_var_obj = binaryninja.function.Variable(func_obj, in_var[0].type, in_var[0].index, in_var[0].storage)
out_var = self.perform_get_parameter_var_for_incoming_var(in_var_obj, func_obj)
result[0].type = out_var.source_type
result[0].index = out_var.index
@@ -350,11 +350,11 @@ class CallingConvention(object):
reg_stack = self.arch.get_reg_stack_for_reg(reg)
if reg_stack is not None:
if reg == self.arch.reg_stacks[reg_stack].stack_top_reg:
- return function.RegisterValue.constant(0)
- return function.RegisterValue()
+ return binaryninja.function.RegisterValue.constant(0)
+ return binaryninja.function.RegisterValue()
def perform_get_incoming_flag_value(self, reg, func):
- return function.RegisterValue()
+ return binaryninja.function.RegisterValue()
def perform_get_incoming_var_for_parameter_var(self, in_var, func):
in_buf = core.BNVariable()
@@ -365,7 +365,7 @@ class CallingConvention(object):
name = None
if (func is not None) and (out_var.type == VariableSourceType.RegisterVariableSourceType):
name = func.arch.get_reg_name(out_var.storage)
- return function.Variable(func, out_var.type, out_var.index, out_var.storage, name)
+ return binaryninja.function.Variable(func, out_var.type, out_var.index, out_var.storage, name)
def perform_get_parameter_var_for_incoming_var(self, in_var, func):
in_buf = core.BNVariable()
@@ -373,7 +373,7 @@ class CallingConvention(object):
in_buf.index = in_var.index
in_buf.storage = in_var.storage
out_var = core.BNGetDefaultParameterVariableForIncomingVariable(self.handle, in_buf)
- return function.Variable(func, out_var.type, out_var.index, out_var.storage)
+ return binaryninja.function.Variable(func, out_var.type, out_var.index, out_var.storage)
def with_confidence(self, confidence):
return CallingConvention(self.arch, handle = core.BNNewCallingConventionReference(self.handle),
@@ -384,14 +384,14 @@ class CallingConvention(object):
func_handle = None
if func is not None:
func_handle = func.handle
- return function.RegisterValue(self.arch, core.BNGetIncomingRegisterValue(self.handle, reg_num, func_handle))
+ return binaryninja.function.RegisterValue(self.arch, core.BNGetIncomingRegisterValue(self.handle, reg_num, func_handle))
def get_incoming_flag_value(self, flag, func):
reg_num = self.arch.get_flag_index(flag)
func_handle = None
if func is not None:
func_handle = func.handle
- return function.RegisterValue(self.arch, core.BNGetIncomingFlagValue(self.handle, reg_num, func_handle))
+ return binaryninja.function.RegisterValue(self.arch, core.BNGetIncomingFlagValue(self.handle, reg_num, func_handle))
def get_incoming_var_for_parameter_var(self, in_var, func):
in_buf = core.BNVariable()
@@ -406,7 +406,7 @@ class CallingConvention(object):
name = None
if (func is not None) and (out_var.type == VariableSourceType.RegisterVariableSourceType):
name = func.arch.get_reg_name(out_var.storage)
- return function.Variable(func, out_var.type, out_var.index, out_var.storage, name)
+ return binaryninja.function.Variable(func, out_var.type, out_var.index, out_var.storage, name)
def get_parameter_var_for_incoming_var(self, in_var, func):
in_buf = core.BNVariable()
@@ -418,4 +418,4 @@ class CallingConvention(object):
else:
func_obj = func.handle
out_var = core.BNGetParameterVariableForIncomingVariable(self.handle, in_buf, func_obj)
- return function.Variable(func, out_var.type, out_var.index, out_var.storage)
+ return binaryninja.function.Variable(func, out_var.type, out_var.index, out_var.storage)
diff --git a/python/databuffer.py b/python/databuffer.py
index 3f9e4ce5..2fb5382d 100644
--- a/python/databuffer.py
+++ b/python/databuffer.py
@@ -21,11 +21,21 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
+from binaryninja import _binaryninjacore as core
+
+# 2-3 compatibility
+from binaryninja import pyNativeStr
class DataBuffer(object):
def __init__(self, contents="", handle=None):
+
+ # python3 no longer has longs
+ try:
+ long
+ except NameError:
+ long = int
+
if handle is not None:
self.handle = core.handle_of_type(handle, core.BNDataBuffer)
elif isinstance(contents, int) or isinstance(contents, long):
@@ -44,7 +54,7 @@ class DataBuffer(object):
def __getitem__(self, i):
if isinstance(i, tuple):
result = ""
- source = str(self)
+ source = bytes(self)
for s in i:
result += source[s]
return result
@@ -59,7 +69,7 @@ class DataBuffer(object):
ctypes.memmove(buf, core.BNGetDataBufferContentsAt(self.handle, start), stop - start)
return buf.raw
else:
- return str(self)[i]
+ return bytes(self)[i]
elif i < 0:
if i >= -len(self):
return chr(core.BNGetDataBufferByte(self.handle, int(len(self) + i)))
@@ -79,7 +89,7 @@ class DataBuffer(object):
if stop < start:
stop = start
if len(value) != (stop - start):
- data = str(self)
+ data = bytes(self)
data = data[0:start] + value + data[stop:]
core.BNSetDataBufferContents(self.handle, data, len(data))
else:
@@ -107,22 +117,24 @@ class DataBuffer(object):
def __str__(self):
buf = ctypes.create_string_buffer(len(self))
ctypes.memmove(buf, core.BNGetDataBufferContents(self.handle), len(self))
- return buf.raw
+ return pyNativeStr(buf.raw)
- def __repr__(self):
- return repr(str(self))
+ def __bytes__(self):
+ buf = ctypes.create_string_buffer(len(self))
+ ctypes.memmove(buf, core.BNGetDataBufferContents(self.handle), len(self))
+ return buf.raw
def escape(self):
return core.BNDataBufferToEscapedString(self.handle)
def unescape(self):
- return DataBuffer(handle=core.BNDecodeEscapedString(str(self)))
+ return DataBuffer(handle=core.BNDecodeEscapedString(bytes(self)))
def base64_encode(self):
return core.BNDataBufferToBase64(self.handle)
def base64_decode(self):
- return DataBuffer(handle = core.BNDecodeBase64(str(self)))
+ return DataBuffer(handle = core.BNDecodeBase64(bytes(self)))
def zlib_compress(self):
buf = core.BNZlibCompress(self.handle)
diff --git a/python/demangle.py b/python/demangle.py
index 11673263..2fa1028f 100644
--- a/python/demangle.py
+++ b/python/demangle.py
@@ -21,8 +21,12 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-import types
+from binaryninja import _binaryninjacore as core
+from binaryninja import types
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import pyNativeStr
def get_qualified_name(names):
@@ -61,8 +65,8 @@ def demangle_ms(arch, mangled_name):
outSize = ctypes.c_ulonglong()
names = []
if core.BNDemangleMS(arch.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize)):
- for i in xrange(outSize.value):
- names.append(outName[i])
+ for i in range(outSize.value):
+ names.append(pyNativeStr(outName[i]))
core.BNFreeDemangledName(ctypes.byref(outName), outSize.value)
return (types.Type(handle), names)
return (None, mangled_name)
@@ -74,8 +78,8 @@ def demangle_gnu3(arch, mangled_name):
outSize = ctypes.c_ulonglong()
names = []
if core.BNDemangleGNU3(arch.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize)):
- for i in xrange(outSize.value):
- names.append(outName[i])
+ for i in range(outSize.value):
+ names.append(pyNativeStr(outName[i]))
core.BNFreeDemangledName(ctypes.byref(outName), outSize.value)
if not handle:
return (None, names)
diff --git a/python/downloadprovider.py b/python/downloadprovider.py
new file mode 100644
index 00000000..14368ee7
--- /dev/null
+++ b/python/downloadprovider.py
@@ -0,0 +1,271 @@
+# Copyright (c) 2015-2018 Vector 35 LLC
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to
+# deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
+
+import abc
+import ctypes
+import sys
+import traceback
+
+# Binary Ninja Components
+import binaryninja._binaryninjacore as core
+from binaryninja.setting import Setting
+from binaryninja import with_metaclass
+from binaryninja import startup
+from binaryninja import log
+
+# 2-3 compatibility
+from binaryninja import pyNativeStr
+
+
+class DownloadInstance(object):
+ def __init__(self, provider, handle = None):
+ if handle is None:
+ self._cb = core.BNDownloadInstanceCallbacks()
+ self._cb.context = 0
+ self._cb.destroyInstance = self._cb.destroyInstance.__class__(self._destroy_instance)
+ self._cb.performRequest = self._cb.performRequest.__class__(self._perform_request)
+ self.handle = core.BNInitDownloadInstance(provider.handle, self._cb)
+ else:
+ self.handle = core.handle_of_type(handle, core.BNDownloadInstance)
+ self._outputCallbacks = None
+
+ def __del__(self):
+ core.BNFreeDownloadInstance(self.handle)
+
+ def _destroy_instance(self, ctxt):
+ try:
+ self.perform_destroy_instance()
+ except:
+ log.log_error(traceback.format_exc())
+
+ def _perform_request(self, ctxt, url):
+ try:
+ return self.perform_request(url)
+ except:
+ log.log_error(traceback.format_exc())
+ return -1
+
+ @abc.abstractmethod
+ def perform_destroy_instance(self):
+ raise NotImplementedError
+
+ @abc.abstractmethod
+ def perform_request(self, ctxt, url):
+ raise NotImplementedError
+
+ def perform_request(self, url, callbacks):
+ return core.BNPerformDownloadRequest(self.handle, url, callbacks)
+
+
+class _DownloadProviderMetaclass(type):
+ @property
+ def list(self):
+ """List all DownloadProvider types (read-only)"""
+ startup._init_plugins()
+ count = ctypes.c_ulonglong()
+ types = core.BNGetDownloadProviderList(count)
+ result = []
+ for i in xrange(0, count.value):
+ result.append(DownloadProvider(types[i]))
+ core.BNFreeDownloadProviderList(types)
+ return result
+
+ def __iter__(self):
+ startup._init_plugins()
+ count = ctypes.c_ulonglong()
+ types = core.BNGetDownloadProviderList(count)
+ try:
+ for i in xrange(0, count.value):
+ yield DownloadProvider(types[i])
+ finally:
+ core.BNFreeDownloadProviderList(types)
+
+ def __getitem__(self, value):
+ startup._init_plugins()
+ provider = core.BNGetDownloadProviderByName(str(value))
+ if provider is None:
+ raise KeyError("'%s' is not a valid download provider" % str(value))
+ return DownloadProvider(provider)
+
+ def __setattr__(self, name, value):
+ try:
+ type.__setattr__(self, name, value)
+ except AttributeError:
+ raise AttributeError("attribute '%s' is read only" % name)
+
+
+class DownloadProvider(with_metaclass(_DownloadProviderMetaclass, object)):
+ name = None
+ instance_class = None
+ _registered_providers = []
+
+ def __init__(self, handle = None):
+ if handle is not None:
+ self.handle = core.handle_of_type(handle, core.BNDownloadProvider)
+ self.__dict__["name"] = core.BNGetDownloadProviderName(handle)
+
+ def register(self):
+ self._cb = core.BNDownloadProviderCallbacks()
+ self._cb.context = 0
+ self._cb.createInstance = self._cb.createInstance.__class__(self._create_instance)
+ self.handle = core.BNRegisterDownloadProvider(self.__class__.name, self._cb)
+ self.__class__._registered_providers.append(self)
+
+ def _create_instance(self, ctxt):
+ try:
+ result = self.__class__.instance_class(self)
+ if result is None:
+ return None
+ return ctypes.cast(core.BNNewDownloadInstanceReference(result.handle), ctypes.c_void_p).value
+ except:
+ log.log_error(traceback.format_exc())
+ return None
+
+ def create_instance(self):
+ result = core.BNCreateDownloadProviderInstance(self.handle)
+ if result is None:
+ return None
+ return DownloadInstance(self, handle = result)
+
+
+if sys.version_info >= (2, 7, 9):
+ try:
+ from urllib.request import urlopen, build_opener, install_opener, ProxyHandler
+ from urllib.error import URLError
+ except ImportError:
+ from urllib2 import urlopen, build_opener, install_opener, ProxyHandler, URLError
+
+ class PythonDownloadInstance(DownloadInstance):
+ def __init__(self, provider):
+ super(PythonDownloadInstance, self).__init__(provider)
+
+ @abc.abstractmethod
+ def perform_destroy_instance(self):
+ pass
+
+ @abc.abstractmethod
+ def perform_request(self, url):
+ try:
+ proxy_setting = Setting('download-client').get_string('https-proxy')
+ if proxy_setting:
+ opener = build_opener(ProxyHandler({'https': proxy_setting}))
+ install_opener(opener)
+
+ r = urlopen(pyNativeStr(url))
+ total_size = int(r.headers.get('content-length', 0))
+ bytes_sent = 0
+ while True:
+ data = r.read(4096)
+ if not data:
+ break
+ raw_bytes = (ctypes.c_ubyte * len(data)).from_buffer_copy(data)
+ bytes_wrote = core.BNWriteDataForDownloadInstance(self.handle, raw_bytes, len(raw_bytes))
+ if bytes_wrote != len(raw_bytes):
+ core.BNSetErrorForDownloadInstance(self.handle, "Bytes written mismatch!")
+ return -1
+ bytes_sent = bytes_sent + bytes_wrote
+ continue_download = core.BNNotifyProgressForDownloadInstance(self.handle, bytes_sent, total_size)
+ if continue_download is False:
+ core.BNSetErrorForDownloadInstance(self.handle, "Download aborted!")
+ return -1
+
+ if not bytes_sent:
+ core.BNSetErrorForDownloadInstance(self.handle, "Received no data!")
+ return -1
+
+ except URLError as e:
+ core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__)
+ return -1
+ except:
+ core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!")
+ log.log_error(traceback.format_exc())
+ return -1
+
+ return 0
+
+ class PythonDownloadProvider(DownloadProvider):
+ name = "DefaultDownloadProvider"
+ instance_class = PythonDownloadInstance
+
+ PythonDownloadProvider().register()
+else:
+ try:
+ import requests
+ from requests import pyopenssl
+ class PythonDownloadInstance(DownloadInstance):
+ def __init__(self, provider):
+ super(PythonDownloadInstance, self).__init__(provider)
+
+ @abc.abstractmethod
+ def perform_destroy_instance(self):
+ pass
+
+ @abc.abstractmethod
+ def perform_request(self, url):
+ try:
+ proxy_setting = Setting('download-client').get_string('https-proxy')
+ if proxy_setting:
+ proxies = {"https": proxy_setting}
+ else:
+ proxies = None
+
+ r = requests.get(pyNativeStr(url), proxies=proxies)
+ if not r.ok:
+ core.BNSetErrorForDownloadInstance(self.handle, "Received error from server")
+ return -1
+ data = r.content
+ if len(data) == 0:
+ core.BNSetErrorForDownloadInstance(self.handle, "No data received from server!")
+ return -1
+ raw_bytes = (ctypes.c_ubyte * len(data)).from_buffer_copy(data)
+ bytes_wrote = core.BNWriteDataForDownloadInstance(self.handle, raw_bytes, len(raw_bytes))
+ if bytes_wrote != len(raw_bytes):
+ core.BNSetErrorForDownloadInstance(self.handle, "Bytes written mismatch!")
+ return -1
+ continue_download = core.BNNotifyProgressForDownloadInstance(self.handle, bytes_wrote, bytes_wrote)
+ if continue_download is False:
+ core.BNSetErrorForDownloadInstance(self.handle, "Download aborted!")
+ return -1
+ except requests.RequestException as e:
+ core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__)
+ return -1
+ except:
+ core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!")
+ log.log_error(traceback.format_exc())
+ return -1
+
+ return 0
+
+ class PythonDownloadProvider(DownloadProvider):
+ name = "DefaultDownloadProvider"
+ instance_class = PythonDownloadInstance
+
+ PythonDownloadProvider().register()
+ except ImportError:
+ if sys.platform == "win32":
+ log.log_error("Provided Python version is too old! Only Python 2.7.10 and above are known to work on Windows!")
+ else:
+ log.log_error("On Python versions below 2.7.9, the pip requests[security] package is required for network connectivity!")
+ log.log_error("On an Ubuntu 14.04 install, the following three commands are sufficient to enable networking for the current user:")
+ log.log_error(" sudo apt install python-pip")
+ log.log_error(" python -m pip install pip --upgrade --user")
+ log.log_error(" python -m pip install requests[security] --upgrade --user")
+
diff --git a/python/examples/bin_info.py b/python/examples/bin_info.py
index 17a96685..05a72ed0 100644
--- a/python/examples/bin_info.py
+++ b/python/examples/bin_info.py
@@ -20,11 +20,15 @@
# IN THE SOFTWARE.
import sys
+
import binaryninja.log as log
from binaryninja.binaryview import BinaryViewType
import binaryninja.interaction as interaction
from binaryninja.plugin import PluginCommand
+# 2-3 compatibility
+from binaryninja import range
+
def get_bininfo(bv):
if bv is None:
@@ -48,13 +52,13 @@ def get_bininfo(bv):
contents += "| Start | Name |\n"
contents += "|------:|:-------|\n"
- for i in xrange(min(10, len(bv.functions))):
+ for i in range(min(10, len(bv.functions))):
contents += "| 0x%x | %s |\n" % (bv.functions[i].start, bv.functions[i].symbol.full_name)
contents += "### First 10 Strings ###\n"
contents += "| Start | Length | String |\n"
contents += "|------:|-------:|:-------|\n"
- for i in xrange(min(10, len(bv.strings))):
+ for i in range(min(10, len(bv.strings))):
start = bv.strings[i].start
length = bv.strings[i].length
string = bv.read(start, length)
@@ -67,6 +71,6 @@ def display_bininfo(bv):
if __name__ == "__main__":
- print get_bininfo(None)
+ print(get_bininfo(None))
else:
PluginCommand.register("Binary Info", "Display basic info about the binary", display_bininfo)
diff --git a/python/examples/breakpoint.py b/python/examples/breakpoint.py
index a2801511..cbcb86d4 100644
--- a/python/examples/breakpoint.py
+++ b/python/examples/breakpoint.py
@@ -44,7 +44,7 @@ def write_breakpoint(view, start, length):
if bkpt is None:
log_error(err)
return
- view.write(start, bkpt * length / len(bkpt))
+ view.write(start, bkpt * length // len(bkpt))
PluginCommand.register_for_range("Convert to breakpoint", "Fill region with breakpoint instructions.", write_breakpoint)
diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py
index 7814c9fd..5bb27824 100755
--- a/python/examples/export_svg.py
+++ b/python/examples/export_svg.py
@@ -48,15 +48,15 @@ def save_svg(bv, function):
def instruction_data_flow(function, address):
''' TODO: Extract data flow information '''
- length = function.view.get_instruction_length(address)
- bytes = function.view.read(address, length)
+ length = binaryninja.function.view.get_instruction_length(address)
+ bytes = binaryninja.function.view.read(address, length)
hex = bytes.encode('hex')
padded = ' '.join([hex[i:i + 2] for i in range(0, len(hex), 2)])
return 'Opcode: {bytes}'.format(bytes=padded)
def render_svg(function, origname):
- graph = function.create_graph()
+ graph = binaryninja.function.create_graph()
graph.layout_and_wait()
heightconst = 15
ratio = 0.48
diff --git a/python/examples/jump_table.py b/python/examples/jump_table.py
index 419cc188..4ed8dda2 100644
--- a/python/examples/jump_table.py
+++ b/python/examples/jump_table.py
@@ -43,16 +43,16 @@ def find_jump_table(bv, addr):
break
jump_addr += info.length
if jump_addr >= block.end:
- print "Unable to find jump after instruction 0x%x" % addr
+ print("Unable to find jump after instruction 0x%x" % addr)
continue
- print "Jump at 0x%x" % jump_addr
+ print("Jump at 0x%x" % jump_addr)
# Collect the branch targets for any tables referenced by the clicked instruction
branches = []
for token in tokens:
if InstructionTextTokenType(token.type) == InstructionTextTokenType.PossibleAddressToken: # Table addresses will be a "possible address" token
tbl = token.value
- print "Found possible table at 0x%x" % tbl
+ print("Found possible table at 0x%x" % tbl)
i = 0
while True:
# Read the next pointer from the table
@@ -66,7 +66,7 @@ def find_jump_table(bv, addr):
# If the pointer is within the binary, add it as a destination and continue
# to the next entry
if (ptr >= bv.start) and (ptr < bv.end):
- print "Found destination 0x%x" % ptr
+ print("Found destination 0x%x" % ptr)
branches.append((arch, ptr))
else:
# Once a value that is not a pointer is encountered, the jump table is ended
diff --git a/python/examples/nds.py b/python/examples/nds.py
index 34d8a292..a99303cf 100644
--- a/python/examples/nds.py
+++ b/python/examples/nds.py
@@ -27,12 +27,15 @@ from binaryninja.log import log_error
import struct
import traceback
+# 2-3 compatibility
+from binaryninja import range
+
def crc16(data):
crc = 0xffff
for ch in data:
crc ^= ord(ch)
- for bit in xrange(0, 8):
+ for bit in range(0, 8):
if (crc & 1) == 1:
crc = (crc >> 1) ^ 0xa001
else:
diff --git a/python/examples/nes.py b/python/examples/nes.py
index 00451abd..d3f75cc6 100644
--- a/python/examples/nes.py
+++ b/python/examples/nes.py
@@ -31,6 +31,10 @@ from binaryninja.log import log_error
from binaryninja.enums import (BranchType, InstructionTextTokenType,
LowLevelILOperation, LowLevelILFlagCondition, FlagRole, SegmentFlag, SymbolType)
+# 2-3 compatibility
+from binaryninja import range
+
+
InstructionNames = [
"brk", "ora", None, None, None, "ora", "asl", None, # 0x00
"php", "ora", "asl@", None, None, "ora", "asl", None, # 0x08
@@ -631,7 +635,7 @@ class NESView(BinaryView):
banks = []
-for i in xrange(0, 32):
+for i in range(0, 32):
class NESViewBank(NESView):
bank = i
name = "NES Bank %X" % i
diff --git a/python/examples/notification_callbacks.py b/python/examples/notification_callbacks.py
index b7c9cde9..02c74db9 100644
--- a/python/examples/notification_callbacks.py
+++ b/python/examples/notification_callbacks.py
@@ -1,51 +1,74 @@
-from binaryninja import BinaryDataNotification, PluginCommand, log_info
+# Copyright (c) 2015-2017 Vector 35 LLC
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to
+# deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
import inspect
+from binaryninja import BinaryDataNotification
+from binaryninja import PluginCommand
+
+
def reg_notif(view):
- demo_notification = DemoNotification(view)
- view.register_notification(demo_notification)
+ demo_notification = DemoNotification(view)
+ view.register_notification(demo_notification)
class DemoNotification(BinaryDataNotification):
- def __init__(self, view):
- self.view = view
+ def __init__(self, view):
+ self.view = view
- def data_written(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_written(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def data_inserted(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_inserted(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def data_removed(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_removed(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def function_added(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def function_added(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def function_removed(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def function_removed(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def function_updated(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def function_updated(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def data_var_added(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_var_added(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def data_var_updated(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_var_updated(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def data_var_removed(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def data_var_removed(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def string_found(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def string_found(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def string_removed(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def string_removed(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def type_defined(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def type_defined(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
- def type_undefined(self, *args):
- log_info(inspect.stack()[0][3] + str(args))
+ def type_undefined(self, *args):
+ log.log_info(inspect.stack()[0][3] + str(args))
PluginCommand.register("Register Notification", "", reg_notif)
diff --git a/python/examples/version_switcher.py b/python/examples/version_switcher.py
index 3e1cab40..c990a6c0 100644
--- a/python/examples/version_switcher.py
+++ b/python/examples/version_switcher.py
@@ -34,15 +34,15 @@ def load_channel(newchannel):
global channel
global versions
if (channel is not None and newchannel == channel.name):
- print "Same channel, not updating."
+ print("Same channel, not updating.")
else:
try:
- print "Loading channel %s" % newchannel
+ print("Loading channel %s" % newchannel)
channel = UpdateChannel[newchannel]
- print "Loading versions..."
+ print("Loading versions...")
versions = channel.versions
except Exception:
- print "%s is not a valid channel name. Defaulting to " % chandefault
+ print("%s is not a valid channel name. Defaulting to " % chandefault)
channel = UpdateChannel[chandefault]
@@ -50,12 +50,12 @@ def select(version):
done = False
date = datetime.datetime.fromtimestamp(version.time).strftime('%c')
while not done:
- print "Version:\t%s" % version.version
- print "Updated:\t%s" % date
- print "Notes:\n\n-----\n%s" % version.notes
- print "-----"
- print "\t1)\tSwitch to version"
- print "\t2)\tMain Menu"
+ print("Version:\t%s" % version.version)
+ print("Updated:\t%s" % date)
+ print("Notes:\n\n-----\n%s" % version.notes)
+ print("-----")
+ print("\t1)\tSwitch to version")
+ print("\t2)\tMain Menu")
selection = raw_input('Choice: ')
if selection.isdigit():
selection = int(selection)
@@ -65,44 +65,44 @@ def select(version):
done = True
elif (selection == 1):
if (version.version == channel.latest_version.version):
- print "Requesting update to latest version."
+ print("Requesting update to latest version.")
else:
- print "Requesting update to prior version."
+ print("Requesting update to prior version.")
if are_auto_updates_enabled():
- print "Disabling automatic updates."
+ print("Disabling automatic updates.")
set_auto_updates_enabled(False)
if (version.version == core_version):
- print "Already running %s" % version.version
+ print("Already running %s" % version.version)
else:
- print "version.version %s" % version.version
- print "core_version %s" % core_version
- print "Downloading..."
- print version.update()
- print "Installing..."
+ print("version.version %s" % version.version)
+ print("core_version %s" % core_version)
+ print("Downloading...")
+ print(version.update())
+ print("Installing...")
if is_update_installation_pending:
#note that the GUI will be launched after update but should still do the upgrade headless
install_pending_update()
# forward updating won't work without reloading
sys.exit()
else:
- print "Invalid selection"
+ print("Invalid selection")
def list_channels():
done = False
- print "\tSelect channel:\n"
+ print("\tSelect channel:\n")
while not done:
channel_list = UpdateChannel.list
for index, item in enumerate(channel_list):
- print "\t%d)\t%s" % (index + 1, item.name)
- print "\t%d)\t%s" % (len(channel_list) + 1, "Main Menu")
+ print("\t%d)\t%s" % (index + 1, item.name))
+ print("\t%d)\t%s" % (len(channel_list) + 1, "Main Menu"))
selection = raw_input('Choice: ')
if selection.isdigit():
selection = int(selection)
else:
selection = 0
if (selection <= 0 or selection > len(channel_list) + 1):
- print "%s is an invalid choice." % selection
+ print("%s is an invalid choice." % selection)
else:
done = True
if (selection != len(channel_list) + 1):
@@ -118,23 +118,23 @@ def main():
done = False
load_channel(chandefault)
while not done:
- print "\n\tBinary Ninja Version Switcher"
- print "\t\tCurrent Channel:\t%s" % channel.name
- print "\t\tCurrent Version:\t%s" % core_version
- print "\t\tAuto-Updates On:\t%s\n" % are_auto_updates_enabled()
+ print("\n\tBinary Ninja Version Switcher")
+ print("\t\tCurrent Channel:\t%s" % channel.name)
+ print("\t\tCurrent Version:\t%s" % core_version)
+ print("\t\tAuto-Updates On:\t%s\n" % are_auto_updates_enabled())
for index, version in enumerate(versions):
date = datetime.datetime.fromtimestamp(version.time).strftime('%c')
- print "\t%d)\t%s (%s)" % (index + 1, version.version, date)
- print "\t%d)\t%s" % (len(versions) + 1, "Switch Channel")
- print "\t%d)\t%s" % (len(versions) + 2, "Toggle Auto Updates")
- print "\t%d)\t%s" % (len(versions) + 3, "Exit")
+ print("\t%d)\t%s (%s)" % (index + 1, version.version, date))
+ print("\t%d)\t%s" % (len(versions) + 1, "Switch Channel"))
+ print("\t%d)\t%s" % (len(versions) + 2, "Toggle Auto Updates"))
+ print("\t%d)\t%s" % (len(versions) + 3, "Exit"))
selection = raw_input('Choice: ')
if selection.isdigit():
selection = int(selection)
else:
selection = 0
if (selection <= 0 or selection > len(versions) + 3):
- print "%d is an invalid choice.\n\n" % selection
+ print("%d is an invalid choice.\n\n" % selection)
else:
if (selection == len(versions) + 3):
done = True
diff --git a/python/fileaccessor.py b/python/fileaccessor.py
index 2c1f1d19..c2539b39 100644
--- a/python/fileaccessor.py
+++ b/python/fileaccessor.py
@@ -22,9 +22,7 @@ import traceback
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-import log
-
+from binaryninja import _binaryninjacore as core
class FileAccessor(object):
def __init__(self):
diff --git a/python/filemetadata.py b/python/filemetadata.py
index 4bfc0214..cafe1d67 100644
--- a/python/filemetadata.py
+++ b/python/filemetadata.py
@@ -18,16 +18,15 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+from __future__ import absolute_import
import traceback
import ctypes
-# Binary Ninja components
-import _binaryninjacore as core
-import startup
-import associateddatastore
-import log
-import binaryview
-
+# Binary Ninja Components
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja import associateddatastore #required for _FileMetadataAssociatedDataStore
+from binaryninja import log
class NavigationHandler(object):
def _register(self, handle):
@@ -66,12 +65,13 @@ class _FileMetadataAssociatedDataStore(associateddatastore._AssociatedDataStore)
class FileMetadata(object):
- _associated_data = {}
-
"""
``class FileMetadata`` represents the file being analyzed by Binary Ninja. It is responsible for opening,
closing, creating the database (.bndb) files, and is used to keep track of undoable actions.
"""
+
+ _associated_data = {}
+
def __init__(self, filename = None, handle = None):
"""
Instantiates a new FileMetadata class.
@@ -82,7 +82,7 @@ class FileMetadata(object):
if handle is not None:
self.handle = core.handle_of_type(handle, core.BNFileMetadata)
else:
- startup._init_plugins()
+ binaryninja._init_plugins()
self.handle = core.BNCreateFileMetadata()
if filename is not None:
core.BNSetFilename(self.handle, str(filename))
@@ -167,7 +167,7 @@ class FileMetadata(object):
view = core.BNGetFileViewOfType(self.handle, "Raw")
if view is None:
return None
- return binaryview.BinaryView(file_metadata = self, handle = view)
+ return binaryninja.binaryview.BinaryView(file_metadata = self, handle = view)
@property
def saved(self):
@@ -322,7 +322,7 @@ class FileMetadata(object):
lambda ctxt, cur, total: progress_func(cur, total)))
if view is None:
return None
- return binaryview.BinaryView(file_metadata = self, handle = view)
+ return binaryninja.binaryview.BinaryView(file_metadata = self, handle = view)
def save_auto_snapshot(self, progress_func = None):
if progress_func is None:
@@ -341,7 +341,7 @@ class FileMetadata(object):
view = core.BNCreateBinaryViewOfType(view_type, self.raw.handle)
if view is None:
return None
- return binaryview.BinaryView(file_metadata = self, handle = view)
+ return binaryninja.binaryview.BinaryView(file_metadata = self, handle = view)
def __setattr__(self, name, value):
try:
diff --git a/python/flowgraph.py b/python/flowgraph.py
index e6c98597..cf2d8e02 100644
--- a/python/flowgraph.py
+++ b/python/flowgraph.py
@@ -23,16 +23,19 @@ import threading
import traceback
# Binary Ninja components
-import _binaryninjacore as core
-from enums import (BranchType, InstructionTextTokenType, HighlightStandardColor)
-import function
-import binaryview
-import lowlevelil
-import mediumlevelil
-import basicblock
-import log
-import interaction
-import highlight
+import binaryninja
+from binaryninja.enums import (BranchType, InstructionTextTokenType, HighlightStandardColor)
+from binaryninja import _binaryninjacore as core
+from binaryninja import function
+from binaryninja import binaryview
+from binaryninja import lowlevelil
+from binaryninja import mediumlevelil
+from binaryninja import basicblock
+from binaryninja import log
+from binaryninja import highlight
+
+# 2-3 compatibility
+from binaryninja import range
class FlowGraphEdge(object):
@@ -118,7 +121,7 @@ class FlowGraphNode(object):
lines = core.BNGetFlowGraphNodeLines(self.handle, count)
block = self.basic_block
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
addr = lines[i].addr
if (lines[i].instrIndex != 0xffffffffffffffff) and (block is not None) and hasattr(block, 'il_function'):
il_instr = block.il_function[lines[i].instrIndex]
@@ -126,7 +129,7 @@ class FlowGraphNode(object):
il_instr = None
color = highlight.HighlightColor._from_core_struct(lines[i].highlight)
tokens = []
- for j in xrange(0, lines[i].count):
+ for j in range(0, lines[i].count):
token_type = InstructionTextTokenType(lines[i].tokens[j].type)
text = lines[i].tokens[j].text
value = lines[i].tokens[j].value
@@ -145,7 +148,7 @@ class FlowGraphNode(object):
if isinstance(lines, str):
lines = lines.split('\n')
line_buf = (core.BNDisassemblyTextLine * len(lines))()
- for i in xrange(0, len(lines)):
+ for i in range(0, len(lines)):
line = lines[i]
if isinstance(line, str):
line = function.DisassemblyTextLine([function.InstructionTextToken(InstructionTextTokenType.TextToken, line)])
@@ -170,7 +173,7 @@ class FlowGraphNode(object):
line_buf[i].highlight = color._get_core_struct()
line_buf[i].count = len(line.tokens)
line_buf[i].tokens = (core.BNInstructionTextToken * len(line.tokens))()
- for j in xrange(0, len(line.tokens)):
+ for j in range(0, len(line.tokens)):
line_buf[i].tokens[j].type = line.tokens[j].type
line_buf[i].tokens[j].text = line.tokens[j].text
line_buf[i].tokens[j].value = line.tokens[j].value
@@ -187,13 +190,13 @@ class FlowGraphNode(object):
count = ctypes.c_ulonglong()
edges = core.BNGetFlowGraphNodeOutgoingEdges(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
branch_type = BranchType(edges[i].type)
target = edges[i].target
if target:
target = FlowGraphNode(self.graph, core.BNNewFlowGraphNodeReference(target))
points = []
- for j in xrange(0, edges[i].pointCount):
+ for j in range(0, edges[i].pointCount):
points.append((edges[i].points[j].x, edges[i].points[j].y))
result.append(FlowGraphEdge(branch_type, self, target, points, edges[i].backEdge))
core.BNFreeFlowGraphNodeOutgoingEdgeList(edges, count.value)
@@ -235,14 +238,14 @@ class FlowGraphNode(object):
lines = core.BNGetFlowGraphNodeLines(self.handle, count)
block = self.basic_block
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
addr = lines[i].addr
if (lines[i].instrIndex != 0xffffffffffffffff) and (block is not None) and hasattr(block, 'il_function'):
il_instr = block.il_function[lines[i].instrIndex]
else:
il_instr = None
tokens = []
- for j in xrange(0, lines[i].count):
+ for j in range(0, lines[i].count):
token_type = InstructionTextTokenType(lines[i].tokens[j].type)
text = lines[i].tokens[j].text
value = lines[i].tokens[j].value
@@ -353,7 +356,7 @@ class FlowGraph(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetFlowGraphNodes(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(FlowGraphNode(self, core.BNNewFlowGraphNodeReference(blocks[i])))
core.BNFreeFlowGraphNodeList(blocks, count.value)
return result
@@ -451,7 +454,7 @@ class FlowGraph(object):
count = ctypes.c_ulonglong()
nodes = core.BNGetFlowGraphNodes(self.handle, count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield FlowGraphNode(self, core.BNNewFlowGraphNodeReference(nodes[i]))
finally:
core.BNFreeFlowGraphNodeList(nodes, count.value)
@@ -492,7 +495,7 @@ class FlowGraph(object):
count = ctypes.c_ulonglong()
nodes = core.BNGetFlowGraphNodesInRegion(self.handle, left, top, right, bottom, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(FlowGraphNode(self, core.BNNewFlowGraphNodeReference(nodes[i])))
core.BNFreeFlowGraphNodeList(nodes, count.value)
return result
@@ -507,7 +510,7 @@ class FlowGraph(object):
return FlowGraphNode(self, node)
def show(self, title):
- interaction.show_graph_report(title, self)
+ binaryninja.interaction.show_graph_report(title, self)
def update(self):
return None
diff --git a/python/function.py b/python/function.py
index f64a9677..e840dd5e 100644
--- a/python/function.py
+++ b/python/function.py
@@ -18,28 +18,25 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+from __future__ import absolute_import
import threading
import traceback
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import (FunctionGraphType, BranchType, SymbolType, InstructionTextTokenType,
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja import associateddatastore # Required in the main scope due to being an argument for _FunctionAssociatedDataStore
+from binaryninja import highlight
+from binaryninja import log
+from binaryninja import types
+from binaryninja.enums import (AnalysisSkipReason, FunctionGraphType, BranchType, SymbolType, InstructionTextTokenType,
HighlightStandardColor, HighlightColorStyle, RegisterValueType, ImplicitRegisterExtend,
DisassemblyOption, IntegerDisplayType, InstructionTextTokenContext, VariableSourceType,
FunctionAnalysisSkipOverride)
-import architecture
-import platform
-import highlight
-import associateddatastore
-import types
-import basicblock
-import lowlevelil
-import mediumlevelil
-import binaryview
-import log
-import callingconvention
-import flowgraph
+
+# 2-3 compatibility
+from binaryninja import range
class LookupTableEntry(object):
@@ -180,7 +177,7 @@ class PossibleValueSet(object):
elif value.state == RegisterValueType.SignedRangeValue:
self.offset = value.value
self.ranges = []
- for i in xrange(0, value.count):
+ for i in range(0, value.count):
start = value.ranges[i].start
end = value.ranges[i].end
step = value.ranges[i].step
@@ -192,7 +189,7 @@ class PossibleValueSet(object):
elif value.state == RegisterValueType.UnsignedRangeValue:
self.offset = value.value
self.ranges = []
- for i in xrange(0, value.count):
+ for i in range(0, value.count):
start = value.ranges[i].start
end = value.ranges[i].end
step = value.ranges[i].step
@@ -200,15 +197,15 @@ class PossibleValueSet(object):
elif value.state == RegisterValueType.LookupTableValue:
self.table = []
self.mapping = {}
- for i in xrange(0, value.count):
+ for i in range(0, value.count):
from_list = []
- for j in xrange(0, value.table[i].fromCount):
+ for j in range(0, value.table[i].fromCount):
from_list.append(value.table[i].fromValues[j])
self.mapping[value.table[i].fromValues[j]] = value.table[i].toValue
self.table.append(LookupTableEntry(from_list, value.table[i].toValue))
elif (value.state == RegisterValueType.InSetOfValues) or (value.state == RegisterValueType.NotInSetOfValues):
self.values = set()
- for i in xrange(0, value.count):
+ for i in range(0, value.count):
self.values.add(value.valueSet[i])
def __repr__(self):
@@ -421,7 +418,7 @@ class Function(object):
arch = core.BNGetFunctionArchitecture(self.handle)
if arch is None:
return None
- self._arch = architecture.CoreArchitecture._from_cache(arch)
+ self._arch = binaryninja.architecture.CoreArchitecture._from_cache(arch)
return self._arch
@property
@@ -433,7 +430,7 @@ class Function(object):
plat = core.BNGetFunctionPlatform(self.handle)
if plat is None:
return None
- self._platform = platform.Platform(None, handle = plat)
+ self._platform = binaryninja.platform.Platform(None, handle = plat)
return self._platform
@property
@@ -486,8 +483,8 @@ class Function(object):
count = ctypes.c_ulonglong()
blocks = core.BNGetFunctionBasicBlockList(self.handle, count)
result = []
- for i in xrange(0, count.value):
- result.append(basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i])))
+ for i in range(0, count.value):
+ result.append(binaryninja.basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i])))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -497,7 +494,7 @@ class Function(object):
count = ctypes.c_ulonglong()
addrs = core.BNGetCommentedAddresses(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result[addrs[i]] = self.get_comment_at(addrs[i])
core.BNFreeAddressList(addrs)
return result
@@ -505,17 +502,17 @@ class Function(object):
@property
def low_level_il(self):
"""returns LowLevelILFunction used to represent Function low level IL (read-only)"""
- return lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLowLevelIL(self.handle), self)
+ return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLowLevelIL(self.handle), self)
@property
def lifted_il(self):
"""returns LowLevelILFunction used to represent lifted IL (read-only)"""
- return lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLiftedIL(self.handle), self)
+ return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLiftedIL(self.handle), self)
@property
def medium_level_il(self):
"""Function medium level IL (read-only)"""
- return mediumlevelil.MediumLevelILFunction(self.arch, core.BNGetFunctionMediumLevelIL(self.handle), self)
+ return binaryninja.mediumlevelil.MediumLevelILFunction(self.arch, core.BNGetFunctionMediumLevelIL(self.handle), self)
@property
def function_type(self):
@@ -532,7 +529,7 @@ class Function(object):
count = ctypes.c_ulonglong()
v = core.BNGetStackLayout(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name,
types.Type(handle = core.BNNewTypeReference(v[i].type), platform = self.platform, confidence = v[i].typeConfidence)))
result.sort(key = lambda x: x.identifier)
@@ -545,7 +542,7 @@ class Function(object):
count = ctypes.c_ulonglong()
v = core.BNGetFunctionVariables(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Variable(self, v[i].var.type, v[i].var.index, v[i].var.storage, v[i].name,
types.Type(handle = core.BNNewTypeReference(v[i].type), platform = self.platform, confidence = v[i].typeConfidence)))
result.sort(key = lambda x: x.identifier)
@@ -558,8 +555,8 @@ class Function(object):
count = ctypes.c_ulonglong()
branches = core.BNGetIndirectBranches(self.handle, count)
result = []
- for i in xrange(0, count.value):
- result.append(IndirectBranchInfo(architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined))
+ for i in range(0, count.value):
+ result.append(IndirectBranchInfo(binaryninja.architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, binaryninja.architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined))
core.BNFreeIndirectBranchList(branches)
return result
@@ -579,7 +576,7 @@ class Function(object):
count = ctypes.c_ulonglong()
info = core.BNGetFunctionAnalysisPerformanceInfo(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result[info[i].name] = info[i].seconds
core.BNFreeAnalysisPerformanceInfo(info, count.value)
return result
@@ -613,7 +610,7 @@ class Function(object):
"""Registers that are used for the return value"""
result = core.BNGetFunctionReturnRegisters(self.handle)
reg_set = []
- for i in xrange(0, result.count):
+ for i in range(0, result.count):
reg_set.append(self.arch.get_reg_name(result.regs[i]))
regs = types.RegisterSet(reg_set, confidence = result.confidence)
core.BNFreeRegisterSet(result)
@@ -624,7 +621,7 @@ class Function(object):
regs = core.BNRegisterSetWithConfidence()
regs.regs = (ctypes.c_uint * len(value))()
regs.count = len(value)
- for i in xrange(0, len(value)):
+ for i in range(0, len(value)):
regs.regs[i] = self.arch.get_reg_index(value[i])
if hasattr(value, 'confidence'):
regs.confidence = value.confidence
@@ -638,7 +635,7 @@ class Function(object):
result = core.BNGetFunctionCallingConvention(self.handle)
if not result.convention:
return None
- return callingconvention.CallingConvention(None, handle = result.convention, confidence = result.confidence)
+ return binaryninja.callingconvention.CallingConvention(None, handle = result.convention, confidence = result.confidence)
@calling_convention.setter
def calling_convention(self, value):
@@ -656,7 +653,7 @@ class Function(object):
"""List of variables for the incoming function parameters"""
result = core.BNGetFunctionParameterVariables(self.handle)
var_list = []
- for i in xrange(0, result.count):
+ for i in range(0, result.count):
var_list.append(Variable(self, result.vars[i].type, result.vars[i].index, result.vars[i].storage))
confidence = result.confidence
core.BNFreeParameterVariables(result)
@@ -671,7 +668,7 @@ class Function(object):
var_conf = core.BNParameterVariablesWithConfidence()
var_conf.vars = (core.BNVariable * len(var_list))()
var_conf.count = len(var_list)
- for i in xrange(0, len(var_list)):
+ for i in range(0, len(var_list)):
var_conf.vars[i].type = var_list[i].source_type
var_conf.vars[i].index = var_list[i].index
var_conf.vars[i].storage = var_list[i].storage
@@ -721,7 +718,7 @@ class Function(object):
count = ctypes.c_ulonglong()
adjust = core.BNGetFunctionRegisterStackAdjustments(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = self.arch.get_reg_stack_name(adjust[i].regStack)
value = types.RegisterStackAdjustmentWithConfidence(adjust[i].adjustment,
confidence = adjust[i].confidence)
@@ -749,7 +746,7 @@ class Function(object):
"""Registers that are modified by this function"""
result = core.BNGetFunctionClobberedRegisters(self.handle)
reg_set = []
- for i in xrange(0, result.count):
+ for i in range(0, result.count):
reg_set.append(self.arch.get_reg_name(result.regs[i]))
regs = types.RegisterSet(reg_set, confidence = result.confidence)
core.BNFreeRegisterSet(result)
@@ -760,7 +757,7 @@ class Function(object):
regs = core.BNRegisterSetWithConfidence()
regs.regs = (ctypes.c_uint * len(value))()
regs.count = len(value)
- for i in xrange(0, len(value)):
+ for i in range(0, len(value)):
regs.regs[i] = self.arch.get_reg_index(value[i])
if hasattr(value, 'confidence'):
regs.confidence = value.confidence
@@ -829,6 +826,11 @@ class Function(object):
"""Whether automatic analysis was skipped for this function"""
return core.BNIsFunctionAnalysisSkipped(self.handle)
+ @property
+ def analysis_skip_reason(self):
+ """Function analysis skip reason"""
+ return AnalysisSkipReason(core.BNGetAnalysisSkipReason(self.handle))
+
@analysis_skipped.setter
def analysis_skipped(self, skip):
if skip:
@@ -851,14 +853,14 @@ class Function(object):
graph = core.BNGetUnresolvedStackAdjustmentGraph(self.handle)
if not graph:
return None
- return flowgraph.CoreFlowGraph(graph)
+ return binaryninja.flowgraph.CoreFlowGraph(graph)
def __iter__(self):
count = ctypes.c_ulonglong()
blocks = core.BNGetFunctionBasicBlockList(self.handle, count)
try:
- for i in xrange(0, count.value):
- yield basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i]))
+ for i in range(0, count.value):
+ yield binaryninja.basicblock.BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i]))
finally:
core.BNFreeBasicBlockList(blocks, count.value)
@@ -928,7 +930,7 @@ class Function(object):
count = ctypes.c_ulonglong()
exits = core.BNGetLowLevelILExitsForInstruction(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(exits[i])
core.BNFreeILInstructionList(exits)
return result
@@ -940,7 +942,7 @@ class Function(object):
:param int addr: virtual address of the instruction to query
:param str reg: string value of native register to query
:param Architecture arch: (optional) Architecture for the given function
- :rtype: function.RegisterValue
+ :rtype: binaryninja.function.RegisterValue
:Example:
>>> func.get_reg_value_at(0x400dbe, 'rdi')
@@ -960,7 +962,7 @@ class Function(object):
:param int addr: virtual address of the instruction to query
:param str reg: string value of native register to query
:param Architecture arch: (optional) Architecture for the given function
- :rtype: function.RegisterValue
+ :rtype: binaryninja.function.RegisterValue
:Example:
>>> func.get_reg_value_after(0x400dbe, 'rdi')
@@ -982,7 +984,7 @@ class Function(object):
:param int offset: stack offset base of stack
:param int size: size of memory to query
:param Architecture arch: (optional) Architecture for the given function
- :rtype: function.RegisterValue
+ :rtype: binaryninja.function.RegisterValue
.. note:: Stack base is zero on entry into the function unless the architecture places the return address on the
stack as in (x86/x86_64) where the stack base will start at address_size
@@ -1027,7 +1029,7 @@ class Function(object):
count = ctypes.c_ulonglong()
regs = core.BNGetRegistersReadByInstruction(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs)
return result
@@ -1038,7 +1040,7 @@ class Function(object):
count = ctypes.c_ulonglong()
regs = core.BNGetRegistersWrittenByInstruction(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(arch.get_reg_name(regs[i]))
core.BNFreeRegisterList(regs)
return result
@@ -1049,7 +1051,7 @@ class Function(object):
count = ctypes.c_ulonglong()
refs = core.BNGetStackVariablesReferencedByInstruction(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
var_type = types.Type(core.BNNewTypeReference(refs[i].type), platform = self.platform, confidence = refs[i].typeConfidence)
result.append(StackVariableReference(refs[i].sourceOperand, var_type,
refs[i].name, Variable.from_identifier(self, refs[i].varIdentifier, refs[i].name, var_type),
@@ -1063,7 +1065,7 @@ class Function(object):
count = ctypes.c_ulonglong()
refs = core.BNGetConstantsReferencedByInstruction(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(ConstantReference(refs[i].value, refs[i].size, refs[i].pointer, refs[i].intermediate))
core.BNFreeConstantReferenceList(refs)
return result
@@ -1084,7 +1086,7 @@ class Function(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetLiftedILFlagUsesForDefinition(self.handle, i, flag, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -1094,7 +1096,7 @@ class Function(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetLiftedILFlagDefinitionsForUse(self.handle, i, flag, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -1103,7 +1105,7 @@ class Function(object):
count = ctypes.c_ulonglong()
flags = core.BNGetFlagsReadByLiftedILInstruction(self.handle, i, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self.arch._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
return result
@@ -1112,7 +1114,7 @@ class Function(object):
count = ctypes.c_ulonglong()
flags = core.BNGetFlagsWrittenByLiftedILInstruction(self.handle, i, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(self.arch._flags_by_index[flags[i]])
core.BNFreeRegisterList(flags)
return result
@@ -1122,7 +1124,7 @@ class Function(object):
settings_obj = settings.handle
else:
settings_obj = None
- return flowgraph.CoreFlowGraph(core.BNCreateFunctionGraph(self.handle, graph_type, settings_obj))
+ return binaryninja.flowgraph.CoreFlowGraph(core.BNCreateFunctionGraph(self.handle, graph_type, settings_obj))
def apply_imported_types(self, sym):
core.BNApplyImportedTypes(self.handle, sym.handle)
@@ -1134,7 +1136,7 @@ class Function(object):
if source_arch is None:
source_arch = self.arch
branch_list = (core.BNArchitectureAndAddress * len(branches))()
- for i in xrange(len(branches)):
+ for i in range(len(branches)):
branch_list[i].arch = branches[i][0].handle
branch_list[i].address = branches[i][1]
core.BNSetAutoIndirectBranches(self.handle, source_arch.handle, source, branch_list, len(branches))
@@ -1143,7 +1145,7 @@ class Function(object):
if source_arch is None:
source_arch = self.arch
branch_list = (core.BNArchitectureAndAddress * len(branches))()
- for i in xrange(len(branches)):
+ for i in range(len(branches)):
branch_list[i].arch = branches[i][0].handle
branch_list[i].address = branches[i][1]
core.BNSetUserIndirectBranches(self.handle, source_arch.handle, source, branch_list, len(branches))
@@ -1154,8 +1156,8 @@ class Function(object):
count = ctypes.c_ulonglong()
branches = core.BNGetIndirectBranchesAt(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
- result.append(IndirectBranchInfo(architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined))
+ for i in range(0, count.value):
+ result.append(IndirectBranchInfo(binaryninja.architecture.CoreArchitecture._from_cache(branches[i].sourceArch), branches[i].sourceAddr, binaryninja.architecture.CoreArchitecture._from_cache(branches[i].destArch), branches[i].destAddr, branches[i].autoDefined))
core.BNFreeIndirectBranchList(branches)
return result
@@ -1165,11 +1167,13 @@ class Function(object):
count = ctypes.c_ulonglong(0)
lines = core.BNGetFunctionBlockAnnotations(self.handle, arch.handle, addr, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
tokens = []
- for j in xrange(0, lines[i].count):
+ for j in range(0, lines[i].count):
token_type = InstructionTextTokenType(lines[i].tokens[j].type)
text = lines[i].tokens[j].text
+ if not isinstance(text, str):
+ text = text.encode("charmap")
value = lines[i].tokens[j].value
size = lines[i].tokens[j].size
operand = lines[i].tokens[j].operand
@@ -1201,7 +1205,7 @@ class Function(object):
regs = core.BNRegisterSetWithConfidence()
regs.regs = (ctypes.c_uint * len(value))()
regs.count = len(value)
- for i in xrange(0, len(value)):
+ for i in range(0, len(value)):
regs.regs[i] = self.arch.get_reg_index(value[i])
if hasattr(value, 'confidence'):
regs.confidence = value.confidence
@@ -1227,7 +1231,7 @@ class Function(object):
var_conf = core.BNParameterVariablesWithConfidence()
var_conf.vars = (core.BNVariable * len(var_list))()
var_conf.count = len(var_list)
- for i in xrange(0, len(var_list)):
+ for i in range(0, len(var_list)):
var_conf.vars[i].type = var_list[i].source_type
var_conf.vars[i].index = var_list[i].index
var_conf.vars[i].storage = var_list[i].storage
@@ -1284,7 +1288,7 @@ class Function(object):
regs = core.BNRegisterSetWithConfidence()
regs.regs = (ctypes.c_uint * len(value))()
regs.count = len(value)
- for i in xrange(0, len(value)):
+ for i in range(0, len(value)):
regs.regs[i] = self.arch.get_reg_index(value[i])
if hasattr(value, 'confidence'):
regs.confidence = value.confidence
@@ -1344,7 +1348,7 @@ class Function(object):
block = core.BNGetFunctionBasicBlockAtAddress(self.handle, arch.handle, addr)
if not block:
return None
- return basicblock.BasicBlock(self._view, handle = block)
+ return binaryninja.basicblock.BasicBlock(self._view, handle = block)
def get_instr_highlight(self, addr, arch=None):
"""
@@ -1472,11 +1476,11 @@ class Function(object):
count = ctypes.c_ulonglong()
lines = core.BNGetFunctionTypeTokens(self.handle, settings, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
addr = lines[i].addr
color = highlight.HighlightColor._from_core_struct(lines[i].highlight)
tokens = []
- for j in xrange(0, lines[i].count):
+ for j in range(0, lines[i].count):
token_type = InstructionTextTokenType(lines[i].tokens[j].type)
text = lines[i].tokens[j].text
value = lines[i].tokens[j].value
@@ -1568,7 +1572,7 @@ class Function(object):
count = ctypes.c_ulonglong()
adjust = core.BNGetCallRegisterStackAdjustment(self.handle, arch.handle, addr, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result[arch.get_reg_stack_name(adjust[i].regStack)] = types.RegisterStackAdjustmentWithConfidence(
adjust[i].adjustment, confidence = adjust[i].confidence)
core.BNFreeRegisterStackAdjustments(adjust)
diff --git a/python/functionrecognizer.py b/python/functionrecognizer.py
index 4ac304ca..319bff0e 100644
--- a/python/functionrecognizer.py
+++ b/python/functionrecognizer.py
@@ -21,15 +21,15 @@
import traceback
# Binary Ninja components
-import _binaryninjacore as core
-import function
-import filemetadata
-import binaryview
-import lowlevelil
-import log
+from binaryninja import _binaryninjacore as core
+from binaryninja import function
+from binaryninja import filemetadata
+from binaryninja import binaryview
+from binaryninja import lowlevelil
class FunctionRecognizer(object):
+
_instance = None
def __init__(self):
diff --git a/python/generator.cpp b/python/generator.cpp
index f838b36d..39e45aea 100644
--- a/python/generator.cpp
+++ b/python/generator.cpp
@@ -117,7 +117,7 @@ void OutputType(FILE* out, Type* type, bool isReturnType = false, bool isCallbac
break;
}
else if ((type->GetChildType()->GetClass() == IntegerTypeClass) &&
- (type->GetChildType()->GetWidth() == 1) && (type->GetChildType()->IsSigned()))
+ (type->GetChildType()->GetWidth() == 1) && (type->GetChildType()->IsSigned()))
{
if (isReturnType)
fprintf(out, "ctypes.POINTER(ctypes.c_byte)");
@@ -173,7 +173,7 @@ int main(int argc, char* argv[])
}
bool ok = arch->GetStandalonePlatform()->ParseTypesFromSourceFile(argv[1], types, vars, funcs, errors);
- fprintf(stderr, "Errors: %s", errors.c_str());
+ fprintf(stderr, "Errors: %s\n", errors.c_str());
if (!ok)
return 1;
@@ -198,7 +198,9 @@ int main(int argc, char* argv[])
fprintf(out, "\t_base_path = os.path.join(os.path.dirname(__file__), \"..\", \"..\")\n");
fprintf(out, "\tcore = ctypes.CDLL(os.path.join(_base_path, \"binaryninjacore.dll\"))\n");
fprintf(out, "else:\n");
- fprintf(out, "\traise Exception(\"OS not supported\")\n\n");
+ fprintf(out, "\traise Exception(\"OS not supported\")\n\n\n");
+
+ fprintf(out, "from binaryninja import cstr, pyNativeStr\n\n\n");
// Create type objects
fprintf(out, "# Type definitions\n");
@@ -211,7 +213,23 @@ int main(int argc, char* argv[])
if (i.second->GetClass() == StructureTypeClass)
{
fprintf(out, "class %s(ctypes.Structure):\n", name.c_str());
- fprintf(out, "\tpass\n");
+
+ // python uses str's, C uses byte-arrays
+ bool stringField = false;
+ for (auto& arg : i.second->GetStructure()->GetMembers())
+ {
+ if ((arg.type->GetClass() == PointerTypeClass) &&
+ (arg.type->GetChildType()->GetWidth() == 1) &&
+ (arg.type->GetChildType()->IsSigned()))
+ {
+ fprintf(out, "\t@property\n\tdef %s(self):\n\t\treturn pyNativeStr(self._%s)\n", arg.name.c_str(), arg.name.c_str());
+ fprintf(out, "\t@%s.setter\n\tdef %s(self, value):\n\t\tself._%s = cstr(value)\n", arg.name.c_str(), arg.name.c_str(), arg.name.c_str());
+ stringField = true;
+ }
+ }
+
+ if (!stringField)
+ fprintf(out, "\tpass\n");
}
else if (i.second->GetClass() == EnumerationTypeClass)
{
@@ -227,7 +245,7 @@ int main(int argc, char* argv[])
}
}
else if ((i.second->GetClass() == BoolTypeClass) || (i.second->GetClass() == IntegerTypeClass) ||
- (i.second->GetClass() == FloatTypeClass) || (i.second->GetClass() == ArrayTypeClass))
+ (i.second->GetClass() == FloatTypeClass) || (i.second->GetClass() == ArrayTypeClass))
{
fprintf(out, "%s = ", name.c_str());
OutputType(out, i.second);
@@ -276,7 +294,15 @@ int main(int argc, char* argv[])
fprintf(out, "%s._fields_ = [\n", name.c_str());
for (auto& j : type->GetStructure()->GetMembers())
{
- fprintf(out, "\t\t(\"%s\", ", j.name.c_str());
+ // To help the python->C wrappers
+ if ((j.type->GetClass() == PointerTypeClass) &&
+ (j.type->GetChildType()->GetWidth() == 1) &&
+ (j.type->GetChildType()->IsSigned()))
+ {
+ fprintf(out, "\t\t(\"_%s\", ", j.name.c_str());
+ }
+ else
+ fprintf(out, "\t\t(\"%s\", ", j.name.c_str());
OutputType(out, j.type);
fprintf(out, "),\n");
}
@@ -310,6 +336,22 @@ int main(int argc, char* argv[])
(i.second->GetChildType()->GetChildType()->IsSigned());
// Pointer returns will be automatically wrapped to return None on null pointer
bool pointerResult = (i.second->GetChildType()->GetClass() == PointerTypeClass);
+
+ // From python -> C python3 requires str -> str.encode('charmap')
+ bool stringArgument = false;
+ for (auto& arg : i.second->GetParameters())
+ {
+ if ((arg.type->GetClass() == PointerTypeClass) &&
+ (arg.type->GetChildType()->GetWidth() == 1) &&
+ (arg.type->GetChildType()->IsSigned()))
+ {
+ stringArgument = true;
+ break;
+ }
+ }
+ if (name == "BNFreeString")
+ stringArgument = false;
+
bool callbackConvention = false;
if (name == "BNAllocString")
{
@@ -321,7 +363,7 @@ int main(int argc, char* argv[])
}
string funcName = name;
- if (stringResult || pointerResult)
+ if (stringResult || pointerResult || stringArgument)
funcName = string("_") + funcName;
fprintf(out, "%s = core.%s\n", funcName.c_str(), name.c_str());
@@ -349,13 +391,45 @@ int main(int argc, char* argv[])
}
fprintf(out, "\t]\n");
}
+ else
+ {
+ // As of writing this, only BNLog's have variable instruction lengths, but in an attempt not to break in the future:
+ if (funcName.compare(0, 6, "_BNLog") == 0)
+ {
+ fprintf(out, "def %s(*args):\n", name.c_str());
+ fprintf(out, "\treturn %s(*[cstr(arg) for arg in args])\n\n", funcName.c_str());
+ continue;
+ }
+ }
if (stringResult)
{
// Emit wrapper to get Python string and free native memory
fprintf(out, "def %s(*args):\n", name.c_str());
- fprintf(out, "\tresult = %s(*args)\n", funcName.c_str());
- fprintf(out, "\tstring = ctypes.cast(result, ctypes.c_char_p).value\n");
+ if (stringArgument)
+ {
+ fprintf(out, "\tresult = %s(", funcName.c_str());
+ string stringArgFuncCall = "";
+ size_t argN = 0;
+ for (auto& arg : i.second->GetParameters())
+ {
+ if ((arg.type->GetClass() == PointerTypeClass) &&
+ (arg.type->GetChildType()->GetWidth() == 1) &&
+ (arg.type->GetChildType()->IsSigned()))
+ {
+ stringArgFuncCall += "cstr(args[" + to_string(argN) + "]), ";
+ }
+ else
+ stringArgFuncCall += "args[" + to_string(argN) + "], ";
+ argN++;
+ }
+ stringArgFuncCall = stringArgFuncCall.substr(0, stringArgFuncCall.size()-2);
+ stringArgFuncCall += ")\n";
+ fprintf(out, "%s", stringArgFuncCall.c_str());
+ }
+ else
+ fprintf(out, "\tresult = %s(*args)\n", funcName.c_str());
+ fprintf(out, "\tstring = str(pyNativeStr(ctypes.cast(result, ctypes.c_char_p).value))\n");
fprintf(out, "\tBNFreeString(result)\n");
fprintf(out, "\treturn string\n");
}
@@ -363,18 +437,76 @@ int main(int argc, char* argv[])
{
// Emit wrapper to return None on null pointer
fprintf(out, "def %s(*args):\n", name.c_str());
- fprintf(out, "\tresult = %s(*args)\n", funcName.c_str());
+ if (stringArgument)
+ {
+ fprintf(out, "\tresult = %s(", funcName.c_str());
+ string stringArgFuncCall = "";
+ size_t argN = 0;
+ for (auto& arg : i.second->GetParameters())
+ {
+ if ((arg.type->GetClass() == PointerTypeClass) &&
+ (arg.type->GetChildType()->GetWidth() == 1) &&
+ (arg.type->GetChildType()->IsSigned()))
+ {
+ stringArgFuncCall += "cstr(args[" + to_string(argN) + "]), ";
+ }
+ else
+ stringArgFuncCall += "args[" + to_string(argN) + "], ";
+ argN++;
+ }
+ stringArgFuncCall = stringArgFuncCall.substr(0, stringArgFuncCall.size()-2);
+ stringArgFuncCall += ")\n";
+ fprintf(out, "%s", stringArgFuncCall.c_str());
+ }
+ else
+ fprintf(out, "\tresult = %s(*args)\n", funcName.c_str());
fprintf(out, "\tif not result:\n");
fprintf(out, "\t\treturn None\n");
fprintf(out, "\treturn result\n");
}
+ else if (stringArgument)
+ {
+ fprintf(out, "def %s(*args):\n", name.c_str());
+ {
+ fprintf(out, "\treturn %s(", funcName.c_str());
+ string stringArgFuncCall = "";
+ size_t argN = 0;
+ for (auto& arg : i.second->GetParameters())
+ {
+ if ((arg.type->GetClass() == PointerTypeClass) &&
+ (arg.type->GetChildType()->GetWidth() == 1) &&
+ (arg.type->GetChildType()->IsSigned()))
+ {
+ stringArgFuncCall += "cstr(args[" + to_string(argN) + "]), ";
+ }
+ else
+ stringArgFuncCall += "args[" + to_string(argN) + "], ";
+ argN++;
+ }
+ stringArgFuncCall = stringArgFuncCall.substr(0, stringArgFuncCall.size()-2);
+ stringArgFuncCall += ")\n";
+ fprintf(out, "%s", stringArgFuncCall.c_str());
+ }
+ }
+ fprintf(out, "\n");
}
fprintf(out, "\n# Helper functions\n");
fprintf(out, "def handle_of_type(value, handle_type):\n");
fprintf(out, "\tif isinstance(value, ctypes.POINTER(handle_type)) or isinstance(value, ctypes.c_void_p):\n");
fprintf(out, "\t\treturn ctypes.cast(value, ctypes.POINTER(handle_type))\n");
- fprintf(out, "\traise ValueError, 'expected pointer to %%s' %% str(handle_type)\n");
+ fprintf(out, "\traise ValueError('expected pointer to %%s' %% str(handle_type))\n");
+
+ // The following method is addapted from python/enum/__init__.py, lines 25-36
+ fprintf(out, "\ntry:\n");
+ fprintf(out, "\tbasestring\n");
+ fprintf(out, "\tunicode\n");
+ fprintf(out, "except NameError:\n");
+ fprintf(out, "\t# In Python 2 basestring is the ancestor of both str and unicode\n");
+ fprintf(out, "\t# in Python 3 it's just str, but was missing in 3.1\n");
+ fprintf(out, "\t# In Python 3 unicode no longer exists (it's just str)\n");
+ fprintf(out, "\tbasestring = str\n");
+ fprintf(out, "\tunicode = str\n");
fprintf(out, "\n# Set path for core plugins\n");
fprintf(out, "BNSetBundledPluginDirectory(os.path.join(_base_path, \"plugins\"))\n");
diff --git a/python/highlight.py b/python/highlight.py
index 87329202..502caa80 100644
--- a/python/highlight.py
+++ b/python/highlight.py
@@ -20,8 +20,8 @@
# Binary Ninja components
-import _binaryninjacore as core
-from enums import HighlightColorStyle, HighlightStandardColor
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import HighlightColorStyle, HighlightStandardColor
class HighlightColor(object):
diff --git a/python/interaction.py b/python/interaction.py
index b9aa0c2a..0170e6aa 100644
--- a/python/interaction.py
+++ b/python/interaction.py
@@ -22,11 +22,14 @@ import ctypes
import traceback
# Binary Ninja components
-import _binaryninjacore as core
-from enums import FormInputFieldType, MessageBoxIcon, MessageBoxButtonSet, MessageBoxButtonResult, ReportType
-import binaryview
-import log
-import flowgraph
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import FormInputFieldType, MessageBoxIcon, MessageBoxButtonSet, MessageBoxButtonResult
+from binaryninja import binaryview
+from binaryninja import log
+from binaryninja import flowgraph
+
+# 2-3 compatibility
+from binaryninja import range
class LabelField(object):
@@ -152,7 +155,11 @@ class AddressField(object):
class ChoiceField(object):
"""
``ChoiceField`` prompts the user to choose from the list of strings provided in ``choices``. Result is stored
- in self.result as an index in to the coices array.
+ in self.result as an index in to the choices array.
+
+ :attr str prompt: prompt to be presented to the user
+ :attr list(str) choices: list of choices to choose from
+
"""
def __init__(self, prompt, choices):
self.prompt = prompt
@@ -163,8 +170,8 @@ class ChoiceField(object):
value.type = FormInputFieldType.ChoiceFormField
value.prompt = self.prompt
choice_buf = (ctypes.c_char_p * len(self.choices))()
- for i in xrange(0, len(self.choices)):
- choice_buf[i] = str(self.choices[i])
+ for i in range(0, len(self.choices)):
+ choice_buf[i] = self.choices[i].encode('charmap')
value.choices = choice_buf
value.count = len(self.choices)
@@ -349,7 +356,7 @@ class InteractionHandler(object):
def _get_choice_input(self, ctxt, result, prompt, title, choice_buf, count):
try:
choices = []
- for i in xrange(0, count):
+ for i in range(0, count):
choices.append(choice_buf[i])
value = self.get_choice_input(prompt, title, choices)
if value is None:
@@ -392,7 +399,7 @@ class InteractionHandler(object):
def _get_form_input(self, ctxt, fields, count, title):
try:
field_objs = []
- for i in xrange(0, count):
+ for i in range(0, count):
if fields[i].type == FormInputFieldType.LabelFormField:
field_objs.append(LabelField(fields[i].prompt))
elif fields[i].type == FormInputFieldType.SeparatorFormField:
@@ -410,7 +417,7 @@ class InteractionHandler(object):
field_objs.append(AddressField(fields[i].prompt, view, fields[i].currentAddress))
elif fields[i].type == FormInputFieldType.ChoiceFormField:
choices = []
- for j in xrange(0, fields[i].count):
+ for j in range(0, fields[i].count):
choices.append(fields[i].choices[j])
field_objs.append(ChoiceField(fields[i].prompt, choices))
elif fields[i].type == FormInputFieldType.OpenFileNameFormField:
@@ -423,7 +430,7 @@ class InteractionHandler(object):
field_objs.append(LabelField(fields[i].prompt))
if not self.get_form_input(field_objs, title):
return False
- for i in xrange(0, count):
+ for i in range(0, count):
field_objs[i]._fill_core_result(fields[i])
return True
except:
@@ -783,8 +790,8 @@ def get_choice_input(prompt, title, choices):
0L
"""
choice_buf = (ctypes.c_char_p * len(choices))()
- for i in xrange(0, len(choices)):
- choice_buf[i] = str(choices[i])
+ for i in range(0, len(choices)):
+ choice_buf[i] = str(choices[i]).encode('charmap')
value = ctypes.c_ulonglong()
if not core.BNGetChoiceInput(value, prompt, title, choice_buf, len(choices)):
return None
@@ -839,11 +846,9 @@ def get_save_filename_input(prompt, ext="", default_name=""):
def get_directory_name_input(prompt, default_name=""):
"""
- ``get_directory_name_input`` prompts the user for a directory name to save as, optionally providing and
- default_name.
+ ``get_directory_name_input`` prompts the user for a directory name to save as, optionally providing a default_name.
- Note: This API function differently on the command line vs. the UI. In the UI a popup is used. On the commandline
- a simple text prompt is used. The ui uses the native window popup for file selection.
+ Note: This API function differently on the command line vs. the UI. In the UI a popup is used. On the commandline a simple text prompt is used. The ui uses the native window popup for file selection.
:param str prompt: Prompt to display.
:param str default_name: Optional, default directory name.
@@ -896,11 +901,11 @@ def get_form_input(fields, title):
3) Maybe
Options 1
>>> True
- >>> print tex_f.result, int_f.result, choice_f.result
+ >>> print(tex_f.result, int_f.result, choice_f.result)
Peter 1337 0
"""
value = (core.BNFormInputField * len(fields))()
- for i in xrange(0, len(fields)):
+ for i in range(0, len(fields)):
if isinstance(fields[i], str):
LabelField(fields[i])._fill_core_struct(value[i])
elif fields[i] is None:
@@ -909,7 +914,7 @@ def get_form_input(fields, title):
fields[i]._fill_core_struct(value[i])
if not core.BNGetFormInput(value, len(fields), title):
return False
- for i in xrange(0, len(fields)):
+ for i in range(0, len(fields)):
if not (isinstance(fields[i], str) or (fields[i] is None)):
fields[i]._get_result(value[i])
core.BNFreeFormInputResults(value, len(fields))
diff --git a/python/log.py b/python/log.py
index f7144183..4997b222 100644
--- a/python/log.py
+++ b/python/log.py
@@ -20,7 +20,8 @@
# Binary Ninja components
-import _binaryninjacore as core
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import LogLevel
_output_to_log = False
@@ -54,7 +55,7 @@ def log(level, text):
:param str text: message to print
:rtype: None
"""
- core.BNLog(level, "%s", str(text))
+ core.BNLog(level, '%s', text)
def log_debug(text):
@@ -69,7 +70,7 @@ def log_debug(text):
>>> log_debug("Hotdogs!")
Hotdogs!
"""
- core.BNLogDebug("%s", str(text))
+ core.BNLogDebug('%s', text)
def log_info(text):
@@ -84,7 +85,7 @@ def log_info(text):
Saucisson!
>>>
"""
- core.BNLogInfo("%s", str(text))
+ core.BNLogInfo('%s', text)
def log_warn(text):
@@ -100,7 +101,7 @@ def log_warn(text):
Chilidogs!
>>>
"""
- core.BNLogWarn("%s", str(text))
+ core.BNLogWarn('%s', text)
def log_error(text):
@@ -116,7 +117,7 @@ def log_error(text):
Spanferkel!
>>>
"""
- core.BNLogError("%s", str(text))
+ core.BNLogError('%s', text)
def log_alert(text):
@@ -132,10 +133,10 @@ def log_alert(text):
Kielbasa!
>>>
"""
- core.BNLogAlert("%s", str(text))
+ core.BNLogAlert('%s', text)
-def log_to_stdout(min_level):
+def log_to_stdout(min_level=LogLevel.InfoLog):
"""
``log_to_stdout`` redirects minimum log level to standard out.
diff --git a/python/lowlevelil.py b/python/lowlevelil.py
index ab6170a5..e714eb48 100644
--- a/python/lowlevelil.py
+++ b/python/lowlevelil.py
@@ -19,14 +19,16 @@
# IN THE SOFTWARE.
import ctypes
+import struct
# Binary Ninja components
-import _binaryninjacore as core
-from .enums import LowLevelILOperation, LowLevelILFlagCondition, InstructionTextTokenType
-import function
-import basicblock
-import mediumlevelil
-import struct
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import LowLevelILOperation, LowLevelILFlagCondition, InstructionTextTokenType
+from binaryninja import basicblock #required for LowLevelILBasicBlock
+
+# 2-3 compatibility
+from binaryninja import range
class LowLevelILLabel(object):
@@ -260,6 +262,7 @@ class LowLevelILInstruction(object):
LowLevelILOperation.LLIL_JUMP_TO: [("dest", "expr"), ("targets", "int_list")],
LowLevelILOperation.LLIL_CALL: [("dest", "expr")],
LowLevelILOperation.LLIL_CALL_STACK_ADJUST: [("dest", "expr"), ("stack_adjustment", "int"), ("reg_stack_adjustments", "reg_stack_adjust")],
+ LowLevelILOperation.LLIL_TAILCALL: [("dest", "expr")],
LowLevelILOperation.LLIL_RET: [("dest", "expr")],
LowLevelILOperation.LLIL_NORET: [],
LowLevelILOperation.LLIL_IF: [("condition", "expr"), ("true", "int"), ("false", "int")],
@@ -328,6 +331,7 @@ class LowLevelILInstruction(object):
LowLevelILOperation.LLIL_FLAG_BIT_SSA: [("src", "flag_ssa"), ("bit", "int")],
LowLevelILOperation.LLIL_CALL_SSA: [("output", "expr"), ("dest", "expr"), ("stack", "expr"), ("param", "expr")],
LowLevelILOperation.LLIL_SYSCALL_SSA: [("output", "expr"), ("stack", "expr"), ("param", "expr")],
+ LowLevelILOperation.LLIL_TAILCALL_SSA: [("output", "expr"), ("dest", "expr"), ("stack", "expr"), ("param", "expr")],
LowLevelILOperation.LLIL_CALL_OUTPUT_SSA: [("dest_memory", "int"), ("dest", "reg_ssa_list")],
LowLevelILOperation.LLIL_CALL_STACK_SSA: [("src", "reg_ssa"), ("src_memory", "int")],
LowLevelILOperation.LLIL_CALL_PARAM: [("src", "expr_list")],
@@ -412,7 +416,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
value.append(operand_list[j])
core.BNLowLevelILFreeOperandList(operand_list)
elif operand_type == "expr_list":
@@ -420,7 +424,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
value.append(LowLevelILInstruction(func, operand_list[j]))
core.BNLowLevelILFreeOperandList(operand_list)
elif operand_type == "reg_or_flag_list":
@@ -428,7 +432,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
if (operand_list[j] & (1 << 32)) != 0:
value.append(ILFlag(func.arch, operand_list[j] & 0xffffffff))
else:
@@ -439,7 +443,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
reg = operand_list[j * 2]
reg_version = operand_list[(j * 2) + 1]
value.append(SSARegister(ILRegister(func.arch, reg), reg_version))
@@ -449,7 +453,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
reg_stack = operand_list[j * 2]
reg_version = operand_list[(j * 2) + 1]
value.append(SSARegisterStack(ILRegisterStack(func.arch, reg_stack), reg_version))
@@ -459,7 +463,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
flag = operand_list[j * 2]
flag_version = operand_list[(j * 2) + 1]
value.append(SSAFlag(ILFlag(func.arch, flag), flag_version))
@@ -469,7 +473,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
if (operand_list[j * 2] & (1 << 32)) != 0:
reg_or_flag = ILFlag(func.arch, operand_list[j * 2] & 0xffffffff)
else:
@@ -482,7 +486,7 @@ class LowLevelILInstruction(object):
operand_list = core.BNLowLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = {}
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
reg_stack = operand_list[j * 2]
adjust = operand_list[(j * 2) + 1]
if adjust & 0x80000000:
@@ -519,7 +523,7 @@ class LowLevelILInstruction(object):
self.expr_index, tokens, count):
return None
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -528,7 +532,7 @@ class LowLevelILInstruction(object):
context = tokens[i].context
confidence = tokens[i].confidence
address = tokens[i].address
- result.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
core.BNFreeInstructionText(tokens, count.value)
return result
@@ -550,7 +554,7 @@ class LowLevelILInstruction(object):
expr = self.function.get_medium_level_il_expr_index(self.expr_index)
if expr is None:
return None
- return mediumlevelil.MediumLevelILInstruction(self.function.medium_level_il, expr)
+ return binaryninja.mediumlevelil.MediumLevelILInstruction(self.function.medium_level_il, expr)
@property
def mapped_medium_level_il(self):
@@ -558,20 +562,20 @@ class LowLevelILInstruction(object):
expr = self.function.get_mapped_medium_level_il_expr_index(self.expr_index)
if expr is None:
return None
- return mediumlevelil.MediumLevelILInstruction(self.function.mapped_medium_level_il, expr)
+ return binaryninja.mediumlevelil.MediumLevelILInstruction(self.function.mapped_medium_level_il, expr)
@property
def value(self):
"""Value of expression if constant or a known value (read-only)"""
value = core.BNGetLowLevelILExprValue(self.function.handle, self.expr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
@property
def possible_values(self):
"""Possible values of expression using path-sensitive static data flow analysis (read-only)"""
value = core.BNGetLowLevelILPossibleExprValues(self.function.handle, self.expr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
@@ -601,74 +605,74 @@ class LowLevelILInstruction(object):
def get_reg_value(self, reg):
reg = self.function.arch.get_reg_index(reg)
value = core.BNGetLowLevelILRegisterValueAtInstruction(self.function.handle, reg, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_reg_value_after(self, reg):
reg = self.function.arch.get_reg_index(reg)
value = core.BNGetLowLevelILRegisterValueAfterInstruction(self.function.handle, reg, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_possible_reg_values(self, reg):
reg = self.function.arch.get_reg_index(reg)
value = core.BNGetLowLevelILPossibleRegisterValuesAtInstruction(self.function.handle, reg, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
def get_possible_reg_values_after(self, reg):
reg = self.function.arch.get_reg_index(reg)
value = core.BNGetLowLevelILPossibleRegisterValuesAfterInstruction(self.function.handle, reg, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
def get_flag_value(self, flag):
flag = self.function.arch.get_flag_index(flag)
value = core.BNGetLowLevelILFlagValueAtInstruction(self.function.handle, flag, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_flag_value_after(self, flag):
flag = self.function.arch.get_flag_index(flag)
value = core.BNGetLowLevelILFlagValueAfterInstruction(self.function.handle, flag, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_possible_flag_values(self, flag):
flag = self.function.arch.get_flag_index(flag)
value = core.BNGetLowLevelILPossibleFlagValuesAtInstruction(self.function.handle, flag, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
def get_possible_flag_values_after(self, flag):
flag = self.function.arch.get_flag_index(flag)
value = core.BNGetLowLevelILPossibleFlagValuesAfterInstruction(self.function.handle, flag, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
def get_stack_contents(self, offset, size):
value = core.BNGetLowLevelILStackContentsAtInstruction(self.function.handle, offset, size, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_stack_contents_after(self, offset, size):
value = core.BNGetLowLevelILStackContentsAfterInstruction(self.function.handle, offset, size, self.instr_index)
- result = function.RegisterValue(self.function.arch, value)
+ result = binaryninja.function.RegisterValue(self.function.arch, value)
return result
def get_possible_stack_contents(self, offset, size):
value = core.BNGetLowLevelILPossibleStackContentsAtInstruction(self.function.handle, offset, size, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
def get_possible_stack_contents_after(self, offset, size):
value = core.BNGetLowLevelILPossibleStackContentsAfterInstruction(self.function.handle, offset, size, self.instr_index)
- result = function.PossibleValueSet(self.function.arch, value)
+ result = binaryninja.function.PossibleValueSet(self.function.arch, value)
core.BNFreePossibleValueSet(value)
return result
@@ -692,7 +696,7 @@ class LowLevelILExpr(object):
class LowLevelILFunction(object):
"""
- ``class LowLevelILFunction`` contains the list of LowLevelILExpr objects that make up a function. LowLevelILExpr
+ ``class LowLevelILFunction`` contains the list of LowLevelILExpr objects that make up a binaryninja.function. LowLevelILExpr
objects can be added to the LowLevelILFunction by calling ``append`` and passing the result of the various class
methods which return LowLevelILExpr objects.
@@ -775,7 +779,7 @@ class LowLevelILFunction(object):
view = None
if self.source_function is not None:
view = self.source_function.view
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(LowLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -802,7 +806,7 @@ class LowLevelILFunction(object):
result = core.BNGetMediumLevelILForLowLevelIL(self.handle)
if not result:
return None
- return mediumlevelil.MediumLevelILFunction(self.arch, result, self.source_function)
+ return binaryninja.mediumlevelil.MediumLevelILFunction(self.arch, result, self.source_function)
@property
def mapped_medium_level_il(self):
@@ -812,7 +816,7 @@ class LowLevelILFunction(object):
result = core.BNGetMappedMediumLevelIL(self.handle)
if not result:
return None
- return mediumlevelil.MediumLevelILFunction(self.arch, result, self.source_function)
+ return binaryninja.mediumlevelil.MediumLevelILFunction(self.arch, result, self.source_function)
def __setattr__(self, name, value):
try:
@@ -842,7 +846,7 @@ class LowLevelILFunction(object):
if self.source_function is not None:
view = self.source_function.view
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield LowLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self)
finally:
core.BNFreeBasicBlockList(blocks, count.value)
@@ -860,7 +864,7 @@ class LowLevelILFunction(object):
def set_indirect_branches(self, branches):
branch_list = (core.BNArchitectureAndAddress * len(branches))()
- for i in xrange(len(branches)):
+ for i in range(len(branches)):
branch_list[i].arch = branches[i][0].handle
branch_list[i].address = branches[i][1]
core.BNLowLevelILSetIndirectBranches(self.handle, branch_list, len(branches))
@@ -1592,10 +1596,20 @@ class LowLevelILFunction(object):
"""
return self.expr(LowLevelILOperation.LLIL_CALL_STACK_ADJUST, dest.index, stack_adjust)
+ def tailcall(self, dest):
+ """
+ ``tailcall`` returns an expression which jumps (branches) to the expression ``dest``
+
+ :param LowLevelILExpr dest: the expression to jump to
+ :return: The expression ``tailcall(dest)``
+ :rtype: LowLevelILExpr
+ """
+ return self.expr(LowLevelILOperation.LLIL_TAILCALL, dest.index)
+
def ret(self, dest):
"""
``ret`` returns an expression which jumps (branches) to the expression ``dest``. ``ret`` is a special alias for
- jump that makes the disassembler top disassembling.
+ jump that makes the disassembler stop disassembling.
:param LowLevelILExpr dest: the expression to jump to
:return: The expression ``jump(dest)``
@@ -1797,8 +1811,9 @@ class LowLevelILFunction(object):
param_list = []
for param in params:
param_list.append(param.index)
- return self.expr(LowLevelILOperation.LLIL_INTRINSIC, len(outputs), self.add_operand_list(output_list),
- self.arch.get_intrinsic_index(intrinsic), len(params), self.add_operand_list(param_list), flags = flags)
+ call_param = self.expr(LowLevelILOperation.LLIL_CALL_PARAM, len(params), self.add_operand_list(param_list).index)
+ return self.expr(LowLevelILOperation.LLIL_INTRINSIC, len(outputs), self.add_operand_list(output_list).index,
+ self.arch.get_intrinsic_index(intrinsic), call_param.index, flags = flags)
def breakpoint(self):
"""
@@ -2165,7 +2180,7 @@ class LowLevelILFunction(object):
:rtype: LowLevelILExpr
"""
label_list = (ctypes.POINTER(core.BNLowLevelILLabel) * len(labels))()
- for i in xrange(len(labels)):
+ for i in range(len(labels)):
label_list[i] = labels[i].handle
return LowLevelILExpr(core.BNLowLevelILAddLabelList(self.handle, label_list, len(labels)))
@@ -2178,7 +2193,7 @@ class LowLevelILFunction(object):
:rtype: LowLevelILExpr
"""
operand_list = (ctypes.c_ulonglong * len(operands))()
- for i in xrange(len(operands)):
+ for i in range(len(operands)):
operand_list[i] = operands[i]
return LowLevelILExpr(core.BNLowLevelILAddOperandList(self.handle, operand_list, len(operands)))
@@ -2261,7 +2276,7 @@ class LowLevelILFunction(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetLowLevelILSSARegisterUses(self.handle, reg, reg_ssa.version, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -2271,7 +2286,7 @@ class LowLevelILFunction(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetLowLevelILSSAFlagUses(self.handle, flag, flag_ssa.version, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -2280,7 +2295,7 @@ class LowLevelILFunction(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetLowLevelILSSAMemoryUses(self.handle, index, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -2288,13 +2303,13 @@ class LowLevelILFunction(object):
def get_ssa_reg_value(self, reg_ssa):
reg = self.arch.get_reg_index(reg_ssa.reg)
value = core.BNGetLowLevelILSSARegisterValue(self.handle, reg, reg_ssa.version)
- result = function.RegisterValue(self.arch, value)
+ result = binaryninja.function.RegisterValue(self.arch, value)
return result
def get_ssa_flag_value(self, flag_ssa):
flag = self.arch.get_flag_index(flag_ssa.flag)
value = core.BNGetLowLevelILSSAFlagValue(self.handle, flag, flag_ssa.version)
- result = function.RegisterValue(self.arch, value)
+ result = binaryninja.function.RegisterValue(self.arch, value)
return result
def get_medium_level_il_instruction_index(self, instr):
@@ -2340,7 +2355,7 @@ class LowLevelILBasicBlock(basicblock.BasicBlock):
self.il_function = owner
def __iter__(self):
- for idx in xrange(self.start, self.end):
+ for idx in range(self.start, self.end):
yield self.il_function[idx]
def __getitem__(self, idx):
diff --git a/python/mainthread.py b/python/mainthread.py
index 3e12bd65..5b0cd53c 100644
--- a/python/mainthread.py
+++ b/python/mainthread.py
@@ -19,8 +19,8 @@
# IN THE SOFTWARE.
# Binary Ninja components
-import _binaryninjacore as core
-import scriptingprovider
+from binaryninja import _binaryninjacore as core
+from binaryninja import scriptingprovider
def execute_on_main_thread(func):
diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py
index d87b5bf7..cfa8f900 100644
--- a/python/mediumlevelil.py
+++ b/python/mediumlevelil.py
@@ -19,15 +19,18 @@
# IN THE SOFTWARE.
import ctypes
+import struct
# Binary Ninja components
-import _binaryninjacore as core
-from .enums import MediumLevelILOperation, InstructionTextTokenType, ILBranchDependence
-import function
-import basicblock
-import lowlevelil
-import types
-import struct
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import MediumLevelILOperation, InstructionTextTokenType, ILBranchDependence
+from binaryninja import basicblock #required for MediumLevelILBasicBlock argument
+from binaryninja import function
+from binaryninja import types
+from binaryninja import lowlevelil
+
+# 2-3 compatibility
+from binaryninja import range
class SSAVariable(object):
@@ -148,6 +151,8 @@ class MediumLevelILInstruction(object):
MediumLevelILOperation.MLIL_ADD_OVERFLOW: [("left", "expr"), ("right", "expr")],
MediumLevelILOperation.MLIL_SYSCALL: [("output", "var_list"), ("params", "expr_list")],
MediumLevelILOperation.MLIL_SYSCALL_UNTYPED: [("output", "expr"), ("params", "expr"), ("stack", "expr")],
+ MediumLevelILOperation.MLIL_TAILCALL: [("output", "var_list"), ("dest", "expr"), ("params", "expr_list")],
+ MediumLevelILOperation.MLIL_TAILCALL_UNTYPED: [("output", "expr"), ("dest", "expr"), ("params", "expr"), ("stack", "expr")],
MediumLevelILOperation.MLIL_BP: [],
MediumLevelILOperation.MLIL_TRAP: [("vector", "int")],
MediumLevelILOperation.MLIL_INTRINSIC: [("output", "var_list"), ("intrinsic", "intrinsic"), ("params", "expr_list")],
@@ -193,6 +198,8 @@ class MediumLevelILInstruction(object):
MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA: [("output", "expr"), ("dest", "expr"), ("params", "expr"), ("stack", "expr")],
MediumLevelILOperation.MLIL_SYSCALL_SSA: [("output", "expr"), ("params", "expr_list"), ("src_memory", "int")],
MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA: [("output", "expr"), ("params", "expr"), ("stack", "expr")],
+ MediumLevelILOperation.MLIL_TAILCALL_SSA: [("output", "expr"), ("dest", "expr"), ("params", "expr_list"), ("src_memory", "int")],
+ MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA: [("output", "expr"), ("dest", "expr"), ("params", "expr"), ("stack", "expr")],
MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA: [("dest_memory", "int"), ("dest", "var_ssa_list")],
MediumLevelILOperation.MLIL_CALL_PARAM_SSA: [("src_memory", "int"), ("src", "var_ssa_list")],
MediumLevelILOperation.MLIL_LOAD_SSA: [("src", "expr"), ("src_memory", "int")],
@@ -252,7 +259,7 @@ class MediumLevelILInstruction(object):
count = ctypes.c_ulonglong()
operand_list = core.BNMediumLevelILGetOperandList(func.handle, self.expr_index, i, count)
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
value.append(operand_list[j])
core.BNMediumLevelILFreeOperandList(operand_list)
elif operand_type == "var_list":
@@ -260,7 +267,7 @@ class MediumLevelILInstruction(object):
operand_list = core.BNMediumLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
value.append(function.Variable.from_identifier(self.function.source_function, operand_list[j]))
core.BNMediumLevelILFreeOperandList(operand_list)
elif operand_type == "var_ssa_list":
@@ -268,7 +275,7 @@ class MediumLevelILInstruction(object):
operand_list = core.BNMediumLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value / 2):
+ for j in range(count.value // 2):
var_id = operand_list[j * 2]
var_version = operand_list[(j * 2) + 1]
value.append(SSAVariable(function.Variable.from_identifier(self.function.source_function,
@@ -279,7 +286,7 @@ class MediumLevelILInstruction(object):
operand_list = core.BNMediumLevelILGetOperandList(func.handle, self.expr_index, i, count)
i += 1
value = []
- for j in xrange(count.value):
+ for j in range(count.value):
value.append(MediumLevelILInstruction(func, operand_list[j]))
core.BNMediumLevelILFreeOperandList(operand_list)
self.operands.append(value)
@@ -313,7 +320,7 @@ class MediumLevelILInstruction(object):
self.expr_index, tokens, count):
return None
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -359,7 +366,7 @@ class MediumLevelILInstruction(object):
count = ctypes.c_ulonglong()
deps = core.BNGetAllMediumLevelILBranchDependence(self.function.handle, self.instr_index, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result[deps[i].branch] = ILBranchDependence(deps[i].dependence)
core.BNFreeILBranchDependenceList(deps)
return result
@@ -410,11 +417,12 @@ class MediumLevelILInstruction(object):
return [self.dest]
elif self.operation in [MediumLevelILOperation.MLIL_SET_VAR_SPLIT, MediumLevelILOperation.MLIL_SET_VAR_SPLIT_SSA]:
return [self.high, self.low]
- elif self.operation in [MediumLevelILOperation.MLIL_CALL, MediumLevelILOperation.MLIL_SYSCALL]:
+ elif self.operation in [MediumLevelILOperation.MLIL_CALL, MediumLevelILOperation.MLIL_SYSCALL, MediumLevelILOperation.MLIL_TAILCALL]:
return self.output
- elif self.operation in [MediumLevelILOperation.MLIL_CALL_UNTYPED, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED,
+ elif self.operation in [MediumLevelILOperation.MLIL_CALL_UNTYPED, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED, MediumLevelILOperation.MLIL_TAILCALL_UNTYPED,
MediumLevelILOperation.MLIL_CALL_SSA, MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA,
- MediumLevelILOperation.MLIL_SYSCALL_SSA, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA]:
+ MediumLevelILOperation.MLIL_SYSCALL_SSA, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA,
+ MediumLevelILOperation.MLIL_TAILCALL_SSA, MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA]:
return self.output.vars_written
elif self.operation in [MediumLevelILOperation.MLIL_CALL_OUTPUT, MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA]:
return self.dest
@@ -430,14 +438,14 @@ class MediumLevelILInstruction(object):
elif self.operation in [MediumLevelILOperation.MLIL_SET_VAR_SSA_FIELD,
MediumLevelILOperation.MLIL_SET_VAR_ALIASED_FIELD]:
return [self.prev] + self.src.vars_read
- elif self.operation in [MediumLevelILOperation.MLIL_CALL, MediumLevelILOperation.MLIL_SYSCALL,
- MediumLevelILOperation.MLIL_CALL_SSA, MediumLevelILOperation.MLIL_SYSCALL_SSA]:
+ elif self.operation in [MediumLevelILOperation.MLIL_CALL, MediumLevelILOperation.MLIL_SYSCALL, MediumLevelILOperation.MLIL_TAILCALL,
+ MediumLevelILOperation.MLIL_CALL_SSA, MediumLevelILOperation.MLIL_SYSCALL_SSA, MediumLevelILOperation.MLIL_TAILCALL_SSA]:
result = []
for param in self.params:
result += param.vars_read
return result
- elif self.operation in [MediumLevelILOperation.MLIL_CALL_UNTYPED, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED,
- MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA]:
+ elif self.operation in [MediumLevelILOperation.MLIL_CALL_UNTYPED, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED, MediumLevelILOperation.MLIL_TAILCALL_UNTYPED,
+ MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA, MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA, MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA]:
return self.params.vars_read
elif self.operation in [MediumLevelILOperation.MLIL_CALL_PARAM, MediumLevelILOperation.MLIL_CALL_PARAM_SSA,
MediumLevelILOperation.MLIL_VAR_PHI]:
@@ -591,7 +599,7 @@ class MediumLevelILExpr(object):
class MediumLevelILFunction(object):
"""
- ``class MediumLevelILFunction`` contains the list of MediumLevelILExpr objects that make up a function. MediumLevelILExpr
+ ``class MediumLevelILFunction`` contains the list of MediumLevelILExpr objects that make up a binaryninja.function. MediumLevelILExpr
objects can be added to the MediumLevelILFunction by calling ``append`` and passing the result of the various class
methods which return MediumLevelILExpr objects.
"""
@@ -642,7 +650,7 @@ class MediumLevelILFunction(object):
view = None
if self.source_function is not None:
view = self.source_function.view
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(MediumLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self))
core.BNFreeBasicBlockList(blocks, count.value)
return result
@@ -699,7 +707,7 @@ class MediumLevelILFunction(object):
if self.source_function is not None:
view = self.source_function.view
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield MediumLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self)
finally:
core.BNFreeBasicBlockList(blocks, count.value)
@@ -770,7 +778,7 @@ class MediumLevelILFunction(object):
:rtype: MediumLevelILExpr
"""
label_list = (ctypes.POINTER(core.BNMediumLevelILLabel) * len(labels))()
- for i in xrange(len(labels)):
+ for i in range(len(labels)):
label_list[i] = labels[i].handle
return MediumLevelILExpr(core.BNMediumLevelILAddLabelList(self.handle, label_list, len(labels)))
@@ -783,7 +791,7 @@ class MediumLevelILFunction(object):
:rtype: MediumLevelILExpr
"""
operand_list = (ctypes.c_ulonglong * len(operands))()
- for i in xrange(len(operands)):
+ for i in range(len(operands)):
operand_list[i] = operands[i]
return MediumLevelILExpr(core.BNMediumLevelILAddOperandList(self.handle, operand_list, len(operands)))
@@ -837,7 +845,7 @@ class MediumLevelILFunction(object):
var_data.storage = ssa_var.var.storage
instrs = core.BNGetMediumLevelILSSAVarUses(self.handle, var_data, ssa_var.version, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -846,7 +854,7 @@ class MediumLevelILFunction(object):
count = ctypes.c_ulonglong()
instrs = core.BNGetMediumLevelILSSAMemoryUses(self.handle, version, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -873,7 +881,7 @@ class MediumLevelILFunction(object):
var_data.storage = var.storage
instrs = core.BNGetMediumLevelILVariableDefinitions(self.handle, var_data, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -886,7 +894,7 @@ class MediumLevelILFunction(object):
var_data.storage = var.storage
instrs = core.BNGetMediumLevelILVariableUses(self.handle, var_data, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(instrs[i])
core.BNFreeILInstructionList(instrs)
return result
@@ -931,7 +939,7 @@ class MediumLevelILBasicBlock(basicblock.BasicBlock):
self.il_function = owner
def __iter__(self):
- for idx in xrange(self.start, self.end):
+ for idx in range(self.start, self.end):
yield self.il_function[idx]
def __getitem__(self, idx):
diff --git a/python/metadata.py b/python/metadata.py
index 554bbcf4..606e4ceb 100644
--- a/python/metadata.py
+++ b/python/metadata.py
@@ -19,11 +19,16 @@
# IN THE SOFTWARE.
+from __future__ import absolute_import
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import MetadataType
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import MetadataType
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import pyNativeStr
class Metadata(object):
@@ -39,7 +44,7 @@ class Metadata(object):
self.handle = core.BNCreateMetadataBooleanData(value)
elif isinstance(value, str):
if raw:
- buffer = (ctypes.c_ubyte * len(value)).from_buffer_copy(value)
+ buffer = (ctypes.c_ubyte * len(value)).from_buffer_copy(value.encode('charmap'))
self.handle = core.BNCreateMetadataRawData(buffer, len(value))
else:
self.handle = core.BNCreateMetadataStringData(value)
@@ -137,13 +142,16 @@ class Metadata(object):
def __iter__(self):
if self.is_array:
- for i in xrange(core.BNMetadataSize(self.handle)):
+ for i in range(core.BNMetadataSize(self.handle)):
yield Metadata(handle=core.BNMetadataGetForIndex(self.handle, i)).value
elif self.is_dict:
result = core.BNMetadataGetValueStore(self.handle)
try:
- for i in xrange(result.contents.size):
- yield result.contents.keys[i]
+ for i in range(result.contents.size):
+ if isinstance(result.contents.keys[i], bytes):
+ yield str(pyNativeStr(result.contents.keys[i]))
+ else:
+ yield result.contents.keys[i]
finally:
core.BNFreeMetadataValueStore(result)
else:
@@ -168,13 +176,13 @@ class Metadata(object):
def __str__(self):
if self.is_string:
- return core.BNMetadataGetString(self.handle)
+ return str(core.BNMetadataGetString(self.handle))
if self.is_raw:
length = ctypes.c_ulonglong()
length.value = 0
native_list = core.BNMetadataGetRaw(self.handle, ctypes.byref(length))
out_list = []
- for i in xrange(length.value):
+ for i in range(length.value):
out_list.append(native_list[i])
core.BNFreeMetadataRaw(native_list)
return ''.join(chr(a) for a in out_list)
diff --git a/python/platform.py b/python/platform.py
index a79e3b9a..9a5f70df 100644
--- a/python/platform.py
+++ b/python/platform.py
@@ -21,42 +21,45 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-import startup
-import architecture
-import callingconvention
-import types
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja import types
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class _PlatformMetaClass(type):
+
@property
def list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
platforms = core.BNGetPlatformList(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Platform(None, core.BNNewPlatformReference(platforms[i])))
core.BNFreePlatformList(platforms, count.value)
return result
@property
def os_list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
platforms = core.BNGetPlatformOSList(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(str(platforms[i]))
core.BNFreePlatformOSList(platforms, count.value)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
platforms = core.BNGetPlatformList(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield Platform(None, core.BNNewPlatformReference(platforms[i]))
finally:
core.BNFreePlatformList(platforms, count.value)
@@ -68,14 +71,14 @@ class _PlatformMetaClass(type):
raise AttributeError("attribute '%s' is read only" % name)
def __getitem__(cls, value):
- startup._init_plugins()
+ binaryninja._init_plugins()
platform = core.BNGetPlatformByName(str(value))
if platform is None:
raise KeyError("'%s' is not a valid platform" % str(value))
return Platform(None, platform)
def get_list(cls, os = None, arch = None):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
if os is None:
platforms = core.BNGetPlatformList(count)
@@ -84,18 +87,17 @@ class _PlatformMetaClass(type):
else:
platforms = core.BNGetPlatformListByArchitecture(os, arch.handle)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Platform(None, core.BNNewPlatformReference(platforms[i])))
core.BNFreePlatformList(platforms, count.value)
return result
-class Platform(object):
+class Platform(with_metaclass(_PlatformMetaClass, object)):
"""
``class Platform`` contains all information releated to the execution environment of the binary, mainly the
calling conventions used.
"""
- __metaclass__ = _PlatformMetaClass
name = None
def __init__(self, arch, handle = None):
@@ -105,7 +107,7 @@ class Platform(object):
else:
self.handle = handle
self.__dict__["name"] = core.BNGetPlatformName(self.handle)
- self.arch = architecture.CoreArchitecture._from_cache(core.BNGetPlatformArchitecture(self.handle))
+ self.arch = binaryninja.architecture.CoreArchitecture._from_cache(core.BNGetPlatformArchitecture(self.handle))
def __del__(self):
core.BNFreePlatform(self.handle)
@@ -121,6 +123,11 @@ class Platform(object):
return ctypes.addressof(self.handle.contents) != ctypes.addressof(value.handle.contents)
@property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
+ @property
def default_calling_convention(self):
"""
Default calling convention.
@@ -132,7 +139,7 @@ class Platform(object):
result = core.BNGetPlatformDefaultCallingConvention(self.handle)
if result is None:
return None
- return callingconvention.CallingConvention(handle=result)
+ return binaryninja.callingconvention.CallingConvention(handle=result)
@default_calling_convention.setter
def default_calling_convention(self, value):
@@ -150,7 +157,7 @@ class Platform(object):
result = core.BNGetPlatformCdeclCallingConvention(self.handle)
if result is None:
return None
- return callingconvention.CallingConvention(handle=result)
+ return binaryninja.callingconvention.CallingConvention(handle=result)
@cdecl_calling_convention.setter
def cdecl_calling_convention(self, value):
@@ -168,7 +175,7 @@ class Platform(object):
result = core.BNGetPlatformStdcallCallingConvention(self.handle)
if result is None:
return None
- return callingconvention.CallingConvention(handle=result)
+ return binaryninja.callingconvention.CallingConvention(handle=result)
@stdcall_calling_convention.setter
def stdcall_calling_convention(self, value):
@@ -186,7 +193,7 @@ class Platform(object):
result = core.BNGetPlatformFastcallCallingConvention(self.handle)
if result is None:
return None
- return callingconvention.CallingConvention(handle=result)
+ return binaryninja.callingconvention.CallingConvention(handle=result)
@fastcall_calling_convention.setter
def fastcall_calling_convention(self, value):
@@ -204,7 +211,7 @@ class Platform(object):
result = core.BNGetPlatformSystemCallConvention(self.handle)
if result is None:
return None
- return callingconvention.CallingConvention(handle=result)
+ return binaryninja.callingconvention.CallingConvention(handle=result)
@system_call_convention.setter
def system_call_convention(self, value):
@@ -221,8 +228,8 @@ class Platform(object):
count = ctypes.c_ulonglong()
cc = core.BNGetPlatformCallingConventions(self.handle, count)
result = []
- for i in xrange(0, count.value):
- result.append(callingconvention.CallingConvention(handle=core.BNNewCallingConventionReference(cc[i])))
+ for i in range(0, count.value):
+ result.append(binaryninja.callingconvention.CallingConvention(handle=core.BNNewCallingConventionReference(cc[i])))
core.BNFreeCallingConventionList(cc, count.value)
return result
@@ -232,7 +239,7 @@ class Platform(object):
count = ctypes.c_ulonglong(0)
type_list = core.BNGetPlatformTypes(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = types.QualifiedName._from_core_struct(type_list[i].name)
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type), platform = self)
core.BNFreeTypeList(type_list, count.value)
@@ -244,7 +251,7 @@ class Platform(object):
count = ctypes.c_ulonglong(0)
type_list = core.BNGetPlatformVariables(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = types.QualifiedName._from_core_struct(type_list[i].name)
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type), platform = self)
core.BNFreeTypeList(type_list, count.value)
@@ -256,7 +263,7 @@ class Platform(object):
count = ctypes.c_ulonglong(0)
type_list = core.BNGetPlatformFunctions(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = types.QualifiedName._from_core_struct(type_list[i].name)
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type), platform = self)
core.BNFreeTypeList(type_list, count.value)
@@ -268,7 +275,7 @@ class Platform(object):
count = ctypes.c_ulonglong(0)
call_list = core.BNGetPlatformSystemCalls(self.handle, count)
result = {}
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
name = types.QualifiedName._from_core_struct(call_list[i].name)
t = types.Type(core.BNNewTypeReference(call_list[i].type), platform = self)
result[call_list[i].number] = (name, t)
@@ -383,8 +390,8 @@ class Platform(object):
if filename is None:
filename = "input"
dir_buf = (ctypes.c_char_p * len(include_dirs))()
- for i in xrange(0, len(include_dirs)):
- dir_buf[i] = str(include_dirs[i])
+ for i in range(0, len(include_dirs)):
+ dir_buf[i] = include_dirs[i].encode('charmap')
parse = core.BNTypeParserResult()
errors = ctypes.c_char_p()
result = core.BNParseTypesFromSource(self.handle, source, filename, parse, errors, dir_buf,
@@ -396,13 +403,13 @@ class Platform(object):
type_dict = {}
variables = {}
functions = {}
- for i in xrange(0, parse.typeCount):
+ 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)
- for i in xrange(0, parse.variableCount):
+ for i in range(0, parse.variableCount):
name = types.QualifiedName._from_core_struct(parse.variables[i].name)
variables[name] = types.Type(core.BNNewTypeReference(parse.variables[i].type), platform = self)
- for i in xrange(0, parse.functionCount):
+ for i in range(0, parse.functionCount):
name = types.QualifiedName._from_core_struct(parse.functions[i].name)
functions[name] = types.Type(core.BNNewTypeReference(parse.functions[i].type), platform = self)
core.BNFreeTypeParserResult(parse)
@@ -429,8 +436,8 @@ class Platform(object):
>>>
"""
dir_buf = (ctypes.c_char_p * len(include_dirs))()
- for i in xrange(0, len(include_dirs)):
- dir_buf[i] = str(include_dirs[i])
+ for i in range(0, len(include_dirs)):
+ dir_buf[i] = include_dirs[i].encode('charmap')
parse = core.BNTypeParserResult()
errors = ctypes.c_char_p()
result = core.BNParseTypesFromSourceFile(self.handle, filename, parse, errors, dir_buf,
@@ -442,13 +449,13 @@ class Platform(object):
type_dict = {}
variables = {}
functions = {}
- for i in xrange(0, parse.typeCount):
+ 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)
- for i in xrange(0, parse.variableCount):
+ for i in range(0, parse.variableCount):
name = types.QualifiedName._from_core_struct(parse.variables[i].name)
variables[name] = types.Type(core.BNNewTypeReference(parse.variables[i].type), platform = self)
- for i in xrange(0, parse.functionCount):
+ for i in range(0, parse.functionCount):
name = types.QualifiedName._from_core_struct(parse.functions[i].name)
functions[name] = types.Type(core.BNNewTypeReference(parse.functions[i].type), platform = self)
core.BNFreeTypeParserResult(parse)
diff --git a/python/plugin.py b/python/plugin.py
index c6cd9fc0..4d9add9c 100644
--- a/python/plugin.py
+++ b/python/plugin.py
@@ -23,15 +23,16 @@ import ctypes
import threading
# Binary Ninja components
-import _binaryninjacore as core
-from enums import PluginCommandType
-import startup
-import filemetadata
-import binaryview
-import function
-import log
-import lowlevelil
-import mediumlevelil
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import PluginCommandType
+from binaryninja import filemetadata
+from binaryninja import binaryview
+from binaryninja import function
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class PluginCommandContext(object):
@@ -46,21 +47,21 @@ class PluginCommandContext(object):
class _PluginCommandMetaClass(type):
@property
def list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
commands = core.BNGetAllPluginCommands(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(PluginCommand(commands[i]))
core.BNFreePluginCommandList(commands)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
commands = core.BNGetAllPluginCommands(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield PluginCommand(commands[i])
finally:
core.BNFreePluginCommandList(commands)
@@ -72,9 +73,8 @@ class _PluginCommandMetaClass(type):
raise AttributeError("attribute '%s' is read only" % name)
-class PluginCommand(object):
+class PluginCommand(with_metaclass(_PluginCommandMetaClass, object)):
_registered_commands = []
- __metaclass__ = _PluginCommandMetaClass
def __init__(self, cmd):
self.command = core.BNPluginCommand()
@@ -83,42 +83,47 @@ class PluginCommand(object):
self.description = str(cmd.description)
self.type = PluginCommandType(cmd.type)
+ @property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
@classmethod
def _default_action(cls, view, action):
try:
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
action(view_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _address_action(cls, view, addr, action):
try:
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
action(view_obj, addr)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _range_action(cls, view, addr, length, action):
try:
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
action(view_obj, addr, length)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _function_action(cls, view, func, action):
try:
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
func_obj = function.Function(view_obj, core.BNNewFunctionReference(func))
action(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _low_level_il_function_action(cls, view, func, action):
@@ -126,10 +131,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func))
- func_obj = lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
action(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _low_level_il_instruction_action(cls, view, func, instr, action):
@@ -137,10 +142,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func))
- func_obj = lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
action(view_obj, func_obj[instr])
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _medium_level_il_function_action(cls, view, func, action):
@@ -148,10 +153,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
- func_obj = mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
action(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _medium_level_il_instruction_action(cls, view, func, instr, action):
@@ -159,21 +164,21 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
- func_obj = mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
action(view_obj, func_obj[instr])
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
@classmethod
def _default_is_valid(cls, view, is_valid):
try:
if is_valid is None:
return True
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
return is_valid(view_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -181,11 +186,11 @@ class PluginCommand(object):
try:
if is_valid is None:
return True
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
return is_valid(view_obj, addr)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -193,11 +198,11 @@ class PluginCommand(object):
try:
if is_valid is None:
return True
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
return is_valid(view_obj, addr, length)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -205,12 +210,12 @@ class PluginCommand(object):
try:
if is_valid is None:
return True
- file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
- view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
+ file_metadata = binaryninja.filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
+ view_obj = binaryninja.binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
func_obj = function.Function(view_obj, core.BNNewFunctionReference(func))
return is_valid(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -221,10 +226,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func))
- func_obj = lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
return is_valid(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -235,10 +240,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func))
- func_obj = lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.lowlevelil.LowLevelILFunction(owner.arch, core.BNNewLowLevelILFunctionReference(func), owner)
return is_valid(view_obj, func_obj[instr])
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -249,10 +254,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
- func_obj = mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
return is_valid(view_obj, func_obj)
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -263,10 +268,10 @@ class PluginCommand(object):
file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
- func_obj = mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
+ func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
return is_valid(view_obj, func_obj[instr])
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
return False
@classmethod
@@ -282,7 +287,7 @@ class PluginCommand(object):
.. warning:: Calling ``register`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView))(lambda ctxt, view: cls._default_action(view, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView))(lambda ctxt, view: cls._default_is_valid(view, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -301,7 +306,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_address`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.c_ulonglong)(lambda ctxt, view, addr: cls._address_action(view, addr, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.c_ulonglong)(lambda ctxt, view, addr: cls._address_is_valid(view, addr, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -320,7 +325,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_range`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.c_ulonglong, ctypes.c_ulonglong)(lambda ctxt, view, addr, length: cls._range_action(view, addr, length, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.c_ulonglong, ctypes.c_ulonglong)(lambda ctxt, view, addr, length: cls._range_is_valid(view, addr, length, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -339,7 +344,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_function`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNFunction))(lambda ctxt, view, func: cls._function_action(view, func, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNFunction))(lambda ctxt, view, func: cls._function_is_valid(view, func, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -358,7 +363,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_low_level_il_function`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNLowLevelILFunction))(lambda ctxt, view, func: cls._low_level_il_function_action(view, func, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNLowLevelILFunction))(lambda ctxt, view, func: cls._low_level_il_function_is_valid(view, func, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -377,7 +382,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_low_level_il_instruction`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNLowLevelILFunction), ctypes.c_ulonglong)(lambda ctxt, view, func, instr: cls._low_level_il_instruction_action(view, func, instr, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNLowLevelILFunction), ctypes.c_ulonglong)(lambda ctxt, view, func, instr: cls._low_level_il_instruction_is_valid(view, func, instr, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -396,7 +401,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_medium_level_il_function`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNMediumLevelILFunction))(lambda ctxt, view, func: cls._medium_level_il_function_action(view, func, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNMediumLevelILFunction))(lambda ctxt, view, func: cls._medium_level_il_function_is_valid(view, func, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -415,7 +420,7 @@ class PluginCommand(object):
.. warning:: Calling ``register_for_medium_level_il_instruction`` with the same function name will replace the existing function but will leak the memory of the original plugin.
"""
- startup._init_plugins()
+ binaryninja._init_plugins()
action_obj = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNMediumLevelILFunction), ctypes.c_ulonglong)(lambda ctxt, view, func, instr: cls._medium_level_il_instruction_action(view, func, instr, action))
is_valid_obj = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.POINTER(core.BNBinaryView), ctypes.POINTER(core.BNMediumLevelILFunction), ctypes.c_ulonglong)(lambda ctxt, view, func, instr: cls._medium_level_il_instruction_is_valid(view, func, instr, is_valid))
cls._registered_commands.append((action_obj, is_valid_obj))
@@ -463,7 +468,7 @@ class PluginCommand(object):
elif self.command.type == PluginCommandType.LowLevelILInstructionPluginCommand:
if context.instruction is None:
return False
- if not isinstance(context.instruction, lowlevelil.LowLevelILInstruction):
+ if not isinstance(context.instruction, binaryninja.lowlevelil.LowLevelILInstruction):
return False
if not self.command.lowLevelILInstructionIsValid:
return True
@@ -478,7 +483,7 @@ class PluginCommand(object):
elif self.command.type == PluginCommandType.MediumLevelILInstructionPluginCommand:
if context.instruction is None:
return False
- if not isinstance(context.instruction, mediumlevelil.MediumLevelILInstruction):
+ if not isinstance(context.instruction, binaryninja.mediumlevelil.MediumLevelILInstruction):
return False
if not self.command.mediumLevelILInstructionIsValid:
return True
@@ -546,7 +551,7 @@ class MainThreadActionHandler(object):
try:
self.add_action(MainThreadAction(action))
except:
- log.log_error(traceback.format_exc())
+ binaryninja.log.log_error(traceback.format_exc())
def add_action(self, action):
pass
@@ -559,25 +564,23 @@ class _BackgroundTaskMetaclass(type):
count = ctypes.c_ulonglong()
tasks = core.BNGetRunningBackgroundTasks(count)
result = []
- for i in xrange(0, count.value):
- result.append(BackgroundTask(core.BNNewBackgroundTaskReference(tasks[i])))
- core.BNFreeBackgroundTaskList(tasks)
+ for i in range(0, count.value):
+ result.append(BackgroundTask(handle=core.BNNewBackgroundTaskReference(tasks[i])))
+ core.BNFreeBackgroundTaskList(tasks, count.value)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
tasks = core.BNGetRunningBackgroundTasks(count)
try:
- for i in xrange(0, count.value):
- yield BackgroundTask(core.BNNewBackgroundTaskReference(tasks[i]))
+ for i in range(0, count.value):
+ yield BackgroundTask(handle=core.BNNewBackgroundTaskReference(tasks[i]))
finally:
- core.BNFreeBackgroundTaskList(tasks)
-
+ core.BNFreeBackgroundTaskList(tasks, count.value)
-class BackgroundTask(object):
- __metaclass__ = _BackgroundTaskMetaclass
+class BackgroundTask(with_metaclass(_BackgroundTaskMetaclass, object)):
def __init__(self, initial_progress_text = "", can_cancel = False, handle = None):
if handle is None:
self.handle = core.BNBeginBackgroundTask(initial_progress_text, can_cancel)
@@ -588,6 +591,11 @@ class BackgroundTask(object):
core.BNFreeBackgroundTask(self.handle)
@property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
+ @property
def progress(self):
"""Text description of the progress of the background task (displayed in status bar of the UI)"""
return core.BNGetBackgroundTaskProgressText(self.handle)
diff --git a/python/pluginmanager.py b/python/pluginmanager.py
index 2f293577..c7a0c83a 100644
--- a/python/pluginmanager.py
+++ b/python/pluginmanager.py
@@ -21,9 +21,10 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from .enums import PluginType, PluginUpdateStatus
-import startup
+from binaryninja import _binaryninjacore as core
+
+# 2-3 compatibility
+from binaryninja import range
class RepoPlugin(object):
@@ -31,7 +32,9 @@ class RepoPlugin(object):
``RepoPlugin` is mostly read-only, however you can install/uninstall enable/disable plugins. RepoPlugins are
created by parsing the plugins.json in a plugin repository.
"""
+ from binaryninja.enums import PluginType, PluginUpdateStatus
def __init__(self, handle):
+ raise Exception("RepoPlugin temporarily disabled!")
self.handle = core.handle_of_type(handle, core.BNRepoPlugin)
def __del__(self):
@@ -110,7 +113,7 @@ class RepoPlugin(object):
result = []
count = ctypes.c_ulonglong(0)
plugintypes = core.BNPluginGetPluginTypes(self.handle, count)
- for i in xrange(count.value):
+ for i in range(count.value):
result.append(PluginType(plugintypes[i]))
core.BNFreePluginTypes(plugintypes)
return result
@@ -136,6 +139,7 @@ class Repository(object):
``Repository`` is a read-only class. Use RepositoryManager to Enable/Disable/Install/Uninstall plugins.
"""
def __init__(self, handle):
+ raise Exception("Repository temporarily disabled!")
self.handle = core.handle_of_type(handle, core.BNRepository)
def __del__(self):
@@ -181,7 +185,7 @@ class Repository(object):
pluginlist = []
count = ctypes.c_ulonglong(0)
result = core.BNRepositoryGetPlugins(self.handle, count)
- for i in xrange(count.value):
+ for i in range(count.value):
pluginlist.append(RepoPlugin(handle=result[i]))
core.BNFreeRepositoryPluginList(result, count.value)
del result
@@ -199,6 +203,7 @@ class RepositoryManager(object):
the plugins that are installed/unstalled enabled/disabled
"""
def __init__(self, handle=None):
+ raise Exception("RepositoryManager temporarily disabled!")
self.handle = core.BNGetRepositoryManager()
def __getitem__(self, repo_path):
@@ -217,7 +222,7 @@ class RepositoryManager(object):
result = []
count = ctypes.c_ulonglong(0)
repos = core.BNRepositoryManagerGetRepositories(self.handle, count)
- for i in xrange(count.value):
+ for i in range(count.value):
result.append(Repository(handle=repos[i]))
core.BNFreeRepositoryManagerRepositoriesList(repos)
return result
@@ -233,7 +238,7 @@ class RepositoryManager(object):
@property
def default_repository(self):
"""Gets the default Repository"""
- startup._init_plugins()
+ binaryninja._init_plugins()
return Repository(handle=core.BNRepositoryManagerGetDefaultRepository(self.handle))
def enable_plugin(self, plugin, install=True, repo=None):
diff --git a/python/scriptingprovider.py b/python/scriptingprovider.py
index e7838748..37642ed4 100644
--- a/python/scriptingprovider.py
+++ b/python/scriptingprovider.py
@@ -26,14 +26,15 @@ import threading
import abc
import sys
-# Binary Ninja Components
-import _binaryninjacore as core
-from enums import ScriptingProviderExecuteResult, ScriptingProviderInputReadyState
-import binaryview
-import function
-import basicblock
-import startup
-import log
+# Binary Ninja components
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import ScriptingProviderExecuteResult, ScriptingProviderInputReadyState
+from binaryninja import log
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class _ThreadActionContext(object):
@@ -135,7 +136,7 @@ class ScriptingInstance(object):
def _set_current_binary_view(self, ctxt, view):
try:
if view:
- view = binaryview.BinaryView(handle = core.BNNewViewReference(view))
+ view = binaryninja.binaryview.BinaryView(handle = core.BNNewViewReference(view))
else:
view = None
self.perform_set_current_binary_view(view)
@@ -145,12 +146,12 @@ class ScriptingInstance(object):
def _set_current_function(self, ctxt, func):
try:
if func:
- func = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewFunctionReference(func))
+ func = binaryninja.function.Function(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewFunctionReference(func))
else:
func = None
self.perform_set_current_function(func)
except:
- log.log.log_error(traceback.format_exc())
+ log.log_error(traceback.format_exc())
def _set_current_basic_block(self, ctxt, block):
try:
@@ -159,7 +160,7 @@ class ScriptingInstance(object):
if func is None:
block = None
else:
- block = basicblock.BasicBlock(binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewBasicBlockReference(block))
+ block = binaryninja.basicblock.BasicBlock(binaryninja.binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewBasicBlockReference(block))
core.BNFreeFunction(func)
else:
block = None
@@ -256,30 +257,31 @@ class ScriptingInstance(object):
class _ScriptingProviderMetaclass(type):
+
@property
def list(self):
"""List all ScriptingProvider types (read-only)"""
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
types = core.BNGetScriptingProviderList(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(ScriptingProvider(types[i]))
core.BNFreeScriptingProviderList(types)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
types = core.BNGetScriptingProviderList(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield ScriptingProvider(types[i])
finally:
core.BNFreeScriptingProviderList(types)
def __getitem__(self, value):
- startup._init_plugins()
+ binaryninja._init_plugins()
provider = core.BNGetScriptingProviderByName(str(value))
if provider is None:
raise KeyError("'%s' is not a valid scripting provider" % str(value))
@@ -292,8 +294,7 @@ class _ScriptingProviderMetaclass(type):
raise AttributeError("attribute '%s' is read only" % name)
-class ScriptingProvider(object):
- __metaclass__ = _ScriptingProviderMetaclass
+class ScriptingProvider(with_metaclass(_ScriptingProviderMetaclass, object)):
name = None
instance_class = None
@@ -304,6 +305,12 @@ class ScriptingProvider(object):
self.handle = core.handle_of_type(handle, core.BNScriptingProvider)
self.__dict__["name"] = core.BNGetScriptingProviderName(handle)
+ @property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
+
def register(self):
self._cb = core.BNScriptingProviderCallbacks()
self._cb.context = 0
@@ -484,7 +491,7 @@ class PythonScriptingInstance(ScriptingInstance):
self.code = None
self.input = ""
- self.interpreter.push("from binaryninja import *\n")
+ self.interpreter.push("from binaryninja import *")
def execute(self, code):
self.code = code
@@ -547,8 +554,8 @@ class PythonScriptingInstance(ScriptingInstance):
self.locals["current_llil"] = self.active_func.low_level_il
self.locals["current_mlil"] = self.active_func.medium_level_il
- for line in code.split("\n"):
- self.interpreter.push(line)
+ for line in code.split(b'\n'):
+ self.interpreter.push(line.decode('charmap'))
if self.locals["here"] != self.active_addr:
if not self.active_view.file.navigate(self.active_view.file.view, self.locals["here"]):
@@ -650,6 +657,8 @@ original_stdin = sys.stdin
original_stdout = sys.stdout
original_stderr = sys.stderr
-sys.stdin = _PythonScriptingInstanceInput(sys.stdin)
-sys.stdout = _PythonScriptingInstanceOutput(sys.stdout, False)
-sys.stderr = _PythonScriptingInstanceOutput(sys.stderr, True)
+def redirect_stdio():
+ sys.stdin = _PythonScriptingInstanceInput(sys.stdin)
+ sys.stdout = _PythonScriptingInstanceOutput(sys.stdout, False)
+ sys.stderr = _PythonScriptingInstanceOutput(sys.stderr, True)
+ sys.excepthook = sys.__excepthook__ \ No newline at end of file
diff --git a/python/setting.py b/python/setting.py
index d58c8955..93f6b72a 100644
--- a/python/setting.py
+++ b/python/setting.py
@@ -21,7 +21,11 @@
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
+from binaryninja import _binaryninjacore as core
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import pyNativeStr
class Setting(object):
@@ -45,7 +49,7 @@ class Setting(object):
default_list[i] = default_value[i]
result = core.BNSettingGetIntegerList(self.plugin_name, name, default_list, ctypes.byref(length))
out_list = []
- for i in xrange(length.value):
+ for i in range(length.value):
out_list.append(result[i])
core.BNFreeSettingIntegerList(result)
return out_list
@@ -55,11 +59,11 @@ class Setting(object):
length.value = len(default_value)
default_list = (ctypes.c_char_p * len(default_value))()
for i in range(len(default_value)):
- default_list[i] = default_value[i]
+ default_list[i] = default_value[i].encode('charmap')
result = core.BNSettingGetStringList(self.plugin_name, name, default_list, ctypes.byref(length))
out_list = []
- for i in xrange(length.value):
- out_list.append(result[i])
+ for i in range(length.value):
+ out_list.append(pyNativeStr(result[i]))
core.BNFreeStringList(result, length)
return out_list
@@ -100,7 +104,7 @@ class Setting(object):
length = ctypes.c_ulonglong()
length.value = len(value)
default_list = (ctypes.c_longlong * len(value))()
- for i in xrange(len(value)):
+ for i in range(len(value)):
default_list[i] = value[i]
return core.BNSettingSetIntegerList(self.plugin_name, name, default_list, length, auto_flush)
@@ -109,8 +113,8 @@ class Setting(object):
length = ctypes.c_ulonglong()
length.value = len(value)
default_list = (ctypes.c_char_p * len(value))()
- for i in xrange(len(value)):
- default_list[i] = str(value[i])
+ for i in range(len(value)):
+ default_list[i] = value[i].encode('charmap')
return core.BNSettingSetStringList(self.plugin_name, name, default_list, length, auto_flush)
@@ -138,4 +142,4 @@ class Setting(object):
core.BNSettingRemoveSettingGroup(self.plugin_name, auto_flush)
def remove_setting(self, setting, auto_flush=True):
- core.BNSettingRemoveSetting(self.plugin_name, setting, auto_flush) \ No newline at end of file
+ core.BNSettingRemoveSetting(self.plugin_name, setting, auto_flush)
diff --git a/python/startup.py b/python/startup.py
index 0abc47cb..04e7b980 100644
--- a/python/startup.py
+++ b/python/startup.py
@@ -18,18 +18,18 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-import _binaryninjacore as core
+#from binaryninja import _binaryninjacore as core
-_plugin_init = False
+#_plugin_init = False
-def _init_plugins():
- global _plugin_init
- if not _plugin_init:
- _plugin_init = True
- core.BNInitCorePlugins()
- core.BNInitUserPlugins()
- core.BNInitRepoPlugins()
- if not core.BNIsLicenseValidated():
- raise RuntimeError("License is not valid. Please supply a valid license.")
+# def _init_plugins():
+# global _plugin_init
+# if not _plugin_init:
+# _plugin_init = True
+# core.BNInitCorePlugins()
+# core.BNInitUserPlugins()
+# core.BNInitRepoPlugins()
+# if not core.BNIsLicenseValidated():
+# raise RuntimeError("License is not valid. Please supply a valid license.")
diff --git a/python/transform.py b/python/transform.py
index 59d719e7..1734d248 100644
--- a/python/transform.py
+++ b/python/transform.py
@@ -23,31 +23,35 @@ import ctypes
import abc
# Binary Ninja components
-import _binaryninjacore as core
-from enums import TransformType
-import startup
-import log
-import databuffer
+import binaryninja
+from binaryninja import log
+from binaryninja import databuffer
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import TransformType
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class _TransformMetaClass(type):
@property
def list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
xforms = core.BNGetTransformTypeList(count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(Transform(xforms[i]))
core.BNFreeTransformTypeList(xforms)
return result
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
xforms = core.BNGetTransformTypeList(count)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield Transform(xforms[i])
finally:
core.BNFreeTransformTypeList(xforms)
@@ -59,14 +63,14 @@ class _TransformMetaClass(type):
raise AttributeError("attribute '%s' is read only" % name)
def __getitem__(cls, name):
- startup._init_plugins()
+ binaryninja._init_plugins()
xform = core.BNGetTransformByName(name)
if xform is None:
raise KeyError("'%s' is not a valid transform" % str(name))
return Transform(xform)
def register(cls):
- startup._init_plugins()
+ binaryninja._init_plugins()
if cls.name is None:
raise ValueError("transform 'name' is not defined")
if cls.long_name is None:
@@ -90,14 +94,13 @@ class TransformParameter(object):
self.fixed_length = fixed_length
-class Transform(object):
+class Transform(with_metaclass(_TransformMetaClass, object)):
transform_type = None
name = None
long_name = None
group = None
parameters = []
_registered_cb = None
- __metaclass__ = _TransformMetaClass
def __init__(self, handle):
if handle is None:
@@ -124,7 +127,7 @@ class Transform(object):
count = ctypes.c_ulonglong()
params = core.BNGetTransformParameterList(self.handle, count)
self.parameters = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
self.parameters.append(TransformParameter(params[i].name, params[i].longName, params[i].fixedLength))
core.BNFreeTransformParameterList(params, count.value)
@@ -145,7 +148,7 @@ class Transform(object):
try:
count[0] = len(self.parameters)
param_buf = (core.BNTransformParameterInfo * len(self.parameters))()
- for i in xrange(0, len(self.parameters)):
+ for i in range(0, len(self.parameters)):
param_buf[i].name = self.parameters[i].name
param_buf[i].longName = self.parameters[i].long_name
param_buf[i].fixedLength = self.parameters[i].fixed_length
@@ -170,7 +173,7 @@ class Transform(object):
try:
input_obj = databuffer.DataBuffer(handle = core.BNDuplicateDataBuffer(input_buf))
param_map = {}
- for i in xrange(0, count):
+ for i in range(0, count):
data = databuffer.DataBuffer(handle = core.BNDuplicateDataBuffer(params[i].value))
param_map[params[i].name] = str(data)
result = self.perform_decode(str(input_obj), param_map)
@@ -187,7 +190,7 @@ class Transform(object):
try:
input_obj = databuffer.DataBuffer(handle = core.BNDuplicateDataBuffer(input_buf))
param_map = {}
- for i in xrange(0, count):
+ for i in range(0, count):
data = databuffer.DataBuffer(handle = core.BNDuplicateDataBuffer(params[i].value))
param_map[params[i].name] = str(data)
result = self.perform_encode(str(input_obj), param_map)
@@ -200,6 +203,11 @@ class Transform(object):
log.log_error(traceback.format_exc())
return False
+ @property
+ def list(self):
+ """Allow tab completion to discover metaclass list property"""
+ pass
+
@abc.abstractmethod
def perform_decode(self, data, params):
if self.type == TransformType.InvertingTransform:
@@ -213,9 +221,9 @@ class Transform(object):
def decode(self, input_buf, params = {}):
input_buf = databuffer.DataBuffer(input_buf)
output_buf = databuffer.DataBuffer()
- keys = params.keys()
+ keys = list(params.keys())
param_buf = (core.BNTransformParameter * len(keys))()
- for i in xrange(0, len(keys)):
+ for i in range(0, len(keys)):
data = databuffer.DataBuffer(params[keys[i]])
param_buf[i].name = keys[i]
param_buf[i].value = data.handle
@@ -226,9 +234,9 @@ class Transform(object):
def encode(self, input_buf, params = {}):
input_buf = databuffer.DataBuffer(input_buf)
output_buf = databuffer.DataBuffer()
- keys = params.keys()
+ keys = list(params.keys())
param_buf = (core.BNTransformParameter * len(keys))()
- for i in xrange(0, len(keys)):
+ for i in range(0, len(keys)):
data = databuffer.DataBuffer(params[keys[i]])
param_buf[i].name = keys[i]
param_buf[i].value = data.handle
diff --git a/python/types.py b/python/types.py
index 42ed7ddd..12e7733f 100644
--- a/python/types.py
+++ b/python/types.py
@@ -18,25 +18,32 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+from __future__ import absolute_import
max_confidence = 255
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import SymbolType, TypeClass, NamedTypeReferenceClass, InstructionTextTokenType, StructureType, ReferenceType, VariableSourceType
-import callingconvention
-import function
+import binaryninja
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import SymbolType, TypeClass, NamedTypeReferenceClass, InstructionTextTokenType, StructureType, ReferenceType, VariableSourceType
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import pyNativeStr
class QualifiedName(object):
def __init__(self, name = []):
if isinstance(name, str):
self.name = [name]
+ self.byte_name = [name.encode('charmap')]
elif isinstance(name, QualifiedName):
self.name = name.name
+ self.byte_name = [n.encode('charmap') for n in name.name]
else:
- self.name = name
+ self.name = [pyNativeStr(i) for i in name]
+ self.byte_name = name
def __str__(self):
return "::".join(self.name)
@@ -98,8 +105,8 @@ class QualifiedName(object):
def _get_core_struct(self):
result = core.BNQualifiedName()
name_list = (ctypes.c_char_p * len(self.name))()
- for i in xrange(0, len(self.name)):
- name_list[i] = self.name[i]
+ for i in range(0, len(self.name)):
+ name_list[i] = self.name[i].encode('charmap')
result.name = name_list
result.nameCount = len(self.name)
return result
@@ -107,7 +114,7 @@ class QualifiedName(object):
@classmethod
def _from_core_struct(cls, name):
result = []
- for i in xrange(0, name.nameCount):
+ for i in range(0, name.nameCount):
result.append(name.name[i])
return QualifiedName(result)
@@ -292,7 +299,7 @@ class Type(object):
result = core.BNGetTypeCallingConvention(self.handle)
if not result.convention:
return None
- return callingconvention.CallingConvention(None, handle = result.convention, confidence = result.confidence)
+ return binaryninja.callingconvention.CallingConvention(None, handle = result.convention, confidence = result.confidence)
@property
def parameters(self):
@@ -300,7 +307,7 @@ class Type(object):
count = ctypes.c_ulonglong()
params = core.BNGetTypeParameters(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
param_type = Type(core.BNNewTypeReference(params[i].type), platform = self.platform, confidence = params[i].typeConfidence)
if params[i].defaultLocation:
param_location = None
@@ -310,7 +317,7 @@ class Type(object):
name = self.platform.arch.get_reg_name(params[i].location.storage)
elif params[i].location.type == VariableSourceType.StackVariableSourceType:
name = "arg_%x" % params[i].location.storage
- param_location = function.Variable(None, params[i].location.type, params[i].location.index,
+ param_location = binaryninja.function.Variable(None, params[i].location.type, params[i].location.index,
params[i].location.storage, name, param_type)
result.append(FunctionParameter(param_type, params[i].name, param_location))
core.BNFreeTypeParameterList(params, count.value)
@@ -344,7 +351,7 @@ class Type(object):
return None
return Enumeration(result)
- @property
+ @property
def named_type_reference(self):
"""Reference to a named type (read-only)"""
result = core.BNGetTypeNamedTypeReference(self.handle)
@@ -376,7 +383,7 @@ class Type(object):
def __repr__(self):
if self.confidence < max_confidence:
- return "<type: %s, %d%% confidence>" % (str(self), (self.confidence * 100) / max_confidence)
+ return "<type: %s, %d%% confidence>" % (str(self), (self.confidence * 100) // max_confidence)
return "<type: %s>" % str(self)
def get_string_before_name(self):
@@ -403,7 +410,7 @@ class Type(object):
platform = self.platform.handle
tokens = core.BNGetTypeTokens(self.handle, platform, base_confidence, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -412,7 +419,7 @@ class Type(object):
context = tokens[i].context
confidence = tokens[i].confidence
address = tokens[i].address
- result.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
core.BNFreeTokenList(tokens, count.value)
return result
@@ -423,7 +430,7 @@ class Type(object):
platform = self.platform.handle
tokens = core.BNGetTypeTokensBeforeName(self.handle, platform, base_confidence, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -432,7 +439,7 @@ class Type(object):
context = tokens[i].context
confidence = tokens[i].confidence
address = tokens[i].address
- result.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
core.BNFreeTokenList(tokens, count.value)
return result
@@ -443,7 +450,7 @@ class Type(object):
platform = self.platform.handle
tokens = core.BNGetTypeTokensAfterName(self.handle, platform, base_confidence, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
token_type = InstructionTextTokenType(tokens[i].type)
text = tokens[i].text
value = tokens[i].value
@@ -452,7 +459,7 @@ class Type(object):
context = tokens[i].context
confidence = tokens[i].confidence
address = tokens[i].address
- result.append(function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
+ result.append(binaryninja.function.InstructionTextToken(token_type, text, value, size, operand, context, address, confidence))
core.BNFreeTokenList(tokens, count.value)
return result
@@ -574,7 +581,7 @@ class Type(object):
:param bool variable_arguments: optional argument for functions that have a variable number of arguments
"""
param_buf = (core.BNFunctionParameter * len(params))()
- for i in xrange(0, len(params)):
+ for i in range(0, len(params)):
if isinstance(params[i], Type):
param_buf[i].name = ""
param_buf[i].type = params[i].handle
@@ -851,7 +858,7 @@ class Structure(object):
count = ctypes.c_ulonglong()
members = core.BNGetStructureMembers(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(StructureMember(Type(core.BNNewTypeReference(members[i].type), confidence = members[i].typeConfidence),
members[i].name, members[i].offset))
core.BNFreeStructureMemberList(members, count.value)
@@ -962,7 +969,7 @@ class Enumeration(object):
count = ctypes.c_ulonglong()
members = core.BNGetEnumerationMembers(self.handle, count)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(EnumerationMember(members[i].name, members[i].value, members[i].isDefault))
core.BNFreeEnumerationMemberList(members, count.value)
return result
@@ -1018,8 +1025,8 @@ def preprocess_source(source, filename=None, include_dirs=[]):
if filename is None:
filename = "input"
dir_buf = (ctypes.c_char_p * len(include_dirs))()
- for i in xrange(0, len(include_dirs)):
- dir_buf[i] = str(include_dirs[i])
+ for i in range(0, len(include_dirs)):
+ dir_buf[i] = include_dirs[i].encode('charmap')
output = ctypes.c_char_p()
errors = ctypes.c_char_p()
result = core.BNPreprocessSource(source, filename, output, errors, dir_buf, len(include_dirs))
diff --git a/python/undoaction.py b/python/undoaction.py
index 7e3c76a0..7891bf54 100644
--- a/python/undoaction.py
+++ b/python/undoaction.py
@@ -23,10 +23,8 @@ import json
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import ActionType
-import startup
-import log
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import ActionType
class UndoAction(object):
@@ -52,7 +50,7 @@ class UndoAction(object):
@classmethod
def register(cls):
- startup._init_plugins()
+ binaryninja._init_plugins()
if cls.name is None:
raise ValueError("undo action 'name' not defined")
if cls.action_type is None:
diff --git a/python/update.py b/python/update.py
index 1eb8ea61..7f5d6f9c 100644
--- a/python/update.py
+++ b/python/update.py
@@ -22,16 +22,18 @@ import traceback
import ctypes
# Binary Ninja components
-import _binaryninjacore as core
-from enums import UpdateResult
-import startup
-import log
+from binaryninja import _binaryninjacore as core
+from binaryninja.enums import UpdateResult
+
+# 2-3 compatibility
+from binaryninja import range
+from binaryninja import with_metaclass
class _UpdateChannelMetaClass(type):
@property
def list(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
errors = ctypes.c_char_p()
channels = core.BNGetUpdateChannels(count, errors)
@@ -40,7 +42,7 @@ class _UpdateChannelMetaClass(type):
core.BNFreeString(ctypes.cast(errors, ctypes.POINTER(ctypes.c_byte)))
raise IOError(error_str)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(UpdateChannel(channels[i].name, channels[i].description, channels[i].latestVersion))
core.BNFreeUpdateChannelList(channels, count.value)
return result
@@ -54,7 +56,7 @@ class _UpdateChannelMetaClass(type):
return core.BNSetActiveUpdateChannel(value)
def __iter__(self):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
errors = ctypes.c_char_p()
channels = core.BNGetUpdateChannels(count, errors)
@@ -63,7 +65,7 @@ class _UpdateChannelMetaClass(type):
core.BNFreeString(ctypes.cast(errors, ctypes.POINTER(ctypes.c_byte)))
raise IOError(error_str)
try:
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
yield UpdateChannel(channels[i].name, channels[i].description, channels[i].latestVersion)
finally:
core.BNFreeUpdateChannelList(channels, count.value)
@@ -75,7 +77,7 @@ class _UpdateChannelMetaClass(type):
raise AttributeError("attribute '%s' is read only" % name)
def __getitem__(cls, name):
- startup._init_plugins()
+ binaryninja._init_plugins()
count = ctypes.c_ulonglong()
errors = ctypes.c_char_p()
channels = core.BNGetUpdateChannels(count, errors)
@@ -84,7 +86,7 @@ class _UpdateChannelMetaClass(type):
core.BNFreeString(ctypes.cast(errors, ctypes.POINTER(ctypes.c_byte)))
raise IOError(error_str)
result = None
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
if channels[i].name == str(name):
result = UpdateChannel(channels[i].name, channels[i].description, channels[i].latestVersion)
break
@@ -108,9 +110,7 @@ class UpdateProgressCallback(object):
log.log_error(traceback.format_exc())
-class UpdateChannel(object):
- __metaclass__ = _UpdateChannelMetaClass
-
+class UpdateChannel(with_metaclass(_UpdateChannelMetaClass, object)):
def __init__(self, name, desc, ver):
self.name = name
self.description = desc
@@ -127,7 +127,7 @@ class UpdateChannel(object):
core.BNFreeString(ctypes.cast(errors, ctypes.POINTER(ctypes.c_byte)))
raise IOError(error_str)
result = []
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
result.append(UpdateVersion(self, versions[i].version, versions[i].notes, versions[i].time))
core.BNFreeUpdateChannelVersionList(versions, count.value)
return result
@@ -143,7 +143,7 @@ class UpdateChannel(object):
core.BNFreeString(ctypes.cast(errors, ctypes.POINTER(ctypes.c_byte)))
raise IOError(error_str)
result = None
- for i in xrange(0, count.value):
+ for i in range(0, count.value):
if versions[i].version == self.latest_version_num:
result = UpdateVersion(self, versions[i].version, versions[i].notes, versions[i].time)
break