<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>1997</year><year>2012</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>asn1ct</title>
<prepared>Kenneth Lundin</prepared>
<responsible>Kenneth Lundin</responsible>
<docno>1</docno>
<approved>Kenneth Lundin</approved>
<checked></checked>
<date>97-10-14</date>
<rev>A</rev>
<file>asn1.sgml</file>
</header>
<module>asn1ct</module>
<modulesummary>ASN.1 compiler and compile-time support functions</modulesummary>
<description>
<p>The ASN.1 compiler takes an ASN.1 module as input and generates a
corresponding Erlang module which can encode and decode the data-types
specified. Alternatively the compiler takes a specification module
(se below) specifying all input modules and generates one module with
encode/decode functions. There are also some generic functions which
can be used in during development of applications which handles ASN.1
data (encoded as BER or PER).</p>
<note>
<p>In R16, the options have been simplified. The back-end is chosen
using one of the options <c>ber</c>, <c>per</c>, or <c>uper</c>.
The options <c>optimize</c>, <c>nif</c>, and <c>driver</c> options
are no longer necessary (and the ASN.1 compiler will print a
warning if they are used). The options <c>ber_bin</c>, <c>per_bin</c>,
and <c>uper_bin</c> options will still work, but will print a warning.
</p>
<p>Another change in R16 is that the generated <c>encode/2</c>
function (and <c>asn1rt:encode/3</c>) always returns a binary.
The <c>encode/2</c> function for the BER back-end used to return
an iolist.</p>
</note>
</description>
<funcs>
<func>
<name>compile(Asn1module) -> ok | {error, Reason}</name>
<name>compile(Asn1module, Options) -> ok | {error, Reason}</name>
<fsummary>Compile an ASN.1 module and generate encode/decode functions according to the encoding rules BER or PER.</fsummary>
<type>
<v>Asn1module = atom() | string()</v>
<v>Options = [Option| OldOption]</v>
<v>Option = ber | per | uper | der | compact_bit_string |
noobj | {n2n, EnumTypeName} |{outdir, Dir} | {i, IncludeDir} |
asn1config | undec_rest |
{macro_name_prefix, Prefix} | {record_name_prefix, Prefix} | verbose | warnings_as_errors</v>
<v>OldOption = ber | per</v>
<v>Reason = term()</v>
<v>Prefix = string()</v>
</type>
<desc>
<p>Compiles the ASN.1 module <c>Asn1module</c> and generates an
Erlang module <c>Asn1module.erl</c> with encode and decode
functions for the types defined in <c>Asn1module</c>. For each
ASN.1 value defined in the module an Erlang function which
returns the value in Erlang representation is generated.</p>
<p>If <c>Asn1module</c> is a filename without extension first
<c>".asn1"</c> is assumed, then <c>".asn"</c> and finally
<c>".py"</c> (to be compatible with the old ASN.1 compiler).
Of course <c>Asn1module</c> can be a full pathname (relative or
absolute) including filename with (or without) extension.
<marker id="asn1set"></marker>
</p>
<p>If one wishes to compile a set of Asn1 modules into one
Erlang file with encode/decode functions one has 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"). The input files' names
must be listed, within quotation marks (""), one at each row
in the file. If the input files are <c>File1.asn</c>,
<c>File2.asn</c> and <c>File3.asn</c> the configuration file
shall look like:</p>
<pre>
File1.asn
File2.asn
File3.asn </pre>
<p>The output files will in this case get their names from the
configuration file. If the configuration file has the name
<c>SetOfFiles.set.asn</c> the name of the output files will be
<c>SetOfFiles.hrl, SetOfFiles.erl and SetOfFiles.asn1db</c>.</p>
<p>Sometimes in a system of ASN.1 modules there are different
default tag modes, e.g. AUTOMATIC, IMPLICIT or EXPLICIT. The
multi file compilation resolves the default tagging as if
the modules were compiled separately.</p>
<p>Another unwanted effect that may occur in multi file compilation
is name collisions. The compiler solves this problem in two
ways: If the definitions are identical then the output module
keeps only one definition with the original name. But if
definitions only have same name and differs in the definition,
then they will be renamed. The new names will be the definition
name and the original module name concatenated.</p>
<p>If any name collision have occurred 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.</p>
<p>
<c>Options</c> is a list with options specific for the asn1
compiler and options that are applied to the Erlang compiler.
The latter are those that not is recognized as asn1 specific.
Available options are:
</p>
<taglist>
<tag><c>ber | per | uper</c></tag>
<item>
<p>
The encoding rule to be used. The supported encoding rules
are BER (Basic Encoding Rules),
PER aligned (Packed Encoding Rules) and PER unaligned.
If the encoding rule option is omitted <c>ber</c>
is the default.
</p>
<p>
The generated Erlang module always gets the same name
as the ASN.1 module and as a consequence of this only one
encoding rule per ASN.1 module can be used at runtime.
</p>
</item>
<tag><c>der</c></tag>
<item>
<p>
By this option the Distinguished Encoding Rules (DER) is chosen.
DER is regarded as a specialized variant of the BER encoding
rule, therefore the <c>der</c> option only makes sense together
with the <c>ber</c> option.
This option
sometimes adds sorting and value checks when encoding, which
implies a slower encoding. The decoding routines are the same
as for <c>ber</c>.
</p>
</item>
<tag><c>compact_bit_string</c></tag>
<item>
<p>
Makes it possible to use a compact notation for values
of the BIT STRING type in Erlang. The notation:
</p>
<pre>
BitString = {Unused, Binary},
Unused = integer(),
Binary = binary()
</pre>
<p>
<c>Unused</c> must be a number in the range 0 to 7. It
tells how many bits in the least significant byte in
<c>Binary</c> that is unused.
For details see
<seealso marker="asn1_ug#BIT STRING">
BIT STRING type section in users guide
</seealso>.
</p>
</item>
<tag><c>{n2n, EnumTypeName}</c></tag>
<item>
<p>
Tells the compiler to generate functions for conversion between
names (as atoms) and numbers and vice versa for the EnumTypeName specified. There can be multiple occurrences of this option in order to specify several type names. The type names must be declared as ENUMERATIONS in the ASN.1 spec.
If the EnumTypeName does not exist in the ASN.1 spec the
compilation will stop with an error code.
The generated conversion functions are named
<c>name2num_EnumTypeName/1</c> and
<c>num2name_EnumTypeName/1</c>.
</p>
</item>
<tag><c>noobj</c></tag>
<item>
<p>Do not compile (i.e do not produce object code) the generated
<c>.erl</c> file. If this option is omitted the generated Erlang module
will be compiled.</p>
</item>
<tag><c>{i, IncludeDir}</c></tag>
<item>
<p>Adds <c>IncludeDir</c> to the search-path for
<c>.asn1db</c> and asn1 source files. The compiler tries
to open a <c>.asn1db</c> file when a module imports
definitions from another ASN.1 module. If no
<c>.asn1db</c> file is found the asn1 source file is
parsed. Several <c>{i, IncludeDir}</c> can be given.
</p>
</item>
<tag><c>{outdir, Dir}</c></tag>
<item>
<p>Specifies the directory <c>Dir</c> where all generated files
shall be placed. If omitted the files are placed in the
current directory.</p>
</item>
<tag><c>asn1config</c></tag>
<item>
<p>When one of the specialized decodes, exclusive or
selective decode, is wanted one has to give instructions in
a configuration file. The option <c>asn1config</c> enables
specialized decodes and takes the configuration file, which
has the same name as the ASN.1 spec but with extension
<c>.asn1config</c>, in concern.
</p>
<p>The instructions for exclusive decode must follow the
<seealso marker="asn1_spec#Exclusive Instruction">instruction and grammar in the User's Guide</seealso>.
</p>
<p>You can also find the instructions for selective decode
in the
<seealso marker="asn1_spec#Selective Instruction">User's Guide</seealso>.
</p>
</item>
<tag><c>undec_rest</c></tag>
<item>
<p>A buffer that holds a message, being decoded may
also have some following bytes. Now it is possible to get
those following bytes returned together with the decoded
value. If an asn1 spec is compiled with this option a tuple
<c>{ok, Value, Rest}</c> is returned. <c>Rest</c> may be a
list or a binary. Earlier versions of the compiler ignored
those following bytes.</p>
</item>
<tag><c>{macro_name_prefix, Prefix}</c></tag>
<item>
<p>All macro names generated by the compiler are prefixed with
<c>Prefix</c>. This is useful when multiple protocols that contains
macros with identical names are included in a single module.</p>
</item>
<tag><c>{record_name_prefix, Prefix}</c></tag>
<item>
<p>All record names generated by the compiler are prefixed with
<c>Prefix</c>. This is useful when multiple protocols that contains
records with identical names are included in a single module.</p>
</item>
<tag><c>verbose</c></tag>
<item>
<p>Causes more verbose information from the compiler
describing what it is doing.</p>
</item>
<tag><c>warnings_as_errors</c></tag>
<item>
<p>Causes warnings to be treated as errors.</p>
</item>
</taglist>
<p>Any additional option that is applied will be passed to
the final step when the generated .erl file is compiled.
</p>
<p>The compiler generates the following files:</p>
<list type="bulleted">
<item>
<p><c>Asn1module.hrl</c> (if any SET or SEQUENCE is defined)</p>
</item>
<item>
<p><c>Asn1module.erl</c> the Erlang module with encode, decode and value functions.</p>
</item>
<item>
<p><c>Asn1module.asn1db</c> intermediate format used by the compiler when modules IMPORTS
definitions from each other.</p>
</item>
</list>
</desc>
</func>
<func>
<name>encode(Module, Type, Value)-> {ok, Bytes} | {error, Reason}</name>
<fsummary>Encode an ASN.1 value.</fsummary>
<type>
<v>Module = Type = atom()</v>
<v>Value = term()</v>
<v>Bytes = binary()</v>
<v>Reason = term()</v>
</type>
<desc>
<p>Encodes <c>Value</c> of <c>Type</c> defined in the ASN.1 module
<c>Module</c>. To get as fast execution as possible the
encode function only performs rudimentary tests that the input
<c>Value</c>
is a correct instance of <c>Type</c>. The length of strings is for example
not always checked. Returns <c>{ok, Bytes}</c> if successful or
<c>{error, Reason}</c> if an error occurred.
</p>
</desc>
</func>
<func>
<name>decode(Module, Type, Bytes) -> {ok, Value} | {error, Reason}</name>
<fsummary>Decode from Bytes into an ASN.1 value.</fsummary>
<type>
<v>Module = Type = atom()</v>
<v>Value = Reason = term()</v>
<v>Bytes = binary()</v>
</type>
<desc>
<p>Decodes <c>Type</c> from <c>Module</c> from the binary
<c>Bytes</c>. Returns <c>{ok, Value}</c> if successful.</p>
</desc>
</func>
<func>
<name>value(Module, Type) -> {ok, Value} | {error, Reason}</name>
<fsummary>Create an ASN.1 value for test purposes.</fsummary>
<type>
<v>Module = Type = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p>Returns an Erlang term which is an example of a valid Erlang
representation of a value of the ASN.1 type <c>Type</c>. The value
is a random value and subsequent calls to this function will for most
types return different values.</p>
</desc>
</func>
<func>
<name>test(Module) -> ok | {error, Reason}</name>
<name>test(Module, Type | Options) -> ok | {error, Reason}</name>
<name>test(Module, Type, Value | Options) -> ok | {error, Reason}</name>
<fsummary>Perform a test of encode and decode for types in an ASN.1 module.</fsummary>
<type>
<v>Module = Type = atom()</v>
<v>Value = term()</v>
<v>Options = [{i, IncludeDir}]</v>
<v>Reason = term()</v>
</type>
<desc>
<p>Performs a test of encode and decode of types in <c>Module</c>.
The generated functions are called by this function.
This function is useful during test to secure that the generated
encode and decode functions and the general runtime support work
as expected.</p>
<list type="bulleted">
<item>
<p><c>test/1</c> iterates over all types in <c>Module</c>.</p>
</item>
<item>
<p><c>test/2</c> tests type <c>Type</c> with a random value.</p>
</item>
<item>
<p><c>test/3</c> tests type <c>Type</c> with <c>Value</c>.</p>
</item>
</list>
<p>Schematically the following happens for each type in the module:
<code type="none">
{ok, Value} = asn1ct:value(Module, Type),
{ok, Bytes} = asn1ct:encode(Module, Type, Value),
{ok, Value} = asn1ct:decode(Module, Type, Bytes).</code></p>
<p>The <c>test</c> functions utilizes the <c>*.asn1db</c> 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 <c>Value</c> no
options are needed.</p>
</desc>
</func>
</funcs>
</erlref>