Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decode_root(self, decoder):
values = {}
optionals = {
optional: decoder.read_bit()
for optional in self.optionals
}
for member in self.root_members:
try:
if optionals.get(member, True):
value = member.decode(decoder)
values[member.name] = value
elif member.has_default():
values[member.name] = member.default
except DecodeError as e:
e.location.append(member.name)
raise
return values
def decode_length(_data):
raise DecodeError('Decode length is not supported for this codec.')
def decode_length_definite(encoded, offset):
length = encoded[offset]
offset += 1
if length > 127:
if length == 128:
raise DecodeError(
'Expected definite length at offset {}, but got indefinite.'.format(
offset - 1))
number_of_bytes = (length & 0x7f)
encoded_length = encoded[offset:number_of_bytes + offset]
if len(encoded_length) != number_of_bytes:
raise IndexError(
'Expected {} length byte(s) at offset {}, but got {}.'.format(
number_of_bytes,
offset,
len(encoded_length)))
length = int(binascii.hexlify(encoded_length), 16)
offset += number_of_bytes
def decode_real_special(control):
try:
return {
0x40: float('inf'),
0x41: float('-inf'),
0x42: float('nan'),
0x43: -0.0
}[control]
except KeyError:
raise DecodeError(
'Unsupported special REAL control word 0x{:02x}.'.format(control))
if (value & 0x80) == 0x00:
return value
elif (value & 0xc0) == 0x80:
return (((value & 0x7f) << 8)
| (self.read_non_negative_binary_integer(8)))
else:
try:
return {
0xc1: 16384,
0xc2: 32768,
0xc3: 49152,
0xc4: 65536
}[value]
except KeyError:
raise DecodeError(
'Bad length determinant fragmentation value 0x{:02x}.'.format(
value))
def decode_length(_data):
raise DecodeError('Decode length is not supported for this codec.')
# will raise DecodeTagError, and end of field will be
# handled in MembersType.decode() method.
if end_offset is None or offset < end_offset:
if isinstance(member, AnyDefinedBy):
value, offset = member.decode(data, offset, values)
else:
value, offset = member.decode(data, offset)
else:
raise IndexError
except (DecodeError, DecodeTagError, IndexError) as e:
if member.optional:
return offset
if not member.has_default():
if isinstance(e, IndexError):
e = DecodeError('out of data at offset {}'.format(offset))
e.location.append(member.name)
raise e
value = member.get_default()
values[member.name] = value
return offset
def decode_root(self, decoder):
if len(self.root_index_to_member) > 1:
index = self.decode_root_index(decoder)
else:
index = 0
try:
member = self.root_index_to_member[index]
except KeyError:
raise DecodeError(
'Expected choice index {}, but got {}.'.format(
self.format_root_indexes(),
index))
return (member.name, member.decode(decoder))
def decode_of(self, element):
name = element.tag
try:
member = self.name_to_member[name]
except KeyError:
raise DecodeError(
"Expected choice {}, but got '{}'.".format(
self.format_names(),
name))
return (name, member.decode(element))