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 ) {
185 attr_dup( Attribute *a )
189 if ( a == NULL) return NULL;
191 tmp = attr_alloc( a->a_desc );
193 if ( a->a_vals != NULL ) {
196 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
200 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
201 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
202 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
203 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
206 BER_BVZERO( &tmp->a_vals[i] );
208 /* a_nvals must be non null; it may be equal to a_vals */
209 assert( a->a_nvals != NULL );
211 if ( a->a_nvals != a->a_vals ) {
214 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
215 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
217 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
218 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
222 BER_BVZERO( &tmp->a_nvals[j] );
225 tmp->a_nvals = tmp->a_vals;
236 attrs_dup( Attribute *a )
238 Attribute *tmp, **next;
240 if( a == NULL ) return NULL;
245 for( ; a != NULL ; a = a->a_next ) {
246 *next = attr_dup( a );
247 next = &((*next)->a_next);
256 * attr_merge - merge the given type and value with the list of
257 * attributes in attrs.
259 * nvals must be NULL if the attribute has no normalizer.
260 * In this case, a->a_nvals will be set equal to a->a_vals.
262 * returns 0 everything went ok
269 AttributeDescription *desc,
277 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
278 if ( (*a)->a_desc == desc ) {
284 *a = attr_alloc( desc );
287 * FIXME: if the attribute already exists, the presence
288 * of nvals and the value of (*a)->a_nvals must be consistent
290 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
291 || ( nvals != NULL && (
292 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
293 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
296 rc = value_add( &(*a)->a_vals, vals );
298 if ( rc == LDAP_SUCCESS ) {
300 rc = value_add( &(*a)->a_nvals, nvals );
301 /* FIXME: what if rc != LDAP_SUCCESS ? */
303 (*a)->a_nvals = (*a)->a_vals;
311 * if a normalization function is defined for the equality matchingRule
312 * of desc, the value is normalized and stored in nval; otherwise nval
317 AttributeDescription *desc,
322 int rc = LDAP_SUCCESS;
323 BerVarray nvals = NULL;
327 if ( desc->ad_type->sat_equality &&
328 desc->ad_type->sat_equality->smr_normalize )
332 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
334 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
335 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
336 rc = desc->ad_type->sat_equality->smr_normalize(
337 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
338 desc->ad_type->sat_syntax,
339 desc->ad_type->sat_equality,
340 &vals[i], &nvals[i], memctx );
342 if ( rc != LDAP_SUCCESS ) {
343 BER_BVZERO( &nvals[i + 1] );
347 BER_BVZERO( &nvals[i] );
352 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
353 ber_bvarray_free_x( nvals, memctx );
360 attr_merge_normalize(
362 AttributeDescription *desc,
366 BerVarray nvals = NULL;
369 rc = attr_normalize( desc, vals, &nvals, memctx );
370 if ( rc == LDAP_SUCCESS ) {
371 rc = attr_merge( e, desc, vals, nvals );
372 if ( nvals != NULL ) {
373 ber_bvarray_free_x( nvals, memctx );
383 AttributeDescription *desc,
385 struct berval *nval )
390 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
391 if ( (*a)->a_desc == desc ) {
397 *a = attr_alloc( desc );
400 rc = value_add_one( &(*a)->a_vals, val );
402 if ( rc == LDAP_SUCCESS ) {
404 rc = value_add_one( &(*a)->a_nvals, nval );
405 /* FIXME: what if rc != LDAP_SUCCESS ? */
407 (*a)->a_nvals = (*a)->a_vals;
414 * if a normalization function is defined for the equality matchingRule
415 * of desc, the value is normalized and stored in nval; otherwise nval
420 AttributeDescription *desc,
425 int rc = LDAP_SUCCESS;
429 if ( desc->ad_type->sat_equality &&
430 desc->ad_type->sat_equality->smr_normalize )
432 rc = desc->ad_type->sat_equality->smr_normalize(
433 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
434 desc->ad_type->sat_syntax,
435 desc->ad_type->sat_equality,
438 if ( rc != LDAP_SUCCESS ) {
447 attr_merge_normalize_one(
449 AttributeDescription *desc,
453 struct berval nval = BER_BVNULL;
454 struct berval *nvalp = NULL;
457 rc = attr_normalize_one( desc, val, &nval, memctx );
458 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
462 rc = attr_merge_one( e, desc, val, nvalp );
463 if ( nvalp != NULL ) {
464 slap_sl_free( nval.bv_val, memctx );
470 * attrs_find - find attribute(s) by AttributeDescription
471 * returns next attribute which is subtype of provided description.
477 AttributeDescription *desc )
479 for ( ; a != NULL; a = a->a_next ) {
480 if ( is_ad_subtype( a->a_desc, desc ) ) {
489 * attr_find - find attribute by type
495 AttributeDescription *desc )
497 for ( ; a != NULL; a = a->a_next ) {
498 if ( a->a_desc == desc ) {
507 * attr_delete - delete the attribute type in list pointed to by attrs
508 * return 0 deleted ok
509 * 1 not found in list a
510 * -1 something bad happened
516 AttributeDescription *desc )
520 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
521 if ( (*a)->a_desc == desc ) {
522 Attribute *save = *a;
530 return LDAP_NO_SUCH_ATTRIBUTE;
536 ldap_pvt_thread_mutex_init( &attr_mutex );
545 for ( a=attr_chunks; a; a=attr_chunks ) {
546 attr_chunks = a->next;
549 ldap_pvt_thread_mutex_destroy( &attr_mutex );