Procedure for Calling Python BER Encode Methods

The procedure to call a Python encode method for the different cases described above is basically the same. It involves the following three steps:

  1. Create an encode message buffer object into which the value will be encoded.

  2. Invoke the encode method.

  3. Invoke encode message buffer methods to access the encoded message component.

The first step is the creation of an encode message buffer object. This is done by simply creating an instance of the Asn1BerEncodeBuffer class:

    encbuf = Asn1BerEncodeBuffer()

The encode method is then invoked. In the simple case of a primitive type with no generated class, this involves invoking one of the methods defined in the encode buffer class to encode a primitive value. For example, to encode an integer, one would do:

enclen = encbuf.encode_integer(10)

This would encode the integer value 10 and add the universal tag and length bytes (the explict argument is set to True by default).

The procedure for invoking a static method is similar. The general form is <classname>.encode(value, encbuf, explicit). So, for example, a class named EmployeeNumber would be generated for the following definition:

EmployeeNumber ::= [APPLICATION 2] IMPLICIT INTEGER

To encode an employee number of 51, one would do the following:

EmployeeNumber.encode(51, encbuf)

This would encode value 51 and add the APPLICATION 2 tag.

Finally, to invoke the instance method in the class generated for a constructed type, one would first populate the attribute values and then invoke the encode method. To encode an instance of the Name class in the employee sample, one would first create an instance of the class, populate the attributes, and then invoke the encode method:

jSmithName = Name()
jSmithName.givenName = 'John'
jSmithName.initial = 'P'
jSmithName.familyName = 'Smith'

jSmithName.encode(encbuf)

This will encode the full name and add the assigned tag.

The final step once the data is encoded is to retrieve a reference to it from the encode buffer object. This is done using the buffer method. The encoded data is returned in the form of an in-memory byte array.

A complete example showing how to invoke an encode method is as follows:

   # Note: personnelRecord object was previously populated with data

   # Step 1: Create an encode buffer object

   encbuf = Asn1BerEncodeBuffer()

   # Step 2: Invoke the encode method. Note that it must be done
   # from within a try/catch block..

   try:
       personnelRecord.encode(encbuf, True);

       if (trace):
           # dump encoded message
           print(encbuf.bin_dump())

       # Step 3: Access the encoded message component. In this
       # case, we use methods in the class to write the component
       # to a file and output a formatted dump to the message.dmp
       # file..

       # Write the encoded record to a file
       f = open('message.dat', 'wb')
       f.write(encbuf.buffer())
       f.close()

       # Generate a hex dump file for comparisons
       f = open('message.hex', 'w')
       f.write(hexdump(encbuf.buffer()))
       f.close()

   except Exception:
       tb = traceback.format_exc()
       print(tb)