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>
45 attr_free( Attribute *a )
47 if ( a->a_nvals && a->a_nvals != a->a_vals )
48 ber_bvarray_free( a->a_nvals );
49 ber_bvarray_free( a->a_vals );
54 attrs_free( Attribute *a )
58 for( ; a != NULL ; a = next ) {
64 Attribute *attr_dup( Attribute *a )
68 if( a == NULL) return NULL;
70 tmp = ch_malloc( sizeof(Attribute) );
72 if( a->a_vals != NULL ) {
75 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
79 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
80 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
81 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
82 if( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
84 BER_BVZERO( &tmp->a_vals[i] );
86 if( a->a_nvals != a->a_vals ) {
87 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
88 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
89 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
90 if( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
92 BER_BVZERO( &tmp->a_nvals[i] );
95 tmp->a_nvals = tmp->a_vals;
103 tmp->a_desc = a->a_desc;
110 Attribute *attrs_dup( Attribute *a )
112 Attribute *tmp, **next;
114 if( a == NULL ) return NULL;
119 for( ; a != NULL ; a = a->a_next ) {
120 *next = attr_dup( a );
121 next = &((*next)->a_next);
131 * attr_merge - merge the given type and value with the list of
132 * attributes in attrs.
134 * nvals must be NULL if the attribute has no normalizer.
135 * In this case, a->a_nvals will be set equal to a->a_vals.
137 * returns 0 everything went ok
144 AttributeDescription *desc,
152 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
153 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
159 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
162 (*a)->a_nvals = NULL;
167 rc = value_add( &(*a)->a_vals, vals );
169 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
170 else (*a)->a_nvals = (*a)->a_vals;
176 attr_merge_normalize(
178 AttributeDescription *desc,
182 BerVarray nvals = NULL;
185 if ( desc->ad_type->sat_equality &&
186 desc->ad_type->sat_equality->smr_normalize )
190 for ( i = 0; vals[i].bv_val; i++ );
192 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
193 for ( i = 0; vals[i].bv_val; i++ ) {
194 rc = (*desc->ad_type->sat_equality->smr_normalize)(
195 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
196 desc->ad_type->sat_syntax,
197 desc->ad_type->sat_equality,
198 &vals[i], &nvals[i], memctx );
200 if ( rc != LDAP_SUCCESS ) {
201 BER_BVZERO( &nvals[i+1] );
205 BER_BVZERO( &nvals[i] );
208 rc = attr_merge( e, desc, vals, nvals );
211 if ( nvals != NULL ) {
212 ber_bvarray_free_x( nvals, memctx );
220 AttributeDescription *desc,
222 struct berval *nval )
227 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
228 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
234 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
237 (*a)->a_nvals = NULL;
242 rc = value_add_one( &(*a)->a_vals, val );
244 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
245 else (*a)->a_nvals = (*a)->a_vals;
250 attr_merge_normalize_one(
252 AttributeDescription *desc,
257 struct berval *nvalp;
260 if ( desc->ad_type->sat_equality &&
261 desc->ad_type->sat_equality->smr_normalize )
263 rc = (*desc->ad_type->sat_equality->smr_normalize)(
264 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
265 desc->ad_type->sat_syntax,
266 desc->ad_type->sat_equality,
267 val, &nval, memctx );
269 if ( rc != LDAP_SUCCESS ) {
277 rc = attr_merge_one( e, desc, val, nvalp );
278 if ( nvalp != NULL ) {
279 slap_sl_free( nval.bv_val, memctx );
285 * attrs_find - find attribute(s) by AttributeDescription
286 * returns next attribute which is subtype of provided description.
292 AttributeDescription *desc )
294 for ( ; a != NULL; a = a->a_next ) {
295 if ( is_ad_subtype( a->a_desc, desc ) ) {
304 * attr_find - find attribute by type
310 AttributeDescription *desc )
312 for ( ; a != NULL; a = a->a_next ) {
313 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
322 * attr_delete - delete the attribute type in list pointed to by attrs
323 * return 0 deleted ok
324 * 1 not found in list a
325 * -1 something bad happened
331 AttributeDescription *desc )
335 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
336 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
337 Attribute *save = *a;
345 return LDAP_NO_SUCH_ATTRIBUTE;