1 /* str2filter.c - parse an rfc 1588 string filter */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
13 #include <ac/socket.h>
18 static char *find_matching_paren(char *s);
19 static Filter *str2list(char *str, long unsigned int ftype);
20 static Filter *str2simple(char *str);
21 static int str2subvals(char *val, Filter *f);
24 str2filter( char *str )
29 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
31 if ( str == NULL || *str == '\0' ) {
35 str = freeme = ch_strdup( str );
39 if ( (end = find_matching_paren( str )) == NULL ) {
49 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
53 f = str2list( str, LDAP_FILTER_AND );
57 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
61 f = str2list( str, LDAP_FILTER_OR );
65 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
69 f = str2list( str, LDAP_FILTER_NOT );
73 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
76 f = str2simple( str );
82 default: /* assume it's a simple type=value filter */
83 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
86 f = str2simple( str );
95 * Put a list of filters like this "(filter1)(filter2)..."
99 str2list( char *str, unsigned long ftype )
106 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
108 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
113 while ( *str && isspace( (unsigned char) *str ) )
118 if ( (next = find_matching_paren( str )) == NULL ) {
125 /* now we have "(filter)" with str pointing to it */
126 if ( (*fp = str2filter( str )) == NULL ) {
142 str2simple( char *str )
146 char *value, savechar;
148 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
150 if ( (s = strchr( str, '=' )) == NULL ) {
157 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
161 f->f_choice = LDAP_FILTER_LE;
165 f->f_choice = LDAP_FILTER_GE;
169 f->f_choice = LDAP_FILTER_APPROX;
173 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
174 f->f_choice = LDAP_FILTER_EQUALITY;
175 } else if ( strcmp( value, "*" ) == 0 ) {
176 f->f_choice = LDAP_FILTER_PRESENT;
178 f->f_choice = LDAP_FILTER_SUBSTRINGS;
179 f->f_sub_type = ch_strdup( str );
180 if ( str2subvals( value, f ) != 0 ) {
191 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
192 f->f_type = ch_strdup( str );
194 f->f_avtype = ch_strdup( str );
195 f->f_avvalue.bv_val = ch_strdup( value );
196 ldap_pvt_filter_value_unescape( f->f_avvalue.bv_val );
197 f->f_avvalue.bv_len = strlen( value );
206 str2subvals( char *val, Filter *f )
208 char *nextstar, *freeme;
211 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
213 val = freeme = ch_strdup( val );
215 while ( val != NULL && *val ) {
216 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
219 ldap_pvt_filter_value_unescape( val );
220 if ( gotstar == 0 ) {
221 f->f_sub_initial = ch_strdup( val );
222 } else if ( nextstar == NULL ) {
223 f->f_sub_final = ch_strdup( val );
225 charray_add( &f->f_sub_any, val );
237 * find_matching_paren - return a pointer to the right paren in s matching
238 * the left paren to which *s currently points
242 find_matching_paren( char *s )
252 else if ( *s == ')' )
255 if ( balance == 0 ) {
258 if ( *s == '\\' && ! escape )