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 );
53 attrs_free( Attribute *a )
57 for( ; a != NULL ; a = next ) {
64 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; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
79 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
80 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
81 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
82 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
85 BER_BVZERO( &tmp->a_vals[i] );
87 /* a_nvals must be non null; it may be equal to a_vals */
90 if ( a->a_nvals != a->a_vals ) {
91 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
92 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
93 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
94 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
97 BER_BVZERO( &tmp->a_nvals[i] );
100 tmp->a_nvals = tmp->a_vals;
108 tmp->a_desc = a->a_desc;
116 attrs_dup( Attribute *a )
118 Attribute *tmp, **next;
120 if( a == NULL ) return NULL;
125 for( ; a != NULL ; a = a->a_next ) {
126 *next = attr_dup( a );
127 next = &((*next)->a_next);
137 * attr_merge - merge the given type and value with the list of
138 * attributes in attrs.
140 * nvals must be NULL if the attribute has no normalizer.
141 * In this case, a->a_nvals will be set equal to a->a_vals.
143 * returns 0 everything went ok
150 AttributeDescription *desc,
158 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
159 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
165 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
168 (*a)->a_nvals = NULL;
173 rc = value_add( &(*a)->a_vals, vals );
175 if ( rc == LDAP_SUCCESS ) {
177 rc = value_add( &(*a)->a_nvals, nvals );
178 /* FIXME: what if rc != LDAP_SUCCESS ? */
180 (*a)->a_nvals = (*a)->a_vals;
188 attr_merge_normalize(
190 AttributeDescription *desc,
194 BerVarray nvals = NULL;
197 if ( desc->ad_type->sat_equality &&
198 desc->ad_type->sat_equality->smr_normalize )
202 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
204 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
205 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
206 rc = (*desc->ad_type->sat_equality->smr_normalize)(
207 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
208 desc->ad_type->sat_syntax,
209 desc->ad_type->sat_equality,
210 &vals[i], &nvals[i], memctx );
212 if ( rc != LDAP_SUCCESS ) {
213 BER_BVZERO( &nvals[i + 1] );
217 BER_BVZERO( &nvals[i] );
220 rc = attr_merge( e, desc, vals, nvals );
223 if ( nvals != NULL ) {
224 ber_bvarray_free_x( nvals, memctx );
232 AttributeDescription *desc,
234 struct berval *nval )
239 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
240 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
246 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
249 (*a)->a_nvals = NULL;
254 rc = value_add_one( &(*a)->a_vals, val );
256 if ( rc == LDAP_SUCCESS ) {
258 rc = value_add_one( &(*a)->a_nvals, nval );
259 /* FIXME: what if rc != LDAP_SUCCESS ? */
261 (*a)->a_nvals = (*a)->a_vals;
268 attr_merge_normalize_one(
270 AttributeDescription *desc,
275 struct berval *nvalp = NULL;
278 if ( desc->ad_type->sat_equality &&
279 desc->ad_type->sat_equality->smr_normalize )
281 rc = (*desc->ad_type->sat_equality->smr_normalize)(
282 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
283 desc->ad_type->sat_syntax,
284 desc->ad_type->sat_equality,
285 val, &nval, memctx );
287 if ( rc != LDAP_SUCCESS ) {
293 rc = attr_merge_one( e, desc, val, nvalp );
294 if ( nvalp != NULL ) {
295 slap_sl_free( nval.bv_val, memctx );
301 * attrs_find - find attribute(s) by AttributeDescription
302 * returns next attribute which is subtype of provided description.
308 AttributeDescription *desc )
310 for ( ; a != NULL; a = a->a_next ) {
311 if ( is_ad_subtype( a->a_desc, desc ) ) {
320 * attr_find - find attribute by type
326 AttributeDescription *desc )
328 for ( ; a != NULL; a = a->a_next ) {
329 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
338 * attr_delete - delete the attribute type in list pointed to by attrs
339 * return 0 deleted ok
340 * 1 not found in list a
341 * -1 something bad happened
347 AttributeDescription *desc )
351 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
352 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
353 Attribute *save = *a;
361 return LDAP_NO_SUCH_ATTRIBUTE;