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 ) {
65 attr_dup( Attribute *a )
69 if ( a == NULL) return NULL;
71 tmp = ch_malloc( sizeof(Attribute) );
73 if ( a->a_vals != NULL ) {
76 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
80 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
81 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
82 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
83 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
86 BER_BVZERO( &tmp->a_vals[i] );
88 /* a_nvals must be non null; it may be equal to a_vals */
91 if ( a->a_nvals != a->a_vals ) {
92 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
93 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
94 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
95 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
98 BER_BVZERO( &tmp->a_nvals[i] );
101 tmp->a_nvals = tmp->a_vals;
109 tmp->a_desc = a->a_desc;
117 attrs_dup( Attribute *a )
119 Attribute *tmp, **next;
121 if( a == NULL ) return NULL;
126 for( ; a != NULL ; a = a->a_next ) {
127 *next = attr_dup( a );
128 next = &((*next)->a_next);
138 * attr_merge - merge the given type and value with the list of
139 * attributes in attrs.
141 * nvals must be NULL if the attribute has no normalizer.
142 * In this case, a->a_nvals will be set equal to a->a_vals.
144 * returns 0 everything went ok
151 AttributeDescription *desc,
159 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
160 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
166 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
169 (*a)->a_nvals = NULL;
174 rc = value_add( &(*a)->a_vals, vals );
176 if ( rc == LDAP_SUCCESS ) {
178 rc = value_add( &(*a)->a_nvals, nvals );
179 /* FIXME: what if rc != LDAP_SUCCESS ? */
181 (*a)->a_nvals = (*a)->a_vals;
189 attr_merge_normalize(
191 AttributeDescription *desc,
195 BerVarray nvals = NULL;
198 if ( desc->ad_type->sat_equality &&
199 desc->ad_type->sat_equality->smr_normalize )
203 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
205 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
206 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
207 rc = (*desc->ad_type->sat_equality->smr_normalize)(
208 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
209 desc->ad_type->sat_syntax,
210 desc->ad_type->sat_equality,
211 &vals[i], &nvals[i], memctx );
213 if ( rc != LDAP_SUCCESS ) {
214 BER_BVZERO( &nvals[i + 1] );
218 BER_BVZERO( &nvals[i] );
221 rc = attr_merge( e, desc, vals, nvals );
224 if ( nvals != NULL ) {
225 ber_bvarray_free_x( nvals, memctx );
233 AttributeDescription *desc,
235 struct berval *nval )
240 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
241 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
247 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
250 (*a)->a_nvals = NULL;
255 rc = value_add_one( &(*a)->a_vals, val );
257 if ( rc == LDAP_SUCCESS ) {
259 rc = value_add_one( &(*a)->a_nvals, nval );
260 /* FIXME: what if rc != LDAP_SUCCESS ? */
262 (*a)->a_nvals = (*a)->a_vals;
269 attr_merge_normalize_one(
271 AttributeDescription *desc,
276 struct berval *nvalp = NULL;
279 if ( desc->ad_type->sat_equality &&
280 desc->ad_type->sat_equality->smr_normalize )
282 rc = (*desc->ad_type->sat_equality->smr_normalize)(
283 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
284 desc->ad_type->sat_syntax,
285 desc->ad_type->sat_equality,
286 val, &nval, memctx );
288 if ( rc != LDAP_SUCCESS ) {
294 rc = attr_merge_one( e, desc, val, nvalp );
295 if ( nvalp != NULL ) {
296 slap_sl_free( nval.bv_val, memctx );
302 * attrs_find - find attribute(s) by AttributeDescription
303 * returns next attribute which is subtype of provided description.
309 AttributeDescription *desc )
311 for ( ; a != NULL; a = a->a_next ) {
312 if ( is_ad_subtype( a->a_desc, desc ) ) {
321 * attr_find - find attribute by type
327 AttributeDescription *desc )
329 for ( ; a != NULL; a = a->a_next ) {
330 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
339 * attr_delete - delete the attribute type in list pointed to by attrs
340 * return 0 deleted ok
341 * 1 not found in list a
342 * -1 something bad happened
348 AttributeDescription *desc )
352 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
353 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
354 Attribute *save = *a;
362 return LDAP_NO_SUCH_ATTRIBUTE;