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