Last active
August 16, 2018 16:42
-
-
Save jn0/82e1fbb07e2dce5a457f5ec2c5a808a9 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 ctypes as c | |
import string | |
import logging; log = logging.getLogger(__name__) | |
# ... | |
def dump(obj, logger=log.info): | |
# assert isinstance(obj, c.C_TYPE) | |
B = [] | |
C = [] | |
offset = 0 | |
last = '' | |
count = 0 | |
raw = c.cast(c.addressof(obj), c.POINTER(c.c_char * c.sizeof(obj))).contents.raw # THIS IS WHAT YOU'RE LOOKING FOR | |
logger('%s size: %u = 0x%x', '*' * 8, len(raw), len(raw)) | |
for x in raw: | |
C.append(x if x in string.printable and not x.isspace() else '.') | |
B.append('%02x' % (ord(x),)) | |
if len(C) == 16: | |
s = ' '.join(B) + ' ' + ''.join(C) | |
if last != s: | |
if count: | |
logger('%s %s', '*' * 8, count) | |
count = 0 | |
logger('%08x %s', offset, s) | |
last = s | |
else: | |
count += 1 | |
offset += 16 | |
while C: C.pop() | |
while B: B.pop() | |
if count: | |
logger('%s %s', '*' * 8, count) | |
if C: | |
while len(C) < 16: | |
C.append(' ') | |
B.append(' ') | |
logger('%08x %s %s', offset, ' '.join(B), ''.join(C)) | |
# EOF # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code like
Writes something like