From fe3b98ab1870399e5dab6a872a99dd6b1dbf12e1 Mon Sep 17 00:00:00 2001 From: elicn Date: Mon, 14 Mar 2022 11:25:59 +0200 Subject: [PATCH] Turn __data__ into a bytearray to avoid copying data around --- pefile.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pefile.py b/pefile.py index 4f6ca4f..2656744 100644 --- a/pefile.py +++ b/pefile.py @@ -6962,14 +6962,18 @@ def set_bytes_at_offset(self, offset, data): raise TypeError("data should be of type: bytes") if 0 <= offset < len(self.__data__): - self.__data__ = ( - self.__data__[:offset] + data + self.__data__[offset + len(data) :] - ) + self.set_data_bytes(offset, data) else: return False return True + def set_data_bytes(self, offset: int, data: bytes): + if not isinstance(self.__data__, bytearray): + self.__data__ = bytearray(self.__data__) + + self.__data__[offset:offset + len(data)] = data + def merge_modified_section_data(self): """Update the PE image content with any individual section data that has been modified. @@ -6983,11 +6987,7 @@ def merge_modified_section_data(self): if section_data_start < len(self.__data__) and section_data_end < len( self.__data__ ): - self.__data__ = ( - self.__data__[:section_data_start] - + section.get_data() - + self.__data__[section_data_end:] - ) + self.set_data_bytes(section_data_start, section.get_data()) def relocate_image(self, new_ImageBase): """Apply the relocation information to the image using the provided image base.