The generated codes consist of header files and C files. The header files contain type-safe C struct definitions corresponding to ASN.1 types, and declaraction of struct specific encode/decode routine, free routine, print routine. The C files contain metadata of the ASN.1 type, and implement of struct specific encode/decode routine, free routine, print routine.
Below are code snippets of the generated codes from The MyHTTP Example.
/* * Generated by ASN.1 C Compiler (https://www.asnlab.org/) * From ASN.1 module "MyHTTP" */ #ifndef ACCEPTTYPES_H #define ACCEPTTYPES_H #include "asnrt.h" #include "Standards.h" #ifdef __cplusplus extern "C" { #endif struct AcceptTypes { Bits* standards; /* OPTIONAL */ List(char*)* others; /* OPTIONAL */ }; extern PUBLIC Type ACCEPTTYPES_TYPE; extern int encode_AcceptTypes(Buffer* buffer, struct AcceptTypes* acceptTypes); extern int decode_AcceptTypes(Buffer* buffer, struct AcceptTypes* acceptTypes); extern void free_AcceptTypes(struct AcceptTypes* acceptTypes); extern void print_AcceptTypes(struct AcceptTypes* acceptTypes, Print print); #ifdef __cplusplus } #endif #endif /*ACCEPTTYPES_H*/
The C struct definition struct AcceptTypes
is mapped from ASN.1 type AcceptTypes
.
Note that members commented as OPTIONAL are optional component either marked as OPTIONAL or DEFAULT in the ASN.1 specifiction.
Also note that those commented as OPTIONAL are pointer members, a NULL pointer value indicate that this component value is not available.
/* * Generated by ASN.1 C Compiler (https://www.asnlab.org/) * From ASN.1 module "MyHTTP" */ #include "AcceptTypes.h" #ifdef __cplusplus extern "C" { #endif IMPLICIT_TYPE(PRIVATE, standards, 0x80, STANDARDS_TYPE); VISIBLESTRING_TYPE(PUBLIC, _others_undly_comp_, NULL, 0, 7, 8, false, false, 4, 4, 1, true, true, 0, 1, false); SEQUENCEOF_TYPE(PRIVATE, _others_undly_, _others_undly_comp_, sizeof(char*), 0, 0, 0, false, false, 0, 0, false); IMPLICIT_TYPE(PRIVATE, others, 0xA1, _others_undly_); PRIVATE Component _components_[] = { { "standards", &standards, true, NULL, offsetof(struct AcceptTypes, standards), sizeof(Bits) }, { "others", &others, true, NULL, offsetof(struct AcceptTypes, others), sizeof(List(char*)) } }; PRIVATE Composite _composite_ = { _components_, 2, false, NULL, 0 }; PUBLIC Type ACCEPTTYPES_TYPE = { &SET_CODEC, &_composite_ }; int encode_AcceptTypes(Buffer* buffer, struct AcceptTypes* acceptTypes) { return encode_object(buffer, acceptTypes, &ACCEPTTYPES_TYPE); } int decode_AcceptTypes(Buffer* buffer, struct AcceptTypes* acceptTypes) { return decode_object(buffer, acceptTypes, &ACCEPTTYPES_TYPE); } void free_AcceptTypes(struct AcceptTypes* acceptTypes) { free_object(acceptTypes, &ACCEPTTYPES_TYPE); } void print_AcceptTypes(struct AcceptTypes* acceptTypes, Print print) { print_object(acceptTypes, &ACCEPTTYPES_TYPE, print); } #ifdef __cplusplus } #endif
The metadata of the ASN.1 type generated here is statically allocated, so the memory can be optimized in compile time. ASN.1 encode/decode routines that are not referenced by these metadata are not linked into the final executable file.
The generations of struct specific encode routine encode_AcceptTypes
, decode routine decode_AcceptTypes
,
free routine free_AcceptTypes
and print routine print_AcceptTypes
are just for the developer's convenience.
In fact, those routines can be removed and the generic routines from the ASN.1 C Runtime Library can be used (as demonstrated by the generated routines).
To remove the struct specific encode/decode/free/print routines, goto the ASN.1 to C Compiler preference page at Windows > Preferences > ASN.1 > ASN.1 to C Compiler, in the Encoding Rules section, uncheck all encoding rules, in the Output Option section, uncheck Generate free() functions and Generate print() functions, and then resave or manually invoke the building of the ASN.1 project.