How to use the bitstring.bitstore.ConstByteStore 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 / bitstore.py View on Github external
self._rawarray.append(joinval)
            self._rawarray.extend(store._rawarray[1:])
        else:
            self._rawarray.extend(store._rawarray)
        self.bitlength += store.bitlength

    @property
    def byteoffset(self):
        return self.offset // 8

    @property
    def rawbytes(self):
        return self._rawarray


class ByteStore(ConstByteStore):
    __slots__ = ()

    def setbit(self, pos):
        assert 0 <= pos < self.bitlength
        byte, bit = divmod(self.offset + pos, 8)
        self._rawarray[byte] |= (128 >> bit)

    def unsetbit(self, pos):
        assert 0 <= pos < self.bitlength
        byte, bit = divmod(self.offset + pos, 8)
        self._rawarray[byte] &= ~(128 >> bit)

    def invertbit(self, pos):
        assert 0 <= pos < self.bitlength
        byte, bit = divmod(self.offset + pos, 8)
        self._rawarray[byte] ^= (128 >> bit)
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def _setfile(self, filename, length, offset):
        """Use file as source of bits."""
        source = open(filename, 'rb')
        if offset is None:
            offset = 0
        if length is None:
            length = os.path.getsize(source.name) * 8 - offset
        byteoffset, offset = divmod(offset, 8)
        bytelength = (length + byteoffset * 8 + offset + 7) // 8 - byteoffset
        m = MmapByteArray(source, bytelength, byteoffset)
        if length + byteoffset * 8 + offset > m.filelength * 8:
            raise bitstring.CreationError("File is not long enough for specified "
                                "length and offset.")
        self._datastore = ConstByteStore(m, length, offset)
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
if length is None:
                length = s.len - offset
            self._setbytes_unsafe(s._datastore.rawbytes, length, s._offset + offset)
            return
        if isinstance(s, file):
            if offset is None:
                offset = 0
            if length is None:
                length = os.path.getsize(s.name) * 8 - offset
            byteoffset, offset = divmod(offset, 8)
            bytelength = (length + byteoffset * 8 + offset + 7) // 8 - byteoffset
            m = MmapByteArray(s, bytelength, byteoffset)
            if length + byteoffset * 8 + offset > m.filelength * 8:
                raise bitstring.CreationError("File is not long enough for specified "
                                    "length and offset.")
            self._datastore = ConstByteStore(m, length, offset)
            return
        if length is not None:
            raise bitstring.CreationError("The length keyword isn't applicable to this initialiser.")
        if offset:
            raise bitstring.CreationError("The offset keyword isn't applicable to this initialiser.")
        if isinstance(s, basestring):
            bs = self._converttobitstring(s)
            assert bs._offset == 0
            self._setbytes_unsafe(bs._datastore.rawbytes, bs.length, 0)
            return
        if isinstance(s, (bytes, bytearray)):
            self._setbytes_unsafe(bytearray(s), len(s) * 8, 0)
            return
        if isinstance(s, numbers.Integral):
            # Initialise with s zero bits.
            if s < 0:
github scott-griffiths / bitstring / bitstring / bits.py View on Github external
def __new__(cls, auto=None, length=None, offset=None, _cache={}, **kwargs):
        # For instances auto-initialised with a string we intern the
        # instance for re-use. 
        try:
            if isinstance(auto, basestring):
                try:
                    return _cache[auto]
                except KeyError:
                    x = object.__new__(Bits)
                    try:
                        _, tokens = tokenparser(auto)
                    except ValueError as e:
                        raise bitstring.CreationError(*e.args)
                    x._datastore = bitstore.ConstByteStore(bytearray(0), 0, 0)
                    for token in tokens:
                        x._datastore._appendstore(Bits._init_with_token(*token)._datastore)
                    assert x._assertsanity()
                    _cache[auto] = x
                    return x
        except TypeError:
            pass
        x = object.__new__(Bits)
        x._initialise(auto, length, offset, **kwargs)
        return x