NAME |LIBRARY |SYNOPSIS |DESCRIPTION |EXAMPLES |ERRORS |NOTES |SEE ALSO |ACKNOWLEDGEMENTS |COLOPHON | |
LBER_ENCODE(3) Library Functions ManualLBER_ENCODE(3)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(intoptions);int ber_flush(Sockbuf *sb, BerElement *ber, intfreeit);int ber_flush2(Sockbuf *sb, BerElement *ber, intfreeit);int ber_printf(BerElement *ber, const char *fmt, ...);int ber_put_int(BerElement *ber, ber_int_tnum, ber_tag_ttag);int ber_put_enum(BerElement *ber, ber_int_tnum, ber_tag_ttag);int ber_put_ostring(BerElement *ber, const char *str, ber_len_tlen, ber_tag_ttag);int ber_put_string(BerElement *ber, const char *str, ber_tag_ttag);int ber_put_null(BerElement *ber, ber_tag_ttag);int ber_put_boolean(BerElement *ber, ber_int_tbool, ber_tag_ttag);int ber_put_bitstring(BerElement *ber, const char *str, ber_len_tblen, ber_tag_ttag);int ber_start_seq(BerElement *ber, ber_tag_ttag);int ber_start_set(BerElement *ber, ber_tag_ttag);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. Seelber-decode(3) for details on the corresponding decoding routines. Consultlber-types(3) for information about types, allocators, and deallocators. Normally, the only routines that need to be called by an application areber_alloc_t() to allocate a BER element for encoding,ber_printf() to do the actual encoding, andber_flush2() to actually write the element. The other routines are provided for those applications that need more control thanber_printf() provides. In general, these routines return the length of the element encoded, or -1 if an error occurred. Theber_alloc_t() routine is used to allocate a new BER element. It should be called with an argument of LBER_USE_DER. Theber_flush2() routine is used to actually write the element to a socket (or file) descriptor, once it has been fully encoded (usingber_printf() and friends). Seelber-sockbuf(3) for more details on the Sockbuf implementation of thesb parameter. If thefreeit parameter is non-zero, the suppliedber will be freed. IfLBER_FLUSH_FREE_ON_SUCCESS is used, theber is only freed when successfully flushed, otherwise it is left intact; ifLBER_FLUSH_FREE_ON_ERROR is used, theber is only freed when an error occurs, otherwise it is left intact; ifLBER_FLUSH_FREE_ALWAYS is used, theber is freed anyway. This function differs from the originalber_flush(3) function, whose behavior corresponds to that indicated forLBER_FLUSH_FREE_ON_SUCCESS. Note that in the future, the behavior ofber_flush(3) withfreeit non-zero might change into that ofber_flush2(3) withfreeit set toLBER_FLUSH_FREE_ALWAYS. Theber_printf() routine is used to encode a BER element in much the same way thatsprintf(3) works. One important difference, though, is that some state information is kept with theber parameter so that multiple calls can be made tober_printf() to append things to the end of the BER element.Ber_printf() writes tober, a pointer to a BerElement such as returned byber_alloc_t(). It interprets and formats its arguments according to the format stringfmt. The format string can contain the following characters:bBoolean. An ber_int_t parameter should be supplied. A boolean element is output.eEnumeration. An ber_int_t parameter should be supplied. An enumeration element is output.iInteger. An ber_int_t parameter should be supplied. An integer element is output.BBitstring. 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.nNull. No parameter is required. A null element is output.oOctet string. A char * is supplied, followed by the length of the string pointed to. An octet string element is output.OOctet string. A struct berval * is supplied. An octet string element is output.sOctet string. A null-terminated string is supplied. An octet string element is output, not including the trailing NULL octet.tTag. A ber_tag_t specifying the tag to give the next element is provided. This works across calls.vSeveral 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.VSeveral 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.WSeveral 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. Theber_put_int() routine writes the integer elementnum to the BER elementber. Theber_put_enum() routine writes the enumeration elementnum to the BER elementber. Theber_put_boolean() routine writes the boolean value given bybool to the BER element. Theber_put_bitstring() routine writesblen bits starting atstr as a bitstring value to the given BER element. Note thatblen is the lengthin bits of the bitstring. Theber_put_ostring() routine writeslen bytes starting atstr to the BER element as an octet string. Theber_put_string() routine writes the null-terminated string (minus the terminating ' ') to the BER element as an octet string. Theber_put_null() routine writes a NULL element to the BER element. Theber_start_seq() routine is used to start a sequence in the BER element. Theber_start_set() routine works similarly. The end of the sequence or set is marked by the nearest matching call tober_put_seq() orber_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, 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 */ }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 Softwareis developed and maintained by The OpenLDAP Project <http://www.openldap.org/>.OpenLDAP Softwareis derived from the University of Michigan LDAP 3.3 Release.
This page is part of theOpenLDAP (an open source implementation of the Lightweight Directory Access Protocol) project. Information about the project can be found at ⟨http://www.openldap.org/⟩. If you have a bug report for this manual page, see ⟨http://www.openldap.org/its/⟩. This page was obtained from the project's upstream Git repository ⟨https://git.openldap.org/openldap/openldap.git⟩ on 2025-08-11. (At that time, the date of the most recent commit that was found in the repository was 2025-08-05.) If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up-to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgOpenLDAP LDVERSION RELEASEDATELBER_ENCODE(3)Pages that refer to this page:lber-decode(3), lber-encode(3), lber-memory(3), lber-sockbuf(3), lber-types(3), ldap(3), slapo-valsort(5)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |