Skip to content

Commit

Permalink
Add doctests for the hexdump utility
Browse files Browse the repository at this point in the history
Related: Gallopsled#837
Related: Gallopsled#840
  • Loading branch information
zachriggle committed Jan 10, 2017
1 parent 65f18b6 commit 6f508ba
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
pwnlib.context.context.reset_local()
pwnlib.context.ContextType.defaults['log_level'] = 'ERROR'
pwnlib.context.ContextType.defaults['randomize'] = False
pwnlib.util.fiddling.default_style = {}
pwnlib.term.text.when = 'never'
pwnlib.log.install_default_handler()
pwnlib.log.rootlogger.setLevel(1)
Expand Down
3 changes: 2 additions & 1 deletion docs/source/util/fiddling.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.. testsetup:: *

from pwnlib.util.fiddling import *
from pwn import *
from pwnlib.util.fiddling import *


:mod:`pwnlib.util.fiddling` --- Utilities bit fiddling
Expand Down
122 changes: 120 additions & 2 deletions pwnlib/util/fiddling.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def style_byte(b):

def hexdump(s, width=16, skip=True, hexii=False, begin=0,
style=None, highlight=None, cyclic=False):
"""hexdump(s, width = 16, skip = True, hexii = False, begin = 0,
r"""hexdump(s, width = 16, skip = True, hexii = False, begin = 0,
style = None, highlight = None, cyclic = False) -> str generator
Return a hexdump-dump of a string.
Expand All @@ -722,7 +722,125 @@ def hexdump(s, width=16, skip=True, hexii=False, begin=0,
Returns:
A hexdump-dump in the form of a string.
"""
Examples:
>>> print hexdump("abc")
00000000 61 62 63 │abc│
00000003
>>> print hexdump('A'*32)
00000000 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 │AAAA│AAAA│AAAA│AAAA│
*
00000020
>>> print hexdump('A'*32, width=8)
00000000 41 41 41 41 41 41 41 41 │AAAA│AAAA│
*
00000020
>>> print hexdump(cyclic(32), width=8, begin=0xdead0000, hexii=True)
dead0000 .a .a .a .a .b .a .a .a │
dead0008 .c .a .a .a .d .a .a .a │
dead0010 .e .a .a .a .f .a .a .a │
dead0018 .g .a .a .a .h .a .a .a │
dead0020
>>> print hexdump(list(map(chr, range(256))))
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │····│····│····│····│
00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f │····│····│····│····│
00000020 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f │ !"#│$%&'│()*+│,-./│
00000030 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f │0123│4567│89:;│<=>?│
00000040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f │@ABC│DEFG│HIJK│LMNO│
00000050 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f │PQRS│TUVW│XYZ[│\]^_│
00000060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f │`abc│defg│hijk│lmno│
00000070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f │pqrs│tuvw│xyz{│|}~·│
00000080 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f │····│····│····│····│
00000090 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f │····│····│····│····│
000000a0 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af │····│····│····│····│
000000b0 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf │····│····│····│····│
000000c0 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf │····│····│····│····│
000000d0 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df │····│····│····│····│
000000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef │····│····│····│····│
000000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff │····│····│····│····│
00000100
>>> print hexdump(list(map(chr, range(256))), hexii=True)
00000000 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │
00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f │
00000020 20 .! ." .# .$ .% .& .' .( .) .* .+ ., .- .. ./ │
00000030 .0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .: .; .< .= .> .? │
00000040 .@ .A .B .C .D .E .F .G .H .I .J .K .L .M .N .O │
00000050 .P .Q .R .S .T .U .V .W .X .Y .Z .[ .\ .] .^ ._ │
00000060 .` .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o │
00000070 .p .q .r .s .t .u .v .w .x .y .z .{ .| .} .~ 7f │
00000080 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f │
00000090 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f │
000000a0 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af │
000000b0 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf │
000000c0 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf │
000000d0 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df │
000000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef │
000000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ## │
00000100
>>> print hexdump('X' * 64)
00000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
*
00000040
>>> print hexdump('X' * 64, skip=False)
00000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
00000010 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
00000020 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
00000030 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
00000040
>>> print hexdump(fit({0x10: 'X'*0x20, 0x50-1: '\xff'*20}, length=0xc0) + '\x00'*32)
00000000 61 61 61 61 62 61 61 61 63 61 61 61 64 61 61 61 │aaaa│baaa│caaa│daaa│
00000010 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
*
00000030 6d 61 61 61 6e 61 61 61 6f 61 61 61 70 61 61 61 │maaa│naaa│oaaa│paaa│
00000040 71 61 61 61 72 61 61 61 73 61 61 61 74 61 61 ff │qaaa│raaa│saaa│taa·│
00000050 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff │····│····│····│····│
00000060 ff ff ff 61 7a 61 61 62 62 61 61 62 63 61 61 62 │···a│zaab│baab│caab│
00000070 64 61 61 62 65 61 61 62 66 61 61 62 67 61 61 62 │daab│eaab│faab│gaab│
00000080 68 61 61 62 69 61 61 62 6a 61 61 62 6b 61 61 62 │haab│iaab│jaab│kaab│
00000090 6c 61 61 62 6d 61 61 62 6e 61 61 62 6f 61 61 62 │laab│maab│naab│oaab│
000000a0 70 61 61 62 71 61 61 62 72 61 61 62 73 61 61 62 │paab│qaab│raab│saab│
000000b0 74 61 61 62 75 61 61 62 76 61 61 62 77 61 61 62 │taab│uaab│vaab│waab│
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │····│····│····│····│
*
000000e0
>>> print hexdump(fit({0x10: 'X'*0x20, 0x50-1: '\xff'*20}, length=0xc0) + '\x00'*32, cyclic=1)
00000000 61 61 61 61 62 61 61 61 63 61 61 61 64 61 61 61 │aaaa│baaa│caaa│daaa│
00000010 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 │XXXX│XXXX│XXXX│XXXX│
*
00000030 6d 61 61 61 6e 61 61 61 6f 61 61 61 70 61 61 61 │maaa│naaa│oaaa│paaa│
00000040 71 61 61 61 72 61 61 61 73 61 61 61 74 61 61 ff │qaaa│raaa│saaa│taa·│
00000050 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff │····│····│····│····│
00000060 ff ff ff 61 7a 61 61 62 62 61 61 62 63 61 61 62 │···a│zaab│baab│caab│
00000070 64 61 61 62 65 61 61 62 66 61 61 62 67 61 61 62 │daab│eaab│faab│gaab│
*
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │····│····│····│····│
*
000000e0
>>> print hexdump(fit({0x10: 'X'*0x20, 0x50-1: '\xff'*20}, length=0xc0) + '\x00'*32, cyclic=1, hexii=1)
00000000 .a .a .a .a .b .a .a .a .c .a .a .a .d .a .a .a │
00000010 .X .X .X .X .X .X .X .X .X .X .X .X .X .X .X .X │
*
00000030 .m .a .a .a .n .a .a .a .o .a .a .a .p .a .a .a │
00000040 .q .a .a .a .r .a .a .a .s .a .a .a .t .a .a ## │
00000050 ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## │
00000060 ## ## ## .a .z .a .a .b .b .a .a .b .c .a .a .b │
00000070 .d .a .a .b .e .a .a .b .f .a .a .b .g .a .a .b │
*
000000c0 │
*
000000e0
"""
s = packing.flat(s)
return '\n'.join(hexdump_iter(StringIO.StringIO(s),
width,
Expand Down

0 comments on commit 6f508ba

Please sign in to comment.