summaryrefslogtreecommitdiff
path: root/python/function.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/function.py')
-rw-r--r--python/function.py250
1 files changed, 250 insertions, 0 deletions
diff --git a/python/function.py b/python/function.py
index d7b03cf8..58565838 100644
--- a/python/function.py
+++ b/python/function.py
@@ -1,3 +1,4 @@
+# coding=utf-8
# Copyright (c) 2015-2019 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -908,6 +909,255 @@ class Function(object):
core.BNFreeAddressList(addrs)
return result
+ def create_tag(self, type, data):
+ """
+ ``create_tag`` creates a new Tag object but does not add it anywhere
+
+ :param TagType type: The Tag Type for this Tag
+ :param str data: Additional data for the Tag
+ :return The created Tag
+ :rtype Tag
+ :Example:
+
+ >>> tt = bv.tag_types["Crabby Functions"]
+ >>> tag = current_function.create_tag(tt, "Get Crabbed")
+ >>> current_function.add_user_address_tag(here, tag)
+ >>>
+ """
+ return self.view.create_tag(type, data)
+
+ @property
+ def address_tags(self):
+ """
+ ``address_tags`` gets a list of all address Tags in the function.
+ Tags are returned as a list of (arch, address, Tag) tuples.
+
+ :type [(Architecture, int, Tag)]
+ """
+ count = ctypes.c_ulonglong()
+ tags = core.BNGetAddressTagReferences(self.handle, count)
+ result = []
+ for i in range(0, count.value):
+ arch = binaryninja.architecture.CoreArchitecture(tags[i].arch)
+ tag = binaryninja.binaryview.Tag(core.BNNewTagReference(tags[i].tag))
+ result.append((arch, tags[i].addr, tag))
+ core.BNFreeTagReferences(tags, count.value)
+ return result
+
+ def get_address_tags_at(self, addr, arch=None):
+ """
+ ``get_address_tags_at`` gets a list of all Tags in the function at a given address.
+
+ :param int addr: Address to get tags at
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :return A list of Tags
+ :rtype [Tag]
+ """
+ if arch is None:
+ arch = self.arch
+ count = ctypes.c_ulonglong()
+ tags = core.BNGetAddressTags(self.handle, arch.handle, addr, count)
+ result = []
+ for i in range(0, count.value):
+ result.append(binaryninja.binaryview.Tag(core.BNNewTagReference(tags[i])))
+ core.BNFreeTagList(tags, count.value)
+ return result
+
+ def add_user_address_tag(self, addr, tag, arch=None):
+ """
+ ``add_user_address_tag`` adds an already-created Tag object at a given address.
+ Since this adds a user tag, it will be added to the current undo buffer.
+
+ :param int addr: Address at which to add the tag
+ :param Tag tag: Tag object to be added
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :rtype None
+ """
+ if arch is None:
+ arch = self.arch
+ core.BNAddUserAddressTag(self.handle, arch.handle, addr, tag.handle)
+
+ def create_user_address_tag(self, addr, type, data, unique=False, arch=None):
+ """
+ ``create_user_address_tag`` creates and adds a Tag object at a given address.
+ Since this adds a user tag, it will be added to the current undo buffer.
+
+ :param int addr: Address at which to add the tag
+ :param TagType type: Tag Type for the Tag that is created
+ :param str data: Additional data for the Tag
+ :param bool unique: If a tag already exists at this location with this data, don't add another
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :return The created Tag
+ :rtype Tag
+ """
+ if arch is None:
+ arch = self.arch
+ if unique:
+ tags = self.get_address_tags_at(addr, arch)
+ for tag in tags:
+ if tag.type == type and tag.data == data:
+ return
+
+ tag = self.create_tag(type, data)
+ core.BNAddUserAddressTag(self.handle, arch.handle, addr, tag.handle)
+ return tag
+
+ def remove_user_address_tag(self, addr, tag, arch=None):
+ """
+ ``remove_user_address_tag`` removes a Tag object at a given address.
+ Since this removes a user tag, it will be added to the current undo buffer.
+
+ :param int addr: Address at which to add the tag
+ :param Tag tag: Tag object to be added
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :rtype None
+ """
+ if arch is None:
+ arch = self.arch
+ core.BNRemoveUserAddressTag(self.handle, arch.handle, addr, tag.handle)
+
+ def add_auto_address_tag(self, addr, tag, arch=None):
+ """
+ ``add_auto_address_tag`` adds an already-created Tag object at a given address.
+
+ :param int addr: Address at which to add the tag
+ :param Tag tag: Tag object to be added
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :rtype None
+ """
+ if arch is None:
+ arch = self.arch
+ core.BNAddAutoAddressTag(self.handle, arch.handle, addr, tag.handle)
+
+ def create_auto_address_tag(self, addr, type, data, unique=False, arch=None):
+ """
+ ``create_auto_address_tag`` creates and adds a Tag object at a given address.
+
+ :param int addr: Address at which to add the tag
+ :param TagType type: Tag Type for the Tag that is created
+ :param str data: Additional data for the Tag
+ :param bool unique: If a tag already exists at this location with this data, don't add another
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :return The created Tag
+ :rtype Tag
+ """
+ if arch is None:
+ arch = self.arch
+ if unique:
+ tags = self.get_address_tags_at(addr, arch)
+ for tag in tags:
+ if tag.type == type and tag.data == data:
+ return
+
+ tag = self.create_tag(type, data)
+ core.BNAddAutoAddressTag(self.handle, arch.handle, addr, tag.handle)
+ return tag
+
+ def remove_auto_address_tag(self, addr, tag, arch=None):
+ """
+ ``remove_auto_address_tag`` removes a Tag object at a given address.
+
+ :param int addr: Address at which to add the tag
+ :param Tag tag: Tag object to be added
+ :param Architecture arch: Architecture for the block in which the Tag is added (optional)
+ :rtype None
+ """
+ if arch is None:
+ arch = self.arch
+ core.BNRemoveAutoAddressTag(self.handle, arch.handle, addr, tag.handle)
+
+ @property
+ def function_tags(self):
+ """
+ ``function_tags`` gets a list of all function Tags for the function.
+
+ :type [Tag]
+ """
+ count = ctypes.c_ulonglong()
+ tags = core.BNGetFunctionTags(self.handle, count)
+ result = []
+ for i in range(0, count.value):
+ result.append(binaryninja.binaryview.Tag(core.BNNewTagReference(tags[i])))
+ core.BNFreeTagList(tags, count.value)
+ return result
+
+ def add_user_function_tag(self, tag):
+ """
+ ``add_user_function_tag`` adds an already-created Tag object as a function tag.
+ Since this adds a user tag, it will be added to the current undo buffer.
+
+ :param Tag tag: Tag object to be added
+ :rtype None
+ """
+ core.BNAddUserFunctionTag(self.handle, tag.handle)
+
+ def create_user_function_tag(self, type, data, unique=False):
+ """
+ ``add_user_function_tag`` creates and adds a Tag object as a function tag.
+ Since this adds a user tag, it will be added to the current undo buffer.
+
+ :param TagType type: Tag Type for the Tag that is created
+ :param str data: Additional data for the Tag
+ :param bool unique: If a tag already exists with this data, don't add another
+ :return The created Tag
+ :rtype Tag
+ """
+ if unique:
+ for tag in self.function_tags:
+ if tag.type == type and tag.data == data:
+ return
+
+ tag = self.create_tag(type, data)
+ core.BNAddUserFunctionTag(self.handle, tag.handle)
+ return tag
+
+ def remove_user_function_tag(self, tag):
+ """
+ ``remove_user_function_tag`` removes a Tag object as a function tag.
+ Since this removes a user tag, it will be added to the current undo buffer.
+
+ :param Tag tag: Tag object to be added
+ :rtype None
+ """
+ core.BNRemoveUserFunctionTag(self.handle, tag.handle)
+
+ def add_auto_function_tag(self, tag):
+ """
+ ``add_user_function_tag`` adds an already-created Tag object as a function tag.
+
+ :param Tag tag: Tag object to be added
+ :rtype None
+ """
+ core.BNAddAutoFunctionTag(self.handle, tag.handle)
+
+ def create_auto_function_tag(self, type, data, unique=False):
+ """
+ ``add_user_function_tag`` creates and adds a Tag object as a function tag.
+
+ :param TagType type: Tag Type for the Tag that is created
+ :param str data: Additional data for the Tag
+ :param bool unique: If a tag already exists with this data, don't add another
+ :return The created Tag
+ :rtype Tag
+ """
+ if unique:
+ for tag in self.function_tags:
+ if tag.type == type and tag.data == data:
+ return
+
+ tag = self.create_tag(type, data)
+ core.BNAddAutoFunctionTag(self.handle, tag.handle)
+ return tag
+
+ def remove_auto_function_tag(self, tag):
+ """
+ ``remove_user_function_tag`` removes a Tag object as a function tag.
+
+ :param Tag tag: Tag object to be added
+ :rtype None
+ """
+ core.BNRemoveAutoFunctionTag(self.handle, tag.handle)
+
@property
def low_level_il(self):
"""Deprecated property provided for compatibility. Use llil instead."""