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 pu_newContext function (to allocate a dynamic context block), or by calling pu_initContext to initialize a static block. Both of these routines allow a message buffer to be specified to receive the encoded message. Specification of a message buffer is optional. If not specified, the encoder will allocate memory automatically for the encoded message. These routines also allow 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.
#include employee.h /* include file generated by ASN1C */ main () { ASN1OCTET msgbuf[1024]; int msglen, stat; ASN1CTXT* pCtxt; ASN1BOOL aligned = TRUE; Employee employee; /* typedef generated by ASN1C */ /* Populate employee C structure */ employee.name.givenName = "SMITH"; ... /* Allocate and initialize a new context pointer */ pCtxt = pu_newContext (msgbuf, sizeof(msgbuf), aligned); if ((stat = asn1PE_Employee (pCtxt, &employee)) == ASN_OK) { msglen = pe_GetMsgLen (pCtxt); ... } else error processing... } free (pCtxt); /* release the context pointer */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_newContext or pu_initContext 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.
#include employee.h /* include file generated by ASN1C */ main () { ASN1OCTET *msgptr; int msglen, stat; ASN1CTXT* pCtxt; ASN1BOOL aligned = TRUE; Employee employee; /* typedef generated by ASN1C */ employee.name.givenName = "SMITH"; ... pCtxt = pu_newContext (0, 0, aligned); if ((stat = asn1PE_Employee (pCtxt, &employee)) == ASN_OK) { msgptr = pe_GetMsgPtr (pCtxt, &msglen); ... } else error processing... }
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 |