Skip to content

Instantly share code, notes, and snippets.

@untoxa
Last active May 1, 2020 21:06
Show Gist options
  • Save untoxa/26601226b860277873507e44b07d960d to your computer and use it in GitHub Desktop.
Save untoxa/26601226b860277873507e44b07d960d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sts=4 sw=4 et
import sys
from struct import unpack
from array import array
def read_snspshot(snapshot_name):
def readasciiz(f):
res = b''
ch = f.read(1)
while (ch != b'') and (ch != b'\x00'):
res = res + ch
ch = f.read(1)
return res.decode('u8')
def readblock(f):
len = unpack('<L', f.read(4))[0]
if len == 0:
return None
if len == 1:
return [len, unpack('B', f.read(1))[0]]
elif len == 2:
return [len, unpack('<H', f.read(2))[0]]
elif len == 4:
return [len, unpack('<L', f.read(4))[0]]
else:
data = array('B')
data.fromfile(f, len)
return data
snapshot = {}
with open(snapshot_name, 'rb') as f:
name = readasciiz(f)
while (name):
data = readblock(f)
snapshot[name] = data
name = readasciiz(f)
return snapshot
# read the snapshot to the dict
snapshot = read_snspshot(sys.argv[1])
# pretty-print the snapshot dict
for k, v in snapshot.items():
if v is None:
print(k)
elif type(v) is array:
if len(v) <= 16:
print("{} [{:s}]".format(k, ', '.join(['0x{:02x}'.format(x) for x in v])))
else:
print("{} ARRAY[{:d}]".format(k, len(v)))
elif type(v) is list:
print("{} 0x{:0{}x}".format(k, v[1], v[0] * 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment