Skip to content

Instantly share code, notes, and snippets.

@digarok
Created December 9, 2023 14:35
Show Gist options
  • Save digarok/b995fba2ba06f233c2776613601b35dc to your computer and use it in GitHub Desktop.
Save digarok/b995fba2ba06f233c2776613601b35dc to your computer and use it in GitHub Desktop.
import struct, sys
def unpack_bytes(len, bytes):
if len == 3:
bytes.extend(b'\x00')
return struct.unpack('<I', bytes)[0]
def read_pgz_file(file_path):
with open(file_path, 'rb') as file:
# Read the file type signature (first byte)
signature = file.read(1)
# Determine the address and size format
if signature == b'Z':
address_size_format = 3 # 24 bit
print('Signature: "Z" 24-bit addresses\n')
elif signature == b'z':
address_size_format = 4 # 32 bit
print('Signature: "z" 32-bit addresses \n')
else:
print("Invalid PGZ file. Signature 'Z' not found.")
return
# Read segments
segment_number = 1
while True:
address = unpack_bytes(address_size_format,bytearray(file.read(address_size_format)))
#print(f'Adr: {address:06x}')
size = unpack_bytes(address_size_format,bytearray(file.read(address_size_format)))
if size == 0:
# Final segment with size $000000 indicates the starting address of the executable
print(f"Segment {segment_number}: Starting address of the executable: {hex(address)}")
break
print(f"Segment {segment_number}: Address: ${address:06x}, Size: ${size:06x} ({size})")
# Read data of the segment
data = file.read(size)
# Process or display data as needed
# For example, you can print the data in hex format: print(f"Data: {data.hex()}")
segment_number += 1
print()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Add pgz argument to command.")
else:
pgz_file_path = sys.argv[1]
read_pgz_file(pgz_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment