summaryrefslogtreecommitdiffstats
path: root/ldap/c-sdk/liblber/decode.c
blob: 732dd6e646c8766c3df89d33cf82b84c2a1fb6a5 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 * 
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-1999
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

/*
 * Copyright (c) 1990 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

/* decode.c - ber input decoding routines */

#include "lber-int.h"

/*
 * Note: ber_get_tag() only uses the ber_end and ber_ptr elements of ber.
 * If that changes, the ber_peek_tag() and/or ber_skip_tag() implementations
 * will need to be changed.
 */
/* return the tag - LBER_DEFAULT returned means trouble */
ber_tag_t
LDAP_CALL
ber_get_tag( BerElement *ber )
{
	unsigned char	xbyte;
	ber_tag_t		tag;
	char			*tagp;
	int				i;

	if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
		return( LBER_DEFAULT );

	if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
		return( (ber_uint_t) xbyte );

	tagp = (char *) &tag;
	tagp[0] = xbyte;
	for ( i = 1; i < sizeof(ber_int_t); i++ ) {
		if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
			return( LBER_DEFAULT );

		tagp[i] = xbyte;

		if ( ! (xbyte & LBER_MORE_TAG_MASK) )
			break;
	}

	/* tag too big! */
	if ( i == sizeof(ber_int_t) )
		return( LBER_DEFAULT );

	/* want leading, not trailing 0's */
	return( tag >> (sizeof(ber_int_t) - i - 1) );
}

/*
 * Note: ber_skip_tag() only uses the ber_end and ber_ptr elements of ber.
 * If that changes, the implementation of ber_peek_tag() will need to
 * be changed.
 */
ber_tag_t
LDAP_CALL
ber_skip_tag( BerElement *ber, ber_len_t *len )
{
	ber_tag_t		tag;
	unsigned char	lc;
	int				noctets, diff;
	ber_len_t		netlen;

	/*
	 * Any ber element looks like this: tag length contents.
	 * Assuming everything's ok, we return the tag byte (we
	 * can assume a single byte), and return the length in len.
	 *
	 * Assumptions:
	 *	1) definite lengths
	 *	2) primitive encodings used whenever possible
	 */

	/*
	 * First, we read the tag.
	 */

	if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
		return( LBER_DEFAULT );

	/*
	 * Next, read the length.  The first byte contains the length of
	 * the length.  If bit 8 is set, the length is the long form,
	 * otherwise it's the short form.  We don't allow a length that's
	 * greater than what we can hold in an unsigned long.
	 */

	*len = netlen = 0;
	if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
		return( LBER_DEFAULT );
	if ( lc & 0x80 ) {
		noctets = (lc & 0x7f);
		if ( noctets > sizeof(ber_uint_t) )
			return( LBER_DEFAULT );
		diff = sizeof(ber_int_t) - noctets;
		if ( ber_read( ber, (char *) &netlen + diff, noctets )
		    != noctets )
			return( LBER_DEFAULT );
		*len = LBER_NTOHL( netlen );
	} else {
		*len = lc;
	}

	return( tag );
}


/*
 * Note: Previously, we passed the "ber" parameter directly to ber_skip_tag(),
 * saving and restoring the ber_ptr element only.  We now take advantage
 * of the fact that the only ber structure elements touched by ber_skip_tag()
 * are ber_end and ber_ptr.  If that changes, this code must change too.
 */
ber_tag_t
LDAP_CALL
ber_peek_tag( BerElement *ber, ber_len_t *len )
{
	BerElement	bercopy;

	bercopy.ber_end = ber->ber_end;
	bercopy.ber_ptr = ber->ber_ptr;
	return( ber_skip_tag( &bercopy, len ));
}

static int
ber_getnint( BerElement *ber, ber_int_t *num, ber_slen_t len )
{
	int				i;
	ber_int_t		value;
	unsigned char	buffer[sizeof(ber_int_t)];
	/*
	 * The tag and length have already been stripped off.  We should
	 * be sitting right before len bytes of 2's complement integer,
	 * ready to be read straight into an int.  We may have to sign
	 * extend after we read it in.
	 */

	if ( len > sizeof(ber_slen_t) )
		return( -1 );

	/* read into the low-order bytes of netnum */
	if ( ber_read( ber, (char *) buffer, len ) != len )
		return( -1 );

	/* This sets the required sign extension */
	if ( len != 0) {
		value = 0x80 & buffer[0] ? (-1) : 0;
	} else {
		value = 0;
	}
		
	for ( i = 0; i < len; i++ )
	  value = (value << 8) | buffer[i];
	
	*num = value;
	
	return( len );
}

