ASN.1: Fix handling of CHOICE in ASN.1 compiler
authorDavid Howells <dhowells@redhat.com>
Wed, 5 Aug 2015 11:54:45 +0000 (12:54 +0100)
committerDavid Howells <dhowells@redhat.com>
Wed, 5 Aug 2015 11:54:45 +0000 (12:54 +0100)
commit8d9b21dcfe6814c92c9f445a7c742b7bab4f86b8
treef8d75cab8ef32c8268b44025ce8cf512d9b420b8
parent52721d9d3334c1cb1f76219a161084094ec634dc
ASN.1: Fix handling of CHOICE in ASN.1 compiler

Fix the handling of CHOICE types in the ASN.1 compiler to make SEQUENCE and
SET elements in a CHOICE be correctly rendered as skippable and conditional
as appropriate.

For example, in the following ASN.1:

Foo ::= SEQUENCE { w1 INTEGER, w2 Bar, w3 OBJECT IDENTIFIER }
Bar ::= CHOICE {
x1 Seq1,
x2 [0] IMPLICIT OCTET STRING,
x3 Seq2,
x4 SET OF INTEGER
}
Seq1 ::= SEQUENCE { y1 INTEGER, y2 INTEGER, y3 INTEGER }
Seq2 ::= SEQUENCE { z1 BOOLEAN, z2 BOOLEAN, z3 BOOLEAN }

the output in foo.c generated by:

./scripts/asn1_compiler foo.asn1 foo.c foo.h

included:

// Bar
// Seq1
[   4] =  ASN1_OP_MATCH,
[   5] =  _tag(UNIV, CONS, SEQ),
...
[  13] =  ASN1_OP_COND_MATCH_OR_SKIP, // x2
[  14] =  _tagn(CONT, PRIM,  0),
// Seq2
[  15] =  ASN1_OP_MATCH,
[  16] =  _tag(UNIV, CONS, SEQ),
...
[  24] =  ASN1_OP_COND_MATCH_JUMP_OR_SKIP, // x4
[  25] =  _tag(UNIV, CONS, SET),
...
[  27] =  ASN1_OP_COND_FAIL,

as a result of the CHOICE - but this is wrong on lines 4 and 15 because
both of these should be skippable (one and only one of the four can be
picked) and the one on line 15 should also be conditional so that it is
ignored if anything before it matches.

After the patch, it looks like:

// Bar
// Seq1
[   4] =  ASN1_OP_MATCH_JUMP_OR_SKIP, // x1
[   5] =  _tag(UNIV, CONS, SEQ),
...
[   7] =  ASN1_OP_COND_MATCH_OR_SKIP, // x2
[   8] =  _tagn(CONT, PRIM,  0),
// Seq2
[   9] =  ASN1_OP_COND_MATCH_JUMP_OR_SKIP, // x3
[  10] =  _tag(UNIV, CONS, SEQ),
...
[  12] =  ASN1_OP_COND_MATCH_JUMP_OR_SKIP, // x4
[  13] =  _tag(UNIV, CONS, SET),
...
[  15] =  ASN1_OP_COND_FAIL,

where all four options are skippable and the second, third and fourth are
all conditional, as is the backstop at the end.

This hasn't been a problem so far because in the ASN.1 specs we have are
either using primitives or are using SET OF and SEQUENCE OF which are
handled correctly.

Whilst we're at it, also make sure that element labels get included in
comments in the output for elements that have complex types.

This cannot be tested with the code as it stands, but rather affects future
code.

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-By: David Woodhouse <David.Woodhouse@intel.com>
scripts/asn1_compiler.c