• David Howells's avatar
    ASN.1: Fix handling of CHOICE in ASN.1 compiler · 8ae2cc61
    David Howells authored
    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: default avatarDavid Howells <dhowells@redhat.com>
    Reviewed-By: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
    8ae2cc61
asn1_compiler.c 34.1 KB