ASN.1 multiple records with multiple record definitions

Papa Tana papa.tana101@REDACTED
Mon Jun 29 14:22:52 CEST 2020


Hello World!

I came across this issue as well
(http://erlang.org/pipermail/erlang-questions/2014-February/077119.html)
and thanks to this, I am now able to decode multiple records using the
same definition.

Now, the problem is as below:

My People.asn file is:

People DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
  Person ::= SEQUENCE {
    name PrintableString,
    location INTEGER {home(0),field(1),roving(2)},
    age INTEGER OPTIONAL
  }
  Contact ::= SEQUENCE {
    name VisibleString,
    phone NumericString
  }
END

Compilation is ok:

> asn1ct:compile("People", [ber]).

Let's check:

> rr('People').

Now, I am adding a 'Person' record, then a 'Contact', a 'Contact'
again, and a 'Person' as next, and so on "randomly":

> {ok, F} = file:open("CDR-people.dat", [write]).
> {ok, Bin1} = 'People':encode('Person', {'Person', "John Smith", 2, 32}).
> {ok, Bin2} = 'People':encode('Contact', {'Contact', "Alicia", "261 5687454121"}).
> {ok, Bin3} = 'People':encode('Contact', {'Contact', "Alexa", "974 3487454121"}).
> {ok, Bin4} = 'People':encode('Person', {'Person', "Rob Dummy", 2, 38}).
> {ok, Bin5} = 'People':encode('Person', {'Person', "Anderson", 2, 34}).
> file:write(F,Bin1).
> file:write(F,Bin2).
> file:write(F,Bin3).
> file:write(F,Bin4).
> file:write(F,Bin5).
> file:close(F).

As expected, when I want to decode this simple binary file as below:

{ok, F} = file:read_file("CDR-people.dat").


{ok, A, B}  = 'People':decode('Person', F).
{ok, C, D}  = 'People':decode('Person', B).
{ok, E, G}  = 'People':decode('Contact', D).

=> It gives me an error, because the second record is a 'Contact', not
a 'Person'.

How can I resolve this please?

The fact is that, I have some CDR file which contains several records
inside them, and those records are added randomly by the peer-end, so
I don't always know what would be the next content.

Am I missing something?


Thank you so much,

Kind Regards,


More information about the erlang-questions mailing list