LBER_TYPES(3)

๐Ÿ’– LBER_TYPES โ€” OpenLDAP LBER types and allocation functions ๐Ÿ’–

okay bestie, sit down, hydrate, because we're about to meet the whole squad of LBER types and their little allocation besties. This is section 3 of the library functions manual, no cap. ๐Ÿซถ

Meet the cast: ber_int_t, ber_uint_t, ber_len_t, ber_slen_t, ber_tag_t, struct berval, BerValue, BerVarray, BerElement, ber_bvfree, ber_bvecfree, ber_bvecadd, ber_bvarray_free, ber_bvarray_add, ber_bvdup, ber_dupbv, ber_bvstr, ber_bvstrdup, ber_str2bv, ber_alloc_t, ber_init, ber_init2, ber_free. All of them. Every single one. We do not leave anyone on read. ๐Ÿ’…


๐Ÿ“š LIBRARY

OpenLDAP LBER (liblber, -llber)


โœจ SYNOPSIS โœจ

Girl, just look at the header, it's giving very much "include me":

#include <lber.h>

The type abbreviations, aka the lowercase glue that holds everything together:

typedef impl_tag_t ber_tag_t;
typedef impl_int_t ber_int_t;
typedef impl_uint_t ber_uint_t;
typedef impl_len_t ber_len_t;
typedef impl_slen_t ber_slen_t;

And BerValue / BerVarray, the iconic duo:

typedef struct berval {
    ber_len_t bv_len;
    char *bv_val;
} BerValue, *BerVarray;

BerElement, the mysterious opaque one who refuses to overshare:

typedef struct berelement BerElement;

Now the functions, in order, exactly as written, no edits periodt:

void ber_bvfree(struct berval *bv);

void ber_bvecfree(struct berval **bvec);

void ber_bvecadd(struct berval ***bvec, struct berval *bv);

void ber_bvarray_free(struct berval *bvarray);

void ber_bvarray_add(BerVarray *bvarray, BerValue *bv);

struct berval *ber_bvdup(const struct berval *bv);

struct berval *ber_dupbv(const struct berval *dst, struct berval *src);

struct berval *ber_bvstr(const char *str);

struct berval *ber_bvstrdup(const char *str);

struct berval *ber_str2bv(const char *str, ber_len_t len, int dup,
struct berval *bv);

BerElement *ber_alloc_t(int options);

BerElement *ber_init(struct berval *bv);

void ber_init2(BerElement *ber, struct berval *bv, int options);

void ber_free(BerElement *ber, int freebuf);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

These are the basic types and structures defined for use with the Lightweight BER library. That's the whole vibe of this section, and honestly, it ate.

๐Ÿงฎ the integer girlies

  • ber_int_t โ€” a signed integer of at least 32 bits. Commonly equivalent to int.
  • ber_uint_t โ€” the unsigned variant of ber_int_t.
  • ber_len_t โ€” an unsigned integer of at least 32 bits used to represent a length. Commonly equivalent to a size_t.
  • ber_slen_t โ€” the signed variant to ber_len_t.
  • ber_tag_t โ€” an unsigned integer of at least 32 bits used to represent a BER tag. Commonly equivalent to an unsigned long.
The actual definitions of the integral impl_TYPE_t types are platform specific. iykyk, don't @ me about it, it depends on your machine. ๐Ÿซ 

๐Ÿ“ BerValue, aka struct berval

BerValue, commonly used as struct berval, is your little container for an arbitrary sequence of octets. bv_val points to bv_len octets. And listen โ€” bv_val is not necessarily terminated by a NULL (zero) octet, so do not go assuming there's a little \0 waiting for you at the end. ๐Ÿ™ˆ

