Saturday, February 9, 2008

Now Generating C and ASN.1 for a Complete Random Module

We can now generate a random ASN.1 module, print out the ASN.1, generate C for it which after a bit of manipulation compiles having first run asn1c on the ASN.1. The next step is to generate the .asn1 and .c files automatically so there's no need for manual manipulation.
*Asn1cTest> quickC
FooBar {1 2 3 4 5 6} DEFINITIONS ::=
BEGIN
Type1 ::= SEQUENCE {}
value1 Type1 ::= {}
Type2 ::= INTEGER
value2 Type2 ::= -2
Type3 ::= SEQUENCE {element0 CHOICE {element1 INTEGER},
element2 SEQUENCE {}}
value3 Type3 ::= {element0 element1:1,
element2 {}}
Type4 ::= CHOICE {element3 INTEGER}
value4 Type4 ::= element3:4
Type5 ::= SEQUENCE {}
value5 Type5 ::= {}
Type6 ::= INTEGER
value6 Type6 ::= -2
Type7 ::= INTEGER
value7 Type7 ::= 3
Type8 ::= SEQUENCE {element4 BIT STRING (SIZE (4..7)) (SIZE (4..6)) (SIZE (5..5)) (SIZE (5..5))}
value8 Type8 ::= {element4 '01110'B}
Type9 ::= INTEGER
value9 Type9 ::= -3
Type10 ::= SEQUENCE {}
value10 Type10 ::= {}
END
Type1 ::= {}
Type2 ::= -2
Type3 ::= {element0 element1:1,
element2 {}}
Type4 ::= element3:4
Type5 ::= {}
Type6 ::= -2
Type7 ::= 3
Type8 ::= {element4 '01110'B}
Type9 ::= -3
Type10 ::= {}
#include /* Type1 ASN.1 type */
#include /* Type2 ASN.1 type */
#include /* Type3 ASN.1 type */
#include /* Type4 ASN.1 type */
#include /* Type5 ASN.1 type */
#include /* Type6 ASN.1 type */
#include /* Type7 ASN.1 type */
#include /* Type8 ASN.1 type */
#include /* Type9 ASN.1 type */
#include /* Type10 ASN.1 type */

/* Declare a pointer to a Type1 type */
Type1_t *type1;

/* Allocate an instance of Type1 */
type1 = calloc(1, sizeof(Type1_t)); /* not malloc! */
assert(type1); /* Assume infinite memory */

/* Declare a pointer to a Type2 type */
Type2_t *type2;

/* Allocate an instance of Type2 */
type2 = calloc(1, sizeof(Type2_t)); /* not malloc! */
assert(type2); /* Assume infinite memory */

/* Declare a pointer to a Type3 type */
Type3_t *type3;

/* Allocate an instance of Type3 */
type3 = calloc(1, sizeof(Type3_t)); /* not malloc! */
assert(type3); /* Assume infinite memory */

/* Declare a pointer to a Type4 type */
Type4_t *type4;

/* Allocate an instance of Type4 */
type4 = calloc(1, sizeof(Type4_t)); /* not malloc! */
assert(type4); /* Assume infinite memory */

/* Declare a pointer to a Type5 type */
Type5_t *type5;

/* Allocate an instance of Type5 */
type5 = calloc(1, sizeof(Type5_t)); /* not malloc! */
assert(type5); /* Assume infinite memory */

/* Declare a pointer to a Type6 type */
Type6_t *type6;

/* Allocate an instance of Type6 */
type6 = calloc(1, sizeof(Type6_t)); /* not malloc! */
assert(type6); /* Assume infinite memory */

/* Declare a pointer to a Type7 type */
Type7_t *type7;

/* Allocate an instance of Type7 */
type7 = calloc(1, sizeof(Type7_t)); /* not malloc! */
assert(type7); /* Assume infinite memory */

/* Declare a pointer to a Type8 type */
Type8_t *type8;

/* Allocate an instance of Type8 */
type8 = calloc(1, sizeof(Type8_t)); /* not malloc! */
assert(type8); /* Assume infinite memory */

/* Declare a pointer to a Type9 type */
Type9_t *type9;

/* Allocate an instance of Type9 */
type9 = calloc(1, sizeof(Type9_t)); /* not malloc! */
assert(type9); /* Assume infinite memory */

/* Declare a pointer to a Type10 type */
Type10_t *type10;

/* Allocate an instance of Type10 */
type10 = calloc(1, sizeof(Type10_t)); /* not malloc! */
assert(type10); /* Assume infinite memory */

(*type2) = -2;

(*type3).element0.present = element0_PR_element1;
(*type3).element0.choice.element1 = 1;

(*type4).present = Type4_PR_element3;
(*type4).choice.element3 = 4;


(*type6) = -2;

(*type7) = 3;


(*type8).element4.buf = calloc (1, 1); /* 1 bytes */
assert((*type8).element4.buf);
(*type8).element4.size = 1;
(*type8).element4.buf[0] = 112;
(*type8).element4.bits_unused = 5; /* Trim unused bits */

(*type9) = -3;

No comments: