The ASN.1 to C Compiler can automatically translate ASN.1 specifications into C structures that can be easily integrated into your applications. ASN Lab ASN.1 to C Compiler is contributed to the ASN.1 Development Tools as an extension and is integrated with the Eclipse IDE. So it can automatically compile ASN.1 files incrementally to C codes while you edit and save your ASN.1 specifiction.
The compiler has two components: the ASN.1 C Code Generator and the ASN.1 C Runtime Library.
The ASN.1 C Code Generator takes a set of ASN.1 modules as input and automatically generates type-safe C structs corresponding to ASN.1 types. The mapping between ASN.1 types and C structs is quite straightforward and intuitive. The ASN.1 C Code Generator always generates type as primitive as possible, the following table lists the mapping from ASN.1 types to C primitive types or structs:
ASN.1 Type | Mapping to C Type |
BOOLEAN | boolean1 |
NULL | char |
INTEGER | int/long |
ENUMERATED | enum |
REAL | float/double |
BIT STRING | Bits2 |
OCTET STRING | Octets3 |
OBJECT IDENTIFIER | Oid4 |
RELATIVE-OID | Oid |
Character Strings | char*/wchar*/uchar*5 |
GeneralizedTime | Time6 |
UTCTime | Time |
CHOICE | union |
SEQUENCE/SET | struct |
SEQUENCE OF/SET OF | List7 |
The ASN.1 C runtime library is delivered as a set of low-level primitive C routines for encoding and decoding the base types. so that it can run on a wide-variety of platforms and processors from mainframes to embedded systems.
Feature | ASN.1 to C Compiler |
BER (Basic Encoding Rules) | YES |
CER (a canonical BER subset) | YES |
DER (another canonical BER subset) | YES |
PER (Packed Encoding Rules) | YES, align and unaligned PER |
XER (XML Encoding Rules) | NO |
Subtype constraints | YES |
Information Object Classes | YES |
Target language | C (C++ compatible, No OOP) |
Compiled code portability | Windows/x86, MacOS X/x86, Solaris/x86, Linux/x86, ARM/ADS |
The long integer are used to hold the encoded tag for optimization reason, which limits the representable tag code to less than 221 but for reasonable ASN.1 specifications this should not be a problem.
The length of enumerated items, bit string, octet string, components or alternatives can't exceed maximum of unsigned int.
The value of INTEGER should be in the range of long type in C.
The value of REAL is allways encoded/decoded in binary form, the precision should never exceed double type in C.
SET OF value dynamic sort is not supported, it's recommended to use SEQUENCE OF type instead of SET OF type.
1) define as unsigned char, with constants false=0, true=1
2) define as typedef struct { unsigned int length; char* bytes; } Bits; N.B. the unit of length is bit
3) define as typedef struct { unsigned int length; char* bytes; } Octets; N.B. the unit of length is byte
4) define as typedef struct { unsigned int length; unsigned int* ids; } Oid;
5) for UniversalString, UTF8String, uchar*(uchar is equivalent to unsigned int); for BMPString, wchar*(uchar is equivalent to unsigned short) is used; for other character strings, char* is used.
6) define as
typedef struct {
signed int year;
signed char month;
signed char day;
signed char hour;
signed char minute;
signed char second;
signed char hour_offset;
signed char minute_offset;
signed int millisec;
} Time;
7) micro define as
#define List(E) \
struct { \
E* elements; \
unsigned int size; \
unsigned int capacity; \
}