1 /* str2filter.c - parse an rfc 1588 string filter */
4 * Copyright 1998-2000 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;
151 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
153 if ( (s = strchr( str, '=' )) == NULL ) {
158 *s-- = '\0'; /* we shouldn't be mucking with str */
161 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
165 f->f_choice = LDAP_FILTER_LE;
169 f->f_choice = LDAP_FILTER_GE;
173 f->f_choice = LDAP_FILTER_APPROX;
177 f->f_choice = LDAP_FILTER_EXT;
183 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
184 f->f_choice = LDAP_FILTER_EQUALITY;
185 } else if ( strcmp( value, "*" ) == 0 ) {
186 f->f_choice = LDAP_FILTER_PRESENT;
188 f->f_choice = LDAP_FILTER_SUBSTRINGS;
189 f->f_sub = ch_calloc( 1, sizeof( SubstringsAssertion ) );
190 rc = slap_str2ad( str, &f->f_sub_desc, &text );
191 if( rc != LDAP_SUCCESS ) {
196 if ( str2subvals( value, f ) != 0 ) {
207 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
208 rc = slap_str2ad( str, &f->f_desc, &text );
209 if( rc != LDAP_SUCCESS ) {
217 f->f_ava = ch_calloc( 1, sizeof( AttributeAssertion ) );
219 rc = slap_str2ad( str, &f->f_av_desc, &text );
220 if( rc != LDAP_SUCCESS ) {
226 tmp = ch_strdup( value );
227 ldap_pvt_filter_value_unescape( tmp );
228 f->f_av_value = ber_bvstr( tmp );
238 str2subvals( const char *in, Filter *f )
240 char *nextstar, *val, *freeme;
243 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
245 if( in == NULL ) return 0;
247 val = freeme = ch_strdup( in );
251 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
254 ldap_pvt_filter_value_unescape( val );
256 if ( gotstar == 0 ) {
257 f->f_sub_initial = ber_bvstrdup( val );
259 } else if ( nextstar == NULL ) {
260 f->f_sub_final = ber_bvstrdup( val );
263 charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
275 * find_matching_paren - return a pointer to the right paren in s matching
276 * the left paren to which *s currently points
280 find_matching_paren( const char *s )
290 else if ( *s == ')' )
293 if ( balance == 0 ) {
296 if ( *s == '\\' && ! escape )