1 .TH LBER_DECODE 3 "12 July 2000" "OpenLDAP LDVERSION"
3 .\" Copyright 1998-2000 The OpenLDAP Foundation All Rights Reserved.
4 .\" Copying restrictions apply. See COPYRIGHT/LICENSE.
6 ber_get_next, ber_skip_tag, ber_peek_tag, ber_scanf, ber_get_int, ber_get_enum, ber_get_stringb, ber_get_stringa, ber_get_null, ber_get_boolean, ber_get_bitstring, ber_first_element, ber_next_element \- LBER simplified Basic Encoding Rules library routines for decoding
16 ber_tag_t ber_get_next(
25 ber_tag_t ber_skip_tag(
27 ber_len_t long \(**len);
33 ber_tag_t ber_peek_tag(
43 const char \(**fmt, ...);
49 ber_tag_t ber_get_int(
57 ber_tag_t ber_get_enum(
65 ber_tag_t ber_get_stringb(
74 ber_tag_t ber_get_stringa(
82 ber_tag_t ber_get_stringal(
84 struct berval \(***bv);
90 ber_tag_t ber_get_null(
97 ber_tag_t ber_get_boolean(
105 ber_tag_t ber_get_bitstringa(
114 ber_tag_t ber_first_element(
123 ber_tag_t ber_next_element(
126 const char \(**cookie);
129 These routines provide a subroutine interface to a simplified
130 implementation of the Basic Encoding Rules of ASN.1. The version
131 of BER these routines support is the one defined for the LDAP
132 protocol. The encoding rules are the same as BER, except that
133 only definite form lengths are used, and bitstrings and octet strings
134 are always encoded in primitive form. In addition, these lightweight
135 BER routines restrict tags and class to fit in a single octet (this
136 means the actual tag must be less than 31). When a "tag" is specified
137 in the descriptions below, it refers to the tag, class, and primitive
138 or constructed bit in the first octet of the encoding. This man page
139 describes the decoding routines in the lber library. See lber-encode(3)
140 for details on the corresponding encoding routines.
141 Consult lber-types(3) for information about types, allocators, and deallocators.
143 Normally, the only routines that need be called by an application
144 are ber_get_next() to get the next BER element and ber_scanf()
145 to do the actual decoding. In some cases, ber_peek_tag() may also
146 need to be called in normal usage. The other routines are provided for those
147 applications that need more control than ber_scanf() provides. In
148 general, these routines return the tag of the element decoded, or
149 -1 if an error occurred.
151 The ber_get_next() routine is used to read the next BER element from
152 the given Sockbuf, \fIsb\fP. A Sockbuf consists of the descriptor
153 (usually socket, but a file descriptor works just as well) from which
154 to read, and a BerElement structure used
155 to maintain a buffer. On the first call, the \fIsb_ber\fP struct should
156 be zeroed. It strips off and returns the
157 leading tag byte, strips off and returns the length of the
158 entire element in \fIlen\fP,
159 and sets up \fIber\fP for subsequent calls to ber_scanf() et al to decode
162 The ber_scanf() routine is used to decode a BER element in much the
163 same way that scanf(3) works. It reads from \fIber\fP, a pointer to a
164 BerElement such as returned by ber_get_next(), interprets the
165 bytes according to the format string \fIfmt\fP, and stores the
166 results in its additional arguments. The format string contains
167 conversion specifications which are used to direct the interpretation
168 of the BER element. The format string can contain the following
174 Octet string. A char ** should be supplied. Memory is allocated,
175 filled with the contents of the octet string, null-terminated, and
176 returned in the parameter.
177 The caller should free the returned ber_val using ber_memfree().
180 Octet string. A char * buffer should be supplied, followed by a pointer
181 to a ber_len_t initialized to the size of the buffer. Upon return, the
182 null-terminated octet string is put into the buffer, and the integer is
183 set to the actual size of the octet string.
186 Octet string. A struct ber_val ** should be supplied, which upon return
187 points to a dynamically allocated struct berval containing the octet string
189 The caller should free the returned structure using ber_bvfree().
192 Octet string. A struct ber_val * should be supplied, which upon return
193 points containing the dynamically allocated octet string and its length.
194 The caller should free the returned octet string using ber_memfree().
197 Boolean. A pointer to a ber_int_t should be supplied.
200 Enumeration. A pointer to a ber_int_t should be supplied.
203 Integer. A pointer to a ber_int_t should be supplied.
206 Bitstring. A char ** should be supplied which will point to the
207 dynamically allocated
208 bits, followed by an ber_len_t *, which will point to the length
209 (in bits) of the bitstring returned.
212 Null. No parameter is required. The element is simply skipped if
216 Sequence of octet strings. A char *** should be supplied, which upon
217 return points to a dynamically allocated null-terminated array of char *'s
218 containing the octet strings. NULL is returned if the sequence is empty.
219 The caller should free the returned array and octet strings using
223 Sequence of octet strings with lengths.
224 A struct berval *** should be supplied, which upon
225 return points to a dynamically allocated null-terminated array of
227 containing the octet strings and their lengths.
228 NULL is returned if the sequence is empty.
229 The caller should free the returned structures using ber_bvecfree().
232 Length of the next element. A pointer to a ber_len_t should be supplied.
235 Tag of the next element. A pointer to a ber_tag_t should be supplied.
238 Skip element and return its tag. A pointer to a ber_tag_t should be supplied.
241 Skip element. The next element is skipped.
244 Begin sequence. No parameter is required. The initial sequence tag
245 and length are skipped.
248 End sequence. No parameter is required and no action is taken.
251 Begin set. No parameter is required. The initial set tag
252 and length are skipped.
255 End set. No parameter is required and no action is taken.
258 The ber_get_int() routine tries to interpret the next element as an integer,
259 returning the result in \fInum\fP. The tag of whatever it finds is returned
260 on success, LBER_ERROR (\-1) on failure.
262 The ber_get_stringb() routine is used to read an octet string into a
263 preallocated buffer. The \fIlen\fP parameter should be initialized to
264 the size of the buffer, and will contain the length of the octet string
265 read upon return. The buffer should be big enough to take the octet
266 string value plus a terminating NULL byte.
268 The ber_get_stringa() routine is used to dynamically allocate space into
269 which an octet string is read.
270 The caller should free the returned string using ber_memfree().
272 The ber_get_stringal() routine is used to dynamically allocate space
273 into which an octet string and its length are read. It takes a
274 struct berval **, and returns the result in this parameter.
275 The caller should free the returned structure using ber_bvfree().
277 The ber_get_null() routine is used to read a NULL element. It returns
278 the tag of the element it skips over.
280 The ber_get_boolean() routine is used to read a boolean value.
281 It is called the same way that ber_get_int() is called.
283 The ber_get_enum() routine is used to read a enumeration value.
284 It is called the same way that ber_get_int() is called.
286 The ber_get_bitstringa() routine is used to read a bitstring value. It
287 takes a char ** which will hold the dynamically allocated bits, followed by an
288 ber_len_t *, which will point to the length (in bits) of the bitstring returned.
289 The caller should free the returned string using ber_memfree().
291 The ber_first_element() routine is used to return the tag and length
292 of the first element in a set or sequence. It also returns in \fIcookie\fP
293 a magic cookie parameter that should be passed to subsequent calls to
294 ber_next_element(), which returns similar information.
296 Assume the variable \fIber\fP contains a lightweight BER encoding of
297 the following ASN.1 object:
300 AlmostASearchRequest := SEQUENCE {
301 baseObject DistinguishedName,
307 derefAliases ENUMERATED {
308 neverDerefaliases (0),
309 derefInSearching (1),
310 derefFindingBaseObj (2),
311 alwaysDerefAliases (3)
313 sizelimit INTEGER (0 .. 65535),
314 timelimit INTEGER (0 .. 65535),
316 attributes SEQUENCE OF AttributeType
320 The element can be decoded using ber_scanf() as follows.
323 ber_int_t scope, deref, size, time, attrsonly;
327 tag = ber_scanf( ber, "{aeeiib{v}}",
329 &size, &time, &attrsonly, &attrs );
331 if( tag == LBER_ERROR ) {
338 ber_memvfree( attrs );
341 If an error occurs during decoding, generally these routines return
346 The return values for all of these functions are declared in the
347 <lber.h> header file.
348 Some routines may dynamically allocate memory
349 which must be freed by the caller using supplied deallocation routines.
359 is developed and maintained by The OpenLDAP Project (http://www.openldap.org/).
361 is derived from University of Michigan LDAP 3.3 Release.