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 ber_bvarray_free( a->a_vals );
53 #ifdef LDAP_COMP_MATCH
55 comp_tree_free( Attribute *a )
59 for( ; a != NULL ; a = next ) {
61 if ( component_destructor && a->a_comp_data ) {
62 if ( a->a_comp_data->cd_mem_op )
63 component_destructor( a->a_comp_data->cd_mem_op );
64 free ( a->a_comp_data );
71 attrs_free( Attribute *a )
75 for( ; a != NULL ; a = next ) {
82 attr_dup( Attribute *a )
86 if ( a == NULL) return NULL;
88 tmp = ch_malloc( sizeof(Attribute) );
90 if ( a->a_vals != NULL ) {
93 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
97 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
98 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
99 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
100 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
103 BER_BVZERO( &tmp->a_vals[i] );
105 /* a_nvals must be non null; it may be equal to a_vals */
106 assert( a->a_nvals );
108 if ( a->a_nvals != a->a_vals ) {
109 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
110 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
111 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
112 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
115 BER_BVZERO( &tmp->a_nvals[i] );
118 tmp->a_nvals = tmp->a_vals;
126 tmp->a_desc = a->a_desc;
129 #ifdef LDAP_COMP_MATCH
130 tmp->a_comp_data = NULL;
137 attrs_dup( Attribute *a )
139 Attribute *tmp, **next;
141 if( a == NULL ) return NULL;
146 for( ; a != NULL ; a = a->a_next ) {
147 *next = attr_dup( a );
148 next = &((*next)->a_next);
158 * attr_merge - merge the given type and value with the list of
159 * attributes in attrs.
161 * nvals must be NULL if the attribute has no normalizer.
162 * In this case, a->a_nvals will be set equal to a->a_vals.
164 * returns 0 everything went ok
171 AttributeDescription *desc,
179 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
180 if ( (*a)->a_desc == desc ) {
186 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
189 (*a)->a_nvals = NULL;
192 #ifdef LDAP_COMP_MATCH
193 (*a)->a_comp_data = NULL;
197 rc = value_add( &(*a)->a_vals, vals );
199 if ( rc == LDAP_SUCCESS ) {
201 rc = value_add( &(*a)->a_nvals, nvals );
202 /* FIXME: what if rc != LDAP_SUCCESS ? */
204 (*a)->a_nvals = (*a)->a_vals;
212 attr_merge_normalize(
214 AttributeDescription *desc,
218 BerVarray nvals = NULL;
221 if ( desc->ad_type->sat_equality &&
222 desc->ad_type->sat_equality->smr_normalize )
226 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
228 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
229 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
230 rc = (*desc->ad_type->sat_equality->smr_normalize)(
231 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
232 desc->ad_type->sat_syntax,
233 desc->ad_type->sat_equality,
234 &vals[i], &nvals[i], memctx );
236 if ( rc != LDAP_SUCCESS ) {
237 BER_BVZERO( &nvals[i + 1] );
241 BER_BVZERO( &nvals[i] );
244 rc = attr_merge( e, desc, vals, nvals );
247 if ( nvals != NULL ) {
248 ber_bvarray_free_x( nvals, memctx );
256 AttributeDescription *desc,
258 struct berval *nval )
263 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
264 if ( (*a)->a_desc == desc ) {
270 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
273 (*a)->a_nvals = NULL;
276 #ifdef LDAP_COMP_MATCH
277 (*a)->a_comp_data = NULL;
281 rc = value_add_one( &(*a)->a_vals, val );
283 if ( rc == LDAP_SUCCESS ) {
285 rc = value_add_one( &(*a)->a_nvals, nval );
286 /* FIXME: what if rc != LDAP_SUCCESS ? */
288 (*a)->a_nvals = (*a)->a_vals;
295 attr_merge_normalize_one(
297 AttributeDescription *desc,
302 struct berval *nvalp = NULL;
305 if ( desc->ad_type->sat_equality &&
306 desc->ad_type->sat_equality->smr_normalize )
308 rc = (*desc->ad_type->sat_equality->smr_normalize)(
309 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
310 desc->ad_type->sat_syntax,
311 desc->ad_type->sat_equality,
312 val, &nval, memctx );
314 if ( rc != LDAP_SUCCESS ) {
320 rc = attr_merge_one( e, desc, val, nvalp );
321 if ( nvalp != NULL ) {
322 slap_sl_free( nval.bv_val, memctx );
328 * attrs_find - find attribute(s) by AttributeDescription
329 * returns next attribute which is subtype of provided description.
335 AttributeDescription *desc )
337 for ( ; a != NULL; a = a->a_next ) {
338 if ( is_ad_subtype( a->a_desc, desc ) ) {
347 * attr_find - find attribute by type
353 AttributeDescription *desc )
355 for ( ; a != NULL; a = a->a_next ) {
356 if ( a->a_desc == desc ) {
365 * attr_delete - delete the attribute type in list pointed to by attrs
366 * return 0 deleted ok
367 * 1 not found in list a
368 * -1 something bad happened
374 AttributeDescription *desc )
378 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
379 if ( (*a)->a_desc == desc ) {
380 Attribute *save = *a;
388 return LDAP_NO_SUCH_ATTRIBUTE;