Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
persistence.write_uint32(output_file, flags)
persistence.write_uint32(output_file, processor_id)
persistence.write_uint32(output_file, num_blocks)
output_file_offset = output_file.tell()
#for block in blocks:
# write_SegmentBlock(output_file, block, program_data.dis_constant_pc_offset)
output_file.write(block_data_string)
if output_file.tell() - output_file_offset != block_data_length:
logger.error("convert_project_format_3_to_4: block length mismatch %d != %d", output_file.tell() - output_file_offset, block_data_length)
return None
# Update the header length field, then fast forward to the end of the hunk.
new_hunk_length = output_file.tell() - output_file_payload_offset
output_file.seek(output_file_length_offset, os.SEEK_SET)
persistence.write_uint32(output_file, new_hunk_length)
output_file.seek(new_hunk_length, os.SEEK_CUR)
if output_file.tell() - output_file_payload_offset != new_hunk_length:
logger.error("convert_project_format_3_to_4: block length mismatch %d != %d", output_file.tell() - output_file_payload_offset, new_hunk_length)
return None
return output_file
}
input_file.seek(0, os.SEEK_END)
file_size = input_file.tell()
input_file.seek(0, os.SEEK_SET)
savefile_id = persistence.read_uint32(input_file)
savefile_version = persistence.read_uint16(input_file)
if savefile_version != 4:
return None
logger.info("Upgrading save-file from version 4 to version 5: symbols by address..")
save_count = persistence.read_uint32(input_file)
output_file = tempfile.TemporaryFile()
persistence.write_uint32(output_file, savefile_id)
persistence.write_uint16(output_file, 5)
persistence.write_uint32(output_file, save_count)
while input_file.tell() < file_size:
# This should be pretty straightforward.
hunk_header_offset = input_file.tell()
hunk_id = persistence.read_uint16(input_file)
hunk_length = persistence.read_uint32(input_file)
hunk_payload_offset = input_file.tell()
actual_hunk_version = persistence.read_uint16(input_file)
expected_hunk_version = SNAPSHOT_HUNK_VERSIONS[hunk_id]
if expected_hunk_version != actual_hunk_version:
logger.error("convert_project_format_4_to_5: hunk %d version mismatch %d != %d", hunk_id, expected_hunk_version, actual_hunk_version)
return None
logger.debug("convert_project_format_4_to_5: file hunk %d", hunk_id)
logger.info("Upgrading save-file from version 2 to version 3: Hunk versioning..")
save_count = persistence.read_uint32(input_file)
output_file = tempfile.TemporaryFile()
persistence.write_uint32(output_file, savefile_id)
persistence.write_uint16(output_file, 3)
persistence.write_uint32(output_file, save_count)
while input_file.tell() < file_size:
# This should be pretty straightforward.
hunk_id = persistence.read_uint16(input_file)
persistence.write_uint16(output_file, hunk_id)
input_hunk_length = persistence.read_uint32(input_file)
output_length_offset = output_file.tell()
persistence.write_uint32(output_file, 0)
output_data_offset = output_file.tell()
# Modification.
persistence.write_uint16(output_file, SNAPSHOT_HUNK_VERSIONS[hunk_id])
input_data = input_file.read(input_hunk_length)
output_file.write(input_data)
output_hunk_length = output_file.tell() - output_data_offset
output_file.seek(output_length_offset, os.SEEK_SET)
persistence.write_uint32(output_file, output_hunk_length)
output_file.seek(output_hunk_length, os.SEEK_CUR)
return output_file
}
input_file.seek(0, os.SEEK_END)
file_size = input_file.tell()
input_file.seek(0, os.SEEK_SET)
savefile_id = persistence.read_uint32(input_file)
savefile_version = persistence.read_uint16(input_file)
if savefile_version != 3:
return None
logger.info("Upgrading save-file from version 3 to version 4: Processor id field..")
save_count = persistence.read_uint32(input_file)
output_file = tempfile.TemporaryFile()
persistence.write_uint32(output_file, savefile_id)
persistence.write_uint16(output_file, 4)
persistence.write_uint32(output_file, save_count)
while input_file.tell() < file_size:
# This should be pretty straightforward.
hunk_header_offset = input_file.tell()
hunk_id = persistence.read_uint16(input_file)
hunk_length = persistence.read_uint32(input_file)
hunk_payload_offset = input_file.tell()
actual_hunk_version = persistence.read_uint16(input_file)
expected_hunk_version = SNAPSHOT_HUNK_VERSIONS[hunk_id]
if expected_hunk_version != actual_hunk_version:
logger.error("convert_project_format_3_to_4: hunk %d version mismatch %d != %d", hunk_id, expected_hunk_version, actual_hunk_version)
return None
logger.debug("convert_project_format_3_to_4: file hunk %d", hunk_id)
def save_disassembly_hunk(f, program_data):
persistence.write_dict_uint32_to_set_of_uint32s(f, program_data.branch_addresses)
persistence.write_dict_uint32_to_set_of_uint32s(f, program_data.reference_addresses)
persistence.write_dict_uint32_to_string(f, program_data.symbols_by_address)
persistence.write_dict_uint32_to_list_of_uint32s(f, program_data.post_segment_addresses)
persistence.write_uint32(f, program_data.flags)
persistence.write_uint32(f, program_data.processor_id)
persistence.write_uint32(f, len(program_data.blocks))
for block in program_data.blocks:
write_SegmentBlock(f, block, program_data.dis_constant_pc_offset)
def write_segment_list_entry(f, v):
persistence.write_uint8(f, v[loaderlib.SI_TYPE])
if v[loaderlib.SI_FILE_OFFSET] == -1:
persistence.write_uint32(f, 0xFFFFFFFF) # Unsigned, special value.
else:
persistence.write_uint32(f, v[loaderlib.SI_FILE_OFFSET])
persistence.write_uint32(f, v[loaderlib.SI_DATA_LENGTH])
persistence.write_uint32(f, v[loaderlib.SI_LENGTH])
persistence.write_uint32(f, v[loaderlib.SI_ADDRESS])
if SAVEFILE_HUNK_DISASSEMBLY == hunk_id:
save_disassembly_hunk(f, program_data)
elif SAVEFILE_HUNK_LOADER == hunk_id:
save_loader_hunk(f, program_data)
elif SAVEFILE_HUNK_LOADERINTERNAL == hunk_id:
save_loaderinternaldata_hunk(f, program_data)
elif SAVEFILE_HUNK_SOURCEDATAINFO == hunk_id:
save_sourcedatainfo_hunk(f, program_data)
elif SAVEFILE_HUNK_SOURCEDATA == hunk_id:
save_sourcedata_hunk(f, program_data, save_options.input_file)
else:
raise RuntimeError("Trying to save a hunk with no handling to do so")
hunk_length = f.tell() - hunk_data_offset
# Go back and fill in the hunk length field.
f.seek(length_offset, os.SEEK_SET)
persistence.write_uint32(f, hunk_length)
# Return to the end of the hunk to perhaps write the next.
f.seek(hunk_length, os.SEEK_CUR)
logger.info("Saved project (%d bytes)", f.tell())
# Write the as yet to be updated header.
persistence.write_uint16(output_file, hunk_id)
output_file_length_offset = output_file.tell()
persistence.write_uint32(output_file, 0)
output_file_payload_offset = output_file.tell()
persistence.write_uint16(output_file, SNAPSHOT_HUNK_VERSIONS[SAVEFILE_HUNK_DISASSEMBLY] + 1)
# Write the payload in the modified format.
persistence.write_dict_uint32_to_set_of_uint32s(output_file, branch_addresses)
persistence.write_dict_uint32_to_set_of_uint32s(output_file, reference_addresses)
persistence.write_dict_uint32_to_string(output_file, symbols_by_address)
persistence.write_dict_uint32_to_list_of_uint32s(output_file, post_segment_addresses)
persistence.write_uint32(output_file, flags)
persistence.write_uint32(output_file, processor_id)
persistence.write_uint32(output_file, num_blocks)
output_file_offset = output_file.tell()
#for block in blocks:
# write_SegmentBlock(output_file, block, program_data.dis_constant_pc_offset)
output_file.write(block_data_string)
if output_file.tell() - output_file_offset != block_data_length:
logger.error("convert_project_format_3_to_4: block length mismatch %d != %d", output_file.tell() - output_file_offset, block_data_length)
return None
# Update the header length field, then fast forward to the end of the hunk.
new_hunk_length = output_file.tell() - output_file_payload_offset
output_file.seek(output_file_length_offset, os.SEEK_SET)
persistence.write_uint32(output_file, new_hunk_length)
output_file.seek(new_hunk_length, os.SEEK_CUR)
if output_file.tell() - output_file_payload_offset != new_hunk_length:
logger.error("convert_project_format_3_to_4: block length mismatch %d != %d", output_file.tell() - output_file_payload_offset, new_hunk_length)
# The length of this instruction is not stored, so we calculate it relative to the next one.
j = i+1
while j < len(block.line_data):
next_type_id, next_entry = block.line_data[j]
if next_type_id == SLD_INSTRUCTION:
if type(next_entry) is int:
block_offset = next_entry
else:
block_offset = next_entry.pc - pc_offset
break
j += 1
else:
persistence.write_uint16(f, block_offset)
block_offset += entry.num_bytes
elif type_id == SLD_EQU_LOCATION_RELATIVE:
persistence.write_uint32(f, entry) # block offset
elif type_id in (SLD_COMMENT_TRAILING, SLD_COMMENT_FULL_LINE):
persistence.write_string(f, entry) # string
else:
logger.error("Trying to save a savefile, did not know how to handle entry of type_id: %d, entry value: %s", type_id, entry)
def write_segment_list(f, v):
start_offset = f.tell()
persistence.write_uint32(f, 0)
data_start_offset = f.tell()
for entry in v:
write_segment_list_entry(f, entry)
end_offset = f.tell()
f.seek(start_offset, os.SEEK_SET)
persistence.write_uint32(f, end_offset - data_start_offset)
f.seek(end_offset, os.SEEK_SET)