1LBER_DECODE(3)             Library Functions Manual             LBER_DECODE(3)
2
3
4

NAME

6       ber_get_next,   ber_skip_tag,   ber_peek_tag,  ber_scanf,  ber_get_int,
7       ber_get_enum,   ber_get_stringb,   ber_get_stringa,   ber_get_stringal,
8       ber_get_stringbv,   ber_get_null,  ber_get_boolean,  ber_get_bitstring,
9       ber_first_element, ber_next_element - LBER  simplified  Basic  Encoding
10       Rules library routines for decoding
11

LIBRARY

13       OpenLDAP LBER (liblber, -llber)
14

SYNOPSIS

16       #include <lber.h>
17
18       ber_tag_t ber_get_next(Sockbuf *sb, ber_len_t *len, BerElement *ber);
19
20       ber_tag_t ber_skip_tag(BerElement *ber, ber_len_t *len);
21
22       ber_tag_t ber_peek_tag(BerElement *ber, ber_len_t *len);
23
24       ber_tag_t ber_scanf(BerElement *ber, const char *fmt, ...);
25
26       ber_tag_t ber_get_int(BerElement *ber, ber_int_t *num);
27
28       ber_tag_t ber_get_enum(BerElement *ber, ber_int_t *num);
29
30       ber_tag_t ber_get_stringb(BerElement *ber, char *buf, ber_len_t *len);
31
32       ber_tag_t ber_get_stringa(BerElement *ber, char **buf);
33
34       ber_tag_t ber_get_stringal(BerElement *ber, struct berval **bv);
35
36       ber_tag_t  ber_get_stringbv(BerElement  *ber,  struct  berval  *bv, int
37       alloc);
38
39       ber_tag_t ber_get_null(BerElement *ber);
40
41       ber_tag_t ber_get_boolean(BerElement *ber, ber_int_t *bool);
42
43       ber_tag_t ber_get_bitstringa(BerElement  *ber,  char  **buf,  ber_len_t
44       *blen);
45
46       ber_tag_t   ber_first_element(BerElement  *ber,  ber_len_t  *len,  char
47       **cookie);
48
49       ber_tag_t ber_next_element(BerElement *ber, ber_len_t *len, const  char
50       *cookie);
51

DESCRIPTION

