/
pyxxd.py
executable file
·94 lines (74 loc) · 3.36 KB
/
pyxxd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python2
import os
import sys
from optparse import OptionParser
from StringIO import StringIO
def readChunk(data,start,end):
data.seek(start)
readdata=data.read(end)
return readdata
#Hex dump code from:
# Author: Boris Mazic
# Date: 04.06.2012
#package rfid.libnfc.hexdump
def hexbytes(xs, group_size=1, byte_separator=' ', group_separator=' '):
def ordc(c):
return ord(c) if isinstance(c,str) else c
if len(xs) <= group_size:
s = byte_separator.join('%02X' % (ordc(x)) for x in xs)
else:
r = len(xs) % group_size
s = group_separator.join(
[byte_separator.join('%02X' % (ordc(x)) for x in group) for group in zip(*[iter(xs)]*group_size)]
)
if r > 0:
s += group_separator + byte_separator.join(['%02X' % (ordc(x)) for x in xs[-r:]])
return s.lower()
def hexprint(xs):
def chrc(c):
return c if isinstance(c,str) else chr(c)
def ordc(c):
return ord(c) if isinstance(c,str) else c
def isprint(c):
return ordc(c) in range(32,127) if isinstance(c,str) else c > 31
return ''.join([chrc(x) if isprint(x) else '.' for x in xs])
def hexdump(xs, group_size=4, byte_separator=' ', group_separator='-', printable_separator=' ', address=0, address_format='%04X', line_size=16):
if address is None:
s = hexbytes(xs, group_size, byte_separator, group_separator)
if printable_separator:
s += printable_separator + hexprint(xs)
else:
r = len(xs) % line_size
s = ''
bytes_len = 0
for offset in range(0, len(xs)-r, line_size):
chunk = xs[offset:offset+line_size]
bytes = hexbytes(chunk, group_size, byte_separator, group_separator)
s += (address_format + ': %s%s\n') % (address + offset, bytes, printable_separator + hexprint(chunk) if printable_separator else '')
bytes_len = len(bytes)
if r > 0:
offset = len(xs)-r
chunk = xs[offset:offset+r]
bytes = hexbytes(chunk, group_size, byte_separator, group_separator)
bytes = bytes + ' '*(bytes_len - len(bytes))
s += (address_format + ': %s%s\n') % (address + offset, bytes, printable_separator + hexprint(chunk) if printable_separator else '')
return s
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-b", dest='bytes' , default=16, type="int", help="number of bytes to show per line")
parser.add_option("-s", dest='start' , default=0, type="int", help="starting byte")
parser.add_option("-l", dest='length' , default=16, type="int", help="length in bytes to dump")
parser.add_option("-f", dest='input', default="stdin",help="input: stdin default, drive name, filename, etc")
(options,args) = parser.parse_args()
if options.input=="stdin" or options.input == '-':
src=sys.stdin.read()
src=StringIO(src)
else:
if os.path.exists(options.input):
src=file(options.input,'rb')
else:
sys.stderr.write(options.input)
sys.stderr.write("No input file specified\n")
sys.exit()
data=readChunk(src,options.start,options.length)
print(hexdump(data, byte_separator='', group_size=2, group_separator=' ', printable_separator=' ', address=options.start, line_size=16,address_format='%07X'))