X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Fmemory.c;h=5b90650868ba19b1f64f7813d2f3e7144b38c6f8;hb=517900ec9bc4f9cf97d3194d5c613adc07dd6b51;hp=de372348e01ac28ee3ff14a463632341614930c8;hpb=c36352c17374b72d0a6c6b4cdc3a21a66380a4ef;p=openldap diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c index de372348e0..5b90650868 100644 --- a/libraries/liblber/memory.c +++ b/libraries/liblber/memory.c @@ -1,6 +1,6 @@ /* $OpenLDAP$ */ /* - * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved. + * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ #include "portable.h" @@ -53,7 +53,7 @@ struct ber_mem_hdr { }; /* Pattern at top of allocated space */ -#define LLBER_MEM_JUNK 0xdeaddadaU +#define LBER_MEM_JUNK 0xdeaddadaU static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 }; @@ -523,6 +523,46 @@ ber_str2bv( return( new ); } +struct berval * +ber_mem2bv( + LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv) +{ + struct berval *new; + + ber_int_options.lbo_valid = LBER_INITIALIZED; + + if( s == NULL ) { + ber_errno = LBER_ERROR_PARAM; + return NULL; + } + + if( bv ) { + new = bv; + } else { + if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) { + ber_errno = LBER_ERROR_MEMORY; + return NULL; + } + } + + new->bv_len = len; + if ( dup ) { + if ( (new->bv_val = LBER_MALLOC( new->bv_len+1 )) == NULL ) { + ber_errno = LBER_ERROR_MEMORY; + if ( !bv ) + LBER_FREE( new ); + return NULL; + } + + AC_MEMCPY( new->bv_val, s, new->bv_len ); + new->bv_val[new->bv_len] = '\0'; + } else { + new->bv_val = (char *) s; + } + + return( new ); +} + char * ber_strdup( LDAP_CONST char *s ) { @@ -603,3 +643,68 @@ ber_strndup__( LDAP_CONST char *s, size_t l ) p[ len ] = '\0'; return p; } + +void +ber_bvarray_free( BerVarray a ) +{ + int i; + + ber_int_options.lbo_valid = LBER_INITIALIZED; + + if (a) { + BER_MEM_VALID( a ); + + for (i=0; a[i].bv_val; i++) { + LBER_FREE(a[i].bv_val); + } + + LBER_FREE(a); + } +} + +int +ber_bvarray_add( BerVarray *a, BerValue *bv ) +{ + int n; + + ber_int_options.lbo_valid = LBER_INITIALIZED; + + if ( *a == NULL ) { + if (bv == NULL) { + return 0; + } + n = 0; + + *a = (BerValue *) LBER_MALLOC( 2 * sizeof(BerValue) ); + if ( *a == NULL ) { + return -1; + } + + } else { + BerVarray atmp; + BER_MEM_VALID( a ); + + for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) { + ; /* just count them */ + } + + if (bv == NULL) { + return n; + } + + atmp = (BerValue *) LBER_REALLOC( (char *) *a, + (n + 2) * sizeof(BerValue) ); + + if( atmp == NULL ) { + return -1; + } + + *a = atmp; + } + + (*a)[n++] = *bv; + (*a)[n].bv_val = NULL; + + return n; +} +