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))
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))
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)
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))
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)
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)
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
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
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
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
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)
def _checkBitFieldLength(length, maxBitFieldLength): if length <= 0 or length > maxBitFieldLength: raise PythonRuntimeException("Asking for bound of bitfield with invalid length %d!" % length)
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}'!" )