asn1ct(3)
π NAME π
asn1ct β the ASN.1 compiler and its compile-time support functions. That's it, that's the whole name. Welcome to asn1ct(3), bestie, we're doing this together. π«Ά
β¨ DESCRIPTION β¨
Okay so. The ASN.1 compiler takes an ASN.1 module as input and generates a corresponding Erlang module, and that generated module can encode and decode the data types you specified. Iconic behavior honestly. π
Or!! Alternatively, the compiler takes a specification module that specifies all the input modules, and generates a module with encode/decode functions. And on top of all that, there are some generic functions you can use while you're developing applications that handle ASN.1 data (encoded as BER or PER). Slay.
NOTE: By default in Erlang/OTP 17, the representation of theBIT STRINGandOCTET STRINGtypes as Erlang terms were changed.BIT STRINGvalues are now Erlang bit strings andOCTET STRINGvalues are binaries. Also, an undecoded open type is now wrapped in anasn1_OPENTYPEtuple. For details, see BIT STRING, OCTET STRING, and ASN.1 Information Objects in the User's Guide. To revert to the old representation of the types, use optionlegacy_erlang_types. π
π₯ FUNCTIONS π₯
compile(Asn1Module, Options) -> ok | {error, Reason}
compile(Asn1Module, Options) -> ok | {error, Reason}
when
Asn1Module :: atom() | string(),
Options :: [Option | OldOption],
Option ::
ber | per | uper | jer | der |
compact_bit_string | legacy_bit_string |
legacy_erlang_types | noobj |
{n2n, EnumTypeName :: term()} |
{outdir, Dir :: term()} |
{i, IncludeDir :: term()} |
asn1config | undec_rest | no_ok_wrapper |
{macro_name_prefix, Prefix} |
{record_name_prefix, Prefix} |
verbose | warnings_as_errors | deterministic,
OldOption :: ber | per,
Reason :: term(),
Prefix :: string().
So what does it do?? It compiles the ASN.1 module Asn1Module and generates an Erlang module Asn1Module.erl with encode and decode functions for all the types defined in that ASN.1 module. Chef's kiss. π
And for every ASN.1 value defined in the module, you also get an Erlang function that returns the value in Erlang representation. Free real estate.
Now, about finding the file. If Asn1Module is a filename without an extension, first .asn1 is assumed, then .asn, and finally .py (to be compatible with the old ASN.1 compiler). Asn1Module can also be a full pathname (relative or absolute) including filename, with (or without) the extension. Whatever works for you, babe.
If you need to compile a set of ASN.1 modules into one Erlang file with encode/decode functions, you list all the involved files in a configuration file, one line per file. That configuration file has to have a double extension: .set.asn1 (and the .asn1 part can alternatively be .asn or .py). So if your input files are File1.asn1, File2.asn1, and File3.asn1, the configuration file should look exactly like this:
File1.asn1
File2.asn1
File3.asn1
In that case, the output files get their names from the configuration file. If the configuration file is named SetOfFiles.set.asn1, then the names of the output files are SetOfFiles.hrl, SetOfFiles.erl, and SetOfFiles.asn1db. Cute, right? π
Sometimes in a system of ASN.1 modules, different modules can have different default tag modes β for example, one uses AUTOMATIC and another uses IMPLICIT. The multi-file compilation resolves the default tagging as if the modules were compiled separately. No cap.
Name collisions are an unwanted effect that can occur in multi-file compilation. The compiler solves this problem in one of two ways:
- If the definitions are identical, the output module keeps only one definition with the original name.
- If the definitions have the same name and differ in the definition, they get renamed. The new names are the definition name and the original module name concatenated.
If a name collision occurs, the compiler reports a "NOTICE: ..." message that tells you whether a definition was renamed, and the new name that must be used to encode/decode data. Pay attention to those, bestie, they matter.
Options is a list with options specific for the ASN.1 compiler, plus options that get applied to the Erlang compiler. The ASN.1 compiler passes on any unrecognized options to the Erlang compiler. The available options are as follows:
ber|per|uper|jerβ the encoding rule to be used. The supported encoding rules are Basic Encoding Rules (ber), Packed Encoding Rules (per) aligned, PER unaligned (uper), and JSON Encoding Rules (jer). Thejeroption can be used by itself to generate a module that only supports encoding/decoding of JER, or it can be used as a supplementary option tober,per, anduper, in which case a module that handles both the main encoding rules and JER will be generated. In that case, the exported functions for JER will bejer_encode(Type, Value)andjer_decode(Type, Bytes).
JER (ITU-T X.697) are experimental in OTP 22. There is support for a subset of the X.697 standard, for example there is no support for:
- JER encoding instructions
- the
REALtype
CHANGE: In Erlang/OTP 27 and later, module json in STDLIB is used for encoding and decoding JSON. Before Erlang/OTP 27, it was necessary to provide an external JSON library.
If the encoding rule option is omitted, ber is the default.
The generated Erlang module always gets the same name as the ASN.1 module. Therefore, only one encoding rule per ASN.1 module can be used at runtime. Pick your fighter. π₯
derβ with this option the Distinguished Encoding Rules (der) is chosen. DER is regarded as a specialized variant of the BER encoding rule. Therefore, this option only makes sense together with optionber. This option sometimes adds sorting and value checks when encoding, which implies slower encoding (the price of correctness, honestly π). The decoding routines are the same as forber.mapsβ this option changes the representation of the typesSEQUENCEandSETto use maps (instead of records). This option also suppresses the generation of.hrlfiles.
For details, see section Map representation for SEQUENCE and SET in the User's Guide.
compact_bit_stringβ theBIT STRINGtype is decoded to "compact notation".
This option is not recommended for new code.
For details, see section BIT STRING in the User's Guide.
This option implies option legacy_erlang_types, and it cannot be combined with option maps. π«
legacy_bit_stringβ theBIT STRINGtype is decoded to the legacy format, that is, a list of zeroes and ones.
This option is not recommended for new code.
For details, see section BIT STRING in the User's Guide
This option implies option legacy_erlang_types, and it cannot be combined with option maps. π«
legacy_erlang_typesβ use the same Erlang types to representBIT STRINGandOCTET STRINGas in Erlang/OTP R16.
This option is not recommended for new code.
For details, see section BIT STRING and section OCTET STRING in the User's Guide.
This option cannot be combined with option maps. π«
{n2n, EnumTypeName}β tells the compiler to generate functions for conversion between names (as atoms) and numbers, and conversely, for the specifiedEnumTypeName. You can have multiple occurrences of this option to specify several type names. The type names must be declared asENUMERATIONSin the ASN.1 specification.
If EnumTypeName does not exist in the ASN.1 specification, the compilation stops with an error code. Bye. β
The generated conversion functions are named name2num_EnumTypeName/1 and num2name_EnumTypeName/1.
noobjβ do not compile (that is, do not produce object code) the generated.erlfile. If this option is omitted, the generated Erlang module is compiled.{i, IncludeDir}β addsIncludeDirto the search-path for.asn1dband ASN.1 source files. The compiler tries to open an.asn1dbfile when a module imports definitions from another ASN.1 module. If no.asn1dbfile is found, the ASN.1 source file is parsed. You can give several{i, IncludeDir}options.{outdir, Dir}β specifies directoryDirwhere all generated files are to be placed. If this option is omitted, the files are placed in the current directory.asn1configβ when using one of the specialized decodes, exclusive or selective decode, instructions must be given in a configuration file. Optionasn1configenables specialized decodes and takes the configuration file into consideration. The configuration file has the same name as the ASN.1 specification, but with extension.asn1config.
For instructions for exclusive decode, see section Exclusive Decode in the User's Guide.
For instructions for selective decode, see section Selective Decode in the User's Guide.
undec_restβ by default when decoding, any bytes following the end of an ASN.1 data structure are discarded. If an ASN.1 module is compiled with optionundec_rest, the decode function returns a tuple{ok, Value, Rest}, whereRestis the bytes following the ASN.1 data structure.Restcan be a list or a binary.no_ok_wrapperβ with this option, the generatedencode/2anddecode/2functions do not wrap a successful return value in an{ok,...}tuple. If any error occurs, an exception will be raised. No chill. π€{macro_name_prefix, Prefix}β all macro names generated by the compiler are prefixed withPrefix. This is useful when multiple protocols that contain macros with identical names are included in a single module.{record_name_prefix, Prefix}β all record names generated by the compiler are prefixed withPrefix. This is useful when multiple protocols that contain records with identical names are included in a single module.
so yeah, picking up right where we left off with the compiler options β these are the ones that change the whole vibe of your build: π
verboseβ makes the compiler spill all the extra tea about what it's doing. more info, more chaos. π£οΈwarnings_as_errorsβ warnings? not today. this treats them as full-on errors. periodt. β οΈdeterministicβ strips every non-deterministic option from the-asn1_info()attribute. so your builds stay consistent, no cap. β¨
Unrecognized options get passed straight to the Erlang compiler when the generated .erl file is compiled. it's giving relay race. πββοΈ
The compiler generates these files, and they're all iconic in their own way:
Asn1Module.hrl(if anySETorSEQUENCEis defined)Asn1Module.erlβ Erlang module with encode, decode, and value functions. the main character fr. πAsn1Module.asn1dbβ intermediate format used by the compiler when modulesIMPORTdefinitions from each other. lowkey the glue holding the group project together. π¦
π§ͺ test(Module) -> ok | {error, Reason}
test(Module) -> ok | {error, Reason}
when Module :: module(), Reason :: term().
Tests encoding and decoding of all types in Module. for more details, see test/3. π
πΈ test(Module, Type | Options) -> ok | {error, Reason}
test(Module, Type | Options) -> ok | {error, Reason}
when
Module :: module(),
Type :: atom(),
Options :: [{i, IncludeDir :: term()}],
Reason :: term().
Tests encoding and decoding of Module. π«
If the second argument is given as atom Type, that type gets tested. if it's given as list Options, those are the options used for testing all types in the module. either way, we're testing. for more details, see test/3. π
π₯ test(Module, Type, Value | Options) -> ok | {error, Reason}
test(Module, Type, Value | Options) -> ok | {error, Reason}
when
Module :: module(),
Type :: atom(),
Value :: term(),
Options :: [{i, IncludeDir :: term()}],
Reason :: term().
Performs a test of encode and decode of types in Module. the generated functions are called by this function. it's useful for testing to make sure the generated encode and decode functions β plus the general runtime support β work as expected. π οΈ
NOTE: Currently, the test functions have many limitations. Essentially, they will mostly work for old specifications based on the 1997 standard for ASN.1, but not for most modern-style applications. Another limitation is that the test functions may not work if options that change code generations strategies such as the optionsmacro_name_prefixandrecord_name_prefixhave been used. π
test/1iterates over all types inModule. πtest/2tests typeTypewith a random value. π²test/3tests typeTypewithValue. π―
Schematically, the following occurs for each type in the module:
{ok, Value} = asn1ct:value(Module, Type),
{ok, Bytes} = Module:encode(Type, Value),
{ok, Value} = Module:decode(Type, Bytes).
The test functions use the *.asn1db files for all included modules. if they're in a different directory than the current working directory, use the include option to add paths. this is only needed when automatically generating values β for static values using Value, no options are needed. ποΈ
π value(Module, Type) -> {ok, Value} | {error, Reason}
value(Module, Type) -> {ok, Value} | {error, Reason}
when
Module :: module(),
Type :: atom(),
Value :: term(),
Reason :: term().
Returns an Erlang term that is an example of a valid Erlang representation of a value of the ASN.1 type Type. β¨
The value is a random value, and subsequent calls to this function will β for most types β return different values. so spicy. πΆοΈ
NOTE: Currently, thevaluefunction has many limitations. Essentially, it will mostly work for old specifications based on the 1997 standard for ASN.1, but not for most modern-style applications. Another limitation is that thevaluefunction may not work if options that change code generations strategies such as the optionsmacro_name_prefixandrecord_name_prefixhave been used. π
and that's the whole manual page, bestie! this comes from Ericsson AB, version asn1 5.5, and it's the asn1ct(3) page β no date in the footer, but it's still iconic. slay. π
β¨
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms