1 /* str2filter.c - parse an rfc 1588 string filter */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
14 #include <ac/socket.h>
19 static char *find_matching_paren( const char *s );
20 static Filter *str2list( const char *str, long unsigned int ftype);
21 static Filter *str2simple( const char *str);
22 static int str2subvals( const char *val, Filter *f);
25 str2filter( const char *str )
30 Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
32 if ( str == NULL || *str == '\0' ) {
36 str = freeme = ch_strdup( str );
40 if ( (end = find_matching_paren( str )) == NULL ) {
50 Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
54 f = str2list( str, LDAP_FILTER_AND );
58 Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
62 f = str2list( str, LDAP_FILTER_OR );
66 Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
70 f = str2list( str, LDAP_FILTER_NOT );
74 Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
77 f = str2simple( str );
83 default: /* assume it's a simple type=value filter */
84 Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
87 f = str2simple( str );
96 * Put a list of filters like this "(filter1)(filter2)..."
100 str2list( const char *str, unsigned long ftype )
107 Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
109 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
114 while ( *str && isspace( (unsigned char) *str ) )
119 if ( (next = find_matching_paren( str )) == NULL ) {
126 /* now we have "(filter)" with str pointing to it */
127 if ( (*fp = str2filter( str )) == NULL ) {
143 str2simple( const char *str )
147 char *value, savechar;
149 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
151 if ( (s = strchr( str, '=' )) == NULL ) {
158 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
162 f->f_choice = LDAP_FILTER_LE;
166 f->f_choice = LDAP_FILTER_GE;
170 f->f_choice = LDAP_FILTER_APPROX;
174 f->f_choice = LDAP_FILTER_EXT;
179 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
180 f->f_choice = LDAP_FILTER_EQUALITY;
181 } else if ( strcmp( value, "*" ) == 0 ) {
182 f->f_choice = LDAP_FILTER_PRESENT;
184 f->f_choice = LDAP_FILTER_SUBSTRINGS;
185 #ifndef SLAPD_SCHEMA_NOT_COMPAT
186 f->f_sub_type = ch_strdup( str );
187 if ( str2subvals( value, f ) != 0 ) {
199 #ifndef SLAPD_SCHEMA_NOT_COMPAT
200 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
201 f->f_type = ch_strdup( str );
203 f->f_avtype = ch_strdup( str );
204 f->f_avvalue.bv_val = ch_strdup( value );
205 ldap_pvt_filter_value_unescape( f->f_avvalue.bv_val );
206 f->f_avvalue.bv_len = strlen( value );
216 str2subvals( const char *in, Filter *f )
218 char *nextstar, *val, *freeme;
221 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
223 if( in == NULL ) return 0;
225 val = freeme = ch_strdup( in );
229 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
232 ldap_pvt_filter_value_unescape( val );
234 if ( gotstar == 0 ) {
235 f->f_sub_initial = ber_bvstrdup( val );
237 } else if ( nextstar == NULL ) {
238 f->f_sub_final = ber_bvstrdup( val );
241 charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
253 * find_matching_paren - return a pointer to the right paren in s matching
254 * the left paren to which *s currently points
258 find_matching_paren( const char *s )
268 else if ( *s == ')' )
271 if ( balance == 0 ) {
274 if ( *s == '\\' && ! escape )