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