From 9058cd48435dca77d59b0010d14fb879e6ae2a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Tue, 25 Mar 2014 13:22:49 +0100 Subject: Correct and modernize the examples for DEFAULT It turns out that the current BER back-end can recognize complex DEFAULT values, so I had to commit up with a more elaborate example to show a difference between the BER and DER back-ends. --- lib/asn1/doc/src/asn1_ug.xml | 119 ++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 65 deletions(-) (limited to 'lib/asn1/doc/src/asn1_ug.xml') diff --git a/lib/asn1/doc/src/asn1_ug.xml b/lib/asn1/doc/src/asn1_ug.xml index 2475eaa153..a16aac0e03 100644 --- a/lib/asn1/doc/src/asn1_ug.xml +++ b/lib/asn1/doc/src/asn1_ug.xml @@ -879,19 +879,31 @@ Pdu ::= SEQUENCE {
 MyPdu = #'Pdu'{a=22,b=77.99,c={0,1,2,3,4},d='NULL'}.      

The decode functions will return a record as result when decoding - a SEQUENCE or a SET. - -

-

A SEQUENCE and a SET may contain a component with a - DEFAULT key word followed by the actual value that is the - default value. In case of BER encoding it is optional to encode the - value if it equals the default value. If the application uses the - atom asn1_DEFAULT as value or if the value is a primitive value - that equals the default value the encoding omits the bytes for - this value, which is more efficient and it results in fever - bytes to send to the receiving application.

-

For instance, if the following types exists in a file "File.asn":

+ a SEQUENCE or a SET.

+ +

A SEQUENCE and a SET may contain a component + with a DEFAULT key word followed by the actual value that + is the default value. The DEFAULT keyword means that the + application doing the encoding can omit encoding of the value, + thus resulting in fewer bytes to send to the receiving + application.

+ +

An application can use the atom asn1_DEFAULT to indicate + that the encoding should be omitted for that position in + the SEQUENCE.

+ +

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 b in Seq1 is not recognized as the - default value. Checking of default values in BER is not done - in case of complex values, because it would be to expensive. - -

-

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>>}
@@ -1005,9 +994,9 @@ Bad ::= SET {i INTEGER, [].

Encoding of a SET with components with DEFAULT values behaves - similar as a SEQUENCE, see above. The DER encoding format restrictions on DEFAULT + similar as a SEQUENCE. The DER encoding format restrictions on DEFAULT values is the same for SET as for SEQUENCE, and is supported by - the compiler, see above.

+ the compiler.

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