]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/memory.c
Fix typo
[openldap] / libraries / liblber / memory.c
index 8cdad0ec8b0a7bee67d9fedd2c550d92b978eaf1..5b90650868ba19b1f64f7813d2f3e7144b38c6f8 100644 (file)
@@ -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 )
 {
@@ -634,22 +674,32 @@ ber_bvarray_add( BerVarray *a, BerValue *bv )
                        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++ ) {
-                       ;       /* NULL */
+                       ;       /* just count them */
                }
 
                if (bv == NULL) {
                        return n;
                }
-               *a = (BerValue *) LBER_REALLOC( (char *) *a,
+
+               atmp = (BerValue *) LBER_REALLOC( (char *) *a,
                    (n + 2) * sizeof(BerValue) );
-       }
-       if ( *a == NULL ) {
-               return -1;
+
+               if( atmp == NULL ) {
+                       return -1;
+               }
+
+               *a = atmp;
        }
 
        (*a)[n++] = *bv;
@@ -657,3 +707,4 @@ ber_bvarray_add( BerVarray *a, BerValue *bv )
 
        return n;
 }
+