Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def flush(self):
if self.zeros > 0:
chunks = self.zeros // 0xFF
rest = self.zeros % 0xFF
for i in range(chunks):
FixedAllocBuffers.write(self, 0)
FixedAllocBuffers.write(self, 0xFF)
if (rest != 0):
FixedAllocBuffers.write(self, 0)
FixedAllocBuffers.write(self, rest)
self.zeros = 0
FixedAllocBuffers.flush(self)
return False
# Increment the active section and fetch its pointer position
tail = self.sections[self.current_section]
self.current_section += 1
head = self.sections[self.current_section]
# Refine the new head and tail
self.section_tail, self.section_head = tail, head
# Seeks the section to 0, ready for reading
self.section_pointer = self.section_tail
return True
class CompressedFixedAllocBuffers(FixedAllocBuffers):
def __init__(self):
FixedAllocBuffers.__init__(self)
self.zeros = 0
def flush(self):
if self.zeros > 0:
chunks = self.zeros // 0xFF
rest = self.zeros % 0xFF
for i in range(chunks):
FixedAllocBuffers.write(self, 0)
FixedAllocBuffers.write(self, 0xFF)
if (rest != 0):
FixedAllocBuffers.write(self, 0)
FixedAllocBuffers.write(self, rest)
def test_buffer_overrun(self):
buf = FixedAllocBuffers()
# Fill almost entire buffer, as we want this section to be removed when we write more than the buffer can
# contain.
write_bytes(buf, [0xAA] * (FIXED_BUFFER_SIZE-10))
buf.new()
# We should have the first [0] section plus the one above.
assert len(buf.sections) == 2
# Writing 20 bytes, should overrun the buffer, and remove the section above to make room.
write_bytes(buf, [0xAA] * 20)
# We can verify this already, that one section is available.
assert len(buf.sections) == 1
# When call .new(), we are back to 2 sections.
buf.new()
assert len(buf.sections) == 2
def commit(self):
FixedAllocBuffers.commit(self)
def new(self):
FixedAllocBuffers.new(self)
def read(self):
if self.zeros > 0:
self.zeros -= 1
return 0
else:
byte = FixedAllocBuffers.read(self)
if byte == 0:
# If the bytes is zero, it means that the next byte will be the counter
self.zeros = FixedAllocBuffers.read(self)
self.zeros -= 1
return byte
def __init__(self):
FixedAllocBuffers.__init__(self)
self.zeros = 0
def seek_frame(self, v):
return FixedAllocBuffers.seek_frame(self, v)