asn1ct(3erl)
π NAME π
asn1ct β ASN.1 compiler and compile-time support functions. This page is asn1ct(3erl), aka the Erlang Module Definition for the ASN.1 compiler, and yes bestie, weβre gonna get through it together. π«Ά
π DESCRIPTION π
okay so picture this: the ASN.1 compiler takes an ASN.1 module as input and generates a corresponding Erlang module, which can encode and decode the specified data types. Slay. π Alternatively, the compiler takes a specification module specifying all input modules, and generates a module with encode/decode functions. In addition, some generic functions can be used during development of applications that handles ASN.1 data (encoded as BER or PER). Very useful, very iconique. π
π‘ Note
By default in OTP 17, the representation of the BIT STRING and OCTET STRING types as Erlang terms were changed. BIT STRING values are now Erlang bit strings and OCTET STRING values are binaries. Also, an undecoded open type is now wrapped in an asn1_OPENTYPE tuple. 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 option legacy_erlang_types. No cap, this one matters. π₯
π‘ Note
In OTP R16, the options were simplified. The back end is chosen using one of the options ber, per, uper or jer. Options optimize, nif, and driver options are no longer necessary (and the ASN.1 compiler generates a warning if they are used). Options ber_bin, per_bin, and uper_bin options still work, but generates a warning. Another change in OTP R16 is that the generated function encode/2 always returns a binary. Function encode/2 for the BER back end used to return an iolist. Vibe check: binaries only now. β¨
β¨ EXPORTS β¨
compile(Asn1module) -> ok | {error, Reason}
compile(Asn1module, Options) -> ok | {error, Reason}
π§Ύ Types
Asn1module = atom() | string()
Options = [Option| OldOption]
Option = ber | per | uper | jer | der | compact_bit_string | legacy_bit_string | legacy_erlang_types | noobj | {n2n, EnumTypeName} |{outdir, Dir} | {i, IncludeDir} | 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()
Compiles the ASN.1 module Asn1module and generates an Erlang module Asn1module.erl with encode and decode functions for the types defined in Asn1module. For each ASN.1 value defined in the module, an Erlang function that returns the value in Erlang representation is generated. Generated code, but make it Erlang. π
If Asn1module is a filename without extension, first ".asn1" is assumed, then ".asn", and finally ".py" (to be compatible with the old ASN.1 compiler). Asn1module can be a full pathname (relative or absolute) including filename with (or without) extension. Flexible filename behavior, lowkey. π¦
If it is needed to compile a set of ASN.1 modules into an Erlang file with encode/decode functions, ensure to list all involved files in a configuration file. This configuration file must have a double extension ".set.asn" (".asn" can alternatively be ".asn1" or ".py"). List the input file names within quotation marks (""), one at each row in the file. If the input files are File1.asn, File2.asn, and File3.asn, the configuration file must look as follows:
File1.asn
File2.asn
File3.asn
The output files in this case get their names from the configuration file. If the configuration file is named SetOfFiles.set.asn, the names of the output files are SetOfFiles.hrl, SetOfFiles.erl, and SetOfFiles.asn1db. Periodt, naming handled. π
Sometimes in a system of ASN.1 modules, different default tag modes, for example, AUTOMATIC, IMPLICIT, or EXPLICIT. The multi-file compilation resolves the default tagging as if the modules were compiled separately. Not me keeping track of tags for you. π
Name collisions is another 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 differs in the definition, they are 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 if a definition was renamed, and the new name that must be used to encode/decode data. Watch for the NOTICE, bestie. π
Options is a list with options specific for the ASN.1 compiler and options that are applied to the Erlang compiler. The latter are not recognized as ASN.1 specific. 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 to JER or it can be used as a supplementary option tober,peranduper. In the latter case a module with for both the main encoding rules and JER will be generated. The exported functions for JER will then bejer_encode(Type, Value)andjer_decode(Type, Bytes).- The
jerencoding rules (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
- Also note that when using the
jerencoding rules the generated module will get a dependency to an external json component. The generated code is currently tested together with:jsxwhich currently is the default.jsonecan be chosen instead ofjsxby providing the option{d,jsone}.
- If the encoding rule option is omitted,
beris 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.
- The
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 a slower encoding. The decoding routines are the same as forber. BERβs fancy cousin, but slower sometimes. πΈ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 SectionMap representation for SEQUENCE and SETin the Userβs Guide. Maps girlies, rise. πΊοΈcompact_bit_stringβ theBIT STRINGtype is decoded to βcompact notationβ. This option is not recommended for new code. This option cannot be combined with the optionmaps. For details, see SectionBIT STRINGin the Userβs Guide. This option implies optionlegacy_erlang_types. Legacy vibes, handle with care. π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. This option cannot be combined with the optionmaps. For details, see SectionBIT STRINGin the Userβs Guide. This option implies optionlegacy_erlang_types. Old-school zeroes and ones. πlegacy_erlang_typesβ use the same Erlang types to representBIT STRINGandOCTET STRINGas in OTP R16. For details, see SectionBIT STRINGand SectionOCTET STRINGin the Userβs Guide. This option is not recommended for new code. This option cannot be combined with the optionmaps. R16 representation, but make it optional. π¦{n2n, EnumTypeName}β tells the compiler to generate functions for conversion between names (as atoms) and numbers and conversely for the specifiedEnumTypeName. There can be multiple occurrences of this option to specify several type names. The type names must be declared asENUMERATIONSin the ASN.1 specification. IfEnumTypeNamedoes not exist in the ASN.1 specification, the compilation stops with an error code. The generated conversion functions are namedname2num_EnumTypeName/1andnum2name_EnumTypeName/1. Name-to-number, number-to-name, iconic. π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. No object code, no problem. π«Ά{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. Several{i, IncludeDir}can be given. Include dirs, stack βem. π{outdir, Dir}β specifies directoryDirwhere all generated files are to be placed. If this option is omitted, the files are placed in the current directory. Put it where you want it. π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 in concern. The configuration file has the same name as the ASN.1 specification, but with extension.asn1config. For instructions for exclusive decode, see SectionExclusive Decodein the Userβs Guide. For instructions for selective decode, see SectionSelective Decodein the Userβs Guide. Specialized decode config, iykyk. π§undec_restβ a buffer that holds a message, being decoded it can also have some following bytes. Those following bytes can now be returned together with the decoded value. If an ASN.1 specification is compiled with this option, a tuple{ok, Value, Rest}is returned.Restcan be a list or a binary. Earlier versions of the compiler ignored those following bytes. Leftover bytes? Now returned, bestie. πno_ok_wrapperβ with this one, the generatedencode/2anddecode/2functions do not wrap a successful return value in an{ok,...}tuple. If any error occurs, an exception will be raised. π{macro_name_prefix, Prefix}β every macro name generated by the compiler gets prefixed withPrefix. This is iconic when multiple protocols that contain macros with identical names are included in a single module. β¨{record_name_prefix, Prefix}β same energy for record names: all record names generated by the compiler get prefixed withPrefix. Use it when multiple protocols that contain records with identical names are included in a single module. πverboseβ causes more verbose information from the compiler describing what it is doing. π£οΈwarnings_as_errorsβ causes warnings to be treated as errors. no cap. β οΈdeterministicβ causes all non-deterministic options to be stripped from the-asn1_info()attribute. π§
Any more option that is applied is passed to the final step when the generated .erl file is compiled.
The compiler generates the following files, bestie:
Asn1module.hrl(if anySETorSEQUENCEis defined)Asn1module.erlβ Erlang module withencode,decode, andvaluefunctionsAsn1module.asn1dbβ intermediate format used by the compiler when modulesIMPORTdefinitions from each other.
value(Module, Type) -> {ok, Value} | {error, Reason}
Types:
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. π²
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. π
test(Module) -> ok | {error, Reason}
test(Module, Type | Options) -> ok | {error, Reason}
test(Module, Type, Value | Options) -> ok | {error, Reason}
Types:
Module = Type = atom()
Value = term()
Options = [{i, IncludeDir}]
Reason = term()
Performs a test of encode and decode of types in Module. The generated functions are called by this function. This function is useful during test to secure that the generated encode and decode functions as well as 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 are located 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. πΈ
Thatβs the whole vibe π. Big credit to Ericsson AB β this is the asn1 5.0.21.1 edition, and the page tag is asn1ct(3erl). Go forth and ASN.1, bestie. ππ¦
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms