Skip to content

Commit

Permalink
binaryxml: Fix stringpool insertion padding (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Jun 24, 2023
1 parent 7db57eb commit 946a65d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions frida_tools/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def get_string(self, index: int) -> str:

string = None
if self.utf8:
# Ignore UTF-16 length
# Ignore number of characters
n = struct.unpack("<B", self.stream.read(1))[0]
if n & 0x80:
n = ((n & 0x7F) << 8) | struct.unpack("<B", self.stream.read(1))[0]
Expand Down Expand Up @@ -299,10 +299,10 @@ def append_str(self, add: str) -> int:
offset = len(chunk_data) - 8 - self.strings_offset + 4

if self.utf8:
# UTF-16 length (ignored)
chunk_data.extend(struct.pack("<B", len(add)))
# UTF-8 length
chunk_data.extend(struct.pack("<B", len(add)))
assert len(add.encode("utf-8")) < 128 # multi-byte len strings not supported yet
length_in_characters = len(add)
length_in_bytes = len(add.encode("utf-8"))
chunk_data.extend(struct.pack("<BB", length_in_characters, length_in_bytes))

chunk_data.extend(add.encode("utf-8"))
# Insert a UTF-8 NUL
Expand All @@ -313,6 +313,11 @@ def append_str(self, add: str) -> int:
# Insert a UTF-16 NUL
chunk_data.extend([0, 0])

# pad to a multiple of 4 bytes
if len(chunk_data) % 4 != 0:
alignment_padding = [0] * (4 - len(chunk_data) % 4)
chunk_data.extend(alignment_padding)

# Insert a new offset at the end of the existing offsets
chunk_data[end : end + 4] = struct.pack("<I", offset)

Expand Down

0 comments on commit 946a65d

Please sign in to comment.