INTEGER

The ASN.1 INTEGER type is converted into one of several different C types depending on constraints specified on the type. By default, an INTEGER with no constraints results in the generation of an OSINT32 type. In the global include file osSysTypes.h, OSINT32 is defined to be an int which is normally a signed 32-bit integer value on most computer systems.

ASN.1 production:

   <name> ::= INTEGER

Generated C code:

   typedef OSINT32 <name>;

Generated C++ code:

   typedef OSINT32 ASN1T_<name>;

Value range constraints can be used to alter the C type used to represent a given integer value. For example, the following declaration from the SNMP SMI specification would cause an OSUINT32 type (mapped to a C unsigned int) to be used:

   Counter ::= [APPLICATION 1] IMPLICIT INTEGER (0..4294967295)

In this case, an OSINT32 could not be used because all values within the given range could not be represented. Other value ranges would cause different integer types to be used that provide the most efficient amount of storage. The following table shows the types that would be used for the different range values:

Min Lower Bound Max Upper Bound ASN1C Type C Type
-128 127 OSINT8 char (signed 8-bit int)
0 255 OSUINT8 unsigned char (unsigned 8-bit number)
-32768 32767 OSINT16 short (signed 16-bit int)
0 65535 OSUINT16 unsigned short (unsigned 16-bit int)
-2147483648 2147483647 OSINT32 int (signed 32-bit integer)
0 4294967295 OSUINT32 unsigned int (unsigned 32-bit integer)

The C type that is used to represent a given integer value can also be altered using the "<ctype>" configuration variable setting. This allows any of the integer types above to be used for a given integer type as well as a 64-bit integer type. The values that can be used with <ctype> are: byte, int16, uint16, int32, uint32, and int64. An example of using this setting is as follows:

Suppose you have the following integer declaration in your ASN.1 source file:

   MyIntType ::= [APPLICATION 1] INTEGER

You could then have ASN1C use a 64-bit integer type for this integer by adding the following declaration to a configuration file to be associated with this module:

   <production>
      <name>MyIntType</name>
      <intCType>int64</intCType>
   </production>

The <intCType> setting is also available at the module level to specify that the given C integer type be used for all unconstrained integers within the module.

Large Integer Support

In C and C++, the maximum size for an integer type is normally 64 bits (or 32 bits on some older platforms). ASN.1 has no such limitation on integer sizes and some applications (security key values for example) demand larger sizes. In order to accommodate these types of applications, the ASN1C compiler allows an integer to be declared a "big integer" via a configuration file variable (the <isBigInteger/> setting is used to do this - see the section describing the configuration file for full details). When the compiler detects this setting, it will declare the integer to be a character string variable instead of a C int or unsigned int type. The character string would then be populated with a character string representation of the value to be encoded. Supported character string representations are hexadecimal (strings starting with 0x), octal (strings starting with 0o) and decimal (no prefix).

For example, the following INTEGER type might be declared in the ASN.1 source file:

   SecurityKeyType ::= [APPLICATION 2] INTEGER

Then, in a configuration file used with the ASN.1 definition above, the following declaration can be made:

   <production>
      <name>SecurityKeyType</name>
      <isBigInteger/>
   </production>

This will cause the compiler to generate the following type declaration:

   typedef const char* SecurityKeyType

The SecurityKeyType variable can now be populated with a hexadecimal string for encoding such as the following:

   SecurityKeyType secKey = "0xfd09874da875cc90240087cd12fd";

Note that in this definition the 0x prefix is required to identify the string as containing hexadecimal characters.

On the decode side, the decoder will populate the variable with the same type of character string after decoding.

There are also a number of run-time functions available for big integer support. This set of functions provides an arbitrary length integer math package that can be used to perform mathematical operations as well as convert values into various string forms. See the ASN1C C/C++ Common Run-time User's Manual for a description of these functions.