Procedure for Using the C++ Control Class Decode Method

The following are the steps are involved in decoding a OER message using the generated C++ class:

  1. Instantiate an ASN.1 OER decode buffer object (ASN1OERDecodeBuffer ) to describe the message to be decoded. The constructor takes as arguments a pointer to the message to be decoded, the length of the message.

  2. If the code is generated using the -coer switch, the decoding will be canonical without the user needing to take any additional steps. But if the code is generated using the -oer switch, in order to make the decoding canonical, the user must set the ASN1CANON flag in the context using the rtxCtxtSetFlag function.

  3. Instantiate an ASN1T_<ProdName> object to hold the decoded message data.

  4. Instantiate an ASN1C_<ProdName> object to decode the message. This class associates the message buffer object with the object that is to receive the decoded data. The results of the decode operation will be placed in the variable declared in step 2.

  5. Invoke the ASN1C_<ProdName> object Decode method.

  6. Check the return status. The return value is a status value indicating whether decoding was successful or not. Zero (0) indicates success. If decoding failed, the status value will be a negative number. The decode buffer method PrintErrorInfo can be invoked to get a textual explanation and stack trace of where the error occurred.

  7. Release dynamic memory that was allocated by the decoder. All memory associated with the decode context is released when both the ASN1OERDecodeBuffer and ASN1C_<ProdName> objects go out of scope.

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];
      int     msglen, stat;
                     
      .. logic to read message into msgbuf ..
               
      // step 1: instantiate a OER decode buffer object
               
      ASN1OERDecodeBuffer decodeBuffer (msgbuf, msglen);
      
      // Specify canonical decoding if desired.
      OSCTXT* pctxt = decodeBuffer.getCtxtPtr();
      rtxCtxtSetFlag(pctxt, ASN1CANON);
               
      // step 2: instantiate an ASN1T_<ProdName> object
               
      ASN1T_PersonnelRecord msgData;
               
      // step 3: instantiate an ASN1C_<ProdName> object
               
      ASN1C_PersonnelRecord employee (decodeBuffer, msgData);
               
      // step 4: decode the record
               
      stat = employee.Decode ();
               
      // step 5: check the return status
               
      if (stat == 0)
      {
         process received data..
      }
      else {
         // error processing..
         decodeBuffer.PrintErrorInfo ();
      }
               
      // step 6: free dynamic memory (will be done automatically
      // when both the decodeBuffer and employee objects go out
      // of scope)..
   }

A stream can be used as the data input source instead of a memory buffer by creating an OSRT input stream object in step1 and associating it with the decodeBuffer object. For example, to read from a file input stream, the decodeBuffer declaration in step 1 would be replaced with the following two statements:

   OSRTFileInputStream istrm (filename);
               ASN1OERDecodeBuffer decodeBuffer (istrm);