]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getdn.c
Sync with HEAD
[openldap] / libraries / libldap / getdn.c
index 4d4b29133cf7d65a5b43f5c69e61891cdf5af3d3..e66f6175be5689413aea883efc141c6afe1d83ff 100644 (file)
@@ -1,13 +1,19 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-/*  Portions
- *  Copyright (c) 1994 Regents of the University of Michigan.
- *  All rights reserved.
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2003 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
- *  getdn.c
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1994 Regents of the University of Michigan.
+ * All rights reserved.
  */
 
 #include "portable.h"
 #include <stdio.h>
 
 #include <ac/stdlib.h>
-
-#include <ac/ctype.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
 
 #include "ldap-int.h"
+#include "ldap_schema.h"
 
 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
  * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
 #define DC_IN_UFN
-
-static int dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout );
-
-/* from libraries/libldap/schema.c */
-extern char * parse_numericoid(const char **sp, int *code, const int flags);
+#define PRETTY_ESCAPE
 
 /* parsing/printing routines */
-static int str2strval( const char *str, struct berval **val, 
-               const char **next, unsigned flags, unsigned *retFlags );
-static int DCE2strval( const char *str, struct berval **val, 
-               const char **next, unsigned flags );
-static int IA52strval( const char *str, struct berval **val, 
-               const char **next, unsigned flags );
-static int quotedIA52strval( const char *str, struct berval **val, 
-               const char **next, unsigned flags );
-static int hexstr2binval( const char *str, struct berval **val, 
-               const char **next, unsigned flags );
+static int str2strval( const char *str, ber_len_t stoplen, struct berval *val, 
+               const char **next, unsigned flags, int *retFlags, void *ctx );
+static int DCE2strval( const char *str, struct berval *val, 
+               const char **next, unsigned flags, void *ctx );
+static int IA52strval( const char *str, struct berval *val, 
+               const char **next, unsigned flags, void *ctx );
+static int quotedIA52strval( const char *str, struct berval *val, 
+               const char **next, unsigned flags, void *ctx );
+static int hexstr2binval( const char *str, struct berval *val, 
+               const char **next, unsigned flags, void *ctx );
 static int hexstr2bin( const char *str, char *c );
 static int byte2hexpair( const char *val, char *pair );
 static int binval2hexstr( struct berval *val, char *str );
@@ -62,29 +63,23 @@ static int strval2ADstrlen( struct berval *val, unsigned flags,
                ber_len_t *len );
 static int strval2ADstr( struct berval *val, char *str, unsigned flags, 
                ber_len_t *len );
-static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
+static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
 
 /* AVA helpers */
 static LDAPAVA * ldapava_new(
-       const struct berval *attr, const struct berval *val, unsigned flags );
-static LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
-static LDAPRDN * ldapava_insert_into_rdn(
-       LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
-static LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
-static LDAPDN * ldapava_insert_into_dn(
-       LDAPDN *dn, LDAPRDN *rdn, unsigned where );
+       const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
 
 /* Higher level helpers */
-static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
+static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
                int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
-static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
+static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
                int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
-static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len  );
-static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
-static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
-static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
-static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
-static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
+static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len  );
+static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
+static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
+static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
+static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
+static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
 
 /*
  * RFC 1823 ldap_get_dn
@@ -95,12 +90,15 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
        char            *dn;
        BerElement      tmp;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
+#endif
 
-       if ( entry == NULL ) {
-               ld->ld_errno = LDAP_PARAM_ERROR;
-               return( NULL );
-       }
+       assert( ld != NULL );
+       assert( LDAP_VALID(ld) );
+       assert( entry != NULL );
 
        tmp = *entry->lm_ber;   /* struct copy */
        if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
@@ -111,6 +109,57 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
        return( dn );
 }
 
+int
+ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
+       BerValue *dn )
+{
+       BerElement      tmp, *ber;
+       ber_len_t       len = 0;
+       int rc = LDAP_SUCCESS;
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
+#else
+       Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
+#endif
+
+       assert( ld != NULL );
+       assert( LDAP_VALID(ld) );
+       assert( entry != NULL );
+       assert( dn != NULL );
+
+       dn->bv_val = NULL;
+       dn->bv_len = 0;
+
+       if ( berout ) {
+               *berout = NULL;
+               ber = ldap_alloc_ber_with_options( ld );
+               if( ber == NULL ) {
+                       return LDAP_NO_MEMORY;
+               }
+               *berout = ber;
+       } else {
+               ber = &tmp;
+       }
+               
+       *ber = *entry->lm_ber;  /* struct copy */
+       if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
+               rc = ld->ld_errno = LDAP_DECODING_ERROR;
+       }
+       if ( rc == LDAP_SUCCESS ) {
+               /* set the length to avoid overrun */
+               rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
+               if( rc != LBER_OPT_SUCCESS ) {
+                       rc = ld->ld_errno = LDAP_LOCAL_ERROR;
+               }
+       }
+       if ( rc != LDAP_SUCCESS && berout ) {
+               ber_free( ber, 0 );
+               *berout = NULL;
+       }
+       return rc;
+}
+
 /*
  * RFC 1823 ldap_dn2ufn
  */
@@ -119,9 +168,14 @@ ldap_dn2ufn( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
+#endif
 
-       ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_UFN );
+       ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
+               &out, LDAP_DN_FORMAT_UFN );
        
        return( out );
 }
@@ -132,83 +186,100 @@ ldap_dn2ufn( LDAP_CONST char *dn )
 char **
 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
 {
-       LDAPDN  *tmpDN;
+       LDAPDN  tmpDN;
        char    **values = NULL;
        int     iRDN;
        unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
        
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
+#endif
 
        if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP ) 
                        != LDAP_SUCCESS ) {
-               return( NULL );
+               return NULL;
        }
 
-       for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
-               char    *str, **v = NULL;
-               
-               ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
+       if( tmpDN == NULL ) {
+               values = LDAP_MALLOC( sizeof( char * ) );
+               if( values == NULL ) return NULL;
 
-               v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
-               if ( v == NULL ) {
-                       LBER_VFREE( values );
-                       ldap_dnfree( tmpDN );
-                       return( NULL );
-               }
-               values = v;
-               values[ iRDN ] = str;
+               values[0] = NULL;
+               return values;
+       }
+
+       for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
+
+       values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
+       if ( values == NULL ) {
+               ldap_dnfree( tmpDN );
+               return NULL;
        }
+
+       for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
+               ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
+       }
+       ldap_dnfree( tmpDN );
        values[ iRDN ] = NULL;
 
-       return( values );
+       return values;
 }
 
 char **
 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
 {
-       LDAPDN  *tmpDN;
-       char    **values = NULL;
-       int     iAVA;
-       unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
+       LDAPRDN         tmpRDN;
+       char            **values = NULL;
+       const char      *p;
+       int             iAVA;
        
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
+#endif
 
        /*
-        * we assume this dn is made of one rdn only
+        * we only parse the first rdn
+        * FIXME: we prefer efficiency over checking if the _ENTIRE_
+        * dn can be parsed
         */
-       if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAP ) 
+       if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP ) 
                        != LDAP_SUCCESS ) {
                return( NULL );
        }
 
-       for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
+       for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
+       values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
+       if ( values == NULL ) {
+               ldap_rdnfree( tmpRDN );
+               return( NULL );
+       }
+
+       for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
                ber_len_t       l = 0, vl, al = 0;
-               char            *str, **v = NULL;
-               LDAPAVA         *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
-               
-               v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
-               if ( v == NULL ) {
-                       goto error_return;
-               }
-               values = v;
+               char            *str;
+               LDAPAVA         *ava = tmpRDN[ iAVA ];
                
-               if ( ava->la_flags == LDAP_AVA_BINARY ) {
-                       vl = 1 + 2 * ava->la_value->bv_len;
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
+                       vl = 1 + 2 * ava->la_value.bv_len;
 
                } else {
-                       if ( strval2strlen( ava->la_value, 
+                       if ( strval2strlen( &ava->la_value, 
                                                ava->la_flags, &vl ) ) {
                                goto error_return;
                        }
                }
                
                if ( !notypes ) {
-                       al = ava->la_attr->bv_len;
-                       l = vl + ava->la_attr->bv_len + 1;
+                       al = ava->la_attr.bv_len;
+                       l = vl + ava->la_attr.bv_len + 1;
 
                        str = LDAP_MALLOC( l + 1 );
-                       AC_MEMCPY( str, ava->la_attr->bv_val, 
-                                       ava->la_attr->bv_len );
+                       AC_MEMCPY( str, ava->la_attr.bv_val, 
+                                       ava->la_attr.bv_len );
                        str[ al++ ] = '=';
 
                } else {
@@ -216,14 +287,14 @@ ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
                        str = LDAP_MALLOC( l + 1 );
                }
                
-               if ( ava->la_flags == LDAP_AVA_BINARY ) {
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ al++ ] = '#';
-                       if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
+                       if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
                                goto error_return;
                        }
 
                } else {
-                       if ( strval2str( ava->la_value, &str[ al ], 
+                       if ( strval2str( &ava->la_value, &str[ al ], 
                                        ava->la_flags, &vl ) ) {
                                goto error_return;
                        }
@@ -234,13 +305,13 @@ ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
        }
        values[ iAVA ] = NULL;
 
-       ldap_dnfree( tmpDN );
+       ldap_rdnfree( tmpRDN );
 
        return( values );
 
 error_return:;
        LBER_VFREE( values );
-       ldap_dnfree( tmpDN );
+       ldap_rdnfree( tmpRDN );
        return( NULL );
 }
 
@@ -249,9 +320,14 @@ ldap_dn2dcedn( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
+#endif
 
-       ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_DCE );
+       ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
+                                  &out, LDAP_DN_FORMAT_DCE );
 
        return( out );
 }
@@ -261,9 +337,13 @@ ldap_dcedn2dn( LDAP_CONST char *dce )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
+#endif
 
