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(char *s);
20 static Filter *str2list(char *str, long unsigned int ftype);
21 static Filter *str2simple(char *str);
22 static int str2subvals(char *val, Filter *f);
25 str2filter( 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( 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( 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 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
175 f->f_choice = LDAP_FILTER_EQUALITY;
176 } else if ( strcmp( value, "*" ) == 0 ) {
177 f->f_choice = LDAP_FILTER_PRESENT;
179 f->f_choice = LDAP_FILTER_SUBSTRINGS;
180 f->f_sub_type = ch_strdup( str );
181 if ( str2subvals( value, f ) != 0 ) {
192 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
193 f->f_type = ch_strdup( str );
195 f->f_avtype = ch_strdup( str );
196 f->f_avvalue.bv_val = ch_strdup( value );
197 ldap_pvt_filter_value_unescape( f->f_avvalue.bv_val );
198 f->f_avvalue.bv_len = strlen( value );
207 str2subvals( char *val, Filter *f )
209 char *nextstar, *freeme;
212 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
214 val = freeme = ch_strdup( val );
216 while ( val != NULL && *val ) {
217 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
220 ldap_pvt_filter_value_unescape( val );
221 if ( gotstar == 0 ) {
222 f->f_sub_initial = ch_strdup( val );
223 } else if ( nextstar == NULL ) {
224 f->f_sub_final = ch_strdup( val );
226 charray_add( &f->f_sub_any, val );
238 * find_matching_paren - return a pointer to the right paren in s matching
239 * the left paren to which *s currently points
243 find_matching_paren( char *s )
253 else if ( *s == ')' )
256 if ( balance == 0 ) {
259 if ( *s == '\\' && ! escape )