From 9058cd48435dca77d59b0010d14fb879e6ae2a9a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= The decode functions will return a record as result when decoding
- a A For instance, if the following types exists in a file "File.asn":
MyPdu = #'Pdu'{a=22,b=77.99,c={0,1,2,3,4},d='NULL'}.
A
An application can use the atom
Depending on the encoding rules, the encoder may also compare + the given value to the default value and automatically omit the + encoding if they are equal. How much effort the encoder makes to + to compare the values depends on the encoding rules. The DER + encoding rules forbids encoding a value equal to the default value, + so it has a more thorough and time-consuming comparison than the + encoders for the other encoding rules.
+ +In the following example we will use this ASN.1 specification:
+File DEFINITIONS AUTOMATIC TAGS ::= +BEGIN Seq1 ::= SEQUENCE { a INTEGER DEFAULT 1, b Seq2 DEFAULT {aa TRUE, bb 15} @@ -901,60 +913,37 @@ Seq2 ::= SEQUENCE { aa BOOLEAN, bb INTEGER } --
Some values and the corresponding encoding in an Erlang terminal - is shown below:
+ +Seq3 ::= SEQUENCE { + bs BIT STRING {a(0), b(1), c(2)} DEFAULT {a, c} +} +END +Here is an example where the BER encoder is able to omit encoding + of the default values:
-1> asn1ct:compile('File'). -Erlang ASN.1 version "1.3.2" compiling "File.asn1" -Compiler Options: [] ---{generated,"File.asn1db"}-- ---{generated,"File.hrl"}-- ---{generated,"File.erl"}-- +1> asn1ct:compile('File', [ber]). ok -2> 'File':encode('Seq1',{'Seq1',asn1_DEFAULT,asn1_DEFAULT}). -{ok,["0",[0],[[],[]]]} -3> lists:flatten(["0",[0],[[],[]]]). -[48,0] -4> 'File':encode('Seq1',{'Seq1',1,{'Seq2',true,15}}). -{ok,["0","\\b",[[],["\\241",[6],[[[128],[1],"\\377"],[[129],[1],[15]]]]]]} -5> lists:flatten(["0","\\b",[[],["\\241",[6],[[[128],[1],"\\377"],[[129],[1],[15]]]]]]). -[48,8,161,6,128,1,255,129,1,15] -6>-
The result after command line 3, in the example above,shows that the
- encoder omits the encoding of default values when they are specific
- by asn1_DEFAULT. Line 5 shows that even primitive values that equals
- the default value are detected and not encoded. But the constructed
- value of component
But, the DER encoding format has stronger requirements regarding - default values both for SET and SEQUENCE. A more elaborate and time - expensive check of default values will take place. The following is - an example with the same types and values as above but with der - encoding format.
--1> asn1ct:compile('File',[der]). -Erlang ASN.1 version "1.3.2" compiling "File.asn1" -Compiler Options: [der] ---{generated,"File.asn1db"}-- ---{generated,"File.hrl"}-- ---{generated,"File.erl"}-- +2> 'File':encode('Seq1', {'Seq1',asn1_DEFAULT,asn1_DEFAULT}). +{ok,<<48,0>>} +3> 'File':encode('Seq1', {'Seq1',1,{'Seq2',true,15}}). +{ok,<<48,0>>}+ +
And here is an example with a named BIT STRING where the BER + encoder will not omit the encoding:
++4> 'File':encode('Seq3', {'Seq3',asn1_DEFAULT). +{ok,<<48,0>>} +5> 'File':encode('Seq3', {'Seq3',<<16#101:3>>). +{ok,<<48,4,128,2,5,160>>}+ +
The DER encoder will omit the encoding for the same BIT STRING:
++6> asn1ct:compile('File', [ber,der]). ok -2> 'File':encode('Seq1',{'Seq1',asn1_DEFAULT,asn1_DEFAULT}). -{ok,["0",[0],[[],[]]]} -3> lists:flatten(["0",[0],[[],[]]]). -[48,0] -4> 'File':encode('Seq1',{'Seq1',1,{'Seq2',true,15}}). -{ok,["0",[0],[[],[]]]} -5> lists:flatten(["0",[0],[[],[]]]). -[48,0] -6> --
Line 5 shows that even values of constructed types is checked and if - it equals the default value it will not be encoded.
+7> 'File':encode('Seq3', {'Seq3',asn1_DEFAULT). +{ok,<<48,0>>} +8> 'File':encode('Seq3', {'Seq3',<<16#101:3>>). +{ok,<<48,0>>}Encoding of a SET with components with DEFAULT values behaves
- similar as a SEQUENCE,
Moreover, in DER the elements of a SET will be sorted. If a component is an un-tagged choice the sorting have to take place in run-time. This fact emphasizes the following recommendation -- cgit v1.2.3