1 /* filterindex.c - generate the list of candidate entries from a filter */
6 #include <sys/socket.h>
10 extern char *first_word();
11 extern char *next_word();
12 extern char *phonetic();
13 extern IDList *index_read();
14 extern IDList *idl_intersection();
15 extern IDList *idl_union();
16 extern IDList *idl_notin();
17 extern IDList *idl_allids();
19 static IDList *ava_candidates();
20 static IDList *presence_candidates();
21 static IDList *approx_candidates();
22 static IDList *list_candidates();
23 static IDList *substring_candidates();
24 static IDList *substring_comp_candidates();
27 * test_filter - test a filter against a single entry.
28 * returns 0 filter matched
29 * -1 filter did not match
30 * >0 an ldap error code
41 Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
44 switch ( f->f_choice ) {
45 case LDAP_FILTER_EQUALITY:
46 Debug( LDAP_DEBUG_FILTER, "\tEQUALITY\n", 0, 0, 0 );
47 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_EQUALITY );
50 case LDAP_FILTER_SUBSTRINGS:
51 Debug( LDAP_DEBUG_FILTER, "\tSUBSTRINGS\n", 0, 0, 0 );
52 result = substring_candidates( be, f );
56 Debug( LDAP_DEBUG_FILTER, "\tGE\n", 0, 0, 0 );
57 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_GE );
61 Debug( LDAP_DEBUG_FILTER, "\tLE\n", 0, 0, 0 );
62 result = ava_candidates( be, &f->f_ava, LDAP_FILTER_LE );
65 case LDAP_FILTER_PRESENT:
66 Debug( LDAP_DEBUG_FILTER, "\tPRESENT\n", 0, 0, 0 );
67 result = presence_candidates( be, f->f_type );
70 case LDAP_FILTER_APPROX:
71 Debug( LDAP_DEBUG_FILTER, "\tAPPROX\n", 0, 0, 0 );
72 result = approx_candidates( be, &f->f_ava );
76 Debug( LDAP_DEBUG_FILTER, "\tAND\n", 0, 0, 0 );
77 result = list_candidates( be, f->f_and, LDAP_FILTER_AND );
81 Debug( LDAP_DEBUG_FILTER, "\tOR\n", 0, 0, 0 );
82 result = list_candidates( be, f->f_or, LDAP_FILTER_OR );
86 Debug( LDAP_DEBUG_FILTER, "\tNOT\n", 0, 0, 0 );
87 result = idl_notin( be, idl_allids( be ), filter_candidates( be,
92 Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %d\n",
93 result ? result->b_nids : 0, 0, 0 );
106 Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
109 case LDAP_FILTER_EQUALITY:
110 idl = index_read( be, ava->ava_type, INDEX_EQUALITY,
111 ava->ava_value.bv_val );
115 idl = idl_allids( be );
119 idl = idl_allids( be );
123 Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %d\n",
124 idl ? idl->b_nids : 0, 0, 0 );
136 Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
138 idl = index_read( be, type, 0, "*" );
140 Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %d\n",
141 idl ? idl->b_nids : 0, 0, 0 );
154 Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
157 for ( w = first_word( ava->ava_value.bv_val ); w != NULL;
158 w = next_word( w ) ) {
160 if ( (tmp = index_read( be, ava->ava_type, INDEX_APPROX, c ))
164 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates NULL\n",
173 idl = idl_intersection( be, idl, tmp );
177 Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %d\n",
178 idl ? idl->b_nids : 0, 0, 0 );
189 IDList *idl, *tmp, *tmp2;
192 Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
195 for ( f = flist; f != NULL; f = f->f_next ) {
196 if ( (tmp = filter_candidates( be, f )) == NULL &&
197 ftype == LDAP_FILTER_AND ) {
198 Debug( LDAP_DEBUG_TRACE,
199 "<= list_candidates NULL\n", 0, 0, 0 );
207 } else if ( ftype == LDAP_FILTER_AND ) {
208 idl = idl_intersection( be, idl, tmp );
212 idl = idl_union( be, idl, tmp );
218 Debug( LDAP_DEBUG_TRACE, "<= list_candidates %d\n",
219 idl ? idl->b_nids : 0, 0, 0 );
224 substring_candidates(
230 IDList *idl, *tmp, *tmp2;
232 Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
237 if ( f->f_sub_initial != NULL ) {
238 if ( (int) strlen( f->f_sub_initial ) < SUBLEN - 1 ) {
239 idl = idl_allids( be );
240 } else if ( (idl = substring_comp_candidates( be, f->f_sub_type,
241 f->f_sub_initial, '^' )) == NULL ) {
247 if ( f->f_sub_final != NULL ) {
248 if ( (int) strlen( f->f_sub_final ) < SUBLEN - 1 ) {
249 tmp = idl_allids( be );
250 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
251 f->f_sub_final, '$' )) == NULL ) {
260 idl = idl_intersection( be, idl, tmp );
266 for ( i = 0; f->f_sub_any != NULL && f->f_sub_any[i] != NULL; i++ ) {
267 if ( (int) strlen( f->f_sub_any[i] ) < SUBLEN ) {
268 tmp = idl_allids( be );
269 } else if ( (tmp = substring_comp_candidates( be, f->f_sub_type,
270 f->f_sub_any[i], 0 )) == NULL ) {
279 idl = idl_intersection( be, idl, tmp );
285 Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %d\n",
286 idl ? idl->b_nids : 0, 0, 0 );
291 substring_comp_candidates(
299 IDList *idl, *tmp, *tmp2;
301 char buf[SUBLEN + 1];
303 Debug( LDAP_DEBUG_TRACE, "=> substring_comp_candidates\n", 0, 0, 0 );
308 /* prepend ^ for initial substring */
309 if ( prepost == '^' ) {
311 for ( i = 0; i < SUBLEN - 1; i++ ) {
316 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
319 } else if ( prepost == '$' ) {
320 p = val + len - SUBLEN + 1;
321 for ( i = 0; i < SUBLEN - 1; i++ ) {
324 buf[SUBLEN - 1] = '$';
327 if ( (idl = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
332 for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
333 for ( i = 0; i < SUBLEN; i++ ) {
338 if ( (tmp = index_read( be, type, INDEX_SUB, buf )) == NULL ) {
347 idl = idl_intersection( be, idl, tmp );
353 Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %d\n",
354 idl ? idl->b_nids : 0, 0, 0 );