-       ( void )dn2dn( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
+       ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
 
        return( out );
 }
@@ -273,26 +353,20 @@ ldap_dn2ad_canonical( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
+#endif
 
-       ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, 
+       ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
                       &out, LDAP_DN_FORMAT_AD_CANONICAL );
 
        return( out );
 }
 
-int
-ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags ) 
-{
-       assert( out );
-
-       Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
-
-       return dn2dn( in, iflags, out, oflags);
-}
-
 /*
- * helper that changes the string representation of dnin
+ * function that changes the string representation of dnin
  * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
  * 
  * fin can be one of:
@@ -308,11 +382,18 @@ ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags
  *     LDAP_DN_FORMAT_UFN              (rfc 1781, partial and with extensions)
  *     LDAP_DN_FORMAT_AD_CANONICAL     (?)
  */
-static int
-dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
+int
+ldap_dn_normalize( LDAP_CONST char *dnin,
+       unsigned fin, char **dnout, unsigned fout )
 {
        int     rc;
-       LDAPDN  *tmpDN = NULL;
+       LDAPDN  tmpDN = NULL;
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
+#else
+       Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
+#endif
 
        assert( dnout );
 
@@ -352,28 +433,28 @@ dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
 #define B4IA5VALUE             0x0040
 #define B4BINARYVALUE          0x0050
 
-/* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
+/*
+ * Helpers (mostly from slap.h)
+ * c is assumed to Unicode in an ASCII compatible format (UTF-8)
+ * Macros assume "C" Locale (ASCII)
+ */
 #define LDAP_DN_ASCII_SPACE(c) \
        ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
-#define LDAP_DN_ASCII_LOWER(c)         ( (c) >= 'a' && (c) <= 'z' )
-#define LDAP_DN_ASCII_UPPER(c)         ( (c) >= 'A' && (c) <= 'Z' )
-#define LDAP_DN_ASCII_ALPHA(c) \
-       ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
-#define LDAP_DN_ASCII_DIGIT(c)         ( (c) >= '0' && (c) <= '9' )
-#define LDAP_DN_ASCII_LCASE_HEXALPHA(c)        ( (c) >= 'a' && (c) <= 'f' )
-#define LDAP_DN_ASCII_UCASE_HEXALPHA(c)        ( (c) >= 'A' && (c) <= 'F' )
-#define LDAP_DN_ASCII_HEXDIGIT(c) \
-       ( LDAP_DN_ASCII_DIGIT(c) \
-         || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
-         || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
-#define LDAP_DN_ASCII_ALNUM(c) \
-       ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
+#define LDAP_DN_ASCII_LOWER(c)         LDAP_LOWER(c)
+#define LDAP_DN_ASCII_UPPER(c)         LDAP_UPPER(c)
+#define LDAP_DN_ASCII_ALPHA(c)         LDAP_ALPHA(c)
+
+#define LDAP_DN_ASCII_DIGIT(c)         LDAP_DIGIT(c)
+#define LDAP_DN_ASCII_LCASE_HEXALPHA(c)        LDAP_HEXLOWER(c)
+#define LDAP_DN_ASCII_UCASE_HEXALPHA(c)        LDAP_HEXUPPER(c)
+#define LDAP_DN_ASCII_HEXDIGIT(c)      LDAP_HEX(c)
+#define LDAP_DN_ASCII_ALNUM(c)         LDAP_ALNUM(c)
 #define LDAP_DN_ASCII_PRINTABLE(c)     ( (c) >= ' ' && (c) <= '~' )
 
 /* attribute type */
-#define LDAP_DN_OID_LEADCHAR(c)                ( LDAP_DN_ASCII_DIGIT(c) )
-#define LDAP_DN_DESC_LEADCHAR(c)       ( LDAP_DN_ASCII_ALPHA(c) )
-#define LDAP_DN_DESC_CHAR(c)           ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
+#define LDAP_DN_OID_LEADCHAR(c)                LDAP_DIGIT(c)
+#define LDAP_DN_DESC_LEADCHAR(c)       LDAP_ALPHA(c)
+#define LDAP_DN_DESC_CHAR(c)           LDAP_LDH(c)
 #define LDAP_DN_LANG_SEP(c)            ( (c) == ';' )
 #define LDAP_DN_ATTRDESC_CHAR(c) \
        ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
@@ -391,16 +472,18 @@ dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
 #define LDAP_DN_NE(c) \
        ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
          || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
+#define LDAP_DN_MAYESCAPE(c) \
+       ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
+         || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
 #define LDAP_DN_NEEDESCAPE(c) \
        ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
-#define LDAP_DN_NEEDESCAPE_LEAD(c) \
-       ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
+#define LDAP_DN_NEEDESCAPE_LEAD(c)     LDAP_DN_MAYESCAPE(c)
 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
        ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
-#define LDAP_DN_WILLESCAPE_CHAR( c) \
-       ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
-#define LDAP_DN_IS_PRETTY( f )         ( (f) & LDAP_DN_PRETTY )
-#define LDAP_DN_WILLESCAPE(f, c) \
+#define LDAP_DN_WILLESCAPE_CHAR(c) \
+       ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
+#define LDAP_DN_IS_PRETTY(f)           ( (f) & LDAP_DN_PRETTY )
+#define LDAP_DN_WILLESCAPE_HEX(f, c) \
        ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
 
 /* LDAPv2 */
@@ -455,17 +538,23 @@ dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
 /* generics */
 #define LDAP_DN_HEXPAIR(s) \
        ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
-#define        LDAP_DC_ATTR                    "dc"
 /* better look at the AttributeDescription? */
 
 /* FIXME: no composite rdn or non-"dc" types, right?
  * (what about "dc" in OID form?) */
 /* FIXME: we do not allow binary values in domain, right? */
 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
-#define LDAP_DN_IS_RDN_DC( rdn ) \
-       ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
-         && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
-         && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
+/* NOTE: don't use strcasecmp() as it is locale specific! */
+#define        LDAP_DC_ATTR    "dc"
+#define        LDAP_DC_ATTRU   "DC"
+#define LDAP_DN_IS_RDN_DC( r ) \
+       ( (r) && (r)[0] && !(r)[1] \
+         && ((r)[0]->la_flags & LDAP_AVA_STRING) \
+         && ((r)[0]->la_attr.bv_len == 2) \
+         && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
+               || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
+         && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
+               || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
 
 /* Composite rules */
 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
@@ -494,100 +583,56 @@ dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
  */
 LDAPAVA *
 ldapava_new( const struct berval *attr, const struct berval *val, 
-               unsigned flags )
+               unsigned flags, void *ctx )
 {
-       LDAPAVA *ava;
+       LDAPAVA *ava;
 
        assert( attr );
        assert( val );
 
-       ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
-       
-       /* should we test it? */
-       if ( ava == NULL ) {
-               return( NULL );
-       }
+       ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
 
-       ava->la_attr = ( struct berval * )attr;
-       ava->la_value = ( struct berval * )val;
-       ava->la_flags = flags;
+       if ( ava ) {
+               ava->la_attr.bv_len = attr->bv_len;
+               ava->la_attr.bv_val = (char *)(ava+1);
+               AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
+               ava->la_attr.bv_val[attr->bv_len] = '\0';
 
-       ava->la_private = NULL;
+               ava->la_value = *val;
+               ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
+
+               ava->la_private = NULL;
+       }
 
        return( ava );
 }
 
 void
-ldap_avafree( LDAPAVA *ava )
+ldapava_free( LDAPAVA *ava, void *ctx )
 {
        assert( ava );
 
 #if 0
-       /* ava's private must be freed by caller */
-       assert( ava->la_private != NULL );
+       /* ava's private must be freed by caller
+        * (at present let's skip this check because la_private
+        * basically holds static data) */
+       assert( ava->la_private == NULL );
 #endif
 
-       ber_bvfree( ava->la_attr );
-       ber_bvfree( ava->la_value );
+       if (ava->la_flags & LDAP_AVA_FREE_VALUE)
+               LDAP_FREEX( ava->la_value.bv_val, ctx );
 
-       LDAP_FREE( ava );
+       LDAP_FREEX( ava, ctx );
 }
 
-LDAPRDN *
-ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
-{
-       LDAPRDN         *newRDN;
-       unsigned        i = 0U;
-
-       assert( ava );
-
-       if ( rdn != NULL ) {
-               for ( i = 0U; rdn[ i ]; i++ ) {
-                       /* no op */
-               }
-       }
-       newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
-       newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
-       newRDN[ i ][ 0 ] = ava;
-       newRDN[ i + 1 ] = NULL;
-
-       return( newRDN );
-}
-
-LDAPRDN *
-ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
+void
+ldap_rdnfree( LDAPRDN rdn )
 {
-       LDAPRDN         *newRDN;
-       unsigned        i = 0U;
-
-       assert( ava );
-
-       if ( rdn != NULL ) {
-               for ( i = 0U; rdn[ i ]; i++ ) {
-                       /* no op */
-               }
-       }
-       if ( where > i ) {
-               where = i;
-               /* assume "at end", which corresponds to
-                * ldapava_append_to_rdn */
-       }
-       
-       newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
-       
-       /* data after insert point */
-       AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
-                       ( i - where ) * sizeof( LDAPRDN * ) );
-
-       newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
-       newRDN[ where ][ 0 ] = ava;
-       newRDN[ i + 1 ] = NULL;
-
-       return( newRDN );
+       ldap_rdnfree_x( rdn, NULL );
 }
 
 void
-ldap_rdnfree( LDAPRDN *rdn )
+ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
 {
        int iAVA;
        
@@ -596,69 +641,20 @@ ldap_rdnfree( LDAPRDN *rdn )
        }
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               assert( rdn[ iAVA ][ 0 ] );
-
-               ldap_avafree( rdn[ iAVA ][ 0 ] );
-       }
-
-       LDAP_VFREE( rdn );
-}
-
-LDAPDN *
-ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
-{
-       LDAPDN          *newDN;
-       unsigned        i = 0U;
-
-       assert( rdn );
-
-       if ( dn != NULL ) {
-               for ( i = 0U; dn[ i ]; i++ ) {
-                       /* no op */
-               }
+               ldapava_free( rdn[ iAVA ], ctx );
        }
-       newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
-       newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
-       newDN[ i ][ 0 ] = rdn;
-       newDN[ i + 1 ] = NULL;
 
-       return( newDN );
+       LDAP_FREEX( rdn, ctx );
 }
 
