1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
37 #include <ac/socket.h>
38 #include <ac/string.h>
44 attr_free( Attribute *a )
46 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
47 ber_bvarray_free( a->a_nvals );
49 if ( a->a_vals != &slap_dummy_bv ) {
50 ber_bvarray_free( a->a_vals );
55 #ifdef LDAP_COMP_MATCH
57 comp_tree_free( Attribute *a )
61 for( ; a != NULL ; a = next ) {
63 if ( component_destructor && a->a_comp_data ) {
64 if ( a->a_comp_data->cd_mem_op )
65 component_destructor( a->a_comp_data->cd_mem_op );
66 free ( a->a_comp_data );
73 attrs_free( Attribute *a )
77 for( ; a != NULL ; a = next ) {
84 attr_dup( Attribute *a )
88 if ( a == NULL) return NULL;
90 tmp = ch_malloc( sizeof(Attribute) );
92 if ( a->a_vals != NULL ) {
95 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
99 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
100 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
101 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
102 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
105 BER_BVZERO( &tmp->a_vals[i] );
107 /* a_nvals must be non null; it may be equal to a_vals */
108 assert( a->a_nvals );
110 if ( a->a_nvals != a->a_vals ) {
111 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
112 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
113 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
114 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
117 BER_BVZERO( &tmp->a_nvals[i] );
120 tmp->a_nvals = tmp->a_vals;
128 tmp->a_desc = a->a_desc;
131 #ifdef LDAP_COMP_MATCH
132 tmp->a_comp_data = NULL;
139 attrs_dup( Attribute *a )
141 Attribute *tmp, **next;
143 if( a == NULL ) return NULL;
148 for( ; a != NULL ; a = a->a_next ) {
149 *next = attr_dup( a );
150 next = &((*next)->a_next);
160 * attr_merge - merge the given type and value with the list of
161 * attributes in attrs.
163 * nvals must be NULL if the attribute has no normalizer.
164 * In this case, a->a_nvals will be set equal to a->a_vals.
166 * returns 0 everything went ok
173 AttributeDescription *desc,
181 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
182 if ( (*a)->a_desc == desc ) {
188 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
191 (*a)->a_nvals = NULL;
194 #ifdef LDAP_COMP_MATCH
195 (*a)->a_comp_data = NULL;
199 * FIXME: if the attribute already exists, the presence
200 * of nvals and the value of (*a)->a_nvals must be consistent
202 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
203 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
206 rc = value_add( &(*a)->a_vals, vals );
208 if ( rc == LDAP_SUCCESS ) {
210 rc = value_add( &(*a)->a_nvals, nvals );
211 /* FIXME: what if rc != LDAP_SUCCESS ? */
213 (*a)->a_nvals = (*a)->a_vals;
221 attr_merge_normalize(
223 AttributeDescription *desc,
227 BerVarray nvals = NULL;
230 if ( desc->ad_type->sat_equality &&
231 desc->ad_type->sat_equality->smr_normalize )
235 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
237 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
238 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
239 rc = (*desc->ad_type->sat_equality->smr_normalize)(
240 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
241 desc->ad_type->sat_syntax,
242 desc->ad_type->sat_equality,
243 &vals[i], &nvals[i], memctx );
245 if ( rc != LDAP_SUCCESS ) {
246 BER_BVZERO( &nvals[i + 1] );
250 BER_BVZERO( &nvals[i] );
253 rc = attr_merge( e, desc, vals, nvals );
256 if ( nvals != NULL ) {
257 ber_bvarray_free_x( nvals, memctx );
265 AttributeDescription *desc,
267 struct berval *nval )
272 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
273 if ( (*a)->a_desc == desc ) {
279 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
282 (*a)->a_nvals = NULL;
285 #ifdef LDAP_COMP_MATCH
286 (*a)->a_comp_data = NULL;
290 rc = value_add_one( &(*a)->a_vals, val );
292 if ( rc == LDAP_SUCCESS ) {
294 rc = value_add_one( &(*a)->a_nvals, nval );
295 /* FIXME: what if rc != LDAP_SUCCESS ? */
297 (*a)->a_nvals = (*a)->a_vals;
304 attr_merge_normalize_one(
306 AttributeDescription *desc,
311 struct berval *nvalp = NULL;
314 if ( desc->ad_type->sat_equality &&
315 desc->ad_type->sat_equality->smr_normalize )
317 rc = (*desc->ad_type->sat_equality->smr_normalize)(
318 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
319 desc->ad_type->sat_syntax,
320 desc->ad_type->sat_equality,
321 val, &nval, memctx );
323 if ( rc != LDAP_SUCCESS ) {
329 rc = attr_merge_one( e, desc, val, nvalp );
330 if ( nvalp != NULL ) {
331 slap_sl_free( nval.bv_val, memctx );
337 * attrs_find - find attribute(s) by AttributeDescription
338 * returns next attribute which is subtype of provided description.
344 AttributeDescription *desc )
346 for ( ; a != NULL; a = a->a_next ) {
347 if ( is_ad_subtype( a->a_desc, desc ) ) {
356 * attr_find - find attribute by type
362 AttributeDescription *desc )
364 for ( ; a != NULL; a = a->a_next ) {
365 if ( a->a_desc == desc ) {
374 * attr_delete - delete the attribute type in list pointed to by attrs
375 * return 0 deleted ok
376 * 1 not found in list a
377 * -1 something bad happened
383 AttributeDescription *desc )
387 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
388 if ( (*a)->a_desc == desc ) {
389 Attribute *save = *a;
397 return LDAP_NO_SUCH_ATTRIBUTE;