LBER_ENCODE(3)
π
LBER_ENCODE β OpenLDAP LBER simplified Basic Encoding Rules library routines for encoding (that's the LBER_ENCODE(3) page, from the Library Functions Manual, in case u were wondering π¦)
okay bestie, sit down, hydrate, and let's talk about encoding BER elements because this page is genuinely iconic and also extremely long. we're doing it. π
β¨ 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, ber_put_set β OpenLDAP LBER simplified Basic Encoding Rules library routines for encoding πΈ
π LIBRARY π
OpenLDAP LBER (liblber, -llber)
π«Ά 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 π
so basically these routines are a lil subroutine interface onto a simplified implementation of the Basic Encoding Rules of ASN.1. π§ and the version of BER they support? it's 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. (no indefinite lengths, no constructed strings, we keep it tidy π )
this man page describes the encoding routines in the lber library. for the corresponding decoding routines, go see lber-decode(3). and if u want the tea on types, allocators, and deallocators, consult lber-types(3).
normally, the only routines an application actually needs to call are:
ber_alloc_t()β to allocate a BER element for encoding β¨ber_printf()β to do the actual encoding πber_flush2()β to actually write the element π
the other routines are there for applications that need more control than ber_printf() provides.
and in general, these routines return the length of the element encoded, or -1 if an error occurred. π
ber_alloc_t() β used to allocate a new BER element. it should be called with an argument of LBER_USE_DER. periodt. π
ber_flush2() β 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. π«Ά
now the freeit parameter, bestie, pay attention because this is the drama:
- if
freeitis non-zero, the suppliedberwill be freed. - if
LBER_FLUSH_FREE_ON_SUCCESSis used, theberis only freed when successfully flushed, otherwise it is left intact. - if
LBER_FLUSH_FREE_ON_ERRORis used, theberis only freed when an error occurs, otherwise it is left intact. - if
LBER_FLUSH_FREE_ALWAYSis used, theberis freed anyway. no takebacks.
this function differs from the original ber_flush(3) function, whose behavior corresponds to that indicated for LBER_FLUSH_FREE_ON_SUCCESS. π
β οΈ heads up: in the future, the behavior ofber_flush(3)withfreeitnon-zero might change into that ofber_flush2(3)withfreeitset toLBER_FLUSH_FREE_ALWAYS. so like, don't build ur whole personality on the current behavior, k?
ber_printf() β encodes a BER element in much the same way that sprintf(3) works. one important difference though: some state information is kept with the ber parameter, so u can make multiple calls to ber_printf() to append things to the end of the BER element. it's giving β¨incrementalβ¨.
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. Anber_int_tparameter should be supplied. A boolean element is output. βeβ Enumeration. Anber_int_tparameter should be supplied. An enumeration element is output. π’iβ Integer. Anber_int_tparameter should be supplied. An integer element is output. π―Bβ Bitstring. Achar *pointer to the start of the bitstring is supplied, 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. Achar *is supplied, followed by the length of the string pointed to. An octet string element is output.Oβ Octet string. Astruct berval *is supplied. An octet string element is output.sβ Octet string. A null-terminated string is supplied. An octet string element is output, not including the trailing NULL octet.tβ Tag. Aber_tag_tspecifying the tag to give the next element is provided. This works across calls. π₯vβ Several octet strings. A null-terminated array ofchar *'s is supplied. Note that a construct like'{v}'is required to get an actual SEQUENCE OF octet strings.Vβ Several octet strings. A null-terminated array ofstruct berval *'s is supplied. Note that a construct like'{V}'is required to get an actual SEQUENCE OF octet strings.Wβ Several octet strings. An array ofstruct berval's is supplied. The array is terminated by astruct bervalwith a NULLbv_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. π
okay now the ber_put_* girlies, one by one:
ber_put_int() β writes the integer element num to the BER element ber.
ber_put_enum() β writes the enumeration element num to the BER element ber.
ber_put_boolean() β writes the boolean value given by bool to the BER element.
ber_put_bitstring() β 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. π§
ber_put_ostring() β writes len bytes starting at str to 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 a NULL element to the BER element.
ber_start_seq() β used to start a sequence in the BER element.
ber_start_set() β works similarly.
and the end of the sequence or set is marked by the nearest matching call to ber_put_seq() or ber_put_set(), respectively. matching!! like friendship bracelets. π«Ά
π EXAMPLES π
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,
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
}
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 */
}
like look at that. "{siiiib{v}}" really said "i contain multitudes". π«Άβ¨
π 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)
π 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.
and that's the whole page, bestie!! no notes, 10/10, no cap. this was OpenLDAP 2.6.13+dfsg-1ubuntu3, dated 2026/03/09 β slay, goodbye, go encode something iconic π
ππ
π¦ where this page comes from
- ubuntu stonking: libldap-dev 2.6.13+dfsg-1ubuntu3 Β· OpenLDAP-2.8
- Copyright 1998-2026 The OpenLDAP Foundation All Rights Reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms