def hexdump(binary: Sequence, length: int = 16, indent: str = "", indent_size: int = 0, newline: str = '\n', prefix_offset: int = 0) -> str: """ Create a string buffer that shows the given data in hexdump format. src -> source buffer length = 16 -> number of bytes per line indent = "" -> indentation before each lines indent_size = 0 -> number of time to repeat that indentation newline = "\n" -> chars used as newline char Example of output: 00000000: 48 54 54 50 2F 31 2E 31 20 34 30 34 20 4E 6F 74 HTTP/1.1 404 Not 00000010: 20 46 6F 75 6E 64 0D 0A 43 6F 6E 74 Found..Cont ... """ generator = chunk(binary, length) line_frmt = "%%s%%08X: %%-%ss %%s" % ((length * 3) - 1) out = [ line_frmt % (indent * indent_size, prefix_offset + (addr * length), dump(d).decode(), d.translate(FILTER).decode()) for addr, d in enumerate(generator) ] return newline.join(out)
def test_hexdump(): data = bytes([random.randint(1, 255) for _ in range(10000)]) dumped = hexdump(data) line = dumped.splitlines()[random.randint(1, 200)] _ = int(line[:8], 16) assert len(line) == 77 assert line[8:11] == ": " for c in chunk(line[11:59], 3): assert c[0] in "abcdef1234567890" assert c[1] in "abcdef1234567890" assert c[2] == " " assert line[59:59 + 2] == " "
def dump(binary: Sequence, size: int = 2, sep: bytes = b" ") -> bytes: hexstr = binascii.hexlify(binary) return sep.join(chunk(hexstr, size))
def dump(binary, size=2, sep=" "): hexstr = binascii.hexlify(binary) return sep.join(chunk(hexstr.upper(), size))