Skip to content

Commit

Permalink
Improve performance of read_string (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
sledgeh4w authored Jul 22, 2024
1 parent e93d6ca commit f87be6d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/chomper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,28 @@ def read_bytes(self, address: int, size: int) -> bytes:
def read_string(self, address: int) -> str:
"""Read string from the address."""
data = bytes()
offset = 0

while True:
byte = self.read_bytes(address + offset, 1)
if byte == b"\x00":
break
block_size = 1024
end = b"\x00"

data += byte
offset += 1
try:
while True:
buf = self.read_bytes(address, block_size)
if buf.find(end) != -1:
data += buf[: buf.index(end)]
break

data += buf
address += block_size

except UcError:
for i in range(block_size):
buf = self.read_bytes(address + i, 1)
if buf == end:
break

data += buf
address += 1

return data.decode("utf-8")

Expand Down

0 comments on commit f87be6d

Please sign in to comment.