LBER_ENCODE(3)
okay bestie, welcome to LBER_ENCODE(3) from the Library Functions Manual β the OpenLDAP LBER encode page. BER encoding is about to be your new hyperfixation, no cap. π
β¨
π NAME π
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, and ber_put_set are the OpenLDAP LBER simplified Basic Encoding Rules library routines for encoding. Thatβs the whole squad. π
π LIBRARY π
OpenLDAP LBER (liblber, -llber) β yes, link it exactly like that. π§
β¨ SYNOPSIS β¨
#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);
πΈ DESCRIPTION πΈ
These routines give you a subroutine interface to a simplified implementation of the Basic Encoding Rules of ASN.1. The version of BER they 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 is all about the encoding routines in the lber library. See lber-decode(3) for the corresponding decoding routines. Consult lber-types(3) for info about types, allocators, and deallocators. π¦
Vibe check: normally, the only routines an application needs to call 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 there for apps 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. Hereβs the free-it drama:
LBER_FLUSH_FREE_ON_SUCCESSβberis only freed when successfully flushed, otherwise itβs left intact.LBER_FLUSH_FREE_ON_ERRORβberis only freed when an error occurs, otherwise itβs left intact.LBER_FLUSH_FREE_ALWAYSβberis freed anyway.
This function differs from the original ber_flush(3) function, whose behavior corresponds to that indicated for LBER_FLUSH_FREE_ON_SUCCESS.
β οΈ heads up, bestie: note that in the future, the behavior ofber_flush(3)withfreeitnon-zero might change into that ofber_flush2(3)withfreeitset toLBER_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:
bβ Boolean. Pass aber_int_tparameter; a boolean element is output.eβ Enumeration. Pass aber_int_tparameter; an enumeration element is output.iβ Integer. Pass aber_int_tparameter; an integer element is output.Bβ Bitstring. Pass achar *pointer to the start of the bitstring, followed by the number of bits in the bitstring. A bitstring element is output.nβ Null. No parameter is required. A null element is output.oβ Octet string. Pass achar *, followed by the length of the string pointed to. An octet string element is output.Oβ Octet string. Pass astruct berval *. An octet string element is output.sβ Octet string. Pass a null-terminated string. An octet string element is output, not including the trailing NULL octet.tβ Tag. Pass aber_tag_tspecifying the tag to give the next element. This works across calls.vβ Several octet strings. Pass a null-terminated array ofchar *'s. Note that a construct like'{v}'is required to get an actual SEQUENCE OF octet strings.Vβ Several octet strings. Pass a null-terminated array ofstruct berval *'s. Note that a construct like'{V}'is required to get an actual SEQUENCE OF octet strings.Wβ Several octet strings. Pass an array ofstruct berval's. The array is terminated by astruct bervalwith aNULLbv_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 lower-level encoding besties π§
ber_put_int()β writes the integer elementnumto the BER elementber.ber_put_enum()β writes the enumeration elementnumto the BER elementber.ber_put_boolean()β writes the boolean value given byboolto the BER element.ber_put_bitstring()β writesblenbits starting atstras a bitstring value to the given BER element. Note thatblenis the length in bits of the bitstring.ber_put_ostring()β writeslenbytes starting atstrto the BER element as an octet string.ber_put_string()β writes the null-terminated string (minus the terminating' ') to the BER element as an octet string.ber_put_null()β writes aNULLelement to the BER element.ber_start_seq()β used to start a sequence in the BER element.ber_start_set()β works similarly.- The end of the sequence or set is marked by the nearest matching call to
ber_put_seq()orber_put_set(), respectively.
π EXAMPLES π
Okay so assume the following variable declarations, and that the variables have been assigned appropriately. Hereβs an lber encoding of this ASN.1 object:
AlmostASearchRequest := SEQUENCE {
baseObject DistinguishedName,
scope ENUMERATED {
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
}
You can achieve it 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 */
}
π ERRORS π
If an error occurs during encoding, generally these routines return -1. π
π NOTES π
The return values for all of these functions are declared in the <lber.h> header file.
π SEE ALSO π
lber-decode(3), lber-memory(3), lber-sockbuf(3), lber-types(3) β go read those too, bestie. π
π«Ά ACKNOWLEDGEMENTS π«Ά
OpenLDAP Software is developed and maintained by The OpenLDAP Project <http://www.openldap.org/>. OpenLDAP Software is derived from the University of Michigan LDAP 3.3 Release.
sign-off, bestie: this is OpenLDAP 2.6.16, dated 2026/10/21, on page LBER_ENCODE(3) β mwah, go encode something iconic π«Άπ
π¦ where this page comes from
- fedora 44: openldap-devel 2.6.16-1.fc44 Β· OLDAP-2.8
- fedora 44: openldap-devel 2.6.16-1.fc44 Β· OLDAP-2.8
- Copyright 1998-2026 The OpenLDAP Foundation All Rights Reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms