Back to index

openldap  2.4.31
getvalues.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 
00019 #include "portable.h"
00020 
00021 #include <stdio.h>
00022 
00023 #include <ac/stdlib.h>
00024 
00025 #include <ac/ctype.h>
00026 #include <ac/socket.h>
00027 #include <ac/string.h>
00028 #include <ac/time.h>
00029 
00030 #include "ldap-int.h"
00031 
00032 char **
00033 ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
00034 {
00035        BerElement    ber;
00036        char          *attr;
00037        int           found = 0;
00038        char          **vals;
00039 
00040        assert( ld != NULL );
00041        assert( LDAP_VALID( ld ) );
00042        assert( entry != NULL );
00043        assert( target != NULL );
00044 
00045        Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
00046 
00047        ber = *entry->lm_ber;
00048 
00049        /* skip sequence, dn, sequence of, and snag the first attr */
00050        if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
00051               ld->ld_errno = LDAP_DECODING_ERROR;
00052               return( NULL );
00053        }
00054 
00055        if ( strcasecmp( target, attr ) == 0 )
00056               found = 1;
00057 
00058        /* break out on success, return out on error */
00059        while ( ! found ) {
00060               LDAP_FREE(attr);
00061               attr = NULL;
00062 
00063               if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
00064                      ld->ld_errno = LDAP_DECODING_ERROR;
00065                      return( NULL );
00066               }
00067 
00068               if ( strcasecmp( target, attr ) == 0 )
00069                      break;
00070 
00071        }
00072 
00073        LDAP_FREE(attr);
00074        attr = NULL;
00075 
00076        /* 
00077         * if we get this far, we've found the attribute and are sitting
00078         * just before the set of values.
00079         */
00080 
00081        if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
00082               ld->ld_errno = LDAP_DECODING_ERROR;
00083               return( NULL );
00084        }
00085 
00086        return( vals );
00087 }
00088 
00089 struct berval **
00090 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
00091 {
00092        BerElement    ber;
00093        char          *attr;
00094        int           found = 0;
00095        struct berval **vals;
00096 
00097        assert( ld != NULL );
00098        assert( LDAP_VALID( ld ) );
00099        assert( entry != NULL );
00100        assert( target != NULL );
00101 
00102        Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
00103 
00104        ber = *entry->lm_ber;
00105 
00106        /* skip sequence, dn, sequence of, and snag the first attr */
00107        if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
00108               ld->ld_errno = LDAP_DECODING_ERROR;
00109               return( NULL );
00110        }
00111 
00112        if ( strcasecmp( target, attr ) == 0 )
00113               found = 1;
00114 
00115        /* break out on success, return out on error */
00116        while ( ! found ) {
00117               LDAP_FREE( attr );
00118               attr = NULL;
00119 
00120               if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
00121                      ld->ld_errno = LDAP_DECODING_ERROR;
00122                      return( NULL );
00123               }
00124 
00125               if ( strcasecmp( target, attr ) == 0 )
00126                      break;
00127        }
00128 
00129        LDAP_FREE( attr );
00130        attr = NULL;
00131 
00132        /* 
00133         * if we get this far, we've found the attribute and are sitting
00134         * just before the set of values.
00135         */
00136 
00137        if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
00138               ld->ld_errno = LDAP_DECODING_ERROR;
00139               return( NULL );
00140        }
00141 
00142        return( vals );
00143 }
00144 
00145 int
00146 ldap_count_values( char **vals )
00147 {
00148        int    i;
00149 
00150        if ( vals == NULL )
00151               return( 0 );
00152 
00153        for ( i = 0; vals[i] != NULL; i++ )
00154               ;      /* NULL */
00155 
00156        return( i );
00157 }
00158 
00159 int
00160 ldap_count_values_len( struct berval **vals )
00161 {
00162        return( ldap_count_values( (char **) vals ) );
00163 }
00164 
00165 void
00166 ldap_value_free( char **vals )
00167 {
00168        LDAP_VFREE( vals );
00169 }
00170 
00171 void
00172 ldap_value_free_len( struct berval **vals )
00173 {
00174        ber_bvecfree( vals );
00175 }
00176 
00177 char **
00178 ldap_value_dup( char *const *vals )
00179 {
00180        char **new;
00181        int i;
00182 
00183        if( vals == NULL ) {
00184               return NULL;
00185        }
00186 
00187        for( i=0; vals[i]; i++ ) {
00188               ;   /* Count the number of values */
00189        }
00190 
00191        if( i == 0 ) {
00192               return NULL;
00193        }
00194 
00195        new = LDAP_MALLOC( (i+1)*sizeof(char *) );  /* Alloc array of pointers */
00196        if( new == NULL ) {
00197               return NULL;
00198        }
00199 
00200        for( i=0; vals[i]; i++ ) {
00201               new[i] = LDAP_STRDUP( vals[i] );   /* Dup each value */
00202               if( new[i] == NULL ) {
00203                      LDAP_VFREE( new );
00204                      return NULL;
00205               }
00206        }
00207        new[i] = NULL;
00208 
00209        return new;
00210 }
00211