ASN.1 and Erlang
In ancient times, when computers were large, and engineers far-sighted, when the spirit of marketers was unknown to knights of bits and terminals, in one kingdom-state, at Kalinovy Bridge, these brave men gathered and decided to come up with a way to represent arbitrary data structures to transfer them over the network so that their beautiful ladies can communicate with each other over the phone and not distract them from solving pressing problems. Since there were few marketers in those parts, the method turned out to be elegant and consuming few resources for encoding / decoding. And they called him ASN.1, or in short - X.208.
The ASN.1 protocol describes the data structure in a simple and understandable language. For example, we need to describe a message that will contain a version field (integer), message type (enumeration) and message body (binary data). We will get this description and save it in the SP.asn file:
There are many different compilers for ASN.1, both paid and free, for different programming languages, but we will focus on one of them - the compiler from the standard Erlang / OTP package. Let's execute a command like this:
After compilation, four files will be created:
Now you can, with a clear conscience, start the Erlang interpreter and encode the message:
We got a list of bytes, which can already be transferred over the network, saved
to disk and do other actions that can be done with bytes.
The reverse to coding is decoding. This is done no more complicated than coding:
These are the most basic ways to use the ASN.1 compiler from Erlang / OTP, to read more about it, read the documentation, for example, here .
The ASN.1 protocol describes the data structure in a simple and understandable language. For example, we need to describe a message that will contain a version field (integer), message type (enumeration) and message body (binary data). We will get this description and save it in the SP.asn file:
SP DEFINITIONS AUTOMATIC TAGS :: = BEGIN Message :: = SEQUENCE { version INTEGER DEFAULT 1, type MsgType, body body } MsgType :: = ENUMERATED { public (0), private (1), tralala (2) } Body :: = OCTET STRING End
There are many different compilers for ASN.1, both paid and free, for different programming languages, but we will focus on one of them - the compiler from the standard Erlang / OTP package. Let's execute a command like this:
$ erlc SP.asn
After compilation, four files will be created:
- SP.erl - Erlang encoder / decoder source code
- SP.hrl - header file containing records of the individual described structures
- SP.beam - BEAM file, compilation result of SP.erl
- SP.asn1db - a file containing the internal representation of the parsed ASN.1 file is used when one file imports the structures of another so as not to parse and analyze the imported file once again
Now you can, with a clear conscience, start the Erlang interpreter and encode the message:
[zert @ pluto]: Habrahabr $ >> erl Erlang R13B01 (erts-5.7.2) [source] [smp: 2: 2] [rq: 2] [async-threads: 0] [hipe] [kernel-poll: false] Eshell V5.7.2 (abort with ^ G) 1> rr ("SP.hrl"). ['Message'] 2> {ok, Bin} = 'SP': encode ('Message', # 'Message' {version = 3, type = private, body = "Hello, ASN.1"}). {ok, [48,20,128,1,3,129,1,1,130,12,72,101,108,108,111,44,32,65,83,78,46,49]}
We got a list of bytes, which can already be transferred over the network, saved
to disk and do other actions that can be done with bytes.
The reverse to coding is decoding. This is done no more complicated than coding:
3> 'SP': decode ('Message', Bin). {ok, # 'Message' {version = 3, type = private, body = "Hello, ASN.1"}}
These are the most basic ways to use the ASN.1 compiler from Erlang / OTP, to read more about it, read the documentation, for example, here .