ASN.1 multiple records with multiple record definitions

Lars Thorsen lars.thorsen@REDACTED
Tue Jun 30 13:09:13 CEST 2020


Hi,
You need to specify that you have a sequence of RecordType.

List ::= SEQUENCE OF RecordType

{ok, Binx} =
        'People':encode('PList',
                        [{person, {'Person', "John Smith", 5, 38}},
                         {contact, {'Contact', "Vanessa", "123 456"}},
                         {contact, {'Contact', "Alicia", "987 654"}},
                         {person, {'Person', "Mercator", 0, 32}}]).

file:write_file("cdr.dat", Binx).

{ok, Txt} = file:read_file("cdr.dat").
'People':decode('PList', Txt).

Best Regards Lars Thorsen
OTP Team

-----Original Message-----
From: erlang-questions <erlang-questions-bounces@REDACTED> On Behalf Of Papa Tana
Sent: den 30 juni 2020 09:13
To: erlang-questions@REDACTED
Subject: Re: ASN.1 multiple records with multiple record definitions

Hello,

One recommended me to add a field so that I can identify the type of Record what I am storing.

Then, I changed my People.asn definition as below:

People DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

  RecordType ::= CHOICE {
	person	Person,
	contact	Contact
  }

  Person ::= SEQUENCE {
    name PrintableString,
    location INTEGER {home(0),field(1),roving(2)},
    age INTEGER OPTIONAL
  }

	Contact ::= SEQUENCE {
		name VisibleString,
		phone NumericString
	}

END

And I can add 4 records:
-> Person
->Contact
->Contact
->Person

{ok, Bin1} = 'People':encode('RecordType', {'person', {'Person', "John Smith", 5,38}} ).
{ok, Bin2} = 'People':encode('RecordType', {'contact', {'Contact', "Vanessa", "123 456"}} ).
{ok, Bin3} = 'People':encode('RecordType', {'contact', {'Contact', "Alicia", "987 654"}} ).
{ok, Bin4} = 'People':encode('RecordType', {'person', {'Person', "Mercator", 0,32}} ).

file:write(F,Bin1).
file:write(F,Bin2).
file:write(F,Bin3).
file:write(F,Bin4).

But when I'm reading the file, and decode, it only returns the first Person, sounds like the 3 upcoming records are ignored:

{ok, F} = file:read_file("CDR-people.dat").
{ok, Txt} = file:read_file("CDR-people.dat").
'People':decode('RecordType', Txt).

>>> {ok,{person,{'Person',"John Smith",5,38}}}


I'm struggling, I really don't figure out where am I doing it wrong, can anyone help?

Kind Regards,

2020-06-29 15:22 UTC+03:00, Papa Tana <papa.tana101@REDACTED>:
> Hello World!
>
> I came across this issue as well
> (https://protect2.fireeye.com/v1/url?k=bd349bf8-e3840660-bd34db63-861f
> cb972bfc-8fdd15469e704e9e&q=1&e=ee9e54a1-e239-4493-912f-75c8f76d6c06&u
> =http%3A%2F%2Ferlang.org%2Fpipermail%2Ferlang-questions%2F2014-Februar
> y%2F077119.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