From: Howard Chu Date: Fri, 6 Sep 2002 05:16:37 +0000 (+0000) Subject: Updated new entry parsing routines from HEAD X-Git-Tag: OPENLDAP_REL_ENG_2_1_5~36 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=f03ff692a01b183e3ba8fcffa602fbf3683b162b;p=openldap Updated new entry parsing routines from HEAD --- diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index 5c17c8e0a5..639d8c818d 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -1326,24 +1326,26 @@ print_entry( ber_free( ber, 0 ); } } -#else /* This is the proposed new way of doing things. */ +#else +/* This is the proposed new way of doing things. + * It is more * efficient, but the API is non-standard. + */ static void print_entry( LDAP *ld, LDAPMessage *entry, int attrsonly) { - char *ufn; + char *ufn = NULL; char tmpfname[ 256 ]; char url[ 256 ]; int i, rc; BerElement *ber = NULL; - struct berval *bvals, bv; + struct berval bv, *bvals, **bvp = &bvals; LDAPControl **ctrls = NULL; FILE *tmpfp; rc = ldap_get_dn_ber( ld, entry, &ber, &bv ); - ufn = NULL; if ( ldif < 2 ) { ufn = ldap_dn2ufn( bv.bv_val ); @@ -1373,17 +1375,17 @@ print_entry( if( ufn != NULL ) ldap_memfree( ufn ); - for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv ); rc == LDAP_SUCCESS; - rc = ldap_get_attribute_ber( ld, entry, ber, &bv ) ) + if ( attrsonly ) bvp = NULL; + + for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ); rc == LDAP_SUCCESS; + rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) ) { if (bv.bv_val == NULL) break; if ( attrsonly ) { write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 ); - /* skip values */ - ber_scanf( ber, "x}" ); - } else if (( rc = ldap_get_values_ber( ld, entry, ber, &bvals )) == LDAP_SUCCESS ) { + } else { for ( i = 0; bvals[i].bv_val != NULL; i++ ) { if ( vals2tmp > 1 || ( vals2tmp && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) )) diff --git a/include/ldap.h b/include/ldap.h index 710f17e49e..c1b76ef42c 100644 --- a/include/ldap.h +++ b/include/ldap.h @@ -1254,11 +1254,8 @@ ldap_get_dn_ber LDAP_P(( LDAP_F( int ) ldap_get_attribute_ber LDAP_P(( - LDAP *ld, LDAPMessage *e, BerElement *ber, struct berval *attr )); - -LDAP_F( int ) -ldap_get_values_ber LDAP_P(( - LDAP *ld, LDAPMessage *e, BerElement *ber, struct berval **bv )); + LDAP *ld, LDAPMessage *e, BerElement *ber, struct berval *attr, + struct berval **vals )); /* * in getattr.c diff --git a/libraries/libldap/getattr.c b/libraries/libldap/getattr.c index db690d13b1..34ee28da56 100644 --- a/libraries/libldap/getattr.c +++ b/libraries/libldap/getattr.c @@ -7,150 +7,171 @@ * Copyright (c) 1990 Regents of the University of Michigan. * All rights reserved. * - * getattr.c + * getvalues.c */ #include "portable.h" #include + #include +#include #include #include #include #include "ldap-int.h" -char * -ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout ) +char ** +ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target ) { - int rc; - ber_tag_t tag; - ber_len_t len = 0; - char *attr; - BerElement *ber; - -#ifdef NEW_LOGGING - LDAP_LOG ( OPERATION, ENTRY, "ldap_first_attribute\n", 0, 0, 0 ); -#else - Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 ); -#endif + BerElement ber; + char *attr; + int found = 0; + char **vals; assert( ld != NULL ); assert( LDAP_VALID( ld ) ); assert( entry != NULL ); - assert( berout != NULL ); + assert( target != NULL ); + +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values\n", 0, 0, 0 ); +#else + Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 ); +#endif - *berout = NULL; + ber = *entry->lm_ber; - ber = ldap_alloc_ber_with_options( ld ); - if( ber == NULL ) { - return NULL; + /* skip sequence, dn, sequence of, and snag the first attr */ + if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) { + ld->ld_errno = LDAP_DECODING_ERROR; + return( NULL ); } - *ber = *entry->lm_ber; + if ( strcasecmp( target, attr ) == 0 ) + found = 1; - /* - * Skip past the sequence, dn, sequence of sequence leaving - * us at the first attribute. - */ + /* break out on success, return out on error */ + while ( ! found ) { + LDAP_FREE(attr); + attr = NULL; - tag = ber_scanf( ber, "{xl{" /*}}*/, &len ); - if( tag == LBER_ERROR ) { - ld->ld_errno = LDAP_DECODING_ERROR; - ber_free( ber, 0 ); - return NULL; - } + if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) { + ld->ld_errno = LDAP_DECODING_ERROR; + return( NULL ); + } - /* set the length to avoid overrun */ - rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len ); - if( rc != LBER_OPT_SUCCESS ) { - ld->ld_errno = LDAP_LOCAL_ERROR; - ber_free( ber, 0 ); - return NULL; - } + if ( strcasecmp( target, attr ) == 0 ) + break; - if ( ber_pvt_ber_remaining( ber ) == 0 ) { - assert( len == 0 ); - ber_free( ber, 0 ); - return NULL; } - assert( len != 0 ); - /* snatch the first attribute */ - tag = ber_scanf( ber, "{ax}", &attr ); - if( tag == LBER_ERROR ) { + LDAP_FREE(attr); + attr = NULL; + + /* + * if we get this far, we've found the attribute and are sitting + * just before the set of values. + */ + + if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; - ber_free( ber, 0 ); - return NULL; + return( NULL ); } - *berout = ber; - return attr; + return( vals ); } -/* ARGSUSED */ -char * -ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber ) +struct berval ** +ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target ) { - ber_tag_t tag; - char *attr; + BerElement ber; + char *attr; + int found = 0; + struct berval **vals; + + assert( ld != NULL ); + assert( LDAP_VALID( ld ) ); + assert( entry != NULL ); + assert( target != NULL ); #ifdef NEW_LOGGING - LDAP_LOG ( OPERATION, ENTRY, "ldap_next_attribute\n", 0, 0, 0 ); + LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values_len\n", 0, 0, 0 ); #else - Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 ); + Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 ); #endif - assert( ld != NULL ); - assert( LDAP_VALID( ld ) ); - assert( entry != NULL ); - assert( ber != NULL ); + ber = *entry->lm_ber; - if ( ber_pvt_ber_remaining( ber ) == 0 ) { - return NULL; + /* skip sequence, dn, sequence of, and snag the first attr */ + if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) { + ld->ld_errno = LDAP_DECODING_ERROR; + return( NULL ); + } + + if ( strcasecmp( target, attr ) == 0 ) + found = 1; + + /* break out on success, return out on error */ + while ( ! found ) { + LDAP_FREE( attr ); + attr = NULL; + + if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) { + ld->ld_errno = LDAP_DECODING_ERROR; + return( NULL ); + } + + if ( strcasecmp( target, attr ) == 0 ) + break; } - /* skip sequence, snarf attribute type, skip values */ - tag = ber_scanf( ber, "{ax}", &attr ); - if( tag == LBER_ERROR ) { + LDAP_FREE( attr ); + attr = NULL; + + /* + * if we get this far, we've found the attribute and are sitting + * just before the set of values. + */ + + if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; - return NULL; + return( NULL ); } - return attr; + return( vals ); } -/* ARGSUSED */ int -ldap_get_attribute_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber, - BerValue *attr ) +ldap_count_values( char **vals ) { - ber_tag_t tag; - int rc = LDAP_SUCCESS; + int i; -#ifdef NEW_LOGGING - LDAP_LOG ( OPERATION, ENTRY, "ldap_get_attribute_ber\n", 0, 0, 0 ); -#else - Debug( LDAP_DEBUG_TRACE, "ldap_get_attribute_ber\n", 0, 0, 0 ); -#endif + if ( vals == NULL ) + return( 0 ); - assert( ld != NULL ); - assert( LDAP_VALID( ld ) ); - assert( entry != NULL ); - assert( ber != NULL ); - assert( attr != NULL ); + for ( i = 0; vals[i] != NULL; i++ ) + ; /* NULL */ - attr->bv_val = NULL; - attr->bv_len = 0; + return( i ); +} - if ( ber_pvt_ber_remaining( ber ) ) { - /* skip sequence, snarf attribute type */ - tag = ber_scanf( ber, "{m", attr ); - if( tag == LBER_ERROR ) { - rc = ld->ld_errno = LDAP_DECODING_ERROR; - } - } +int +ldap_count_values_len( struct berval **vals ) +{ + return( ldap_count_values( (char **) vals ) ); +} + +void +ldap_value_free( char **vals ) +{ + LDAP_VFREE( vals ); +} - return rc; +void +ldap_value_free_len( struct berval **vals ) +{ + ber_bvecfree( vals ); } diff --git a/libraries/libldap/getvalues.c b/libraries/libldap/getvalues.c index ee2962b7c3..34ee28da56 100644 --- a/libraries/libldap/getvalues.c +++ b/libraries/libldap/getvalues.c @@ -144,30 +144,6 @@ ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target ) return( vals ); } -int -ldap_get_values_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber, BerVarray *bv ) -{ - int rc = LDAP_SUCCESS; - - assert( ld != NULL ); - assert( LDAP_VALID( ld ) ); - assert( entry != NULL ); - assert( ber != NULL ); - assert( bv != NULL ); - -#ifdef NEW_LOGGING - LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values_ber\n", 0, 0, 0 ); -#else - Debug( LDAP_DEBUG_TRACE, "ldap_get_values_ber\n", 0, 0, 0 ); -#endif - - /* get the array of vals */ - if ( ber_scanf( ber, "W}" /* }}} */, bv ) == LBER_ERROR ) { - rc = ld->ld_errno = LDAP_DECODING_ERROR; - } - - return( rc ); -} int ldap_count_values( char **vals ) {