How to use the asn1tools.codecs.DecodeError function in asn1tools

To help you get started, we’ve selected a few asn1tools 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 eerimoq / asn1tools / asn1tools / codecs / per.py View on Github external
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
github eerimoq / asn1tools / asn1tools / codecs / uper.py View on Github external
def decode_length(_data):
    raise DecodeError('Decode length is not supported for this codec.')
github eerimoq / asn1tools / asn1tools / codecs / ber.py View on Github external
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
github eerimoq / asn1tools / asn1tools / codecs / ber.py View on Github external
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))
github eerimoq / asn1tools / asn1tools / codecs / per.py View on Github external
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))
github eerimoq / asn1tools / asn1tools / codecs / per.py View on Github external
def decode_length(_data):
    raise DecodeError('Decode length is not supported for this codec.')
github eerimoq / asn1tools / asn1tools / codecs / ber.py View on Github external
# 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
github eerimoq / asn1tools / asn1tools / codecs / per.py View on Github external
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))
github eerimoq / asn1tools / asn1tools / codecs / xer.py View on Github external
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))