1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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.
33 #include <ac/socket.h>
34 #include <ac/string.h>
39 static char *ebuf; /* buf returned by entry2str */
40 static char *ecur; /* pointer to end of currently used ebuf */
41 static int emaxsize;/* max size of ebuf */
46 const Entry slap_entry_root = {
47 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
51 * these mutexes must be used when calling the entry2str()
52 * routine since it returns a pointer to static data.
54 ldap_pvt_thread_mutex_t entry2str_mutex;
56 static const struct berval dn_bv = BER_BVC("dn");
61 * Allocate in chunks, minimum of 1000 at a time.
63 #define CHUNK_SIZE 1000
64 typedef struct slap_list {
65 struct slap_list *next;
67 static slap_list *entry_chunks;
68 static Entry *entry_list;
69 static ldap_pvt_thread_mutex_t entry_mutex;
71 int entry_destroy(void)
74 if ( ebuf ) free( ebuf );
79 for ( e=entry_chunks; e; e=entry_chunks ) {
80 entry_chunks = e->next;
84 ldap_pvt_thread_mutex_destroy( &entry_mutex );
85 ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
86 return attr_destroy();
92 ldap_pvt_thread_mutex_init( &entry2str_mutex );
93 ldap_pvt_thread_mutex_init( &entry_mutex );
100 return str2entry2( s, 1 );
103 #define bvcasematch(bv1, bv2) (ber_bvstrcasecmp(bv1, bv2) == 0)
106 str2entry2( char *s, int checkvals )
110 struct berval *type, *vals, *nvals;
112 AttributeDescription *ad, *ad_prev;
117 Attribute ahead, *atail;
120 * LDIF is used as the string format.
121 * An entry looks like this:
124 * [<attr>:[:] <value>\n]
125 * [<tab><continuedvalue>\n]*
128 * If a double colon is used after a type, it means the
129 * following value is encoded as a base 64 string. This
130 * happens if the value contains a non-printing character
134 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
135 s ? s : "NULL", 0, 0 );
140 Debug( LDAP_DEBUG_ANY,
141 "<= str2entry NULL (entry allocation failed)\n",
146 /* initialize entry */
149 /* dn + attributes */
157 lines = ldif_countlines( s );
158 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
160 nvals = vals+lines+1;
161 freeval = (char *)(nvals+lines+1);
164 /* parse into individual values, record DN */
165 while ( (s = ldif_getline( &next )) != NULL ) {
167 if ( *s == '\n' || *s == '\0' ) {
172 Debug( LDAP_DEBUG_TRACE,
173 "<= str2entry ran past end of entry\n", 0, 0, 0 );
177 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
180 Debug( LDAP_DEBUG_TRACE,
181 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
185 if ( bvcasematch( &type[i], &dn_bv ) ) {
186 if ( e->e_dn != NULL ) {
187 Debug( LDAP_DEBUG_ANY, "str2entry: "
188 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
189 (long) e->e_id, e->e_dn, vals[i].bv_val );
193 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
194 if( rc != LDAP_SUCCESS ) {
195 Debug( LDAP_DEBUG_ANY, "str2entry: "
196 "entry %ld has invalid DN \"%s\"\n",
197 (long) e->e_id, vals[i].bv_val, 0 );
200 if ( freeval[i] ) free( vals[i].bv_val );
201 vals[i].bv_val = NULL;
208 /* check to make sure there was a dn: line */
209 if ( BER_BVISNULL( &e->e_name )) {
210 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
211 (long) e->e_id, 0, 0 );
215 /* Make sure all attributes with multiple values are contiguous */
221 for (i=0; i<lines; i++) {
222 for ( j=i+1; j<lines; j++ ) {
223 if ( bvcasematch( type+i, type+j )) {
224 /* out of order, move intervening attributes down */
228 for ( k=j; k>i; k-- ) {
231 freeval[k] = freeval[k-1];
245 for ( i=0; i<=lines; i++ ) {
247 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
249 rc = slap_bv2ad( type+i, &ad, &text );
251 if( rc != LDAP_SUCCESS ) {
252 Debug( slapMode & SLAP_TOOL_MODE
253 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
254 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
255 if( slapMode & SLAP_TOOL_MODE ) {
259 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
260 if( rc != LDAP_SUCCESS ) {
261 Debug( LDAP_DEBUG_ANY,
262 "<= str2entry: slap_str2undef_ad(%s): %s\n",
263 type[i].bv_val, text, 0 );
268 /* require ';binary' when appropriate (ITS#5071) */
269 if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
270 Debug( LDAP_DEBUG_ANY,
271 "str2entry: attributeType %s #%d: "
272 "needs ';binary' transfer as per syntax %s\n",
273 ad->ad_cname.bv_val, 0,
274 ad->ad_type->sat_syntax->ssyn_oid );
279 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
281 /* FIXME: we only need this when migrating from an unsorted DB */
282 if ( atail != &ahead && atail->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
283 rc = slap_sort_vals( (Modifications *)atail, &text, &j, NULL );
284 if ( rc == LDAP_SUCCESS ) {
285 atail->a_flags |= SLAP_ATTR_SORTED_VALS;
286 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
287 Debug( LDAP_DEBUG_ANY,
288 "str2entry: attributeType %s value #%d provided more than once\n",
289 atail->a_desc->ad_cname.bv_val, j, 0 );
293 atail->a_next = attr_alloc( NULL );
294 atail = atail->a_next;
296 atail->a_numvals = attr_cnt;
297 atail->a_desc = ad_prev;
298 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
299 if( ad_prev->ad_type->sat_equality &&
300 ad_prev->ad_type->sat_equality->smr_normalize )
301 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
303 atail->a_nvals = NULL;
305 for ( j=0; j<attr_cnt; j++ ) {
307 atail->a_vals[j] = vals[k];
309 ber_dupbv( atail->a_vals+j, &vals[k] );
310 vals[k].bv_val = NULL;
311 if ( atail->a_nvals ) {
312 atail->a_nvals[j] = nvals[k];
313 nvals[k].bv_val = NULL;
317 BER_BVZERO( &atail->a_vals[j] );
318 if ( atail->a_nvals ) {
319 BER_BVZERO( &atail->a_nvals[j] );
321 atail->a_nvals = atail->a_vals;
324 if ( i == lines ) break;
327 if ( BER_BVISNULL( &vals[i] ) ) {
328 Debug( LDAP_DEBUG_ANY,
329 "str2entry: attributeType %s #%d: "
331 ad->ad_cname.bv_val, attr_cnt, 0 );
335 if( slapMode & SLAP_TOOL_MODE ) {
337 slap_syntax_validate_func *validate =
338 ad->ad_type->sat_syntax->ssyn_validate;
339 slap_syntax_transform_func *pretty =
340 ad->ad_type->sat_syntax->ssyn_pretty;
343 rc = ordered_value_pretty( ad,
344 &vals[i], &pval, NULL );
346 } else if ( validate ) {
348 * validate value per syntax
350 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
353 Debug( LDAP_DEBUG_ANY,
354 "str2entry: attributeType %s #%d: "
355 "no validator for syntax %s\n",
356 ad->ad_cname.bv_val, attr_cnt,
357 ad->ad_type->sat_syntax->ssyn_oid );
362 Debug( LDAP_DEBUG_ANY,
363 "str2entry: invalid value "
364 "for attributeType %s #%d (syntax %s)\n",
365 ad->ad_cname.bv_val, attr_cnt,
366 ad->ad_type->sat_syntax->ssyn_oid );
371 if ( freeval[i] ) free( vals[i].bv_val );
377 if ( ad->ad_type->sat_equality &&
378 ad->ad_type->sat_equality->smr_normalize )
380 rc = ordered_value_normalize(
381 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
383 ad->ad_type->sat_equality,
384 &vals[i], &nvals[i], NULL );
387 Debug( LDAP_DEBUG_ANY,
388 "<= str2entry NULL (smr_normalize %s %d)\n", ad->ad_cname.bv_val, rc, 0 );
398 atail->a_next = NULL;
399 e->e_attrs = ahead.a_next;
401 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
402 e->e_dn, (unsigned long) e, 0 );
406 for ( i=0; i<lines; i++ ) {
407 if ( freeval[i] ) free( vals[i].bv_val );
408 free( nvals[i].bv_val );
416 #define GRABSIZE BUFSIZ
418 #define MAKE_SPACE( n ) { \
419 while ( ecur + (n) > ebuf + emaxsize ) { \
421 offset = (int) (ecur - ebuf); \
422 ebuf = ch_realloc( ebuf, \
423 emaxsize + GRABSIZE ); \
424 emaxsize += GRABSIZE; \
425 ecur = ebuf + offset; \
429 /* NOTE: only preserved for binary compatibility */
435 return entry2str_wrap( e, len, LDIF_LINE_WIDTH );
452 * In string format, an entry looks like this:
454 * [<attr>: <value>\n]*
460 if ( e->e_dn != NULL ) {
462 tmplen = e->e_name.bv_len;
463 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
464 ldif_sput_wrap( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen, wrap );
467 /* put the attributes */
468 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
469 /* put "<type>:[:] <value>" line for each value */
470 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
472 tmplen = a->a_desc->ad_cname.bv_len;
473 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
474 ldif_sput_wrap( &ecur, LDIF_PUT_VALUE,
475 a->a_desc->ad_cname.bv_val,
476 bv->bv_val, bv->bv_len, wrap );
487 entry_clean( Entry *e )
489 /* free an entry structure */
492 /* e_private must be freed by the caller */
493 assert( e->e_private == NULL );
498 if ( !BER_BVISNULL( &e->e_name ) ) {
499 free( e->e_name.bv_val );
500 BER_BVZERO( &e->e_name );
502 if ( !BER_BVISNULL( &e->e_nname ) ) {
503 free( e->e_nname.bv_val );
504 BER_BVZERO( &e->e_nname );
507 if ( !BER_BVISNULL( &e->e_bv ) ) {
508 free( e->e_bv.bv_val );
509 BER_BVZERO( &e->e_bv );
512 /* free attributes */
514 attrs_free( e->e_attrs );
522 entry_free( Entry *e )
526 ldap_pvt_thread_mutex_lock( &entry_mutex );
527 e->e_private = entry_list;
529 ldap_pvt_thread_mutex_unlock( &entry_mutex );
532 /* These parameters work well on AMD64 */
540 #define STRIDE_FACTOR (STRIDE*STRIPE)
543 entry_prealloc( int num )
545 Entry *e, **prev, *tmp;
551 #if STRIDE_FACTOR > 1
552 /* Round up to our stride factor */
553 num += STRIDE_FACTOR-1;
554 num /= STRIDE_FACTOR;
555 num *= STRIDE_FACTOR;
558 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
559 s->next = entry_chunks;
563 for (i=0; i<STRIPE; i++) {
566 for (j=i; j<num; j+= STRIDE) {
568 prev = (Entry **)&e->e_private;
573 entry_list = (Entry *)(s+1);
583 ldap_pvt_thread_mutex_lock( &entry_mutex );
585 entry_prealloc( CHUNK_SIZE );
587 entry_list = e->e_private;
589 ldap_pvt_thread_mutex_unlock( &entry_mutex );
596 * These routines are used only by Backend.
598 * the Entry has three entry points (ways to find things):
600 * by entry e.g., if you already have an entry from the cache
601 * and want to delete it. (really by entry ptr)
602 * by dn e.g., when looking for the base object of a search
603 * by id e.g., for search candidates
605 * these correspond to three different avl trees that are maintained.
609 entry_cmp( Entry *e1, Entry *e2 )
611 return SLAP_PTRCMP( e1, e2 );
615 entry_dn_cmp( const void *v_e1, const void *v_e2 )
617 /* compare their normalized UPPERCASED dn's */
618 const Entry *e1 = v_e1, *e2 = v_e2;
620 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
624 entry_id_cmp( const void *v_e1, const void *v_e2 )
626 const Entry *e1 = v_e1, *e2 = v_e2;
627 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
630 /* This is like a ber_len */
631 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
632 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
635 entry_putlen(unsigned char **buf, ber_len_t len)
637 ber_len_t lenlen = entry_lenlen(len);
640 **buf = (unsigned char) len;
643 **buf = 0x80 | ((unsigned char) lenlen - 1);
644 for (i=lenlen-1; i>0; i--) {
645 (*buf)[i] = (unsigned char) len;
653 entry_getlen(unsigned char **buf)
670 /* Count up the sizes of the components of an entry */
671 void entry_partsize(Entry *e, ber_len_t *plen,
672 int *pnattrs, int *pnvals, int norm)
674 ber_len_t len, dnlen, ndnlen;
675 int i, nat = 0, nval = 0;
678 dnlen = e->e_name.bv_len;
679 len = dnlen + 1; /* trailing NUL byte */
680 len += entry_lenlen(dnlen);
682 ndnlen = e->e_nname.bv_len;
684 len += entry_lenlen(ndnlen);
686 for (a=e->e_attrs; a; a=a->a_next) {
687 /* For AttributeDesc, we only store the attr name */
689 len += a->a_desc->ad_cname.bv_len+1;
690 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
691 for (i=0; a->a_vals[i].bv_val; i++) {
693 len += a->a_vals[i].bv_len + 1;
694 len += entry_lenlen(a->a_vals[i].bv_len);
696 len += entry_lenlen(i);
697 nval++; /* empty berval at end */
698 if (norm && a->a_nvals != a->a_vals) {
699 for (i=0; a->a_nvals[i].bv_val; i++) {
701 len += a->a_nvals[i].bv_len + 1;
702 len += entry_lenlen(a->a_nvals[i].bv_len);
704 len += entry_lenlen(i); /* i nvals */
707 len += entry_lenlen(0); /* 0 nvals */
710 len += entry_lenlen(nat);
711 len += entry_lenlen(nval);
717 /* Add up the size of the entry for a flattened buffer */
718 ber_len_t entry_flatsize(Entry *e, int norm)
723 entry_partsize(e, &len, &nattrs, &nvals, norm);
724 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
725 (nvals * sizeof(struct berval));
729 /* Flatten an Entry into a buffer. The buffer is filled with just the
730 * strings/bervals of all the entry components. Each field is preceded
731 * by its length, encoded the way ber_put_len works. Every field is NUL
732 * terminated. The entire buffer size is precomputed so that a single
733 * malloc can be performed. The entry size is also recorded,
734 * to aid in entry_decode.
736 int entry_encode(Entry *e, struct berval *bv)
738 ber_len_t len, dnlen, ndnlen, i;
743 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
744 (long) e->e_id, e->e_dn, 0 );
746 dnlen = e->e_name.bv_len;
747 ndnlen = e->e_nname.bv_len;
749 entry_partsize( e, &len, &nattrs, &nvals, 1 );
752 bv->bv_val = ch_malloc(len);
753 ptr = (unsigned char *)bv->bv_val;
754 entry_putlen(&ptr, nattrs);
755 entry_putlen(&ptr, nvals);
756 entry_putlen(&ptr, dnlen);
757 AC_MEMCPY(ptr, e->e_dn, dnlen);
760 entry_putlen(&ptr, ndnlen);
761 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
765 for (a=e->e_attrs; a; a=a->a_next) {
766 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
767 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
768 a->a_desc->ad_cname.bv_len);
769 ptr += a->a_desc->ad_cname.bv_len;
772 for (i=0; a->a_vals[i].bv_val; i++);
773 assert( i == a->a_numvals );
774 entry_putlen(&ptr, i);
775 for (i=0; a->a_vals[i].bv_val; i++) {
776 entry_putlen(&ptr, a->a_vals[i].bv_len);
777 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
778 a->a_vals[i].bv_len);
779 ptr += a->a_vals[i].bv_len;
782 if (a->a_nvals != a->a_vals) {
783 entry_putlen(&ptr, i);
784 for (i=0; a->a_nvals[i].bv_val; i++) {
785 entry_putlen(&ptr, a->a_nvals[i].bv_len);
786 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
787 a->a_nvals[i].bv_len);
788 ptr += a->a_nvals[i].bv_len;
792 entry_putlen(&ptr, 0);
797 Debug( LDAP_DEBUG_TRACE, "<= entry_encode(0x%08lx): %s\n",
798 (long) e->e_id, e->e_dn, 0 );
803 /* Retrieve an Entry that was stored using entry_encode above.
804 * First entry_header must be called to decode the size of the entry.
805 * Then a single block of memory must be malloc'd to accomodate the
806 * bervals and the bulk data. Next the bulk data is retrieved from
807 * the DB and parsed by entry_decode.
809 * Note: everything is stored in a single contiguous block, so
810 * you can not free individual attributes or names from this
811 * structure. Attempting to do so will likely corrupt memory.
813 int entry_header(EntryHeader *eh)
815 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
817 /* Some overlays can create empty entries
818 * so don't check for zeros here.
820 eh->nattrs = entry_getlen(&ptr);
821 eh->nvals = entry_getlen(&ptr);
822 eh->data = (char *)ptr;
827 entry_decode_dn( EntryHeader *eh, struct berval *dn, struct berval *ndn )
830 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
832 assert( dn != NULL || ndn != NULL );
834 ptr = (unsigned char *)eh->data;
835 i = entry_getlen(&ptr);
837 dn->bv_val = (char *) ptr;
843 i = entry_getlen(&ptr);
844 ndn->bv_val = (char *) ptr;
848 Debug( LDAP_DEBUG_TRACE,
849 "entry_decode_dn: \"%s\"\n",
850 dn ? dn->bv_val : ndn->bv_val, 0, 0 );
855 #ifdef SLAP_ZONE_ALLOC
856 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
858 int entry_decode(EntryHeader *eh, Entry **e)
861 int i, j, nattrs, nvals;
866 AttributeDescription *ad;
867 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
873 x->e_attrs = attrs_alloc( nattrs );
874 ptr = (unsigned char *)eh->data;
875 i = entry_getlen(&ptr);
876 x->e_name.bv_val = (char *) ptr;
877 x->e_name.bv_len = i;
879 i = entry_getlen(&ptr);
880 x->e_nname.bv_val = (char *) ptr;
881 x->e_nname.bv_len = i;
883 Debug( LDAP_DEBUG_TRACE,
884 "entry_decode: \"%s\"\n",
889 bptr = (BerVarray)eh->bv.bv_val;
891 while ((i = entry_getlen(&ptr))) {
894 bv.bv_val = (char *) ptr;
896 rc = slap_bv2ad( &bv, &ad, &text );
898 if( rc != LDAP_SUCCESS ) {
899 Debug( LDAP_DEBUG_TRACE,
900 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
901 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
903 if( rc != LDAP_SUCCESS ) {
904 Debug( LDAP_DEBUG_ANY,
905 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
912 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
913 j = entry_getlen(&ptr);
918 i = entry_getlen(&ptr);
920 bptr->bv_val = (char *)ptr;
929 j = entry_getlen(&ptr);
933 i = entry_getlen(&ptr);
935 bptr->bv_val = (char *)ptr;
944 a->a_nvals = a->a_vals;
946 /* FIXME: This is redundant once a sorted entry is saved into the DB */
947 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
948 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
949 if ( rc == LDAP_SUCCESS ) {
950 a->a_flags |= SLAP_ATTR_SORTED_VALS;
951 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
952 /* should never happen */
953 Debug( LDAP_DEBUG_ANY,
954 "entry_decode: attributeType %s value #%d provided more than once\n",
955 a->a_desc->ad_cname.bv_val, j, 0 );
965 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
972 entry_dup2( Entry *dest, Entry *source )
974 assert( dest != NULL );
975 assert( source != NULL );
977 assert( dest->e_private == NULL );
979 dest->e_id = source->e_id;
980 ber_dupbv( &dest->e_name, &source->e_name );
981 ber_dupbv( &dest->e_nname, &source->e_nname );
982 dest->e_attrs = attrs_dup( source->e_attrs );
983 dest->e_ocflags = source->e_ocflags;
989 entry_dup( Entry *e )
991 return entry_dup2( entry_alloc(), e );
995 /* Duplicates an entry using a single malloc. Saves CPU time, increases
996 * heap usage because a single large malloc is harder to satisfy than
997 * lots of small ones, and the freed space isn't as easily reusable.
999 * Probably not worth using this function.
1001 Entry *entry_dup_bv( Entry *e )
1008 Attribute *src, *dst;
1010 ret = entry_alloc();
1012 entry_partsize(e, &len, &nattrs, &nvals, 1);
1013 ret->e_id = e->e_id;
1014 ret->e_attrs = attrs_alloc( nattrs );
1015 ret->e_ocflags = e->e_ocflags;
1016 ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
1017 ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
1019 bvl = (struct berval *)ret->e_bv.bv_val;
1020 ptr = (char *)(bvl + nvals);
1022 ret->e_name.bv_len = e->e_name.bv_len;
1023 ret->e_name.bv_val = ptr;
1024 AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
1025 ptr += e->e_name.bv_len;
1028 ret->e_nname.bv_len = e->e_nname.bv_len;
1029 ret->e_nname.bv_val = ptr;
1030 AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
1031 ptr += e->e_name.bv_len;
1035 for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
1037 dst->a_desc = src->a_desc;
1038 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
1040 dst->a_numvals = src->a_numvals;
1041 for ( i=0; src->a_vals[i].bv_val; i++ ) {
1042 bvl->bv_len = src->a_vals[i].bv_len;
1044 AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
1051 if ( src->a_vals != src->a_nvals ) {
1053 for ( i=0; src->a_nvals[i].bv_val; i++ ) {
1054 bvl->bv_len = src->a_nvals[i].bv_len;
1056 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );