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