]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Add trickle-sync for quick mode
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/errno.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35
36 #include "slap.h"
37 #include "ldif.h"
38
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                     */
42
43 /*
44  * Empty root entry
45  */
46 const Entry slap_entry_root = {
47         NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
48 };
49
50 /*
51  * these mutexes must be used when calling the entry2str()
52  * routine since it returns a pointer to static data.
53  */
54 ldap_pvt_thread_mutex_t entry2str_mutex;
55
56 static const struct berval dn_bv = BER_BVC("dn");
57
58 /*
59  * Entry free list
60  *
61  * Allocate in chunks, minimum of 1000 at a time.
62  */
63 #define CHUNK_SIZE      1000
64 typedef struct slap_list {
65         struct slap_list *next;
66 } slap_list;
67 static slap_list *entry_chunks;
68 static Entry *entry_list;
69 static ldap_pvt_thread_mutex_t entry_mutex;
70
71 int entry_destroy(void)
72 {
73         slap_list *e;
74         if ( ebuf ) free( ebuf );
75         ebuf = NULL;
76         ecur = NULL;
77         emaxsize = 0;
78
79         for ( e=entry_chunks; e; e=entry_chunks ) {
80                 entry_chunks = e->next;
81                 free( e );
82         }
83
84         ldap_pvt_thread_mutex_destroy( &entry_mutex );
85         ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
86         return attr_destroy();
87 }
88
89 int
90 entry_init(void)
91 {
92         ldap_pvt_thread_mutex_init( &entry2str_mutex );
93         ldap_pvt_thread_mutex_init( &entry_mutex );
94         return attr_init();
95 }
96
97 Entry *
98 str2entry( char *s )
99 {
100         return str2entry2( s, 1 );
101 }
102
103 Entry *
104 str2entry2( char *s, int checkvals )
105 {
106         int rc;
107         Entry           *e;
108         struct berval   *type, *vals, *nvals;
109         char    *freeval;
110         AttributeDescription *ad, *ad_prev;
111         const char *text;
112         char    *next;
113         int             attr_cnt;
114         int             i, lines;
115         Attribute       ahead, *atail;
116
117         /*
118          * LDIF is used as the string format.
119          * An entry looks like this:
120          *
121          *      dn: <dn>\n
122          *      [<attr>:[:] <value>\n]
123          *      [<tab><continuedvalue>\n]*
124          *      ...
125          *
126          * If a double colon is used after a type, it means the
127          * following value is encoded as a base 64 string.  This
128          * happens if the value contains a non-printing character
129          * or newline.
130          */
131
132         Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
133                 s ? s : "NULL", 0, 0 );
134
135         e = entry_alloc();
136
137         if( e == NULL ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "<= str2entry NULL (entry allocation failed)\n",
140                         0, 0, 0 );
141                 return( NULL );
142         }
143
144         /* initialize entry */
145         e->e_id = NOID;
146
147         /* dn + attributes */
148         atail = &ahead;
149         ahead.a_next = NULL;
150         ad = NULL;
151         ad_prev = NULL;
152         attr_cnt = 0;
153         next = s;
154
155         lines = ldif_countlines( s );
156         type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
157         vals = type+lines+1;
158         nvals = vals+lines+1;
159         freeval = (char *)(nvals+lines+1);
160         i = -1;
161
162         /* parse into individual values, record DN */
163         while ( (s = ldif_getline( &next )) != NULL ) {
164                 int freev;
165                 if ( *s == '\n' || *s == '\0' ) {
166                         break;
167                 }
168                 i++;
169                 if (i >= lines) {
170                         Debug( LDAP_DEBUG_TRACE,
171                                 "<= str2entry ran past end of entry\n", 0, 0, 0 );
172                         goto fail;
173                 }
174
175                 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
176                 freeval[i] = freev;
177                 if ( rc ) {
178                         Debug( LDAP_DEBUG_TRACE,
179                                 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
180                         continue;
181                 }
182
183                 if ( type[i].bv_len == dn_bv.bv_len &&
184                         strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
185
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 );
190                                 goto fail;
191                         }
192
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 );
198                                 goto fail;
199                         }
200                         if ( freeval[i] ) free( vals[i].bv_val );
201                         vals[i].bv_val = NULL;
202                         i--;
203                         continue;
204                 }
205         }
206         lines = i+1;
207
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 );
212                 goto fail;
213         }
214
215 #define bvcasematch(bv1, bv2)   ( ((bv1)->bv_len == (bv2)->bv_len) && (strncasecmp((bv1)->bv_val, (bv2)->bv_val, (bv1)->bv_len) == 0) )
216
217         /* Make sure all attributes with multiple values are contiguous */
218         if ( checkvals ) {
219                 int j, k;
220                 struct berval bv;
221                 int fv;
222
223                 for (i=0; i<lines; i++) {
224                         for ( j=i+1; j<lines; j++ ) {
225                                 if ( bvcasematch( type+i, type+j )) {
226                                         /* out of order, move intervening attributes down */
227                                         if ( j != i+1 ) {
228                                                 bv = vals[j];
229                                                 fv = freeval[j];
230                                                 for ( k=j; k>i; k-- ) {
231                                                         type[k] = type[k-1];
232                                                         vals[k] = vals[k-1];
233                                                         freeval[k] = freeval[k-1];
234                                                 }
235                                                 k++;
236                                                 type[k] = type[i];
237                                                 vals[k] = bv;
238                                                 freeval[k] = fv;
239                                         }
240                                         i++;
241                                 }
242                         }
243                 }
244         }
245
246         for ( i=0; i<=lines; i++ ) {
247                 ad_prev = ad;
248                 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
249                         ad = NULL;
250                         rc = slap_bv2ad( type+i, &ad, &text );
251
252                         if( rc != LDAP_SUCCESS ) {
253                                 Debug( slapMode & SLAP_TOOL_MODE
254                                         ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
255                                         "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
256                                 if( slapMode & SLAP_TOOL_MODE ) {
257                                         goto fail;
258                                 }
259
260                                 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
261                                 if( rc != LDAP_SUCCESS ) {
262                                         Debug( LDAP_DEBUG_ANY,
263                                                 "<= str2entry: slap_str2undef_ad(%s): %s\n",
264                                                         type[i].bv_val, text, 0 );
265                                         goto fail;
266                                 }
267                         }
268                 }
269
270                 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
271                         int j, k;
272                         atail->a_next = attr_alloc( NULL );
273                         atail = atail->a_next;
274                         atail->a_flags = 0;
275                         atail->a_desc = ad_prev;
276                         atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
277                         if( ad_prev->ad_type->sat_equality &&
278                                 ad_prev->ad_type->sat_equality->smr_normalize )
279                                 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
280                         else
281                                 atail->a_nvals = NULL;
282                         k = i - attr_cnt;
283                         for ( j=0; j<attr_cnt; j++ ) {
284                                 if ( freeval[k] )
285                                         atail->a_vals[j] = vals[k];
286                                 else
287                                         ber_dupbv( atail->a_vals+j, &vals[k] );
288                                 vals[k].bv_val = NULL;
289                                 if ( atail->a_nvals ) {
290                                         atail->a_nvals[j] = nvals[k];
291                                         nvals[k].bv_val = NULL;
292                                 }
293                                 k++;
294                         }
295                         BER_BVZERO( &atail->a_vals[j] );
296                         if ( atail->a_nvals ) {
297                                 BER_BVZERO( &atail->a_nvals[j] );
298                         } else {
299                                 atail->a_nvals = atail->a_vals;
300                         }
301                         attr_cnt = 0;
302                         if ( i == lines ) break;
303                 }
304
305                 if( slapMode & SLAP_TOOL_MODE ) {
306                         struct berval pval;
307                         slap_syntax_validate_func *validate =
308                                 ad->ad_type->sat_syntax->ssyn_validate;
309                         slap_syntax_transform_func *pretty =
310                                 ad->ad_type->sat_syntax->ssyn_pretty;
311
312                         if ( pretty ) {
313                                 rc = ordered_value_pretty( ad,
314                                         &vals[i], &pval, NULL );
315
316                         } else if ( validate ) {
317                                 /*
318                                  * validate value per syntax
319                                  */
320                                 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
321
322                         } else {
323                                 Debug( LDAP_DEBUG_ANY,
324                                         "str2entry: attributeType %s #%d: "
325                                         "no validator for syntax %s\n", 
326                                         ad->ad_cname.bv_val, attr_cnt,
327                                         ad->ad_type->sat_syntax->ssyn_oid );
328                                 goto fail;
329                         }
330
331                         if( rc != 0 ) {
332                                 Debug( LDAP_DEBUG_ANY,
333                                         "str2entry: invalid value "
334                                         "for attributeType %s #%d (syntax %s)\n",
335                                         ad->ad_cname.bv_val, attr_cnt,
336                                         ad->ad_type->sat_syntax->ssyn_oid );
337                                 goto fail;
338                         }
339
340                         if( pretty ) {
341                                 if ( freeval[i] ) free( vals[i].bv_val );
342                                 vals[i] = pval;
343                                 freeval[i] = 1;
344                         }
345                 }
346
347                 if ( ad->ad_type->sat_equality &&
348                         ad->ad_type->sat_equality->smr_normalize )
349                 {
350                         rc = ordered_value_normalize(
351                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
352                                 ad,
353                                 ad->ad_type->sat_equality,
354                                 &vals[i], &nvals[i], NULL );
355
356                         if ( rc ) {
357                                 Debug( LDAP_DEBUG_ANY,
358                                         "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
359                                 goto fail;
360                         }
361                 }
362
363                 attr_cnt++;
364         }
365
366         free( type );
367         atail->a_next = NULL;
368         e->e_attrs = ahead.a_next;
369
370         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
371                 e->e_dn, (unsigned long) e, 0 );
372         return( e );
373
374 fail:
375         for ( i=0; i<lines; i++ ) {
376                 if ( freeval[i] ) free( vals[i].bv_val );
377                 free( nvals[i].bv_val );
378         }
379         free( type );
380         entry_free( e );
381         return NULL;
382 }
383
384
385 #define GRABSIZE        BUFSIZ
386
387 #define MAKE_SPACE( n ) { \
388                 while ( ecur + (n) > ebuf + emaxsize ) { \
389                         ptrdiff_t       offset; \
390                         offset = (int) (ecur - ebuf); \
391                         ebuf = ch_realloc( ebuf, \
392                                 emaxsize + GRABSIZE ); \
393                         emaxsize += GRABSIZE; \
394                         ecur = ebuf + offset; \
395                 } \
396         }
397
398 char *
399 entry2str(
400         Entry   *e,
401         int             *len )
402 {
403         Attribute       *a;
404         struct berval   *bv;
405         int             i;
406         ber_len_t tmplen;
407
408         assert( e != NULL );
409
410         /*
411          * In string format, an entry looks like this:
412          *      dn: <dn>\n
413          *      [<attr>: <value>\n]*
414          */
415
416         ecur = ebuf;
417
418         /* put the dn */
419         if ( e->e_dn != NULL ) {
420                 /* put "dn: <dn>" */
421                 tmplen = e->e_name.bv_len;
422                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
423                 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
424         }
425
426         /* put the attributes */
427         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
428                 /* put "<type>:[:] <value>" line for each value */
429                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
430                         bv = &a->a_vals[i];
431                         tmplen = a->a_desc->ad_cname.bv_len;
432                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
433                         ldif_sput( &ecur, LDIF_PUT_VALUE,
434                                 a->a_desc->ad_cname.bv_val,
435                                 bv->bv_val, bv->bv_len );
436                 }
437         }
438         MAKE_SPACE( 1 );
439         *ecur = '\0';
440         *len = ecur - ebuf;
441
442         return( ebuf );
443 }
444
445 void
446 entry_clean( Entry *e )
447 {
448         /* free an entry structure */
449         assert( e != NULL );
450
451         /* e_private must be freed by the caller */
452         assert( e->e_private == NULL );
453
454         e->e_id = 0;
455
456         /* free DNs */
457         if ( !BER_BVISNULL( &e->e_name ) ) {
458                 free( e->e_name.bv_val );
459                 BER_BVZERO( &e->e_name );
460         }
461         if ( !BER_BVISNULL( &e->e_nname ) ) {
462                 free( e->e_nname.bv_val );
463                 BER_BVZERO( &e->e_nname );
464         }
465
466         if ( !BER_BVISNULL( &e->e_bv ) ) {
467                 free( e->e_bv.bv_val );
468                 BER_BVZERO( &e->e_bv );
469         }
470
471         /* free attributes */
472         if ( e->e_attrs ) {
473                 attrs_free( e->e_attrs );
474                 e->e_attrs = NULL;
475         }
476
477         e->e_ocflags = 0;
478 }
479
480 void
481 entry_free( Entry *e )
482 {
483         entry_clean( e );
484
485         ldap_pvt_thread_mutex_lock( &entry_mutex );
486         e->e_private = entry_list;
487         entry_list = e;
488         ldap_pvt_thread_mutex_unlock( &entry_mutex );
489 }
490
491 /* These parameters work well on AMD64 */
492 #if 0
493 #define STRIDE 8
494 #define STRIPE 5
495 #else
496 #define STRIDE 1
497 #define STRIPE 1
498 #endif
499 #define STRIDE_FACTOR (STRIDE*STRIPE)
500
501 int
502 entry_prealloc( int num )
503 {
504         Entry *e, **prev, *tmp;
505         slap_list *s;
506         int i, j;
507
508         if (!num) return 0;
509
510 #if STRIDE_FACTOR > 1
511         /* Round up to our stride factor */
512         num += STRIDE_FACTOR-1;
513         num /= STRIDE_FACTOR;
514         num *= STRIDE_FACTOR;
515 #endif
516
517         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
518         s->next = entry_chunks;
519         entry_chunks = s;
520
521         prev = &tmp;
522         for (i=0; i<STRIPE; i++) {
523                 e = (Entry *)(s+1);
524                 e += i;
525                 for (j=i; j<num; j+= STRIDE) {
526                         *prev = e;
527                         prev = (Entry **)&e->e_private;
528                         e += STRIDE;
529                 }
530         }
531         *prev = entry_list;
532         entry_list = (Entry *)(s+1);
533
534         return 0;
535 }
536
537 Entry *
538 entry_alloc( void )
539 {
540         Entry *e;
541
542         ldap_pvt_thread_mutex_lock( &entry_mutex );
543         if ( !entry_list )
544                 entry_prealloc( CHUNK_SIZE );
545         e = entry_list;
546         entry_list = e->e_private;
547         e->e_private = NULL;
548         ldap_pvt_thread_mutex_unlock( &entry_mutex );
549
550         return e;
551 }
552
553
554 /*
555  * These routines are used only by Backend.
556  *
557  * the Entry has three entry points (ways to find things):
558  *
559  *      by entry        e.g., if you already have an entry from the cache
560  *                      and want to delete it. (really by entry ptr)
561  *      by dn           e.g., when looking for the base object of a search
562  *      by id           e.g., for search candidates
563  *
564  * these correspond to three different avl trees that are maintained.
565  */
566
567 int
568 entry_cmp( Entry *e1, Entry *e2 )
569 {
570         return SLAP_PTRCMP( e1, e2 );
571 }
572
573 int
574 entry_dn_cmp( const void *v_e1, const void *v_e2 )
575 {
576         /* compare their normalized UPPERCASED dn's */
577         const Entry *e1 = v_e1, *e2 = v_e2;
578
579         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
580 }
581
582 int
583 entry_id_cmp( const void *v_e1, const void *v_e2 )
584 {
585         const Entry *e1 = v_e1, *e2 = v_e2;
586         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
587 }
588
589 /* This is like a ber_len */
590 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
591         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
592
593 static void
594 entry_putlen(unsigned char **buf, ber_len_t len)
595 {
596         ber_len_t lenlen = entry_lenlen(len);
597
598         if (lenlen == 1) {
599                 **buf = (unsigned char) len;
600         } else {
601                 int i;
602                 **buf = 0x80 | ((unsigned char) lenlen - 1);
603                 for (i=lenlen-1; i>0; i--) {
604                         (*buf)[i] = (unsigned char) len;
605                         len >>= 8;
606                 }
607         }
608         *buf += lenlen;
609 }
610
611 static ber_len_t
612 entry_getlen(unsigned char **buf)
613 {
614         ber_len_t len;
615         int i;
616
617         len = *(*buf)++;
618         if (len <= 0x7f)
619                 return len;
620         i = len & 0x7f;
621         len = 0;
622         for (;i > 0; i--) {
623                 len <<= 8;
624                 len |= *(*buf)++;
625         }
626         return len;
627 }
628
629 /* Count up the sizes of the components of an entry */
630 void entry_partsize(Entry *e, ber_len_t *plen,
631         int *pnattrs, int *pnvals, int norm)
632 {
633         ber_len_t len, dnlen, ndnlen;
634         int i, nat = 0, nval = 0;
635         Attribute *a;
636
637         dnlen = e->e_name.bv_len;
638         len = dnlen + 1;        /* trailing NUL byte */
639         len += entry_lenlen(dnlen);
640         if (norm) {
641                 ndnlen = e->e_nname.bv_len;
642                 len += ndnlen + 1;
643                 len += entry_lenlen(ndnlen);
644         }
645         for (a=e->e_attrs; a; a=a->a_next) {
646                 /* For AttributeDesc, we only store the attr name */
647                 nat++;
648                 len += a->a_desc->ad_cname.bv_len+1;
649                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
650                 for (i=0; a->a_vals[i].bv_val; i++) {
651                         nval++;
652                         len += a->a_vals[i].bv_len + 1;
653                         len += entry_lenlen(a->a_vals[i].bv_len);
654                 }
655                 len += entry_lenlen(i);
656                 nval++; /* empty berval at end */
657                 if (norm && a->a_nvals != a->a_vals) {
658                         for (i=0; a->a_nvals[i].bv_val; i++) {
659                                 nval++;
660                                 len += a->a_nvals[i].bv_len + 1;
661                                 len += entry_lenlen(a->a_nvals[i].bv_len);
662                         }
663                         len += entry_lenlen(i); /* i nvals */
664                         nval++;
665                 } else {
666                         len += entry_lenlen(0); /* 0 nvals */
667                 }
668         }
669         len += entry_lenlen(nat);
670         len += entry_lenlen(nval);
671         *plen = len;
672         *pnattrs = nat;
673         *pnvals = nval;
674 }
675
676 /* Add up the size of the entry for a flattened buffer */
677 ber_len_t entry_flatsize(Entry *e, int norm)
678 {
679         ber_len_t len;
680         int nattrs, nvals;
681
682         entry_partsize(e, &len, &nattrs, &nvals, norm);
683         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
684                 (nvals * sizeof(struct berval));
685         return len;
686 }
687
688 /* Flatten an Entry into a buffer. The buffer is filled with just the
689  * strings/bervals of all the entry components. Each field is preceded
690  * by its length, encoded the way ber_put_len works. Every field is NUL
691  * terminated.  The entire buffer size is precomputed so that a single
692  * malloc can be performed. The entry size is also recorded,
693  * to aid in entry_decode.
694  */
695 int entry_encode(Entry *e, struct berval *bv)
696 {
697         ber_len_t len, dnlen, ndnlen;
698         int i, nattrs, nvals;
699         Attribute *a;
700         unsigned char *ptr;
701
702         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
703                 (long) e->e_id, e->e_dn, 0 );
704         dnlen = e->e_name.bv_len;
705         ndnlen = e->e_nname.bv_len;
706
707         entry_partsize( e, &len, &nattrs, &nvals, 1 );
708
709         bv->bv_len = len;
710         bv->bv_val = ch_malloc(len);
711         ptr = (unsigned char *)bv->bv_val;
712         entry_putlen(&ptr, nattrs);
713         entry_putlen(&ptr, nvals);
714         entry_putlen(&ptr, dnlen);
715         AC_MEMCPY(ptr, e->e_dn, dnlen);
716         ptr += dnlen;
717         *ptr++ = '\0';
718         entry_putlen(&ptr, ndnlen);
719         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
720         ptr += ndnlen;
721         *ptr++ = '\0';
722
723         for (a=e->e_attrs; a; a=a->a_next) {
724                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
725                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
726                         a->a_desc->ad_cname.bv_len);
727                 ptr += a->a_desc->ad_cname.bv_len;
728                 *ptr++ = '\0';
729                 if (a->a_vals) {
730                         for (i=0; a->a_vals[i].bv_val; i++);
731                         entry_putlen(&ptr, i);
732                         for (i=0; a->a_vals[i].bv_val; i++) {
733                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
734                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
735                                         a->a_vals[i].bv_len);
736                                 ptr += a->a_vals[i].bv_len;
737                                 *ptr++ = '\0';
738                         }
739                         if (a->a_nvals != a->a_vals) {
740                                 entry_putlen(&ptr, i);
741                                 for (i=0; a->a_nvals[i].bv_val; i++) {
742                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
743                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
744                                         a->a_nvals[i].bv_len);
745                                         ptr += a->a_nvals[i].bv_len;
746                                         *ptr++ = '\0';
747                                 }
748                         } else {
749                                 entry_putlen(&ptr, 0);
750                         }
751                 }
752         }
753         return 0;
754 }
755
756 /* Retrieve an Entry that was stored using entry_encode above.
757  * First entry_header must be called to decode the size of the entry.
758  * Then a single block of memory must be malloc'd to accomodate the
759  * bervals and the bulk data. Next the bulk data is retrieved from
760  * the DB and parsed by entry_decode.
761  *
762  * Note: everything is stored in a single contiguous block, so
763  * you can not free individual attributes or names from this
764  * structure. Attempting to do so will likely corrupt memory.
765  */
766 int entry_header(EntryHeader *eh)
767 {
768         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
769
770         eh->nattrs = entry_getlen(&ptr);
771         if ( !eh->nattrs ) {
772                 Debug( LDAP_DEBUG_ANY,
773                         "entry_header: attribute count was zero\n", 0, 0, 0);
774                 return LDAP_OTHER;
775         }
776         eh->nvals = entry_getlen(&ptr);
777         if ( !eh->nvals ) {
778                 Debug( LDAP_DEBUG_ANY,
779                         "entry_header: value count was zero\n", 0, 0, 0);
780                 return LDAP_OTHER;
781         }
782         eh->data = (char *)ptr;
783         return LDAP_SUCCESS;
784 }
785
786 #ifdef SLAP_ZONE_ALLOC
787 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
788 #else
789 int entry_decode(EntryHeader *eh, Entry **e)
790 #endif
791 {
792         int i, j, count, nattrs, nvals;
793         int rc;
794         Attribute *a;
795         Entry *x;
796         const char *text;
797         AttributeDescription *ad;
798         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
799         BerVarray bptr;
800
801         nattrs = eh->nattrs;
802         nvals = eh->nvals;
803         x = entry_alloc();
804         x->e_attrs = attrs_alloc( nattrs );
805         ptr = (unsigned char *)eh->data;
806         i = entry_getlen(&ptr);
807         x->e_name.bv_val = (char *) ptr;
808         x->e_name.bv_len = i;
809         ptr += i+1;
810         i = entry_getlen(&ptr);
811         x->e_nname.bv_val = (char *) ptr;
812         x->e_nname.bv_len = i;
813         ptr += i+1;
814         Debug( LDAP_DEBUG_TRACE,
815                 "entry_decode: \"%s\"\n",
816                 x->e_dn, 0, 0 );
817         x->e_bv = eh->bv;
818
819         a = x->e_attrs;
820         bptr = (BerVarray)eh->bv.bv_val;
821
822         while ((i = entry_getlen(&ptr))) {
823                 struct berval bv;
824                 bv.bv_len = i;
825                 bv.bv_val = (char *) ptr;
826                 ad = NULL;
827                 rc = slap_bv2ad( &bv, &ad, &text );
828
829                 if( rc != LDAP_SUCCESS ) {
830                         Debug( LDAP_DEBUG_TRACE,
831                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
832                         rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
833
834                         if( rc != LDAP_SUCCESS ) {
835                                 Debug( LDAP_DEBUG_ANY,
836                                         "<= entry_decode: slap_str2undef_ad(%s): %s\n",
837                                                 ptr, text, 0 );
838                                 return rc;
839                         }
840                 }
841                 ptr += i + 1;
842                 a->a_desc = ad;
843                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
844                 count = j = entry_getlen(&ptr);
845                 a->a_vals = bptr;
846
847                 while (j) {
848                         i = entry_getlen(&ptr);
849                         bptr->bv_len = i;
850                         bptr->bv_val = (char *)ptr;
851                         ptr += i+1;
852                         bptr++;
853                         j--;
854                 }
855                 bptr->bv_val = NULL;
856                 bptr->bv_len = 0;
857                 bptr++;
858
859                 j = entry_getlen(&ptr);
860                 if (j) {
861                         a->a_nvals = bptr;
862                         while (j) {
863                                 i = entry_getlen(&ptr);
864                                 bptr->bv_len = i;
865                                 bptr->bv_val = (char *)ptr;
866                                 ptr += i+1;
867                                 bptr++;
868                                 j--;
869                         }
870                         bptr->bv_val = NULL;
871                         bptr->bv_len = 0;
872                         bptr++;
873                 } else {
874                         a->a_nvals = a->a_vals;
875                 }
876                 a = a->a_next;
877                 nattrs--;
878                 if ( !nattrs )
879                         break;
880         }
881
882         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
883                 x->e_dn, 0, 0 );
884         *e = x;
885         return 0;
886 }
887
888 Entry *entry_dup( Entry *e )
889 {
890         Entry *ret;
891
892         ret = entry_alloc();
893
894         ret->e_id = e->e_id;
895         ber_dupbv( &ret->e_name, &e->e_name );
896         ber_dupbv( &ret->e_nname, &e->e_nname );
897         ret->e_attrs = attrs_dup( e->e_attrs );
898         ret->e_ocflags = e->e_ocflags;
899
900         return ret;
901 }
902
903 #if 1
904 /* Duplicates an entry using a single malloc. Saves CPU time, increases
905  * heap usage because a single large malloc is harder to satisfy than
906  * lots of small ones, and the freed space isn't as easily reusable.
907  *
908  * Probably not worth using this function.
909  */
910 Entry *entry_dup_bv( Entry *e )
911 {
912         ber_len_t len;
913         int nattrs, nvals;
914         Entry *ret;
915         struct berval *bvl;
916         char *ptr;
917         Attribute *src, *dst;
918
919         ret = entry_alloc();
920
921         entry_partsize(e, &len, &nattrs, &nvals, 1);
922         ret->e_id = e->e_id;
923         ret->e_attrs = attrs_alloc( nattrs );
924         ret->e_ocflags = e->e_ocflags;
925         ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
926         ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
927
928         bvl = (struct berval *)ret->e_bv.bv_val;
929         ptr = (char *)(bvl + nvals);
930
931         ret->e_name.bv_len = e->e_name.bv_len;
932         ret->e_name.bv_val = ptr;
933         AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
934         ptr += e->e_name.bv_len;
935         *ptr++ = '\0';
936
937         ret->e_nname.bv_len = e->e_nname.bv_len;
938         ret->e_nname.bv_val = ptr;
939         AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
940         ptr += e->e_name.bv_len;
941         *ptr++ = '\0';
942
943         dst = ret->e_attrs;
944         for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
945                 int i;
946                 dst->a_desc = src->a_desc;
947                 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
948                 dst->a_vals = bvl;
949                 for ( i=0; src->a_vals[i].bv_val; i++ ) {
950                         bvl->bv_len = src->a_vals[i].bv_len;
951                         bvl->bv_val = ptr;
952                         AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
953                         ptr += bvl->bv_len;
954                         *ptr++ = '\0';
955                         bvl++;
956                 }
957                 BER_BVZERO(bvl);
958                 bvl++;
959                 if ( src->a_vals != src->a_nvals ) {
960                         dst->a_nvals = bvl;
961                         for ( i=0; src->a_nvals[i].bv_val; i++ ) {
962                                 bvl->bv_len = src->a_nvals[i].bv_len;
963                                 bvl->bv_val = ptr;
964                                 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
965                                 ptr += bvl->bv_len;
966                                 *ptr++ = '\0';
967                                 bvl++;
968                         }
969                         BER_BVZERO(bvl);
970                         bvl++;
971                 }
972         }
973         return ret;
974 }
975 #endif