/* $OpenLDAP$ */
/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
BerMemoryFunctions *ber_int_memory_fns = NULL;
-#if 0 && defined( LDAP_MEMORY_DEBUG )
-void
-ber_int_memfree( void **p )
-{
- assert( p != NULL );
- BER_MEM_VALID( *p );
-
- ber_memfree( p );
-
- *p = BER_MEM_BADADDR;
-}
-#endif
-
void
ber_memfree( void *p )
{
/* realloc(NULL,s) -> malloc(s) */
if( p == NULL ) {
- return ber_memalloc( s );
+ return LBER_MALLOC( s );
}
/* realloc(p,0) -> free(p) */
if( s == 0 ) {
- ber_memfree( p );
+ LBER_FREE( p );
return NULL;
}
return 0;
}
- *bvec = ber_memalloc( 2 * sizeof(struct berval *) );
+ *bvec = LBER_MALLOC( 2 * sizeof(struct berval *) );
if( *bvec == NULL ) {
return -1;
return i;
}
- new = ber_memrealloc( *bvec, (i+2) * sizeof(struct berval *));
+ new = LBER_REALLOC( *bvec, (i+2) * sizeof(struct berval *));
if( new == NULL ) {
return -1;
struct berval *
ber_dupbv(
- struct berval *dst, LDAP_CONST struct berval *src )
+ struct berval *dst, struct berval *src )
{
struct berval *new;
return new;
}
+
+struct berval *
+ber_bvdup(
+ struct berval *src )
+{
+ return ber_dupbv( NULL, src );
+}
+
+
struct berval *
ber_str2bv(
- LDAP_CONST char *s, int dup, struct berval *bv )
+ LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
{
struct berval *new;
}
}
- new->bv_len = strlen( s );
+ new->bv_len = len ? len : strlen( s );
+ 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 );
+}
+
+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 || len == 0 ) {
+ 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;
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) );
+ } else {
+ BER_MEM_VALID( a );
+
+ for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
+ ; /* NULL */
+ }
+
+ if (bv == NULL) {
+ return n;
+ }
+ *a = (BerValue *) LBER_REALLOC( (char *) *a,
+ (n + 2) * sizeof(BerValue) );
+ }
+ if ( *a == NULL ) {
+ return -1;
+ }
+
+ (*a)[n++] = *bv;
+ (*a)[n].bv_val = NULL;
+
+ return n;
+}
+