Description

These functions provide a subfunction interface to a simplified implementation of the
Basic Encoding Rules of ASN.1. The version of BER these functions
support is the one defined for the LDAP protocol. The encoding
rules are the same as BER, except that only definite form
lengths are used, and bitstrings and octet strings are always encoded in primitive
form. In addition, these lightweight BER functions restrict tags and class
to fit in a single octet (this means the actual tag must
be less than 31). When a “tag”is specified in the descriptions below,
it refers to the tag, class, and primitive or constructed bit in the
first octet of the encoding. This man page describes the encoding
functions in the lber library. See ber_decode(3LDAP) for details on the
corresponding decoding functions.

Normally, the only functions that need be called by an application are
ber_alloc(), to allocate a BER element, and ber_printf() to do the
actual encoding. The other functions are provided for those applications that
need more control than ber_printf() provides. In general, these functions return
the length of the element encoded, or -1 if an error occurred.

The ber_alloc() function is used to allocate a new BER element.

The ber_printf() function is used to encode a BER element in
much the same way that sprintf(3S) works. One important difference,
though, is that some state information is kept with the ber parameter
so that multiple calls can be made to ber_printf() to append things
to the end of the BER element. Ber_printf() writes to
ber, a pointer to a BerElement such as returned by ber_alloc().
It interprets and formats its arguments according to the format string fmt. The
format string can contain the following characters:

b

Boolean. An integer parameter should be supplied. A boolean element is output.

B

Bitstring. A char * pointer to the start of the bitstring is supplied, followed by the number of bits in the bitstring. A bitstring element is output.

i

Integer. An integer parameter should be supplied. An integer element is output.

n

Null. No parameter is required. A null element is output.

o

Octet string. A char * is supplied, followed by the length of the string pointed to. An octet string element is output.

Octet string. A null-terminated string is supplied. An octet string element is output, not including the trailing null octet.

t

Tag. An int specifying the tag to give the next element is provided. This works across calls.

v

Several octet strings. A null-terminated array of char * is supplied. Note that a construct like '{v}' is required to get an actual sequence of octet strings.

{

Begin sequence. No parameter is required.

}

End sequence. No parameter is required.

[

Begin set. No parameter is required.

]

End set. No parameter is required.

The ber_put_int() function writes the integer element num to the BER
element ber.

The ber_put_boolean() function writes the boolean value given by bool to
the BER element.

The ber_put_bitstring() function writes blen bits starting at str as a
bitstring value to the given BER element. Note that blen is
the length in bits of the bitstring.

The ber_put_ostring() function writes len bytes starting at str to the
BER element as an octet string.

The ber_put_string() function writes the null-terminated string (minus the terminating '')
to the BER element as an octet string.

The ber_put_null() function writes a NULL element to the BER
element.

The ber_start_seq() function is used to start a sequence in the
BER element. The ber_start_set() function works similarly. The end of
the sequence or set is marked by the nearest matching call to
ber_put_seq() or ber_put_set(), respectively.

The ber_first_element() function is used to return the tag and length
of the first element in a set or sequence. It also
returns in cookie a magic cookie parameter that should be passed to
subsequent calls to ber_next_element(), which returns similar information.

Examples

Example 1 Assuming the following variable declarations, and that the variables have been assigned appropriately, an BER encoding of the following ASN.1 object: