Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def write_unaligned_bytes(self, data):
assert isinstance(data, str)
for c in data:
self.write_bits(byte_to_int(c), 8)
def __str__(self):
s = '{:02x}'.format(byte_to_int(self._data[self._used])) \
if self._used < len(self._data) else '--'
return 'buffer({0:02x}/{1:d},[{2:d}]={3:s})'.format(
self._nextbits and self._next or 0,
self._nextbits,
self._used,
s
)
def read_bits(self, bits):
result = 0
resultbits = 0
while resultbits != bits:
if self._nextbits == 0:
if self.done():
raise TruncatedError(self)
self._next = byte_to_int(self._data[self._used])
self._used += 1
self._nextbits = 8
copybits = min(bits - resultbits, self._nextbits)
copy = (self._next & ((1 << copybits) - 1))
if self._bigendian:
result |= copy << (bits - resultbits - copybits)
else:
result |= copy << resultbits
self._next >>= copybits
self._nextbits -= copybits
resultbits += copybits
return result