From 82d4730da75b01e9521344b7497f2238bbdef18f Mon Sep 17 00:00:00 2001 From: Galen Williamson Date: Wed, 22 Dec 2021 16:07:27 -0500 Subject: added BinaryView.get_data_offset_for_address to python API --- python/binaryview.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'python/binaryview.py') diff --git a/python/binaryview.py b/python/binaryview.py index 3b6cb0da..3f3f1808 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -6864,7 +6864,7 @@ class BinaryView: def get_address_for_data_offset(self, offset:int) -> Optional[int]: """ - ``get_address_for_data_offset`` returns the virtual address that maps to the specific file offset + ``get_address_for_data_offset`` returns the virtual address that maps to the specific file offset. :param int offset: file offset :return: the virtual address of the first segment that contains that file location @@ -6875,6 +6875,23 @@ class BinaryView: return None return address.value + def get_data_offset_for_address(self, address:int) -> Optional[int]: + """ + ``get_data_offset_for_address`` returns the file offset that maps to the given virtual address, if possible. + + If `address` falls within a bss segment or an external segment, for example, no mapping is possible, and `None` will be returned. + + :param int address: virtual address + :return: the file location that is mapped to the given virtual address, or None if no such mapping is possible + :rtype: Int + """ + segment = self.get_segment_at(address) + if segment is not None and segment.start <= address < segment.end: + offset = address - segment.start + if offset < segment.data_length: + return offset + segment.data_offset + return None + def add_auto_section(self, name:str, start:int, length:int, semantics:SectionSemantics = SectionSemantics.DefaultSectionSemantics, type:str = "", align:int = 1, entry_size:int = 1, linked_section:str = "", info_section:str = "", info_data:int = 0) -> None: -- cgit v1.3.1