6198 Let's EOL cachefs
[illumos-gate.git] / usr / src / man / man3ldap / ber_encode.3ldap
blobaaa094e20cdf2f6fbeceac8507fb1ad5dc12a2f1
1 '\" te
2 .\" Copyright (C) 1990, Regents of the University of Michigan.  All Rights Reserved.
3 .\" Portions Copyright (C) 2008, Sun Microsystems, Inc. All Rights Reserved.
4 .\" 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.
5 .\" 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.
6 .\" 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]
7 .TH BER_ENCODE 3LDAP "Oct 6, 2008"
8 .SH NAME
9 ber_encode, ber_alloc, ber_printf, ber_put_int, ber_put_ostring,
10 ber_put_string, ber_put_null, ber_put_boolean, ber_put_bitstring,
11 ber_start_seq, ber_start_set, ber_put_seq, ber_put_set \- simplified Basic
12 Encoding Rules library encoding functions
13 .SH SYNOPSIS
14 .LP
15 .nf
16 cc[ \fIflag\fR... ] \fIfile\fR... -lldap[ \fIlibrary\fR... ]
17 #include <lber.h>
19 \fBBerElement *\fR\fBber_alloc\fR();
20 .fi
22 .LP
23 .nf
24 \fB\fR\fBber_printf\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**fmt\fR[, \fIarg\fR... ]);
25 .fi
27 .LP
28 .nf
29 \fB\fR\fBber_put_int\fR(\fBBerElement\fR \fI*ber\fR, \fBlong\fR \fInum\fR, \fBchar\fR \fItag\fR);
30 .fi
32 .LP
33 .nf
34 \fB\fR\fBber_put_ostring\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**str\fR, \fBunsigned long\fR \fIlen\fR,
35      \fBchar\fR \fItag\fR);
36 .fi
38 .LP
39 .nf
40 \fB\fR\fBber_put_string\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI**str\fR, \fBchar\fR \fItag\fR);
41 .fi
43 .LP
44 .nf
45 \fB\fR\fBber_put_null\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
46 .fi
48 .LP
49 .nf
50 \fB\fR\fBber_put_boolean\fR(\fBBerElement\fR \fI*ber\fR, \fBint\fR \fIbool\fR, \fBchar\fR \fItag\fR);
51 .fi
53 .LP
54 .nf
55 \fB\fR\fBber_put_bitstring\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fI*str\fR, \fBint\fR \fIblen\fR, \fBchar\fR \fItag\fR);
56 .fi
58 .LP
59 .nf
60 \fB\fR\fBber_start_seq\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
61 .fi
63 .LP
64 .nf
65 \fB\fR\fBber_start_set\fR(\fBBerElement\fR \fI*ber\fR, \fBchar\fR \fItag\fR);
66 .fi
68 .LP
69 .nf
70 \fB\fR\fBber_put_seq\fR(\fBBerElement\fR \fI*ber\fR);
71 .fi
73 .LP
74 .nf
75 \fB\fR\fBber_put_set\fR(\fBBerElement\fR \fI*ber\fR);
76 .fi
78 .SH DESCRIPTION
79 .sp
80 .LP
81 These functions provide a subfunction interface to a simplified implementation
82 of the Basic Encoding Rules of ASN.1.  The version of BER these functions
83 support is the one defined for the LDAP protocol.  The encoding rules are the
84 same as BER, except that  only definite form lengths are used, and bitstrings
85 and octet strings are always encoded in primitive form.  In addition, these
86 lightweight BER functions restrict tags and class to fit in a single octet
87 (this means the actual tag must be less than 31). When a "tag"is specified in
88 the descriptions below, it refers to the tag, class, and primitive or
89 constructed bit in the first octet of the encoding.  This man page describes
90 the encoding functions in the lber library.  See \fBber_decode\fR(3LDAP) for
91 details on the corresponding decoding functions.
92 .sp
93 .LP
94 Normally, the only functions that need be called by an application are
95 \fBber_alloc()\fR, to allocate a \fBBER\fR element, and \fBber_printf()\fR to
96 do the actual encoding.  The other functions are provided for those
97 applications that need more control than  \fBber_printf()\fR provides.  In
98 general, these functions return the length of the element encoded, or
99 \fB\(mi1\fR if an error occurred.
102 The  \fBber_alloc()\fR function is used to allocate a new BER element.
105 The  \fBber_printf()\fR function is used to encode a BER element in much the
106 same way that  \fBsprintf(3S)\fR works.  One important difference, though, is
107 that some state information is kept with the \fIber\fR parameter so that
108 multiple calls can be made to  \fBber_printf()\fR to append things to the end
109 of the BER element.   \fBBer_printf()\fR writes to \fIber\fR, a pointer to a
110 \fBBerElement\fR such as returned by  \fBber_alloc()\fR. It interprets and
111 formats its arguments according to the format string \fBfmt\fR. The format
112 string can contain the following characters:
114 .ne 2
116 \fB\fBb\fR\fR
118 .RS 5n
119 Boolean.  An integer parameter should be supplied.  A boolean element is
120 output.
124 .ne 2
126 \fB\fBB\fR\fR
128 .RS 5n
129 Bitstring.  A \fBchar *\fR pointer to the start of the bitstring is supplied,
130 followed by the number of bits in the bitstring.  A bitstring element is
131 output.
135 .ne 2
137 \fB\fBi\fR\fR
139 .RS 5n
140 Integer.  An integer parameter should be supplied.  An integer element is
141 output.
145 .ne 2
147 \fB\fBn\fR\fR
149 .RS 5n
150 Null.  No parameter is required.  A null element is output.
154 .ne 2
156 \fB\fBo\fR\fR
158 .RS 5n
159 Octet string.  A \fBchar *\fR is supplied, followed by the length of the string
160 pointed to.  An octet string element is output.
164 .ne 2
166 \fB\fBO\fR\fR
168 .RS 5n
169 Octet string.  A \fBstruct berval *\fR is supplied.  An octet string element is
170 output.
174 .ne 2
176 \fB\fBs\fR\fR
178 .RS 5n
179 Octet string.  A null-terminated string is supplied.  An octet string element
180 is output, not including the trailing null octet.
184 .ne 2
186 \fB\fBt\fR\fR
188 .RS 5n
189 Tag.  An int specifying the tag to give the next element is provided. This
190 works across calls.
194 .ne 2
196 \fB\fBv\fR\fR
198 .RS 5n
199 Several octet strings.  A null-terminated array of \fBchar *\fR is supplied.
200 Note that a construct like '{v}' is required to get an actual sequence of octet
201 strings.
205 .ne 2
207 \fB\fB{\fR\fR
209 .RS 5n
210 Begin sequence.  No parameter is required.
214 .ne 2
216 \fB\fB}\fR\fR
218 .RS 5n
219 End sequence.  No parameter is required.
223 .ne 2
225 \fB\fB[\fR\fR
227 .RS 5n
228 Begin set.  No parameter is required.
232 .ne 2
234 \fB\fB]\fR\fR
236 .RS 5n
237 End set.  No parameter is required.
242 The  \fBber_put_int()\fR function writes the integer element \fInum\fR to the
243 BER element \fIber\fR.
246 The  \fBber_put_boolean()\fR function writes the boolean value given by
247 \fIbool\fR to the BER element.
250 The  \fBber_put_bitstring()\fR function writes \fIblen\fR bits starting at
251 \fIstr\fR as a bitstring value to the given BER element.  Note that \fIblen\fR
252 is the length in \fIbits\fR of the bitstring.
255 The  \fBber_put_ostring()\fR function writes \fIlen\fR bytes starting at
256 \fIstr\fR to the BER element as an octet string.
259 The  \fBber_put_string()\fR function writes the null-terminated string (minus
260 the terminating '') to the BER element as an octet string.
263 The  \fBber_put_null()\fR function writes a  \fINULL\fR element to the BER
264 element.
267 The  \fBber_start_seq()\fR function is used to start a sequence in the BER
268 element.  The  \fBber_start_set()\fR function works similarly. The end of the
269 sequence or set is marked by the nearest matching call to  \fBber_put_seq()\fR
270 or  \fBber_put_set()\fR, respectively.
273 The  \fBber_first_element()\fR function is used to return the tag and length of
274 the first element in a set or sequence.  It also returns in \fIcookie\fR a
275 magic cookie parameter that should be passed to subsequent calls to
276 \fBber_next_element()\fR, which returns similar information.
277 .SH EXAMPLES
279 \fBExample 1 \fRAssuming the following variable declarations, and that the
280 variables have been assigned appropriately, an BER encoding of the following
281 ASN.1 object:
283 .in +2
285       AlmostASearchRequest := SEQUENCE {
286           baseObject      DistinguishedName,
287           scope           ENUMERATED {
288               baseObject    (0),
289               singleLevel   (1),
290               wholeSubtree  (2)
291           },
292           derefAliases    ENUMERATED {
293               neverDerefaliases   (0),
294               derefInSearching    (1),
295               derefFindingBaseObj (2),
296               alwaysDerefAliases  (3N)
297           },
298           sizelimit       INTEGER (0 .. 65535),
299           timelimit       INTEGER (0 .. 65535),
300           attrsOnly       BOOLEAN,
301           attributes      SEQUENCE OF AttributeType
302       }
304 .in -2
308 can be achieved like so:
310 .in +2
312       int    scope, ali, size, time, attrsonly;
313       char   *dn, **attrs;
315       /* ... fill in values ... */
316       if ( (ber = ber_alloc(\|)) == NULLBER )
317       /* error */
319       if ( ber_printf( ber, "{siiiib{v}}", dn, scope, ali,
320           size, time, attrsonly, attrs ) == -1 )
321               /* error */
322       else
323               /* success */
325 .in -2
327 .SH RETURN VALUES
330 If an error occurs during encoding, \fBber_alloc()\fR returns \fINULL\fR; other
331 functions generally return \fB\(mi1\fR\&.
332 .SH ATTRIBUTES
335 See \fBattributes\fR(5) for a description of the following attributes:
340 box;
341 c | c
342 l | l .
343 ATTRIBUTE TYPE  ATTRIBUTE VALUE
345 Interface Stability     Committed
348 .SH SEE ALSO
351 \fBber_decode\fR(3LDAP), \fBattributes\fR(5)
354 Yeong, W., Howes, T., and Hardcastle-Kille, S., "Lightweight Directory Access
355 Protocol", OSI-DS-26, April 1992.
358 Information Processing - Open Systems Interconnection - Model and Notation -
359 Service Definition - Specification of Basic Encoding Rules for Abstract Syntax
360 Notation One, International Organization for Standardization, International
361 Standard 8825.
362 .SH NOTES
365 The return values for all of these functions are declared in \fB<lber.h>\fR.