eerimoq / asn1tools

ASN.1 parsing, encoding and decoding.
MIT License
289 stars 98 forks source link

How to access named integers and enums by name #54

Open rhorenov opened 5 years ago

rhorenov commented 5 years ago

Question/Enhancement.

Is there any way how to refer to named integers and enums by name when asn1tools.compile_files(...) is used?

It would be great if the names were part of the compiled types.

Illustrative example of an ASN.1 specification with named integers:

Foo DEFINITIONS ::= BEGIN

    Constants ::= INTEGER  {
        min     (-1),
        max     (1),
        unknown (2)
    } (-1..2)

    SomeDays ::= ENUMERATED {
    monday(0),
    tuesday(1)
    }

END
eerimoq commented 5 years ago

Please give an example of how you want to use the requested functionality. It makes it lots easier to understand how/if it should be implemented.

rhorenov commented 5 years ago

Hi @eerimoq, thank you for your reply. Here is few ideas, not sure whether they are good or not or even feasible.

BASIC_ASN is the ASN.1 specification from the OP.

Access to the named values

foo = asn1tools.compile_string(BASIC_ASN, codec="uper")
type = foo.types["SomeDays"]
## Nice to have - access to enum names and values:
value = type.named_values["tuesday"]
assert value == 1
## Nice to have - access to values and names of named integers:
type = foo.types["Constants"]
value = type.named_values["min"]
assert value == -1

Possibility to encode using names of named integers

foo = asn1tools.compile_string(BASIC_ASN, codec="uper")
## Works fine
encoded = foo.encode("Constants", 1)
## Would be nice
encoded = foo.encode("Constants", "min")
eerimoq commented 5 years ago

I don't have time to think about this now. Sorry.