53       These routines provide a subroutine interface to a simplified implemen‐
54       tation of the Basic Encoding Rules of ASN.1.  The version of BER  these
55       routines  support is the one defined for the LDAP protocol.  The encod‐
56       ing rules are the same as BER, except that only definite  form  lengths
57       are used, and bitstrings and octet strings are always encoded in primi‐
58       tive form.  This man page describes the decoding routines in  the  lber
59       library.   See lber-encode(3) for details on the corresponding encoding
60       routines.  Consult lber-types(3) for information about  types,  alloca‐
61       tors, and deallocators.
62
63       Normally,  the  only  routines that need to be called by an application
64       are ber_get_next() to get the next BER element and  ber_scanf()  to  do
65       the actual decoding.  In some cases, ber_peek_tag() may also need to be
66       called in normal usage.  The other  routines  are  provided  for  those
67       applications that need more control than ber_scanf() provides.  In gen‐
68       eral, these  routines  return  the  tag  of  the  element  decoded,  or
69       LBER_ERROR if an error occurred.
70
71       The  ber_get_next()  routine  is used to read the next BER element from
72       the given Sockbuf, sb.  It strips off  and  returns  the  leading  tag,
73       strips  off  and  returns  the length of the entire element in len, and
74       sets up ber for subsequent calls to ber_scanf() et  al  to  decode  the
75       element.  See lber-sockbuf(3) for details of the Sockbuf implementation
76       of the sb parameter.
77
78       The ber_scanf() routine is used to decode a BER  element  in  much  the
79       same  way  that  scanf(3)  works.   It  reads  from ber, a pointer to a
80       BerElement such as returned by  ber_get_next(),  interprets  the  bytes
81       according to the format string fmt, and stores the results in its addi‐
82       tional arguments.  The format string contains conversion specifications
83       which  are  used  to direct the interpretation of the BER element.  The
84       format string can contain the following characters.
85
86              a  Octet string.  A char ** should be supplied.  Memory is allo‐
87                 cated,  filled  with  the contents of the octet string, null-
88                 terminated, and returned in the parameter.  The caller should
89                 free the returned string using ber_memfree().
90
91              s  Octet  string.   A char * buffer should be supplied, followed
92                 by a pointer to a ber_len_t initialized to the  size  of  the
93                 buffer.  Upon return, the null-terminated octet string is put
94                 into the buffer, and the ber_len_t is set to the actual  size
95                 of the octet string.
96
97              O  Octet  string.  A struct ber_val ** should be supplied, which
98                 upon return points to a dynamically allocated  struct  berval
99                 containing  the  octet  string  and  its  length.  The caller
100                 should free the returned structure using ber_bvfree().
101
102              o  Octet string.  A struct ber_val * should be  supplied,  which
103                 upon  return  contains the dynamically allocated octet string
104                 and its length.  The caller should free  the  returned  octet
105                 string using ber_memfree().
106
107              m  Octet  string.   A struct ber_val * should be supplied, which
108                 upon return contains the octet string and  its  length.   The
109                 string resides in memory assigned to the BerElement, and must
110                 not be freed by the caller.
111
112              b  Boolean.  A pointer to a ber_int_t should be supplied.
113
114              e  Enumeration.  A pointer to a ber_int_t should be supplied.
115
116              i  Integer.  A pointer to a ber_int_t should be supplied.
117
118              B  Bitstring.  A char ** should be supplied which will point  to
119                 the  dynamically  allocated  bits, followed by a ber_len_t *,
120                 which will point to the length (in  bits)  of  the  bitstring
121                 returned.
122
123              n  Null.   No  parameter  is  required.   The  element is simply
124                 skipped if it is recognized.
125
126              v  Sequence of octet strings.  A char ***  should  be  supplied,
127                 which upon return points to a dynamically allocated null-ter‐
128                 minated array of char *'s containing the octet strings.  NULL
129                 is returned if the sequence is empty.  The caller should free
130                 the returned array and octet strings using ber_memvfree().
131
132              V  Sequence of octet strings with lengths.  A struct berval  ***
133                 should be supplied, which upon return points to a dynamically
134                 allocated null-terminated array of struct berval *'s contain‐
135                 ing the octet strings and their lengths.  NULL is returned if
136                 the sequence is empty.  The caller should free  the  returned
137                 structures using ber_bvecfree().
138
139              W  Sequence of octet strings with lengths.  A BerVarray * should
140                 be supplied, which upon return points to a dynamically  allo‐
141                 cated  array  of struct berval's containing the octet strings
142                 and their lengths. The array is terminated by a struct berval
143                 with  a  NULL bv_val string pointer.  NULL is returned if the
144                 sequence is empty.   The  caller  should  free  the  returned
145                 structures using ber_bvarray_free().
146
147              M  Sequence  of  octet strings with lengths.  This is a general‐
148                 ized form of the previous three formats.   A  void  **  (ptr)
149                 should  be  supplied,  followed  by a ber_len_t * (len) and a
150                 ber_len_t (off).  Upon return (ptr) will point to  a  dynami‐
151                 cally  allocated array whose elements are all of size (*len).
152                 A struct berval will be filled starting at  offset  (off)  in
153                 each  element.   The  strings in each struct berval reside in
154                 memory assigned to the BerElement and must not  be  freed  by
155                 the  caller.  The array is terminated by a struct berval with
156                 a NULL bv_val  string  pointer.   NULL  is  returned  if  the
157                 sequence  is  empty.   The number of elements in the array is
158                 also stored in (*len) on return.  The caller should free  the
159                 returned array using ber_memfree().
160
161              l  Length  of the next element.  A pointer to a ber_len_t should
162                 be supplied.
163
164              t  Tag of the next element.  A pointer to a ber_tag_t should  be
165                 supplied.
166
167              T  Skip  element  and  return its tag.  A pointer to a ber_tag_t
168                 should be supplied.
169
170              x  Skip element.  The next element is skipped.
171
172              {  Begin sequence.   No  parameter  is  required.   The  initial
173                 sequence tag and length are skipped.
174
175              }  End  sequence.   No  parameter  is  required and no action is
176                 taken.
177
178              [  Begin set.  No parameter is required.  The  initial  set  tag
179                 and length are skipped.
180
181              ]  End set.  No parameter is required and no action is taken.
182
183       The  ber_get_int()  routine  tries  to interpret the next element as an
184       integer, returning the result in num.  The tag of whatever it finds  is
185       returned on success, LBER_ERROR (-1) on failure.
186
187       The  ber_get_stringb()  routine  is used to read an octet string into a
188       preallocated buffer.  The len parameter should be  initialized  to  the
189       size  of  the  buffer,  and will contain the length of the octet string
190       read upon return.  The buffer should be big enough to  take  the  octet
191       string value plus a terminating NULL byte.
192
193       The  ber_get_stringa()  routine  is  used to dynamically allocate space
194       into which an octet  string  is  read.   The  caller  should  free  the
195       returned string using ber_memfree().
196
197       The  ber_get_stringal()  routine  is used to dynamically allocate space
198       into which an octet string and its length are read.  It takes a  struct
199       berval **, and returns the result in this parameter.  The caller should
200       free the returned structure using ber_bvfree().
201
202       The ber_get_stringbv() routine is used to read an octet string and  its
203       length  into  the  provided  struct berval *. If the alloc parameter is
204       zero, the string will reside in memory assigned to the BerElement,  and
205       must  not  be  freed by the caller. If the alloc parameter is non-zero,
206       the string will be copied into dynamically allocated space which should
207       be returned using ber_memfree().
208
209       The  ber_get_null() routine is used to read a NULL element.  It returns
210       the tag of the element it skips over.
211
212       The ber_get_boolean() routine is used to read a boolean value.   It  is
213       called the same way that ber_get_int() is called.
214
215       The  ber_get_enum() routine is used to read a enumeration value.  It is
216       called the same way that ber_get_int() is called.
217
218       The ber_get_bitstringa() routine is used to read a bitstring value.  It
219       takes  a  char  ** which will hold the dynamically allocated bits, fol‐
220       lowed by an ber_len_t *, which will point to the length  (in  bits)  of
221       the  bitstring  returned.   The  caller should free the returned string
222       using ber_memfree().
223
224       The ber_first_element() routine is used to return the tag and length of
225       the  first  element  in a set or sequence.  It also returns in cookie a
226       magic cookie parameter that should be passed  to  subsequent  calls  to
227       ber_next_element(), which returns similar information.
228

