TOC PREV NEXT INDEX


Procedure for Calling C Encode Functions



This section describes the step-by-step procedure for calling a C PER encode 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 a PER encode function can be called, the user must first initialize an encoding 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 user then must call pu_setBuffer to specify a message buffer to receive the encoded message. Specification of a dynamic message buffer is possible by setting the buffer address argument to null and the buffer size argument to zero. The pu_setBuffer function also allows for the specification of aligned or unaligned encoding.

An encode function can then be called to encode the message. If the return status indicates success (ASN_OK), then the message will have been encoded in the given buffer. PER encoding starts from the beginning of the buffer and proceeds from low memory to high memory until the message is complete. This differs from BER where encoding was done from back-to-front. Therefore, the buffer start address is where the encoded PER message begins. The length of the encoded message can be obtained by calling the pe_GetMsgLen run-time function. If dynamic encoding was specified (i.e., a buffer start address and length were not given), the run-time routine pe_GetMsgPtr can be used to obtain the start address of the message. This routine will also return the length of the encoded message.

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

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

 
   main ()
 
   {
 
	ASN1OCTET msgbuf[1024];
 
	int       msglen, stat;
 
	ASN1CTXT  ctxt;
 
	ASN1BOOL  aligned = TRUE;
 
	Employee  employee; 	/* typedef generated by ASN1C */
 

 
	/* Populate employee C structure */
 

 
	employee.name.givenName = "SMITH";
 
	...
 

 
	/* Allocate and initialize a new context pointer */
 

 
      stat = rtInitContext (&ctxt);
 

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

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

 
	if ((stat = asn1PE_Employee (&ctxt, &employee)) == ASN_OK) {
 
	   msglen = pe_GetMsgLen (&ctxt);
 
	   ...
 
	}
 
	else
 
	   error processing...
 
   }
 


In general, static buffers should be used for encoding messages where possible as they offer a substantial performance benefit over dynamic buffer allocation. The problem with static buffers, however, is that you are required to estimate in advance the approximate size of the messages you will be encoding. There is no built-in formula to do this, the size of an ASN.1 message can vary widely based on data types and other factors.

If performance is not a significant issue, then dynamic buffer allocation is a good alternative. Setting the buffer pointer argument to NULL in the call to pu_setBuffer specifies dynamic allocation. This tells the encoding functions to allocate a buffer dynamically. The address of the start of the message is obtained after encoding by calling the run-time function pe_GetMsgPtr.

The following code fragment illustrates PER encoding using a dynamic buffer:

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

 
   main ()
 
   {
 
	ASN1OCTET *msgptr;
 
	int       msglen, stat;
 
	ASN1CTXT  ctxt;
 
	ASN1BOOL  aligned = TRUE;
 
	Employee  employee;	/* typedef generated by ASN1C */
 

 
	employee.name.givenName = "SMITH";
 
	...
 

 
      stat = rtInitContext (&ctxt);
 

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

 
	pu_setBuffer (&ctxt, 0, 0, aligned);
 

 
	if ((stat = asn1PE_Employee (&ctxt, &employee)) == ASN_OK) {
 
	   msgptr = pe_GetMsgPtr (&ctxt, &msglen);
 
	   ...
 

 
	}
 
	else
 
	   error processing...
 
   }
 


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