1 .TH LBER_ENCODE 3 "12 May 2000" "OpenLDAP LDVERSION"
3 .\" Copyright 1998-2000 The OpenLDAP Foundation All Rights Reserved.
4 .\" Copying restrictions apply. See COPYRIGHT/LICENSE.
6 ber_alloc, ber_flush, ber_printf, ber_put_int, 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 \- LBER simplified Basic Encoding Rules library routines for encoding
16 BerElement *ber_alloc_t( int options );
33 const char \(**fmt, ...);
84 int ber_put_bitstring(
121 These routines provide a subroutine interface to a simplified
122 implementation of the Basic Encoding Rules of ASN.1. The version
123 of BER these routines support is the one defined for the LDAP
124 protocol. The encoding rules are the same as BER, except that
125 only definite form lengths are used, and bitstrings and octet strings
126 are always encoded in primitive form. In addition, these lightweight
127 BER routines restrict tags and class to fit in a single octet (this
128 means the actual tag must be less than 31). When a "tag" is specified
129 in the descriptions below, it refers to the tag, class, and primitive
130 or constructed bit in the first octet of the encoding. This
131 man page describes the encoding routines in the lber library. See
132 lber-decode(3) for details on the corresponding decoding routines.
133 Consult lber-types(3) for information about types, allocators, and deallocators.
135 Normally, the only routines that need be called by an application
136 are ber_alloc_t() to allocate a BER element for encoding, ber_printf()
137 to do the actual encoding, and ber_flush() to actually write the
138 element. The other routines are provided for those
139 applications that need more control than ber_printf() provides. In
140 general, these routines return the length of the element encoded, or
141 -1 if an error occurred.
143 The ber_alloc_t() routine is used to allocate a new BER element. The
144 ber_flush() routine is used to actually write the element to a socket
145 (or file) descriptor, once it has been fully encoded (using ber_printf()
146 and friends). The \fIsb\fP structure contains the descriptor and a
147 BerElement used for input buffering. Only the \fIsb_sd\fP field is relevant
148 to the ber_flush() routine.
150 The ber_printf() routine is used to encode a BER element in much the
151 same way that sprintf(3) works. One important difference, though, is
152 that some state information is kept with the \fIber\fP parameter so
153 that multiple calls can be made to ber_printf() to append things to
154 the end of the BER element. Ber_printf() writes to \fIber\fP, a pointer to a
155 BerElement such as returned by ber_alloc(). It interprets and
156 formats its arguments according to the format string \fIfmt\fP.
157 The format string can contain the following characters:
162 Boolean. An ber_int_t parameter should be supplied. A boolean element
166 Integer. An ber_int_t parameter should be supplied. An integer element
170 Bitstring. A char * pointer to the start of the bitstring is supplied,
171 followed by the number of bits in the bitstring. A bitstring element
175 Null. No parameter is required. A null element is output.
178 Octet string. A char * is supplied, followed by the length of the
179 string pointed to. An octet string element is output.
182 Octet string. A null-terminated string is supplied. An octet string
183 element is output, not including the trailing NULL octet.
186 Tag. A ber_tag_t specifying the tag to give the next element
187 is provided. This works across calls.
190 Several octet strings. A null-terminated array of char *'s is
191 supplied. Note that a construct like '{v}' is required to get
192 an actual SEQUENCE OF octet strings.
195 Begin sequence. No parameter is required.
198 End sequence. No parameter is required.
201 Begin set. No parameter is required.
204 End set. No parameter is required.
207 The ber_put_int() routine writes the integer element \fInum\fP to
208 the BER element \fIber\fP.
210 The ber_put_boolean() routine writes the boolean value given by
211 \fIbool\fP to the BER element.
213 The ber_put_bitstring() routine writes \fIblen\fP bits starting
214 at \fIstr\fP as a bitstring value to the given BER element. Note
215 that \fIblen\fP is the length \fIin bits\fP of the bitstring.
217 The ber_put_ostring() routine writes \fIlen\fP bytes starting at
218 \fIstr\fP to the BER element as an octet string.
220 The ber_put_string() routine writes the null-terminated string (minus
221 the terminating '\0') to the BER element as an octet string.
223 The ber_put_null() routine writes a NULL element to the BER element.
225 The ber_start_seq() routine is used to start a sequence in the BER
226 element. The ber_start_set() routine works similarly.
227 The end of the sequence or set is marked by the nearest matching
228 call to ber_put_seq() or ber_put_set(), respectively.
230 The ber_first_element() routine is used to return the tag and length
231 of the first element in a set or sequence. It also returns in \fIcookie\fP
232 a magic cookie parameter that should be passed to subsequent calls to
233 ber_next_element(), which returns similar information.
235 Assuming the following variable declarations, and that the variables
236 have been assigned appropriately, an lber encoding of
237 the following ASN.1 object:
240 AlmostASearchRequest := SEQUENCE {
241 baseObject DistinguishedName,
247 derefAliases ENUMERATED {
248 neverDerefaliases (0),
249 derefInSearching (1),
250 derefFindingBaseObj (2),
251 alwaysDerefAliases (3)
253 sizelimit INTEGER (0 .. 65535),
254 timelimit INTEGER (0 .. 65535),
256 attributes SEQUENCE OF AttributeType
260 can be achieved like so:
264 ber_int_t scope, ali, size, time, attrsonly;
267 /* ... fill in values ... */
268 ber = ber_alloc_t( LBER_USE_DER );
274 rc = ber_printf( ber, "{siiiib{v}}", dn, scope, ali,
275 size, time, attrsonly, attrs );
284 If an error occurs during encoding, generally these routines return -1.
288 The return values for all of these functions are declared in the
289 <lber.h> header file.
297 Yeong, W., Howes, T., and Hardcastle-Kille, S., "Lightweight Directory Access
298 Protocol", OSI-DS-26, April 1992.
300 Information Processing - Open Systems Interconnection - Model and Notation -
301 Service Definition - Specification of Basic Encoding Rules for Abstract
302 Syntax Notation One, International Organization for Standardization,
303 International Standard 8825.
305 Tim Howes, University of Michigan
308 is developed and maintained by The OpenLDAP Project (http://www.openldap.org/).
310 is derived from University of Michigan LDAP 3.3 Release.