ASN.1 to C Compiler

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.

ASN.1 C Code Generator

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 TypeMapping to C Type
BOOLEANboolean1
NULLchar
INTEGER int/long
ENUMERATEDenum
REALfloat/double
BIT STRINGBits2
OCTET STRINGOctets3
OBJECT IDENTIFIEROid4
RELATIVE-OIDOid
Character Stringschar*/wchar*/uchar*5
GeneralizedTimeTime6
UTCTimeTime
CHOICEunion
SEQUENCE/SETstruct
SEQUENCE OF/SET OFList7

ASN.1 C Runtime Library

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.

Features

FeatureASN.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 constraintsYES
Information Object ClassesYES
Target languageC (C++ compatible, No OOP)
Compiled code portabilityWindows/x86, MacOS X/x86, Solaris/x86, Linux/x86,
ARM/ADS

Strength

Known limitations



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;  \
}