From 01ef34b51943423ec6b19c4fd87d0f7bae6ae88c Mon Sep 17 00:00:00 2001 From: Martinez Date: Wed, 12 Aug 2015 18:30:38 +0200 Subject: [PATCH] add header to over.core.misc.hexdump --- core/misc.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/misc.py b/core/misc.py index 3955da5..e4adc1c 100644 --- a/core/misc.py +++ b/core/misc.py @@ -87,11 +87,12 @@ def debugger(): # -------------------------------------------------- -def hexdump(data, indent=0, offset=16, show_offsets=True, show_ascii=True, output=sys.stdout): +def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show_ascii=True, output=sys.stdout): """ Writes a hex dump of 'data' to 'output'. The output text is indented with spaces and contains 'offset' bytes per line. + If show_header is True, a single line with byte numbers preceeds all output. If show_offsets is True, each line is prefixed with the address of the first byte of that line. If show_ascii is True, each line is suffixed with its ASCII representation. Unprintable characters are replaced with a dot. @@ -101,9 +102,24 @@ def hexdump(data, indent=0, offset=16, show_offsets=True, show_ascii=True, outpu if type(data) is not bytes: raise ValueError('data must be bytes') - format_str = '%%0%dx ' %(math.ceil(math.log2(len(data)) / 8) * 2) + offset_figures = math.ceil(math.log2(len(data)) / 8) * 2 + format_str = '%%0%dx ' %(offset_figures) ptr = 0 + if show_header: + line = [] + + if show_offsets: + line.append('offset'[:offset_figures].ljust(offset_figures + 2)) + + for i in range(offset): + line.append('%2x' %(i)) + + if show_ascii: + line.append(' ASCII') + + output.write(' '.join(line) + '\n') + while data[ptr:]: if indent: output.write(' ' * indent) @@ -129,7 +145,7 @@ def hexdump(data, indent=0, offset=16, show_offsets=True, show_ascii=True, outpu output.write(' '.join(hex_bytes)) if show_ascii: - output.write(' ' + ''.join(ascii_bytes)) + output.write(' |' + ''.join(ascii_bytes) + '|') output.write('\n')