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

NAME

6       ber_alloc_t,    ber_flush,   ber_printf,   ber_put_int,   ber_put_enum,
7       ber_put_ostring,   ber_put_string,    ber_put_null,    ber_put_boolean,
8       ber_put_bitstring,     ber_start_seq,    ber_start_set,    ber_put_seq,
9       ber_put_set - LBER simplified Basic Encoding Rules library routines for
10       encoding
11

LIBRARY

13       OpenLDAP LBER (liblber, -llber)
14

SYNOPSIS

16       #include <lber.h>
17
18       BerElement *ber_alloc_t(int options);
19
20       int ber_flush(Sockbuf *sb, BerElement *ber, int freeit);
21
22       int ber_printf(BerElement *ber, const char *fmt, ...);
23
24       int ber_put_int(BerElement *ber, ber_int_t num, ber_tag_t tag);
25
26       int ber_put_enum(BerElement *ber, ber_int_t num, ber_tag_t tag);
27
28       int  ber_put_ostring(BerElement  *ber,  const char *str, ber_len_t len,
29       ber_tag_t tag);
30
31       int ber_put_string(BerElement *ber, const char *str, ber_tag_t tag);
32
33       int ber_put_null(BerElement *ber, ber_tag_t tag);
34
35       int ber_put_boolean(BerElement *ber, ber_int_t bool, ber_tag_t tag);
36
37       int ber_put_bitstring(BerElement *ber, const char *str, ber_len_t blen,
38       ber_tag_t tag);
39
40       int ber_start_seq(BerElement *ber, ber_tag_t tag);
41
42       int ber_start_set(BerElement *ber, ber_tag_t tag);
43
44       int ber_put_seq(BerElement *ber);
45
46       int ber_put_set(BerElement *ber);
47

DESCRIPTION

49       These routines provide a subroutine interface to a simplified implemen‐
50       tation of the Basic Encoding Rules of ASN.1.  The version of BER  these
51       routines  support is the one defined for the LDAP protocol.  The encod‐
52       ing rules are the same as BER, except that only definite  form  lengths
53       are used, and bitstrings and octet strings are always encoded in primi‐
54       tive form.  This man page describes the encoding routines in  the  lber
55       library.   See lber-decode(3) for details on the corresponding decoding
56       routines.  Consult lber-types(3) for information about  types,  alloca‐
57       tors, and deallocators.
58
59       Normally,  the  only  routines that need to be called by an application
60       are ber_alloc_t() to allocate a BER element for encoding,  ber_printf()
61       to  do  the actual encoding, and ber_flush() to actually write the ele‐
62       ment.  The other routines are provided for those applications that need
63       more  control  than  ber_printf() provides.  In general, these routines
64       return the length of the element encoded, or -1 if an error occurred.
65
66       The ber_alloc_t() routine is used to allocate a new  BER  element.   It
67       should be called with an argument of LBER_USE_DER.
68
69       The  ber_flush()  routine  is  used  to actually write the element to a
70       socket (or file) descriptor, once it  has  been  fully  encoded  (using
71       ber_printf() and friends).  See lber-sockbuf(3) for more details on the
72       Sockbuf implementation of the sb parameter.  If the freeit parameter is
73       non-zero,  the  supplied ber will be freed after its contents have been
74       flushed.
75
76       The ber_printf() routine is used to encode a BER element  in  much  the
77       same  way  that sprintf(3) works.  One important difference, though, is
78       that some state information is kept with the ber parameter so that mul‐
79       tiple  calls can be made to ber_printf() to append things to the end of
80       the BER element.  Ber_printf() writes to ber, a pointer to a BerElement
81       such as returned by ber_alloc_t().  It interprets and formats its argu‐
82       ments according to the format string fmt.  The format string  can  con‐
83       tain the following characters:
84
85              b  Boolean.  An ber_int_t parameter should be supplied.  A bool‐
86                 ean element is output.
87
88              e  Enumeration.  An ber_int_t parameter should be supplied.   An
89                 enumeration element is output.
90
91              i  Integer.   An  ber_int_t  parameter  should  be supplied.  An
92                 integer element is output.
93
94              B  Bitstring.  A char * pointer to the start of the bitstring is
95                 supplied, followed by the number of bits in the bitstring.  A
96                 bitstring element is output.
97
98              n  Null.  No parameter is required.  A null element is output.
99
100              o  Octet string.  A char * is supplied, followed by  the  length
101                 of the string pointed to.  An octet string element is output.
102
103              O  Octet  string.   A  struct  berval  *  is supplied.  An octet
104                 string element is output.
105
106              s  Octet string.  A  null-terminated  string  is  supplied.   An
107                 octet  string  element  is output, not including the trailing
108                 NULL octet.
109
110              t  Tag.  A ber_tag_t specifying the tag to give the next element
111                 is provided.  This works across calls.
112
113              v  Several  octet  strings.  A null-terminated array of char *'s
114                 is supplied.  Note that a construct like '{v}' is required to
115                 get an actual SEQUENCE OF octet strings.
116
117              V  Several  octet  strings.   A  null-terminated array of struct
118                 berval *'s is supplied.  Note that a construct like '{V}'  is
119                 required to get an actual SEQUENCE OF octet strings.
120
121              W  Several  octet  strings.  An array of struct berval's is sup‐
122                 plied.  The array is terminated by a  struct  berval  with  a
123                 NULL bv_val.  Note that a construct like '{W}' is required to
124                 get an actual SEQUENCE OF octet strings.
125
126              {  Begin sequence.  No parameter is required.
127
128              }  End sequence.  No parameter is required.
129
130              [  Begin set.  No parameter is required.
131
132              ]  End set.  No parameter is required.
133
134       The ber_put_int() routine writes the integer element  num  to  the  BER
135       element ber.
136
137       The  ber_put_enum()  routine  writes the enumeration element num to the
138       BER element ber.
139
140       The ber_put_boolean() routine writes the boolean value given by bool to
141       the BER element.
142
143       The  ber_put_bitstring()  routine writes blen bits starting at str as a
144       bitstring value to the given BER element.  Note that blen is the length
145       in bits of the bitstring.
146
147       The  ber_put_ostring()  routine writes len bytes starting at str to the
148       BER element as an octet string.
149
150       The ber_put_string() routine writes the null-terminated  string  (minus
151       the terminating ' ') to the BER element as an octet string.
152
153       The ber_put_null() routine writes a NULL element to the BER element.
154
155       The ber_start_seq() routine is used to start a sequence in the BER ele‐
156       ment.  The ber_start_set() routine works similarly.   The  end  of  the
157       sequence or set is marked by the nearest matching call to ber_put_seq()
158       or ber_put_set(), respectively.
159

