-
-
Save kettenis/8a810aa333e08a95222f80cea2a57d5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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