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 * FIXME: if the attribute already exists, the presence
198 * of nvals and the value of (*a)->a_nvals must be consistent
200 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
201 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
204 rc = value_add( &(*a)->a_vals, vals );
206 if ( rc == LDAP_SUCCESS ) {
208 rc = value_add( &(*a)->a_nvals, nvals );
209 /* FIXME: what if rc != LDAP_SUCCESS ? */
211 (*a)->a_nvals = (*a)->a_vals;
219 attr_merge_normalize(
221 AttributeDescription *desc,
225 BerVarray nvals = NULL;
228 if ( desc->ad_type->sat_equality &&
229 desc->ad_type->sat_equality->smr_normalize )
233 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
235 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
236 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
237 rc = (*desc->ad_type->sat_equality->smr_normalize)(
238 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
239 desc->ad_type->sat_syntax,
240 desc->ad_type->sat_equality,
241 &vals[i], &nvals[i], memctx );
243 if ( rc != LDAP_SUCCESS ) {
244 BER_BVZERO( &nvals[i + 1] );
248 BER_BVZERO( &nvals[i] );
251 rc = attr_merge( e, desc, vals, nvals );
254 if ( nvals != NULL ) {
255 ber_bvarray_free_x( nvals, memctx );
263 AttributeDescription *desc,
265 struct berval *nval )
270 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
271 if ( (*a)->a_desc == desc ) {
277 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
280 (*a)->a_nvals = NULL;
283 #ifdef LDAP_COMP_MATCH
284 (*a)->a_comp_data = NULL;
288 rc = value_add_one( &(*a)->a_vals, val );
290 if ( rc == LDAP_SUCCESS ) {
292 rc = value_add_one( &(*a)->a_nvals, nval );
293 /* FIXME: what if rc != LDAP_SUCCESS ? */
295 (*a)->a_nvals = (*a)->a_vals;
302 attr_merge_normalize_one(
304 AttributeDescription *desc,
309 struct berval *nvalp = NULL;
312 if ( desc->ad_type->sat_equality &&
313 desc->ad_type->sat_equality->smr_normalize )
315 rc = (*desc->ad_type->sat_equality->smr_normalize)(
316 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
317 desc->ad_type->sat_syntax,
318 desc->ad_type->sat_equality,
319 val, &nval, memctx );
321 if ( rc != LDAP_SUCCESS ) {
327 rc = attr_merge_one( e, desc, val, nvalp );
328 if ( nvalp != NULL ) {
329 slap_sl_free( nval.bv_val, memctx );
335 * attrs_find - find attribute(s) by AttributeDescription
336 * returns next attribute which is subtype of provided description.
342 AttributeDescription *desc )
344 for ( ; a != NULL; a = a->a_next ) {
345 if ( is_ad_subtype( a->a_desc, desc ) ) {
354 * attr_find - find attribute by type
360 AttributeDescription *desc )
362 for ( ; a != NULL; a = a->a_next ) {
363 if ( a->a_desc == desc ) {
372 * attr_delete - delete the attribute type in list pointed to by attrs
373 * return 0 deleted ok
374 * 1 not found in list a
375 * -1 something bad happened
381 AttributeDescription *desc )
385 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
386 if ( (*a)->a_desc == desc ) {
387 Attribute *save = *a;
395 return LDAP_NO_SUCH_ATTRIBUTE;