Chapter 17. Compact Code Generation

The -compact command line switch can be used to reduce the amount of source code generated for a given ASN.1 specification. This is done by generating the code for simple definitions inline within structured type definitions instead of creating separate classes.

For example, consider the following definition:

   X ::= [APPLICATION 1] INTEGER

   Y ::= [APPLICATION 2] OCTET STRING (SIZE (1..32))

   Z ::= [APPLICATION 3] SEQUENCE {
         x     [0] X,
         y     [1] Y
   }

In normal mode, the compiler would generate three classes for these productions: one corresponding to X, Y, and Z respectively. But in compact mode, it is recognized that a user would normally not be interested in encoding or decoding X and Y on their own. They would primarily be interested in encoding or decoding the more complex structured types (i.e. the PDU’s) that make up fully formed messages. Taking this into account, when –compact is specified, the compiler will not generate separate classes for X and Y in the above definition. Instead, it will include only the base types for X and Y in the generated code for the SEQUENCE Z. All logic to handle the tags and constraints will be built directly into the Z encode and decode methods.

So the result will be only a single class generated (Z) that will contain an Asn1Integer object to represent X and an Asn1OctetString object to represent Y. The logic to process the application tags and the size constraint on the octet string will be generated inline in the encode and decode methods in Z.

Due to the way some ASN.1 specifications are written, this can have a significant effect in reducing the amount of generated code. For example, in the TAP3 sample program, the total number of generated classes was reduced from 20 to 3.