Skip to content

Commit

Permalink
Change bytes literals to bytearray
Browse files Browse the repository at this point in the history
  • Loading branch information
itsybitsypixel authored and mborgerson committed Jun 12, 2024
1 parent d927c05 commit 64d5432
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions xbe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ def off_to_addr(off: int) -> int:
raw_off = round_up(self.header.headers_size)

# Construct section data
section_data = b""
section_data = bytearray()
for name in sorted(
self.sections, key=lambda x: cast(int, self.sections[x].header.virtual_addr)
):
Expand All @@ -1062,23 +1062,23 @@ def off_to_addr(off: int) -> int:
"Expected %#x for %s, got %#x", s.header.raw_addr, name, raw_off
)
s.header.raw_addr = raw_off
section_data += s.data
section_data.extend(s.data)
raw_off += len(s.data)

# Align to 4k
new_offset = round_up(raw_off)
section_data += bytes(new_offset - raw_off)
section_data.extend(bytes(new_offset - raw_off))
raw_off = new_offset

#
# Construct headers
#
headers_data = b""
headers_data = bytearray()

def do_append(data: bytes) -> int:
nonlocal raw_off, headers_data
old_off = raw_off
headers_data += data
headers_data.extend(data)
raw_off += len(data)
return off_to_addr(old_off)

Expand Down Expand Up @@ -1204,20 +1204,20 @@ def do_append(data: bytes) -> int:
)

# Construct final image
output = b""
output += bytes(self.header)
output += bytes(self.cert)
output = bytearray()
output.extend(bytes(self.header))
output.extend(bytes(self.cert))
for name, s in self.sections.items():
log.info("Writing section %s at %#x", name, len(output))
output += bytes(s.header)
output += headers_data
output += section_data
output.extend(bytes(s.header))
output.extend(headers_data)
output.extend(section_data)

# MS XBEs seem to always add an extra page if the last page is completely filled
# FIXME: Why?
if output[-1] != 0:
output += bytes(0x1000)
return output
output.extend(bytes(0x1000))
return bytes(output)

@classmethod
def from_file(cls, path: str) -> "Xbe":
Expand Down

0 comments on commit 64d5432

Please sign in to comment.