]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
In ordered_value_sort, allow zero-length ordered values.
[openldap] / servers / slapd / value.c
1 /* value.c - routines for dealing with values */
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 /*
17  * Copyright (c) 1995 Regents of the University of Michigan.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * provided that this notice is preserved and that due credit is given
22  * to the University of Michigan at Ann Arbor. The name of the University
23  * may not be used to endorse or promote products derived from this
24  * software without specific prior written permission. This software
25  * is provided ``as is'' without express or implied warranty.
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/ctype.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/time.h>
36
37 #include <sys/stat.h>
38
39 #include "slap.h"
40
41 int
42 value_add( 
43     BerVarray   *vals,
44     BerVarray   addvals )
45 {
46         int             n, nn = 0;
47         BerVarray       v2;
48
49         if ( addvals != NULL ) {
50                 for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ )
51                         ;       /* NULL */
52         }
53
54         if ( *vals == NULL ) {
55                 *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
56                     * sizeof(struct berval) );
57                 if( *vals == NULL ) {
58                         Debug(LDAP_DEBUG_TRACE,
59                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
60                         return LBER_ERROR_MEMORY;
61                 }
62                 n = 0;
63
64         } else {
65                 for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
66                         ;       /* Empty */
67                 }
68                 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
69                     (n + nn + 1) * sizeof(struct berval) );
70                 if( *vals == NULL ) {
71                         Debug(LDAP_DEBUG_TRACE,
72                       "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
73                         return LBER_ERROR_MEMORY;
74                 }
75         }
76
77         v2 = &(*vals)[n];
78         for ( ; !BER_BVISNULL( addvals ); v2++, addvals++ ) {
79                 ber_dupbv( v2, addvals );
80                 if ( BER_BVISNULL( v2 ) ) break;
81         }
82         BER_BVZERO( v2 );
83
84         return LDAP_SUCCESS;
85 }
86
87 int
88 value_add_one( 
89     BerVarray           *vals,
90     struct berval       *addval )
91 {
92         int             n;
93         BerVarray       v2;
94
95         if ( *vals == NULL ) {
96                 *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
97                 if( *vals == NULL ) {
98                         Debug(LDAP_DEBUG_TRACE,
99                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
100                         return LBER_ERROR_MEMORY;
101                 }
102                 n = 0;
103
104         } else {
105                 for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
106                         ;       /* Empty */
107                 }
108                 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
109                     (n + 2) * sizeof(struct berval) );
110                 if( *vals == NULL ) {
111                         Debug(LDAP_DEBUG_TRACE,
112                       "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
113                         return LBER_ERROR_MEMORY;
114                 }
115         }
116
117         v2 = &(*vals)[n];
118         ber_dupbv(v2, addval);
119
120         v2++;
121         BER_BVZERO( v2 );
122
123         return LDAP_SUCCESS;
124 }
125
126 int asserted_value_validate_normalize( 
127         AttributeDescription *ad,
128         MatchingRule *mr,
129         unsigned usage,
130         struct berval *in,
131         struct berval *out,
132         const char ** text,
133         void *ctx )
134 {
135         int rc;
136         struct berval pval;
137         pval.bv_val = NULL;
138
139         /* we expect the value to be in the assertion syntax */
140         assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
141
142         if( mr == NULL ) {
143                 *text = "inappropriate matching request";
144                 return LDAP_INAPPROPRIATE_MATCHING;
145         }
146
147         if( !mr->smr_match ) {
148                 *text = "requested matching rule not supported";
149                 return LDAP_INAPPROPRIATE_MATCHING;
150         }
151
152         if( mr->smr_syntax->ssyn_pretty ) {
153                 rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
154                 in = &pval;
155
156         } else {
157                 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
158         }
159
160         if( rc != LDAP_SUCCESS ) {
161                 *text = "value does not conform to assertion syntax";
162                 return LDAP_INVALID_SYNTAX;
163         }
164
165         if( mr->smr_normalize ) {
166                 rc = (mr->smr_normalize)(
167                         usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
168                         ad ? ad->ad_type->sat_syntax : NULL,
169                         mr, in, out, ctx );
170
171                 if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
172
173                 if( rc != LDAP_SUCCESS ) {
174                         *text = "unable to normalize value for matching";
175                         return LDAP_INVALID_SYNTAX;
176                 }
177
178         } else if ( pval.bv_val != NULL ) {
179                 *out = pval;
180
181         } else {
182                 ber_dupbv_x( out, in, ctx );
183         }
184
185         return LDAP_SUCCESS;
186 }
187
188 int
189 value_match(
190         int *match,
191         AttributeDescription *ad,
192         MatchingRule *mr,
193         unsigned flags,
194         struct berval *v1, /* stored value */
195         void *v2, /* assertion */
196         const char ** text )
197 {
198         int rc;
199
200         assert( mr != NULL );
201
202         if( !mr->smr_match ) {
203                 return LDAP_INAPPROPRIATE_MATCHING;
204         }
205
206         rc = (mr->smr_match)( match, flags,
207                 ad->ad_type->sat_syntax, mr, v1, v2 );
208         
209         return rc;
210 }
211
212 int value_find_ex(
213         AttributeDescription *ad,
214         unsigned flags,
215         BerVarray vals,
216         struct berval *val,
217         void *ctx )
218 {
219         int     i;
220         int rc;
221         struct berval nval = BER_BVNULL;
222         MatchingRule *mr = ad->ad_type->sat_equality;
223
224         if( mr == NULL || !mr->smr_match ) {
225                 return LDAP_INAPPROPRIATE_MATCHING;
226         }
227
228         assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
229
230         if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
231                 mr->smr_normalize )
232         {
233                 rc = (mr->smr_normalize)(
234                         flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
235                         ad ? ad->ad_type->sat_syntax : NULL,
236                         mr, val, &nval, ctx );
237
238                 if( rc != LDAP_SUCCESS ) {
239                         return LDAP_INVALID_SYNTAX;
240                 }
241         }
242
243         for ( i = 0; vals[i].bv_val != NULL; i++ ) {
244                 int match;
245                 const char *text;
246
247                 rc = value_match( &match, ad, mr, flags,
248                         &vals[i], nval.bv_val == NULL ? val : &nval, &text );
249
250                 if( rc == LDAP_SUCCESS && match == 0 ) {
251                         slap_sl_free( nval.bv_val, ctx );
252                         return rc;
253                 }
254         }
255
256         slap_sl_free( nval.bv_val, ctx );
257         return LDAP_NO_SUCH_ATTRIBUTE;
258 }
259
260 /* assign new indexes to an attribute's ordered values */
261 void
262 ordered_value_renumber( Attribute *a, int vals )
263 {
264         char *ptr, ibuf[64];    /* many digits */
265         struct berval ibv, tmp, vtmp;
266         int i;
267
268         ibv.bv_val = ibuf;
269
270         for (i=0; i<vals; i++) {
271                 ibv.bv_len = sprintf(ibv.bv_val, "{%d}", i);
272                 vtmp = a->a_vals[i];
273                 if ( vtmp.bv_val[0] == '{' ) {
274                         ptr = strchr(vtmp.bv_val, '}') + 1;
275                         vtmp.bv_len -= ptr - vtmp.bv_val;
276                         vtmp.bv_val = ptr;
277                 }
278                 tmp.bv_len = ibv.bv_len + vtmp.bv_len;
279                 tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
280                 strcpy( tmp.bv_val, ibv.bv_val );
281                 AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
282                 tmp.bv_val[tmp.bv_len] = '\0';
283                 ch_free( a->a_vals[i].bv_val );
284                 a->a_vals[i] = tmp;
285
286                 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
287                         vtmp = a->a_nvals[i];
288                         if ( vtmp.bv_val[0] == '{' ) {
289                                 ptr = strchr(vtmp.bv_val, '}') + 1;
290                                 vtmp.bv_len -= ptr - vtmp.bv_val;
291                                 vtmp.bv_val = ptr;
292                         }
293                         tmp.bv_len = ibv.bv_len + vtmp.bv_len;
294                         tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
295                         strcpy( tmp.bv_val, ibv.bv_val );
296                         AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
297                         tmp.bv_val[tmp.bv_len] = '\0';
298                         ch_free( a->a_nvals[i].bv_val );
299                         a->a_nvals[i] = tmp;
300                 }
301         }
302 }
303
304 /* Sort the values in an X-ORDERED VALUES attribute.
305  * If the values have no index, index them in their given order.
306  * If the values have indexes, sort them.
307  * If some are indexed and some are not, return Error.
308  */
309 int
310 ordered_value_sort( Attribute *a, int do_renumber )
311 {
312         int i, vals;
313         int index = 0, noindex = 0, renumber = 0, gotnvals = 0;
314         struct berval tmp;
315
316         if ( a->a_nvals && a->a_nvals != a->a_vals )
317                 gotnvals = 1;
318
319         /* count attrs, look for index */
320         for (i=0; a->a_vals[i].bv_val; i++) {
321                 if ( a->a_vals[i].bv_val[0] == '{' ) {
322                         char *ptr;
323                         index = 1;
324                         ptr = strchr( a->a_vals[i].bv_val, '}' );
325                         if ( !ptr )
326                                 return LDAP_INVALID_SYNTAX;
327                         if ( noindex )
328                                 return LDAP_INVALID_SYNTAX;
329                 } else {
330                         noindex = 1;
331                         if ( index )
332                                 return LDAP_INVALID_SYNTAX;
333                 }
334         }
335         vals = i;
336
337         /* If values have indexes, sort the values */
338         if ( index ) {
339                 int *indexes, j, idx;
340                 struct berval ntmp;
341
342 #if 0
343                 /* Strip index from normalized values */
344                 if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
345                         a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
346                         BER_BVZERO(a->a_nvals+vals);
347                         for ( i=0; i<vals; i++ ) {
348                                 ptr = strchr(a->a_vals[i].bv_val, '}') + 1;
349                                 a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
350                                         (ptr - a->a_vals[i].bv_val);
351                                 a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
352                                 strcpy(a->a_nvals[i].bv_val, ptr );
353                         }
354                 } else {
355                         for ( i=0; i<vals; i++ ) {
356                                 ptr = strchr(a->a_nvals[i].bv_val, '}') + 1;
357                                 a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
358                                 strcpy(a->a_nvals[i].bv_val, ptr);
359                         }
360                 }
361 #endif
362                                 
363                 indexes = ch_malloc( vals * sizeof(int) );
364                 for ( i=0; i<vals; i++) {
365                         char *ptr;
366                         indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
367                         if ( *ptr != '}' ) {
368                                 ch_free( indexes );
369                                 return LDAP_INVALID_SYNTAX;
370                         }
371                 }
372
373                 /* Insertion sort */
374                 for ( i=1; i<vals; i++ ) {
375                         idx = indexes[i];
376                         tmp = a->a_vals[i];
377                         if ( gotnvals ) ntmp = a->a_nvals[i];
378                         j = i;
379                         while ((j > 0) && (indexes[j-1] > idx)) {
380                                 indexes[j] = indexes[j-1];
381                                 a->a_vals[j] = a->a_vals[j-1];
382                                 if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
383                                 j--;
384                         }
385                         indexes[j] = idx;
386                         a->a_vals[j] = tmp;
387                         if ( gotnvals ) a->a_nvals[j] = ntmp;
388                 }
389
390                 /* If range is not contiguous, must renumber */
391                 if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
392                         renumber = 1;
393                 }
394                 ch_free( indexes );
395         } else {
396                 renumber = 1;
397         }
398
399         if ( do_renumber && renumber )
400                 ordered_value_renumber( a, vals );
401
402         return 0;
403 }
404
405 /*
406  * wrapper for validate function
407  * uses the validate function of the syntax after removing
408  * the index, if allowed an present
409  */
410 int
411 ordered_value_validate(
412         AttributeDescription *ad,
413         struct berval *in )
414 {
415         struct berval   bv = *in;
416
417         assert( ad->ad_type->sat_syntax != NULL );
418         assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
419
420         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
421
422                 /* Skip past the assertion index */
423                 if ( bv.bv_val[0] == '{' ) {
424                         char    *ptr;
425
426                         ptr = strchr( bv.bv_val, '}' );
427                         if ( ptr == NULL ) {
428                                 return LDAP_INVALID_SYNTAX;
429                         }
430                         ptr++;
431                         bv.bv_len -= ptr - bv.bv_val;
432                         bv.bv_val = ptr;
433                         in = &bv;
434                 }
435         }
436
437         return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
438 }
439
440 /*
441  * wrapper for pretty function
442  * uses the pretty function of the syntax after removing
443  * the index, if allowed and present; in case, it's prepended
444  * to the pretty value
445  */
446 int
447 ordered_value_pretty(
448         AttributeDescription *ad,
449         struct berval *val,
450         struct berval *out,
451         void *ctx )
452 {
453         struct berval   bv = *val,
454                         idx = BER_BVNULL;
455         int             rc;
456
457         assert( ad->ad_type->sat_syntax != NULL );
458         assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
459         assert( val != NULL );
460         assert( out != NULL );
461
462         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
463
464                 /* Skip past the assertion index */
465                 if ( bv.bv_val[0] == '{' ) {
466                         char    *ptr;
467
468                         ptr = strchr( bv.bv_val, '}' );
469                         if ( ptr == NULL ) {
470                                 return LDAP_INVALID_SYNTAX;
471                         }
472                         ptr++;
473
474                         idx = bv;
475                         idx.bv_len = ptr - bv.bv_val;
476
477                         bv.bv_len -= idx.bv_len;
478                         bv.bv_val = ptr;
479
480                         val = &bv;
481                 }
482         }
483
484         rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
485
486         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
487                 bv = *out;
488
489                 out->bv_len = idx.bv_len + bv.bv_len;
490                 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
491                 
492                 AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
493                 AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
494
495                 ber_memfree_x( bv.bv_val, ctx );
496         }
497
498         return rc;
499 }
500
501 /*
502  * wrapper for normalize function
503  * uses the normalize function of the attribute description equality rule
504  * after removing the index, if allowed and present; in case, it's
505  * prepended to the value
506  */
507 int
508 ordered_value_normalize(
509         slap_mask_t usage,
510         AttributeDescription *ad,
511         MatchingRule *mr,
512         struct berval *val,
513         struct berval *normalized,
514         void *ctx )
515 {
516         struct berval   bv = *val,
517                         idx = BER_BVNULL;
518         int             rc;
519
520         assert( ad->ad_type->sat_equality != NULL );
521         assert( ad->ad_type->sat_equality->smr_normalize != NULL );
522         assert( val != NULL );
523         assert( normalized != NULL );
524
525         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
526
527                 /* Skip past the assertion index */
528                 if ( bv.bv_val[0] == '{' ) {
529                         char    *ptr;
530
531                         ptr = strchr( bv.bv_val, '}' );
532                         if ( ptr == NULL ) {
533                                 return LDAP_INVALID_SYNTAX;
534                         }
535                         ptr++;
536
537                         idx = bv;
538                         idx.bv_len = ptr - bv.bv_val;
539
540                         bv.bv_len -= idx.bv_len;
541                         bv.bv_val = ptr;
542
543                         val = &bv;
544                 }
545         }
546
547         rc = ad->ad_type->sat_equality->smr_normalize( usage,
548                 ad->ad_type->sat_syntax, mr, val, normalized, ctx );
549
550         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
551                 bv = *normalized;
552
553                 normalized->bv_len = idx.bv_len + bv.bv_len;
554                 normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
555                 
556                 AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
557                 AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
558
559                 ber_memfree_x( bv.bv_val, ctx );
560         }
561
562         return rc;
563 }
564
565 /* A wrapper for value match, handles Equality matches for attributes
566  * with ordered values.
567  */
568 int
569 ordered_value_match(
570         int *match,
571         AttributeDescription *ad,
572         MatchingRule *mr,
573         unsigned flags,
574         struct berval *v1, /* stored value */
575         struct berval *v2, /* assertion */
576         const char ** text )
577 {
578         struct berval bv1, bv2;
579
580         /* X-ORDERED VALUES equality matching:
581          * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are
582          * comparing two attribute values. In this case, we want to ignore
583          * the ordering index of both values, we just want to know if their
584          * main values are equal.
585          *
586          * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing
587          * an assertion against an attribute value.
588          *    If the assertion has no index, the index of the value is ignored. 
589          *    If the assertion has only an index, the remainder of the value is
590          *      ignored.
591          *    If the assertion has index and value, both are compared.
592          */
593         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
594                 char *ptr;
595                 struct berval iv;
596
597                 bv1 = *v1;
598                 bv2 = *v2;
599                 iv = bv2;
600
601                 /* Skip past the assertion index */
602                 if ( bv2.bv_val[0] == '{' ) {
603                         ptr = strchr( bv2.bv_val, '}' ) + 1;
604                         bv2.bv_len -= ptr - bv2.bv_val;
605                         bv2.bv_val = ptr;
606                         v2 = &bv2;
607                 }
608
609                 if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) {
610                         if ( iv.bv_val[0] == '{' && bv1.bv_val[0] == '{' ) {
611                         /* compare index values first */
612                                 long l1, l2, ret;
613
614                                 l1 = strtol( bv1.bv_val+1, NULL, 0 );
615                                 l2 = strtol( iv.bv_val+1, &ptr, 0 );
616
617                                 ret = l1 - l2;
618
619                                 /* If not equal, or we're only comparing the index,
620                                  * return result now.
621                                  */
622                                 if ( ret || ptr == iv.bv_val + iv.bv_len - 1 ) {
623                                         *match = ( ret < 0 ) ? -1 : (ret > 0 );
624                                         return LDAP_SUCCESS;
625                                 }
626                         }
627                 }
628                 /* Skip past the attribute index */
629                 if ( bv1.bv_val[0] == '{' ) {
630                         ptr = strchr( bv1.bv_val, '}' ) + 1;
631                         bv1.bv_len -= ptr - bv1.bv_val;
632                         bv1.bv_val = ptr;
633                         v1 = &bv1;
634                 }
635         }
636
637         if ( !mr || !mr->smr_match ) {
638                 *match = ber_bvcmp( v1, v2 );
639                 return LDAP_SUCCESS;
640         }
641
642         return value_match( match, ad, mr, flags, v1, v2, text );
643 }
644
645 int
646 ordered_value_add(
647         Entry *e,
648         AttributeDescription *ad,
649         Attribute *a,
650         BerVarray vals,
651         BerVarray nvals
652 )
653 {
654         int i, j, k, anum, vnum;
655         BerVarray new, nnew = NULL;
656
657         /* count new vals */
658         for (i=0; !BER_BVISNULL( vals+i ); i++) ;
659         vnum = i;
660
661         if ( a ) {
662                 for (i=0; !BER_BVISNULL( a->a_vals+i ); i++) ;
663                 anum = i;
664                 ordered_value_sort( a, 0 );
665         } else {
666                 Attribute **ap;
667                 anum = 0;
668                 for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ;
669                 a = ch_calloc( 1, sizeof(Attribute) );
670                 a->a_desc = ad;
671                 *ap = a;
672         }
673
674         new = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
675         if ( a->a_nvals && a->a_nvals != a->a_vals ) {
676                 nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
677                 /* Shouldn't happen... */
678                 if ( !nvals ) nvals = vals;
679         }
680         if ( anum ) {
681                 AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval));
682                 if ( nnew )
683                         AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval));
684         }
685
686         for (i=0; i<vnum; i++) {
687                 k = -1;
688                 if ( vals[i].bv_val[0] == '{' ) {
689                         k = strtol( vals[i].bv_val+1, NULL, 0 );
690                         if ( k > anum ) k = -1;
691                 }
692                 /* No index, or index is greater than current number of
693                  * values, just tack onto the end
694                  */
695                 if ( k < 0 ) {
696                         ber_dupbv( new+anum, vals+i );
697                         if ( nnew ) ber_dupbv( nnew+anum, nvals+i );
698
699                 /* Indexed, push everything else down one and insert */
700                 } else {
701                         for (j=anum; j>k; j--) {
702                                 new[j] = new[j-1];
703                                 if ( nnew ) nnew[j] = nnew[j-1];
704                         }
705                         ber_dupbv( new+k, vals+i );
706                         if ( nnew ) ber_dupbv( nnew+k, nvals+i );
707                 }
708                 anum++;
709         }
710         BER_BVZERO( new+anum );
711         ch_free( a->a_vals );
712         a->a_vals = new;
713         if ( nnew ) {
714                 BER_BVZERO( nnew+anum );
715                 ch_free( a->a_nvals );
716                 a->a_nvals = nnew;
717         } else {
718                 a->a_nvals = a->a_vals;
719         }
720
721         ordered_value_renumber( a, anum );
722
723         return 0;
724 }