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