]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/memory.c
Coverted LDAP_LOG macro to use subsystem ID int values instead of string values
[openldap] / libraries / liblber / memory.c
index 4766a21f23addd7f27d8f8ad09ce056e1c864793..599a542988ad757e9e3b342b4f1265fcaaec65e6 100644 (file)
@@ -1,6 +1,6 @@
 /* $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"
@@ -112,19 +112,6 @@ static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
 
 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 )
 {
@@ -294,12 +281,12 @@ ber_memrealloc( void* p, ber_len_t s )
 
        /* 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;
        }
 
@@ -407,7 +394,7 @@ ber_bvecadd( struct berval ***bvec, struct berval *bv )
                        return 0;
                }
 
-               *bvec = ber_memalloc( 2 * sizeof(struct berval *) );
+               *bvec = LBER_MALLOC( 2 * sizeof(struct berval *) );
 
                if( *bvec == NULL ) {
                        return -1;
@@ -430,7 +417,7 @@ ber_bvecadd( struct berval ***bvec, struct berval *bv )
                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;
@@ -447,7 +434,7 @@ ber_bvecadd( struct berval ***bvec, struct berval *bv )
 
 struct berval *
 ber_dupbv(
-       struct berval *dst, LDAP_CONST struct berval *src )
+       struct berval *dst, struct berval *src )
 {
        struct berval *new;
 
@@ -487,9 +474,18 @@ ber_dupbv(
        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;
 
@@ -509,7 +505,47 @@ ber_str2bv(
                }
        }
 
-       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;
@@ -607,3 +643,58 @@ 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) );
+       } 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;
+}
+