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 ber_bvarray_free( a->a_vals );
48 if (a->a_nvals != a->a_vals) ber_bvarray_free( a->a_nvals );
53 attrs_free( Attribute *a )
57 for( ; a != NULL ; a = next ) {
63 Attribute *attr_dup( Attribute *a )
67 if( a == NULL) return NULL;
69 tmp = ch_malloc( sizeof(Attribute) );
71 if( a->a_vals != NULL ) {
74 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
78 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
79 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
80 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
81 if( tmp->a_vals[i].bv_val == NULL ) break;
83 tmp->a_vals[i].bv_val = NULL;
85 if( a->a_nvals != a->a_vals ) {
86 tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
87 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
88 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
89 if( tmp->a_nvals[i].bv_val == NULL ) break;
91 tmp->a_nvals[i].bv_val = NULL;
94 tmp->a_nvals = tmp->a_vals;
102 tmp->a_desc = a->a_desc;
109 Attribute *attrs_dup( Attribute *a )
111 Attribute *tmp, **next;
113 if( a == NULL ) return NULL;
118 for( ; a != NULL ; a = a->a_next ) {
119 *next = attr_dup( a );
120 next = &((*next)->a_next);
130 * attr_merge - merge the given type and value with the list of
131 * attributes in attrs.
133 * nvals must be NULL if the attribute has no normalizer.
134 * In this case, a->a_nvals will be set equal to a->a_vals.
136 * returns 0 everything went ok
143 AttributeDescription *desc,
151 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
152 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
158 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
161 (*a)->a_nvals = NULL;
166 rc = value_add( &(*a)->a_vals, vals );
168 if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
169 else (*a)->a_nvals = (*a)->a_vals;
175 attr_merge_normalize(
177 AttributeDescription *desc,
181 BerVarray nvals = NULL;
184 if ( desc->ad_type->sat_equality &&
185 desc->ad_type->sat_equality->smr_normalize )
189 for ( i = 0; vals[i].bv_val; i++ );
191 nvals = sl_calloc( sizeof(struct berval), i + 1, memctx );
192 for ( i = 0; vals[i].bv_val; i++ ) {
193 rc = (*desc->ad_type->sat_equality->smr_normalize)(
194 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
195 desc->ad_type->sat_syntax,
196 desc->ad_type->sat_equality,
197 &vals[i], &nvals[i], memctx );
199 if ( rc != LDAP_SUCCESS ) {
200 nvals[i+1].bv_val = NULL;
204 nvals[i].bv_val = NULL;
207 rc = attr_merge( e, desc, vals, nvals );
210 if ( nvals != NULL ) {
211 ber_bvarray_free_x( nvals, memctx );
219 AttributeDescription *desc,
226 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
227 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
233 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
236 (*a)->a_nvals = NULL;
241 rc = value_add_one( &(*a)->a_vals, val );
243 if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
244 else (*a)->a_nvals = (*a)->a_vals;
249 attr_merge_normalize_one(
251 AttributeDescription *desc,
256 struct berval *nvalp;
259 if ( desc->ad_type->sat_equality &&
260 desc->ad_type->sat_equality->smr_normalize )
262 rc = (*desc->ad_type->sat_equality->smr_normalize)(
263 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
264 desc->ad_type->sat_syntax,
265 desc->ad_type->sat_equality,
266 val, &nval, memctx );
268 if ( rc != LDAP_SUCCESS ) {
276 rc = attr_merge_one( e, desc, val, nvalp );
277 if ( nvalp != NULL ) {
278 sl_free( nval.bv_val, memctx );
284 * attrs_find - find attribute(s) by AttributeDescription
285 * returns next attribute which is subtype of provided description.
291 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
313 for ( ; a != NULL; a = a->a_next ) {
314 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
323 * attr_delete - delete the attribute type in list pointed to by attrs
324 * return 0 deleted ok
325 * 1 not found in list a
326 * -1 something bad happened
332 AttributeDescription *desc
337 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
338 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
339 Attribute *save = *a;
347 return LDAP_NO_SUCH_ATTRIBUTE;