1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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 * Allocate in chunks, minimum of 1000 at a time.
46 #define CHUNK_SIZE 1000
47 typedef struct slap_list {
48 struct slap_list *next;
50 static slap_list *attr_chunks;
51 static Attribute *attr_list;
52 static ldap_pvt_thread_mutex_t attr_mutex;
55 attr_prealloc( int num )
62 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Attribute));
63 s->next = attr_chunks;
66 a = (Attribute *)(s+1);
67 for ( ;num>1; num--) {
71 a->a_next = attr_list;
72 attr_list = (Attribute *)(s+1);
78 attr_alloc( AttributeDescription *ad )
82 ldap_pvt_thread_mutex_lock( &attr_mutex );
84 attr_prealloc( CHUNK_SIZE );
86 attr_list = a->a_next;
88 ldap_pvt_thread_mutex_unlock( &attr_mutex );
95 /* Return a list of num attrs */
97 attrs_alloc( int num )
99 Attribute *head = NULL;
102 ldap_pvt_thread_mutex_lock( &attr_mutex );
103 for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
110 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
112 for ( ; *a && num > 0; a = &(*a)->a_next ) {
120 ldap_pvt_thread_mutex_unlock( &attr_mutex );
127 attr_free( Attribute *a )
129 if ( a->a_nvals && a->a_nvals != a->a_vals &&
130 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
131 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
134 ber_bvarray_free( a->a_nvals );
137 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
138 * this is used as a placeholder for attributes that do not carry
139 * values, e.g. when proxying search entries with the "attrsonly"
141 if ( a->a_vals != &slap_dummy_bv &&
142 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
143 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
146 ber_bvarray_free( a->a_vals );
149 memset( a, 0, sizeof( Attribute ));
150 ldap_pvt_thread_mutex_lock( &attr_mutex );
151 a->a_next = attr_list;
153 ldap_pvt_thread_mutex_unlock( &attr_mutex );
156 #ifdef LDAP_COMP_MATCH
158 comp_tree_free( Attribute *a )
162 for( ; a != NULL ; a = next ) {
164 if ( component_destructor && a->a_comp_data ) {
165 if ( a->a_comp_data->cd_mem_op )
166 component_destructor( a->a_comp_data->cd_mem_op );
167 free ( a->a_comp_data );
174 attrs_free( Attribute *a )
178 for( ; a != NULL ; a = next ) {
186 attr_dup2( Attribute *tmp, Attribute *a )
188 if ( a->a_vals != NULL ) {
191 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
195 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
196 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
197 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
198 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
201 BER_BVZERO( &tmp->a_vals[i] );
203 /* a_nvals must be non null; it may be equal to a_vals */
204 assert( a->a_nvals != NULL );
206 if ( a->a_nvals != a->a_vals ) {
209 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
210 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
212 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
213 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
217 BER_BVZERO( &tmp->a_nvals[j] );
220 tmp->a_nvals = tmp->a_vals;
226 attr_dup( Attribute *a )
230 if ( a == NULL) return NULL;
232 tmp = attr_alloc( a->a_desc );
238 attrs_dup( Attribute *a )
241 Attribute *tmp, *anew;
243 if( a == NULL ) return NULL;
246 for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
250 anew = attrs_alloc( i );
252 for( tmp=anew; a; a=a->a_next ) {
253 tmp->a_desc = a->a_desc;
263 * attr_merge - merge the given type and value with the list of
264 * attributes in attrs.
266 * nvals must be NULL if the attribute has no normalizer.
267 * In this case, a->a_nvals will be set equal to a->a_vals.
269 * returns 0 everything went ok
276 AttributeDescription *desc,
284 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
285 if ( (*a)->a_desc == desc ) {
291 *a = attr_alloc( desc );
294 * FIXME: if the attribute already exists, the presence
295 * of nvals and the value of (*a)->a_nvals must be consistent
297 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
298 || ( nvals != NULL && (
299 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
300 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
303 rc = value_add( &(*a)->a_vals, vals );
305 if ( rc == LDAP_SUCCESS ) {
307 rc = value_add( &(*a)->a_nvals, nvals );
308 /* FIXME: what if rc != LDAP_SUCCESS ? */
310 (*a)->a_nvals = (*a)->a_vals;
318 * if a normalization function is defined for the equality matchingRule
319 * of desc, the value is normalized and stored in nval; otherwise nval
324 AttributeDescription *desc,
329 int rc = LDAP_SUCCESS;
330 BerVarray nvals = NULL;
334 if ( desc->ad_type->sat_equality &&
335 desc->ad_type->sat_equality->smr_normalize )
339 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
341 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
342 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
343 rc = desc->ad_type->sat_equality->smr_normalize(
344 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
345 desc->ad_type->sat_syntax,
346 desc->ad_type->sat_equality,
347 &vals[i], &nvals[i], memctx );
349 if ( rc != LDAP_SUCCESS ) {
350 BER_BVZERO( &nvals[i + 1] );
354 BER_BVZERO( &nvals[i] );
359 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
360 ber_bvarray_free_x( nvals, memctx );
367 attr_merge_normalize(
369 AttributeDescription *desc,
373 BerVarray nvals = NULL;
376 rc = attr_normalize( desc, vals, &nvals, memctx );
377 if ( rc == LDAP_SUCCESS ) {
378 rc = attr_merge( e, desc, vals, nvals );
379 if ( nvals != NULL ) {
380 ber_bvarray_free_x( nvals, memctx );
390 AttributeDescription *desc,
392 struct berval *nval )
397 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
398 if ( (*a)->a_desc == desc ) {
404 *a = attr_alloc( desc );
407 rc = value_add_one( &(*a)->a_vals, val );
409 if ( rc == LDAP_SUCCESS ) {
411 rc = value_add_one( &(*a)->a_nvals, nval );
412 /* FIXME: what if rc != LDAP_SUCCESS ? */
414 (*a)->a_nvals = (*a)->a_vals;
421 * if a normalization function is defined for the equality matchingRule
422 * of desc, the value is normalized and stored in nval; otherwise nval
427 AttributeDescription *desc,
432 int rc = LDAP_SUCCESS;
436 if ( desc->ad_type->sat_equality &&
437 desc->ad_type->sat_equality->smr_normalize )
439 rc = desc->ad_type->sat_equality->smr_normalize(
440 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
441 desc->ad_type->sat_syntax,
442 desc->ad_type->sat_equality,
445 if ( rc != LDAP_SUCCESS ) {
454 attr_merge_normalize_one(
456 AttributeDescription *desc,
460 struct berval nval = BER_BVNULL;
461 struct berval *nvalp = NULL;
464 rc = attr_normalize_one( desc, val, &nval, memctx );
465 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
469 rc = attr_merge_one( e, desc, val, nvalp );
470 if ( nvalp != NULL ) {
471 slap_sl_free( nval.bv_val, memctx );
477 * attrs_find - find attribute(s) by AttributeDescription
478 * returns next attribute which is subtype of provided description.
484 AttributeDescription *desc )
486 for ( ; a != NULL; a = a->a_next ) {
487 if ( is_ad_subtype( a->a_desc, desc ) ) {
496 * attr_find - find attribute by type
502 AttributeDescription *desc )
504 for ( ; a != NULL; a = a->a_next ) {
505 if ( a->a_desc == desc ) {
514 * attr_delete - delete the attribute type in list pointed to by attrs
515 * return 0 deleted ok
516 * 1 not found in list a
517 * -1 something bad happened
523 AttributeDescription *desc )
527 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
528 if ( (*a)->a_desc == desc ) {
529 Attribute *save = *a;
537 return LDAP_NO_SUCH_ATTRIBUTE;
543 ldap_pvt_thread_mutex_init( &attr_mutex );
552 for ( a=attr_chunks; a; a=attr_chunks ) {
553 attr_chunks = a->next;
556 ldap_pvt_thread_mutex_destroy( &attr_mutex );