-LDAPDN *
-ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
+void
+ldap_dnfree( LDAPDN dn )
 {
-       LDAPDN          *newDN;
-       unsigned        i = 0U;
-
-       assert( rdn );
-
-       if ( dn != NULL ) {
-               for ( i = 0U; dn[ i ]; i++ ) {
-                       /* no op */
-               }
-       }
-       if ( where > i ) {
-               where = i;
-               /* assume "at end", which corresponds to
-                * ldapava_append_to_dn */
-       }
-       
-       newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
-       
-       /* data after insert point */
-       AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
-                       ( i - where ) * sizeof( LDAPDN * ) );
-
-       newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
-       newDN[ where ][ 0 ] = rdn;
-       newDN[ i + 1 ] = NULL;
-
-       return( newDN );
+       ldap_dnfree_x( dn, NULL );
 }
 
 void
-ldap_dnfree( LDAPDN *dn )
+ldap_dnfree_x( LDAPDN dn, void *ctx )
 {
        int iRDN;
        
@@ -667,12 +663,10 @@ ldap_dnfree( LDAPDN *dn )
        }
 
        for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
-               assert( dn[ iRDN ][ 0 ] );
-
-               ldap_rdnfree( dn[ iRDN ][ 0 ] );
+               ldap_rdnfree_x( dn[ iRDN ], ctx );
        }
 
-       LDAP_VFREE( dn );
+       LDAP_FREEX( dn, ctx );
 }
 
 /*
@@ -689,19 +683,56 @@ ldap_dnfree( LDAPDN *dn )
  * and readable as soon as it works as expected.
  */
 
+/*
+ * Default sizes of AVA and RDN static working arrays; if required
+ * the are dynamically resized.  The values can be tuned in case
+ * of special requirements (e.g. very deep DN trees or high number 
+ * of AVAs per RDN).
+ */
+#define        TMP_AVA_SLOTS   8
+#define        TMP_RDN_SLOTS   32
+
 int
-ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
+ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
 {
-       const char      *p;
-       int             rc = LDAP_INVALID_DN_SYNTAX;
+       struct berval   bv;
 
-       LDAPDN          *newDN = NULL;
-       LDAPRDN         *newRDN = NULL;
-       
        assert( str );
+
+       bv.bv_len = strlen( str );
+       bv.bv_val = (char *) str;
+       
+       return ldap_bv2dn_x( &bv, dn, flags, NULL );
+}
+
+int
+ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
+{
+       return ldap_bv2dn_x( bv, dn, flags, NULL );
+}
+
+int
+ldap_bv2dn_x( struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )
+{
+       const char      *p;
+       int             rc = LDAP_DECODING_ERROR;
+       int             nrdns = 0;
+
+       LDAPDN          newDN = NULL;
+       LDAPRDN         newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
+       int             num_slots = TMP_RDN_SLOTS;
+       char            *str = bv->bv_val;
+       char            *end = str + bv->bv_len;
+       
+       assert( bv );
+       assert( bv->bv_val );
        assert( dn );
 
-       Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
+#else
+       Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
+#endif
 
        *dn = NULL;
 
@@ -715,14 +746,20 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
        /* unsupported in str2dn */
        case LDAP_DN_FORMAT_UFN:
        case LDAP_DN_FORMAT_AD_CANONICAL:
-               return( LDAP_INVALID_DN_SYNTAX );
+               return LDAP_PARAM_ERROR;
 
+       case LDAP_DN_FORMAT_LBER:
        default:
-               return( LDAP_OTHER );
+               return LDAP_PARAM_ERROR;
        }
 
-       if ( str[ 0 ] == '\0' ) {
-               return( LDAP_SUCCESS );
+       if ( bv->bv_len == 0 ) {
+               return LDAP_SUCCESS;
+       }
+
+       if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
+               /* value must have embedded NULs */
+               return LDAP_DECODING_ERROR;
        }
 
        p = str;
@@ -736,7 +773,12 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
                        goto parsing_error;
                }
                p++;
-               
+
+       /*
+        * actually we do not want to accept by default the DCE form,
+        * we do not want to auto-detect it
+        */
+#if 0
        } else if ( LDAP_DN_LDAP( flags ) ) {
                /*
                 * if dn starts with '/' let's make it a DCE dn
@@ -745,13 +787,16 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
                        flags |= LDAP_DN_FORMAT_DCE;
                        p++;
                }
+#endif
        }
 
-       for ( ; p[ 0 ]; p++ ) {
-               LDAPDN          *dn;
+       for ( ; p < end; p++ ) {
                int             err;
+               struct berval   tmpbv;
+               tmpbv.bv_len = bv->bv_len - ( p - str );
+               tmpbv.bv_val = (char *)p;
                
-               err = ldap_str2rdn( p, &newRDN, &p, flags );
+               err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
                if ( err != LDAP_SUCCESS ) {
                        goto parsing_error;
                }
@@ -759,11 +804,11 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
                /* 
                 * We expect a rdn separator
                 */
-               if ( p[ 0 ] ) {
+               if ( p < end && p[ 0 ] ) {
                        switch ( LDAP_DN_FORMAT( flags ) ) {
                        case LDAP_DN_FORMAT_LDAPV3:
                                if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
-                                       rc = LDAP_OTHER;
+                                       rc = LDAP_DECODING_ERROR;
                                        goto parsing_error;
                                }
                                break;
@@ -771,14 +816,14 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
                        case LDAP_DN_FORMAT_LDAP:
                        case LDAP_DN_FORMAT_LDAPV2:
                                if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
-                                       rc = LDAP_OTHER;
+                                       rc = LDAP_DECODING_ERROR;
                                        goto parsing_error;
                                }
                                break;
        
                        case LDAP_DN_FORMAT_DCE:
                                if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
-                                       rc = LDAP_OTHER;
+                                       rc = LDAP_DECODING_ERROR;
                                        goto parsing_error;
                                }
                                break;
@@ -786,44 +831,82 @@ ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
                }
 
 
-               if ( LDAP_DN_DCE( flags ) ) {
-                       /* add in reversed order */
-                       dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
-               } else {
-                       dn = ldapava_append_to_dn( newDN, newRDN );
-               }
+               tmpDN[nrdns++] = newRDN;
+               newRDN = NULL;
 
-               if ( dn == NULL ) {
-                       rc = LDAP_NO_MEMORY;
-                       goto parsing_error;
-               }
+               /*
+                * make the static RDN array dynamically rescalable
+                */
+               if ( nrdns == num_slots ) {
+                       LDAPRDN *tmp;
 
-               newDN = dn;
-               newRDN = NULL;
+                       if ( tmpDN == tmpDN_ ) {
+                               tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
+                               if ( tmp == NULL ) {
+                                       rc = LDAP_NO_MEMORY;
+                                       goto parsing_error;
+                               }
+                               AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
+
+                       } else {
+                               tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
+                               if ( tmp == NULL ) {
+                                       rc = LDAP_NO_MEMORY;
+                                       goto parsing_error;
+                               }
+                       }
+
+                       tmpDN = tmp;
+                       num_slots *= 2;
+               }
                                
-               if ( p[ 0 ] == '\0' ) {
-                                       
+               if ( p >= end || p[ 0 ] == '\0' ) {
                        /* 
                         * the DN is over, phew
                         */
-                       rc = LDAP_SUCCESS;
+                       newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
+                       if ( newDN == NULL ) {
+                               rc = LDAP_NO_MEMORY;
+                               goto parsing_error;
+                       } else {
+                               int i;
+
+                               if ( LDAP_DN_DCE( flags ) ) {
+                                       /* add in reversed order */
+                                       for ( i=0; i<nrdns; i++ )
+                                               newDN[i] = tmpDN[nrdns-1-i];
+                               } else {
+                                       for ( i=0; i<nrdns; i++ )
+                                               newDN[i] = tmpDN[i];
+                               }
+                               newDN[nrdns] = NULL;
+                               rc = LDAP_SUCCESS;
+                       }
                        goto return_result;
                }
        }
        
 parsing_error:;
        if ( newRDN ) {
-               ldap_rdnfree( newRDN );
+               ldap_rdnfree_x( newRDN, ctx );
        }
 
-       if ( newDN ) {
-               ldap_dnfree( newDN );
-               newDN = NULL;
+       for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
+               ldap_rdnfree_x( tmpDN[nrdns], ctx );
        }
 
 return_result:;
 
-       Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
+       if ( tmpDN != tmpDN_ ) {
+               LDAP_FREEX( tmpDN, ctx );
+       }
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n", 
+               str, flags, rc );
+#else
+       Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
+#endif
        *dn = newDN;
        
        return( rc );
@@ -838,26 +921,61 @@ return_result:;
  * corresponds to the rdn separator or to '\0' in case the string is over.
  */
 int
-ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
+ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
+       char **n_in, unsigned flags )
 {
+       struct berval   bv;
+
+       assert( str );
+       assert( str[ 0 ] != '\0' );     /* FIXME: is this required? */
+
+       bv.bv_len = strlen( str );
+       bv.bv_val = (char *) str;
+
+       return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
+}
+
+int
+ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
+       char **n_in, unsigned flags )
+{
+       return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
+}
+
+int
+ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
+       char **n_in, unsigned flags, void *ctx )
+{
+       const char      **n = (const char **) n_in;
        const char      *p;
+       int             navas = 0;
        int             state = B4AVA;
-       int             rc = LDAP_INVALID_DN_SYNTAX;
+       int             rc = LDAP_DECODING_ERROR;
        int             attrTypeEncoding = LDAP_AVA_STRING, 
                        attrValueEncoding = LDAP_AVA_STRING;
 
-       struct berval   *attrType = NULL;
-       struct berval   *attrValue = NULL;
+       struct berval   attrType = { 0, NULL };
+       struct berval   attrValue = { 0, NULL };
+
+       LDAPRDN         newRDN = NULL;
+       LDAPAVA         *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
+       int             num_slots = TMP_AVA_SLOTS;
 
-       LDAPRDN         *newRDN = NULL;
+       char            *str;
+       ber_len_t       stoplen;
        
-       assert( str );
-       assert( rdn );
+       assert( bv );
+       assert( bv->bv_len );
+       assert( bv->bv_val );
+       assert( rdn || flags & LDAP_DN_SKIP );
        assert( n );
 
-       Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
+       str = bv->bv_val;
+       stoplen = bv->bv_len;
 
-       *rdn = NULL;
+       if ( rdn ) {
+               *rdn = NULL;
+       }
        *n = NULL;
 
        switch ( LDAP_DN_FORMAT( flags ) ) {
@@ -870,14 +988,21 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
        /* unsupported in str2dn */
        case LDAP_DN_FORMAT_UFN:
        case LDAP_DN_FORMAT_AD_CANONICAL:
-               return( LDAP_INVALID_DN_SYNTAX );
+               return LDAP_PARAM_ERROR;
 
+       case LDAP_DN_FORMAT_LBER:
        default:
-               return( LDAP_OTHER );
+               return LDAP_PARAM_ERROR;
        }
 
-       if ( str[ 0 ] == '\0' ) {
-               return( LDAP_SUCCESS );
+       if ( bv->bv_len == 0 ) {
+               return LDAP_SUCCESS;
+
+       }
+
+       if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
+               /* value must have embedded NULs */
+               return LDAP_DECODING_ERROR;
        }
 
        p = str;
@@ -968,19 +1093,15 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                
                case B4OIDATTRTYPE: {
                        int             err = LDAP_SUCCESS;
-                       char            *type;
                        
-                       type = parse_numericoid( &p, &err, 0 );
-                       if ( type == NULL ) {
-                               goto parsing_error;
-                       }
-                       attrType = LDAP_MALLOC( sizeof( struct berval ) );
-                       if ( attrType== NULL ) {
-                               rc = LDAP_NO_MEMORY;
+                       attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
+                               LDAP_SCHEMA_SKIP);
+
+                       if ( err != LDAP_SUCCESS ) {
                                goto parsing_error;
                        }
-                       attrType->bv_val = type;
-                       attrType->bv_len = strlen( type );
+                       attrType.bv_len = p - attrType.bv_val;
+
                        attrTypeEncoding = LDAP_AVA_BINARY;
 
                        state = B4AVAEQUALS;
@@ -1032,18 +1153,6 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                                goto parsing_error;
                        }
                        
-                       assert( attrType == NULL );
-                       attrType = LDAP_MALLOC( sizeof( struct berval ) );
-                       if ( attrType == NULL ) {
-                               rc = LDAP_NO_MEMORY;
-                               goto parsing_error;
-                       }
-                       attrType->bv_val = LDAP_STRNDUP( startPos, len );
-                       if ( attrType->bv_val == NULL ) {
-                               rc = LDAP_NO_MEMORY;
-                               goto parsing_error;
-                       }
-                       attrType->bv_len = len;
                        attrTypeEncoding = LDAP_AVA_STRING;
 
                        /*
@@ -1053,6 +1162,14 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                         */
                        
                        state = B4AVAEQUALS;
+
+                       if ( flags & LDAP_DN_SKIP ) {
+                               break;
+                       }
+
+                       attrType.bv_val = (char *)startPos;
+                       attrType.bv_len = len;
+
                        break;
                }
                                
@@ -1134,11 +1251,16 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                         * here STRING means RFC 2253 string
                         * FIXME: what about DCE strings? 
                         */
-                       state = B4STRINGVALUE;
+                       if ( !p[ 0 ] ) {
+                               /* empty value */
+                               state = GOTAVA;
+                       } else {
+                               state = B4STRINGVALUE;
+                       }
                        break;
 
                case B4BINARYVALUE:
-                       if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
+                       if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1149,14 +1271,15 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                        switch ( LDAP_DN_FORMAT( flags ) ) {
                        case LDAP_DN_FORMAT_LDAP:
                        case LDAP_DN_FORMAT_LDAPV3:
-                               if ( str2strval( p, &attrValue, &p, flags, 
-                                                       &attrValueEncoding ) ) {
+                               if ( str2strval( p, stoplen - ( p - str ),
+                                                       &attrValue, &p, flags, 
+                                                       &attrValueEncoding, ctx ) ) {
                                        goto parsing_error;
                                }
                                break;
 
                        case LDAP_DN_FORMAT_DCE:
-                               if ( DCE2strval( p, &attrValue, &p, flags ) ) {
+                               if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
                                        goto parsing_error;
                                }
                                break;
@@ -1169,7 +1292,7 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                        break;
 
                case B4IA5VALUE:
-                       if ( IA52strval( p, &attrValue, &p, flags ) ) {
+                       if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1180,7 +1303,7 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
 
                        /* lead quote already stripped */
                        if ( quotedIA52strval( p, &attrValue, 
-                                               &p, flags ) ) {
+                                               &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1188,26 +1311,51 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                        break;
 
                case GOTAVA: {
-                       LDAPAVA *ava;
-                       LDAPRDN *rdn;
                        int     rdnsep = 0;
 
-                       /*
-                        * we accept empty values
-                        */
-                       ava = ldapava_new( attrType, attrValue, 
-                                       attrValueEncoding );
-                       if ( ava == NULL ) {
-                               rc = LDAP_NO_MEMORY;
-                               goto parsing_error;
-                       }
+                       if ( !( flags & LDAP_DN_SKIP ) ) {
+                               LDAPAVA *ava;
 
-                       rdn = ldapava_append_to_rdn( newRDN, ava );
-                       if ( rdn == NULL ) {
-                               rc = LDAP_NO_MEMORY;
-                               goto parsing_error;
+                               /*
+                                * we accept empty values
+                                */
+                               ava = ldapava_new( &attrType, &attrValue, 
+                                               attrValueEncoding, ctx );
+                               if ( ava == NULL ) {
+                                       rc = LDAP_NO_MEMORY;
+                                       goto parsing_error;
+                               }
+                               tmpRDN[navas++] = ava;
+
+                               attrValue.bv_val = NULL;
+                               attrValue.bv_len = 0;
+
+                               /*
+                                * prepare room for new AVAs if needed
+                                */
+                               if (navas == num_slots) {
+                                       LDAPAVA **tmp;
+                                       
+                                       if ( tmpRDN == tmpRDN_ ) {
+                                               tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
+                                               if ( tmp == NULL ) {
+                                                       rc = LDAP_NO_MEMORY;
+                                                       goto parsing_error;
+                                               }
+                                               AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
+
+                                       } else {
+                                               tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
+                                               if ( tmp == NULL ) {
+                                                       rc = LDAP_NO_MEMORY;
+                                                       goto parsing_error;
+                                               }
+                                       }
+
+                                       tmpRDN = tmp;
+                                       num_slots *= 2;
+                               }
                        }
-                       newRDN = rdn;
                        
                        /* 
                         * if we got an AVA separator ('+', or ',' for DCE ) 
@@ -1235,13 +1383,25 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                                 * the RDN is over, phew
                                 */
                                *n = p;
+                               if ( !( flags & LDAP_DN_SKIP ) ) {
+                                       newRDN = (LDAPRDN)LDAP_MALLOCX( 
+                                               sizeof(LDAPAVA) * (navas+1), ctx );
+                                       if ( newRDN == NULL ) {
+                                               rc = LDAP_NO_MEMORY;
+                                               goto parsing_error;
+                                       } else {
+                                               AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
+                                               newRDN[navas] = NULL;
+                                       }
+
+                               }
                                rc = LDAP_SUCCESS;
                                goto return_result;
                        }
 
                        /* they should have been used in an AVA */
-                       attrType = NULL;
-                       attrValue = NULL;
+                       attrType.bv_val = NULL;
+                       attrValue.bv_val = NULL;
                        
                        p++;
                        state = B4AVA;
@@ -1253,27 +1413,28 @@ ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
                        goto parsing_error;
                }
        }
+       *n = p;
        
 parsing_error:;
        /* They are set to NULL after they're used in an AVA */
-       if ( attrType ) {
-               ber_bvfree( attrType );
-       }
 
-       if ( attrValue ) {
-               ber_bvfree( attrValue );
+       if ( attrValue.bv_val ) {
+               LDAP_FREEX( attrValue.bv_val, ctx );
        }
 
-       if ( newRDN ) {
-               ldap_rdnfree( newRDN );
-               newRDN = NULL;
+       for ( navas-- ; navas >= 0; navas-- ) {
+               ldapava_free( tmpRDN[navas], ctx );
        }
 
 return_result:;
 
-       Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n", 
-                       *n - p, str, rc );
-       *rdn = newRDN;
+       if ( tmpRDN != tmpRDN_ ) {
+               LDAP_FREEX( tmpRDN, ctx );
+       }
+
+       if ( rdn ) {
+               *rdn = newRDN;
+       }
        
        return( rc );
 }
@@ -1284,27 +1445,24 @@ return_result:;
  * '\' + HEXPAIR(p) -> unhex(p)
  */
 static int
-str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
+str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
 {
-       const char      *p, *startPos, *endPos = NULL;
-       ber_len_t       len, escapes, unescapes;
+       const char      *p, *end, *startPos, *endPos = NULL;
+       ber_len_t       len, escapes;
 
        assert( str );
        assert( val );
        assert( next );
 
-       *val = NULL;
        *next = NULL;
-
-       for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
+       end = str + stoplen;
+       for ( startPos = p = str, escapes = 0; p < end; p++ ) {
                if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
                        p++;
                        if ( p[ 0 ] == '\0' ) {
                                return( 1 );
                        }
-                       if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
-                                       || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
-                                       || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
+                       if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
                                escapes++;
                                continue;
                        }
@@ -1331,14 +1489,18 @@ str2strval( const char *str, struct berval **val, const char **next, unsigned fl
                                return( 1 );
                        }
                        /* 
-                        * FIXME: we allow escaping 
+                        * we do not allow escaping 
                         * of chars that don't need 
                         * to and do not belong to 
-                        * HEXDIGITS (we also allow
-                        * single hexdigit; maybe we 
-                        * shouldn't).
+                        * HEXDIGITS
                         */
-                       unescapes++;
+                       return( 1 );
+
+               } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
+                       if ( p[ 0 ] == '\0' ) {
+                               return( 1 );
+                       }
+                       *retFlags = LDAP_AVA_NONPRINTABLE;
 
                } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) 
                                || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
@@ -1373,62 +1535,63 @@ str2strval( const char *str, struct berval **val, const char **next, unsigned fl
                }
        }
 