EXAMPLES

161       Assuming the following variable declarations, and  that  the  variables
162       have  been  assigned  appropriately,  an lber encoding of the following
163       ASN.1 object:
164
165             AlmostASearchRequest := SEQUENCE {
166                 baseObject      DistinguishedName,
167                 scope           ENUMERATED {
168                     baseObject    (0),
169                     singleLevel   (1),
170                     wholeSubtree  (2)
171                 },
172                 derefAliases    ENUMERATED {
173                     neverDerefaliases   (0),
174                     derefInSearching    (1),
175                     derefFindingBaseObj (2),
176                     alwaysDerefAliases  (3)
177                 },
178                 sizelimit       INTEGER (0 .. 65535),
179                 timelimit       INTEGER (0 .. 65535),
180                 attrsOnly       BOOLEAN,
181                 attributes      SEQUENCE OF AttributeType
182             }
183
184       can be achieved like so:
185
186             int rc;
187             ber_int_t    scope, ali, size, time, attrsonly;
188             char   *dn, **attrs;
189             BerElement *ber;
190
191             /* ... fill in values ... */
192
193             ber = ber_alloc_t( LBER_USE_DER );
194
195             if ( ber == NULL ) {
196                     /* error */
197             }
198
199             rc = ber_printf( ber, "{siiiib{v}}", dn, scope, ali,
200                 size, time, attrsonly, attrs );
201
202             if( rc == -1 ) {
203                     /* error */
204             } else {
205                     /* success */
206             }
207

ERRORS

209       If an error occurs during encoding, generally these routines return -1.
210

NOTES

212       The return values for all  of  these  functions  are  declared  in  the
213       <lber.h> header file.
214

SEE ALSO

216       lber-decode(3), lber-memory(3), lber-sockbuf(3), lber-types(3)
217

ACKNOWLEDGEMENTS

219       OpenLDAP   is   developed   and  maintained  by  The  OpenLDAP  Project
220       (http://www.openldap.org/).  OpenLDAP is  derived  from  University  of
221       Michigan LDAP 3.3 Release.
222
223
224
225OpenLDAP 2.3.34                    2007/2/16                    LBER_ENCODE(3)
Impressum