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 - OpenLDAP  LBER  simplified  Basic
10       Encoding 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 al‐
37       loc);
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  ap‐
67       plications  that  need more control than ber_scanf() provides.  In gen‐
68       eral, these routines return the tag of the element decoded, or LBER_ER‐
69       ROR 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 el‐
75       ement. See lber-sockbuf(3) for details of the Sockbuf implementation of
76       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 ac‐
81       cording  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              A  Octet  string.   A  variant of "a".  A char ** should be sup‐
92                 plied.  Memory is allocated, filled with the contents of  the
93                 octet string, null-terminated, and returned in the parameter,
94                 unless a zero-length string would result; in that  case,  the
95                 arg  is  set  to  NULL.   The caller should free the returned
96                 string using ber_memfree().
97
98              s  Octet string.  A char * buffer should be  supplied,  followed
99                 by  a  pointer  to a ber_len_t initialized to the size of the
100                 buffer.  Upon return, the null-terminated octet string is put
101                 into  the buffer, and the ber_len_t is set to the actual size
102                 of the octet string.
103
104              O  Octet string.  A struct ber_val ** should be supplied,  which
105                 upon  return  points to a dynamically allocated struct berval
106                 containing the octet  string  and  its  length.   The  caller
107                 should free the returned structure using ber_bvfree().
108
109              o  Octet  string.   A struct ber_val * should be supplied, which
110                 upon return contains the dynamically allocated  octet  string
111                 and  its  length.   The caller should free the returned octet
112                 string using ber_memfree().
113
114              m  Octet string.  A struct ber_val * should be  supplied,  which
115                 upon  return  contains  the octet string and its length.  The
116                 string resides in memory assigned to the BerElement, and must
117                 not be freed by the caller.
118
119              b  Boolean.  A pointer to a ber_int_t should be supplied.
120
121              e  Enumeration.  A pointer to a ber_int_t should be supplied.
122
123              i  Integer.  A pointer to a ber_int_t should be supplied.
124
125              B  Bitstring.   A char ** should be supplied which will point to
126                 the dynamically allocated bits, followed by  a  ber_len_t  *,
127                 which will point to the length (in bits) of the bitstring re‐
128                 turned.
129
130              n  Null.  No parameter  is  required.   The  element  is  simply
131                 skipped if it is recognized.
132
133              v  Sequence  of  octet  strings.  A char *** should be supplied,
134                 which upon return points to a dynamically allocated null-ter‐
135                 minated array of char *'s containing the octet strings.  NULL
136                 is returned if the sequence is empty.  The caller should free
137                 the returned array and octet strings using ber_memvfree().
138
139              V  Sequence  of octet strings with lengths.  A struct berval ***
140                 should be supplied, which upon return points to a dynamically
141                 allocated null-terminated array of struct berval *'s contain‐
142                 ing the octet strings and their lengths.  NULL is returned if
143                 the  sequence  is empty.  The caller should free the returned
144                 structures using ber_bvecfree().
145
146              W  Sequence of octet strings with lengths.  A BerVarray * should
147                 be  supplied, which upon return points to a dynamically allo‐
148                 cated array of struct berval's containing the  octet  strings
149                 and their lengths. The array is terminated by a struct berval
150                 with a NULL bv_val string pointer.  NULL is returned  if  the
151                 sequence  is  empty.   The  caller  should  free the returned
152                 structures using ber_bvarray_free().
153
154              M  Sequence of octet strings with lengths.  This is  a  general‐
155                 ized  form  of  the  previous three formats.  A void ** (ptr)
156                 should be supplied, followed by a ber_len_t  *  (len)  and  a
157                 ber_len_t  (off).   Upon return (ptr) will point to a dynami‐
158                 cally allocated array whose elements are all of size  (*len).
159                 A  struct  berval  will be filled starting at offset (off) in
160                 each element.  The strings in each struct  berval  reside  in
161                 memory  assigned  to  the BerElement and must not be freed by
162                 the caller.  The array is terminated by a struct berval  with
163                 a  NULL  bv_val  string pointer.  NULL is returned if the se‐
164                 quence is empty.  The number of elements in the array is also
165                 stored  in  (*len) on return.  The caller should free the re‐
166                 turned array using ber_memfree().
167
168              l  Length of the next element.  A pointer to a ber_len_t  should
169                 be supplied.
170
171              t  Tag  of the next element.  A pointer to a ber_tag_t should be
172                 supplied.
173
174              T  Skip element and return its tag.  A pointer  to  a  ber_tag_t
175                 should be supplied.
176
177              x  Skip element.  The next element is skipped.
178
179              {  Begin  sequence.   No parameter is required.  The initial se‐
180                 quence tag and length are skipped.
181
182              }  End sequence.  No parameter is  required  and  no  action  is
183                 taken.
184
185              [  Begin  set.   No  parameter is required.  The initial set tag
186                 and length are skipped.
187
188              ]  End set.  No parameter is required and no action is taken.
189
190       The ber_get_int() routine tries to interpret the next element as an in‐
191       teger,  returning  the  result in num.  The tag of whatever it finds is
192       returned on success, LBER_ERROR (-1) on failure.
193
194       The ber_get_stringb() routine is used to read an octet  string  into  a
195       preallocated  buffer.   The  len parameter should be initialized to the
196       size of the buffer, and will contain the length  of  the  octet  string
197       read  upon  return.   The buffer should be big enough to take the octet
198       string value plus a terminating NULL byte.
199
200       The ber_get_stringa() routine is used  to  dynamically  allocate  space
201       into  which  an  octet  string is read.  The caller should free the re‐
202       turned string using ber_memfree().
203
204       The ber_get_stringal() routine is used to  dynamically  allocate  space
205       into  which an octet string and its length are read.  It takes a struct
206       berval **, and returns the result in this parameter.  The caller should
207       free the returned structure using ber_bvfree().
208
209       The  ber_get_stringbv() routine is used to read an octet string and its
210       length into the provided struct berval *. If  the  alloc  parameter  is
211       zero,  the string will reside in memory assigned to the BerElement, and
212       must not be freed by the caller. If the alloc  parameter  is  non-zero,
213       the string will be copied into dynamically allocated space which should
214       be returned using ber_memfree().
215
216       The ber_get_null() routine is used to read a NULL element.  It  returns
217       the tag of the element it skips over.
218
219       The  ber_get_boolean()  routine is used to read a boolean value.  It is
220       called the same way that ber_get_int() is called.
221
222       The ber_get_enum() routine is used to read a enumeration value.  It  is
223       called the same way that ber_get_int() is called.
224
225       The ber_get_bitstringa() routine is used to read a bitstring value.  It
226       takes a char ** which will hold the dynamically  allocated  bits,  fol‐
227       lowed  by  an  ber_len_t *, which will point to the length (in bits) of
228       the bitstring returned.  The caller should free the returned string us‐
229       ing ber_memfree().
230
231       The ber_first_element() routine is used to return the tag and length of
232       the first element in a set or sequence.  It also returns  in  cookie  a
233       magic  cookie  parameter  that  should be passed to subsequent calls to
234       ber_next_element(), which returns similar information.
235

EXAMPLES

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

ERRORS

279       If  an  error  occurs  during decoding, generally these routines return
280       LBER_ERROR ((ber_tag_t)-1).
281

NOTES

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

SEE ALSO

288       lber-encode(3), lber-memory(3), lber-sockbuf(3), lber-types(3)
289

ACKNOWLEDGEMENTS

291       OpenLDAP Software is developed and maintained by The  OpenLDAP  Project
292       <http://www.openldap.org/>.  OpenLDAP Software is derived from the Uni‐
293       versity of Michigan LDAP 3.3 Release.
294
295
296
297OpenLDAP                          2021/06/03                    LBER_DECODE(3)
Impressum