ber_tag_t
LDAP_CALL
ber_get_int( BerElement *ber, ber_int_t *num )
{
	ber_tag_t	tag;
	ber_len_t	len;

	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
		return( LBER_DEFAULT );

	/*
     * len is being demoted to a long here --  possible conversion error
     */
  
	if ( ber_getnint( ber, num, (int)len ) != (ber_slen_t)len )
		return( LBER_DEFAULT );
	else
		return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_stringb( BerElement *ber, char *buf, ber_len_t *len )
{
	ber_len_t	datalen;
	ber_tag_t	tag;
#ifdef STR_TRANSLATION
	char		*transbuf;
#endif /* STR_TRANSLATION */

	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
		return( LBER_DEFAULT );
	if ( datalen > (*len - 1) )
		return( LBER_DEFAULT );

	/*
     * datalen is being demoted to a long here --  possible conversion error
     */

	if ( ber_read( ber, buf, datalen ) != (ber_slen_t) datalen )
		return( LBER_DEFAULT );

	buf[datalen] = '\0';

#ifdef STR_TRANSLATION
	if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
	    != 0 && ber->ber_decode_translate_proc != NULL ) {
		transbuf = buf;
		++datalen;
		if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
		    0 ) != 0 ) {
			return( LBER_DEFAULT );
		}
		if ( datalen > *len ) {
			NSLBERI_FREE( transbuf );
			return( LBER_DEFAULT );
		}
		SAFEMEMCPY( buf, transbuf, datalen );
		NSLBERI_FREE( transbuf );
		--datalen;
	}
#endif /* STR_TRANSLATION */

	*len = datalen;
	return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_stringa( BerElement *ber, char **buf )
{
	ber_len_t	datalen, ndatalen;
	ber_tag_t	tag;

	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
		return( LBER_DEFAULT );

	if ( ((ndatalen = (size_t)datalen + 1) < (size_t) datalen) ||
	   ( datalen > (ber->ber_end - ber->ber_ptr) ) ||
	   ( (*buf = (char *)NSLBERI_MALLOC( (size_t)ndatalen )) == NULL ))
		return( LBER_DEFAULT );

    /*
     * datalen is being demoted to a long here --  possible conversion error
     */
	if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen ) {
		NSLBERI_FREE( *buf );
		*buf = NULL;
		return( LBER_DEFAULT );
	}

	(*buf)[datalen] = '\0';

#ifdef STR_TRANSLATION
	if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
	    != 0 && ber->ber_decode_translate_proc != NULL ) {
		++datalen;
		if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
		    != 0 ) {
			NSLBERI_FREE( *buf );
			*buf = NULL;
			return( LBER_DEFAULT );
		}
	}
#endif /* STR_TRANSLATION */

	return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_stringal( BerElement *ber, struct berval **bv )
{
	ber_len_t	len, nlen;
	ber_tag_t	tag;

	if ( (*bv = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
	    == NULL ) {
		return( LBER_DEFAULT );
	}
	
	(*bv)->bv_val = NULL;
	(*bv)->bv_len = 0;

	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
		NSLBERI_FREE( *bv );
		*bv = NULL;
		return( LBER_DEFAULT );
	}

	if ( ((nlen = (size_t) len + 1) < (size_t)len) ||
	     ( len > (ber->ber_end - ber->ber_ptr) ) ||
	     (((*bv)->bv_val = (char *)NSLBERI_MALLOC( (size_t)nlen ))
	    == NULL )) {
		NSLBERI_FREE( *bv );
		*bv = NULL;
		return( LBER_DEFAULT );
	}

    /*
     * len is being demoted to a long here --  possible conversion error
     */
	if ( ber_read( ber, (*bv)->bv_val, len ) != (ber_slen_t) len ) {
		NSLBERI_FREE( (*bv)->bv_val );
		(*bv)->bv_val = NULL;
		NSLBERI_FREE( *bv );
		*bv = NULL;
		return( LBER_DEFAULT );
	}

	((*bv)->bv_val)[len] = '\0';
	(*bv)->bv_len = len;

#ifdef STR_TRANSLATION
	if ( len > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS ) != 0
	    && ber->ber_decode_translate_proc != NULL ) {
		++len;
		if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
		    &len, 1 ) != 0 ) {
			NSLBERI_FREE( (*bv)->bv_val );
			(*bv)->bv_val = NULL;
			NSLBERI_FREE( *bv );
			*bv = NULL;
			return( LBER_DEFAULT );
		}
		(*bv)->bv_len = len - 1;
	}