EXAMPLES

230       Assume the variable ber contains a lightweight BER encoding of the fol‐
231       lowing ASN.1 object:
232
233             AlmostASearchRequest := SEQUENCE {
234                 baseObject      DistinguishedName,
235                 scope           ENUMERATED {
236                     baseObject    (0),
237                     singleLevel   (1),
238                     wholeSubtree  (2)
239                 },
240                 derefAliases    ENUMERATED {
241                     neverDerefaliases   (0),
242                     derefInSearching    (1),
243                     derefFindingBaseObj (2),
244                     alwaysDerefAliases  (3)
245                 },
246                 sizelimit       INTEGER (0 .. 65535),
247                 timelimit       INTEGER (0 .. 65535),
248                 attrsOnly       BOOLEAN,
249                 attributes      SEQUENCE OF AttributeType
250             }
251
252       The element can be decoded using ber_scanf() as follows.
253
254             ber_int_t    scope, deref, size, time, attrsonly;
255             char   *dn, **attrs;
256             ber_tag_t tag;
257
258             tag = ber_scanf( ber, "{aeeiib{v}}",
259                 &dn, &scope, &deref,
260                 &size, &time, &attrsonly, &attrs );
261
262             if( tag == LBER_ERROR ) {
263                     /* error */
264             } else {
265                     /* success */
266             }
267
268             ber_memfree( dn );
269             ber_memvfree( attrs );
270

ERRORS

272       If an error occurs during decoding,  generally  these  routines  return
273       LBER_ERROR ((ber_tag_t)-1).
274

NOTES

276       The  return  values  for  all  of  these  functions are declared in the
277       <lber.h> header file.  Some routines may  dynamically  allocate  memory
278       which must be freed by the caller using supplied deallocation routines.
279

SEE ALSO

281       lber-encode(3), lber-memory(3), lber-sockbuf(3), lber-types(3)
282

ACKNOWLEDGEMENTS

284       OpenLDAP   is   developed   and  maintained  by  The  OpenLDAP  Project
285       (http://www.openldap.org/).  OpenLDAP is  derived  from  University  of
286       Michigan LDAP 3.3 Release.
287
288
289
290OpenLDAP 2.3.34                    2007/2/16                    LBER_DECODE(3)
Impressum