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 &&
166 desc->ad_type->sat_equality->smr_normalize )
170 for ( i = 0; vals[i].bv_val; i++ );
172 nvals = sl_calloc( sizeof(struct berval), i + 1, memctx );
173 for ( i = 0; vals[i].bv_val; i++ ) {
174 rc = (*desc->ad_type->sat_equality->smr_normalize)(
175 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
176 desc->ad_type->sat_syntax,
177 desc->ad_type->sat_equality,
178 &vals[i], &nvals[i], memctx );
180 if ( rc != LDAP_SUCCESS ) {
181 nvals[i+1].bv_val = NULL;
185 nvals[i].bv_val = NULL;
188 rc = attr_merge( e, desc, vals, nvals );
191 if ( nvals != NULL ) {
192 ber_bvarray_free_x( nvals, memctx );
200 AttributeDescription *desc,
207 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
208 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
214 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
217 (*a)->a_nvals = NULL;
222 rc = value_add_one( &(*a)->a_vals, val );
224 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
225 else (*a)->a_nvals = (*a)->a_vals;
230 attr_merge_normalize_one(
232 AttributeDescription *desc,
237 struct berval *nvalp;
240 if ( desc->ad_type->sat_equality &&
241 desc->ad_type->sat_equality->smr_normalize )
243 rc = (*desc->ad_type->sat_equality->smr_normalize)(
244 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
245 desc->ad_type->sat_syntax,
246 desc->ad_type->sat_equality,
247 val, &nval, memctx );
249 if ( rc != LDAP_SUCCESS ) {
257 rc = attr_merge_one( e, desc, val, nvalp );
258 if ( nvalp != NULL ) {
259 sl_free( nval.bv_val, memctx );
265 * attrs_find - find attribute(s) by AttributeDescription
266 * returns next attribute which is subtype of provided description.
272 AttributeDescription *desc
275 for ( ; a != NULL; a = a->a_next ) {
276 if ( is_ad_subtype( a->a_desc, desc ) ) {
285 * attr_find - find attribute by type
291 AttributeDescription *desc
294 for ( ; a != NULL; a = a->a_next ) {
295 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
304 * attr_delete - delete the attribute type in list pointed to by attrs
305 * return 0 deleted ok
306 * 1 not found in list a
307 * -1 something bad happened
313 AttributeDescription *desc
318 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
319 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
320 Attribute *save = *a;
328 return LDAP_NO_SUCH_ATTRIBUTE;