3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* attr.c - routines for dealing with attributes */
18 #include <ac/socket.h>
19 #include <ac/string.h>
26 attr_free( Attribute *a )
28 ber_bvarray_free( a->a_vals );
29 if (a->a_nvals != a->a_vals) ber_bvarray_free( a->a_nvals );
34 attrs_free( Attribute *a )
38 for( ; a != NULL ; a = next ) {
44 Attribute *attr_dup( Attribute *a )
48 if( a == NULL) return NULL;
50 tmp = ch_malloc( sizeof(Attribute) );
52 if( a->a_vals != NULL ) {
55 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
59 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
60 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
61 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
62 if( tmp->a_vals[i].bv_val == NULL ) break;
64 tmp->a_vals[i].bv_val = NULL;
66 if( a->a_nvals != a->a_vals ) {
67 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
68 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
69 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
70 if( tmp->a_nvals[i].bv_val == NULL ) break;
72 tmp->a_nvals[i].bv_val = NULL;
75 tmp->a_nvals = tmp->a_vals;
83 tmp->a_desc = a->a_desc;
90 Attribute *attrs_dup( Attribute *a )
92 Attribute *tmp, **next;
94 if( a == NULL ) return NULL;
99 for( ; a != NULL ; a = a->a_next ) {
100 *next = attr_dup( a );
101 next = &((*next)->a_next);
111 * attr_merge - merge the given type and value with the list of
112 * attributes in attrs.
114 * nvals must be NULL if the attribute has no normalizer.
115 * In this case, a->a_nvals will be set equal to a->a_vals.
117 * returns 0 everything went ok
124 AttributeDescription *desc,
132 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
133 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
139 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
142 (*a)->a_nvals = NULL;
147 rc = value_add( &(*a)->a_vals, vals );
149 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
150 else (*a)->a_nvals = (*a)->a_vals;
156 attr_merge_normalize(
158 AttributeDescription *desc,
162 BerVarray nvals = NULL;
165 if ( desc->ad_type->sat_equality && desc->ad_type->sat_equality->smr_normalize ) {
168 for ( i = 0; vals[i].bv_val; i++ );
170 nvals = ch_calloc( sizeof(struct berval), i + 1 );
171 for ( i = 0; vals[i].bv_val; i++ ) {
172 rc = (*desc->ad_type->sat_equality->smr_normalize)(
174 desc->ad_type->sat_syntax,
175 desc->ad_type->sat_equality,
176 &vals[i], &nvals[i], memctx );
178 if ( rc != LDAP_SUCCESS ) {
179 nvals[i+1].bv_val = NULL;
183 nvals[i].bv_val = NULL;
186 rc = attr_merge( e, desc, vals, nvals );
189 if ( nvals != NULL ) {
190 ber_bvarray_free( nvals );
198 AttributeDescription *desc,
205 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
206 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
212 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
215 (*a)->a_nvals = NULL;
220 rc = value_add_one( &(*a)->a_vals, val );
222 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
223 else (*a)->a_nvals = (*a)->a_vals;
228 attr_merge_normalize_one(
230 AttributeDescription *desc,
235 struct berval *nvalp;
238 if ( desc->ad_type->sat_equality && desc->ad_type->sat_equality->smr_normalize ) {
239 rc = (*desc->ad_type->sat_equality->smr_normalize)(
241 desc->ad_type->sat_syntax,
242 desc->ad_type->sat_equality,
243 val, &nval, memctx );
245 if ( rc != LDAP_SUCCESS ) {
253 rc = attr_merge_one( e, desc, val, nvalp );
254 if ( nvalp != NULL ) {
255 ch_free( nval.bv_val );
261 * attrs_find - find attribute(s) by AttributeDescription
262 * returns next attribute which is subtype of provided description.
268 AttributeDescription *desc
271 for ( ; a != NULL; a = a->a_next ) {
272 if ( is_ad_subtype( a->a_desc, desc ) ) {
281 * attr_find - find attribute by type
287 AttributeDescription *desc
290 for ( ; a != NULL; a = a->a_next ) {
291 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
300 * attr_delete - delete the attribute type in list pointed to by attrs
301 * return 0 deleted ok
302 * 1 not found in list a
303 * -1 something bad happened
309 AttributeDescription *desc
314 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
315 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
316 Attribute *save = *a;
324 return LDAP_NO_SUCH_ATTRIBUTE;