Ejemplo n.º 1
0
from construct.lib.py3compat import byte2int, int2byte, bytes2str

# Map an integer in the inclusive range 0-255 to its string byte representation
_printable = dict((i, ".") for i in range(256))
_printable.update((i, bytes2str(int2byte(i))) for i in range(32, 128))


def hexdump(data, linesize):
    """
    data is a bytes object. The returned result is a string.
    """
    prettylines = []
    if len(data) < 65536:
        fmt = "%%04X   %%-%ds   %%s"
    else:
        fmt = "%%08X   %%-%ds   %%s"
    fmt = fmt % (3 * linesize - 1, )
    for i in range(0, len(data), linesize):
        line = data[i:i + linesize]
        hextext = " ".join('%02x' % byte2int(b) for b in line)
        rawtext = "".join(_printable[byte2int(b)] for b in line)
        prettylines.append(fmt % (i, str(hextext), str(rawtext)))
    return prettylines


class HexString(bytes):
    """
    Represents bytes that will be hex-dumped to a string when its string
    representation is requested.
    """
    def __init__(self, data, linesize=16):
Ejemplo n.º 2
0
from construct.lib.py3compat import byte2int, int2byte, bytes2str, iteratebytes, iterateints


# Map an integer in the inclusive range 0-255 to its string byte representation
_printable = [bytes2str(int2byte(i)) if 32 <= i < 128 else '.' for i in range(256)]
_hexprint = [format(i, '02X') for i in range(256)]

def hexdump(data, linesize):
    r"""
    Turns bytes into a unicode string of the format:

    >>>print(hexdump(b'0' * 100, 16))
    0000   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0010   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0020   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0030   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0040   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0050   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0060   30 30 30 30                                       0000             
    """
    if len(data) < 16**4:
        fmt = "%%04X   %%-%ds   %%s" % (3 * linesize - 1,)
    elif len(data) < 16**8:
        fmt = "%%08X   %%-%ds   %%s" % (3 * linesize - 1,)
    else:
        raise ValueError("hexdump cannot process more than 16**8 or 4294967296 bytes")
    prettylines = []
    for i in range(0, len(data), linesize):
        line = data[i:i+linesize]
        hextext = " ".join(_hexprint[b] for b in iterateints(line))
        rawtext = "".join(_printable[b] for b in iterateints(line))
Ejemplo n.º 3
0
from construct.lib.py3compat import byte2int, int2byte, bytes2str, iteratebytes, iterateints
from binascii import hexlify, unhexlify

# Map an integer in the inclusive range 0-255 to its string byte representation
_printable = [
    bytes2str(int2byte(i)) if 32 <= i < 128 else '.' for i in range(256)
]
_hexprint = [format(i, '02X') for i in range(256)]


def hexdump(data, linesize):
    r"""
    Turns bytes into a unicode string of the format:

    >>>print(hexdump(b'0' * 100, 16))
    0000   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0010   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0020   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0030   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0040   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0050   30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30   0000000000000000
    0060   30 30 30 30                                       0000             
    """
    if len(data) < 16**4:
        fmt = "%%04X   %%-%ds   %%s" % (3 * linesize - 1, )
    elif len(data) < 16**8:
        fmt = "%%08X   %%-%ds   %%s" % (3 * linesize - 1, )
    else:
        raise ValueError(
            "hexdump cannot process more than 16**8 or 4294967296 bytes")
    prettylines = []
Ejemplo n.º 4
0
from construct.lib.py3compat import byte2int, int2byte, bytes2str


# Map an integer in the inclusive range 0-255 to its string byte representation
_printable = dict((i, ".") for i in range(256))
_printable.update((i, bytes2str(int2byte(i))) for i in range(32, 128))


def hexdump(data, linesize):
    """
    data is a bytes object. The returned result is a string.
    """
    prettylines = []
    if len(data) < 65536:
        fmt = "%%04X   %%-%ds   %%s"
    else:
        fmt = "%%08X   %%-%ds   %%s"
    fmt = fmt % (3 * linesize - 1,)
    for i in range(0, len(data), linesize):
        line = data[i : i + linesize]
        hextext = " ".join("%02x" % byte2int(b) for b in line)
        rawtext = "".join(_printable[byte2int(b)] for b in line)
        prettylines.append(fmt % (i, str(hextext), str(rawtext)))
    return prettylines


try:
    basecls = bytes
except NameError:
    basecls = str