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 /* decode.c - ber input decoding 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
45*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
46*0Sstevel@tonic-gate #include "msdos.h"
47*0Sstevel@tonic-gate #endif /* DOS */
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate #include <string.h>
50*0Sstevel@tonic-gate #include "lber.h"
51*0Sstevel@tonic-gate #include "ldap.h"
52*0Sstevel@tonic-gate #include "ldap-private.h"
53*0Sstevel@tonic-gate #include "ldap-int.h"
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
56*0Sstevel@tonic-gate int lber_debug;
57*0Sstevel@tonic-gate #endif
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate #ifdef NEEDPROTOS
60*0Sstevel@tonic-gate static int ber_getnint( BerElement *ber, int *num, int len );
61*0Sstevel@tonic-gate #endif /* NEEDPROTOS */
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate /* return the tag - LBER_DEFAULT returned means trouble */
65*0Sstevel@tonic-gate unsigned int
ber_get_tag(BerElement * ber)66*0Sstevel@tonic-gate ber_get_tag( BerElement *ber )
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate unsigned char xbyte;
69*0Sstevel@tonic-gate unsigned int tag;
70*0Sstevel@tonic-gate char *tagp;
71*0Sstevel@tonic-gate int i;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
74*0Sstevel@tonic-gate return( LBER_DEFAULT );
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
77*0Sstevel@tonic-gate return( (unsigned int) xbyte );
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate tagp = (char *) &tag;
80*0Sstevel@tonic-gate tagp[0] = xbyte;
81*0Sstevel@tonic-gate for ( i = 1; i < sizeof(int); i++ ) {
82*0Sstevel@tonic-gate if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
83*0Sstevel@tonic-gate return( LBER_DEFAULT );
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate tagp[i] = xbyte;
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate if ( ! (xbyte & LBER_MORE_TAG_MASK) )
88*0Sstevel@tonic-gate break;
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate /* tag too big! */
92*0Sstevel@tonic-gate if ( i == sizeof(int) )
93*0Sstevel@tonic-gate return( LBER_DEFAULT );
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate /* want leading, not trailing 0's */
96*0Sstevel@tonic-gate return( tag >> (sizeof(int) - i - 1) );
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate unsigned int
ber_skip_tag(BerElement * ber,unsigned int * len)100*0Sstevel@tonic-gate ber_skip_tag( BerElement *ber, unsigned int *len )
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate unsigned int tag;
103*0Sstevel@tonic-gate unsigned char lc;
104*0Sstevel@tonic-gate int noctets, diff;
105*0Sstevel@tonic-gate unsigned int netlen;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate * Any ber element looks like this: tag length contents.
109*0Sstevel@tonic-gate * Assuming everything's ok, we return the tag byte (we
110*0Sstevel@tonic-gate * can assume a single byte), and return the length in len.
111*0Sstevel@tonic-gate *
112*0Sstevel@tonic-gate * Assumptions:
113*0Sstevel@tonic-gate * 1) definite lengths
114*0Sstevel@tonic-gate * 2) primitive encodings used whenever possible
115*0Sstevel@tonic-gate */
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate * First, we read the tag.
119*0Sstevel@tonic-gate */
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
122*0Sstevel@tonic-gate return( LBER_DEFAULT );
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate * Next, read the length. The first byte contains the length of
126*0Sstevel@tonic-gate * the length. If bit 8 is set, the length is the int form,
127*0Sstevel@tonic-gate * otherwise it's the short form. We don't allow a length that's
128*0Sstevel@tonic-gate * greater than what we can hold in an unsigned int.
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate *len = netlen = 0;
132*0Sstevel@tonic-gate if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
133*0Sstevel@tonic-gate return( LBER_DEFAULT );
134*0Sstevel@tonic-gate if ( lc & 0x80 ) {
135*0Sstevel@tonic-gate noctets = (lc & 0x7f);
136*0Sstevel@tonic-gate if ( noctets > sizeof(unsigned int) )
137*0Sstevel@tonic-gate return( LBER_DEFAULT );
138*0Sstevel@tonic-gate diff = (int)sizeof(unsigned int) - noctets;
139*0Sstevel@tonic-gate if ( ber_read( ber, (char *) &netlen + diff, noctets )
140*0Sstevel@tonic-gate != noctets )
141*0Sstevel@tonic-gate return( LBER_DEFAULT );
142*0Sstevel@tonic-gate *len = LBER_NTOHL( netlen );
143*0Sstevel@tonic-gate } else {
144*0Sstevel@tonic-gate *len = lc;
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate return( tag );
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate unsigned int
ber_peek_tag(BerElement * ber,unsigned int * len)151*0Sstevel@tonic-gate ber_peek_tag( BerElement *ber, unsigned int *len )
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate char *save;
154*0Sstevel@tonic-gate unsigned int tag;
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate save = ber->ber_ptr;
157*0Sstevel@tonic-gate tag = ber_skip_tag( ber, len );
158*0Sstevel@tonic-gate ber->ber_ptr = save;
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate return( tag );
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate static int
ber_getnint(BerElement * ber,int * num,int len)164*0Sstevel@tonic-gate ber_getnint( BerElement *ber, int *num, int len )
165*0Sstevel@tonic-gate { /* New patch much cleaner, from David Wilson, Isode. Old code not kept*/
166*0Sstevel@tonic-gate int i;
167*0Sstevel@tonic-gate unsigned char buffer[sizeof(int)];
168*0Sstevel@tonic-gate int value;
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate * The tag and length have already been stripped off. We should
172*0Sstevel@tonic-gate * be sitting right before len bytes of 2's complement integer,
173*0Sstevel@tonic-gate * ready to be read straight into an int.
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate if ( len > sizeof(int) )
177*0Sstevel@tonic-gate return( -1 );
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate if ( ber_read( ber, (char *) buffer, len ) != len )
180*0Sstevel@tonic-gate return( -1 );
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /* This sets the required sign extension */
183*0Sstevel@tonic-gate value = 0x80 & buffer[0] ? (-1) : 0;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate for ( i = 0; i < len; i++ )
186*0Sstevel@tonic-gate value = (value << 8) | buffer[i];
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate *num = value;
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate return( len );
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate unsigned int
ber_get_int(BerElement * ber,int * num)194*0Sstevel@tonic-gate ber_get_int( BerElement *ber, int *num )
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate unsigned int tag, len;
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
199*0Sstevel@tonic-gate return( LBER_DEFAULT );
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate if ( ber_getnint( ber, num, (int)len ) != len )
202*0Sstevel@tonic-gate return( LBER_DEFAULT );
203*0Sstevel@tonic-gate else
204*0Sstevel@tonic-gate return( tag );
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate unsigned int
ber_get_stringb(BerElement * ber,char * buf,unsigned int * len)208*0Sstevel@tonic-gate ber_get_stringb( BerElement *ber, char *buf, unsigned int *len )
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate unsigned int datalen, tag;
211*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
212*0Sstevel@tonic-gate char *transbuf;
213*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
216*0Sstevel@tonic-gate return( LBER_DEFAULT );
217*0Sstevel@tonic-gate if ( datalen > (*len - 1) )
218*0Sstevel@tonic-gate return( LBER_DEFAULT );
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate if ( ber_read( ber, buf, datalen ) != datalen )
221*0Sstevel@tonic-gate return( LBER_DEFAULT );
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate buf[datalen] = '\0';
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
226*0Sstevel@tonic-gate if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
227*0Sstevel@tonic-gate && ber->ber_decode_translate_proc != NULL ) {
228*0Sstevel@tonic-gate transbuf = buf;
229*0Sstevel@tonic-gate ++datalen;
230*0Sstevel@tonic-gate if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
231*0Sstevel@tonic-gate 0 ) != 0 ) {
232*0Sstevel@tonic-gate return( LBER_DEFAULT );
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate if ( datalen > *len ) {
235*0Sstevel@tonic-gate free( transbuf );
236*0Sstevel@tonic-gate return( LBER_DEFAULT );
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate (void) SAFEMEMCPY( buf, transbuf, datalen );
239*0Sstevel@tonic-gate free( transbuf );
240*0Sstevel@tonic-gate --datalen;
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate *len = datalen;
245*0Sstevel@tonic-gate return( tag );
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate unsigned int
ber_get_stringa(BerElement * ber,char ** buf)249*0Sstevel@tonic-gate ber_get_stringa( BerElement *ber, char **buf )
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate unsigned int datalen, tag;
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
254*0Sstevel@tonic-gate return( LBER_DEFAULT );
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
257*0Sstevel@tonic-gate return( LBER_DEFAULT );
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate if ( ber_read( ber, *buf, datalen ) != datalen )
260*0Sstevel@tonic-gate return( LBER_DEFAULT );
261*0Sstevel@tonic-gate (*buf)[datalen] = '\0';
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
264*0Sstevel@tonic-gate if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
265*0Sstevel@tonic-gate && ber->ber_decode_translate_proc != NULL ) {
266*0Sstevel@tonic-gate ++datalen;
267*0Sstevel@tonic-gate if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
268*0Sstevel@tonic-gate != 0 ) {
269*0Sstevel@tonic-gate free( *buf );
270*0Sstevel@tonic-gate return( LBER_DEFAULT );
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate return( tag );
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate unsigned int
ber_get_stringal(BerElement * ber,struct berval ** bv)279*0Sstevel@tonic-gate ber_get_stringal( BerElement *ber, struct berval **bv )
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate unsigned int len, tag;
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
284*0Sstevel@tonic-gate return( LBER_DEFAULT );
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
287*0Sstevel@tonic-gate return( LBER_DEFAULT );
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
290*0Sstevel@tonic-gate return( LBER_DEFAULT );
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate if ( ber_read( ber, (*bv)->bv_val, len ) != len )
293*0Sstevel@tonic-gate return( LBER_DEFAULT );
294*0Sstevel@tonic-gate ((*bv)->bv_val)[len] = '\0';
295*0Sstevel@tonic-gate (*bv)->bv_len = len;
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
298*0Sstevel@tonic-gate if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
299*0Sstevel@tonic-gate && ber->ber_decode_translate_proc != NULL ) {
300*0Sstevel@tonic-gate ++len;
301*0Sstevel@tonic-gate if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
302*0Sstevel@tonic-gate &len, 1 ) != 0 ) {
303*0Sstevel@tonic-gate free( (*bv)->bv_val );
304*0Sstevel@tonic-gate return( LBER_DEFAULT );
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate (*bv)->bv_len = len - 1;
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate return( tag );
311*0Sstevel@tonic-gate }
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate unsigned int
ber_get_bitstringa(BerElement * ber,char ** buf,unsigned int * blen)314*0Sstevel@tonic-gate ber_get_bitstringa( BerElement *ber, char **buf, unsigned int *blen )
315*0Sstevel@tonic-gate {
316*0Sstevel@tonic-gate unsigned int datalen, tag;
317*0Sstevel@tonic-gate unsigned char unusedbits;
318*0Sstevel@tonic-gate
319*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
320*0Sstevel@tonic-gate return( LBER_DEFAULT );
321*0Sstevel@tonic-gate --datalen;
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
324*0Sstevel@tonic-gate return( LBER_DEFAULT );
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
327*0Sstevel@tonic-gate return( LBER_DEFAULT );
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate if ( ber_read( ber, *buf, datalen ) != datalen )
330*0Sstevel@tonic-gate return( LBER_DEFAULT );
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate *blen = datalen * 8 - unusedbits;
333*0Sstevel@tonic-gate return( tag );
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate unsigned int
ber_get_null(BerElement * ber)337*0Sstevel@tonic-gate ber_get_null( BerElement *ber )
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate unsigned int len, tag;
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
342*0Sstevel@tonic-gate return( LBER_DEFAULT );
343*0Sstevel@tonic-gate
344*0Sstevel@tonic-gate if ( len != 0 )
345*0Sstevel@tonic-gate return( LBER_DEFAULT );
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate return( tag );
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate
350*0Sstevel@tonic-gate unsigned int
ber_get_boolean(BerElement * ber,int * boolval)351*0Sstevel@tonic-gate ber_get_boolean( BerElement *ber, int *boolval )
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate int longbool;
354*0Sstevel@tonic-gate int rc;
355*0Sstevel@tonic-gate
356*0Sstevel@tonic-gate rc = ber_get_int( ber, &longbool );
357*0Sstevel@tonic-gate *boolval = longbool;
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate return( rc );
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate unsigned int
ber_first_element(BerElement * ber,unsigned int * len,char ** last)363*0Sstevel@tonic-gate ber_first_element( BerElement *ber, unsigned int *len, char **last )
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate /* skip the sequence header, use the len to mark where to stop */
366*0Sstevel@tonic-gate if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
367*0Sstevel@tonic-gate return( LBER_DEFAULT );
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate *last = ber->ber_ptr + *len;
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate if ( *last == ber->ber_ptr ) {
373*0Sstevel@tonic-gate return( LBER_DEFAULT );
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate return( ber_peek_tag( ber, len ) );
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate
379*0Sstevel@tonic-gate unsigned int
ber_next_element(BerElement * ber,unsigned int * len,char * last)380*0Sstevel@tonic-gate ber_next_element( BerElement *ber, unsigned int *len, char *last )
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate if ( ber->ber_ptr == last ) {
383*0Sstevel@tonic-gate return( LBER_DEFAULT );
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate return( ber_peek_tag( ber, len ) );
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate /* VARARGS */
390*0Sstevel@tonic-gate unsigned int
ber_scanf(BerElement * ber,char * fmt,...)391*0Sstevel@tonic-gate ber_scanf(
392*0Sstevel@tonic-gate #if defined(MACOS) || defined(BC31) || defined(_WIN32) || defined(__sun)
393*0Sstevel@tonic-gate BerElement *ber, char *fmt, ... )
394*0Sstevel@tonic-gate #else
395*0Sstevel@tonic-gate va_alist )
396*0Sstevel@tonic-gate va_dcl
397*0Sstevel@tonic-gate #endif
398*0Sstevel@tonic-gate {
399*0Sstevel@tonic-gate va_list ap;
400*0Sstevel@tonic-gate #if !defined(MACOS) && !defined(BC31) && !defined(_WIN32) && !defined(__sun)
401*0Sstevel@tonic-gate BerElement *ber;
402*0Sstevel@tonic-gate char *fmt;
403*0Sstevel@tonic-gate #endif
404*0Sstevel@tonic-gate char *last;
405*0Sstevel@tonic-gate char *s, **ss, ***sss;
406*0Sstevel@tonic-gate struct berval ***bv, **bvp, *bval;
407*0Sstevel@tonic-gate int *i, j;
408*0Sstevel@tonic-gate int *l, rc, tag;
409*0Sstevel@tonic-gate unsigned int len;
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gate #if defined(MACOS) || defined(BC31) || defined(_WIN32) || defined(__sun)
412*0Sstevel@tonic-gate va_start( ap, fmt );
413*0Sstevel@tonic-gate #else
414*0Sstevel@tonic-gate va_start( ap );
415*0Sstevel@tonic-gate ber = va_arg( ap, BerElement * );
416*0Sstevel@tonic-gate fmt = va_arg( ap, char * );
417*0Sstevel@tonic-gate #endif
418*0Sstevel@tonic-gate
419*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
420*0Sstevel@tonic-gate if ( lber_debug & 64 ) {
421*0Sstevel@tonic-gate (void) fprintf( stderr, catgets(slapdcat, 1, 73, "ber_scanf fmt (%s) ber:\n"), fmt );
422*0Sstevel@tonic-gate ber_dump( ber, 1 );
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate #endif
425*0Sstevel@tonic-gate
426*0Sstevel@tonic-gate for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
427*0Sstevel@tonic-gate switch ( *fmt ) {
428*0Sstevel@tonic-gate case 'a': /* octet string - allocate storage as needed */
429*0Sstevel@tonic-gate ss = va_arg( ap, char ** );
430*0Sstevel@tonic-gate rc = ber_get_stringa( ber, ss );
431*0Sstevel@tonic-gate break;
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate case 'b': /* boolean */
434*0Sstevel@tonic-gate i = va_arg( ap, int * );
435*0Sstevel@tonic-gate rc = ber_get_boolean( ber, i );
436*0Sstevel@tonic-gate break;
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate case 'e': /* enumerated */
439*0Sstevel@tonic-gate case 'i': /* int */
440*0Sstevel@tonic-gate l = va_arg( ap, int * );
441*0Sstevel@tonic-gate rc = ber_get_int( ber, l );
442*0Sstevel@tonic-gate break;
443*0Sstevel@tonic-gate
444*0Sstevel@tonic-gate case 'l': /* length of next item */
445*0Sstevel@tonic-gate l = va_arg( ap, int * );
446*0Sstevel@tonic-gate rc = ber_peek_tag( ber, (unsigned int *)l );
447*0Sstevel@tonic-gate break;
448*0Sstevel@tonic-gate
449*0Sstevel@tonic-gate case 'n': /* null */
450*0Sstevel@tonic-gate rc = ber_get_null( ber );
451*0Sstevel@tonic-gate break;
452*0Sstevel@tonic-gate
453*0Sstevel@tonic-gate case 's': /* octet string - in a buffer */
454*0Sstevel@tonic-gate s = va_arg( ap, char * );
455*0Sstevel@tonic-gate l = va_arg( ap, int * );
456*0Sstevel@tonic-gate rc = ber_get_stringb( ber, s, (unsigned int *)l );
457*0Sstevel@tonic-gate break;
458*0Sstevel@tonic-gate
459*0Sstevel@tonic-gate case 'o': /* octet string in a supplied berval */
460*0Sstevel@tonic-gate bval = va_arg( ap, struct berval * );
461*0Sstevel@tonic-gate ber_peek_tag( ber, &bval->bv_len );
462*0Sstevel@tonic-gate rc = ber_get_stringa( ber, &bval->bv_val );
463*0Sstevel@tonic-gate break;
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gate case 'O': /* octet string - allocate & include length */
466*0Sstevel@tonic-gate bvp = va_arg( ap, struct berval ** );
467*0Sstevel@tonic-gate rc = ber_get_stringal( ber, bvp );
468*0Sstevel@tonic-gate break;
469*0Sstevel@tonic-gate
470*0Sstevel@tonic-gate case 'B': /* bit string - allocate storage as needed */
471*0Sstevel@tonic-gate ss = va_arg( ap, char ** );
472*0Sstevel@tonic-gate l = va_arg( ap, int * ); /* for length, in bits */
473*0Sstevel@tonic-gate rc = ber_get_bitstringa( ber, ss, (unsigned int *)l );
474*0Sstevel@tonic-gate break;
475*0Sstevel@tonic-gate
476*0Sstevel@tonic-gate case 't': /* tag of next item */
477*0Sstevel@tonic-gate i = va_arg( ap, int * );
478*0Sstevel@tonic-gate *i = rc = ber_peek_tag( ber, &len );
479*0Sstevel@tonic-gate break;
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate case 'T': /* skip tag of next item */
482*0Sstevel@tonic-gate i = va_arg( ap, int * );
483*0Sstevel@tonic-gate *i = rc = ber_skip_tag( ber, &len );
484*0Sstevel@tonic-gate break;
485*0Sstevel@tonic-gate
486*0Sstevel@tonic-gate case 'v': /* sequence of strings */
487*0Sstevel@tonic-gate sss = va_arg( ap, char *** );
488*0Sstevel@tonic-gate *sss = NULL;
489*0Sstevel@tonic-gate j = 0;
490*0Sstevel@tonic-gate for ( tag = ber_first_element( ber, &len, &last );
491*0Sstevel@tonic-gate tag != LBER_DEFAULT && rc != LBER_DEFAULT;
492*0Sstevel@tonic-gate tag = ber_next_element( ber, &len, last ) ) {
493*0Sstevel@tonic-gate if ( *sss == NULL ) {
494*0Sstevel@tonic-gate *sss = (char **) malloc(
495*0Sstevel@tonic-gate 2 * sizeof(char *) );
496*0Sstevel@tonic-gate } else {
497*0Sstevel@tonic-gate *sss = (char **) realloc( *sss,
498*0Sstevel@tonic-gate (j + 2) * sizeof(char *) );
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate rc = ber_get_stringa( ber, &((*sss)[j]) );
501*0Sstevel@tonic-gate j++;
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate if ( j > 0 )
504*0Sstevel@tonic-gate (*sss)[j] = NULL;
505*0Sstevel@tonic-gate break;
506*0Sstevel@tonic-gate
507*0Sstevel@tonic-gate case 'V': /* sequence of strings + lengths */
508*0Sstevel@tonic-gate bv = va_arg( ap, struct berval *** );
509*0Sstevel@tonic-gate *bv = NULL;
510*0Sstevel@tonic-gate j = 0;
511*0Sstevel@tonic-gate for ( tag = ber_first_element( ber, &len, &last );
512*0Sstevel@tonic-gate tag != LBER_DEFAULT && rc != LBER_DEFAULT;
513*0Sstevel@tonic-gate tag = ber_next_element( ber, &len, last ) ) {
514*0Sstevel@tonic-gate if ( *bv == NULL ) {
515*0Sstevel@tonic-gate *bv = (struct berval **) malloc(
516*0Sstevel@tonic-gate 2 * sizeof(struct berval *) );
517*0Sstevel@tonic-gate } else {
518*0Sstevel@tonic-gate *bv = (struct berval **) realloc( *bv,
519*0Sstevel@tonic-gate (j + 2) * sizeof(struct berval *) );
520*0Sstevel@tonic-gate }
521*0Sstevel@tonic-gate rc = ber_get_stringal( ber, &((*bv)[j]) );
522*0Sstevel@tonic-gate j++;
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate if ( j > 0 )
525*0Sstevel@tonic-gate (*bv)[j] = NULL;
526*0Sstevel@tonic-gate break;
527*0Sstevel@tonic-gate
528*0Sstevel@tonic-gate case 'x': /* skip the next element - whatever it is */
529*0Sstevel@tonic-gate if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
530*0Sstevel@tonic-gate break;
531*0Sstevel@tonic-gate ber->ber_ptr += len;
532*0Sstevel@tonic-gate break;
533*0Sstevel@tonic-gate
534*0Sstevel@tonic-gate case '{': /* begin sequence */
535*0Sstevel@tonic-gate case '[': /* begin set */
536*0Sstevel@tonic-gate if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
537*0Sstevel@tonic-gate rc = ber_skip_tag( ber, &len );
538*0Sstevel@tonic-gate break;
539*0Sstevel@tonic-gate
540*0Sstevel@tonic-gate case '}': /* end sequence */
541*0Sstevel@tonic-gate case ']': /* end set */
542*0Sstevel@tonic-gate break;
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate default:
545*0Sstevel@tonic-gate #ifndef NO_USERINTERFACE
546*0Sstevel@tonic-gate (void) fprintf( stderr, catgets(slapdcat, 1, 74, "unknown fmt %c\n"), *fmt );
547*0Sstevel@tonic-gate #endif /* NO_USERINTERFACE */
548*0Sstevel@tonic-gate rc = (int) LBER_DEFAULT;
549*0Sstevel@tonic-gate break;
550*0Sstevel@tonic-gate }
551*0Sstevel@tonic-gate }
552*0Sstevel@tonic-gate
553*0Sstevel@tonic-gate va_end( ap );
554*0Sstevel@tonic-gate
555*0Sstevel@tonic-gate return( rc );
556*0Sstevel@tonic-gate }
557*0Sstevel@tonic-gate
558*0Sstevel@tonic-gate void
ber_bvfree(struct berval * bv)559*0Sstevel@tonic-gate ber_bvfree( struct berval *bv )
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate if ( bv->bv_val != NULL )
562*0Sstevel@tonic-gate free( bv->bv_val );
563*0Sstevel@tonic-gate free( (char *) bv );
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gate void
ber_bvecfree(struct berval ** bv)567*0Sstevel@tonic-gate ber_bvecfree( struct berval **bv )
568*0Sstevel@tonic-gate {
569*0Sstevel@tonic-gate int i;
570*0Sstevel@tonic-gate
571*0Sstevel@tonic-gate for ( i = 0; bv[i] != NULL; i++ )
572*0Sstevel@tonic-gate ber_bvfree( bv[i] );
573*0Sstevel@tonic-gate free( (char *) bv );
574*0Sstevel@tonic-gate }
575*0Sstevel@tonic-gate
576*0Sstevel@tonic-gate struct berval *
ber_bvdup(struct berval * bv)577*0Sstevel@tonic-gate ber_bvdup( struct berval *bv )
578*0Sstevel@tonic-gate {
579*0Sstevel@tonic-gate struct berval *new;
580*0Sstevel@tonic-gate
581*0Sstevel@tonic-gate if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
582*0Sstevel@tonic-gate == NULL ) {
583*0Sstevel@tonic-gate return( NULL );
584*0Sstevel@tonic-gate }
585*0Sstevel@tonic-gate if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
586*0Sstevel@tonic-gate free(new);
587*0Sstevel@tonic-gate return( NULL );
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
590*0Sstevel@tonic-gate new->bv_val[bv->bv_len] = '\0';
591*0Sstevel@tonic-gate new->bv_len = bv->bv_len;
592*0Sstevel@tonic-gate
593*0Sstevel@tonic-gate return( new );
594*0Sstevel@tonic-gate }
595*0Sstevel@tonic-gate
596*0Sstevel@tonic-gate
597*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
598*0Sstevel@tonic-gate void
ber_set_string_translators(BerElement * ber,BERTranslateProc encode_proc,BERTranslateProc decode_proc)599*0Sstevel@tonic-gate ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
600*0Sstevel@tonic-gate BERTranslateProc decode_proc )
601*0Sstevel@tonic-gate {
602*0Sstevel@tonic-gate ber->ber_encode_translate_proc = encode_proc;
603*0Sstevel@tonic-gate ber->ber_decode_translate_proc = decode_proc;
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
606*0Sstevel@tonic-gate
ber_flatten(BerElement * ber,struct berval ** bvPtr)607*0Sstevel@tonic-gate int ber_flatten(BerElement *ber, struct berval **bvPtr)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate struct berval * bv;
610*0Sstevel@tonic-gate int len;
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate if ((ber == NULL) || (ber->ber_buf == NULL))
613*0Sstevel@tonic-gate return (-1);
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate len = ber->ber_ptr - ber->ber_buf;
616*0Sstevel@tonic-gate
617*0Sstevel@tonic-gate if ((bv = (struct berval *)malloc(sizeof(struct berval))) == NULL)
618*0Sstevel@tonic-gate return (-1);
619*0Sstevel@tonic-gate if ((bv->bv_val = (char *) malloc(len + 1)) == NULL) {
620*0Sstevel@tonic-gate free(bv);
621*0Sstevel@tonic-gate return (-1);
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate SAFEMEMCPY(bv->bv_val, ber->ber_buf, (size_t)len);
625*0Sstevel@tonic-gate bv->bv_val[len] = '\0';
626*0Sstevel@tonic-gate bv->bv_len = len;
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate *bvPtr = bv;
629*0Sstevel@tonic-gate return (0);
630*0Sstevel@tonic-gate }
631