TOC PREV NEXT INDEX


Procedure for Calling C Decode Functions



This section describes the step-by-step procedure for calling a C PER 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.

Unlike BER, the user must know the ASN.1 type of a PER message before it can be decoded. This is because the type cannot be determined at run-time. There are no embedded tag values to reference to determine the type of message received.

There are three steps to calling a compiler-generated decode function:


Before a PER decode function can be called, the user must first initialize a context block structure. The context block is initialized by either calling the rtNewContext function (to allocate a dynamic context block), or by calling rtInitContext to initialize a static block. The pu_setBuffer function must them be called to specify a message buffer that contains the PER-encoded message to be decoded. This function also allows for the specification of aligned or unaligned decoding.

A decode function can then be called to decode the message. If the return status indicates success (ASN_OK), then the message will have been decoded into the given ASN.1 type variable. The decode function may automatically allocate dynamic memory to hold variable length variables during the course of decoding. This memory will be tracked in the context structure, so the programmer does not need to worry about freeing it. It will be released when the context is freed.

The final step of the procedure is to free the context block. This must be done regardless of whether the block is static (declared on the stack and initialized using rtInitContext), or dynamic (created using rtNewContext). The function to free the context is rtFreeContext.

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

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

 
    main ()
 
    {
 
        ASN1OCTET msgbuf[1024];
 
        ASN1TAG   msgtag;
 
        int       msglen, stat;
 
        ASN1CTXT  ctxt;
 
        ASN1BOOL  aligned = TRUE;
 
 	  PersonnelRecord employee;
 

 
        .. logic to read message into msgbuf ..
 

 
        /* This example uses a static context block */
 

 
        /* step 1: prepare the context block */
 

 
        stat = rtInitContext (&ctxt);
 

 
	if (stat != ASN_OK) {
 
	   printf ("rtInitContext failed (check license)\n");
 
	   rtErrPrint (&ctxt);
 
	   return stat;
 
	}
 

 
	pu_setBuffer (&ctxt, msgbuf, msglen, aligned);
 

 
        /* step 2: decode the record */
 

 
        stat = asn1PD_PersonnelRecord (&ctxt, &employee);
 

 
        if (stat == ASN_OK)
 
        {
 
            process received data..
 
        }
 
        else {
 
            /* error processing... */
 
            rtErrPrint (&ctxt);
 
        }
 

 
        /* step 3: free the context */
 

 
        rtFreeContext (&ctxt);
 
    }
 



Objective Systems, Inc.

102 Pickering Way, Suite #506
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