Skip to content

Commit

Permalink
Turn __data__ into a bytearray to avoid copying data around
Browse files Browse the repository at this point in the history
  • Loading branch information
elicn committed Mar 14, 2022
1 parent e351420 commit fe3b98a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit fe3b98a

Please sign in to comment.