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>
44 attr_free( Attribute *a )
46 if ( a->a_nvals && a->a_nvals != a->a_vals )
47 ber_bvarray_free( a->a_nvals );
48 ber_bvarray_free( a->a_vals );
52 #ifdef LDAP_COMP_MATCH
54 comp_tree_free( Attribute *a )
58 for( ; a != NULL ; a = next ) {
60 if ( component_destructor && a->a_comp_data &&
61 a->a_comp_data->cd_mem_op ) {
62 component_destructor( a->a_comp_data->cd_mem_op );
63 free ( a->a_comp_data );
70 attrs_free( Attribute *a )
74 for( ; a != NULL ; a = next ) {
81 attr_dup( Attribute *a )
85 if ( a == NULL) return NULL;
87 tmp = ch_malloc( sizeof(Attribute) );
89 if ( a->a_vals != NULL ) {
92 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
96 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
97 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
98 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
99 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
102 BER_BVZERO( &tmp->a_vals[i] );
104 /* a_nvals must be non null; it may be equal to a_vals */
105 assert( a->a_nvals );
107 if ( a->a_nvals != a->a_vals ) {
108 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
109 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
110 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
111 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
114 BER_BVZERO( &tmp->a_nvals[i] );
117 tmp->a_nvals = tmp->a_vals;
125 tmp->a_desc = a->a_desc;
128 #ifdef LDAP_COMP_MATCH
129 tmp->a_comp_data = NULL;
136 attrs_dup( Attribute *a )
138 Attribute *tmp, **next;
140 if( a == NULL ) return NULL;
145 for( ; a != NULL ; a = a->a_next ) {
146 *next = attr_dup( a );
147 next = &((*next)->a_next);
157 * attr_merge - merge the given type and value with the list of
158 * attributes in attrs.
160 * nvals must be NULL if the attribute has no normalizer.
161 * In this case, a->a_nvals will be set equal to a->a_vals.
163 * returns 0 everything went ok
170 AttributeDescription *desc,
178 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
179 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
185 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
188 (*a)->a_nvals = NULL;
191 #ifdef LDAP_COMP_MATCH
192 (*a)->a_comp_data = NULL;
196 rc = value_add( &(*a)->a_vals, vals );
198 if ( rc == LDAP_SUCCESS ) {
200 rc = value_add( &(*a)->a_nvals, nvals );
201 /* FIXME: what if rc != LDAP_SUCCESS ? */
203 (*a)->a_nvals = (*a)->a_vals;
211 attr_merge_normalize(
213 AttributeDescription *desc,
217 BerVarray nvals = NULL;
220 if ( desc->ad_type->sat_equality &&
221 desc->ad_type->sat_equality->smr_normalize )
225 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
227 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
228 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
229 rc = (*desc->ad_type->sat_equality->smr_normalize)(
230 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
231 desc->ad_type->sat_syntax,
232 desc->ad_type->sat_equality,
233 &vals[i], &nvals[i], memctx );
235 if ( rc != LDAP_SUCCESS ) {
236 BER_BVZERO( &nvals[i + 1] );
240 BER_BVZERO( &nvals[i] );
243 rc = attr_merge( e, desc, vals, nvals );
246 if ( nvals != NULL ) {
247 ber_bvarray_free_x( nvals, memctx );
255 AttributeDescription *desc,
257 struct berval *nval )
262 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
263 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
269 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
272 (*a)->a_nvals = NULL;
275 #ifdef LDAP_COMP_MATCH
276 (*a)->a_comp_data = NULL;
280 rc = value_add_one( &(*a)->a_vals, val );
282 if ( rc == LDAP_SUCCESS ) {
284 rc = value_add_one( &(*a)->a_nvals, nval );
285 /* FIXME: what if rc != LDAP_SUCCESS ? */
287 (*a)->a_nvals = (*a)->a_vals;
294 attr_merge_normalize_one(
296 AttributeDescription *desc,
301 struct berval *nvalp = NULL;
304 if ( desc->ad_type->sat_equality &&
305 desc->ad_type->sat_equality->smr_normalize )
307 rc = (*desc->ad_type->sat_equality->smr_normalize)(
308 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
309 desc->ad_type->sat_syntax,
310 desc->ad_type->sat_equality,
311 val, &nval, memctx );
313 if ( rc != LDAP_SUCCESS ) {
319 rc = attr_merge_one( e, desc, val, nvalp );
320 if ( nvalp != NULL ) {
321 slap_sl_free( nval.bv_val, memctx );
327 * attrs_find - find attribute(s) by AttributeDescription
328 * returns next attribute which is subtype of provided description.
334 AttributeDescription *desc )
336 for ( ; a != NULL; a = a->a_next ) {
337 if ( is_ad_subtype( a->a_desc, desc ) ) {
346 * attr_find - find attribute by type
352 AttributeDescription *desc )
354 for ( ; a != NULL; a = a->a_next ) {
355 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
364 * attr_delete - delete the attribute type in list pointed to by attrs
365 * return 0 deleted ok
366 * 1 not found in list a
367 * -1 something bad happened
373 AttributeDescription *desc )
377 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
378 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
379 Attribute *save = *a;
387 return LDAP_NO_SUCH_ATTRIBUTE;