Skip to content

Instantly share code, notes, and snippets.

@kettenis
Created March 19, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kettenis/8a810aa333e08a95222f80cea2a57d5d to your computer and use it in GitHub Desktop.
Save kettenis/8a810aa333e08a95222f80cea2a57d5d to your computer and use it in GitHub Desktop.
import struct
import sys
dtnode = 'II'
dtprop = '32sI'
def print_node(fp, indent):
node = fp.read(struct.calcsize(dtnode))
(nprops, nchilds) = struct.unpack(dtnode, node)
while nprops > 0:
prop = fp.read(struct.calcsize(dtprop))
(name, length) = struct.unpack(dtprop, prop)
name = name.decode('utf-8').rstrip('\x00')
value = fp.read(length)
try:
s = value.decode('utf-8').rstrip('\x00')
if s.isprintable() and len(s) > 1:
value = s
else:
raise(Hell)
except:
s = ''
if length % 4 == 0:
count = length / 4
while count > 0:
s = s + ("%08x " % struct.unpack('I', value[0:4])[0])
value = value[4:]
count -= 1
continue
value = s
else:
count = length
while count > 0:
s = s + ("%02x " % value[0])
value = value[1:]
count -= 1
continue
value = s
pass
pass
print(indent + name + ': ' + value)
if length % 4:
fp.read(4 - length % 4)
pass
nprops -= 1
continue
print()
while nchilds > 0:
print_node(fp, indent + ' ')
nchilds -= 1
continue
fp = open(sys.argv[1], 'rb')
print_node(fp, '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment