コード例 #1
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack64_many(s):
    r"""Unpacks a bytestring into a number of integers.

    The integers are unpacked as 64-bit, unsigned, little-endian values.

    Examples:
        >>> from pcstools.packing.big_endian import unpack64_many
        >>> unpack64_many(b'AAAAAAAA')
        [4702111234474983745]
        >>> unpack64_many(b'\x00\x00\x00\x00\x00\x00\x00\x01')
        [1]
        >>> unpack64_many(b'')
        []
        >>> unpack64_many(b'AAAAAAAABBBBBBBB')
        [4702111234474983745, 4774451407313060418]
        >>> unpack64_many(u'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
        >>> unpack64_many(b'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be divisible into groups of 8 bytes
    """
    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) % 8 != 0:
        raise ValueError("Argument must divisible into groups of 8 bytes")

    return [struct.unpack('>Q', s[n:n + 8])[0] for n in range(0, len(s), 8)]
コード例 #2
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack64(s):
    r"""Unpacks a bytestring into an integer.

    The integer is unpacked as a 64-bit, unsigned, big-endian value.

    Examples:
        >>> from pcstools.packing.big_endian import unpack64
        >>> unpack64(b'ABCDEFGH')
        4702394921427289928
        >>> unpack64(b'\x01\x00\x00\x00\x00\x00\x00\x00')
        72057594037927936
        >>> unpack64(b'\x00\x00\x00\x00\x00\x00\x00\x01')
        1
        >>> unpack64(b'')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be of length 8
        >>> unpack64(u'ABCDEFGH')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
    """

    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) != 8:
        raise ValueError("Argument must be of length 8")

    return struct.unpack('>Q', s)[0]
コード例 #3
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack32_many(s):
    r"""Unpacks a bytestring into a number of integers.

    The integers are unpacked as 32-bit, unsigned, little-endian values.

    Examples:
        >>> from pcstools.packing.big_endian import unpack32_many
        >>> unpack32_many(b'AAAA')
        [1094795585]
        >>> unpack32_many(b'\x00\x00\x00\x01')
        [1]
        >>> unpack32_many(b'')
        []
        >>> unpack32_many(b'AAAABBBB')
        [1094795585, 1111638594]
        >>> unpack32_many(u'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
        >>> unpack32_many(b'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be divisible into groups of 4 bytes
    """
    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) % 4 != 0:
        raise ValueError("Argument must divisible into groups of 4 bytes")

    return [struct.unpack('>I', s[n:n + 4])[0] for n in range(0, len(s), 4)]
コード例 #4
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack32(s):
    r"""Unpacks a bytestring into an integer.

    The integer is unpacked as a 32-bit, unsigned, big-endian value.

    Examples:
        >>> from pcstools.packing.big_endian import unpack32
        >>> unpack32(b'ABCD')
        1094861636
        >>> unpack32(b'\x01\x00\x00\x00')
        16777216
        >>> unpack32(b'\x00\x00\x00\x01')
        1
        >>> unpack32(b'')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be of length 4
        >>> unpack32(u'ABCD')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
    """

    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) != 4:
        raise ValueError("Argument must be of length 4")

    return struct.unpack('>I', s)[0]
コード例 #5
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack16(s):
    r"""Unpacks a bytestring into an integer.

    The integer is unpacked as a 16-bit, unsigned, big-endian value.

    Examples:
        >>> from pcstools.packing.big_endian import unpack16
        >>> unpack16(b'AB')
        16706
        >>> unpack16(b'\x01\x00')
        256
        >>> unpack16(b'\x00\x01')
        1
        >>> unpack16(b'')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be of length 2
        >>> unpack16(u'AB')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
    """

    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) != 2:
        raise ValueError("Argument must be of length 2")

    return struct.unpack('>H', s)[0]
コード例 #6
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack8(s):
    r"""Unpacks a bytestring into an integer.

    The integer is unpacked as an 8-bit, unsigned, big-endian value.

    Examples:
        >>> from pcstools.packing.big_endian import unpack8
        >>> unpack8(b'A')
        65
        >>> unpack8(b'\x01')
        1
        >>> unpack8(b'')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be of length 1
        >>> unpack8(u'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
    """

    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) != 1:
        raise ValueError("Argument must be of length 1")

    return struct.unpack('>B', s)[0]
コード例 #7
0
def unpack16_many(s):
    r"""Unpacks a bytestring into a number of integers.

    The integers are unpacked as 16-bit, unsigned, little-endian values.

    Examples:
        >>> from pcstools.packing.little_endian import unpack16_many
        >>> unpack16_many(b'AA')
        [16705]
        >>> unpack16_many(b'\x01\x00')
        [1]
        >>> unpack16_many(b'')
        []
        >>> unpack16_many(b'AABB')
        [16705, 16962]
        >>> unpack16_many(u'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
        >>> unpack16_many(b'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be divisible into groups of 2 bytes
    """
    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    if len(s) % 2 != 0:
        raise ValueError("Argument must divisible into groups of 2 bytes")

    return [struct.unpack('<H', s[n:n + 2])[0] for n in range(0, len(s), 2)]
コード例 #8
0
def pack(format, bits, arg):
    if is_int(arg):
        if 0 <= arg < 2**bits:
            return struct.pack(format, arg)
        else:
            raise ValueError("Number must be positive and below 2**{}, but number == {}".format(
                bits, arg))
    elif is_bytes(arg):
        return arg
    elif is_unicode(arg):
        return arg.encode('utf-8')
    elif is_bytearray(arg):
        return bytes(arg)
    elif is_iterable(arg):
        return b''.join(pack(format, bits, a) for a in arg)
    else:
        raise ValueError(u"Cannot pack value {}".format(arg))
コード例 #9
0
ファイル: big_endian.py プロジェクト: DIKU-PCS/pcstools
def unpack8_many(s):
    r"""Unpacks a bytestring into a number of integers.

    The integers are unpacked as 8-bit, unsigned, little-endian values.

    Examples:
        >>> from pcstools.packing.big_endian import unpack8_many
        >>> unpack8_many(b'A')
        [65]
        >>> unpack8_many(b'\x01')
        [1]
        >>> unpack8_many(b'')
        []
        >>> unpack8_many(b'ABC')
        [65, 66, 67]
        >>> unpack8_many(u'A')
        Traceback (most recent call last):
           ...
        ValueError: Argument must be a bytestring
    """
    if not is_bytes(s):
        raise ValueError("Argument must be a bytestring")

    return [struct.unpack('>B', s[n:n + 1])[0] for n in range(len(s))]