17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public License
97c478bd9Sstevel@tonic-gate  * Version 1.0 (the "NPL"); you may not use this file except in
107c478bd9Sstevel@tonic-gate  * compliance with the NPL.  You may obtain a copy of the NPL at
117c478bd9Sstevel@tonic-gate  * http://www.mozilla.org/NPL/
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Software distributed under the NPL is distributed on an "AS IS" basis,
147c478bd9Sstevel@tonic-gate  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
157c478bd9Sstevel@tonic-gate  * for the specific language governing rights and limitations under the
167c478bd9Sstevel@tonic-gate  * NPL.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * The Initial Developer of this code under the NPL is Netscape
197c478bd9Sstevel@tonic-gate  * Communications Corporation.  Portions created by Netscape are
207c478bd9Sstevel@tonic-gate  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
217c478bd9Sstevel@tonic-gate  * Reserved.
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 Regents of the University of Michigan.
267c478bd9Sstevel@tonic-gate  * All rights reserved.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
297c478bd9Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
307c478bd9Sstevel@tonic-gate  * to the University of Michigan at Ann Arbor. The name of the University
317c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
327c478bd9Sstevel@tonic-gate  * software without specific prior written permission. This software
337c478bd9Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* decode.c - ber input decoding routines */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "lber-int.h"
397c478bd9Sstevel@tonic-gate LDAP_API(void) LDAP_CALL ber_svecfree( char **vals );
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Note: ber_get_tag() only uses the ber_end and ber_ptr elements of ber.
437c478bd9Sstevel@tonic-gate  * If that changes, the ber_peek_tag() and/or ber_skip_tag() implementations
447c478bd9Sstevel@tonic-gate  * will need to be changed.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate /* return the tag - LBER_DEFAULT returned means trouble */
477c478bd9Sstevel@tonic-gate ber_tag_t
487c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_tag(BerElement * ber)497c478bd9Sstevel@tonic-gate ber_get_tag( BerElement *ber )
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	unsigned char	xbyte;
527c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
537c478bd9Sstevel@tonic-gate 	char		*tagp;
547c478bd9Sstevel@tonic-gate 	int		i;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
577c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
607c478bd9Sstevel@tonic-gate 		return( (ber_uint_t) xbyte );
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	tagp = (char *) &tag;
637c478bd9Sstevel@tonic-gate 	tagp[0] = xbyte;
647c478bd9Sstevel@tonic-gate 	for ( i = 1; i < sizeof(ber_int_t); i++ ) {
657c478bd9Sstevel@tonic-gate 		if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
667c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		tagp[i] = xbyte;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		if ( ! (xbyte & LBER_MORE_TAG_MASK) )
717c478bd9Sstevel@tonic-gate 			break;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/* tag too big! */
757c478bd9Sstevel@tonic-gate 	if ( i == sizeof(ber_int_t) )
767c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/* want leading, not trailing 0's */
797c478bd9Sstevel@tonic-gate 	return( tag >> (sizeof(ber_int_t) - i - 1) );
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Note: ber_skip_tag() only uses the ber_end and ber_ptr elements of ber.
847c478bd9Sstevel@tonic-gate  * If that changes, the implementation of ber_peek_tag() will need to
857c478bd9Sstevel@tonic-gate  * be changed.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate ber_tag_t
887c478bd9Sstevel@tonic-gate LDAP_CALL
ber_skip_tag(BerElement * ber,ber_len_t * len)897c478bd9Sstevel@tonic-gate ber_skip_tag( BerElement *ber, ber_len_t *len )
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
927c478bd9Sstevel@tonic-gate 	unsigned char	lc;
937c478bd9Sstevel@tonic-gate 	int		noctets, diff;
947c478bd9Sstevel@tonic-gate 	ber_len_t	netlen;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * Any ber element looks like this: tag length contents.
987c478bd9Sstevel@tonic-gate 	 * Assuming everything's ok, we return the tag byte (we
997c478bd9Sstevel@tonic-gate 	 * can assume a single byte), and return the length in len.
1007c478bd9Sstevel@tonic-gate 	 *
1017c478bd9Sstevel@tonic-gate 	 * Assumptions:
1027c478bd9Sstevel@tonic-gate 	 *	1) definite lengths
1037c478bd9Sstevel@tonic-gate 	 *	2) primitive encodings used whenever possible
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * First, we read the tag.
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
1117c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * Next, read the length.  The first byte contains the length of
1157c478bd9Sstevel@tonic-gate 	 * the length.  If bit 8 is set, the length is the long form,
1167c478bd9Sstevel@tonic-gate 	 * otherwise it's the short form.  We don't allow a length that's
1177c478bd9Sstevel@tonic-gate 	 * greater than what we can hold in an unsigned long.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	*len = netlen = 0;
1217c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
1227c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
1237c478bd9Sstevel@tonic-gate 	if ( lc & 0x80 ) {
1247c478bd9Sstevel@tonic-gate 		noctets = (lc & 0x7f);
1257c478bd9Sstevel@tonic-gate 		if ( noctets > sizeof(ber_uint_t) )
1267c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
1277c478bd9Sstevel@tonic-gate 		diff = sizeof(ber_int_t) - noctets;
1287c478bd9Sstevel@tonic-gate 		if ( ber_read( ber, (char *) &netlen + diff, noctets )
1297c478bd9Sstevel@tonic-gate 		    != noctets )
1307c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
1317c478bd9Sstevel@tonic-gate 		*len = LBER_NTOHL( netlen );
1327c478bd9Sstevel@tonic-gate 	} else {
1337c478bd9Sstevel@tonic-gate 		*len = lc;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return( tag );
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Note: Previously, we passed the "ber" parameter directly to ber_skip_tag(),
1427c478bd9Sstevel@tonic-gate  * saving and restoring the ber_ptr element only.  We now take advantage
1437c478bd9Sstevel@tonic-gate  * of the fact that the only ber structure elements touched by ber_skip_tag()
1447c478bd9Sstevel@tonic-gate  * are ber_end and ber_ptr.  If that changes, this code must change too.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate ber_tag_t
1477c478bd9Sstevel@tonic-gate LDAP_CALL
ber_peek_tag(BerElement * ber,ber_len_t * len)1487c478bd9Sstevel@tonic-gate ber_peek_tag( BerElement *ber, ber_len_t *len )
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	BerElement	bercopy;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	bercopy.ber_end = ber->ber_end;
1537c478bd9Sstevel@tonic-gate 	bercopy.ber_ptr = ber->ber_ptr;
1547c478bd9Sstevel@tonic-gate 	return( ber_skip_tag( &bercopy, len ));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static int
ber_getnint(BerElement * ber,ber_int_t * num,ber_slen_t len)1587c478bd9Sstevel@tonic-gate ber_getnint( BerElement *ber, ber_int_t *num, ber_slen_t len )
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	int i;
1617c478bd9Sstevel@tonic-gate 	ber_int_t value;
1627c478bd9Sstevel@tonic-gate 	unsigned char buffer[sizeof(ber_int_t)];
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * The tag and length have already been stripped off.  We should
1657c478bd9Sstevel@tonic-gate 	 * be sitting right before len bytes of 2's complement integer,
1667c478bd9Sstevel@tonic-gate 	 * ready to be read straight into an int.  We may have to sign
1677c478bd9Sstevel@tonic-gate 	 * extend after we read it in.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if ( len > sizeof(ber_slen_t) )
1717c478bd9Sstevel@tonic-gate 		return( -1 );
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* read into the low-order bytes of netnum */
1747c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, (char *) buffer, len ) != len )
1757c478bd9Sstevel@tonic-gate 		return( -1 );
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* This sets the required sign extension */
1787c478bd9Sstevel@tonic-gate 	if ( len != 0) {
1797c478bd9Sstevel@tonic-gate 		value = 0x80 & buffer[0] ? (LBER_FUNC_VALUE) : 0;
1807c478bd9Sstevel@tonic-gate 	} else {
1817c478bd9Sstevel@tonic-gate 		value = 0;
1827c478bd9Sstevel@tonic-gate 	}
183*1da57d55SToomas Soome 
1847c478bd9Sstevel@tonic-gate 	for ( i = 0; i < len; i++ )
1857c478bd9Sstevel@tonic-gate 	  value = (value << 8) | buffer[i];
186*1da57d55SToomas Soome 
1877c478bd9Sstevel@tonic-gate 	*num = value;
188*1da57d55SToomas Soome 
1897c478bd9Sstevel@tonic-gate 	return( len );
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate ber_tag_t
1937c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_int(BerElement * ber,ber_int_t * num)1947c478bd9Sstevel@tonic-gate ber_get_int( BerElement *ber, ber_int_t *num )
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
1977c478bd9Sstevel@tonic-gate 	ber_len_t	len;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
2007c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate      * len is being demoted to a long here --  possible conversion error
2047c478bd9Sstevel@tonic-gate      */
205*1da57d55SToomas Soome 
2067c478bd9Sstevel@tonic-gate 	if ( ber_getnint( ber, num, (int)len ) != (ber_slen_t)len )
2077c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2087c478bd9Sstevel@tonic-gate 	else
2097c478bd9Sstevel@tonic-gate 		return( tag );
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate ber_tag_t
2137c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_stringb(BerElement * ber,char * buf,ber_len_t * len)2147c478bd9Sstevel@tonic-gate ber_get_stringb( BerElement *ber, char *buf, ber_len_t *len )
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	ber_len_t	datalen;
2177c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
2187c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
2197c478bd9Sstevel@tonic-gate 	char		*transbuf;
2207c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
2237c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2247c478bd9Sstevel@tonic-gate 	if ( datalen > (*len - 1) )
2257c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate      * datalen is being demoted to a long here --  possible conversion error
2297c478bd9Sstevel@tonic-gate      */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, buf, datalen ) != (ber_slen_t) datalen )
2327c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	buf[datalen] = '\0';
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
2377c478bd9Sstevel@tonic-gate 	if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
2387c478bd9Sstevel@tonic-gate 	    != 0 && ber->ber_decode_translate_proc != NULL ) {
2397c478bd9Sstevel@tonic-gate 		transbuf = buf;
2407c478bd9Sstevel@tonic-gate 		++datalen;
2417c478bd9Sstevel@tonic-gate 		if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
2427c478bd9Sstevel@tonic-gate 		    0 ) != 0 ) {
2437c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		if ( datalen > *len ) {
2467c478bd9Sstevel@tonic-gate 			NSLBERI_FREE( transbuf );
2477c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		SAFEMEMCPY( buf, transbuf, datalen );
2507c478bd9Sstevel@tonic-gate 		NSLBERI_FREE( transbuf );
2517c478bd9Sstevel@tonic-gate 		--datalen;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	*len = datalen;
2567c478bd9Sstevel@tonic-gate 	return( tag );
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate ber_tag_t
2607c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_stringa(BerElement * ber,char ** buf)2617c478bd9Sstevel@tonic-gate ber_get_stringa( BerElement *ber, char **buf )
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	ber_len_t	datalen;
2647c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
2677c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if ( (*buf = (char *)NSLBERI_MALLOC( (size_t)datalen + 1 )) == NULL )
2707c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate      * datalen is being demoted to a long here --  possible conversion error
2747c478bd9Sstevel@tonic-gate      */
2757c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen )
2767c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
2777c478bd9Sstevel@tonic-gate 	(*buf)[datalen] = '\0';
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
2807c478bd9Sstevel@tonic-gate 	if ( datalen > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS )
2817c478bd9Sstevel@tonic-gate 	    != 0 && ber->ber_decode_translate_proc != NULL ) {
2827c478bd9Sstevel@tonic-gate 		++datalen;
2837c478bd9Sstevel@tonic-gate 		if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
2847c478bd9Sstevel@tonic-gate 		    != 0 ) {
2857c478bd9Sstevel@tonic-gate 			NSLBERI_FREE( *buf );
2867c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return( tag );
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate ber_tag_t
2957c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_stringal(BerElement * ber,struct berval ** bv)2967c478bd9Sstevel@tonic-gate ber_get_stringal( BerElement *ber, struct berval **bv )
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	ber_len_t	len;
2997c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if ( (*bv = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
3027c478bd9Sstevel@tonic-gate 	    == NULL ) {
3037c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
3077c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if ( ((*bv)->bv_val = (char *)NSLBERI_MALLOC( (size_t)len + 1 ))
3117c478bd9Sstevel@tonic-gate 	    == NULL ) {
3127c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate      * len is being demoted to a long here --  possible conversion error
3177c478bd9Sstevel@tonic-gate      */
3187c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, (*bv)->bv_val, len ) != (ber_slen_t) len )
3197c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3207c478bd9Sstevel@tonic-gate 	((*bv)->bv_val)[len] = '\0';
3217c478bd9Sstevel@tonic-gate 	(*bv)->bv_len = len;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
3247c478bd9Sstevel@tonic-gate 	if ( len > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS ) != 0
3257c478bd9Sstevel@tonic-gate 	    && ber->ber_decode_translate_proc != NULL ) {
3267c478bd9Sstevel@tonic-gate 		++len;
3277c478bd9Sstevel@tonic-gate 		if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
3287c478bd9Sstevel@tonic-gate 		    &len, 1 ) != 0 ) {
3297c478bd9Sstevel@tonic-gate 			NSLBERI_FREE( (*bv)->bv_val );
3307c478bd9Sstevel@tonic-gate 			return( LBER_DEFAULT );
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		(*bv)->bv_len = len - 1;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	return( tag );
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate ber_tag_t
3407c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_bitstringa(BerElement * ber,char ** buf,ber_len_t * blen)3417c478bd9Sstevel@tonic-gate ber_get_bitstringa( BerElement *ber, char **buf, ber_len_t *blen )
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	ber_len_t	datalen;
3447c478bd9Sstevel@tonic-gate 	ber_tag_t	tag;
3457c478bd9Sstevel@tonic-gate 	unsigned char	unusedbits;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
3487c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3497c478bd9Sstevel@tonic-gate 	--datalen;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if ( (*buf = (char *)NSLBERI_MALLOC( (size_t)datalen )) == NULL )
3527c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
3557c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate      * datalen is being demoted to a long here --  possible conversion error
3597c478bd9Sstevel@tonic-gate      */
3607c478bd9Sstevel@tonic-gate 	if ( ber_read( ber, *buf, datalen ) != (ber_slen_t) datalen )
3617c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	*blen = datalen * 8 - unusedbits;
3647c478bd9Sstevel@tonic-gate 	return( tag );
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate ber_tag_t
3687c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_null(BerElement * ber)3697c478bd9Sstevel@tonic-gate ber_get_null( BerElement *ber )
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	ber_len_t	len;
3727c478bd9Sstevel@tonic-gate 	ber_tag_t tag;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
3757c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if ( len != 0 )
3787c478bd9Sstevel@tonic-gate 		return( LBER_DEFAULT );
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return( tag );
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate ber_tag_t
3847c478bd9Sstevel@tonic-gate LDAP_CALL
ber_get_boolean(BerElement * ber,int * boolval)3857c478bd9Sstevel@tonic-gate ber_get_boolean( BerElement *ber, int *boolval )
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	ber_int_t	longbool;
3887c478bd9Sstevel@tonic-gate 	int		rc;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	rc = ber_get_int( ber, &longbool );
3917c478bd9Sstevel@tonic-gate 	*boolval = longbool;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	return( rc );
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate ber_tag_t
3977c478bd9Sstevel@tonic-gate LDAP_CALL
ber_first_element(BerElement * ber,ber_len_t * len,char ** last)3987c478bd9Sstevel@tonic-gate ber_first_element( BerElement *ber, ber_len_t *len, char **last )
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	/* skip the sequence header, use the len to mark where to stop */
4017c478bd9Sstevel@tonic-gate 	if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
4027c478bd9Sstevel@tonic-gate 		return( LBER_ERROR );
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	*last = ber->ber_ptr + *len;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ( *last == ber->ber_ptr ) {
4087c478bd9Sstevel@tonic-gate 		return( LBER_END_OF_SEQORSET );
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return( ber_peek_tag( ber, len ) );
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate ber_tag_t
4157c478bd9Sstevel@tonic-gate LDAP_CALL
ber_next_element(BerElement * ber,ber_len_t * len,char * last)4167c478bd9Sstevel@tonic-gate ber_next_element( BerElement *ber, ber_len_t *len, char *last )
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	if ( ber->ber_ptr == last ) {
4197c478bd9Sstevel@tonic-gate 		return( LBER_END_OF_SEQORSET );
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return( ber_peek_tag( ber, len ) );
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /* VARARGS */
4267c478bd9Sstevel@tonic-gate ber_tag_t
4277c478bd9Sstevel@tonic-gate LDAP_C
ber_scanf(BerElement * ber,const char * fmt,...)4287c478bd9Sstevel@tonic-gate ber_scanf( BerElement *ber, const char *fmt, ... )
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	va_list		ap;
4317c478bd9Sstevel@tonic-gate 	char		*last, *p;
4327c478bd9Sstevel@tonic-gate 	char		*s, **ss, ***sss;
4337c478bd9Sstevel@tonic-gate 	struct berval 	***bv, **bvp, *bval;
4347c478bd9Sstevel@tonic-gate 	int		*i, j;
4357c478bd9Sstevel@tonic-gate 	ber_int_t	*l, rc, tag;
4367c478bd9Sstevel@tonic-gate 	ber_tag_t	*t;
4377c478bd9Sstevel@tonic-gate 	ber_len_t	len;
4387c478bd9Sstevel@tonic-gate 	size_t		array_size;
439*1da57d55SToomas Soome 
4407c478bd9Sstevel@tonic-gate 	va_start( ap, fmt );
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
4437c478bd9Sstevel@tonic-gate 	if ( lber_debug & 64 ) {
4447c478bd9Sstevel@tonic-gate 		char msg[80];
4457c478bd9Sstevel@tonic-gate 		sprintf( msg, "ber_scanf fmt (%s) ber:\n", fmt );
4467c478bd9Sstevel@tonic-gate 		ber_err_print( msg );
4477c478bd9Sstevel@tonic-gate 		ber_dump( ber, 1 );
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	for ( rc = 0, p = (char *)fmt; *p && rc != LBER_DEFAULT; p++ ) {
4527c478bd9Sstevel@tonic-gate 		switch ( *p ) {
4537c478bd9Sstevel@tonic-gate 		case 'a':	/* octet string - allocate storage as needed */
4547c478bd9Sstevel@tonic-gate 			ss = va_arg( ap, char ** );
4557c478bd9Sstevel@tonic-gate 			rc = ber_get_stringa( ber, ss );
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		case 'b':	/* boolean */
4597c478bd9Sstevel@tonic-gate 			i = va_arg( ap, int * );
4607c478bd9Sstevel@tonic-gate 			rc = ber_get_boolean( ber, i );
4617c478bd9Sstevel@tonic-gate 			break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		case 'e':	/* enumerated */
4647c478bd9Sstevel@tonic-gate 		case 'i':	/* int */
4657c478bd9Sstevel@tonic-gate 			l = va_arg( ap, ber_slen_t * );
4667c478bd9Sstevel@tonic-gate 			rc = ber_get_int( ber, l );
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		case 'l':	/* length of next item */
4707c478bd9Sstevel@tonic-gate 			l = va_arg( ap, ber_slen_t * );
4717c478bd9Sstevel@tonic-gate 			rc = ber_peek_tag( ber, (ber_len_t *)l );
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		case 'n':	/* null */
4757c478bd9Sstevel@tonic-gate 			rc = ber_get_null( ber );
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		case 's':	/* octet string - in a buffer */
4797c478bd9Sstevel@tonic-gate 			s = va_arg( ap, char * );
4807c478bd9Sstevel@tonic-gate 			l = va_arg( ap, ber_slen_t * );
4817c478bd9Sstevel@tonic-gate 			rc = ber_get_stringb( ber, s, (ber_len_t *)l );
4827c478bd9Sstevel@tonic-gate 			break;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		case 'o':	/* octet string in a supplied berval */
4857c478bd9Sstevel@tonic-gate 			bval = va_arg( ap, struct berval * );
4867c478bd9Sstevel@tonic-gate 			ber_peek_tag( ber, &bval->bv_len );
4877c478bd9Sstevel@tonic-gate 			rc = ber_get_stringa( ber, &bval->bv_val );
4887c478bd9Sstevel@tonic-gate 			break;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		case 'O':	/* octet string - allocate & include length */
4917c478bd9Sstevel@tonic-gate 			bvp = va_arg( ap, struct berval ** );
4927c478bd9Sstevel@tonic-gate 			rc = ber_get_stringal( ber, bvp );
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		case 'B':	/* bit string - allocate storage as needed */
4967c478bd9Sstevel@tonic-gate 			ss = va_arg( ap, char ** );
4977c478bd9Sstevel@tonic-gate 			l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
4987c478bd9Sstevel@tonic-gate 			rc = ber_get_bitstringa( ber, ss, (ber_len_t *)l );
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		case 't':	/* tag of next item */
5027c478bd9Sstevel@tonic-gate 			t = va_arg( ap, ber_tag_t * );
5037c478bd9Sstevel@tonic-gate 			*t = rc = ber_peek_tag( ber, &len );
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		case 'T':	/* skip tag of next item */
5077c478bd9Sstevel@tonic-gate 			t = va_arg( ap, ber_tag_t * );
5087c478bd9Sstevel@tonic-gate 			*t = rc = ber_skip_tag( ber, &len );
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		case 'v':	/* sequence of strings */
5127c478bd9Sstevel@tonic-gate 			sss = va_arg( ap, char *** );
5137c478bd9Sstevel@tonic-gate 			*sss = NULL;
5147c478bd9Sstevel@tonic-gate 			j = 0;
5157c478bd9Sstevel@tonic-gate 			array_size = 0;
5167c478bd9Sstevel@tonic-gate 			for ( tag = ber_first_element( ber, &len, &last );
5177c478bd9Sstevel@tonic-gate 			    tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
5187c478bd9Sstevel@tonic-gate 			    && rc != LBER_DEFAULT;
5197c478bd9Sstevel@tonic-gate 			    tag = ber_next_element( ber, &len, last ) ) {
5207c478bd9Sstevel@tonic-gate 				if ( *sss == NULL ) {
5217c478bd9Sstevel@tonic-gate 				    /* Make room for at least 15 strings */
5227c478bd9Sstevel@tonic-gate 				    *sss = (char **)NSLBERI_MALLOC(16 * sizeof(char *) );
5237c478bd9Sstevel@tonic-gate 				    array_size = 16;
5247c478bd9Sstevel@tonic-gate 				} else {
5257c478bd9Sstevel@tonic-gate 				    if ( (size_t)(j+2) > array_size) {
5267c478bd9Sstevel@tonic-gate 					/* We'v overflowed our buffer */
5277c478bd9Sstevel@tonic-gate 					*sss = (char **)NSLBERI_REALLOC( *sss, (array_size * 2) * sizeof(char *) );
5287c478bd9Sstevel@tonic-gate 					array_size = array_size * 2;
5297c478bd9Sstevel@tonic-gate 				    }
5307c478bd9Sstevel@tonic-gate 				}
5317c478bd9Sstevel@tonic-gate 				rc = ber_get_stringa( ber, &((*sss)[j]) );
5327c478bd9Sstevel@tonic-gate 				j++;
5337c478bd9Sstevel@tonic-gate 			}
5347c478bd9Sstevel@tonic-gate 			if ( rc != LBER_DEFAULT &&
5357c478bd9Sstevel@tonic-gate 			    tag != LBER_END_OF_SEQORSET ) {
5367c478bd9Sstevel@tonic-gate 				rc = LBER_DEFAULT;
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 			if ( j > 0 )
5397c478bd9Sstevel@tonic-gate 				(*sss)[j] = NULL;
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		case 'V':	/* sequence of strings + lengths */
5437c478bd9Sstevel@tonic-gate 			bv = va_arg( ap, struct berval *** );
5447c478bd9Sstevel@tonic-gate 			*bv = NULL;
5457c478bd9Sstevel@tonic-gate 			j = 0;
5467c478bd9Sstevel@tonic-gate 			for ( tag = ber_first_element( ber, &len, &last );
5477c478bd9Sstevel@tonic-gate 			    tag != LBER_DEFAULT && tag != LBER_END_OF_SEQORSET
5487c478bd9Sstevel@tonic-gate 			    && rc != LBER_DEFAULT;
5497c478bd9Sstevel@tonic-gate 			    tag = ber_next_element( ber, &len, last ) ) {
5507c478bd9Sstevel@tonic-gate 				if ( *bv == NULL ) {
5517c478bd9Sstevel@tonic-gate 					*bv = (struct berval **)NSLBERI_MALLOC(
5527c478bd9Sstevel@tonic-gate 					    2 * sizeof(struct berval *) );
5537c478bd9Sstevel@tonic-gate 				} else {
5547c478bd9Sstevel@tonic-gate 					*bv = (struct berval **)NSLBERI_REALLOC(
5557c478bd9Sstevel@tonic-gate 					    *bv,
5567c478bd9Sstevel@tonic-gate 					    (j + 2) * sizeof(struct berval *) );
5577c478bd9Sstevel@tonic-gate 				}
5587c478bd9Sstevel@tonic-gate 				rc = ber_get_stringal( ber, &((*bv)[j]) );
5597c478bd9Sstevel@tonic-gate 				j++;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 			if ( rc != LBER_DEFAULT &&
5627c478bd9Sstevel@tonic-gate 			    tag != LBER_END_OF_SEQORSET ) {
5637c478bd9Sstevel@tonic-gate 				rc = LBER_DEFAULT;
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 			if ( j > 0 )
5667c478bd9Sstevel@tonic-gate 				(*bv)[j] = NULL;
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		case 'x':	/* skip the next element - whatever it is */
5707c478bd9Sstevel@tonic-gate 			if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
5717c478bd9Sstevel@tonic-gate 				break;
5727c478bd9Sstevel@tonic-gate 			ber->ber_ptr += len;
5737c478bd9Sstevel@tonic-gate 			break;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		case '{':	/* begin sequence */
5767c478bd9Sstevel@tonic-gate 		case '[':	/* begin set */
5777c478bd9Sstevel@tonic-gate 			if ( *(p + 1) != 'v' && *(p + 1) != 'V' )
5787c478bd9Sstevel@tonic-gate 				rc = ber_skip_tag( ber, &len );
5797c478bd9Sstevel@tonic-gate 			break;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		case '}':	/* end sequence */
5827c478bd9Sstevel@tonic-gate 		case ']':	/* end set */
5837c478bd9Sstevel@tonic-gate 			break;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		default:
5867c478bd9Sstevel@tonic-gate 			{
5877c478bd9Sstevel@tonic-gate 				char msg[80];
5887c478bd9Sstevel@tonic-gate 				sprintf( msg, "unknown fmt %c\n", *p );
5897c478bd9Sstevel@tonic-gate 				ber_err_print( msg );
5907c478bd9Sstevel@tonic-gate 			}
5917c478bd9Sstevel@tonic-gate 			rc = LBER_DEFAULT;
5927c478bd9Sstevel@tonic-gate 			break;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	va_end( ap );
5987c478bd9Sstevel@tonic-gate         if (rc == LBER_DEFAULT) {
5997c478bd9Sstevel@tonic-gate           va_start( ap, fmt );
6007c478bd9Sstevel@tonic-gate           for ( p--; fmt < p && *fmt; fmt++ ) {
6017c478bd9Sstevel@tonic-gate                 switch ( *fmt ) {
6027c478bd9Sstevel@tonic-gate                 case 'a':       /* octet string - allocate storage as needed */
6037c478bd9Sstevel@tonic-gate                         ss = va_arg( ap, char ** );
6047c478bd9Sstevel@tonic-gate                         NSLBERI_FREE(*ss);
6057c478bd9Sstevel@tonic-gate                         *ss = NULL;
6067c478bd9Sstevel@tonic-gate                         break;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate                 case 'b':       /* boolean */
6097c478bd9Sstevel@tonic-gate                         i = va_arg( ap, int * );
6107c478bd9Sstevel@tonic-gate                         break;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate                 case 'e':       /* enumerated */
6137c478bd9Sstevel@tonic-gate                 case 'i':       /* int */
6147c478bd9Sstevel@tonic-gate                         l = va_arg( ap, ber_slen_t * );
6157c478bd9Sstevel@tonic-gate                         break;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate                 case 'l':       /* length of next item */
6187c478bd9Sstevel@tonic-gate                         l = va_arg( ap, ber_slen_t * );
6197c478bd9Sstevel@tonic-gate                         break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate                 case 'n':       /* null */
6227c478bd9Sstevel@tonic-gate                         break;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate                 case 's':       /* octet string - in a buffer */
6257c478bd9Sstevel@tonic-gate                         s = va_arg( ap, char * );
6267c478bd9Sstevel@tonic-gate                         l = va_arg( ap, ber_slen_t * );
6277c478bd9Sstevel@tonic-gate                         break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate                 case 'o':       /* octet string in a supplied berval */
6307c478bd9Sstevel@tonic-gate                         bval = va_arg( ap, struct berval * );
6317c478bd9Sstevel@tonic-gate                         if (bval->bv_val) NSLBERI_FREE(bval->bv_val);
6327c478bd9Sstevel@tonic-gate                         memset(bval, 0, sizeof(struct berval));
6337c478bd9Sstevel@tonic-gate                         break;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate                 case 'O':       /* octet string - allocate & include length */
6367c478bd9Sstevel@tonic-gate                         bvp = va_arg( ap, struct berval ** );
6377c478bd9Sstevel@tonic-gate                         ber_bvfree(*bvp);
6387c478bd9Sstevel@tonic-gate                         bvp = NULL;
6397c478bd9Sstevel@tonic-gate                         break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate                 case 'B':       /* bit string - allocate storage as needed */
6427c478bd9Sstevel@tonic-gate                         ss = va_arg( ap, char ** );
6437c478bd9Sstevel@tonic-gate                         l = va_arg( ap, ber_slen_t * ); /* for length, in bits */
6447c478bd9Sstevel@tonic-gate                         if (*ss) NSLBERI_FREE(*ss);
6457c478bd9Sstevel@tonic-gate                         *ss = NULL;
6467c478bd9Sstevel@tonic-gate                         break;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate                 case 't':       /* tag of next item */
6497c478bd9Sstevel@tonic-gate                         t = va_arg( ap, ber_tag_t * );
6507c478bd9Sstevel@tonic-gate                         break;
6517c478bd9Sstevel@tonic-gate                 case 'T':       /* skip tag of next item */
6527c478bd9Sstevel@tonic-gate                         t = va_arg( ap, ber_tag_t * );
6537c478bd9Sstevel@tonic-gate                         break;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate                 case 'v':       /* sequence of strings */
6567c478bd9Sstevel@tonic-gate                         sss = va_arg( ap, char *** );
6577c478bd9Sstevel@tonic-gate                         ber_svecfree(*sss);
6587c478bd9Sstevel@tonic-gate                         *sss = NULL;
6597c478bd9Sstevel@tonic-gate                         break;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate                 case 'V':       /* sequence of strings + lengths */
6627c478bd9Sstevel@tonic-gate                         bv = va_arg( ap, struct berval *** );
6637c478bd9Sstevel@tonic-gate                         ber_bvecfree(*bv);
6647c478bd9Sstevel@tonic-gate                         *bv = NULL;
6657c478bd9Sstevel@tonic-gate                         break;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate                 case 'x':       /* skip the next element - whatever it is */
6687c478bd9Sstevel@tonic-gate                         break;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate                 case '{':       /* begin sequence */
6717c478bd9Sstevel@tonic-gate                 case '[':       /* begin set */
6727c478bd9Sstevel@tonic-gate                         break;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate                 case '}':       /* end sequence */
6757c478bd9Sstevel@tonic-gate                 case ']':       /* end set */
6767c478bd9Sstevel@tonic-gate                         break;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate                 default:
6797c478bd9Sstevel@tonic-gate                         break;
6807c478bd9Sstevel@tonic-gate                 }
6817c478bd9Sstevel@tonic-gate           } /* for */
6827c478bd9Sstevel@tonic-gate           va_end( ap );
6837c478bd9Sstevel@tonic-gate         } /* if */
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	return( rc );
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate void
6907c478bd9Sstevel@tonic-gate LDAP_CALL
ber_bvfree(struct berval * bv)6917c478bd9Sstevel@tonic-gate ber_bvfree( struct berval *bv )
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	if ( bv != NULL ) {
6947c478bd9Sstevel@tonic-gate 		if ( bv->bv_val != NULL ) {
6957c478bd9Sstevel@tonic-gate 			NSLBERI_FREE( bv->bv_val );
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 		NSLBERI_FREE( (char *) bv );
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate void
7027c478bd9Sstevel@tonic-gate LDAP_CALL
ber_bvecfree(struct berval ** bv)7037c478bd9Sstevel@tonic-gate ber_bvecfree( struct berval **bv )
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	int	i;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if ( bv != NULL ) {
7087c478bd9Sstevel@tonic-gate 		for ( i = 0; bv[i] != NULL; i++ ) {
7097c478bd9Sstevel@tonic-gate 			ber_bvfree( bv[i] );
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 		NSLBERI_FREE( (char *) bv );
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate struct berval *
7167c478bd9Sstevel@tonic-gate LDAP_CALL
ber_bvdup(const struct berval * bv)7177c478bd9Sstevel@tonic-gate ber_bvdup( const struct berval *bv )
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	struct berval	*new;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if ( (new = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
7227c478bd9Sstevel@tonic-gate 	    == NULL ) {
7237c478bd9Sstevel@tonic-gate 		return( NULL );
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	if ( bv->bv_val == NULL ) {
7267c478bd9Sstevel@tonic-gate 	    new->bv_val = NULL;
7277c478bd9Sstevel@tonic-gate 	    new->bv_len = 0;
7287c478bd9Sstevel@tonic-gate 	} else {
7297c478bd9Sstevel@tonic-gate 	    if ( (new->bv_val = (char *)NSLBERI_MALLOC( bv->bv_len + 1 ))
7307c478bd9Sstevel@tonic-gate 		== NULL ) {
7317c478bd9Sstevel@tonic-gate 		    return( NULL );
7327c478bd9Sstevel@tonic-gate 	    }
7337c478bd9Sstevel@tonic-gate 	    SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
7347c478bd9Sstevel@tonic-gate 	    new->bv_val[bv->bv_len] = '\0';
7357c478bd9Sstevel@tonic-gate 	    new->bv_len = bv->bv_len;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	return( new );
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate void
7427c478bd9Sstevel@tonic-gate LDAP_CALL
ber_svecfree(char ** vals)7437c478bd9Sstevel@tonic-gate ber_svecfree( char **vals )
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate         int     i;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate         if ( vals == NULL )
7487c478bd9Sstevel@tonic-gate                 return;
7497c478bd9Sstevel@tonic-gate         for ( i = 0; vals[i] != NULL; i++ )
7507c478bd9Sstevel@tonic-gate                 NSLBERI_FREE( vals[i] );
7517c478bd9Sstevel@tonic-gate         NSLBERI_FREE( (char *) vals );
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
7557c478bd9Sstevel@tonic-gate void
7567c478bd9Sstevel@tonic-gate LDAP_CALL
ber_set_string_translators(BerElement * ber,BERTranslateProc encode_proc,BERTranslateProc decode_proc)7577c478bd9Sstevel@tonic-gate ber_set_string_translators(
7587c478bd9Sstevel@tonic-gate     BerElement		*ber,
7597c478bd9Sstevel@tonic-gate     BERTranslateProc	encode_proc,
7607c478bd9Sstevel@tonic-gate     BERTranslateProc	decode_proc
7617c478bd9Sstevel@tonic-gate )
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate     ber->ber_encode_translate_proc = encode_proc;
7647c478bd9Sstevel@tonic-gate     ber->ber_decode_translate_proc = decode_proc;
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
767