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