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 )
83 ldap_pvt_thread_mutex_lock( &attr_mutex );
85 attr_prealloc( CHUNK_SIZE );
87 attr_list = a->a_next;
89 ldap_pvt_thread_mutex_unlock( &attr_mutex );
96 /* Return a list of num attrs */
98 attrs_alloc( int num )
100 Attribute *head = NULL;
103 ldap_pvt_thread_mutex_lock( &attr_mutex );
104 for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
111 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
113 for ( ; *a && num > 0; a = &(*a)->a_next ) {
121 ldap_pvt_thread_mutex_unlock( &attr_mutex );
128 attr_free( Attribute *a )
130 if ( a->a_nvals && a->a_nvals != a->a_vals &&
131 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
132 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
135 ber_bvarray_free( a->a_nvals );
138 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
139 * this is used as a placeholder for attributes that do not carry
140 * values, e.g. when proxying search entries with the "attrsonly"
142 if ( a->a_vals != &slap_dummy_bv &&
143 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
144 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
147 ber_bvarray_free( a->a_vals );
150 memset( a, 0, sizeof( Attribute ));
151 ldap_pvt_thread_mutex_lock( &attr_mutex );
152 a->a_next = attr_list;
154 ldap_pvt_thread_mutex_unlock( &attr_mutex );
157 #ifdef LDAP_COMP_MATCH
159 comp_tree_free( Attribute *a )
163 for( ; a != NULL ; a = next ) {
165 if ( component_destructor && a->a_comp_data ) {
166 if ( a->a_comp_data->cd_mem_op )
167 component_destructor( a->a_comp_data->cd_mem_op );
168 free ( a->a_comp_data );
175 attrs_free( Attribute *a )
179 for( ; a != NULL ; a = next ) {
186 attr_dup( Attribute *a )
190 if ( a == NULL) return NULL;
192 tmp = attr_alloc( a->a_desc );
194 if ( a->a_vals != NULL ) {
197 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
201 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
202 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
203 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
204 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
207 BER_BVZERO( &tmp->a_vals[i] );
209 /* a_nvals must be non null; it may be equal to a_vals */
210 assert( a->a_nvals != NULL );
212 if ( a->a_nvals != a->a_vals ) {
215 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
216 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
218 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
219 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
223 BER_BVZERO( &tmp->a_nvals[j] );
226 tmp->a_nvals = tmp->a_vals;
237 attrs_dup( Attribute *a )
239 Attribute *tmp, **next;
241 if( a == NULL ) return NULL;
246 for( ; a != NULL ; a = a->a_next ) {
247 *next = attr_dup( a );
248 next = &((*next)->a_next);
257 * attr_merge - merge the given type and value with the list of
258 * attributes in attrs.
260 * nvals must be NULL if the attribute has no normalizer.
261 * In this case, a->a_nvals will be set equal to a->a_vals.
263 * returns 0 everything went ok
270 AttributeDescription *desc,
278 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
279 if ( (*a)->a_desc == desc ) {
285 *a = attr_alloc( desc );
288 * FIXME: if the attribute already exists, the presence
289 * of nvals and the value of (*a)->a_nvals must be consistent
291 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
292 || ( nvals != NULL && (
293 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
294 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
297 rc = value_add( &(*a)->a_vals, vals );
299 if ( rc == LDAP_SUCCESS ) {
301 rc = value_add( &(*a)->a_nvals, nvals );
302 /* FIXME: what if rc != LDAP_SUCCESS ? */
304 (*a)->a_nvals = (*a)->a_vals;
312 attr_merge_normalize(
314 AttributeDescription *desc,
318 BerVarray nvals = NULL;
321 if ( desc->ad_type->sat_equality &&
322 desc->ad_type->sat_equality->smr_normalize )
326 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
328 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
329 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
330 rc = (*desc->ad_type->sat_equality->smr_normalize)(
331 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
332 desc->ad_type->sat_syntax,
333 desc->ad_type->sat_equality,
334 &vals[i], &nvals[i], memctx );
336 if ( rc != LDAP_SUCCESS ) {
337 BER_BVZERO( &nvals[i + 1] );
341 BER_BVZERO( &nvals[i] );
344 rc = attr_merge( e, desc, vals, nvals );
347 if ( nvals != NULL ) {
348 ber_bvarray_free_x( nvals, memctx );
356 AttributeDescription *desc,
358 struct berval *nval )
363 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
364 if ( (*a)->a_desc == desc ) {
370 *a = attr_alloc( desc );
373 rc = value_add_one( &(*a)->a_vals, val );
375 if ( rc == LDAP_SUCCESS ) {
377 rc = value_add_one( &(*a)->a_nvals, nval );
378 /* FIXME: what if rc != LDAP_SUCCESS ? */
380 (*a)->a_nvals = (*a)->a_vals;
387 attr_merge_normalize_one(
389 AttributeDescription *desc,
394 struct berval *nvalp = NULL;
397 if ( desc->ad_type->sat_equality &&
398 desc->ad_type->sat_equality->smr_normalize )
400 rc = (*desc->ad_type->sat_equality->smr_normalize)(
401 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
402 desc->ad_type->sat_syntax,
403 desc->ad_type->sat_equality,
404 val, &nval, memctx );
406 if ( rc != LDAP_SUCCESS ) {
412 rc = attr_merge_one( e, desc, val, nvalp );
413 if ( nvalp != NULL ) {
414 slap_sl_free( nval.bv_val, memctx );
420 * attrs_find - find attribute(s) by AttributeDescription
421 * returns next attribute which is subtype of provided description.
427 AttributeDescription *desc )
429 for ( ; a != NULL; a = a->a_next ) {
430 if ( is_ad_subtype( a->a_desc, desc ) ) {
439 * attr_find - find attribute by type
445 AttributeDescription *desc )
447 for ( ; a != NULL; a = a->a_next ) {
448 if ( a->a_desc == desc ) {
457 * attr_delete - delete the attribute type in list pointed to by attrs
458 * return 0 deleted ok
459 * 1 not found in list a
460 * -1 something bad happened
466 AttributeDescription *desc )
470 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
471 if ( (*a)->a_desc == desc ) {
472 Attribute *save = *a;
480 return LDAP_NO_SUCH_ATTRIBUTE;
486 ldap_pvt_thread_mutex_init( &attr_mutex );
495 for ( a=attr_chunks; a; a=attr_chunks ) {
496 attr_chunks = a->next;
499 ldap_pvt_thread_mutex_destroy( &attr_mutex );