Commit 253eb71217e59528869f1bd65acb3eebbe166e7d
1 parent
8b184d70
ezhexviewer: more fixes to run on Python 2+3 (issue #62)
Showing
1 changed file
with
14 additions
and
9 deletions
oletools/ezhexviewer.py
| ... | ... | @@ -63,6 +63,9 @@ if sys.version_info[0] >= 3: |
| 63 | 63 | # Python 3 specific adaptations |
| 64 | 64 | # py3 range = py2 xrange |
| 65 | 65 | xrange = range |
| 66 | + PYTHON3 = True | |
| 67 | +else: | |
| 68 | + PYTHON3 = False | |
| 66 | 69 | |
| 67 | 70 | def xord(char): |
| 68 | 71 | ''' |
| ... | ... | @@ -88,12 +91,12 @@ def bchr(x): |
| 88 | 91 | :param x: int |
| 89 | 92 | :return: chr(x) as a bytes string |
| 90 | 93 | ''' |
| 91 | - if sys.version_info[0] <= 2: | |
| 92 | - return chr(x) | |
| 93 | - else: | |
| 94 | + if PYTHON3: | |
| 94 | 95 | # According to the Python 3 documentation, bytes() can be |
| 95 | 96 | # initialized with an iterable: |
| 96 | 97 | return bytes([x]) |
| 98 | + else: | |
| 99 | + return chr(x) | |
| 97 | 100 | |
| 98 | 101 | #------------------------------------------------------------------------------ |
| 99 | 102 | # The following code (hexdump3 only) is a modified version of the hex dumper |
| ... | ... | @@ -103,7 +106,7 @@ def bchr(x): |
| 103 | 106 | # PSF license: http://docs.python.org/license.html |
| 104 | 107 | # Copyright (c) 2001-2012 Python Software Foundation; All Rights Reserved |
| 105 | 108 | |
| 106 | -FILTER = b''.join([(len(repr(bchr(x)))<=4) and bchr(x) or b'.' for x in range(256)]) | |
| 109 | +FILTER = b''.join([(len(repr(bchr(x)))<=4 and x != 0x0A) and bchr(x) or b'.' for x in range(256)]) | |
| 107 | 110 | |
| 108 | 111 | def hexdump3(src, length=8, startindex=0): |
| 109 | 112 | """ |
| ... | ... | @@ -113,11 +116,13 @@ def hexdump3(src, length=8, startindex=0): |
| 113 | 116 | """ |
| 114 | 117 | result=[] |
| 115 | 118 | for i in xrange(0, len(src), length): |
| 116 | - s = src[i:i+length] | |
| 117 | - hexa = ' '.join(["%02X" % xord(x) for x in s]) | |
| 118 | - printable = s.translate(FILTER) | |
| 119 | - printable = printable.decode('latin1') | |
| 120 | - result.append("%08X %-*s %s\n" % (i+startindex, length*3, hexa, printable)) | |
| 119 | + s = src[i:i+length] | |
| 120 | + hexa = ' '.join(["%02X" % xord(x) for x in s]) | |
| 121 | + printable = s.translate(FILTER) | |
| 122 | + if PYTHON3: | |
| 123 | + # On Python 3, need to convert printable from bytes to str: | |
| 124 | + printable = printable.decode('latin1') | |
| 125 | + result.append("%08X %-*s %s\n" % (i+startindex, length*3, hexa, printable)) | |
| 121 | 126 | return ''.join(result) |
| 122 | 127 | |
| 123 | 128 | # end of PSF-licensed code. | ... | ... |