Procedure for Calling C# BER Decode Methods

The general procedure to decode an ASN.1 BER message involves the following three steps:

  1. Create a decode message buffer object to describe the message to be decoded

  2. Invoke the decode method

  3. Process the decoded data values

The first step is the creation of a decode message buffer object. The Asn1BerDecodeBuffer object contains constructors that can either accept a message as a byte array or as an I/O input stream. The input stream option makes it possible to decode messages directly from other mediums other than a memory buffer (for example, a message can be decoded directly from a file).

The Asn1BerDecodeBuffer object contains a method called PeekTag that can be used to determine the outer-level tag on a message. This can be used to determine the type of message received in applications that must deal with multiple message types.

The final step is to process the data. All data is contained within public member variables so access is quite easy. And of course C# has the distinct advantage of not requiring any clean-up once you are done with the data. The garbage collector will collect the unused memory when it is no longer referenced.

A complete example showing how to invoke a decode method is as follows:

   try {

      // Step 1: create a decode message buffer object to describe the
      // message to be decoded. This example will use a file input
      // stream to decode a message directly from a binary file..

      // Create an input file stream object

      System.IO.FileStream ins = new System.IO.FileStream(
         filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);

      // Create a decode buffer object

      Asn1BerDecodeBuffer decodeBuffer = new Asn1BerDecodeBuffer (ins);

      // Step 2: create an object of the generated type and invoke the
      // decode method..

      PersonnelRecord personnelRecord = new PersonnelRecord ();
      personnelRecord.Decode (decodeBuffer);

      // Step 3: process the data

      if (trace) {
         System.Console.Out.WriteLine ("Decode was successful");
         personnelRecord.Print ("personnelRecord");
      }
   }
   catch (Exception e) {
      System.Console.Out.WriteLine (e.Message);
      Asn1Util.WriteStackTrace(e, Console.Error);
      return;
   }