#endif /* STR_TRANSLATION */

	return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_bitstringa( BerElement *ber, char **buf, ber_len_t *blen )
{
	ber_len_t		datalen;
	ber_tag_t		tag;
	unsigned char	unusedbits;

	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
		return( LBER_DEFAULT );
	--datalen;

	if ( (datalen > (ber->ber_end - ber->ber_ptr)) ||
	   ( (*buf = (char *)NSLBERI_MALLOC((size_t)datalen )) == NULL ) )
		return( LBER_DEFAULT );

	if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
		NSLBERI_FREE( *buf );
		*buf = NULL;
		return( LBER_DEFAULT );
	}

	/*
     * datalen is being demoted to a long here --  possible conversion error
     */
	if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen ) {
		NSLBERI_FREE( *buf );
		*buf = NULL;
		return( LBER_DEFAULT );
	}

	*blen = datalen * 8 - unusedbits;
	return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_null( BerElement *ber )
{
	ber_len_t	len;
	ber_tag_t	tag;

	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
		return( LBER_DEFAULT );

	if ( len != 0 )
		return( LBER_DEFAULT );

	return( tag );
}

ber_tag_t
LDAP_CALL
ber_get_boolean( BerElement *ber, ber_int_t *boolval )
{
	int	rc;

	rc = ber_get_int( ber, boolval );

	return( rc );
}

ber_tag_t
LDAP_CALL
ber_first_element( BerElement *ber, ber_len_t *len, char **last )
{
	/* skip the sequence header, use the len to mark where to stop */
	if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
		return( LBER_ERROR );
	}

	*last = ber->ber_ptr + *len;

	if ( *last == ber->ber_ptr ) {
		return( LBER_END_OF_SEQORSET );
	}

	return( ber_peek_tag( ber, len ) );
}

ber_tag_t
LDAP_CALL
ber_next_element( BerElement *ber, ber_len_t *len, char *last )
{
	if ( ber->ber_ptr == last ) {
		return( LBER_END_OF_SEQORSET );
	}

	return( ber_peek_tag( ber, len ) );
}

