
Procedure for Using the C++ Control Class Encode Method
- Instantiate an ASN.1 PER encode buffer object (ASN1PEREncodeBuffer) to describe the buffer into which the message will be encoded. Two overloaded constructors are available. The first form takes as arguments a static encode buffer and size and a Boolean value indicating whether aligned encoding is to be done. The second form only takes the Boolean aligned argument. This form is used to specify dynamic encoding.
- Check the return status. The return value is a status value indicating whether encoding was successful or not. Zero (ASN_OK) indicates success. If encoding failed, the status value will be a negative number. The encode buffer method printErrorInfo can be invoked to get a textual explanation and stack trace of where the error occurred.
- If encoding was successful, get the start-of-message pointer and message length. The start-of-message pointer is obtained by calling the getMsgPtr method of the encode buffer object. If static encoding was specified (i.e., a message buffer address and size were specified to the PER Encode Buffer class constructor), the start-of-message pointer is the buffer start address. The message length is obtained by calling the getMsgLen method of the encode buffer object.
#include employee.h // include file generated by ASN1C main () { const ASN1OCTET* msgptr; ASN1OCTET msgbuf[1024]; int msglen, stat; ASN1BOOL aligned = TRUE; // step 1: instantiate an instance of the PER encode // buffer class. This example specifies a static // message buffer.. ASN1PEREncodeBuffer encodeBuffer (msgbuf, sizeof(msgbuf), aligned); // step 2: populate msgData with data to be encoded ASN1T_PersonnelRecord msgData; msgData.name.givenName = "SMITH"; ... // step 3: instantiate an instance of the ASN1C_<ProdName> // class to associate the encode buffer and message data.. ASN1C_PersonnelRecord employee (encodeBuffer, msgData); // steps 4 and 5: encode and check return status if ((stat = employee.Encode ()) == ASN_OK) { printf ("Encoding was successful\n"); printf ("Hex dump of encoded record:\n"); encodeBuffer.hexDump (); printf ("Binary dump:\n"); encodeBuffer.binDump ("employee"); // step 6: get start-of-message pointer and message length. // start-of-message pointer is start of msgbuf // call getMsgLen to get message length.. msgptr = encodeBuffer.getMsgPtr (); // will return &msgbuf len = encodeBuffer.getMsgLen (); } else { printf ("Encoding failed\n"); encodeBuffer.printErrorInfo (); exit (0); } // msgptr and len now describe fully encoded message ...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. Using the form of the ASN1PEREncodeBuffer constructor that does not include buffer address and size arguments specifies dynamic buffer allocation. This constructor only requires a Boolean value to specify whether aligned or unaligned encoding should be performed (aligned is true).
#include employee.h // include file generated by ASN1C main () { ASN1OCTET *msgptr; int msglen, stat; ASN1BOOL aligned = TRUE; // Create an instance of the compiler generated class. // This example does dynamic encoding (no message buffer // is specified).. ASN1PEREncodeBuffer encodeBuffer (aligned); ASN1T_PersonnelRecord msgData; ASN1C_PersonnelRecord employee (encodeBuffer, msgData); // Populate msgData within the class variable msgData.name.givenName = "SMITH"; ... // Encode if ((stat = employee.Encode ()) == ASN_OK) { printf ("Encoding was successful\n"); printf ("Hex dump of encoded record:\n"); encodeBuffer.hexDump (); printf ("Binary dump:\n"); encodeBuffer.binDump ("employee"); // Get start-of-message pointer and length msgptr = encodeBuffer.getMsgPtr (); len = encodeBuffer.getMsgLen (); } else { printf ("Encoding failed\n"); encodeBuffer.printErrorInfo (); exit (0); } return 0; }
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 |