- if( sub->sa_initial ) {
- inlen += sub->sa_initial->bv_len;
- }
- if( sub->sa_any ) {
- for(i=0; sub->sa_any[i] != NULL; i++) {
- inlen += sub->sa_any[i]->bv_len;
- }
- }
- if( sub->sa_final ) {
- inlen += sub->sa_final->bv_len;
- }
-
- if( sub->sa_initial ) {
- if( inlen > left.bv_len ) {
- match = 1;
- goto done;
- }
-
- match = strncmp( sub->sa_initial->bv_val, left.bv_val,
- sub->sa_initial->bv_len );
-
- if( match != 0 ) {
- goto done;
- }
-
- left.bv_val += sub->sa_initial->bv_len;
- left.bv_len -= sub->sa_initial->bv_len;
- inlen -= sub->sa_initial->bv_len;
- }
-
- if( sub->sa_final ) {
- if( inlen > left.bv_len ) {
- match = 1;
- goto done;
- }
-
- match = strncmp( sub->sa_final->bv_val,
- &left.bv_val[left.bv_len - sub->sa_final->bv_len],
- sub->sa_final->bv_len );
-
- if( match != 0 ) {
- goto done;
- }
-
- left.bv_len -= sub->sa_final->bv_len;
- inlen -= sub->sa_final->bv_len;
- }
-
- if( sub->sa_any ) {
- for(i=0; sub->sa_any[i]; i++) {
- ber_len_t idx;
- char *p;
-
-retry:
- if( inlen > left.bv_len ) {
- /* not enough length */
- match = 1;
- goto done;
- }
-
- if( sub->sa_any[i]->bv_len == 0 ) {
- continue;
- }
-
- p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
-
- if( p == NULL ) {
- match = 1;
- goto done;
- }
-
- idx = p - left.bv_val;
- assert( idx < left.bv_len );
-
- if( idx >= left.bv_len ) {
- /* this shouldn't happen */
- return LDAP_OTHER;
- }
-
- left.bv_val = p;
- left.bv_len -= idx;
-
- if( sub->sa_any[i]->bv_len > left.bv_len ) {
- /* not enough left */
- match = 1;
- goto done;
- }
-
- match = strncmp( left.bv_val,
- sub->sa_any[i]->bv_val,
- sub->sa_any[i]->bv_len );
-
- if( match != 0 ) {
- left.bv_val++;
- left.bv_len--;
- goto retry;
- }
-
- left.bv_val += sub->sa_any[i]->bv_len;
- left.bv_len -= sub->sa_any[i]->bv_len;
- inlen -= sub->sa_any[i]->bv_len;
- }
- }
-
-done:
-#if UTF8MATCH
- free( nav );
- if( sub != NULL ) {
- ch_free( sub->sa_final );
- ber_bvecfree( sub->sa_any );
- ch_free( sub->sa_initial );
- ch_free( sub );
- }
-#endif
- *matchp = match;
- return LDAP_SUCCESS;
-}
-
-/* Index generation function */
-int caseExactIndexer(
- slap_mask_t use,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *prefix,
- struct berval **values,
- struct berval ***keysp )
-{
- int i;
- size_t slen, mlen;
- struct berval **keys;
- HASH_CONTEXT HASHcontext;
- unsigned char HASHdigest[HASH_BYTES];
- struct berval digest;
- digest.bv_val = HASHdigest;
- digest.bv_len = sizeof(HASHdigest);
-
- /* we should have at least one value at this point */
- assert( values != NULL && values[0] != NULL );
-
- for( i=0; values[i] != NULL; i++ ) {
- /* just count them */
- }
-
- keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
-
- slen = strlen( syntax->ssyn_oid );
- mlen = strlen( mr->smr_oid );
-
- for( i=0; values[i] != NULL; i++ ) {
- struct berval *value;
-#if UTF8MATCH
- value = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_NOCASEFOLD ) );
-#else
- value = values[i];
-#endif
-
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- value->bv_val, value->bv_len );
- HASH_Final( HASHdigest, &HASHcontext );
-
-#if UTF8MATCH
- ber_bvfree( value );
-#endif
-
- keys[i] = ber_bvdup( &digest );
- }
-
- keys[i] = NULL;
- *keysp = keys;
- return LDAP_SUCCESS;
-}
-
-/* Index generation function */
-int caseExactFilter(
- slap_mask_t use,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *prefix,
- void * assertValue,
- struct berval ***keysp )
-{
- size_t slen, mlen;
- struct berval **keys;
- HASH_CONTEXT HASHcontext;
- unsigned char HASHdigest[HASH_BYTES];
- struct berval *value;
- struct berval digest;
- digest.bv_val = HASHdigest;
- digest.bv_len = sizeof(HASHdigest);
-
- slen = strlen( syntax->ssyn_oid );
- mlen = strlen( mr->smr_oid );
-
-#if UTF8MATCH
- value = ber_bvstr( UTF8normalize( ((struct berval *) assertValue)->bv_val,
- UTF8_NOCASEFOLD ) );
- /* This usually happens if filter contains bad UTF8 */
- if( value == NULL ) {
- keys = ch_malloc( sizeof( struct berval * ) );
- keys[0] = NULL;
- return LDAP_SUCCESS;
- }
-#else
- value = (struct berval *) assertValue;
-#endif
-
- keys = ch_malloc( sizeof( struct berval * ) * 2 );
-
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- value->bv_val, value->bv_len );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[0] = ber_bvdup( &digest );
- keys[1] = NULL;
-
-#if UTF8MATCH
- ber_bvfree( value );
-#endif
-
- *keysp = keys;
- return LDAP_SUCCESS;
-}
-
-/* Substrings Index generation function */
-int caseExactSubstringsIndexer(
- slap_mask_t use,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *prefix,
- struct berval **values,
- struct berval ***keysp )
-{
- ber_len_t i, nkeys;
- size_t slen, mlen;
- struct berval **keys;
-#if UTF8MATCH
- struct berval **nvalues;
-#endif
-
- HASH_CONTEXT HASHcontext;
- unsigned char HASHdigest[HASH_BYTES];
- struct berval digest;
- digest.bv_val = HASHdigest;
- digest.bv_len = sizeof(HASHdigest);
-
- /* we should have at least one value at this point */
- assert( values != NULL && values[0] != NULL );
-
- nkeys=0;
-
-#if UTF8MATCH
- /* create normalized copy of values */
- for( i=0; values[i] != NULL; i++ ) {
- /* empty */
- }
- nvalues = ch_malloc( sizeof( struct berval * ) * (i+1) );
- for( i=0; values[i] != NULL; i++ ) {
- nvalues[i] = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_NOCASEFOLD ) );
- }
- nvalues[i] = NULL;
- values = nvalues;
-#endif
-
- for( i=0; values[i] != NULL; i++ ) {
- /* count number of indices to generate */
- if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
- continue;
- }
-
- if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
- if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
- nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
- ( SLAP_INDEX_SUBSTR_MINLEN - 1);
- } else {
- nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
- }
- }
-
- if( flags & SLAP_INDEX_SUBSTR_ANY ) {
- if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
- nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
- }
- }
-
- if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
- if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
- nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
- ( SLAP_INDEX_SUBSTR_MINLEN - 1);
- } else {
- nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
- }
- }
- }
-
- if( nkeys == 0 ) {
- /* no keys to generate */
- *keysp = NULL;
- return LDAP_SUCCESS;
- }
-
- keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
-
- slen = strlen( syntax->ssyn_oid );
- mlen = strlen( mr->smr_oid );
-
- nkeys=0;
- for( i=0; values[i] != NULL; i++ ) {
- ber_len_t j,max;
- struct berval *value;
-
- if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
-
- value = values[i];
-
- if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
- ( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
- {
- char pre = SLAP_INDEX_SUBSTR_PREFIX;
- max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
-
- for( j=0; j<max; j++ ) {
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
-
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- &value->bv_val[j],
- SLAP_INDEX_SUBSTR_MAXLEN );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
- }
-
- max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
- ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
-
- for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
- char pre;
-
- if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
- pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- value->bv_val, j );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
-
- if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
- pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- &value->bv_val[value->bv_len-j], j );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
-
- }
-
- }
-
- if( nkeys > 0 ) {
- keys[nkeys] = NULL;
- *keysp = keys;
- } else {
- ch_free( keys );
- *keysp = NULL;
- }
-
-#if UTF8MATCH
- ber_bvecfree( nvalues );
-#endif
-
- return LDAP_SUCCESS;
-}
-
-int caseExactSubstringsFilter(
- slap_mask_t use,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *prefix,
- void * assertValue,
- struct berval ***keysp )
-{
- SubstringsAssertion *sa;
- char pre;
- ber_len_t nkeys = 0;
- size_t slen, mlen, klen;
- struct berval **keys;
- HASH_CONTEXT HASHcontext;
- unsigned char HASHdigest[HASH_BYTES];
- struct berval *value;
- struct berval digest;
-
-#if UTF8MATCH
- sa = UTF8SubstringsassertionNormalize( assertValue, UTF8_NOCASEFOLD );
-#else
- sa = assertValue;
-#endif
- if( sa == NULL ) {
- *keysp = NULL;
- return LDAP_SUCCESS;
- }
-
- if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
- sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
- {
- nkeys++;
- }
-
- if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
- ber_len_t i;
- for( i=0; sa->sa_any[i] != NULL; i++ ) {
- if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
- /* don't bother accounting for stepping */
- nkeys += sa->sa_any[i]->bv_len -
- ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
- }
- }
- }
-
- if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
- sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
- {
- nkeys++;
- }
-
- if( nkeys == 0 ) {
- *keysp = NULL;
- return LDAP_SUCCESS;
- }
-
- digest.bv_val = HASHdigest;
- digest.bv_len = sizeof(HASHdigest);
-
- slen = strlen( syntax->ssyn_oid );
- mlen = strlen( mr->smr_oid );
-
- keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
- nkeys = 0;
-
- if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
- sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
- {
- pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
- value = sa->sa_initial;
-
- klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
- ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
-
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- value->bv_val, klen );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
-
- if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
- ber_len_t i, j;
- pre = SLAP_INDEX_SUBSTR_PREFIX;
- klen = SLAP_INDEX_SUBSTR_MAXLEN;
-
- for( i=0; sa->sa_any[i] != NULL; i++ ) {
- if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
- continue;
- }
-
- value = sa->sa_any[i];
-
- for(j=0;
- j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
- j += SLAP_INDEX_SUBSTR_STEP )
- {
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- &value->bv_val[j], klen );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
-
- }
- }
-
- if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
- sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
- {
- pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
- value = sa->sa_final;
-
- klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
- ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
-
- HASH_Init( &HASHcontext );
- if( prefix != NULL && prefix->bv_len > 0 ) {
- HASH_Update( &HASHcontext,
- prefix->bv_val, prefix->bv_len );
- }
- HASH_Update( &HASHcontext,
- &pre, sizeof( pre ) );
- HASH_Update( &HASHcontext,
- syntax->ssyn_oid, slen );
- HASH_Update( &HASHcontext,
- mr->smr_oid, mlen );
- HASH_Update( &HASHcontext,
- &value->bv_val[value->bv_len-klen], klen );
- HASH_Final( HASHdigest, &HASHcontext );
-
- keys[nkeys++] = ber_bvdup( &digest );
- }
-
- if( nkeys > 0 ) {
- keys[nkeys] = NULL;
- *keysp = keys;
- } else {
- ch_free( keys );
- *keysp = NULL;
- }
-#if UTF8MATCH
- ch_free( sa->sa_final );
- ber_bvecfree( sa->sa_any );
- ch_free( sa->sa_initial );
- ch_free( sa );
-#endif
-
- return LDAP_SUCCESS;
-}
-
-static int
-caseIgnoreMatch(
- int *matchp,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *value,
- void *assertedValue )
-{
-#if UTF8MATCH
- *matchp = UTF8normcmp( value->bv_val,
- ((struct berval *) assertedValue)->bv_val,
- UTF8_CASEFOLD );
-#else
- int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
-
- if( match == 0 ) {
- match = strncasecmp( value->bv_val,
- ((struct berval *) assertedValue)->bv_val,
- value->bv_len );
- }
-
- *matchp = match;
-#endif
- return LDAP_SUCCESS;
-}
-
-static int
-caseIgnoreSubstringsMatch(
- int *matchp,
- slap_mask_t flags,
- Syntax *syntax,
- MatchingRule *mr,
- struct berval *value,
- void *assertedValue )
-{
- int match = 0;
- SubstringsAssertion *sub;
- struct berval left;
- int i;
- ber_len_t inlen=0;
-#if UTF8MATCH
- char *nav;
-#endif
-
-#if UTF8MATCH
- nav = UTF8normalize( value->bv_val, UTF8_CASEFOLD );
- if( nav == NULL ) {
- match = 1;
- goto done;
- }
- left.bv_val = nav;
- left.bv_len = strlen( nav );
- sub = UTF8SubstringsassertionNormalize( assertedValue, UTF8_CASEFOLD );
-#else
- left = *value;
- sub = assertedValue;
-#endif
- if( sub == NULL ) {
- match = -1;
- goto done;
- }
-
- /* Add up asserted input length */
- if( sub->sa_initial ) {
- inlen += sub->sa_initial->bv_len;