]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
cleanup
[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 #if STRIDE_FACTOR > 1
504         /* Round up to our stride factor */
505         num += STRIDE_FACTOR-1;
506         num /= STRIDE_FACTOR;
507         num *= STRIDE_FACTOR;
508 #endif
509
510         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
511         s->next = entry_chunks;
512         entry_chunks = s;
513
514         prev = &tmp;
515         for (i=0; i<STRIPE; i++) {
516                 e = (Entry *)(s+1);
517                 e += i;
518                 for (j=i; j<num; j+= STRIDE) {
519                         *prev = e;
520                         prev = (Entry **)&e->e_private;
521                         e += STRIDE;
522                 }
523         }
524         *prev = entry_list;
525         entry_list = (Entry *)(s+1);
526
527         return 0;
528 }
529
530 Entry *
531 entry_alloc( void )
532 {
533         Entry *e;
534
535         ldap_pvt_thread_mutex_lock( &entry_mutex );
536         if ( !entry_list )
537                 entry_prealloc( CHUNK_SIZE );
538         e = entry_list;
539         entry_list = e->e_private;
540         e->e_private = NULL;
541         ldap_pvt_thread_mutex_unlock( &entry_mutex );
542
543         return e;
544 }
545
546
547 /*
548  * These routines are used only by Backend.
549  *
550  * the Entry has three entry points (ways to find things):
551  *
552  *      by entry        e.g., if you already have an entry from the cache
553  *                      and want to delete it. (really by entry ptr)
554  *      by dn           e.g., when looking for the base object of a search
555  *      by id           e.g., for search candidates
556  *
557  * these correspond to three different avl trees that are maintained.
558  */
559
560 int
561 entry_cmp( Entry *e1, Entry *e2 )
562 {
563         return SLAP_PTRCMP( e1, e2 );
564 }
565
566 int
567 entry_dn_cmp( const void *v_e1, const void *v_e2 )
568 {
569         /* compare their normalized UPPERCASED dn's */
570         const Entry *e1 = v_e1, *e2 = v_e2;
571
572         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
573 }
574
575 int
576 entry_id_cmp( const void *v_e1, const void *v_e2 )
577 {
578         const Entry *e1 = v_e1, *e2 = v_e2;
579         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
580 }
581
582 /* This is like a ber_len */
583 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
584         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
585
586 static void
587 entry_putlen(unsigned char **buf, ber_len_t len)
588 {
589         ber_len_t lenlen = entry_lenlen(len);
590
591         if (lenlen == 1) {
592                 **buf = (unsigned char) len;
593         } else {
594                 int i;
595                 **buf = 0x80 | ((unsigned char) lenlen - 1);
596                 for (i=lenlen-1; i>0; i--) {
597                         (*buf)[i] = (unsigned char) len;
598                         len >>= 8;
599                 }
600         }
601         *buf += lenlen;
602 }
603
604 static ber_len_t
605 entry_getlen(unsigned char **buf)
606 {
607         ber_len_t len;
608         int i;
609
610         len = *(*buf)++;
611         if (len <= 0x7f)
612                 return len;
613         i = len & 0x7f;
614         len = 0;
615         for (;i > 0; i--) {
616                 len <<= 8;
617                 len |= *(*buf)++;
618         }
619         return len;
620 }
621
622 /* Count up the sizes of the components of an entry */
623 void entry_partsize(Entry *e, ber_len_t *plen,
624         int *pnattrs, int *pnvals, int norm)
625 {
626         ber_len_t len, dnlen, ndnlen;
627         int i, nat = 0, nval = 0;
628         Attribute *a;
629
630         dnlen = e->e_name.bv_len;
631         len = dnlen + 1;        /* trailing NUL byte */
632         len += entry_lenlen(dnlen);
633         if (norm) {
634                 ndnlen = e->e_nname.bv_len;
635                 len += ndnlen + 1;
636                 len += entry_lenlen(ndnlen);
637         }
638         for (a=e->e_attrs; a; a=a->a_next) {
639                 /* For AttributeDesc, we only store the attr name */
640                 nat++;
641                 len += a->a_desc->ad_cname.bv_len+1;
642                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
643                 for (i=0; a->a_vals[i].bv_val; i++) {
644                         nval++;
645                         len += a->a_vals[i].bv_len + 1;
646                         len += entry_lenlen(a->a_vals[i].bv_len);
647                 }
648                 len += entry_lenlen(i);
649                 nval++; /* empty berval at end */
650                 if (norm && a->a_nvals != a->a_vals) {
651                         for (i=0; a->a_nvals[i].bv_val; i++) {
652                                 nval++;
653                                 len += a->a_nvals[i].bv_len + 1;
654                                 len += entry_lenlen(a->a_nvals[i].bv_len);
655                         }
656                         len += entry_lenlen(i); /* i nvals */
657                         nval++;
658                 } else {
659                         len += entry_lenlen(0); /* 0 nvals */
660                 }
661         }
662         len += entry_lenlen(nat);
663         len += entry_lenlen(nval);
664         *plen = len;
665         *pnattrs = nat;
666         *pnvals = nval;
667 }
668
669 /* Add up the size of the entry for a flattened buffer */
670 ber_len_t entry_flatsize(Entry *e, int norm)
671 {
672         ber_len_t len;
673         int nattrs, nvals;
674
675         entry_partsize(e, &len, &nattrs, &nvals, norm);
676         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
677                 (nvals * sizeof(struct berval));
678         return len;
679 }
680
681 /* Flatten an Entry into a buffer. The buffer is filled with just the
682  * strings/bervals of all the entry components. Each field is preceded
683  * by its length, encoded the way ber_put_len works. Every field is NUL
684  * terminated.  The entire buffer size is precomputed so that a single
685  * malloc can be performed. The entry size is also recorded,
686  * to aid in entry_decode.
687  */
688 int entry_encode(Entry *e, struct berval *bv)
689 {
690         ber_len_t len, dnlen, ndnlen;
691         int i, nattrs, nvals;
692         Attribute *a;
693         unsigned char *ptr;
694
695         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
696                 (long) e->e_id, e->e_dn, 0 );
697         dnlen = e->e_name.bv_len;
698         ndnlen = e->e_nname.bv_len;
699
700         entry_partsize( e, &len, &nattrs, &nvals, 1 );
701
702         bv->bv_len = len;
703         bv->bv_val = ch_malloc(len);
704         ptr = (unsigned char *)bv->bv_val;
705         entry_putlen(&ptr, nattrs);
706         entry_putlen(&ptr, nvals);
707         entry_putlen(&ptr, dnlen);
708         AC_MEMCPY(ptr, e->e_dn, dnlen);
709         ptr += dnlen;
710         *ptr++ = '\0';
711         entry_putlen(&ptr, ndnlen);
712         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
713         ptr += ndnlen;
714         *ptr++ = '\0';
715
716         for (a=e->e_attrs; a; a=a->a_next) {
717                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
718                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
719                         a->a_desc->ad_cname.bv_len);
720                 ptr += a->a_desc->ad_cname.bv_len;
721                 *ptr++ = '\0';
722                 if (a->a_vals) {
723                         for (i=0; a->a_vals[i].bv_val; i++);
724                         entry_putlen(&ptr, i);
725                         for (i=0; a->a_vals[i].bv_val; i++) {
726                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
727                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
728                                         a->a_vals[i].bv_len);
729                                 ptr += a->a_vals[i].bv_len;
730                                 *ptr++ = '\0';
731                         }
732                         if (a->a_nvals != a->a_vals) {
733                                 entry_putlen(&ptr, i);
734                                 for (i=0; a->a_nvals[i].bv_val; i++) {
735                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
736                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
737                                         a->a_nvals[i].bv_len);
738                                         ptr += a->a_nvals[i].bv_len;
739                                         *ptr++ = '\0';
740                                 }
741                         } else {
742                                 entry_putlen(&ptr, 0);
743                         }
744                 }
745         }
746         return 0;
747 }
748
749 /* Retrieve an Entry that was stored using entry_encode above.
750  * First entry_header must be called to decode the size of the entry.
751  * Then a single block of memory must be malloc'd to accomodate the
752  * bervals and the bulk data. Next the bulk data is retrieved from
753  * the DB and parsed by entry_decode.
754  *
755  * Note: everything is stored in a single contiguous block, so
756  * you can not free individual attributes or names from this
757  * structure. Attempting to do so will likely corrupt memory.
758  */
759 int entry_header(EntryHeader *eh)
760 {
761         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
762
763         eh->nattrs = entry_getlen(&ptr);
764         if ( !eh->nattrs ) {
765                 Debug( LDAP_DEBUG_ANY,
766                         "entry_header: attribute count was zero\n", 0, 0, 0);
767                 return LDAP_OTHER;
768         }
769         eh->nvals = entry_getlen(&ptr);
770         if ( !eh->nvals ) {
771                 Debug( LDAP_DEBUG_ANY,
772                         "entry_header: value count was zero\n", 0, 0, 0);
773                 return LDAP_OTHER;
774         }
775         eh->data = (char *)ptr;
776         return LDAP_SUCCESS;
777 }
778
779 #ifdef SLAP_ZONE_ALLOC
780 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
781 #else
782 int entry_decode(EntryHeader *eh, Entry **e)
783 #endif
784 {
785         int i, j, count, nattrs, nvals;
786         int rc;
787         Attribute *a;
788         Entry *x;
789         const char *text;
790         AttributeDescription *ad;
791         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
792         BerVarray bptr;
793
794         nattrs = eh->nattrs;
795         nvals = eh->nvals;
796         x = entry_alloc();
797         x->e_attrs = attrs_alloc( nattrs );
798         ptr = (unsigned char *)eh->data;
799         i = entry_getlen(&ptr);
800         x->e_name.bv_val = (char *) ptr;
801         x->e_name.bv_len = i;
802         ptr += i+1;
803         i = entry_getlen(&ptr);
804         x->e_nname.bv_val = (char *) ptr;
805         x->e_nname.bv_len = i;
806         ptr += i+1;
807         Debug( LDAP_DEBUG_TRACE,
808                 "entry_decode: \"%s\"\n",
809                 x->e_dn, 0, 0 );
810         x->e_bv = eh->bv;
811
812         a = x->e_attrs;
813         bptr = (BerVarray)eh->bv.bv_val;
814
815         while ((i = entry_getlen(&ptr))) {
816                 struct berval bv;
817                 bv.bv_len = i;
818                 bv.bv_val = (char *) ptr;
819                 ad = NULL;
820                 rc = slap_bv2ad( &bv, &ad, &text );
821
822                 if( rc != LDAP_SUCCESS ) {
823                         Debug( LDAP_DEBUG_TRACE,
824                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
825                         rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
826
827                         if( rc != LDAP_SUCCESS ) {
828                                 Debug( LDAP_DEBUG_ANY,
829                                         "<= entry_decode: slap_str2undef_ad(%s): %s\n",
830                                                 ptr, text, 0 );
831                                 return rc;
832                         }
833                 }
834                 ptr += i + 1;
835                 a->a_desc = ad;
836                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
837                 count = j = entry_getlen(&ptr);
838                 a->a_vals = bptr;
839
840                 while (j) {
841                         i = entry_getlen(&ptr);
842                         bptr->bv_len = i;
843                         bptr->bv_val = (char *)ptr;
844                         ptr += i+1;
845                         bptr++;
846                         j--;
847                 }
848                 bptr->bv_val = NULL;
849                 bptr->bv_len = 0;
850                 bptr++;
851
852                 j = entry_getlen(&ptr);
853                 if (j) {
854                         a->a_nvals = bptr;
855                         while (j) {
856                                 i = entry_getlen(&ptr);
857                                 bptr->bv_len = i;
858                                 bptr->bv_val = (char *)ptr;
859                                 ptr += i+1;
860                                 bptr++;
861                                 j--;
862                         }
863                         bptr->bv_val = NULL;
864                         bptr->bv_len = 0;
865                         bptr++;
866                 } else {
867                         a->a_nvals = a->a_vals;
868                 }
869                 a = a->a_next;
870                 nattrs--;
871                 if ( !nattrs )
872                         break;
873         }
874
875         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
876                 x->e_dn, 0, 0 );
877         *e = x;
878         return 0;
879 }
880
881 Entry *entry_dup( Entry *e )
882 {
883         Entry *ret;
884
885         ret = entry_alloc();
886
887         ret->e_id = e->e_id;
888         ber_dupbv( &ret->e_name, &e->e_name );
889         ber_dupbv( &ret->e_nname, &e->e_nname );
890         ret->e_attrs = attrs_dup( e->e_attrs );
891         ret->e_ocflags = e->e_ocflags;
892
893         return ret;
894 }
895
896 #if 1
897 /* Duplicates an entry using a single malloc. Saves CPU time, increases
898  * heap usage because a single large malloc is harder to satisfy than
899  * lots of small ones, and the freed space isn't as easily reusable.
900  *
901  * Probably not worth using this function.
902  */
903 Entry *entry_dup_bv( Entry *e )
904 {
905         ber_len_t len;
906         int nattrs, nvals;
907         Entry *ret;
908         struct berval *bvl;
909         char *ptr;
910         Attribute *src, *dst;
911
912         ret = entry_alloc();
913
914         entry_partsize(e, &len, &nattrs, &nvals, 1);
915         ret->e_id = e->e_id;
916         ret->e_attrs = attrs_alloc( nattrs );
917         ret->e_ocflags = e->e_ocflags;
918         ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
919         ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
920
921         bvl = (struct berval *)ret->e_bv.bv_val;
922         ptr = (char *)(bvl + nvals);
923
924         ret->e_name.bv_len = e->e_name.bv_len;
925         ret->e_name.bv_val = ptr;
926         AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
927         ptr += e->e_name.bv_len;
928         *ptr++ = '\0';
929
930         ret->e_nname.bv_len = e->e_nname.bv_len;
931         ret->e_nname.bv_val = ptr;
932         AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
933         ptr += e->e_name.bv_len;
934         *ptr++ = '\0';
935
936         dst = ret->e_attrs;
937         for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
938                 int i;
939                 dst->a_desc = src->a_desc;
940                 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
941                 dst->a_vals = bvl;
942                 for ( i=0; src->a_vals[i].bv_val; i++ ) {
943                         bvl->bv_len = src->a_vals[i].bv_len;
944                         bvl->bv_val = ptr;
945                         AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
946                         ptr += bvl->bv_len;
947                         *ptr++ = '\0';
948                         bvl++;
949                 }
950                 BER_BVZERO(bvl);
951                 bvl++;
952                 if ( src->a_vals != src->a_nvals ) {
953                         dst->a_nvals = bvl;
954                         for ( i=0; src->a_nvals[i].bv_val; i++ ) {
955                                 bvl->bv_len = src->a_nvals[i].bv_len;
956                                 bvl->bv_val = ptr;
957                                 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
958                                 ptr += bvl->bv_len;
959                                 *ptr++ = '\0';
960                                 bvl++;
961                         }
962                         BER_BVZERO(bvl);
963                         bvl++;
964                 }
965         }
966         return ret;
967 }
968 #endif