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 * For SLAP_NVALUES: nvals must be NULL if the attribute has no
115 * normalizer. 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->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 ber_bvarray_free( nvals );
196 AttributeDescription *desc,
203 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
204 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
210 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
213 (*a)->a_nvals = NULL;
218 rc = value_add_one( &(*a)->a_vals, val );
220 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
221 else (*a)->a_nvals = (*a)->a_vals;
226 attr_merge_normalize_one(
228 AttributeDescription *desc,
233 struct berval *nvalp;
236 if ( desc->ad_type->sat_equality && desc->ad_type->sat_equality->smr_normalize ) {
237 rc = (*desc->ad_type->sat_equality->smr_normalize)(
239 desc->ad_type->sat_syntax,
240 desc->ad_type->sat_equality,
241 val, &nval, memctx );
243 if ( rc != LDAP_SUCCESS ) {
251 rc = attr_merge_one( e, desc, val, nvalp );
252 if ( nvalp != NULL ) {
253 ch_free( nval.bv_val );
259 * attrs_find - find attribute(s) by AttributeDescription
260 * returns next attribute which is subtype of provided description.
266 AttributeDescription *desc
269 for ( ; a != NULL; a = a->a_next ) {
270 if ( is_ad_subtype( a->a_desc, desc ) ) {
279 * attr_find - find attribute by type
285 AttributeDescription *desc
288 for ( ; a != NULL; a = a->a_next ) {
289 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
298 * attr_delete - delete the attribute type in list pointed to by attrs
299 * return 0 deleted ok
300 * 1 not found in list a
301 * -1 something bad happened
307 AttributeDescription *desc
312 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
313 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
314 Attribute *save = *a;
322 return LDAP_NO_SUCH_ATTRIBUTE;