Three types of Python encode functions/methods are available for encoding different types of data. These are:
Standard base run-time functions. These exist in the asn1ber.py run-time module and exist within the Asn1BerEncodeBuffer run-time class. Examples are encode_boolean, encode_integer, encode_bitstr, etc.
Static methods in Python classes for primitive types. Classes are generated for primitive types that have been altered by adding tagging information or constraints. The method name in this case is ber_encode.
Instance methods in Python classes for constructed types. Classes are generated for SEQUENCE, SET, SEQUENCE/SET OF, and CHOICE constructs. The encode method in theis case encodes the instance attributes which correspond to the ASN.1 elements defind in the ASN.1 types. The method name in this case is also ber_encode as it was in the static method case.
The signature for a Standard base run-time method in the Asn1BerEncodeBuffer is as follows:
def encode_* (self, value, explicit=True)
where * would be a primitive type name (boolean, integer, etc.).
The self argument is a reference to the Asn1BerEncodeBuffer object from which it was invoked (note that in Python, this argument is not explicitly passed, it refers to the object invoking the method). The value argument is the value to be encoded. The explicit argumnet is a boolean argument indicating if the universal tag and length associated with the primitive type should be applied on top of the encoded content.
The return value is the length in octets of the encoded message component. Unlike the C/C++ version, a negative value is never returned to indicate an encoding failure. That is handled by the Python exception mechanism.
The signature for a static BER encode method in a class generated for a primitive type is as follows:
@staticmethod def ber_encode(value, encbuf, explicit=True):
In this case, there is no 'self' argument. A reference to the encode buffer object is passed as a formal argument in the 2nd position. The value and explicit arguments are as they were in standard base run-time method case and the return value is also the same (the encoded length).
The signature for an instance BER encode method in a class generated for a constructed type is as follows:
def ber_encode(self, encbuf, explicit=True)
The self argument is a reference to an object of the generated class (note that in Python, this argument is not explicitly passed, it refers to the object invoking the method). >The encbuf argument is a reference to BER encode buffer object to be used to encode the data. The explicit argument is that same as in the other two cases.