TOC PREV NEXT INDEX


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

 
        if (rtInitContext (&ctxt) != 0) {
 
           /* 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 rtxMemFree should be called to free the allocated memory.

A program fragment that could be used to decode an employee record is as follows:

  #include employee.h         /* include file generated by ASN1C */
 

 
    main ()
 
    {
 
        OSOCTET   msgbuf[1024];
 
        ASN1TAG   msgtag;
 
        int       msglen;
 
        OSCTXT    ctxt;
 
        PersonnelRecord employee;
 

 
        .. logic to read message into msgbuf ..
 

 
        /* Step 1: Initialize a context variable for decoding */
 

 
        if (rtInitContext (&ctxt) != 0) {
 
           /* 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 == 0)
 
            {
 
                process received data in `employee' variable..
 

                /* Remember to release dynamic memory when done! */
 

 
                rtxMemFree (&ctxt);
 
            }
 
            else
 
                error processing...
 
        }
 
        else
 
           check for other known message types..
 
    }
 



Objective Systems, Inc.

55 Dowlin Forge Road
Exton, 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
TOC PREV NEXT INDEX