silc_asn1_encode

SYNOPSIS

    SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);

DESCRIPTION

Encodes ASN.1 encoded buffer into `dest', from variable argument list of ASN.1 types. The variable argument list forms the ASN.1 trees and nodes that are encoded into the `dest'. By default, the memory for `dest' is allocated from the `asn1', and the buffer becomes invalid either by calling silc_asn1_free, silc_asn1_uninit, or when silc_asn1_encode is called for the next time with `asn1' context.

If the SILC_ASN1_OPTS macro with SILC_ASN1_ALLOC option is given then the `dest' is dynamically allocated and caller must free it by itself. If the `stack' was given to silc_asn1_alloc, the SILC_ASN1_ALLOC will allocate from that stack and consume the stack. Alternatively if SILC_ASN1_ACCUMUL is given then memory is accumulated from `asn1' for `dest' and it is freed only when silc_asn1_free or silc_asn1_uninit is called. Next call to silc_asn1_encode will not cancel the previous result, but will accumulate more memory for new result.

The variable argument list is constructed by using various macros, for example SILC_ASN1_SEQUENCE, etc. The variable argument list must always be ended with SILC_ASN1_END type.

If encoding is successful this returns TRUE, FALSE on error.

EXAMPLE

    silc_asn1_encode(asn1, buf,
                     SILC_ASN1_SEQUENCE,
                       SILC_ASN1_BOOLEAN(bool_val),
                       SILC_ASN1_OCTET_STRING(string, string_len),
                       SILC_ASN1_SEQUENCE_T(0, 2),
                         SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 100, foo),
                       SILC_ASN1_END,
                       SILC_ASN1_OCTET_STRING_T(0, 1, string2, string2_len),
                     SILC_ASN1_END, SILC_ASN1_END);

    Creates ASN.1 tree that looks something like:

    buf ::= SEQUENCE {
      bool_val      BOOLEAN,
      string        OCTET-STRING,
               [2]  SEQUENCE {
                      foo   [100] EXPLICIT BOOLEAN }
      string2  [1]  OCTET-STRING }