+       *next = p;
+       if ( flags & LDAP_DN_SKIP ) {
+               return( 0 );
+       }
+
        /*
         * FIXME: test memory?
         */
-       len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
-       *val = LDAP_MALLOC( sizeof( struct berval ) );
-       ( *val )->bv_len = len;
+       len = ( endPos ? endPos : p ) - startPos - escapes;
+       val->bv_len = len;
 
-       if ( escapes == 0 && unescapes == 0 ) {
-               ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
+       if ( escapes == 0 ) {
+               if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
+                       val->bv_val = LDAP_MALLOCX( len + 1, ctx );
+                       AC_MEMCPY( val->bv_val, startPos, len );
+                       val->bv_val[ len ] = '\0';
+               } else {
+                       val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
+               }
 
        } else {
                ber_len_t       s, d;
 
-               ( *val )->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
                                s++;
-                               if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
-                                               || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
-                                               || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
-                                       ( *val )->bv_val[ d++ ] = 
+                               if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
+                                       val->bv_val[ d++ ] = 
                                                startPos[ s++ ];
                                        
                                } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
                                        char    c;
 
                                        hexstr2bin( &startPos[ s ], &c );
-                                       ( *val )->bv_val[ d++ ] = c;
+                                       val->bv_val[ d++ ] = c;
                                        s += 2;
                                        
                                } else {
-                                       /*
-                                        * we allow escaping of chars
-                                        * that do not need to 
-                                        */
-                                       ( *val )->bv_val[ d++ ] = 
-                                               startPos[ s++ ];
+                                       /* we should never get here */
+                                       assert( 0 );
                                }
 
                        } else {
-                               ( *val )->bv_val[ d++ ] = startPos[ s++ ];
+                               val->bv_val[ d++ ] = startPos[ s++ ];
                        }
                }
 
-               ( *val )->bv_val[ d ] = '\0';
-               assert( strlen( ( *val )->bv_val ) == len );
+               val->bv_val[ d ] = '\0';
+               assert( d == len );
        }
 
-
-       *next = p;
-
        return( 0 );
 }
 
 static int
-DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
+DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len, escapes;
@@ -1437,7 +1600,6 @@ DCE2strval( const char *str, struct berval **val, const char **next, unsigned fl
        assert( val );
        assert( next );
 
-       *val = NULL;
        *next = NULL;
        
        for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
@@ -1480,17 +1642,20 @@ DCE2strval( const char *str, struct berval **val, const char **next, unsigned fl
                }
        }
 
-
+       *next = p;
+       if ( flags & LDAP_DN_SKIP ) {
+               return( 0 );
+       }
+       
        len = ( endPos ? endPos : p ) - startPos - escapes;
-       *val = LDAP_MALLOC( sizeof( struct berval ) );
-       ( *val )->bv_len = len;
+       val->bv_len = len;
        if ( escapes == 0 ){
-               ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
 
-               ( *val )->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        /*
                         * This point is reached only if escapes 
@@ -1501,19 +1666,17 @@ DCE2strval( const char *str, struct berval **val, const char **next, unsigned fl
                                s++;
 
                        }
-                       ( *val )->bv_val[ d++ ] = startPos[ s++ ];
+                       val->bv_val[ d++ ] = startPos[ s++ ];
                }
-               ( *val )->bv_val[ d ] = '\0';
-               assert( strlen( ( *val )->bv_val ) == len );
+               val->bv_val[ d ] = '\0';
+               assert( strlen( val->bv_val ) == len );
        }
        
-       *next = p;
-       
        return( 0 );
 }
 
 static int
-IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
+IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len, escapes;
@@ -1522,7 +1685,6 @@ IA52strval( const char *str, struct berval **val, const char **next, unsigned fl
        assert( val );
        assert( next );
 
-       *val = NULL;
        *next = NULL;
 
        /*
@@ -1561,32 +1723,35 @@ IA52strval( const char *str, struct berval **val, const char **next, unsigned fl
                /* no op */
        }
 
-       *val = LDAP_MALLOC( sizeof( struct berval ) );
+       *next = p;
+       if ( flags & LDAP_DN_SKIP ) {
+               return( 0 );
+       }
+
        len = ( endPos ? endPos : p ) - startPos - escapes;
-       ( *val )->bv_len = len;
+       val->bv_len = len;
        if ( escapes == 0 ) {
-               ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
                
-               ( *val )->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
                                s++;
                        }
-                       ( *val )->bv_val[ d++ ] = startPos[ s++ ];
+                       val->bv_val[ d++ ] = startPos[ s++ ];
                }
-               ( *val )->bv_val[ d ] = '\0';
-               assert( strlen( ( *val )->bv_val ) == len );
+               val->bv_val[ d ] = '\0';
+               assert( strlen( val->bv_val ) == len );
        }
-       *next = p;
 
        return( 0 );
 }
 
 static int
-quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
+quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len;
@@ -1596,7 +1761,6 @@ quotedIA52strval( const char *str, struct berval **val, const char **next, unsig
        assert( val );
        assert( next );
 
-       *val = NULL;
        *next = NULL;
 
        /* initial quote already eaten */
@@ -1648,31 +1812,33 @@ quotedIA52strval( const char *str, struct berval **val, const char **next, unsig
                /* no op */
        }
 
+       *next = p;
+       if ( flags & LDAP_DN_SKIP ) {
+               return( 0 );
+       }
+
        len = endPos - startPos - escapes;
-       assert( len >= 0 );
-       *val = LDAP_MALLOC( sizeof( struct berval ) );
-       ( *val )->bv_len = len;
+       assert( endPos >= startPos + escapes );
+       val->bv_len = len;
        if ( escapes == 0 ) {
-               ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
                
-               ( *val )->bv_val = LDAP_MALLOC( len + 1 );
-               ( *val )->bv_len = len;
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
+               val->bv_len = len;
 
                for ( s = d = 0; d < len; ) {
                        if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
                                s++;
                        }
-                       ( *val )->bv_val[ d++ ] = str[ s++ ];
+                       val->bv_val[ d++ ] = str[ s++ ];
                }
-               ( *val )->bv_val[ d ] = '\0';
-               assert( strlen( ( *val )->bv_val ) == len );
+               val->bv_val[ d ] = '\0';
+               assert( strlen( val->bv_val ) == len );
        }
 
-       *next = p;
-
        return( 0 );
 }
 
@@ -1691,9 +1857,10 @@ hexstr2bin( const char *str, char *c )
                *c = c1 - '0';
 
        } else {
-               c1 = tolower( c1 );
-
-               if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
+               if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
+                       *c = c1 - 'A' + 10;
+               } else {
+                       assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
                        *c = c1 - 'a' + 10;
                }
        }
@@ -1704,9 +1871,10 @@ hexstr2bin( const char *str, char *c )
                *c += c2 - '0';
                
        } else {
-               c2 = tolower( c2 );
-
-               if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
+               if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
+                       *c += c2 - 'A' + 10;
+               } else {
+                       assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
                        *c += c2 - 'a' + 10;
                }
        }
@@ -1715,7 +1883,7 @@ hexstr2bin( const char *str, char *c )
 }
 
 static int
-hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
+hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len;
@@ -1725,7 +1893,6 @@ hexstr2binval( const char *str, struct berval **val, const char **next, unsigned
        assert( val );
        assert( next );
 
-       *val = NULL;
        *next = NULL;
 
        for ( startPos = p = str; p[ 0 ]; p += 2 ) {
@@ -1788,19 +1955,18 @@ hexstr2binval( const char *str, struct berval **val, const char **next, unsigned
 
 end_of_value:;
 
+       *next = p;
+       if ( flags & LDAP_DN_SKIP ) {
+               return( 0 );
+       }
+
        len = ( ( endPos ? endPos : p ) - startPos ) / 2;
        /* must be even! */
        assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
 
-       *val = LDAP_MALLOC( sizeof( struct berval ) );
-       if ( *val == NULL ) {
-               return( LDAP_NO_MEMORY );
-       }
-
-       ( *val )->bv_len = len;
-       ( *val )->bv_val = LDAP_MALLOC( len + 1 );
-       if ( ( *val )->bv_val == NULL ) {
-               LDAP_FREE( *val );
+       val->bv_len = len;
+       val->bv_val = LDAP_MALLOCX( len + 1, ctx );
+       if ( val->bv_val == NULL ) {
                return( LDAP_NO_MEMORY );
        }
 
@@ -1809,11 +1975,10 @@ end_of_value:;
 
                hexstr2bin( &startPos[ s ], &c );
 
-               ( *val )->bv_val[ d ] = c;
+               val->bv_val[ d ] = c;
        }
 
-       ( *val )->bv_val[ d ] = '\0';
-       *next = p;
+       val->bv_val[ d ] = '\0';
 
        return( 0 );
 }
@@ -1824,7 +1989,7 @@ end_of_value:;
 static int
 byte2hexpair( const char *val, char *pair )
 {
-       static const char       hexdig[] = "0123456789abcdef";
+       static const char       hexdig[] = "0123456789ABCDEF";
 
        assert( val );
        assert( pair );
@@ -1877,6 +2042,9 @@ strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
        ber_len_t       l, cl = 1;
        char            *p;
        int             escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
+#ifdef PRETTY_ESCAPE
+       int             escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
+#endif /* PRETTY_ESCAPE */
        
        assert( val );
        assert( len );
@@ -1886,8 +2054,18 @@ strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
                return( 0 );
        }
 
-       for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
-               cl = ldap_utf8_charlen( p );
+       for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
+
+               /* 
+                * escape '%x00' 
+                */
+               if ( p[ 0 ] == '\0' ) {
+                       cl = 1;
+                       l += 3;
+                       continue;
+               }
+
+               cl = LDAP_UTF8_CHARLEN2( p, cl );
                if ( cl == 0 ) {
                        /* illegal utf-8 char! */
                        return( -1 );
@@ -1896,23 +2074,35 @@ strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
                        ber_len_t cnt;
 
                        for ( cnt = 1; cnt < cl; cnt++ ) {
-                               if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
+                               if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
                                        return( -1 );
                                }
                        }
                        l += escaped_byte_len * cl;
-               
-               /* 
-                * there might be some chars we want to escape in form
-                * of a couple of hexdigits for optimization purposes
-                */
-               } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
-                       l += 3;
 
                } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
                                || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
                                || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
-                       l += 2;
+#ifdef PRETTY_ESCAPE
+#if 0
+                       if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
+#else
+                       if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
+#endif
+
+                               /* 
+                                * there might be some chars we want 
+                                * to escape in form of a couple 
+                                * of hexdigits for optimization purposes
+                                */
+                               l += 3;
+
+                       } else {
+                               l += escaped_ascii_len;
+                       }
+#else /* ! PRETTY_ESCAPE */
+                       l += 3;
+#endif /* ! PRETTY_ESCAPE */
 
                } else {
                        l++;
@@ -1947,14 +2137,45 @@ strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
         * of the value
         */
        for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
-               ber_len_t       cl = ldap_utf8_charlen( &val->bv_val[ s ] );
+               ber_len_t       cl;
+
+               /* 
+                * escape '%x00' 
+                */
+               if ( val->bv_val[ s ] == '\0' ) {
+                       cl = 1;
+                       str[ d++ ] = '\\';
+                       str[ d++ ] = '0';
+                       str[ d++ ] = '0';
+                       s++;
+                       continue;
+               }
+               
+               /*
+                * The length was checked in strval2strlen();
+                * LDAP_UTF8_CHARLEN() should suffice
+                */
+               cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
+               assert( cl > 0 );
                
                /* 
                 * there might be some chars we want to escape in form
                 * of a couple of hexdigits for optimization purposes
                 */
                if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) ) 
-                               || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
+#ifdef PRETTY_ESCAPE
+#if 0
+                               || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] ) 
+#else
+                               || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] ) 
+#endif
+#else /* ! PRETTY_ESCAPE */
+                               || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
+                               || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
+                               || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
+
+#endif /* ! PRETTY_ESCAPE */
+                               ) {
                        for ( ; cl--; ) {
                                str[ d++ ] = '\\';
                                byte2hexpair( &val->bv_val[ s ], &str[ d ] );
@@ -1968,11 +2189,19 @@ strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
                        }
 
                } else {
+#ifdef PRETTY_ESCAPE
                        if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
                                        || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
                                        || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
                                str[ d++ ] = '\\';
+                               if ( !LDAP_DN_IS_PRETTY( flags ) ) {
+                                       byte2hexpair( &val->bv_val[ s ], &str[ d ] );
+                                       s++;
+                                       d += 2;
+                                       continue;
+                               }
                        }
+#endif /* PRETTY_ESCAPE */
                        str[ d++ ] = val->bv_val[ s++ ];
                }
        }
@@ -2243,34 +2472,37 @@ strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
 
 /*
  * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
- * the forst part of the AD representation of the DN is written in DNS
+ * the first part of the AD representation of the DN is written in DNS
  * form, i.e. dot separated domain name components (as suggested 
  * by Luke Howard, http://www.padl.com/~lukeh)
  */
 static int
-dn2domain( LDAPDN *dn, char *str, int *iRDN )
+dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
 {
        int             i;
        int             domain = 0, first = 1;
        ber_len_t       l = 1; /* we move the null also */
+       char            *str;
 
        /* we are guaranteed there's enough memory in str */
 
        /* sanity */
        assert( dn );
-       assert( str );
+       assert( bv );
        assert( iRDN );
-       assert( *iRDN > 0 );
+       assert( *iRDN >= 0 );
+
+       str = bv->bv_val + pos;
 
        for ( i = *iRDN; i >= 0; i-- ) {
-               LDAPRDN         *rdn;
+               LDAPRDN         rdn;
                LDAPAVA         *ava;
 
-               assert( dn[ i ][ 0 ] );
-               rdn = dn[ i ][ 0 ];
+               assert( dn[ i ] );
+               rdn = dn[ i ];
 
-               assert( rdn[ 0 ][ 0 ] );
-               ava = rdn[ 0 ][ 0 ];
+               assert( rdn[ 0 ] );
+               ava = rdn[ 0 ];
 
                if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
                        break;
@@ -2280,26 +2512,27 @@ dn2domain( LDAPDN *dn, char *str, int *iRDN )
                
                if ( first ) {
                        first = 0;
-                       AC_MEMCPY( str, ava->la_value->bv_val, 
-                                       ava->la_value->bv_len + 1);
-                       l += ava->la_value->bv_len;
+                       AC_MEMCPY( str, ava->la_value.bv_val, 
+                                       ava->la_value.bv_len + 1);
+                       l += ava->la_value.bv_len;
 
                } else {
-                       AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
-                       AC_MEMCPY( str, ava->la_value->bv_val, 
-                                       ava->la_value->bv_len );
-                       str[ ava->la_value->bv_len ] = '.';
-                       l += ava->la_value->bv_len + 1;
+                       AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
+                       AC_MEMCPY( str, ava->la_value.bv_val, 
+                                       ava->la_value.bv_len );
+                       str[ ava->la_value.bv_len ] = '.';
+                       l += ava->la_value.bv_len + 1;
                }
        }
 
        *iRDN = i;
+       bv->bv_len = pos + l - 1;
 
        return( domain );
 }
 
 static int
-rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
+rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
         int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
 {
        int             iAVA;
@@ -2308,20 +2541,20 @@ rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
        *len = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* len(type) + '=' + '+' | ',' */
-               l += ava->la_attr->bv_len + 2;
+               l += ava->la_attr.bv_len + 2;
 
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the length */
-                       l += 1 + 2 * ava->la_value->bv_len;
+                       l += 1 + 2 * ava->la_value.bv_len;
 
                } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
-                       if ( ( *s2l )( ava->la_value, f, &vl ) ) {
+                       if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
@@ -2334,38 +2567,38 @@ rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
 }
 
 static int
-rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
+rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
        int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
-               AC_MEMCPY( &str[ l ], ava->la_attr->bv_val, 
-                               ava->la_attr->bv_len );
-               l += ava->la_attr->bv_len;
+               AC_MEMCPY( &str[ l ], ava->la_attr.bv_val, 
+                               ava->la_attr.bv_len );
+               l += ava->la_attr.bv_len;
 
                str[ l++ ] = '=';
 
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
-                       if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
+                       if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
-                       l += 2 * ava->la_value->bv_len;
+                       l += 2 * ava->la_value.bv_len;
 
                } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
-                       if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
+                       if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
                }
-               str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
+               str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
        }
 
        *len = l;
@@ -2374,7 +2607,7 @@ rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
 }
 
 static int
-rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
@@ -2382,30 +2615,22 @@ rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
        *len = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* len(type) + '=' + ',' | '/' */
-               l += ava->la_attr->bv_len + 2;
+               l += ava->la_attr.bv_len + 2;
 
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the length */
-                       l += 1 + 2 * ava->la_value->bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+                       l += 1 + 2 * ava->la_value.bv_len;
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
-                       if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
+                       if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
        
@@ -2415,13 +2640,13 @@ rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
+rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( first ) {
                        first = 0;
@@ -2429,34 +2654,26 @@ rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
                        str[ l++ ] = ( iAVA ? ',' : '/' );
                }
 
-               AC_MEMCPY( &str[ l ], ava->la_attr->bv_val, 
-                               ava->la_attr->bv_len );
-               l += ava->la_attr->bv_len;
+               AC_MEMCPY( &str[ l ], ava->la_attr.bv_val, 
+                               ava->la_attr.bv_len );
+               l += ava->la_attr.bv_len;
 
                str[ l++ ] = '=';
 
-               switch ( ava->la_flags ) {
-                       case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
-                       if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
+                       if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
-                       l += 2 * ava->la_value->bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+                       l += 2 * ava->la_value.bv_len;
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
-                       if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
+                       if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-                                     
-               default:
-                       return( -1 );
                }
        }
 
@@ -2466,7 +2683,7 @@ rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
 }
 
 static int
-rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
@@ -2477,7 +2694,7 @@ rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
        *len = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* ' + ' | ', ' */
                l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
@@ -2485,13 +2702,13 @@ rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
                /* FIXME: are binary values allowed in UFN? */
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the value */
-                       l += 1 + 2 * ava->la_value->bv_len;
+                       l += 1 + 2 * ava->la_value.bv_len;
 
                } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
-                       if ( strval2strlen( ava->la_value, f, &vl ) ) {
+                       if ( strval2strlen( &ava->la_value, f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
@@ -2504,32 +2721,32 @@ rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
+rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
-                       if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
+                       if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
-                       l += 2 * ava->la_value->bv_len;
+                       l += 2 * ava->la_value.bv_len;
                        
                } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
-                       if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
+                       if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
                }
 
-               if ( rdn[ iAVA + 1 ]) {
+               if ( rdn[ iAVA + 1 ] ) {
                        AC_MEMCPY( &str[ l ], " + ", 3 );
                        l += 3;
 
@@ -2545,7 +2762,7 @@ rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
@@ -2556,31 +2773,23 @@ rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
        *len = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* ',' | '/' */
                l++;
 
                /* FIXME: are binary values allowed in UFN? */
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the value */
-                       l += 1 + 2 * ava->la_value->bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+                       l += 1 + 2 * ava->la_value.bv_len;
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
-                       if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
+                       if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
        
@@ -2590,13 +2799,13 @@ rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
+rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
        for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ iAVA ][ 0 ];
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( first ) {
                        first = 0;
@@ -2604,28 +2813,20 @@ rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
                        str[ l++ ] = ( iAVA ? ',' : '/' );
                }
 
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
-                       if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
+                       if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
-                       l += 2 * ava->la_value->bv_len;
-                       break;
-                       
-               case LDAP_AVA_STRING: {
+                       l += 2 * ava->la_value.bv_len;
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
-                       if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
+                       if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
 
@@ -2642,15 +2843,41 @@ rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
  * this is wanted to reduce the allocation of temporary buffers.
  */
 int
-ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
+ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
+{
+       struct berval bv;
+       int rc;
+
+       assert( str );
+
+       if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
+               return LDAP_PARAM_ERROR;
+       }
+
+       rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
+       *str = bv.bv_val;
+       return rc;
+}
+
+int
+ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
+{
+       return ldap_rdn2bv_x( rdn, bv, flags, NULL );
+}
+
+int
+ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
 {
        int             rc, back;
        ber_len_t       l;
        
-       assert( str );
+       assert( bv );
+
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
 
        if ( rdn == NULL ) {
-               *str = LDAP_STRDUP( "" );
+               bv->bv_val = LDAP_STRDUPX( "", ctx );
                return( LDAP_SUCCESS );
        }
 
@@ -2658,83 +2885,83 @@ ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
         * This routine wastes "back" bytes at the end of the string
         */
 
-       *str = NULL;
        switch ( LDAP_DN_FORMAT( flags ) ) {
        case LDAP_DN_FORMAT_LDAPV3:
                if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
-                       return( LDAP_OTHER );
+                       return LDAP_DECODING_ERROR;
                }
                break;
 
        case LDAP_DN_FORMAT_LDAPV2:
                if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
-                       return( LDAP_OTHER );
+                       return LDAP_DECODING_ERROR;
                }
                break;
 
        case LDAP_DN_FORMAT_UFN:
                if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
-                       return( LDAP_OTHER );
+                       return LDAP_DECODING_ERROR;
                }
                break;
 
        case LDAP_DN_FORMAT_DCE:
                if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
-                       return( LDAP_OTHER );
+                       return LDAP_DECODING_ERROR;
                }
                break;
 
        case LDAP_DN_FORMAT_AD_CANONICAL:
                if ( rdn2ADstrlen( rdn, flags, &l ) ) {
-                       return( LDAP_OTHER );
+                       return LDAP_DECODING_ERROR;
                }
                break;
 
        default:
-               return( LDAP_INVALID_DN_SYNTAX );
+               return LDAP_PARAM_ERROR;
        }
 
-       *str = LDAP_MALLOC( l + 1 );
+       bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
 
        switch ( LDAP_DN_FORMAT( flags ) ) {
        case LDAP_DN_FORMAT_LDAPV3:
-               rc = rdn2str( rdn, *str, flags, &l, strval2str );
+               rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
                back = 1;
                break;
 
        case LDAP_DN_FORMAT_LDAPV2:
-               rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
+               rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
                back = 1;
                break;
 
        case LDAP_DN_FORMAT_UFN:
-               rc = rdn2UFNstr( rdn, *str, flags, &l );
+               rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
                back = 2;
                break;
 
        case LDAP_DN_FORMAT_DCE:
-               rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
+               rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
                back = 0;
                break;
 
        case LDAP_DN_FORMAT_AD_CANONICAL:
-               rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
+               rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
                back = 0;
                break;
 
        default:
                /* need at least one of the previous */
-               return( LDAP_OTHER );
+               return LDAP_PARAM_ERROR;
        }
 
        if ( rc ) {
-               ldap_memfree( *str );
-               return( LDAP_OTHER );
+               LDAP_FREEX( bv->bv_val, ctx );
+               return rc;
        }
 
-       ( *str )[ l - back ] = '\0';
+       bv->bv_len = l - back;
+       bv->bv_val[ bv->bv_len ] = '\0';
 
-       return( LDAP_SUCCESS );
+       return LDAP_SUCCESS;
 }
 
 /*
@@ -2749,28 +2976,54 @@ ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
  *   c) what do we do when binary values must be converted in UTF/DCE/AD?
  *      use binary encoded BER
  */ 
-int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
+int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
+{
+       struct berval bv;
+       int rc;
+
+       assert( str );
+
+       if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
+               return LDAP_PARAM_ERROR;
+       }
+       
+       rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
+       *str = bv.bv_val;
+       return rc;
+}
+
+int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
+{
+       return ldap_dn2bv_x( dn, bv, flags, NULL );
+}
+
+int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
 {
        int             iRDN;
-       int             rc = LDAP_OTHER;
+       int             rc = LDAP_ENCODING_ERROR;
        ber_len_t       len, l;
 
        /* stringifying helpers for LDAPv3/LDAPv2 */
        int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
        int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
 
-       assert( str );
-
-       Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
+       assert( bv );
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
 
-       *str = NULL;
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s", 
+               flags, "", "" );
+#else
+       Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
+#endif
 
        /* 
         * a null dn means an empty dn string 
         * FIXME: better raise an error?
         */
        if ( dn == NULL ) {
-               *str = LDAP_STRDUP( "" );
+               bv->bv_val = LDAP_STRDUPX( "", ctx );
                return( LDAP_SUCCESS );
        }
 
@@ -2778,37 +3031,34 @@ int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
        case LDAP_DN_FORMAT_LDAPV3:
                sv2l = strval2strlen;
                sv2s = strval2str;
-               goto got_funcs;
 
+               if( 0 ) {
        case LDAP_DN_FORMAT_LDAPV2:
-               sv2l = strval2IA5strlen;
-               sv2s = strval2IA5str;
-got_funcs:
-               
+                       sv2l = strval2IA5strlen;
+                       sv2s = strval2IA5str;
+               }
+
                for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
-                       
-                       if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
+                       if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
                for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                       if ( rdn2str( rdn, &( *str )[ l ], flags, 
+                       if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags, 
                                        &rdnl, sv2s ) ) {
-                               LDAP_FREE( *str );
-                               *str = NULL;
+                               LDAP_FREEX( bv->bv_val, ctx );
+                               bv->bv_val = NULL;
                                goto return_results;
                        }
                        l += rdnl;
@@ -2820,13 +3070,13 @@ got_funcs:
                 * trim the last ',' (the allocated memory 
                 * is one byte longer than required)
                 */
-               ( *str )[ len - 1 ] = '\0';
+               bv->bv_len = len - 1;
+               bv->bv_val[ bv->bv_len ] = '\0';
 
                rc = LDAP_SUCCESS;
                break;
 
        case LDAP_DN_FORMAT_UFN: {
-
                /*
                 * FIXME: quoting from RFC 1781:
                 *
@@ -2868,15 +3118,14 @@ got_funcs:
 
                for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                       if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
                        len += rdnl;
 
 #ifdef DC_IN_UFN
-                       if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
+                       if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
                                if ( leftmost_dc == -1 ) {
                                        leftmost_dc = iRDN;
                                }
@@ -2886,7 +3135,7 @@ got_funcs:
 #endif /* DC_IN_UFN */
                }
 
-               if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
@@ -2896,12 +3145,11 @@ got_funcs:
 #endif /* DC_IN_UFN */
                        for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
                                ber_len_t       vl;
-                               LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                               if ( rdn2UFNstr( rdn, &( *str )[ l ], 
+                               if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &vl ) ) {
-                                       LDAP_FREE( *str );
-                                       *str = NULL;
+                                       LDAP_FREEX( bv->bv_val, ctx );
+                                       bv->bv_val = NULL;
                                        goto return_results;
                                }
                                l += vl;
@@ -2911,27 +3159,27 @@ got_funcs:
                         * trim the last ', ' (the allocated memory 
                         * is two bytes longer than required)
                         */
-                       ( *str )[ len - 2 ] = '\0';
+                       bv->bv_len = len - 2;
+                       bv->bv_val[ bv->bv_len ] = '\0';
 #ifdef DC_IN_UFN
                } else {
                        last_iRDN = iRDN - 1;
 
                        for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
                                ber_len_t       vl;
-                               LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                               if ( rdn2UFNstr( rdn, &( *str )[ l ], 
+                               if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &vl ) ) {
-                                       LDAP_FREE( *str );
-                                       *str = NULL;
+                                       LDAP_FREEX( bv->bv_val, ctx );
+                                       bv->bv_val = NULL;
                                        goto return_results;
                                }
                                l += vl;
                        }
 
-                       if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
-                               LDAP_FREE( *str );
-                               *str = NULL;
+                       if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
+                               LDAP_FREEX( bv->bv_val, ctx );
+                               bv->bv_val = NULL;
                                goto return_results;
                        }
 
@@ -2940,35 +3188,31 @@ got_funcs:
 #endif /* DC_IN_UFN */
                
                rc = LDAP_SUCCESS;
-               break;
-       }
 
-       case LDAP_DN_FORMAT_DCE:
+       } break;
 
+       case LDAP_DN_FORMAT_DCE:
                for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
-                       
-                       if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
                for ( l = 0; iRDN--; ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                       if ( rdn2DCEstr( rdn, &( *str )[ l ], flags, 
+                       if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags, 
                                        &rdnl, 0 ) ) {
-                               LDAP_FREE( *str );
-                               *str = NULL;
+                               LDAP_FREEX( bv->bv_val, ctx );
+                               bv->bv_val = NULL;
                                goto return_results;
                        }
                        l += rdnl;
@@ -2976,13 +3220,13 @@ got_funcs:
 
                assert( l == len );
 
-               ( *str )[ len ] = '\0';
+               bv->bv_len = len;
+               bv->bv_val[ bv->bv_len ] = '\0';
 
                rc = LDAP_SUCCESS;
                break;
 
        case LDAP_DN_FORMAT_AD_CANONICAL: {
-               
                /*
                 * Sort of UFN for DCE DNs: a slash ('/') separated
                 * global->local DN with no types; strictly speaking,
@@ -2999,30 +3243,28 @@ got_funcs:
                 */ 
                for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                       if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
                iRDN--;
-               if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
-                       for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
+               if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
+                       for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
                                ber_len_t       rdnl;
-                               LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                               if ( rdn2ADstr( rdn, &( *str )[ l ], 
+                               if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &rdnl, 0 ) ) {
-                                       LDAP_FREE( *str );
-                                       *str = NULL;
+                                       LDAP_FREEX( bv->bv_val, ctx );
+                                       bv->bv_val = NULL;
                                        goto return_results;
                                }
                                l += rdnl;
@@ -3037,20 +3279,19 @@ got_funcs:
                         * i.e. terminated by a domain component
                         */
                        if ( flags & LDAP_DN_PEDANTIC ) {
-                               LDAP_FREE( *str );
-                               *str = NULL;
-                               rc = LDAP_INVALID_DN_SYNTAX;
+                               LDAP_FREEX( bv->bv_val, ctx );
+                               bv->bv_val = NULL;
+                               rc = LDAP_ENCODING_ERROR;
                                break;
                        }
 
                        for ( l = 0; iRDN >= 0 ; iRDN-- ) {
                                ber_len_t       rdnl;
-                               LDAPRDN         *rdn = dn[ iRDN ][ 0 ];
                        
-                               if ( rdn2ADstr( rdn, &( *str )[ l ], 
+                               if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &rdnl, first ) ) {
-                                       LDAP_FREE( *str );
-                                       *str = NULL;
+                                       LDAP_FREEX( bv->bv_val, ctx );
+                                       bv->bv_val = NULL;
                                        goto return_results;
                                }
                                if ( first ) {
@@ -3060,19 +3301,224 @@ got_funcs:
                        }
                }
 
-               ( *str )[ len ] = '\0';
+               bv->bv_len = len;
+               bv->bv_val[ bv->bv_len ] = '\0';
 
                rc = LDAP_SUCCESS;
-               break;
-       }
+       } break;
 
        default:
-               return( LDAP_INVALID_DN_SYNTAX );
-
+               return LDAP_PARAM_ERROR;
        }
 
-       Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n", 
+               bv->bv_val, flags, rc );
+#else
+       Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
+               bv->bv_val, flags, rc );
+#endif
+
 return_results:;
        return( rc );
 }
 
+#ifdef HAVE_TLS
+#include <openssl/x509.h>
+#include <openssl/err.h>
+
+/* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
+ * x509_name must be an (X509_NAME *). If func is non-NULL, the
+ * constructed DN will use numeric OIDs to identify attributeTypes,
+ * and the func() will be invoked to rewrite the DN with the given
+ * flags.
+ *
+ * Otherwise the DN will use shortNames as defined in the OpenSSL
+ * library.
+ *
+ * It's preferable to let slapd do the OID to attributeType mapping,
+ * because the OpenSSL tables are known to have many typos in versions
+ * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
+ * so we're forced to use OpenSSL's mapping there.
+ *  -- Howard Chu 2002-04-18
+ */
+
+int
+ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
+       unsigned flags )
+{
+       LDAPDN  newDN;
+       LDAPRDN newRDN;
+       LDAPAVA *newAVA, *baseAVA;
+       X509_NAME_ENTRY *ne;
+       ASN1_OBJECT *obj;
+       ASN1_STRING *str;
+       char oids[8192], *oidptr = oids, *oidbuf = NULL;
+       void *ptrs[2048];
+       int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
+       int set = -1;
+       size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
+       int csize;
+
+       struct berval   Val;
+
+       assert( bv );
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
+
+       /* Get the number of AVAs. This is not necessarily the same as
+        * the number of RDNs.
+        */
+       navas = X509_NAME_entry_count( x509_name );
+
+       /* Get the last element, to see how many RDNs there are */
+       ne = X509_NAME_get_entry( x509_name, navas - 1 );
+       nrdns = ne->set + 1;
+
+       /* Allocate the DN/RDN/AVA stuff as a single block */    
+       dnsize = sizeof(LDAPRDN) * (nrdns+1);
+       dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
+       dnsize += sizeof(LDAPAVA) * navas;
+       if (dnsize > sizeof(ptrs)) {
+               newDN = (LDAPDN)LDAP_MALLOC( dnsize );
+               if ( newDN == NULL )
+                       return LDAP_NO_MEMORY;
+       } else {
+               newDN = (LDAPDN)ptrs;
+       }
+       
+       newDN[nrdns] = NULL;
+       newRDN = (LDAPRDN)(newDN + nrdns+1);
+       newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
+       baseAVA = newAVA;
+
+       /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
+       for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
+               ne = X509_NAME_get_entry( x509_name, i );
+               obj = X509_NAME_ENTRY_get_object( ne );
+               str = X509_NAME_ENTRY_get_data( ne );
+
+               /* If set changed, move to next RDN */
+               if ( set != ne->set ) {
+                       /* If this is not the first time, end the
+                        * previous RDN and advance.
+                        */
+                       if ( j > 0 ) {
+                               newRDN[k] = NULL;
+                               newRDN += k+1;
+                       }
+                       newDN[j++] = newRDN;
+
+                       k = 0;
+                       set = ne->set;
+               }
+               newAVA->la_private = NULL;
+               newAVA->la_flags = LDAP_AVA_STRING;
+
+               if ( !func ) {
+                       int n = OBJ_obj2nid( obj );
+
+                       if (n == NID_undef)
+                               goto get_oid;
+                       newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
+                       newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
+#ifdef HAVE_EBCDIC
+                       newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
+                       __etoa( newAVA->la_attr.bv_val );
+                       newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
+#endif
+               } else {
+get_oid:               newAVA->la_attr.bv_val = oidptr;
+                       newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
+#ifdef HAVE_EBCDIC
+                       __etoa( newAVA->la_attr.bv_val );
+#endif
+                       oidptr += newAVA->la_attr.bv_len + 1;
+                       oidrem -= newAVA->la_attr.bv_len + 1;
+
+                       /* Running out of OID buffer space? */
+                       if (oidrem < 128) {
+                               if ( oidsize == 0 ) {
+                                       oidsize = sizeof(oids) * 2;
+                                       oidrem = oidsize;
+                                       oidbuf = LDAP_MALLOC( oidsize );
+                                       if ( oidbuf == NULL ) goto nomem;
+                                       oidptr = oidbuf;
+                               } else {
+                                       char *old = oidbuf;
+                                       oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
+                                       if ( oidbuf == NULL ) goto nomem;
+                                       /* Buffer moved! Fix AVA pointers */
+                                       if ( old != oidbuf ) {
+                                               LDAPAVA *a;
+                                               long dif = oidbuf - old;
+
+                                               for (a=baseAVA; a<=newAVA; a++){
+                                                       if (a->la_attr.bv_val >= old &&
+                                                               a->la_attr.bv_val <= (old + oidsize))
+                                                               a->la_attr.bv_val += dif;
+                                               }
+                                       }
+                                       oidptr = oidbuf + oidsize - oidrem;
+                                       oidrem += oidsize;
+                                       oidsize *= 2;
+                               }
+                       }
+               }
+               Val.bv_val = (char *) str->data;
+               Val.bv_len = str->length;
+               switch( str->type ) {
+               case V_ASN1_UNIVERSALSTRING:
+                       /* This uses 32-bit ISO 10646-1 */
+                       csize = 4; goto to_utf8;
+               case V_ASN1_BMPSTRING:
+                       /* This uses 16-bit ISO 10646-1 */
+                       csize = 2; goto to_utf8;
+               case V_ASN1_T61STRING:
+                       /* This uses 8-bit, assume ISO 8859-1 */
+                       csize = 1;
+to_utf8:               rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
+                       newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
+                       if (rc != LDAP_SUCCESS) goto nomem;
+                       newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
+                       break;
+               case V_ASN1_UTF8STRING:
+                       newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
+                       /* This is already in UTF-8 encoding */
+               case V_ASN1_IA5STRING:
+               case V_ASN1_PRINTABLESTRING:
+                       /* These are always 7-bit strings */
+                       newAVA->la_value = Val;
+               default:
+                       ;
+               }
+               newRDN[k] = newAVA;
+               newAVA++;
+               k++;
+       }
+       newRDN[k] = NULL;
+
+       if ( func ) {
+               rc = func( newDN, flags, NULL );
+               if ( rc != LDAP_SUCCESS )
+                       goto nomem;
+       }
+
+       rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
+
+nomem:
+       for (;baseAVA < newAVA; baseAVA++) {
+               if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
+                       LDAP_FREE( baseAVA->la_attr.bv_val );
+               if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
+                       LDAP_FREE( baseAVA->la_value.bv_val );
+       }
+
+       if ( oidsize != 0 )
+               LDAP_FREE( oidbuf );
+       if ( newDN != (LDAPDN) ptrs )
+               LDAP_FREE( newDN );
+       return rc;
+}
+#endif /* HAVE_TLS */
+