ber_alloc_t, ber_flush, ber_flush2, ber_printf, ber_put_int, ber_put_enum, ber_put_ostring, ber_put_string, ber_put_null, ber_put_boolean, ber_put_bitstring, ber_start_seq, ber_start_set, ber_put_seq, ber_put_set − OpenLDAP LBER simplified Basic Encoding Rules library routines for encoding


OpenLDAP LBER (liblber, −llber)


#include <lber.h>

BerElement *ber_alloc_t(int options);

int ber_flush(Sockbuf *sb, BerElement *ber, int freeit);

int ber_flush2(Sockbuf *sb, BerElement *ber, int freeit);

int ber_printf(BerElement *ber, const char *fmt, ...);

int ber_put_int(BerElement *ber, ber_int_t num, ber_tag_t tag);

int ber_put_enum(BerElement *ber, ber_int_t num, ber_tag_t tag);

int ber_put_ostring(BerElement *ber, const char *str, ber_len_t len, ber_tag_t tag);

int ber_put_string(BerElement *ber, const char *str, ber_tag_t tag);

int ber_put_null(BerElement *ber, ber_tag_t tag);

int ber_put_boolean(BerElement *ber, ber_int_t bool, ber_tag_t tag);

int ber_put_bitstring(BerElement *ber, const char *str, ber_len_t blen, ber_tag_t tag);

int ber_start_seq(BerElement *ber, ber_tag_t tag);

int ber_start_set(BerElement *ber, ber_tag_t tag);

int ber_put_seq(BerElement *ber);

int ber_put_set(BerElement *ber);


These routines provide a subroutine interface to a simplified implementation of the Basic Encoding Rules of ASN.1. The version of BER these routines 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. This man page describes the encoding routines in the lber library. See lber-decode(3) for details on the corresponding decoding routines. Consult lber-types(3) for information about types, allocators, and deallocators.

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

The ber_alloc_t() routine is used to allocate a new BER element. It should be called with an argument of LBER_USE_DER.

The ber_flush2() routine is used to actually write the element to a socket (or file) descriptor, once it has been fully encoded (using ber_printf() and friends). See lber-sockbuf(3) for more details on the Sockbuf implementation of the sb parameter. If the freeit parameter is non-zero, the supplied ber will be freed. If LBER_FLUSH_FREE_ON_SUCCESS is used, the ber is only freed when successfully flushed, otherwise it is left intact; if LBER_FLUSH_FREE_ON_ERROR is used, the ber is only freed when an error occurs, otherwise it is left intact; if LBER_FLUSH_FREE_ALWAYS is used, the ber is freed anyway. This function differs from the original ber_flush(3) function, whose behavior corresponds to that indicated for LBER_FLUSH_FREE_ON_SUCCESS. Note that in the future, the behavior of ber_flush(3) with freeit non-zero might change into that of ber_flush2(3) with freeit set to LBER_FLUSH_FREE_ALWAYS.

The ber_printf() routine is used to encode a BER element in much the same way that sprintf(3) 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_t(). It interprets and formats its arguments according to the format string fmt. The format string can contain the following characters:


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


Enumeration. An ber_int_t parameter should be supplied. An enumeration element is output.


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


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.


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


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


Octet string. A struct berval * is supplied. 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.


Tag. A ber_tag_t specifying the tag to give the next element is provided. This works across calls.


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


Several octet strings. A null-terminated array of struct berval *’s is supplied. Note that a construct like ’{V}’ is required to get an actual SEQUENCE OF octet strings.


Several octet strings. An array of struct berval’s is supplied. The array is terminated by a struct berval with a NULL bv_val. Note that a construct like ’{W}’ 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() routine writes the integer element num to the BER element ber.

The ber_put_enum() routine writes the enumeration element num to the BER element ber.

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

The ber_put_bitstring() routine 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() routine writes len bytes starting at str to the BER element as an octet string.

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

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

The ber_start_seq() routine is used to start a sequence in the BER element. The ber_start_set() routine 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.


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

AlmostASearchRequest := SEQUENCE {
baseObject DistinguishedName,
baseObject (0),
singleLevel (1),
wholeSubtree (2)
derefAliases ENUMERATED {
neverDerefaliases (0),
derefInSearching (1),
derefFindingBaseObj (2),
alwaysDerefAliases (3)
sizelimit INTEGER (0 .. 65535),
timelimit INTEGER (0 .. 65535),
attrsOnly BOOLEAN,
attributes SEQUENCE OF AttributeType

can be achieved like so:

int rc;
ber_int_t scope, ali, size, time, attrsonly;
char *dn, **attrs;
BerElement *ber;

/* ... fill in values ... */

ber = ber_alloc_t( LBER_USE_DER );

if ( ber == NULL ) {
/* error */

rc = ber_printf( ber, "{siiiib{v}}", dn, scope, ali,
size, time, attrsonly, attrs );

if( rc == −1 ) {
/* error */
} else {
/* success */


If an error occurs during encoding, generally these routines return −1.


The return values for all of these functions are declared in the <lber.h> header file.


lber-decode(3), lber-memory(3), lber-sockbuf(3), lber-types(3)


OpenLDAP Software is developed and maintained by The OpenLDAP Project <>. OpenLDAP Software is derived from University of Michigan LDAP 3.3 Release.

More Linux Commands

XvGetPortAttribute(3) - return current port attribute value
XvGetPortAttribute(3) returns the current value of the requested port attribute in the integer pointed to by p_value. The attribute is identified using an Atom

synclient(1) - commandline utility to query and modify Synap
This program lets you change your Synaptics TouchPad driver for XOrg/XFree86 server parameters while X is running. OPTIONS -l List current user settings. This i

glutSolidTetrahedron(3) - render a solid or wireframe tetrah
glutSolidTetrahedron and glutWireTetrahedron render a solid or wireframe tetrahedron respectively centered at the modeling coordinates origin with a radius of s

reset(1) - terminal initialization - Linux manual page......
Tset initializes terminals. Tset first determines the type of terminal that you are using. This determination is done as follows, using the first terminal type

pam_chauthtok(3) - updating authentication tokens (ManPage)
The pam_chauthtok function is used to change the authentication token for a given user (as indicated by the state associated with the handle pamh). The pamh arg

strtoumax(3) - convert string to integer - Linux man page...
These functions are just like strtol(3) and strtoul(3), except that they return a value of type intmax_t and uintmax_t, respectively. RETURN VALUE On success, t

sysctl(8) - configure kernel parameters at runtime (ManPage)
sysctl is used to modify kernel parameters at runtime. The parameters available are those listed under /proc/sys/. Procfs is required for sysctl support in Linu

sasl_getconfpath_t(3) - The SASL callback to indicate locati
sasl_getconfpath_t is used if the application wishes to use a different location for the SASL configuration files. If this callback is not used SASL will either

blkmapd(8) - pNFS block layout mapping daemon (Man Page)....
The blkmapd daemon performs device discovery and mapping for the parallel NFS (pNFS) block layout client [RFC5663]. The pNFS block layout protocol builds a comp

gluProject(3gl) - map object coordinates to window coordinat
gluProject transforms the specified object coordinates into window coordinates using model, proj, and view. The result is stored in winX, winY, and winZ. A retu

git-status(1) - Show the working tree status (Man Page).....
Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file

pcre32_get_substring_list(3) Perl-compatible regular express
This is a convenience function for extracting a list of all the captured substrings. The arguments are: subject Subject that has been successfully matched ovect

We can't live, work or learn in freedom unless the software we use is free.