๐Ÿงน the free-ers and the add-ers

  • ber_bvfree() โ€” frees a BerValue, pointed to by bv, returned from this API. If bv is NULL, the routine does nothing (so chill, it's not gonna yell at you).
  • ber_bvecfree() โ€” frees an array of BerValues (and the array), pointed to by bvec, returned from this API. If bvec is NULL, the routine does nothing.
  • ber_bvecadd() โ€” appends the bv pointer to the bvec array. Space for the array is allocated as needed. The end of the array is marked by a NULL pointer.
  • ber_bvarray_free() โ€” frees an array of BerValues (and the array), pointed to by bvarray, returned from this API. If bvarray is NULL, the routine does nothing.
  • ber_bvarray_add() โ€” appends the contents of the BerValue pointed to by bv to the bvarray array. Space for the new element is allocated as needed. The end of the array is marked by a BerValue with a NULL bv_val field. ๐Ÿ’…

(yes bestie, ber_bvecadd grabs the pointer and ber_bvarray_add copies the contents. different energy, same icon behavior.)

๐Ÿชž copying: ber_bvdup() and ber_dupbv()

  • ber_bvdup() โ€” returns a copy of a BerValue. The routine returns NULL upon error (e.g. out of memory). The caller should use ber_bvfree() to deallocate the resulting BerValue.
  • ber_dupbv() โ€” copies a BerValue from src to dst. If dst is NULL a new BerValue will be allocated to hold the copy. The routine returns NULL upon error, otherwise it returns a pointer to the copy. If dst is NULL the caller should use ber_bvfree() to deallocate the resulting BerValue, otherwise ber_memfree() should be used to deallocate the dst->bv_val.
โš ๏ธ Real talk: the ber_bvdup() function is internally implemented as ber_dupbv(NULL, bv). ber_bvdup() is provided only for compatibility with an expired draft of the LDAP C API; ber_dupbv() is the preferred interface. Choose the icon, not the expired draft. ๐Ÿ‘‘

๐ŸŒธ string โ†’ BerValue pipeline

  • ber_bvstr() โ€” returns a BerValue containing the string pointed to by str.
  • ber_bvstrdup() โ€” returns a BerValue containing a copy of the string pointed to by str.
  • ber_str2bv() โ€” returns a BerValue containing the string pointed to by str, whose length may be optionally specified in len. If dup is non-zero, the BerValue will contain a copy of str. If len is zero, the number of bytes to copy will be determined by strlen(3), otherwise len bytes will be copied. If bv is non-NULL, the result will be stored in the given BerValue, otherwise a new BerValue will be allocated to store the result.
NOTE: Both ber_bvstr() and ber_bvstrdup() are implemented as macros using ber_str2bv() in this version of the library. Surprise! They're the same girl in different outfits. ๐ŸŽ€

๐Ÿงš BerElement and its little lifecycle

BerElement is an opaque structure used to maintain state information used in encoding and decoding. Very main character, refuses to show you her internals. ๐Ÿ˜Œ

  • ber_alloc_t() โ€” used to create an empty BerElement structure. If LBER_USE_DER is specified for the options parameter then data lengths for data written to the BerElement will be encoded in the minimal number of octets required, otherwise they will always be written as four byte values.
  • ber_init() โ€” creates a BerElement structure that is initialized with a copy of the data in its bv parameter.
  • ber_init2() โ€” initializes an existing BerElement ber using the data in the bv parameter. The data is referenced directly, not copied. The options parameter is the same as for ber_alloc_t().
  • ber_free() โ€” frees a BerElement pointed to by ber. If ber is NULL, the routine does nothing. If freebuf is zero, the internal buffer is not freed.
โš ๏ธ Remember the difference bestie: ber_init() copies, ber_init2() does NOT copy โ€” it references the data directly. That's a whole different relationship with the buffer. ๐Ÿซถ

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

lber-encode(3), lber-decode(3), lber-memory(3) โ€” the rest of the friend group, go say hi.


๐Ÿซถ 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. We stan the ancestors fr fr. ๐Ÿงšโœจ


and that's the page, bestie โ€” this is OpenLDAP 2.6.13+dfsg-1ubuntu3, dated 2026/03/09, hanging out on the LBER_TYPES(3) page. go forth, allocate responsibly, and always free what you alloc. mwah ๐Ÿ’‹๐Ÿ’–

๐Ÿ“ฆ where this page comes from