]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Make ebuf/ecur char* instead of unsigned char* (which they are never used as)
[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-2005 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 static const struct berval dn_bv = BER_BVC("dn");
51
52 int entry_destroy(void)
53 {
54         if ( ebuf ) free( ebuf );
55         ebuf = NULL;
56         ecur = NULL;
57         emaxsize = 0;
58         return 0;
59 }
60
61
62 Entry *
63 str2entry( char *s )
64 {
65         return str2entry2( s, 1 );
66 }
67
68 Entry *
69 str2entry2( char *s, int checkvals )
70 {
71         int rc;
72         Entry           *e;
73         struct berval   *type, *vals, *nvals;
74         char    *freeval;
75         AttributeDescription *ad, *ad_prev;
76         const char *text;
77         char    *next;
78         int             attr_cnt;
79         int             i, lines;
80         Attribute       ahead, *atail;
81
82         /*
83          * LDIF is used as the string format.
84          * An entry looks like this:
85          *
86          *      dn: <dn>\n
87          *      [<attr>:[:] <value>\n]
88          *      [<tab><continuedvalue>\n]*
89          *      ...
90          *
91          * If a double colon is used after a type, it means the
92          * following value is encoded as a base 64 string.  This
93          * happens if the value contains a non-printing character
94          * or newline.
95          */
96
97         Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
98                 s ? s : "NULL", 0, 0 );
99
100         /* initialize reader/writer lock */
101         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
102
103         if( e == NULL ) {
104                 Debug( LDAP_DEBUG_ANY,
105                         "<= str2entry NULL (entry allocation failed)\n",
106                         0, 0, 0 );
107                 return( NULL );
108         }
109
110         /* initialize entry */
111         e->e_id = NOID;
112
113         /* dn + attributes */
114         atail = &ahead;
115         ahead.a_next = NULL;
116         ad = NULL;
117         ad_prev = NULL;
118         attr_cnt = 0;
119         next = s;
120
121         lines = ldif_countlines( s );
122         type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
123         vals = type+lines+1;
124         nvals = vals+lines+1;
125         freeval = (char *)(nvals+lines+1);
126         i = -1;
127
128         /* parse into individual values, record DN */
129         while ( (s = ldif_getline( &next )) != NULL ) {
130                 int freev;
131                 if ( *s == '\n' || *s == '\0' ) {
132                         break;
133                 }
134                 i++;
135
136                 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
137                 freeval[i] = freev;
138                 if ( rc ) {
139                         Debug( LDAP_DEBUG_TRACE,
140                                 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
141                         continue;
142                 }
143
144                 if ( type[i].bv_len == dn_bv.bv_len &&
145                         strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
146
147                         if ( e->e_dn != NULL ) {
148                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
149                                         "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
150                                         (long) e->e_id, e->e_dn, vals[i].bv_val );
151                                 goto fail;
152                         }
153
154                         rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
155                         if( rc != LDAP_SUCCESS ) {
156                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
157                                         "entry %ld has invalid DN \"%s\"\n",
158                                         (long) e->e_id, vals[i].bv_val, 0 );
159                                 goto fail;
160                         }
161                         if ( freeval[i] ) free( vals[i].bv_val );
162                         vals[i].bv_val = NULL;
163                         i--;
164                         continue;
165                 }
166         }
167         lines = i+1;
168
169         /* check to make sure there was a dn: line */
170         if ( BER_BVISNULL( &e->e_name )) {
171                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
172                         (long) e->e_id, 0, 0 );
173                 goto fail;
174         }
175
176         /* Make sure all attributes with multiple values are contiguous */
177         if ( checkvals ) {
178                 int j, k;
179                 struct berval bv;
180                 int fv;
181
182                 for (i=0; i<lines; i++) {
183                         for ( j=i+1; j<lines; j++ ) {
184                                 if ( bvmatch( type+i, type+j )) {
185                                         /* out of order, move intervening attributes down */
186                                         if ( j != i+1 ) {
187                                                 bv = vals[j];
188                                                 fv = freeval[j];
189                                                 for ( k=j; k>i; k-- ) {
190                                                         type[k] = type[k-1];
191                                                         vals[k] = vals[k-1];
192                                                         freeval[k] = freeval[k-1];
193                                                 }
194                                                 k++;
195                                                 type[k] = type[i];
196                                                 vals[k] = bv;
197                                                 freeval[k] = fv;
198                                         }
199                                         i++;
200                                 }
201                         }
202                 }
203         }
204
205         for ( i=0; i<=lines; i++ ) {
206                 ad_prev = ad;
207                 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
208                         ad = NULL;
209                         rc = slap_bv2ad( type+i, &ad, &text );
210
211                         if( rc != LDAP_SUCCESS ) {
212                                 Debug( slapMode & SLAP_TOOL_MODE
213                                         ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
214                                         "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
215                                 if( slapMode & SLAP_TOOL_MODE ) {
216                                         goto fail;
217                                 }
218
219                                 rc = slap_bv2undef_ad( type+i, &ad, &text );
220                                 if( rc != LDAP_SUCCESS ) {
221                                         Debug( LDAP_DEBUG_ANY,
222                                                 "<= str2entry: str2undef_ad(%s): %s\n",
223                                                         type[i].bv_val, text, 0 );
224                                         goto fail;
225                                 }
226                         }
227                 }
228
229                 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
230                         int j, k;
231                         atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
232                         atail = atail->a_next;
233                         atail->a_desc = ad_prev;
234                         atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
235                         if( ad_prev->ad_type->sat_equality &&
236                                 ad_prev->ad_type->sat_equality->smr_normalize )
237                                 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
238                         else
239                                 atail->a_nvals = NULL;
240                         k = i - attr_cnt;
241                         for ( j=0; j<attr_cnt; j++ ) {
242                                 if ( freeval[k] )
243                                         atail->a_vals[j] = vals[k];
244                                 else
245                                         ber_dupbv( atail->a_vals+j, &vals[k] );
246                                 vals[k].bv_val = NULL;
247                                 if ( atail->a_nvals ) {
248                                         atail->a_nvals[j] = nvals[k];
249                                         nvals[k].bv_val = NULL;
250                                 }
251                                 k++;
252                         }
253                         BER_BVZERO( &atail->a_vals[j] );
254                         if ( atail->a_nvals ) {
255                                 BER_BVZERO( &atail->a_nvals[j] );
256                         } else {
257                                 atail->a_nvals = atail->a_vals;
258                         }
259                         attr_cnt = 0;
260                         if ( i == lines ) break;
261                 }
262
263                 if( slapMode & SLAP_TOOL_MODE ) {
264                         struct berval pval;
265                         slap_syntax_validate_func *validate =
266                                 ad->ad_type->sat_syntax->ssyn_validate;
267                         slap_syntax_transform_func *pretty =
268                                 ad->ad_type->sat_syntax->ssyn_pretty;
269
270                         if( pretty ) {
271                                 rc = pretty( ad->ad_type->sat_syntax,
272                                         &vals[i], &pval, NULL );
273
274                         } else if( validate ) {
275                                 /*
276                                  * validate value per syntax
277                                  */
278                                 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
279
280                         } else {
281                                 Debug( LDAP_DEBUG_ANY,
282                                         "str2entry: attributeType %s #%d: "
283                                         "no validator for syntax %s\n", 
284                                         ad->ad_cname.bv_val, attr_cnt,
285                                         ad->ad_type->sat_syntax->ssyn_oid );
286                                 goto fail;
287                         }
288
289                         if( rc != 0 ) {
290                                 Debug( LDAP_DEBUG_ANY,
291                                         "str2entry: invalid value "
292                                         "for attributeType %s #%d (syntax %s)\n",
293                                         ad->ad_cname.bv_val, attr_cnt,
294                                         ad->ad_type->sat_syntax->ssyn_oid );
295                                 goto fail;
296                         }
297
298                         if( pretty ) {
299                                 if ( freeval[i] ) free( vals[i].bv_val );
300                                 vals[i] = pval;
301                                 freeval[i] = 1;
302                         }
303                 }
304
305                 if( ad->ad_type->sat_equality &&
306                         ad->ad_type->sat_equality->smr_normalize )
307                 {
308                         rc = ad->ad_type->sat_equality->smr_normalize(
309                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
310                                 ad->ad_type->sat_syntax,
311                                 ad->ad_type->sat_equality,
312                                 &vals[i], &nvals[i], NULL );
313
314                         if( rc ) {
315                                 Debug( LDAP_DEBUG_ANY,
316                                         "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
317                                 goto fail;
318                         }
319                 }
320
321                 attr_cnt++;
322         }
323
324         free( type );
325         atail->a_next = NULL;
326         e->e_attrs = ahead.a_next;
327
328         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
329                 e->e_dn, (unsigned long) e, 0 );
330         return( e );
331
332 fail:
333         for ( i=0; i<lines; i++ ) {
334                 if ( freeval[i] ) free( vals[i].bv_val );
335                 free( nvals[i].bv_val );
336         }
337         free( type );
338         entry_free( e );
339         return NULL;
340 }
341
342
343 #define GRABSIZE        BUFSIZ
344
345 #define MAKE_SPACE( n ) { \
346                 while ( ecur + (n) > ebuf + emaxsize ) { \
347                         ptrdiff_t       offset; \
348                         offset = (int) (ecur - ebuf); \
349                         ebuf = ch_realloc( ebuf, \
350                                 emaxsize + GRABSIZE ); \
351                         emaxsize += GRABSIZE; \
352                         ecur = ebuf + offset; \
353                 } \
354         }
355
356 char *
357 entry2str(
358         Entry   *e,
359         int             *len )
360 {
361         Attribute       *a;
362         struct berval   *bv;
363         int             i;
364         ber_len_t tmplen;
365
366         assert( e != NULL );
367
368         /*
369          * In string format, an entry looks like this:
370          *      dn: <dn>\n
371          *      [<attr>: <value>\n]*
372          */
373
374         ecur = ebuf;
375
376         /* put the dn */
377         if ( e->e_dn != NULL ) {
378                 /* put "dn: <dn>" */
379                 tmplen = e->e_name.bv_len;
380                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
381                 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
382         }
383
384         /* put the attributes */
385         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
386                 /* put "<type>:[:] <value>" line for each value */
387                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
388                         bv = &a->a_vals[i];
389                         tmplen = a->a_desc->ad_cname.bv_len;
390                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
391                         ldif_sput( &ecur, LDIF_PUT_VALUE,
392                                 a->a_desc->ad_cname.bv_val,
393                                 bv->bv_val, bv->bv_len );
394                 }
395         }
396         MAKE_SPACE( 1 );
397         *ecur = '\0';
398         *len = ecur - ebuf;
399
400         return( ebuf );
401 }
402
403 void
404 entry_clean( Entry *e )
405 {
406         /* free an entry structure */
407         assert( e != NULL );
408
409         /* e_private must be freed by the caller */
410         assert( e->e_private == NULL );
411         e->e_private = NULL;
412
413         /* free DNs */
414         if ( !BER_BVISNULL( &e->e_name ) ) {
415                 free( e->e_name.bv_val );
416                 BER_BVZERO( &e->e_name );
417         }
418         if ( !BER_BVISNULL( &e->e_nname ) ) {
419                 free( e->e_nname.bv_val );
420                 BER_BVZERO( &e->e_nname );
421         }
422
423         if ( !BER_BVISNULL( &e->e_bv ) ) {
424                 free( e->e_bv.bv_val );
425                 BER_BVZERO( &e->e_bv );
426         }
427
428         /* free attributes */
429         attrs_free( e->e_attrs );
430         e->e_attrs = NULL;
431 }
432
433 void
434 entry_free( Entry *e )
435 {
436         entry_clean( e );
437
438         free( e );
439 }
440
441 /*
442  * These routines are used only by Backend.
443  *
444  * the Entry has three entry points (ways to find things):
445  *
446  *      by entry        e.g., if you already have an entry from the cache
447  *                      and want to delete it. (really by entry ptr)
448  *      by dn           e.g., when looking for the base object of a search
449  *      by id           e.g., for search candidates
450  *
451  * these correspond to three different avl trees that are maintained.
452  */
453
454 int
455 entry_cmp( Entry *e1, Entry *e2 )
456 {
457         return SLAP_PTRCMP( e1, e2 );
458 }
459
460 int
461 entry_dn_cmp( const void *v_e1, const void *v_e2 )
462 {
463         /* compare their normalized UPPERCASED dn's */
464         const Entry *e1 = v_e1, *e2 = v_e2;
465
466         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
467 }
468
469 int
470 entry_id_cmp( const void *v_e1, const void *v_e2 )
471 {
472         const Entry *e1 = v_e1, *e2 = v_e2;
473         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
474 }
475
476 /* This is like a ber_len */
477 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
478         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
479
480 static void
481 entry_putlen(unsigned char **buf, ber_len_t len)
482 {
483         ber_len_t lenlen = entry_lenlen(len);
484
485         if (lenlen == 1) {
486                 **buf = (unsigned char) len;
487         } else {
488                 int i;
489                 **buf = 0x80 | ((unsigned char) lenlen - 1);
490                 for (i=lenlen-1; i>0; i--) {
491                         (*buf)[i] = (unsigned char) len;
492                         len >>= 8;
493                 }
494         }
495         *buf += lenlen;
496 }
497
498 static ber_len_t
499 entry_getlen(unsigned char **buf)
500 {
501         ber_len_t len;
502         int i;
503
504         len = *(*buf)++;
505         if (len <= 0x7f)
506                 return len;
507         i = len & 0x7f;
508         len = 0;
509         for (;i > 0; i--) {
510                 len <<= 8;
511                 len |= *(*buf)++;
512         }
513         return len;
514 }
515
516 /* Count up the sizes of the components of an entry */
517 void entry_partsize(Entry *e, ber_len_t *plen,
518         int *pnattrs, int *pnvals, int norm)
519 {
520         ber_len_t len, dnlen, ndnlen;
521         int i, nat = 0, nval = 0;
522         Attribute *a;
523
524         dnlen = e->e_name.bv_len;
525         len = dnlen + 1;        /* trailing NUL byte */
526         len += entry_lenlen(dnlen);
527         if (norm) {
528                 ndnlen = e->e_nname.bv_len;
529                 len += ndnlen + 1;
530                 len += entry_lenlen(ndnlen);
531         }
532         for (a=e->e_attrs; a; a=a->a_next) {
533                 /* For AttributeDesc, we only store the attr name */
534                 nat++;
535                 len += a->a_desc->ad_cname.bv_len+1;
536                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
537                 for (i=0; a->a_vals[i].bv_val; i++) {
538                         nval++;
539                         len += a->a_vals[i].bv_len + 1;
540                         len += entry_lenlen(a->a_vals[i].bv_len);
541                 }
542                 len += entry_lenlen(i);
543                 nval++; /* empty berval at end */
544                 if (norm && a->a_nvals != a->a_vals) {
545                         for (i=0; a->a_nvals[i].bv_val; i++) {
546                                 nval++;
547                                 len += a->a_nvals[i].bv_len + 1;
548                                 len += entry_lenlen(a->a_nvals[i].bv_len);
549                         }
550                         len += entry_lenlen(i); /* i nvals */
551                         nval++;
552                 } else {
553                         len += entry_lenlen(0); /* 0 nvals */
554                 }
555         }
556         len += entry_lenlen(nat);
557         len += entry_lenlen(nval);
558         *plen = len;
559         *pnattrs = nat;
560         *pnvals = nval;
561 }
562
563 /* Add up the size of the entry for a flattened buffer */
564 ber_len_t entry_flatsize(Entry *e, int norm)
565 {
566         ber_len_t len;
567         int nattrs, nvals;
568
569         entry_partsize(e, &len, &nattrs, &nvals, norm);
570         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
571                 (nvals * sizeof(struct berval));
572         return len;
573 }
574
575 /* Flatten an Entry into a buffer. The buffer is filled with just the
576  * strings/bervals of all the entry components. Each field is preceded
577  * by its length, encoded the way ber_put_len works. Every field is NUL
578  * terminated.  The entire buffer size is precomputed so that a single
579  * malloc can be performed. The entry size is also recorded,
580  * to aid in entry_decode.
581  */
582 int entry_encode(Entry *e, struct berval *bv)
583 {
584         ber_len_t len, dnlen, ndnlen;
585         int i, nattrs, nvals;
586         Attribute *a;
587         unsigned char *ptr;
588
589         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
590                 (long) e->e_id, e->e_dn, 0 );
591         dnlen = e->e_name.bv_len;
592         ndnlen = e->e_nname.bv_len;
593
594         entry_partsize( e, &len, &nattrs, &nvals, 1 );
595
596         bv->bv_len = len;
597         bv->bv_val = ch_malloc(len);
598         ptr = (unsigned char *)bv->bv_val;
599         entry_putlen(&ptr, nattrs);
600         entry_putlen(&ptr, nvals);
601         entry_putlen(&ptr, dnlen);
602         AC_MEMCPY(ptr, e->e_dn, dnlen);
603         ptr += dnlen;
604         *ptr++ = '\0';
605         entry_putlen(&ptr, ndnlen);
606         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
607         ptr += ndnlen;
608         *ptr++ = '\0';
609
610         for (a=e->e_attrs; a; a=a->a_next) {
611                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
612                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
613                         a->a_desc->ad_cname.bv_len);
614                 ptr += a->a_desc->ad_cname.bv_len;
615                 *ptr++ = '\0';
616                 if (a->a_vals) {
617                         for (i=0; a->a_vals[i].bv_val; i++);
618                                 entry_putlen(&ptr, i);
619                                 for (i=0; a->a_vals[i].bv_val; i++) {
620                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
621                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
622                                         a->a_vals[i].bv_len);
623                                 ptr += a->a_vals[i].bv_len;
624                                 *ptr++ = '\0';
625                         }
626                         if (a->a_nvals != a->a_vals) {
627                                 entry_putlen(&ptr, i);
628                                 for (i=0; a->a_nvals[i].bv_val; i++) {
629                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
630                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
631                                         a->a_nvals[i].bv_len);
632                                         ptr += a->a_nvals[i].bv_len;
633                                         *ptr++ = '\0';
634                                 }
635                         } else {
636                                 entry_putlen(&ptr, 0);
637                         }
638                 }
639         }
640         return 0;
641 }
642
643 /* Retrieve an Entry that was stored using entry_encode above.
644  * We malloc a single block with the size stored above for the Entry
645  * and all of its Attributes. We also must lookup the stored
646  * attribute names to get AttributeDescriptions. To detect if the
647  * attributes of an Entry are later modified, we note that e->e_attr
648  * is always a constant offset from (e).
649  *
650  * Note: everything is stored in a single contiguous block, so
651  * you can not free individual attributes or names from this
652  * structure. Attempting to do so will likely corrupt memory.
653  */
654 #ifdef SLAP_ZONE_ALLOC
655 int entry_decode(struct berval *bv, Entry **e, void *ctx)
656 #else
657 int entry_decode(struct berval *bv, Entry **e)
658 #endif
659 {
660         int i, j, count, nattrs, nvals;
661         int rc;
662         Attribute *a;
663         Entry *x;
664         const char *text;
665         AttributeDescription *ad;
666         unsigned char *ptr = (unsigned char *)bv->bv_val;
667         BerVarray bptr;
668
669         nattrs = entry_getlen(&ptr);
670         if (!nattrs) {
671                 Debug( LDAP_DEBUG_ANY,
672                         "entry_decode: attribute count was zero\n", 0, 0, 0);
673                 return LDAP_OTHER;
674         }
675         nvals = entry_getlen(&ptr);
676         if (!nvals) {
677                 Debug( LDAP_DEBUG_ANY,
678                         "entry_decode: value count was zero\n", 0, 0, 0);
679                 return LDAP_OTHER;
680         }
681         i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
682                 (nvals * sizeof(struct berval));
683 #ifdef SLAP_ZONE_ALLOC
684         x = slap_zn_calloc(1, i + bv->bv_len, ctx);
685         AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
686         bv->bv_val = (char*)x + i;
687         ptr = (unsigned char *)bv->bv_val;
688         /* pointer is reset, now advance past nattrs and nvals again */
689         entry_getlen(&ptr);
690         entry_getlen(&ptr);
691 #else
692         x = ch_calloc(1, i);
693 #endif
694         i = entry_getlen(&ptr);
695         x->e_name.bv_val = (char *) ptr;
696         x->e_name.bv_len = i;
697         ptr += i+1;
698         i = entry_getlen(&ptr);
699         x->e_nname.bv_val = (char *) ptr;
700         x->e_nname.bv_len = i;
701         ptr += i+1;
702         Debug( LDAP_DEBUG_TRACE,
703                 "entry_decode: \"%s\"\n",
704                 x->e_dn, 0, 0 );
705         x->e_bv = *bv;
706
707         /* A valid entry must have at least one attr, so this
708          * pointer can never be NULL
709          */
710         x->e_attrs = (Attribute *)(x+1);
711         bptr = (BerVarray)x->e_attrs;
712         a = NULL;
713
714         while ((i = entry_getlen(&ptr))) {
715                 struct berval bv;
716                 bv.bv_len = i;
717                 bv.bv_val = (char *) ptr;
718                 if (a) {
719                         a->a_next = (Attribute *)bptr;
720                 }
721                 a = (Attribute *)bptr;
722                 ad = NULL;
723                 rc = slap_bv2ad( &bv, &ad, &text );
724
725                 if( rc != LDAP_SUCCESS ) {
726                         Debug( LDAP_DEBUG_TRACE,
727                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
728                         rc = slap_bv2undef_ad( &bv, &ad, &text );
729
730                         if( rc != LDAP_SUCCESS ) {
731                                 Debug( LDAP_DEBUG_ANY,
732                                         "<= entry_decode: str2undef_ad(%s): %s\n",
733                                                 ptr, text, 0 );
734                                 return rc;
735                         }
736                 }
737                 ptr += i + 1;
738                 a->a_desc = ad;
739                 bptr = (BerVarray)(a+1);
740                 a->a_vals = bptr;
741                 a->a_flags = 0;
742 #ifdef LDAP_COMP_MATCH
743                 a->a_comp_data = NULL;
744 #endif
745                 count = j = entry_getlen(&ptr);
746
747                 while (j) {
748                         i = entry_getlen(&ptr);
749                         bptr->bv_len = i;
750                         bptr->bv_val = (char *)ptr;
751                         ptr += i+1;
752                         bptr++;
753                         j--;
754                 }
755                 bptr->bv_val = NULL;
756                 bptr->bv_len = 0;
757                 bptr++;
758
759                 j = entry_getlen(&ptr);
760                 if (j) {
761                         a->a_nvals = bptr;
762                         while (j) {
763                                 i = entry_getlen(&ptr);
764                                 bptr->bv_len = i;
765                                 bptr->bv_val = (char *)ptr;
766                                 ptr += i+1;
767                                 bptr++;
768                                 j--;
769                         }
770                         bptr->bv_val = NULL;
771                         bptr->bv_len = 0;
772                         bptr++;
773                 } else {
774                         a->a_nvals = a->a_vals;
775                 }
776                 nattrs--;
777                 if ( !nattrs )
778                         break;
779         }
780
781         if (a) a->a_next = NULL;
782         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
783                 x->e_dn, 0, 0 );
784         *e = x;
785         return 0;
786 }
787
788 Entry *entry_dup( Entry *e )
789 {
790         Entry *ret;
791
792         ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
793
794         ret->e_id = e->e_id;
795         ber_dupbv( &ret->e_name, &e->e_name );
796         ber_dupbv( &ret->e_nname, &e->e_nname );
797         ret->e_attrs = attrs_dup( e->e_attrs );
798         ret->e_ocflags = e->e_ocflags;
799         ret->e_bv.bv_val = NULL;
800         ret->e_bv.bv_len = 0;
801         ret->e_private = NULL;
802
803         return ret;
804 }
805