summaryrefslogtreecommitdiff
path: root/usr/src/man/man3ldap/ber_encode.3ldap
blob: ae7eb4a8250e31ac955beb34fa78e286bb4dce79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'\" te
.\" Copyright (C) 1990, Regents of the University of Michigan.  All Rights Reserved.
.\" Portions Copyright (C) 2008, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH BER_ENCODE 3LDAP "Oct 6, 2008"
.SH NAME
ber_encode, ber_alloc, 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 \- simplified Basic
Encoding Rules library encoding functions
.SH SYNOPSIS
.LP
.nf
cc[ \fIflag\fR... ] \fIfile\fR... -lldap[ \fIlibrary\fR... ]
#include <lber.h>

\fBBerElement *\fR\fBber_alloc\fR();
.fi

.LP
.nf
\fB\fR\fBber_printf\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**fmt\fR[, \fIarg\fR... ]);
.fi

.LP
.nf
\fB\fR\fBber_put_int\fR(\fBBerElement\fR \fI*ber\fR, \fBlong\fR \fInum\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_ostring\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**str\fR, \fBunsigned long\fR \fIlen\fR,
     \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_string\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**str\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_null\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_boolean\fR(\fBBerElement\fR \fI*ber\fR, \fBint\fR \fIbool\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_bitstring\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI*str\fR, \fBint\fR \fIblen\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_start_seq\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_start_set\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_seq\fR(\fBBerElement\fR \fI*ber\fR);
.fi

.LP
.nf
\fB\fR\fBber_put_set\fR(\fBBerElement\fR \fI*ber\fR);
.fi

.SH DESCRIPTION
.sp
.LP
These functions provide a subfunction interface to a simplified implementation
of the Basic Encoding Rules of ASN.1.  The version of BER these functions
support is the one defined for the LDAP protocol.  The encoding rules are the
same as BER, except that  only definite form lengths are used, and bitstrings
and octet strings are always encoded in primitive form.  In addition, these
lightweight BER functions restrict tags and class to fit in a single octet
(this means the actual tag must be less than 31). When a "tag"is specified in
the descriptions below, it refers to the tag, class, and primitive or
constructed bit in the first octet of the encoding.  This man page describes
the encoding functions in the lber library.  See \fBber_decode\fR(3LDAP) for
details on the corresponding decoding functions.
.sp
.LP
Normally, the only functions that need be called by an application are
\fBber_alloc()\fR, to allocate a \fBBER\fR element, and \fBber_printf()\fR to
do the actual encoding.  The other functions are provided for those
applications that need more control than  \fBber_printf()\fR provides.  In
general, these functions return the length of the element encoded, or
\fB\(mi1\fR if an error occurred.
.sp
.LP
The  \fBber_alloc()\fR function is used to allocate a new BER element.
.sp
.LP
The  \fBber_printf()\fR function is used to encode a BER element in much the
same way that \fBsprintf(3C)\fR works.  One important difference, though, is
that some state information is kept with the \fIber\fR parameter so that
multiple calls can be made to  \fBber_printf()\fR to append things to the end
of the BER element.   \fBBer_printf()\fR writes to \fIber\fR, a pointer to a
\fBBerElement\fR such as returned by  \fBber_alloc()\fR. It interprets and
formats its arguments according to the format string \fBfmt\fR. The format
string can contain the following characters:
.sp
.ne 2
.na
\fB\fBb\fR\fR
.ad
.RS 5n
Boolean.  An integer parameter should be supplied.  A boolean element is
output.
.RE

.sp
.ne 2
.na
\fB\fBB\fR\fR
.ad
.RS 5n
Bitstring.  A \fBchar *\fR pointer to the start of the bitstring is supplied,
followed by the number of bits in the bitstring.  A bitstring element is
output.
.RE

.sp
.ne 2
.na
\fB\fBi\fR\fR
.ad
.RS 5n
Integer.  An integer parameter should be supplied.  An integer element is
output.
.RE

.sp
.ne 2
.na
\fB\fBn\fR\fR
.ad
.RS 5n
Null.  No parameter is required.  A null element is output.
.RE

.sp
.ne 2
.na
\fB\fBo\fR\fR
.ad
.RS 5n
Octet string.  A \fBchar *\fR is supplied, followed by the length of the string
pointed to.  An octet string element is output.
.RE

.sp
.ne 2
.na
\fB\fBO\fR\fR
.ad
.RS 5n
Octet string.  A \fBstruct berval *\fR is supplied.  An octet string element is
output.
.RE

.sp
.ne 2
.na
\fB\fBs\fR\fR
.ad
.RS 5n
Octet string.  A null-terminated string is supplied.  An octet string element
is output, not including the trailing null octet.
.RE

.sp
.ne 2
.na
\fB\fBt\fR\fR
.ad
.RS 5n
Tag.  An int specifying the tag to give the next element is provided. This
works across calls.
.RE

.sp
.ne 2
.na
\fB\fBv\fR\fR
.ad
.RS 5n
Several octet strings.  A null-terminated array of \fBchar *\fR is supplied.
Note that a construct like '{v}' is required to get an actual sequence of octet
strings.
.RE

.sp
.ne 2
.na
\fB\fB{\fR\fR
.ad
.RS 5n
Begin sequence.  No parameter is required.
.RE

.sp
.ne 2
.na
\fB\fB}\fR\fR
.ad
.RS 5n
End sequence.  No parameter is required.
.RE

.sp
.ne 2
.na
\fB\fB[\fR\fR
.ad
.RS 5n
Begin set.  No parameter is required.
.RE

.sp
.ne 2
.na
\fB\fB]\fR\fR
.ad
.RS 5n
End set.  No parameter is required.
.RE

.sp
.LP
The  \fBber_put_int()\fR function writes the integer element \fInum\fR to the
BER element \fIber\fR.
.sp
.LP
The  \fBber_put_boolean()\fR function writes the boolean value given by
\fIbool\fR to the BER element.
.sp
.LP
The  \fBber_put_bitstring()\fR function writes \fIblen\fR bits starting at
\fIstr\fR as a bitstring value to the given BER element.  Note that \fIblen\fR
is the length in \fIbits\fR of the bitstring.
.sp
.LP
The  \fBber_put_ostring()\fR function writes \fIlen\fR bytes starting at
\fIstr\fR to the BER element as an octet string.
.sp
.LP
The  \fBber_put_string()\fR function writes the null-terminated string (minus
the terminating '') to the BER element as an octet string.
.sp
.LP
The  \fBber_put_null()\fR function writes a  \fINULL\fR element to the BER
element.
.sp
.LP
The  \fBber_start_seq()\fR function is used to start a sequence in the BER
element.  The  \fBber_start_set()\fR function works similarly. The end of the
sequence or set is marked by the nearest matching call to  \fBber_put_seq()\fR
or  \fBber_put_set()\fR, respectively.
.sp
.LP
The  \fBber_first_element()\fR function is used to return the tag and length of
the first element in a set or sequence.  It also returns in \fIcookie\fR a
magic cookie parameter that should be passed to subsequent calls to
\fBber_next_element()\fR, which returns similar information.
.SH EXAMPLES
.LP
\fBExample 1 \fRAssuming the following variable declarations, and that the
variables have been assigned appropriately, an BER encoding of the following
ASN.1 object:
.sp
.in +2
.nf
      AlmostASearchRequest := SEQUENCE {
          baseObject      DistinguishedName,
          scope           ENUMERATED {
              baseObject    (0),
              singleLevel   (1),
              wholeSubtree  (2)
          },
          derefAliases    ENUMERATED {
              neverDerefaliases   (0),
              derefInSearching    (1),
              derefFindingBaseObj (2),
              alwaysDerefAliases  (3N)
          },
          sizelimit       INTEGER (0 .. 65535),
          timelimit       INTEGER (0 .. 65535),
          attrsOnly       BOOLEAN,
          attributes      SEQUENCE OF AttributeType
      }
.fi
.in -2

.sp
.LP
can be achieved like so:
.sp
.in +2
.nf
      int    scope, ali, size, time, attrsonly;
      char   *dn, **attrs;

      /* ... fill in values ... */
      if ( (ber = ber_alloc(\|)) == NULLBER )
      /* error */

      if ( ber_printf( ber, "{siiiib{v}}", dn, scope, ali,
          size, time, attrsonly, attrs ) == -1 )
              /* error */
      else
              /* success */
.fi
.in -2

.SH RETURN VALUES
.sp
.LP
If an error occurs during encoding, \fBber_alloc()\fR returns \fINULL\fR; other
functions generally return \fB\(mi1\fR\&.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for a description of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
.TE

.SH SEE ALSO
.sp
.LP
\fBber_decode\fR(3LDAP), \fBattributes\fR(5)
.sp
.LP
Yeong, W., Howes, T., and Hardcastle-Kille, S., "Lightweight Directory Access
Protocol", OSI-DS-26, April 1992.
.sp
.LP
Information Processing - Open Systems Interconnection - Model and Notation -
Service Definition - Specification of Basic Encoding Rules for Abstract Syntax
Notation One, International Organization for Standardization, International
Standard 8825.
.SH NOTES
.sp
.LP
The return values for all of these functions are declared in \fB<lber.h>\fR.