How to use the bitstring.InterpretError function in bitstring

To help you get started, we’ve selected a few bitstring examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _readuint(self, length, start):
        """Read bits and interpret as an unsigned int."""
        if not length:
            raise bitstring.InterpretError("Cannot interpret a zero length bitstring "
                                           "as an integer.")
        offset = self._offset
        startbyte = (start + offset) // 8
        endbyte = (start + offset + length - 1) // 8

        b = binascii.hexlify(bytes(self._datastore.getbyteslice(startbyte, endbyte + 1)))
        if not b:
            return 0
        i = int(b, 16)
        final_bits = 8 - ((start + offset + length) % 8)
        if final_bits != 8:
            i >>= final_bits
        i &= (1 << length) - 1
        return i
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _readuintle(self, length, start):
        """Read bits and interpret as a little-endian unsigned int."""
        if length % 8:
            raise bitstring.InterpretError("Little-endian integers must be whole-byte. "
                                 "Length = {0} bits.", length)
        assert start + length <= self.len
        absolute_pos = start + self._offset
        startbyte, offset = divmod(absolute_pos, 8)
        val = 0
        if not offset:
            endbyte = (absolute_pos + length - 1) // 8
            chunksize = 4 # for 'L' format
            while endbyte - chunksize + 1 >= startbyte:
                val <<= 8 * chunksize
                val += struct.unpack('
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _readoct(self, length, start):
        """Read bits and interpret as an octal string."""
        if length % 3:
            raise bitstring.InterpretError("Cannot convert to octal unambiguously - "
                                 "not multiple of 3 bits.")
        if not length:
            return ''
        # Get main octal bit by converting from int.
        # Strip starting 0 or 0o depending on Python version.
        end = oct(self._readuint(length, start))[LEADING_OCT_CHARS:]
        if end.endswith('L'):
            end = end[:-1]
        middle = '0' * (length // 3 - len(end))
        return middle + end
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _readhex(self, length, start):
        """Read bits and interpret as a hex string."""
        if length % 4:
            raise bitstring.InterpretError("Cannot convert to hex unambiguously - "
                                           "not multiple of 4 bits.")
        if not length:
            return ''
        # This monstrosity is the only thing I could get to work for both 2.6 and 3.1.
        # TODO: Optimize: This really shouldn't call __getitem__.
        # TODO: Is utf-8 really what we mean here?
        s = str(binascii.hexlify(self[start:start + length].tobytes()).decode('utf-8'))
        # If there's one nibble too many then cut it off
        return s[:-1] if (length // 4) % 2 else s
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _readintbe(self, length, start):
        """Read bits and interpret as a big-endian signed int."""
        if length % 8:
            raise bitstring.InterpretError("Big-endian integers must be whole-byte. "
                                 "Length = {0} bits.", length)
        return self._readint(length, start)
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _getbool(self):
        if self.length != 1:
            msg = "For a bool interpretation a bitstring must be 1 bit long, not {0} bits."
            raise bitstring.InterpretError(msg, self.length)
        return self[0]
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
"""Read bits and interpret as a little-endian float."""
        startbyte, offset = divmod(start + self._offset, 8)
        if not offset:
            if length == 32:
                f, = struct.unpack('
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _getuie(self):
        """Return data as unsigned interleaved exponential-Golomb code.

        Raises InterpretError if bitstring is not a single exponential-Golomb code.

        """
        try:
            value, newpos = self._readuie(0)
            if value is None or newpos != self.len:
                raise bitstring.ReadError
        except bitstring.ReadError:
            raise bitstring.InterpretError("Bitstring is not a single interleaved exponential-Golomb code.")
        return value
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _getsie(self):
        """Return data as signed interleaved exponential-Golomb code.

        Raises InterpretError if bitstring is not a single exponential-Golomb code.

        """
        try:
            value, newpos = self._readsie(0)
            if value is None or newpos != self.len:
                raise bitstring.ReadError
        except bitstring.ReadError:
            raise bitstring.InterpretError("Bitstring is not a single interleaved exponential-Golomb code.")
        return value