예제 #1
0
def _bitsizeof_varnum(value: int, max_values: typing.Sequence[int],
                      varint_name: str) -> int:
    if value >= 0:
        abs_value = abs(value)
        for i, max_value in enumerate(max_values):
            if abs_value <= max_value:
                return (i + 1) * 8

    raise PythonRuntimeException(
        "bitsizeof: Value '%d' is out of range for %s!" % (value, varint_name))
예제 #2
0
def _getBitSizeOfVarIntImpl(value: int, maxValues: typing.Sequence[int],
                            varIntName: str) -> int:
    if value >= 0:
        absValue = abs(value)
        for i, maxValue in enumerate(maxValues):
            if absValue <= maxValue:
                return (i + 1) * 8

    raise PythonRuntimeException(
        "bitsizeof: Value '%d' is out of range for %s!" % (value, varIntName))
예제 #3
0
    def writeBits(self, value: int, numBits: int) -> None:
        """
        Writes the given value with the given number of bits to the underlying storage.

        :param value: Value to write.
        :param numBits: Number of bits to write.
        :raises PythonRuntimeException: If the value is out of the range or if the number of bits is invalid.
        """

        if numBits <= 0:
            raise PythonRuntimeException("BitStreamWriter: numBits '%d' is less than 1!" % numBits)

        minValue = 0
        maxValue = (1 << numBits) - 1
        if value < minValue or value > maxValue:
            raise PythonRuntimeException("BitStreamWriter: Value '%d' is out of the range <%d,%d>!" %
                                         (value, minValue, maxValue))

        self._writeBitsImpl(value, numBits, signed=False)
예제 #4
0
def _getBitSizeOfVarIntImpl(value, maxValues, *, signed):
    if signed or value >= 0:
        absValue = abs(value)
        for i, maxValue in enumerate(maxValues):
            if absValue <= maxValue:
                return (i + 1) * 8

    raise PythonRuntimeException("Var%sInt%s value %d is out of range!" %
                                 ("" if signed else "U",
                                  str(len(maxValues) * 8) if len(maxValues) != 9 else "",
                                  value))
예제 #5
0
    def write_bits(self, value: int, numbits: int) -> None:
        """
        Writes the given value with the given number of bits to the underlying storage.

        :param value: Value to write.
        :param numbits: Number of bits to write.
        :raises PythonRuntimeException: If the value is out of the range or if the number of bits is invalid.
        """

        if numbits <= 0:
            raise PythonRuntimeException(
                f"BitStreamWriter: numbits '{numbits}' is less than 1!")

        min_value = 0
        max_value = (1 << numbits) - 1
        if value < min_value or value > max_value:
            raise PythonRuntimeException(
                f"BitStreamWriter: Value '{value}' is out of the range "
                f"<{min_value},{max_value}>!")

        self._write_bits(value, numbits, signed=False)
예제 #6
0
    def writeSignedBits(self, value, numBits):
        """
        Writes the given signed value with the given number of bits to the underlying storage.
        Provided for convenience.

        :param value: Signed value to write.
        :param numBits: Number of bits to write.
        :raises PythonRuntimeException: If the value is out of the range or if the number of bits is invalid.
        """

        if numBits <= 0:
            raise PythonRuntimeException("BitStreamWriter.writeSignedBits: numBits '%d' is less than 1!" %
                                         numBits)

        minValue = -(1 << (numBits - 1))
        maxValue = (1 << (numBits - 1)) - 1
        if value < minValue or value > maxValue:
            raise PythonRuntimeException("BitStreamWriter.writeSignedBits: "
                                         "Value '%d' is out of the range <%d,%d>!" %
                                         (value, minValue, maxValue))

        self._writeBitsImpl(value, numBits, signed=True)
예제 #7
0
def bitsToBytes(numBits: int) -> int:
    """
    Converts number of bits to bytes.

    :param numBits: The number of bits to convert.
    :returns: Number of bytes
    :raises PythonRuntimeException: If number of bits to convert is not divisible by 8.
    """

    if numBits % 8 != 0:
        raise PythonRuntimeException(
            "bitPosition: %d is not a multiple of 8!" % numBits)

    return numBits // 8
예제 #8
0
파일: bitposition.py 프로젝트: ndsev/zserio
def bits_to_bytes(numbits: int) -> int:
    """
    Converts number of bits to bytes.

    :param numbits: The number of bits to convert.
    :returns: Number of bytes
    :raises PythonRuntimeException: If number of bits to convert is not divisible by 8.
    """

    if numbits % 8 != 0:
        raise PythonRuntimeException(
            f"bitposition: '{numbits}' is not a multiple of 8!")

    return numbits // 8
예제 #9
0
    def __init__(self, buffer, bitSize=None):
        """
        Constructs bit buffer from bytes buffer and bit size.

        :param buffer: Bytes-like buffer to construct from.
        :param bitSize: Number of bits stored in buffer to use.
        :raises PythonRuntimeException: If the numBits is invalid number of the reading goes behind the stream.
        """

        if bitSize is None:
            bitSize = len(buffer) * 8
        elif len(buffer) * 8 < bitSize:
            raise PythonRuntimeException("BitBuffer: Bit size %d out of range for given buffer byte "
                                         "size %d!" % (bitSize, len(buffer)))
        self._buffer = buffer
        self._bitSize = bitSize
예제 #10
0
    def __init__(self, buffer: bytes, bitsize: typing.Optional[int] = None) -> None:
        """
        Constructs bit buffer from bytes buffer and bit size.

        :param buffer: Bytes-like buffer to construct from.
        :param bitsize: Number of bits stored in buffer to use.
        :raises PythonRuntimeException: If bitsize is out of range.
        """

        if bitsize is None:
            bitsize = len(buffer) * 8
        elif len(buffer) * 8 < bitsize:
            raise PythonRuntimeException("BitBuffer: Bit size %d out of range for given buffer byte "
                                         "size %d!" % (bitsize, len(buffer)))
        self._buffer: bytes = buffer
        self._bitsize: int = bitsize
예제 #11
0
def _checkBitFieldLength(length: int) -> None:
    if length <= 0 or length > MAX_BITFIELD_BITS:
        raise PythonRuntimeException(
            "bitfield: Asking for bound of bitfield with invalid length %d!" %
            length)
예제 #12
0
def _checkBitFieldLength(length, maxBitFieldLength):
    if length <= 0 or length > maxBitFieldLength:
        raise PythonRuntimeException("Asking for bound of bitfield with invalid length %d!" % length)
예제 #13
0
def _check_bitfield_length(length: int) -> None:
    if length <= 0 or length > MAX_BITFIELD_BITS:
        raise PythonRuntimeException(
            f"bitfield: Asking for bound of bitfield with invalid length '{length}'!"
        )