/* VARARGS */
ber_tag_t
LDAP_C
ber_scanf( BerElement *ber, const char *fmt, ... )
{
	va_list			ap;
	char			*last, *p;
	char			*s, **ss, ***sss;
	struct berval 	***bv, **bvp, *bval;
	int				*i, j;
	ber_int_t		*l, rc, tag;
	ber_tag_t		*t;
	ber_len_t		len;
	size_t			array_size;
	
	va_start( ap, fmt );

#ifdef LDAP_DEBUG
	if ( lber_debug & 64 ) {
		char msg[80];
		sprintf( msg, "ber_scanf fmt (%s) ber:\n", fmt );
		ber_err_print( msg );
		ber_dump( ber, 1 );
	}
#endif
	for ( rc = 0, p = (char *) fmt; *p && rc != LBER_DEFAULT; p++ ) {
		switch ( *p ) {
		case 'a':	/* octet string - allocate storage as needed */
			ss = va_arg( ap, char ** );
			rc = ber_get_stringa( ber, ss );
			break;

		case 'b':	/* boolean */
			i = va_arg( ap, int * );
			rc = ber_get_boolean( ber, i );
			break;

		case 'e':	/* enumerated */
		case 'i':	/* int */
			l = va_arg( ap, ber_slen_t * );
			rc = ber_get_int( ber, l );
			break;

		case 'l':	/* length of next item */
			l = va_arg( ap, ber_slen_t * );
			rc = ber_peek_tag( ber, (ber_len_t *)l );
			break;

		case 'n':	/* null */
			rc = ber_get_null( ber );
			break;

		case 's':	/* octet string - in a buffer */
			s = va_arg( ap, char * );
			l = va_arg( ap, ber_slen_t * );
			rc = ber_get_stringb( ber, s, (ber_len_t *)l );
			break;

		case 'o':	/* octet string in a supplied berval */
			bval = va_arg( ap, struct berval * );
			ber_peek_tag( ber, &bval->bv_len );
			rc = ber_get_stringa( ber, &bval->bv_val );
			break;

		case 'O':	/* octet string - allocate & include length */
			bvp = va_arg( ap, struct berval ** );
			rc = ber_get_stringal( ber, bvp );
			break;

		case 'B':	/* bit string - allocate storage as needed */
			ss = va_arg( ap, char ** );
			l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
			rc = ber_get_bitstringa( ber, ss, (ber_len_t *)l );
			break;

		case 't':	/* tag of next item */
			t = va_arg( ap, ber_tag_t * );
			*t = rc = ber_peek_tag( ber, &len );
			break;

		case 'T':	/* skip tag of next item */
			t = va_arg( ap, ber_tag_t * );
			*t = rc = ber_skip_tag( ber, &len );
			break;

		case 'v':	/* sequence of strings */
			sss = va_arg( ap, char *** );
			*sss = NULL;
			j = 0;
			array_size = 0;
			for ( tag = ber_first_element( ber, &len, &last );
			    tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
			    && rc != LBER_DEFAULT;
			    tag = ber_next_element( ber, &len, last ) ) {
				if ( *sss == NULL ) {
				    /* Make room for at least 15 strings */
				    *sss = (char **)NSLBERI_MALLOC(16 * sizeof(char *) );
					if (!*sss) {
						rc = LBER_DEFAULT;
						break; /* out of memory - cannot continue */
                    }
				    array_size = 16;
				} else {
				    char **save_sss = *sss;
				    if ( (size_t)(j+2) > array_size) {
						/* We'v overflowed our buffer */
						*sss = (char **)NSLBERI_REALLOC( *sss, (array_size * 2) * sizeof(char *) );
						array_size = array_size * 2;
				    }
					if (!*sss) {
						rc = LBER_DEFAULT;
						ber_svecfree(save_sss);
						break; /* out of memory - cannot continue */
					}
				}
				(*sss)[j] = NULL;
				rc = ber_get_stringa( ber, &((*sss)[j]) );
				j++;
			}
			if ( rc != LBER_DEFAULT &&
			    tag != LBER_END_OF_SEQORSET ) {
				rc = LBER_DEFAULT;
			}
			if ( *sss && (j > 0) ) {
				(*sss)[j] = NULL;
			}
			break;

		case 'V':	/* sequence of strings + lengths */
			bv = va_arg( ap, struct berval *** );
			*bv = NULL;
			j = 0;
			for ( tag = ber_first_element( ber, &len, &last );
			    tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
			    && rc != LBER_DEFAULT;
			    tag = ber_next_element( ber, &len, last ) ) {
				if ( *bv == NULL ) {
					*bv = (struct berval **)NSLBERI_MALLOC(
					    2 * sizeof(struct berval *) );
					if (!*bv) {
						rc = LBER_DEFAULT;
						break; /* out of memory - cannot continue */
					}
				} else {
					struct berval **save_bv = *bv;
					*bv = (struct berval **)NSLBERI_REALLOC(
					    *bv,
					    (j + 2) * sizeof(struct berval *) );
					if (!*bv) {
						rc = LBER_DEFAULT;
						ber_bvecfree(save_bv);
						break; /* out of memory - cannot continue */
					}
				}
				rc = ber_get_stringal( ber, &((*bv)[j]) );
				j++;
			}
			if ( rc != LBER_DEFAULT &&
			    tag != LBER_END_OF_SEQORSET ) {
				rc = LBER_DEFAULT;
			}
			if ( *bv && (j > 0) ) {
				(*bv)[j] = NULL;
			}
			break;

		case 'x':	/* skip the next element - whatever it is */
			if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
				break;
			ber->ber_ptr += len;
			break;

		case '{':	/* begin sequence */
		case '[':	/* begin set */
			if ( *(p + 1) != 'v' && *(p + 1) != 'V' )
				rc = ber_skip_tag( ber, &len );
			break;

		case '}':	/* end sequence */
		case ']':	/* end set */
			break;

		default:
			{
				char msg[80];
				sprintf( msg, "unknown fmt %c\n", *p );
				ber_err_print( msg );
			}
			rc = LBER_DEFAULT;
			break;
		}
	}

	va_end( ap );

	if (rc == LBER_DEFAULT) {
	  va_start( ap, fmt );
	  for ( p--; fmt < p && *fmt; fmt++ ) {
		switch ( *fmt ) {
		case 'a':	/* octet string - allocate storage as needed */
			ss = va_arg( ap, char ** );
			NSLBERI_FREE(*ss);
			*ss = NULL;
			break;

		case 'b':	/* boolean */
			i = va_arg( ap, int * );
			break;

		case 'e':	/* enumerated */
		case 'i':	/* int */
			l = va_arg( ap, ber_slen_t * );
			break;

		case 'l':	/* length of next item */
			l = va_arg( ap, ber_slen_t * );
			break;

		case 'n':	/* null */
			break;

		case 's':	/* octet string - in a buffer */
			s = va_arg( ap, char * );
			l = va_arg( ap, ber_slen_t * );
			break;

		case 'o':	/* octet string in a supplied berval */
			bval = va_arg( ap, struct berval * );
			if (bval->bv_val) NSLBERI_FREE(bval->bv_val);
			memset(bval, 0, sizeof(struct berval));
			break;

		case 'O':	/* octet string - allocate & include length */
			bvp = va_arg( ap, struct berval ** );
			ber_bvfree(*bvp);
			bvp = NULL;
			break;

		case 'B':	/* bit string - allocate storage as needed */
			ss = va_arg( ap, char ** );
			l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
			if (*ss) NSLBERI_FREE(*ss);
			*ss = NULL;
			break;

		case 't':	/* tag of next item */
			t = va_arg( ap, ber_tag_t * );
			break;

		case 'T':	/* skip tag of next item */
			t = va_arg( ap, ber_tag_t * );
			break;

		case 'v':	/* sequence of strings */
			sss = va_arg( ap, char *** );
			ber_svecfree(*sss);
			*sss = NULL;
			break;

		case 'V':	/* sequence of strings + lengths */
			bv = va_arg( ap, struct berval *** );
			ber_bvecfree(*bv);
			*bv = NULL;
			break;

		case 'x':	/* skip the next element - whatever it is */
			break;

		case '{':	/* begin sequence */
		case '[':	/* begin set */
			break;

		case '}':	/* end sequence */
		case ']':	/* end set */
			break;

		default:
			break;
		}
	  } /* for */
	  va_end( ap );
	} /* if */

	return( rc );
}

