This section describes the step-by-step procedure for calling a C MDER decode function. This method must be used if C code generation was done. This method can also be used as an alternative to using the control class interface if C++ code generation was done.
Before any decode function can be called; the user must first initialize a context variable. This is a variable of type OSCTXT. This variable holds all of the working data used during the decoding of a message. The context variable is declared as a normal automatic variable within the top-level calling function. It must be initialized before use. This can be accomplished as follows:
OSCTXT ctxt; int stat; stat = rtInitContext (&ctxt); if (stat != 0) { rtxErrPrint (&ctxt); rtFreeContext (&ctxt); return stat; }
The next step is to create a stream reader that will read from the given source. In our example, we read from a file, but it is also possible to read data from a socket or other source as well.
A decode function can then be called to decode the message. If
the return status indicates success, the C variable that was passed
as an argument will contain the decoded message contents. Note that
the decoder may have allocated dynamic memory and stored pointers to
objects in the C structure. After processing on the C structure is
complete, the run-time library function rtxMemFree
should be called to free the allocated memory.
A program fragment that could be used to decode a simple PDU type follows:
#include IEEE-11073-20601-ASN1.h /* include file generated by ASN1C */ #include "rtxsrc/rtxStreamFile.h" int main (void) { ApduType data; OSCTXT ctxt; const char* filename = "message.dat"; int stat; /* Step 1: Initialize a context variable for decoding */ stat = rtInitContext (&ctxt); if (stat != 0) { /* initialization failed, could be a license problem */ rtxErrPrint (&ctxt); rtFreeContext (&ctxt); return stat; } /* Step 2: Initialize a stream reader */ stat = rtxStreamFileCreateReader (&ctxt, filename); if (0 != stat) { rtxErrPrint (&ctxt); rtFreeContext (&ctxt); return stat; } /* Step 3: decode */ asn1Init_ApduType (&data); stat = MDERDec_ApduType (&ctxt, &data); if (stat != 0) { printf ("decode of data failed\n"); rtxErrPrint (&ctxt); rtFreeContext (&ctxt); return -1; } }