Skip to content
Snippets Groups Projects
Commit 9fab5d4d authored by Sebastien Mainand's avatar Sebastien Mainand
Browse files

Add dump option to [line|c]hexdump to get the result in a variable

parent c6e1e6cc
No related branches found
No related tags found
No related merge requests found
...@@ -64,37 +64,83 @@ def lhex(x): ...@@ -64,37 +64,83 @@ def lhex(x):
return x return x
@conf.commands.register @conf.commands.register
def hexdump(x): def hexdump(x, dump=False):
x=str(x) """ Build a tcpdump like hexadecimal view
:param x: a Packet
:param dump: define if the result must be printed or returned in a variable
:returns: a String only when dump=True
"""
s = ""
x = str(x)
l = len(x) l = len(x)
i = 0 i = 0
while i < l: while i < l:
print "%04x " % i, s += "%04x " % i
for j in xrange(16): for j in xrange(16):
if i+j < l: if i+j < l:
print "%02X" % ord(x[i+j]), s += "%02X" % ord(x[i+j])
else: else:
print " ", s += " "
if j%16 == 7: if j%16 == 7:
print "", s += ""
print " ", s += " "
print sane_color(x[i:i+16]) s += sane_color(x[i:i+16])
i += 16 i += 16
s += "\n"
# remove trailing \n
if s.endswith("\n"):
s = s[:-1]
if dump:
return s
else:
print s
@conf.commands.register @conf.commands.register
def linehexdump(x, onlyasc=0, onlyhex=0): def linehexdump(x, onlyasc=0, onlyhex=0, dump=False):
""" Build an equivalent view of hexdump() on a single line
Note that setting both onlyasc and onlyhex to 1 results in a empty output
:param x: a Packet
:param onlyasc: 1 to display only the ascii view
:param onlyhex: 1 to display only the hexadecimal view
:param dump: print the view if False
:returns: a String only when dump=True
"""
s = ""
x = str(x) x = str(x)
l = len(x) l = len(x)
if not onlyasc: if not onlyasc:
for i in xrange(l): for i in xrange(l):
print "%02X" % ord(x[i]), s += "%02X" % ord(x[i])
print "", if not onlyhex: # separate asc & hex if both are displayed
s += " "
if not onlyhex: if not onlyhex:
print sane_color(x) s += sane_color(x)
if dump:
return s
else:
print s
def chexdump(x): def chexdump(x, dump=False):
x=str(x) """ Build a per byte hexadecimal representation
print ", ".join(map(lambda x: "%#04x"%ord(x), x))
Example:
>>> chexdump(IP())
0x45, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x40, 0x00, 0x7c, 0xe7, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01
:param x: a Packet
:param dump: print the view if False
:returns: a String only if dump=True
"""
x = str(x)
s = str(", ".join(map(lambda x: "%#04x"%ord(x), x)))
if dump:
return s
else:
print s
def hexstr(x, onlyasc=0, onlyhex=0): def hexstr(x, onlyasc=0, onlyhex=0):
s = [] s = []
...@@ -104,7 +150,6 @@ def hexstr(x, onlyasc=0, onlyhex=0): ...@@ -104,7 +150,6 @@ def hexstr(x, onlyasc=0, onlyhex=0):
s.append(sane(x)) s.append(sane(x))
return " ".join(s) return " ".join(s)
@conf.commands.register @conf.commands.register
def hexdiff(x,y): def hexdiff(x,y):
"""Show differences between 2 binary strings""" """Show differences between 2 binary strings"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment