]> git.sur5r.net Git - openldap/commitdiff
More tweaks to ldif_parse_line2 for str2entry
authorHoward Chu <hyc@openldap.org>
Wed, 19 Jan 2005 12:07:06 +0000 (12:07 +0000)
committerHoward Chu <hyc@openldap.org>
Wed, 19 Jan 2005 12:07:06 +0000 (12:07 +0000)
include/ldif.h
libraries/liblutil/ldif.c
servers/slapd/entry.c

index b484da6ae126f3980a206c512a0da17316d10e89..88c062217ea8bd58af52dfa6f330f7e2030fbbb0 100644 (file)
@@ -61,11 +61,10 @@ ldif_parse_line LDAP_P((
 
 LDAP_LDIF_F( int )
 ldif_parse_line2 LDAP_P((
-       LDAP_CONST char *line,
-       char **name,
-       char **value,
-       ber_len_t *vlen,
-       int *alloc ));
+       char *line,
+       struct berval *type,
+       struct berval *value,
+       int *freeval ));
 
 LDAP_LDIF_F( int )
 ldif_fetch_url LDAP_P((
index 9d67b3cf75056ff54eb004c5b9b332a4d2de3136..79f11406113fe8ce6984b445bfff5a368542d071 100644 (file)
@@ -86,7 +86,9 @@ static const unsigned char b642nib[0x80] = {
  *
  * ldif_parse_line2 - operates in-place on input buffer, returning type
  * in-place. Will return value in-place if possible, (must malloc for
- * fetched URLs).
+ * fetched URLs). If freeval is NULL, all return data will be malloc'd
+ * and the input line will be unmodified. Otherwise freeval is set to
+ * True if the value was malloc'd.
  */
 
 int
@@ -97,35 +99,37 @@ ldif_parse_line(
     ber_len_t *vlenp
 )
 {
-       return ldif_parse_line2( line, typep, valuep, vlenp, NULL );
+       struct berval type, value;
+       int rc = ldif_parse_line2( (char *)line, &type, &value, NULL );
+
+       *typep = type.bv_val;
+       *valuep = value.bv_val;
+       *vlenp = value.bv_len;
+       return rc;
 }
 
 int
 ldif_parse_line2(
-    LDAP_CONST char    *line,
-    char       **typep,
-    char       **valuep,
-    ber_len_t *vlenp,
-       int             *alloc
+    char       *line,
+       struct berval *type,
+       struct berval *value,
+       int             *freeval
 )
 {
        char    *s, *p, *d; 
        char    nib;
        int     b64, url;
-       char    *type, *value;
-       ber_len_t vlen;
 
-       *typep = NULL;
-       *valuep = NULL;
-       *vlenp = 0;
+       BER_BVZERO( type );
+       BER_BVZERO( value );
 
        /* skip any leading space */
        while ( isspace( (unsigned char) *line ) ) {
                line++;
        }
 
-       if ( alloc ) {
-               *alloc = 0;
+       if ( freeval ) {
+               *freeval = 0;
        } else {
                line = ber_strdup( line );
 
@@ -136,23 +140,24 @@ ldif_parse_line2(
                }
        }
 
-       type = line;
+       type->bv_val = line;
 
-       s = strchr( type, ':' );
+       s = strchr( type->bv_val, ':' );
 
        if ( s == NULL ) {
                ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
                        _("ldif_parse_line: missing ':' after %s\n"),
                        type );
-               if ( !alloc ) ber_memfree( line );
+               if ( !freeval ) ber_memfree( line );
                return( -1 );
        }
 
        /* trim any space between type and : */
-       for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
+       for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) {
                *p = '\0';
        }
        *s++ = '\0';
+       type->bv_len = s - type->bv_val - 1;
 
        url = 0;
        b64 = 0;
@@ -186,13 +191,13 @@ ldif_parse_line2(
                        /* no value is present, error out */
                        ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
                                _("ldif_parse_line: %s missing base64 value\n"), type );
-                       if ( !alloc ) ber_memfree( line );
+                       if ( !freeval ) ber_memfree( line );
                        return( -1 );
                }
 
-               byte = value = s;
+               byte = value->bv_val = s;
 
-               for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
+               for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) {
                        int i;
                        for ( i = 0; i < 4; i++ ) {
                                if ( p[i] != '=' && (p[i] & 0x80 ||
@@ -201,7 +206,7 @@ ldif_parse_line2(
                                                _("ldif_parse_line: %s: invalid base64 encoding"
                                                " char (%c) 0x%x\n"),
                                            type, p[i], p[i] );
-                                       if ( !alloc ) ber_memfree( line );
+                                       if ( !freeval ) ber_memfree( line );
                                        return( -1 );
                                }
                        }
@@ -215,7 +220,7 @@ ldif_parse_line2(
                        byte[1] = (nib & RIGHT4) << 4;
                        /* third digit */
                        if ( p[2] == '=' ) {
-                               vlen += 1;
+                               value->bv_len += 1;
                                break;
                        }
                        nib = b642nib[ p[2] & 0x7f ];
@@ -223,7 +228,7 @@ ldif_parse_line2(
                        byte[2] = (nib & RIGHT2) << 6;
                        /* fourth digit */
                        if ( p[3] == '=' ) {
-                               vlen += 2;
+                               value->bv_len += 2;
                                break;
                        }
                        nib = b642nib[ p[3] & 0x7f ];
@@ -231,63 +236,59 @@ ldif_parse_line2(
 
                        byte += 3;
                }
-               s[ vlen ] = '\0';
+               s[ value->bv_len ] = '\0';
 
        } else if ( url ) {
                if ( *s == '\0' ) {
                        /* no value is present, error out */
                        ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
                                _("ldif_parse_line: %s missing URL value\n"), type );
-                       if ( !alloc ) ber_memfree( line );
+                       if ( !freeval ) ber_memfree( line );
                        return( -1 );
                }
 
-               if( ldif_fetch_url( s, &value, &vlen ) ) {
+               if( ldif_fetch_url( s, &value->bv_val, &value->bv_len ) ) {
                        ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
                                _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
                                type, s );
-                       if ( !alloc ) ber_memfree( line );
+                       if ( !freeval ) ber_memfree( line );
                        return( -1 );
                }
-               if ( alloc ) *alloc = 1;
+               if ( freeval ) *freeval = 1;
 
        } else {
-               value = s;
-               vlen = (int) (d - s);
+               value->bv_val = s;
+               value->bv_len = (int) (d - s);
        }
 
-       if ( !alloc ) {
-               type = ber_strdup( type );
+       if ( !freeval ) {
+               struct berval bv = *type;
+
+               ber_dupbv( type, &bv );
 
-               if( type == NULL ) {
+               if( BER_BVISNULL( type )) {
                        ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
                                _("ldif_parse_line: type malloc failed\n"));
-                       if( url ) ber_memfree( value );
+                       if( url ) ber_memfree( value->bv_val );
                        ber_memfree( line );
                        return( -1 );
                }
 
                if( !url ) {
-                       p = ber_memalloc( vlen + 1 );
-                       if( p == NULL ) {
+                       bv = *value;
+                       ber_dupbv( value, &bv );
+                       if( BER_BVISNULL( value )) {
                                ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
                                        _("ldif_parse_line: value malloc failed\n"));
-                               ber_memfree( type );
+                               ber_memfree( type->bv_val );
                                ber_memfree( line );
                                return( -1 );
                        }
-                       AC_MEMCPY( p, value, vlen );
-                       p[vlen] = '\0';
-                       value = p;
                }
 
                ber_memfree( line );
        }
 
-       *typep = type;
-       *valuep = value;
-       *vlenp = vlen;
-
        return( 0 );
 }
 
index c1a1116360188c9c7f34e59d185d7a8f5583e29a..11dfa830229c7c618bb19ea9087c7d9b6ab5896a 100644 (file)
@@ -47,6 +47,8 @@ const Entry slap_entry_root = {
        NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
 };
 
+static const struct berval dn_bv = BER_BVC("dn");
+
 int entry_destroy(void)
 {
        if ( ebuf ) free( ebuf );
@@ -62,7 +64,7 @@ str2entry( char *s )
 {
        int rc;
        Entry           *e;
-       char            *type;
+       struct berval   type;
        struct berval   vals[2];
        struct berval   nvals[2], *nvalsp;
        AttributeDescription *ad, *ad_prev;
@@ -115,14 +117,14 @@ str2entry( char *s )
                        break;
                }
 
-               if ( ldif_parse_line2( s, &type, &vals[0].bv_val, &vals[0].bv_len,
-                       &freeval ) != 0 ) {
+               if ( ldif_parse_line2( s, &type, vals, &freeval ) != 0 ) {
                        Debug( LDAP_DEBUG_TRACE,
                                "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
                        continue;
                }
 
-               if ( strcasecmp( type, "dn" ) == 0 ) {
+               if ( type.bv_len == dn_bv.bv_len &&
+                       strcasecmp( type.bv_val, dn_bv.bv_val ) == 0 ) {
 
                        if ( e->e_dn != NULL ) {
                                Debug( LDAP_DEBUG_ANY, "str2entry: "
@@ -147,23 +149,23 @@ str2entry( char *s )
 
                ad_prev = ad;
                ad = NULL;
-               rc = slap_str2ad( type, &ad, &text );
+               rc = slap_bv2ad( &type, &ad, &text );
 
                if( rc != LDAP_SUCCESS ) {
                        Debug( slapMode & SLAP_TOOL_MODE
                                ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
-                               "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
+                               "<= str2entry: str2ad(%s): %s\n", type.bv_val, text, 0 );
                        if( slapMode & SLAP_TOOL_MODE ) {
                                entry_free( e );
                                if ( freeval ) free( vals[0].bv_val );
                                return NULL;
                        }
 
-                       rc = slap_str2undef_ad( type, &ad, &text );
+                       rc = slap_bv2undef_ad( &type, &ad, &text );
                        if( rc != LDAP_SUCCESS ) {
                                Debug( LDAP_DEBUG_ANY,
                                        "<= str2entry: str2undef_ad(%s): %s\n",
-                                               type, text, 0 );
+                                               type.bv_val, text, 0 );
                                entry_free( e );
                                if ( freeval ) free( vals[0].bv_val );
                                return NULL;