1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
43 #ifdef LDAP_COMP_MATCH
44 extern free_component_func* component_destructor;
47 attr_free( Attribute *a )
49 if ( a->a_nvals && a->a_nvals != a->a_vals )
50 ber_bvarray_free( a->a_nvals );
51 ber_bvarray_free( a->a_vals );
52 #ifdef LDAP_COMP_MATCH
53 if ( component_destructor && a->a_comp_data && a->a_comp_data->cd_mem_op ) {
54 component_destructor( a->a_comp_data->cd_mem_op );
55 free ( a->a_comp_data );
62 attrs_free( Attribute *a )
66 for( ; a != NULL ; a = next ) {
73 attr_dup( Attribute *a )
77 if ( a == NULL) return NULL;
79 tmp = ch_malloc( sizeof(Attribute) );
81 if ( a->a_vals != NULL ) {
84 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
88 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
89 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
90 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
91 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
94 BER_BVZERO( &tmp->a_vals[i] );
96 /* a_nvals must be non null; it may be equal to a_vals */
99 if ( a->a_nvals != a->a_vals ) {
100 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
101 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
102 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
103 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
106 BER_BVZERO( &tmp->a_nvals[i] );
109 tmp->a_nvals = tmp->a_vals;
117 tmp->a_desc = a->a_desc;
120 #ifdef LDAP_COMP_MATCH
121 tmp->a_comp_data = NULL;
128 attrs_dup( Attribute *a )
130 Attribute *tmp, **next;
132 if( a == NULL ) return NULL;
137 for( ; a != NULL ; a = a->a_next ) {
138 *next = attr_dup( a );
139 next = &((*next)->a_next);
149 * attr_merge - merge the given type and value with the list of
150 * attributes in attrs.
152 * nvals must be NULL if the attribute has no normalizer.
153 * In this case, a->a_nvals will be set equal to a->a_vals.
155 * returns 0 everything went ok
162 AttributeDescription *desc,
170 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
171 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
177 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
180 (*a)->a_nvals = NULL;
183 #ifdef LDAP_COMP_MATCH
184 (*a)->a_comp_data = NULL;
188 rc = value_add( &(*a)->a_vals, vals );
190 if ( rc == LDAP_SUCCESS ) {
192 rc = value_add( &(*a)->a_nvals, nvals );
193 /* FIXME: what if rc != LDAP_SUCCESS ? */
195 (*a)->a_nvals = (*a)->a_vals;
203 attr_merge_normalize(
205 AttributeDescription *desc,
209 BerVarray nvals = NULL;
212 if ( desc->ad_type->sat_equality &&
213 desc->ad_type->sat_equality->smr_normalize )
217 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
219 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
220 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
221 rc = (*desc->ad_type->sat_equality->smr_normalize)(
222 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
223 desc->ad_type->sat_syntax,
224 desc->ad_type->sat_equality,
225 &vals[i], &nvals[i], memctx );
227 if ( rc != LDAP_SUCCESS ) {
228 BER_BVZERO( &nvals[i + 1] );
232 BER_BVZERO( &nvals[i] );
235 rc = attr_merge( e, desc, vals, nvals );
238 if ( nvals != NULL ) {
239 ber_bvarray_free_x( nvals, memctx );
247 AttributeDescription *desc,
249 struct berval *nval )
254 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
255 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
261 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
264 (*a)->a_nvals = NULL;
267 #ifdef LDAP_COMP_MATCH
268 (*a)->a_comp_data = NULL;
272 rc = value_add_one( &(*a)->a_vals, val );
274 if ( rc == LDAP_SUCCESS ) {
276 rc = value_add_one( &(*a)->a_nvals, nval );
277 /* FIXME: what if rc != LDAP_SUCCESS ? */
279 (*a)->a_nvals = (*a)->a_vals;
286 attr_merge_normalize_one(
288 AttributeDescription *desc,
293 struct berval *nvalp = NULL;
296 if ( desc->ad_type->sat_equality &&
297 desc->ad_type->sat_equality->smr_normalize )
299 rc = (*desc->ad_type->sat_equality->smr_normalize)(
300 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
301 desc->ad_type->sat_syntax,
302 desc->ad_type->sat_equality,
303 val, &nval, memctx );
305 if ( rc != LDAP_SUCCESS ) {
311 rc = attr_merge_one( e, desc, val, nvalp );
312 if ( nvalp != NULL ) {
313 slap_sl_free( nval.bv_val, memctx );
319 * attrs_find - find attribute(s) by AttributeDescription
320 * returns next attribute which is subtype of provided description.
326 AttributeDescription *desc )
328 for ( ; a != NULL; a = a->a_next ) {
329 if ( is_ad_subtype( a->a_desc, desc ) ) {
338 * attr_find - find attribute by type
344 AttributeDescription *desc )
346 for ( ; a != NULL; a = a->a_next ) {
347 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
356 * attr_delete - delete the attribute type in list pointed to by attrs
357 * return 0 deleted ok
358 * 1 not found in list a
359 * -1 something bad happened
365 AttributeDescription *desc )
369 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
370 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
371 Attribute *save = *a;
379 return LDAP_NO_SUCH_ATTRIBUTE;