BER messages can be encoded directly to an output stream such as a file, network or memory stream. The ASN1C compiler has the -stream option to generate encode functions of this type. For each ASN.1 production defined in the ASN.1 source file, a C stream encode function is generated. This function will encode a populated C variable of the given type into an encoded ASN.1 message and write it to a stream.
If the return status indicates success (0), the message will have been encoded to the given stream. Streaming BER encoding starts from the beginning of the message until the message is complete. This is sometimes referred to as “forward encoding”. This differs from regular BER where encoding is done from back-to-front. Indefinite lengths are used for all constructed elements in the message. Also, there is no permanent buffer for streaming encoding, all octets are written to the stream. The buffer in the context structure is used only as a cache.
If C++ code generation is specified, a control class is generated that contains an EncodeTo method that wraps the stream encode C function. This function is invoked through the class interface to convert a populated msgData attribute variable into an encoded ASN.1 message.