
Procedure for Calling C Decode Functions
This section describes the step-by-step procedure for calling a C BER or DER 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 ASN1CTXT. 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:
ASN1CTXT ctxt; if (rtInitContext (&ctxt) != ASN_OK) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; }The next step is the specification of a buffer containing a message to be decoded. This is accomplished by calling the xd_setp run-time library function. This function takes as an argument the start address of the message to be decoded. The function returns the starting tag value and overall length of the message. This makes it possible to identify the type of message received and apply the appropriate decode function to decode it.
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 rtMemFree should be called to free the allocated memory.
#include employee.h /* include file generated by ASN1C */ main () { OSOCTET msgbuf[1024]; ASN1TAG msgtag; int msglen; ASN1CTXT ctxt; PersonnelRecord employee; .. logic to read message into msgbuf .. /* Step 1: Initialize a context variable for decoding */ if (rtInitContext (&ctxt) != ASN_OK) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; }xd_setp (&ctxt, msgbuf, 0, &msgtag, &msglen); /* Step 2: Test message tag for type of message received */ /* (note: this is optional, the decode function can be */ /* called directly if the type of message is known).. */ if (msgtag == TV_PersonnelRecord) { /* Step 3: Call decode function (note: last two args */ /* should always be ASN1EXPL and 0).. */ status = asn1D_PersonnelRecord (&ctxt, &employee, ASN1EXPL, 0); /* Step 4: Check return status */ if (status == ASN_OK) { process received data in `employee' variable../* Remember to release dynamic memory when done! */ rtMemFree (&ctxt); } else error processing... } else check for other known message types.. }
Objective Systems, Inc.102 Pickering Way, Suite #506Exton, Pennsylvania 19341 http://www.obj-sys.com Phone: (484) 875-9841 Toll-free: (877) 307-6855 (US only) Fax: (484) 875-9830 info@obj-sys.com |