void
LDAP_CALL
ber_bvfree( struct berval *bv )
{
	if ( bv != NULL ) {
		if ( bv->bv_val != NULL ) {
			NSLBERI_FREE( bv->bv_val );
		}
		NSLBERI_FREE( (char *) bv );
	}
}

void
LDAP_CALL
ber_bvecfree( struct berval **bv )
{
	int	i;

	if ( bv != NULL ) {
		for ( i = 0; bv[i] != NULL; i++ ) {
			ber_bvfree( bv[i] );
		}
		NSLBERI_FREE( (char *) bv );
	}
}

struct berval *
LDAP_CALL
ber_bvdup( const struct berval *bv )
{
	struct berval	*new;

	if ( (new = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
	    == NULL ) {
		return( NULL );
	}
	if ( bv->bv_val == NULL ) {
	    new->bv_val = NULL;
	    new->bv_len = 0;
	} else {
	    if ( (new->bv_val = (char *)NSLBERI_MALLOC( bv->bv_len + 1 ))
		== NULL ) {
			NSLBERI_FREE( new );
			new = NULL;
		    return( NULL );
	    }
	    SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
	    new->bv_val[bv->bv_len] = '\0';
	    new->bv_len = bv->bv_len;
	}

	return( new );
}

void
LDAP_CALL
ber_svecfree( char **vals )
{
        int     i;

        if ( vals == NULL )
                return;
        for ( i = 0; vals[i] != NULL; i++ )
                NSLBERI_FREE( vals[i] );
        NSLBERI_FREE( (char *) vals );
}

#ifdef STR_TRANSLATION
void
LDAP_CALL
ber_set_string_translators(
    BerElement		*ber,
    BERTranslateProc	encode_proc,
    BERTranslateProc	decode_proc
)
{
    ber->ber_encode_translate_proc = encode_proc;
    ber->ber_decode_translate_proc = decode_proc;
}
#endif /* STR_TRANSLATION */