From eb01d189dbe11554d641986cbbc19e660feff720 Mon Sep 17 00:00:00 2001 From: "T. Joseph Carter" Date: Sat, 8 Jul 2017 17:37:15 -0700 Subject: [PATCH] Move util functions out of legacy The util functions consist entirely of hexdump and its helper function right now, both of which are completely unused at the moment. I don't intend for legacy to ever call these functions, but I should start using them soon. :) --- blocksfree/legacy.py | 43 -------------------------------- blocksfree/util.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 blocksfree/util.py diff --git a/blocksfree/legacy.py b/blocksfree/legacy.py index f5382ff..d8fcf55 100755 --- a/blocksfree/legacy.py +++ b/blocksfree/legacy.py @@ -57,7 +57,6 @@ import uuid # for temp directory import subprocess #import tempfile # not used, but should be for temp directory? import struct -from typing import Sequence from binascii import a2b_hex, b2a_hex from . import diskimg @@ -895,48 +894,6 @@ def isnumber(number): #---- end IvanX general purpose functions ----# -### UTIL - -def seqsplit(seq: Sequence, num: int) -> Sequence: - """split Sequence into smaller Sequences of size 'num'""" - for i in range(0, len(seq), num): - yield seq[i:i + num] - -def hexdump( - buf: bytes, - striphigh: bool = False, - wordsize: int = 2, - sep: str = ' ', - sep2: str = ' ' - ) -> str: - """return a multi-line debugging hexdump of a bytes object""" - '''Format is configurable but defaults to that of xxd: - - ########: #### #### #### #### #### #### #### #### |................| - - wordsize is the number of bytes between separators - sep is the separator between words - sep2 is the midline separator - striphigh considers 0xa0-0xfe to be printable ASCII (as on Apple II) - ''' - out = [] - hlen = 32 + len(sep2) + (16//wordsize-2) * len(sep) - wordlen = wordsize * 2 - for i, vals in enumerate(seqsplit(buf, 16)): - hexs = sep2.join([ - sep.join(seqsplit(b2a_hex(x).decode(), wordlen)) - for x in seqsplit(vals,8) - ]) - if striphigh: - vals = [x & 0x7f for x in vals] - chars = ''.join([ - chr(x) if x >= 0x20 and x < 0x7f else '.' - for x in vals - ]) - out.append('{i:07x}0: {hexs:{hlen}} |{chars}|'.format(**locals())) - return '\n'.join(out) - - def run_cppo(): try: disk = diskimg.Disk(g.image_file) diff --git a/blocksfree/util.py b/blocksfree/util.py new file mode 100644 index 0000000..4ad890f --- /dev/null +++ b/blocksfree/util.py @@ -0,0 +1,58 @@ +# vim: set tabstop=4 shiftwidth=4 noexpandtab filetype=python: + +# Copyright (C) 2017 T. Joseph Carter +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from typing import Sequence + +def seqsplit(seq: Sequence, num: int) -> Sequence: + """split Sequence into smaller Sequences of size 'num'""" + for i in range(0, len(seq), num): + yield seq[i:i + num] + +def hexdump( + buf: bytes, + striphigh: bool = False, + wordsize: int = 2, + sep: str = ' ', + sep2: str = ' ' + ) -> str: + """return a multi-line debugging hexdump of a bytes object""" + '''Format is configurable but defaults to that of xxd: + + ########: #### #### #### #### #### #### #### #### |................| + + wordsize is the number of bytes between separators + sep is the separator between words + sep2 is the midline separator + striphigh considers 0xa0-0xfe to be printable ASCII (as on Apple II) + ''' + out = [] + hlen = 32 + len(sep2) + (16//wordsize-2) * len(sep) + wordlen = wordsize * 2 + for i, vals in enumerate(seqsplit(buf, 16)): + hexs = sep2.join([ + sep.join(seqsplit(b2a_hex(x).decode(), wordlen)) + for x in seqsplit(vals,8) + ]) + if striphigh: + vals = [x & 0x7f for x in vals] + chars = ''.join([ + chr(x) if x >= 0x20 and x < 0x7f else '.' + for x in vals + ]) + out.append('{i:07x}0: {hexs:{hlen}} |{chars}|'.format(**locals())) + return '\n'.join(out)