Implement simple write_bytes helper func
@@ -10,6 +10,11 @@ def read_bytes(data, offset, length, format_string):
unpacked_data = struct.unpack(format_string, requested_data)
return unpacked_data
+def write_bytes(data, offset, raw_bytes):
+ data.seek(offset)
+ data.write(raw_bytes)
+
+
def read_str(data, offset, length):
data_length = data.seek(0, 2)
if offset+length > data_length:
@@ -19,18 +24,6 @@ def read_str(data, offset, length):
string = string.rstrip("\0") # Remove trailing null bytes
return string
-def write_str(data, offset, new_string, max_length):
- str_len = len(new_string)
- if str_len > max_length:
- raise Exception("String %s is too long (max length %X)" % (new_string, max_length))
-
- padding_length = max_length - str_len
- null_padding = b"\x00"*padding_length
- new_value = new_string.encode("ascii") + null_padding
-
- data.seek(offset)
- data.write(new_value)
-
def try_read_str(data, offset, length):
try:
return read_str(data, offset, length)
@@ -52,6 +45,18 @@ def read_str_until_null_character(data, offset):
offset += 1
return str
+def write_str(data, offset, new_string, max_length):
+ str_len = len(new_string)
+ if str_len > max_length:
+ raise Exception("String %s is too long (max length %X)" % (new_string, max_length))
+
+ padding_length = max_length - str_len
+ null_padding = b"\x00"*padding_length
+ new_value = new_string.encode("ascii") + null_padding
+
+ data.seek(offset)
+ data.write(new_value)
+
def read_u8(data, offset):
data.seek(offset)
