Back to index

openldap  2.4.31
value.c
Go to the documentation of this file.
00001 /* value.c - routines for dealing with values */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /*
00017  * Copyright (c) 1995 Regents of the University of Michigan.
00018  * All rights reserved.
00019  *
00020  * Redistribution and use in source and binary forms are permitted
00021  * provided that this notice is preserved and that due credit is given
00022  * to the University of Michigan at Ann Arbor. The name of the University
00023  * may not be used to endorse or promote products derived from this
00024  * software without specific prior written permission. This software
00025  * is provided ``as is'' without express or implied warranty.
00026  */
00027 
00028 #include "portable.h"
00029 
00030 #include <stdio.h>
00031 
00032 #include <ac/ctype.h>
00033 #include <ac/socket.h>
00034 #include <ac/string.h>
00035 #include <ac/time.h>
00036 
00037 #include <sys/stat.h>
00038 
00039 #include "slap.h"
00040 
00041 int
00042 value_add( 
00043     BerVarray *vals,
00044     BerVarray addvals )
00045 {
00046        int           n, nn = 0;
00047        BerVarray     v2;
00048 
00049        if ( addvals != NULL ) {
00050               for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ )
00051                      ;      /* NULL */
00052        }
00053 
00054        if ( *vals == NULL ) {
00055               *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
00056                   * sizeof(struct berval) );
00057               if( *vals == NULL ) {
00058                      Debug(LDAP_DEBUG_TRACE,
00059                     "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
00060                      return LBER_ERROR_MEMORY;
00061               }
00062               n = 0;
00063 
00064        } else {
00065               for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
00066                      ;      /* Empty */
00067               }
00068               *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
00069                   (n + nn + 1) * sizeof(struct berval) );
00070               if( *vals == NULL ) {
00071                      Debug(LDAP_DEBUG_TRACE,
00072                     "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
00073                      return LBER_ERROR_MEMORY;
00074               }
00075        }
00076 
00077        v2 = &(*vals)[n];
00078        for ( n = 0 ; n < nn; v2++, addvals++ ) {
00079               ber_dupbv( v2, addvals );
00080               if ( BER_BVISNULL( v2 ) ) break;
00081        }
00082        BER_BVZERO( v2 );
00083 
00084        return LDAP_SUCCESS;
00085 }
00086 
00087 int
00088 value_add_one( 
00089     BerVarray        *vals,
00090     struct berval    *addval )
00091 {
00092        int           n;
00093        BerVarray     v2;
00094 
00095        if ( *vals == NULL ) {
00096               *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
00097               if( *vals == NULL ) {
00098                      Debug(LDAP_DEBUG_TRACE,
00099                     "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
00100                      return LBER_ERROR_MEMORY;
00101               }
00102               n = 0;
00103 
00104        } else {
00105               for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
00106                      ;      /* Empty */
00107               }
00108               *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
00109                   (n + 2) * sizeof(struct berval) );
00110               if( *vals == NULL ) {
00111                      Debug(LDAP_DEBUG_TRACE,
00112                     "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
00113                      return LBER_ERROR_MEMORY;
00114               }
00115        }
00116 
00117        v2 = &(*vals)[n];
00118        ber_dupbv(v2, addval);
00119 
00120        v2++;
00121        BER_BVZERO( v2 );
00122 
00123        return LDAP_SUCCESS;
00124 }
00125 
00126 int asserted_value_validate_normalize( 
00127        AttributeDescription *ad,
00128        MatchingRule *mr,
00129        unsigned usage,
00130        struct berval *in,
00131        struct berval *out,
00132        const char ** text,
00133        void *ctx )
00134 {
00135        int rc;
00136        struct berval pval;
00137        pval.bv_val = NULL;
00138 
00139        /* we expect the value to be in the assertion syntax */
00140        assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
00141 
00142        if( mr == NULL ) {
00143               *text = "inappropriate matching request";
00144               return LDAP_INAPPROPRIATE_MATCHING;
00145        }
00146 
00147        if( !mr->smr_match ) {
00148               *text = "requested matching rule not supported";
00149               return LDAP_INAPPROPRIATE_MATCHING;
00150        }
00151 
00152        if( mr->smr_syntax->ssyn_pretty ) {
00153               rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
00154               in = &pval;
00155 
00156        } else if ( mr->smr_syntax->ssyn_validate ) {
00157               rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
00158 
00159        } else {
00160               *text = "inappropriate matching request";
00161               return LDAP_INAPPROPRIATE_MATCHING;
00162        }
00163 
00164        if( rc != LDAP_SUCCESS ) {
00165               *text = "value does not conform to assertion syntax";
00166               return LDAP_INVALID_SYNTAX;
00167        }
00168 
00169        if( mr->smr_normalize ) {
00170               rc = (mr->smr_normalize)(
00171                      usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
00172                      ad ? ad->ad_type->sat_syntax : NULL,
00173                      mr, in, out, ctx );
00174 
00175               if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
00176 
00177               if( rc != LDAP_SUCCESS ) {
00178                      *text = "unable to normalize value for matching";
00179                      return LDAP_INVALID_SYNTAX;
00180               }
00181 
00182        } else if ( pval.bv_val != NULL ) {
00183               *out = pval;
00184 
00185        } else {
00186               ber_dupbv_x( out, in, ctx );
00187        }
00188 
00189        return LDAP_SUCCESS;
00190 }
00191 
00192 int
00193 value_match(
00194        int *match,
00195        AttributeDescription *ad,
00196        MatchingRule *mr,
00197        unsigned flags,
00198        struct berval *v1, /* stored value */
00199        void *v2, /* assertion */
00200        const char ** text )
00201 {
00202        int rc;
00203 
00204        assert( mr != NULL );
00205 
00206        if( !mr->smr_match ) {
00207               return LDAP_INAPPROPRIATE_MATCHING;
00208        }
00209 
00210        rc = (mr->smr_match)( match, flags,
00211               ad->ad_type->sat_syntax, mr, v1, v2 );
00212        
00213        return rc;
00214 }
00215 
00216 int value_find_ex(
00217        AttributeDescription *ad,
00218        unsigned flags,
00219        BerVarray vals,
00220        struct berval *val,
00221        void *ctx )
00222 {
00223        int    i;
00224        int rc;
00225        struct berval nval = BER_BVNULL;
00226        MatchingRule *mr = ad->ad_type->sat_equality;
00227 
00228        if( mr == NULL || !mr->smr_match ) {
00229               return LDAP_INAPPROPRIATE_MATCHING;
00230        }
00231 
00232        assert( SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ) != 0 );
00233 
00234        if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
00235               mr->smr_normalize )
00236        {
00237               rc = (mr->smr_normalize)(
00238                      flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
00239                      ad->ad_type->sat_syntax,
00240                      mr, val, &nval, ctx );
00241 
00242               if( rc != LDAP_SUCCESS ) {
00243                      return LDAP_INVALID_SYNTAX;
00244               }
00245        }
00246 
00247        for ( i = 0; vals[i].bv_val != NULL; i++ ) {
00248               int match;
00249               const char *text;
00250 
00251               rc = value_match( &match, ad, mr, flags,
00252                      &vals[i], nval.bv_val == NULL ? val : &nval, &text );
00253 
00254               if( rc == LDAP_SUCCESS && match == 0 ) {
00255                      slap_sl_free( nval.bv_val, ctx );
00256                      return rc;
00257               }
00258        }
00259 
00260        slap_sl_free( nval.bv_val, ctx );
00261        return LDAP_NO_SUCH_ATTRIBUTE;
00262 }
00263 
00264 /* assign new indexes to an attribute's ordered values */
00265 void
00266 ordered_value_renumber( Attribute *a )
00267 {
00268        char *ptr, ibuf[64]; /* many digits */
00269        struct berval ibv, tmp, vtmp;
00270        unsigned i;
00271 
00272        ibv.bv_val = ibuf;
00273 
00274        for (i=0; i<a->a_numvals; i++) {
00275               ibv.bv_len = sprintf(ibv.bv_val, "{%u}", i);
00276               vtmp = a->a_vals[i];
00277               if ( vtmp.bv_val[0] == '{' ) {
00278                      ptr = ber_bvchr(&vtmp, '}');
00279                      assert( ptr != NULL );
00280                      ++ptr;
00281                      vtmp.bv_len -= ptr - vtmp.bv_val;
00282                      vtmp.bv_val = ptr;
00283               }
00284               tmp.bv_len = ibv.bv_len + vtmp.bv_len;
00285               tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
00286               strcpy( tmp.bv_val, ibv.bv_val );
00287               AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
00288               tmp.bv_val[tmp.bv_len] = '\0';
00289               ch_free( a->a_vals[i].bv_val );
00290               a->a_vals[i] = tmp;
00291 
00292               if ( a->a_nvals && a->a_nvals != a->a_vals ) {
00293                      vtmp = a->a_nvals[i];
00294                      if ( vtmp.bv_val[0] == '{' ) {
00295                             ptr = ber_bvchr(&vtmp, '}');
00296                             assert( ptr != NULL );
00297                             ++ptr;
00298                             vtmp.bv_len -= ptr - vtmp.bv_val;
00299                             vtmp.bv_val = ptr;
00300                      }
00301                      tmp.bv_len = ibv.bv_len + vtmp.bv_len;
00302                      tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
00303                      strcpy( tmp.bv_val, ibv.bv_val );
00304                      AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
00305                      tmp.bv_val[tmp.bv_len] = '\0';
00306                      ch_free( a->a_nvals[i].bv_val );
00307                      a->a_nvals[i] = tmp;
00308               }
00309        }
00310 }
00311 
00312 /* Sort the values in an X-ORDERED VALUES attribute.
00313  * If the values have no index, index them in their given order.
00314  * If the values have indexes, sort them.
00315  * If some are indexed and some are not, return Error.
00316  */
00317 int
00318 ordered_value_sort( Attribute *a, int do_renumber )
00319 {
00320        int i, vals;
00321        int index = 0, noindex = 0, renumber = 0, gotnvals = 0;
00322        struct berval tmp;
00323 
00324        if ( a->a_nvals && a->a_nvals != a->a_vals )
00325               gotnvals = 1;
00326 
00327        /* count attrs, look for index */
00328        for (i=0; a->a_vals[i].bv_val; i++) {
00329               if ( a->a_vals[i].bv_val[0] == '{' ) {
00330                      char *ptr;
00331                      index = 1;
00332                      ptr = ber_bvchr( &a->a_vals[i], '}' );
00333                      if ( !ptr )
00334                             return LDAP_INVALID_SYNTAX;
00335                      if ( noindex )
00336                             return LDAP_INVALID_SYNTAX;
00337               } else {
00338                      noindex = 1;
00339                      if ( index )
00340                             return LDAP_INVALID_SYNTAX;
00341               }
00342        }
00343        vals = i;
00344 
00345        /* If values have indexes, sort the values */
00346        if ( index ) {
00347               int *indexes, j, idx;
00348               struct berval ntmp;
00349 
00350 #if 0
00351               /* Strip index from normalized values */
00352               if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
00353                      a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
00354                      BER_BVZERO(a->a_nvals+vals);
00355                      for ( i=0; i<vals; i++ ) {
00356                             char *ptr = ber_bvchr(&a->a_vals[i], '}') + 1;
00357                             a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
00358                                    (ptr - a->a_vals[i].bv_val);
00359                             a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
00360                             strcpy(a->a_nvals[i].bv_val, ptr );
00361                      }
00362               } else {
00363                      for ( i=0; i<vals; i++ ) {
00364                             char *ptr = ber_bvchr(&a->a_nvals[i], '}') + 1;
00365                             a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
00366                             strcpy(a->a_nvals[i].bv_val, ptr);
00367                      }
00368               }
00369 #endif
00370                             
00371               indexes = ch_malloc( vals * sizeof(int) );
00372               for ( i=0; i<vals; i++) {
00373                      char *ptr;
00374                      indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
00375                      if ( *ptr != '}' ) {
00376                             ch_free( indexes );
00377                             return LDAP_INVALID_SYNTAX;
00378                      }
00379               }
00380 
00381               /* Insertion sort */
00382               for ( i=1; i<vals; i++ ) {
00383                      idx = indexes[i];
00384                      tmp = a->a_vals[i];
00385                      if ( gotnvals ) ntmp = a->a_nvals[i];
00386                      j = i;
00387                      while ((j > 0) && (indexes[j-1] > idx)) {
00388                             indexes[j] = indexes[j-1];
00389                             a->a_vals[j] = a->a_vals[j-1];
00390                             if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
00391                             j--;
00392                      }
00393                      indexes[j] = idx;
00394                      a->a_vals[j] = tmp;
00395                      if ( gotnvals ) a->a_nvals[j] = ntmp;
00396               }
00397 
00398               /* If range is not contiguous, must renumber */
00399               if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
00400                      renumber = 1;
00401               }
00402               ch_free( indexes );
00403        } else {
00404               renumber = 1;
00405        }
00406 
00407        if ( do_renumber && renumber )
00408               ordered_value_renumber( a );
00409 
00410        return 0;
00411 }
00412 
00413 /*
00414  * wrapper for validate function
00415  * uses the validate function of the syntax after removing
00416  * the index, if allowed and present
00417  */
00418 int
00419 ordered_value_validate(
00420        AttributeDescription *ad,
00421        struct berval *in,
00422        int mop )
00423 {
00424        struct berval bv = *in;
00425 
00426        assert( ad->ad_type->sat_syntax != NULL );
00427        assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
00428 
00429        if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
00430 
00431               /* Skip past the assertion index */
00432               if ( bv.bv_val[0] == '{' ) {
00433                      char          *ptr;
00434 
00435                      ptr = ber_bvchr( &bv, '}' );
00436                      if ( ptr != NULL ) {
00437                             struct berval ns;
00438 
00439                             ns.bv_val = bv.bv_val + 1;
00440                             ns.bv_len = ptr - ns.bv_val;
00441 
00442                             if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
00443                                    ptr++;
00444                                    bv.bv_len -= ptr - bv.bv_val;
00445                                    bv.bv_val = ptr;
00446                                    in = &bv;
00447                                    /* If deleting by index, just succeed */
00448                                    if ( mop == LDAP_MOD_DELETE && BER_BVISEMPTY( &bv ) ) {
00449                                           return LDAP_SUCCESS;
00450                                    }
00451                             }
00452                      }
00453               }
00454        }
00455 
00456        return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
00457 }
00458 
00459 /*
00460  * wrapper for pretty function
00461  * uses the pretty function of the syntax after removing
00462  * the index, if allowed and present; in case, it's prepended
00463  * to the pretty value
00464  */
00465 int
00466 ordered_value_pretty(
00467        AttributeDescription *ad,
00468        struct berval *val,
00469        struct berval *out,
00470        void *ctx )
00471 {
00472        struct berval bv,
00473                      idx = BER_BVNULL;
00474        int           rc;
00475 
00476        assert( ad->ad_type->sat_syntax != NULL );
00477        assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
00478        assert( val != NULL );
00479        assert( out != NULL );
00480 
00481        bv = *val;
00482 
00483        if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
00484 
00485               /* Skip past the assertion index */
00486               if ( bv.bv_val[0] == '{' ) {
00487                      char   *ptr;
00488 
00489                      ptr = ber_bvchr( &bv, '}' );
00490                      if ( ptr != NULL ) {
00491                             struct berval ns;
00492 
00493                             ns.bv_val = bv.bv_val + 1;
00494                             ns.bv_len = ptr - ns.bv_val;
00495 
00496                             if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
00497                                    ptr++;
00498 
00499                                    idx = bv;
00500                                    idx.bv_len = ptr - bv.bv_val;
00501 
00502                                    bv.bv_len -= idx.bv_len;
00503                                    bv.bv_val = ptr;
00504 
00505                                    val = &bv;
00506                             }
00507                      }
00508               }
00509        }
00510 
00511        rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
00512 
00513        if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
00514               bv = *out;
00515 
00516               out->bv_len = idx.bv_len + bv.bv_len;
00517               out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
00518               
00519               AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
00520               AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
00521 
00522               ber_memfree_x( bv.bv_val, ctx );
00523        }
00524 
00525        return rc;
00526 }
00527 
00528 /*
00529  * wrapper for normalize function
00530  * uses the normalize function of the attribute description equality rule
00531  * after removing the index, if allowed and present; in case, it's
00532  * prepended to the value
00533  */
00534 int
00535 ordered_value_normalize(
00536        slap_mask_t usage,
00537        AttributeDescription *ad,
00538        MatchingRule *mr,
00539        struct berval *val,
00540        struct berval *normalized,
00541        void *ctx )
00542 {
00543        struct berval bv,
00544                      idx = BER_BVNULL;
00545        int           rc;
00546 
00547        assert( ad->ad_type->sat_equality != NULL );
00548        assert( ad->ad_type->sat_equality->smr_normalize != NULL );
00549        assert( val != NULL );
00550        assert( normalized != NULL );
00551 
00552        bv = *val;
00553 
00554        if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
00555 
00556               /* Skip past the assertion index */
00557               if ( bv.bv_val[ 0 ] == '{' ) {
00558                      char   *ptr;
00559 
00560                      ptr = ber_bvchr( &bv, '}' );
00561                      if ( ptr != NULL ) {
00562                             struct berval ns;
00563 
00564                             ns.bv_val = bv.bv_val + 1;
00565                             ns.bv_len = ptr - ns.bv_val;
00566 
00567                             if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
00568                                    ptr++;
00569 
00570                                    idx = bv;
00571                                    idx.bv_len = ptr - bv.bv_val;
00572 
00573                                    bv.bv_len -= idx.bv_len;
00574                                    bv.bv_val = ptr;
00575 
00576                                    /* validator will already prevent this for Adds */
00577                                    if ( BER_BVISEMPTY( &bv )) {
00578                                           ber_dupbv_x( normalized, &idx, ctx );
00579                                           return LDAP_SUCCESS;
00580                                    }
00581                                    val = &bv;
00582                             }
00583                      }
00584               }
00585        }
00586 
00587        rc = ad->ad_type->sat_equality->smr_normalize( usage,
00588               ad->ad_type->sat_syntax, mr, val, normalized, ctx );
00589 
00590        if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
00591               bv = *normalized;
00592 
00593               normalized->bv_len = idx.bv_len + bv.bv_len;
00594               normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
00595               
00596               AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
00597               AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
00598 
00599               ber_memfree_x( bv.bv_val, ctx );
00600        }
00601 
00602        return rc;
00603 }
00604 
00605 /* A wrapper for value match, handles Equality matches for attributes
00606  * with ordered values.
00607  */
00608 int
00609 ordered_value_match(
00610        int *match,
00611        AttributeDescription *ad,
00612        MatchingRule *mr,
00613        unsigned flags,
00614        struct berval *v1, /* stored value */
00615        struct berval *v2, /* assertion */
00616        const char ** text )
00617 {
00618        struct berval bv1, bv2;
00619 
00620        /* X-ORDERED VALUES equality matching:
00621         * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are
00622         * comparing two attribute values. In this case, we want to ignore
00623         * the ordering index of both values, we just want to know if their
00624         * main values are equal.
00625         *
00626         * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing
00627         * an assertion against an attribute value.
00628         *    If the assertion has no index, the index of the value is ignored. 
00629         *    If the assertion has only an index, the remainder of the value is
00630         *      ignored.
00631         *    If the assertion has index and value, both are compared.
00632         */
00633        if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
00634               char *ptr;
00635               struct berval ns1 = BER_BVNULL, ns2 = BER_BVNULL;
00636 
00637               bv1 = *v1;
00638               bv2 = *v2;
00639 
00640               /* Skip past the assertion index */
00641               if ( bv2.bv_val[0] == '{' ) {
00642                      ptr = ber_bvchr( &bv2, '}' );
00643                      if ( ptr != NULL ) {
00644                             ns2.bv_val = bv2.bv_val + 1;
00645                             ns2.bv_len = ptr - ns2.bv_val;
00646 
00647                             if ( numericStringValidate( NULL, &ns2 ) == LDAP_SUCCESS ) {
00648                                    ptr++;
00649                                    bv2.bv_len -= ptr - bv2.bv_val;
00650                                    bv2.bv_val = ptr;
00651                                    v2 = &bv2;
00652                             }
00653                      }
00654               }
00655 
00656               /* Skip past the attribute index */
00657               if ( bv1.bv_val[0] == '{' ) {
00658                      ptr = ber_bvchr( &bv1, '}' );
00659                      if ( ptr != NULL ) {
00660                             ns1.bv_val = bv1.bv_val + 1;
00661                             ns1.bv_len = ptr - ns1.bv_val;
00662 
00663                             if ( numericStringValidate( NULL, &ns1 ) == LDAP_SUCCESS ) {
00664                                    ptr++;
00665                                    bv1.bv_len -= ptr - bv1.bv_val;
00666                                    bv1.bv_val = ptr;
00667                                    v1 = &bv1;
00668                             }
00669                      }
00670               }
00671 
00672               if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) {
00673                      if ( !BER_BVISNULL( &ns2 ) && !BER_BVISNULL( &ns1 ) ) {
00674                             /* compare index values first */
00675                             (void)octetStringOrderingMatch( match, 0, NULL, NULL, &ns1, &ns2 );
00676 
00677                             /* If not equal, or we're only comparing the index,
00678                              * return result now.
00679                              */
00680                             if ( *match != 0 || BER_BVISEMPTY( &bv2 ) ) {
00681                                    return LDAP_SUCCESS;
00682                             }
00683                      }
00684               }
00685 
00686        }
00687 
00688        if ( !mr || !mr->smr_match ) {
00689               *match = ber_bvcmp( v1, v2 );
00690               return LDAP_SUCCESS;
00691        }
00692 
00693        return value_match( match, ad, mr, flags, v1, v2, text );
00694 }
00695 
00696 int
00697 ordered_value_add(
00698        Entry *e,
00699        AttributeDescription *ad,
00700        Attribute *a,
00701        BerVarray vals,
00702        BerVarray nvals
00703 )
00704 {
00705        int i, j, k, anum, vnum;
00706        BerVarray new, nnew = NULL;
00707 
00708        /* count new vals */
00709        for (i=0; !BER_BVISNULL( vals+i ); i++) ;
00710        vnum = i;
00711 
00712        if ( a ) {
00713               ordered_value_sort( a, 0 );
00714        } else {
00715               Attribute **ap;
00716               for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ;
00717               a = attr_alloc( ad );
00718               *ap = a;
00719        }
00720        anum = a->a_numvals;
00721 
00722        new = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
00723 
00724        /* sanity check: if normalized modifications come in, either
00725         * no values are present or normalized existing values differ
00726         * from non-normalized; if no normalized modifications come in,
00727         * either no values are present or normalized existing values
00728         * don't differ from non-normalized */
00729        if ( nvals != NULL ) {
00730               assert( nvals != vals );
00731               assert( a->a_nvals == NULL || a->a_nvals != a->a_vals );
00732 
00733        } else {
00734               assert( a->a_nvals == NULL || a->a_nvals == a->a_vals );
00735        }
00736 
00737        if ( ( a->a_nvals && a->a_nvals != a->a_vals ) || nvals != NULL ) {
00738               nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
00739               /* Shouldn't happen... */
00740               if ( !nvals ) nvals = vals;
00741        }
00742        if ( anum ) {
00743               AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval));
00744               if ( nnew && a->a_nvals )
00745                      AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval));
00746        }
00747 
00748        for (i=0; i<vnum; i++) {
00749               char   *next;
00750 
00751               k = -1;
00752               if ( vals[i].bv_val[0] == '{' ) {
00753                      /* FIXME: strtol() could go past end... */
00754                      k = strtol( vals[i].bv_val + 1, &next, 0 );
00755                      if ( next == vals[i].bv_val + 1 ||
00756                             next[ 0 ] != '}' ||
00757                             (ber_len_t) (next - vals[i].bv_val) > vals[i].bv_len )
00758                      {
00759                             ch_free( nnew );
00760                             ch_free( new );
00761                             return -1;
00762                      }
00763                      if ( k > anum ) k = -1;
00764               }
00765               /* No index, or index is greater than current number of
00766                * values, just tack onto the end
00767                */
00768               if ( k < 0 ) {
00769                      ber_dupbv( new+anum, vals+i );
00770                      if ( nnew ) ber_dupbv( nnew+anum, nvals+i );
00771 
00772               /* Indexed, push everything else down one and insert */
00773               } else {
00774                      for (j=anum; j>k; j--) {
00775                             new[j] = new[j-1];
00776                             if ( nnew ) nnew[j] = nnew[j-1];
00777                      }
00778                      ber_dupbv( new+k, vals+i );
00779                      if ( nnew ) ber_dupbv( nnew+k, nvals+i );
00780               }
00781               anum++;
00782        }
00783        BER_BVZERO( new+anum );
00784        ch_free( a->a_vals );
00785        a->a_vals = new;
00786        if ( nnew ) {
00787               BER_BVZERO( nnew+anum );
00788               ch_free( a->a_nvals );
00789               a->a_nvals = nnew;
00790        } else {
00791               a->a_nvals = a->a_vals;
00792        }
00793 
00794        a->a_numvals = anum;
00795        ordered_value_renumber( a );
00796 
00797        return 0;
00798 }