1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate /* encode.c - ber output encoding routines */
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan.
11*0Sstevel@tonic-gate * All rights reserved.
12*0Sstevel@tonic-gate *
13*0Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
14*0Sstevel@tonic-gate * provided that this notice is preserved and that due credit is given
15*0Sstevel@tonic-gate * to the University of Michigan at Ann Arbor. The name of the University
16*0Sstevel@tonic-gate * may not be used to endorse or promote products derived from this
17*0Sstevel@tonic-gate * software without specific prior written permission. This software
18*0Sstevel@tonic-gate * is provided ``as is'' without express or implied warranty.
19*0Sstevel@tonic-gate */
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #ifdef MACOS
23*0Sstevel@tonic-gate #include <stdlib.h>
24*0Sstevel@tonic-gate #include <stdarg.h>
25*0Sstevel@tonic-gate #include "macos.h"
26*0Sstevel@tonic-gate #else /* MACOS */
27*0Sstevel@tonic-gate #if defined(NeXT) || defined(VMS)
28*0Sstevel@tonic-gate #include <stdlib.h>
29*0Sstevel@tonic-gate #else /* next || vms */
30*0Sstevel@tonic-gate #include <malloc.h>
31*0Sstevel@tonic-gate #endif /* next || vms */
32*0Sstevel@tonic-gate #if defined( BC31 ) || defined( _WIN32 ) || defined(__sun)
33*0Sstevel@tonic-gate #include <stdarg.h>
34*0Sstevel@tonic-gate #else /* BC31 || _WIN32 */
35*0Sstevel@tonic-gate #include <varargs.h>
36*0Sstevel@tonic-gate #endif /* BC31 || _WIN32 */
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/socket.h>
39*0Sstevel@tonic-gate #include <netinet/in.h>
40*0Sstevel@tonic-gate #ifdef PCNFS
41*0Sstevel@tonic-gate #include <tklib.h>
42*0Sstevel@tonic-gate #endif /* PCNFS */
43*0Sstevel@tonic-gate #endif /* MACOS */
44*0Sstevel@tonic-gate #ifndef VMS
45*0Sstevel@tonic-gate #include <memory.h>
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate #include <string.h>
48*0Sstevel@tonic-gate #include "lber.h"
49*0Sstevel@tonic-gate #include "ldap.h"
50*0Sstevel@tonic-gate #include "ldap-private.h"
51*0Sstevel@tonic-gate #include "ldap-int.h"
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
54*0Sstevel@tonic-gate #include "msdos.h"
55*0Sstevel@tonic-gate #endif /* DOS */
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate #ifdef NEEDPROTOS
58*0Sstevel@tonic-gate static int ber_put_len( BerElement *ber, unsigned int len, int nosos );
59*0Sstevel@tonic-gate static int ber_start_seqorset( BerElement *ber, unsigned int tag );
60*0Sstevel@tonic-gate static int ber_put_seqorset( BerElement *ber );
61*0Sstevel@tonic-gate static int ber_put_int_or_enum( BerElement *ber, int num, unsigned int tag );
62*0Sstevel@tonic-gate #endif /* NEEDPROTOS */
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate extern int ber_realloc(BerElement *ber, unsigned int len);
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate static int
ber_calc_taglen(unsigned int tag)67*0Sstevel@tonic-gate ber_calc_taglen( unsigned int tag )
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate int i;
70*0Sstevel@tonic-gate int mask;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /* find the first non-all-zero byte in the tag */
73*0Sstevel@tonic-gate for ( i = sizeof(int) - 1; i > 0; i-- ) {
74*0Sstevel@tonic-gate mask = (0xffL << (i * 8));
75*0Sstevel@tonic-gate /* not all zero */
76*0Sstevel@tonic-gate if ( tag & mask )
77*0Sstevel@tonic-gate break;
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate return( i + 1 );
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate static int
ber_put_tag(BerElement * ber,unsigned int tag,int nosos)84*0Sstevel@tonic-gate ber_put_tag( BerElement *ber, unsigned int tag, int nosos )
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate int taglen;
87*0Sstevel@tonic-gate unsigned int ntag;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate taglen = ber_calc_taglen( tag );
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate ntag = LBER_HTONL( tag );
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate return( ber_write( ber, ((char *) &ntag) + sizeof(int) - taglen,
94*0Sstevel@tonic-gate taglen, nosos ) );
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate static int
ber_calc_lenlen(unsigned int len)98*0Sstevel@tonic-gate ber_calc_lenlen( unsigned int len )
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate * short len if it's less than 128 - one byte giving the len,
102*0Sstevel@tonic-gate * with bit 8 0.
103*0Sstevel@tonic-gate */
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate if ( len <= 0x7F )
106*0Sstevel@tonic-gate return( 1 );
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate * int len otherwise - one byte with bit 8 set, giving the
110*0Sstevel@tonic-gate * length of the length, followed by the length itself.
111*0Sstevel@tonic-gate */
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate if ( len <= 0xFF )
114*0Sstevel@tonic-gate return( 2 );
115*0Sstevel@tonic-gate if ( len <= 0xFFFF )
116*0Sstevel@tonic-gate return( 3 );
117*0Sstevel@tonic-gate if ( len <= 0xFFFFFF )
118*0Sstevel@tonic-gate return( 4 );
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate return( 5 );
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate static int
ber_put_len(BerElement * ber,unsigned int len,int nosos)124*0Sstevel@tonic-gate ber_put_len( BerElement *ber, unsigned int len, int nosos )
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate int i;
127*0Sstevel@tonic-gate char lenlen;
128*0Sstevel@tonic-gate int mask;
129*0Sstevel@tonic-gate unsigned int netlen;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate * short len if it's less than 128 - one byte giving the len,
133*0Sstevel@tonic-gate * with bit 8 0.
134*0Sstevel@tonic-gate */
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate if ( len <= 127 ) {
137*0Sstevel@tonic-gate netlen = LBER_HTONL( len );
138*0Sstevel@tonic-gate return( ber_write( ber, (char *) &netlen + sizeof(int) - 1,
139*0Sstevel@tonic-gate 1, nosos ) );
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate * int len otherwise - one byte with bit 8 set, giving the
144*0Sstevel@tonic-gate * length of the length, followed by the length itself.
145*0Sstevel@tonic-gate */
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate /* find the first non-all-zero byte */
148*0Sstevel@tonic-gate for ( i = sizeof(int) - 1; i > 0; i-- ) {
149*0Sstevel@tonic-gate mask = (0xff << (i * 8));
150*0Sstevel@tonic-gate /* not all zero */
151*0Sstevel@tonic-gate if ( len & mask )
152*0Sstevel@tonic-gate break;
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate lenlen = ++i;
155*0Sstevel@tonic-gate if ( lenlen > 4 )
156*0Sstevel@tonic-gate return( -1 );
157*0Sstevel@tonic-gate lenlen |= 0x80;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /* write the length of the length */
160*0Sstevel@tonic-gate if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
161*0Sstevel@tonic-gate return( -1 );
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate /* write the length itself */
164*0Sstevel@tonic-gate netlen = LBER_HTONL( len );
165*0Sstevel@tonic-gate if ( ber_write( ber, (char *) &netlen + (sizeof(int) - i), i, nosos )
166*0Sstevel@tonic-gate != i )
167*0Sstevel@tonic-gate return( -1 );
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate return( i + 1 );
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate static int
ber_put_int_or_enum(BerElement * ber,int num,unsigned int tag)173*0Sstevel@tonic-gate ber_put_int_or_enum( BerElement *ber, int num, unsigned int tag )
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate int i, sign, taglen;
176*0Sstevel@tonic-gate int len, lenlen;
177*0Sstevel@tonic-gate int netnum, mask;
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate sign = (num < 0);
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate /*
182*0Sstevel@tonic-gate * high bit is set - look for first non-all-one byte
183*0Sstevel@tonic-gate * high bit is clear - look for first non-all-zero byte
184*0Sstevel@tonic-gate */
185*0Sstevel@tonic-gate for ( i = sizeof(int) - 1; i > 0; i-- ) {
186*0Sstevel@tonic-gate mask = (0xff << (i * 8));
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate if ( sign ) {
189*0Sstevel@tonic-gate /* not all ones */
190*0Sstevel@tonic-gate if ( (num & mask) != mask )
191*0Sstevel@tonic-gate break;
192*0Sstevel@tonic-gate } else {
193*0Sstevel@tonic-gate /* not all zero */
194*0Sstevel@tonic-gate if ( num & mask )
195*0Sstevel@tonic-gate break;
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate /*
200*0Sstevel@tonic-gate * we now have the "leading byte". if the high bit on this
201*0Sstevel@tonic-gate * byte matches the sign bit, we need to "back up" a byte.
202*0Sstevel@tonic-gate */
203*0Sstevel@tonic-gate mask = (num & (0x80 << (i * 8)));
204*0Sstevel@tonic-gate if ( (mask && !sign) || (sign && !mask) )
205*0Sstevel@tonic-gate i++;
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate len = i + 1;
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
210*0Sstevel@tonic-gate return( -1 );
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
213*0Sstevel@tonic-gate return( -1 );
214*0Sstevel@tonic-gate i++;
215*0Sstevel@tonic-gate netnum = LBER_HTONL( num );
216*0Sstevel@tonic-gate if ( ber_write( ber, (char *) &netnum + (sizeof(int) - i), i, 0 )
217*0Sstevel@tonic-gate != i )
218*0Sstevel@tonic-gate return( -1 );
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate /* length of tag + length + contents */
221*0Sstevel@tonic-gate return( taglen + lenlen + i );
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate int
ber_put_enum(BerElement * ber,int num,unsigned int tag)225*0Sstevel@tonic-gate ber_put_enum( BerElement *ber, int num, unsigned int tag )
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
228*0Sstevel@tonic-gate tag = LBER_ENUMERATED;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate return( ber_put_int_or_enum( ber, num, tag ) );
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate int
ber_put_int(BerElement * ber,int num,unsigned int tag)234*0Sstevel@tonic-gate ber_put_int( BerElement *ber, int num, unsigned int tag )
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
237*0Sstevel@tonic-gate tag = LBER_INTEGER;
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate return( ber_put_int_or_enum( ber, num, tag ) );
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate int
ber_put_ostring(BerElement * ber,char * str,unsigned int len,unsigned int tag)243*0Sstevel@tonic-gate ber_put_ostring( BerElement *ber, char *str, unsigned int len,
244*0Sstevel@tonic-gate unsigned int tag )
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate int taglen, lenlen, rc;
247*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
248*0Sstevel@tonic-gate int free_str;
249*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
252*0Sstevel@tonic-gate tag = LBER_OCTETSTRING;
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
255*0Sstevel@tonic-gate return( -1 );
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
258*0Sstevel@tonic-gate if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
259*0Sstevel@tonic-gate ber->ber_encode_translate_proc != NULL ) {
260*0Sstevel@tonic-gate if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
261*0Sstevel@tonic-gate != 0 ) {
262*0Sstevel@tonic-gate return( -1 );
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate free_str = 1;
265*0Sstevel@tonic-gate } else {
266*0Sstevel@tonic-gate free_str = 0;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
271*0Sstevel@tonic-gate ber_write( ber, str, len, 0 ) != len ) {
272*0Sstevel@tonic-gate rc = -1;
273*0Sstevel@tonic-gate } else {
274*0Sstevel@tonic-gate /* return length of tag + length + contents */
275*0Sstevel@tonic-gate rc = taglen + lenlen + len;
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
279*0Sstevel@tonic-gate if ( free_str ) {
280*0Sstevel@tonic-gate free( str );
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate return( rc );
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate int
ber_put_string(BerElement * ber,char * str,unsigned int tag)288*0Sstevel@tonic-gate ber_put_string( BerElement *ber, char *str, unsigned int tag )
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate return( ber_put_ostring( ber, str, (unsigned int)strlen( str ), tag ));
291*0Sstevel@tonic-gate }
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate int
ber_put_bitstring(BerElement * ber,char * str,unsigned int blen,unsigned int tag)294*0Sstevel@tonic-gate ber_put_bitstring( BerElement *ber, char *str,
295*0Sstevel@tonic-gate unsigned int blen /* in bits */, unsigned int tag )
296*0Sstevel@tonic-gate {
297*0Sstevel@tonic-gate int taglen, lenlen, len;
298*0Sstevel@tonic-gate unsigned char unusedbits;
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
301*0Sstevel@tonic-gate tag = LBER_BITSTRING;
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
304*0Sstevel@tonic-gate return( -1 );
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate len = ( blen + 7 ) / 8;
307*0Sstevel@tonic-gate unusedbits = len * 8 - blen;
308*0Sstevel@tonic-gate if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
309*0Sstevel@tonic-gate return( -1 );
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
312*0Sstevel@tonic-gate return( -1 );
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate if ( ber_write( ber, str, len, 0 ) != len )
315*0Sstevel@tonic-gate return( -1 );
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate /* return length of tag + length + unused bit count + contents */
318*0Sstevel@tonic-gate return( taglen + 1 + lenlen + len );
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate int
ber_put_null(BerElement * ber,unsigned int tag)322*0Sstevel@tonic-gate ber_put_null( BerElement *ber, unsigned int tag )
323*0Sstevel@tonic-gate {
324*0Sstevel@tonic-gate int taglen;
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
327*0Sstevel@tonic-gate tag = LBER_NULL;
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
330*0Sstevel@tonic-gate return( -1 );
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate if ( ber_put_len( ber, 0, 0 ) != 1 )
333*0Sstevel@tonic-gate return( -1 );
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate return( taglen + 1 );
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate int
ber_put_boolean(BerElement * ber,int boolval,unsigned int tag)339*0Sstevel@tonic-gate ber_put_boolean( BerElement *ber, int boolval, unsigned int tag )
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate int taglen;
342*0Sstevel@tonic-gate unsigned char trueval = 0xff;
343*0Sstevel@tonic-gate unsigned char falseval = 0x00;
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
346*0Sstevel@tonic-gate tag = LBER_BOOLEAN;
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
349*0Sstevel@tonic-gate return( -1 );
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate if ( ber_put_len( ber, 1, 0 ) != 1 )
352*0Sstevel@tonic-gate return( -1 );
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gate if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
355*0Sstevel@tonic-gate != 1 )
356*0Sstevel@tonic-gate return( -1 );
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate return( taglen + 2 );
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate #define FOUR_BYTE_LEN 5
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate static int
ber_start_seqorset(BerElement * ber,unsigned int tag)364*0Sstevel@tonic-gate ber_start_seqorset( BerElement *ber, unsigned int tag )
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate Seqorset *new;
367*0Sstevel@tonic-gate
368*0Sstevel@tonic-gate if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
369*0Sstevel@tonic-gate == NULLSEQORSET )
370*0Sstevel@tonic-gate return( -1 );
371*0Sstevel@tonic-gate new->sos_ber = ber;
372*0Sstevel@tonic-gate if ( ber->ber_sos == NULLSEQORSET )
373*0Sstevel@tonic-gate new->sos_first = ber->ber_ptr;
374*0Sstevel@tonic-gate else
375*0Sstevel@tonic-gate new->sos_first = ber->ber_sos->sos_ptr;
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate /* Set aside room for a 4 byte length field */
378*0Sstevel@tonic-gate new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
379*0Sstevel@tonic-gate new->sos_tag = tag;
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate new->sos_next = ber->ber_sos;
382*0Sstevel@tonic-gate ber->ber_sos = new;
383*0Sstevel@tonic-gate if (ber->ber_sos->sos_ptr > ber->ber_end)
384*0Sstevel@tonic-gate ber_realloc(ber, ber->ber_sos->sos_ptr - ber->ber_end);
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate return( 0 );
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate int
ber_start_seq(BerElement * ber,unsigned int tag)390*0Sstevel@tonic-gate ber_start_seq( BerElement *ber, unsigned int tag )
391*0Sstevel@tonic-gate {
392*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
393*0Sstevel@tonic-gate tag = LBER_SEQUENCE;
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate return( ber_start_seqorset( ber, tag ) );
396*0Sstevel@tonic-gate }
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gate int
ber_start_set(BerElement * ber,unsigned int tag)399*0Sstevel@tonic-gate ber_start_set( BerElement *ber, unsigned int tag )
400*0Sstevel@tonic-gate {
401*0Sstevel@tonic-gate if ( tag == LBER_DEFAULT )
402*0Sstevel@tonic-gate tag = LBER_SET;
403*0Sstevel@tonic-gate
404*0Sstevel@tonic-gate return( ber_start_seqorset( ber, tag ) );
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gate static int
ber_put_seqorset(BerElement * ber)408*0Sstevel@tonic-gate ber_put_seqorset( BerElement *ber )
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate unsigned int len, netlen;
411*0Sstevel@tonic-gate int taglen, lenlen;
412*0Sstevel@tonic-gate unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
413*0Sstevel@tonic-gate Seqorset *next;
414*0Sstevel@tonic-gate Seqorset **sos = &ber->ber_sos;
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate /*
417*0Sstevel@tonic-gate * If this is the toplevel sequence or set, we need to actually
418*0Sstevel@tonic-gate * write the stuff out. Otherwise, it's already been put in
419*0Sstevel@tonic-gate * the appropriate buffer and will be written when the toplevel
420*0Sstevel@tonic-gate * one is written. In this case all we need to do is update the
421*0Sstevel@tonic-gate * length and tag.
422*0Sstevel@tonic-gate */
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate len = (*sos)->sos_clen;
425*0Sstevel@tonic-gate netlen = LBER_HTONL( len );
426*0Sstevel@tonic-gate /* CONSTCOND */
427*0Sstevel@tonic-gate if ( sizeof(int) > 4 && len > 0xFFFFFFFF )
428*0Sstevel@tonic-gate return( -1 );
429*0Sstevel@tonic-gate
430*0Sstevel@tonic-gate if ( ber->ber_options & LBER_USE_DER ) {
431*0Sstevel@tonic-gate lenlen = ber_calc_lenlen( len );
432*0Sstevel@tonic-gate } else {
433*0Sstevel@tonic-gate lenlen = FOUR_BYTE_LEN;
434*0Sstevel@tonic-gate }
435*0Sstevel@tonic-gate
436*0Sstevel@tonic-gate if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
437*0Sstevel@tonic-gate /* write the tag */
438*0Sstevel@tonic-gate if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
439*0Sstevel@tonic-gate return( -1 );
440*0Sstevel@tonic-gate
441*0Sstevel@tonic-gate if ( ber->ber_options & LBER_USE_DER ) {
442*0Sstevel@tonic-gate /* Write the length in the minimum # of octets */
443*0Sstevel@tonic-gate if ( ber_put_len( ber, len, 1 ) == -1 )
444*0Sstevel@tonic-gate return( -1 );
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gate if (lenlen != FOUR_BYTE_LEN) {
447*0Sstevel@tonic-gate /*
448*0Sstevel@tonic-gate * We set aside FOUR_BYTE_LEN bytes for
449*0Sstevel@tonic-gate * the length field. Move the data if
450*0Sstevel@tonic-gate * we don't actually need that much
451*0Sstevel@tonic-gate */
452*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first + taglen +
453*0Sstevel@tonic-gate lenlen, (*sos)->sos_first + taglen +
454*0Sstevel@tonic-gate FOUR_BYTE_LEN, len );
455*0Sstevel@tonic-gate }
456*0Sstevel@tonic-gate } else {
457*0Sstevel@tonic-gate /* Fill FOUR_BYTE_LEN bytes for length field */
458*0Sstevel@tonic-gate /* one byte of length length */
459*0Sstevel@tonic-gate if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 )
460*0Sstevel@tonic-gate return( -1 );
461*0Sstevel@tonic-gate
462*0Sstevel@tonic-gate /* the length itself */
463*0Sstevel@tonic-gate if ( ber_write( ber, (char *) &netlen + sizeof(int)
464*0Sstevel@tonic-gate - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
465*0Sstevel@tonic-gate != FOUR_BYTE_LEN - 1 )
466*0Sstevel@tonic-gate return( -1 );
467*0Sstevel@tonic-gate }
468*0Sstevel@tonic-gate /* The ber_ptr is at the set/seq start - move it to the end */
469*0Sstevel@tonic-gate (*sos)->sos_ber->ber_ptr += len;
470*0Sstevel@tonic-gate } else {
471*0Sstevel@tonic-gate unsigned int ntag;
472*0Sstevel@tonic-gate
473*0Sstevel@tonic-gate /* the tag */
474*0Sstevel@tonic-gate taglen = ber_calc_taglen( (*sos)->sos_tag );
475*0Sstevel@tonic-gate ntag = LBER_HTONL( (*sos)->sos_tag );
476*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
477*0Sstevel@tonic-gate sizeof(int) - taglen, taglen );
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate if ( ber->ber_options & LBER_USE_DER ) {
480*0Sstevel@tonic-gate ltag = (lenlen == 1) ? len : 0x80 + (lenlen - 1);
481*0Sstevel@tonic-gate }
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gate /* one byte of length length */
484*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first + 1, <ag, 1 );
485*0Sstevel@tonic-gate
486*0Sstevel@tonic-gate if ( ber->ber_options & LBER_USE_DER ) {
487*0Sstevel@tonic-gate if (lenlen > 1) {
488*0Sstevel@tonic-gate /* Write the length itself */
489*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first + 2,
490*0Sstevel@tonic-gate (char *)&netlen + sizeof(unsigned int) -
491*0Sstevel@tonic-gate (lenlen - 1),
492*0Sstevel@tonic-gate lenlen - 1 );
493*0Sstevel@tonic-gate }
494*0Sstevel@tonic-gate if (lenlen != FOUR_BYTE_LEN) {
495*0Sstevel@tonic-gate /*
496*0Sstevel@tonic-gate * We set aside FOUR_BYTE_LEN bytes for
497*0Sstevel@tonic-gate * the length field. Move the data if
498*0Sstevel@tonic-gate * we don't actually need that much
499*0Sstevel@tonic-gate */
500*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first + taglen +
501*0Sstevel@tonic-gate lenlen, (*sos)->sos_first + taglen +
502*0Sstevel@tonic-gate FOUR_BYTE_LEN, len );
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate } else {
505*0Sstevel@tonic-gate /* the length itself */
506*0Sstevel@tonic-gate (void) SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
507*0Sstevel@tonic-gate (char *) &netlen + sizeof(int) -
508*0Sstevel@tonic-gate (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate
511*0Sstevel@tonic-gate next->sos_clen += (taglen + lenlen + len);
512*0Sstevel@tonic-gate next->sos_ptr += (taglen + lenlen + len);
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate
515*0Sstevel@tonic-gate /* we're done with this seqorset, so free it up */
516*0Sstevel@tonic-gate free( (char *) (*sos) );
517*0Sstevel@tonic-gate *sos = next;
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate return( taglen + lenlen + len );
520*0Sstevel@tonic-gate }
521*0Sstevel@tonic-gate
522*0Sstevel@tonic-gate int
ber_put_seq(BerElement * ber)523*0Sstevel@tonic-gate ber_put_seq( BerElement *ber )
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate return( ber_put_seqorset( ber ) );
526*0Sstevel@tonic-gate }
527*0Sstevel@tonic-gate
528*0Sstevel@tonic-gate int
ber_put_set(BerElement * ber)529*0Sstevel@tonic-gate ber_put_set( BerElement *ber )
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate return( ber_put_seqorset( ber ) );
532*0Sstevel@tonic-gate }
533*0Sstevel@tonic-gate
534*0Sstevel@tonic-gate /* VARARGS */
535*0Sstevel@tonic-gate int
ber_printf(BerElement * ber,char * fmt,...)536*0Sstevel@tonic-gate ber_printf(
537*0Sstevel@tonic-gate #if defined(MACOS) || defined(_WIN32) || defined(BC31) || defined(__sun)
538*0Sstevel@tonic-gate BerElement *ber, char *fmt, ... )
539*0Sstevel@tonic-gate #else /* MACOS || _WIN32 || BC31 */
540*0Sstevel@tonic-gate va_alist )
541*0Sstevel@tonic-gate va_dcl
542*0Sstevel@tonic-gate #endif /* MACOS || _WIN32 || BC31 */
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate va_list ap;
545*0Sstevel@tonic-gate #if !defined(MACOS) && !defined(_WIN32) && !defined(BC31) && !defined(__sun)
546*0Sstevel@tonic-gate BerElement *ber;
547*0Sstevel@tonic-gate char *fmt;
548*0Sstevel@tonic-gate #endif /* !MACOS && !_WIN32 && !BC31 */
549*0Sstevel@tonic-gate char *s, **ss;
550*0Sstevel@tonic-gate struct berval **bv;
551*0Sstevel@tonic-gate int rc, i;
552*0Sstevel@tonic-gate unsigned int len;
553*0Sstevel@tonic-gate
554*0Sstevel@tonic-gate #if defined(MACOS) || defined(_WIN32) || defined(BC31) || defined(__sun)
555*0Sstevel@tonic-gate va_start( ap, fmt );
556*0Sstevel@tonic-gate #else /* MACOS || _WIN32 || BC31 */
557*0Sstevel@tonic-gate va_start( ap );
558*0Sstevel@tonic-gate ber = va_arg( ap, BerElement * );
559*0Sstevel@tonic-gate fmt = va_arg( ap, char * );
560*0Sstevel@tonic-gate #endif /* MACOS || _WIN32 || BC31 */
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gate for ( rc = 0; *fmt && rc != -1; fmt++ ) {
563*0Sstevel@tonic-gate switch ( *fmt ) {
564*0Sstevel@tonic-gate case 'b': /* boolean */
565*0Sstevel@tonic-gate i = va_arg( ap, int );
566*0Sstevel@tonic-gate rc = ber_put_boolean( ber, i, ber->ber_tag );
567*0Sstevel@tonic-gate break;
568*0Sstevel@tonic-gate
569*0Sstevel@tonic-gate case 'i': /* int */
570*0Sstevel@tonic-gate i = va_arg( ap, int );
571*0Sstevel@tonic-gate rc = ber_put_int( ber, i, ber->ber_tag );
572*0Sstevel@tonic-gate break;
573*0Sstevel@tonic-gate
574*0Sstevel@tonic-gate case 'e': /* enumeration */
575*0Sstevel@tonic-gate i = va_arg( ap, int );
576*0Sstevel@tonic-gate rc = ber_put_enum( ber, i, ber->ber_tag );
577*0Sstevel@tonic-gate break;
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate case 'n': /* null */
580*0Sstevel@tonic-gate rc = ber_put_null( ber, ber->ber_tag );
581*0Sstevel@tonic-gate break;
582*0Sstevel@tonic-gate
583*0Sstevel@tonic-gate case 'o': /* octet string (non-null terminated) */
584*0Sstevel@tonic-gate s = va_arg( ap, char * );
585*0Sstevel@tonic-gate len = va_arg( ap, int );
586*0Sstevel@tonic-gate rc = ber_put_ostring( ber, s, len, ber->ber_tag );
587*0Sstevel@tonic-gate break;
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate case 's': /* string */
590*0Sstevel@tonic-gate s = va_arg( ap, char * );
591*0Sstevel@tonic-gate rc = ber_put_string( ber, s, ber->ber_tag );
592*0Sstevel@tonic-gate break;
593*0Sstevel@tonic-gate
594*0Sstevel@tonic-gate case 'B': /* bit string */
595*0Sstevel@tonic-gate s = va_arg( ap, char * );
596*0Sstevel@tonic-gate len = va_arg( ap, int ); /* in bits */
597*0Sstevel@tonic-gate rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
598*0Sstevel@tonic-gate break;
599*0Sstevel@tonic-gate
600*0Sstevel@tonic-gate case 't': /* tag for the next element */
601*0Sstevel@tonic-gate ber->ber_tag = va_arg( ap, unsigned int );
602*0Sstevel@tonic-gate ber->ber_usertag = 1;
603*0Sstevel@tonic-gate break;
604*0Sstevel@tonic-gate
605*0Sstevel@tonic-gate case 'v': /* vector of strings */
606*0Sstevel@tonic-gate if ( (ss = va_arg( ap, char ** )) == NULL )
607*0Sstevel@tonic-gate break;
608*0Sstevel@tonic-gate for ( i = 0; ss[i] != NULL; i++ ) {
609*0Sstevel@tonic-gate if ( (rc = ber_put_string( ber, ss[i],
610*0Sstevel@tonic-gate ber->ber_tag )) == -1 )
611*0Sstevel@tonic-gate break;
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate break;
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate case 'V': /* sequences of strings + lengths */
616*0Sstevel@tonic-gate if ( (bv = va_arg( ap, struct berval ** )) == NULL )
617*0Sstevel@tonic-gate break;
618*0Sstevel@tonic-gate for ( i = 0; bv[i] != NULL; i++ ) {
619*0Sstevel@tonic-gate if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
620*0Sstevel@tonic-gate bv[i]->bv_len, ber->ber_tag )) == -1 )
621*0Sstevel@tonic-gate break;
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate break;
624*0Sstevel@tonic-gate
625*0Sstevel@tonic-gate case '{': /* begin sequence */
626*0Sstevel@tonic-gate rc = ber_start_seq( ber, ber->ber_tag );
627*0Sstevel@tonic-gate break;
628*0Sstevel@tonic-gate
629*0Sstevel@tonic-gate case '}': /* end sequence */
630*0Sstevel@tonic-gate rc = ber_put_seqorset( ber );
631*0Sstevel@tonic-gate break;
632*0Sstevel@tonic-gate
633*0Sstevel@tonic-gate case '[': /* begin set */
634*0Sstevel@tonic-gate rc = ber_start_set( ber, ber->ber_tag );
635*0Sstevel@tonic-gate break;
636*0Sstevel@tonic-gate
637*0Sstevel@tonic-gate case ']': /* end set */
638*0Sstevel@tonic-gate rc = ber_put_seqorset( ber );
639*0Sstevel@tonic-gate break;
640*0Sstevel@tonic-gate
641*0Sstevel@tonic-gate default:
642*0Sstevel@tonic-gate #ifndef NO_USERINTERFACE
643*0Sstevel@tonic-gate (void) fprintf( stderr, catgets(slapdcat, 1, 74, "unknown fmt %c\n"), *fmt );
644*0Sstevel@tonic-gate #endif /* NO_USERINTERFACE */
645*0Sstevel@tonic-gate rc = -1;
646*0Sstevel@tonic-gate break;
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gate if ( ber->ber_usertag == 0 )
650*0Sstevel@tonic-gate ber->ber_tag = LBER_DEFAULT;
651*0Sstevel@tonic-gate else
652*0Sstevel@tonic-gate ber->ber_usertag = 0;
653*0Sstevel@tonic-gate }
654*0Sstevel@tonic-gate
655*0Sstevel@tonic-gate va_end( ap );
656*0Sstevel@tonic-gate
657*0Sstevel@tonic-gate return( rc );
658*0Sstevel@tonic-gate }
659