Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change bytes literals to bytearray #34

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading