Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _read_extended(self, offset):
next_byte = int_from_byte(self._buffer[offset])
type_num = next_byte + 7
if type_num < 7:
raise InvalidDatabaseError(
"Something went horribly wrong in the decoder. An "
"extended type resolved to a type number < 8 "
"({type})".format(type=type_num)
)
return type_num, offset + 1
def decode(self, offset):
"""Decode a section of the data section starting at offset
Arguments:
offset -- the location of the data structure to decode
"""
new_offset = offset + 1
ctrl_byte = int_from_byte(self._buffer[offset])
type_num = ctrl_byte >> 5
# Extended type
if not type_num:
(type_num, new_offset) = self._read_extended(new_offset)
try:
decoder = self._type_decoder[type_num]
except KeyError:
raise InvalidDatabaseError(
"Unexpected type number ({type}) " "encountered".format(type=type_num)
)
(size, new_offset) = self._size_from_ctrl_byte(ctrl_byte, new_offset, type_num)
return decoder(self, size, new_offset)
def _size_from_ctrl_byte(self, ctrl_byte, offset, type_num):
size = ctrl_byte & 0x1F
if type_num == 1 or size < 29:
return size, offset
if size == 29:
size = 29 + int_from_byte(self._buffer[offset])
return size, offset + 1
# Using unpack rather than int_from_bytes as it is faster
# here and below.
if size == 30:
new_offset = offset + 2
size_bytes = self._buffer[offset:new_offset]
size = 285 + struct.unpack(b"!H", size_bytes)[0]
return size, new_offset
new_offset = offset + 3
size_bytes = self._buffer[offset:new_offset]
size = struct.unpack(b"!I", b"\x00" + size_bytes)[0] + 65821
return size, new_offset