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;
148 #ifdef SLAPD_SCHEMA_NOT_COMPAT
153 Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
155 if ( (s = strchr( str, '=' )) == NULL ) {
160 *s-- = '\0'; /* we shouldn't be mucking with str */
163 f = (Filter *) ch_calloc( 1, sizeof(Filter) );
167 f->f_choice = LDAP_FILTER_LE;
171 f->f_choice = LDAP_FILTER_GE;
175 f->f_choice = LDAP_FILTER_APPROX;
179 f->f_choice = LDAP_FILTER_EXT;
184 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
185 f->f_choice = LDAP_FILTER_EQUALITY;
186 } else if ( strcmp( value, "*" ) == 0 ) {
187 f->f_choice = LDAP_FILTER_PRESENT;
189 f->f_choice = LDAP_FILTER_SUBSTRINGS;
190 #ifdef SLAPD_SCHEMA_NOT_COMPAT
191 rc = slap_str2ad( str, &f->f_sub_desc, &text );
192 if( rc != LDAP_SUCCESS ) {
198 f->f_sub_type = ch_strdup( str );
200 if ( str2subvals( value, f ) != 0 ) {
211 if ( f->f_choice == LDAP_FILTER_PRESENT ) {
212 #ifdef SLAPD_SCHEMA_NOT_COMPAT
213 rc = slap_str2ad( str, &f->f_desc, &text );
214 if( rc != LDAP_SUCCESS ) {
220 f->f_type = ch_strdup( str );
223 #ifdef SLAPD_SCHEMA_NOT_COMPAT
226 rc = slap_str2ad( str, &f->f_av_desc, &text );
227 if( rc != LDAP_SUCCESS ) {
233 tmp = ch_strdup( value );
234 ldap_pvt_filter_value_unescape( tmp );
235 f->f_av_value = ber_bvstr( tmp );
237 f->f_avtype = ch_strdup( str );
238 f->f_avvalue.bv_val = ch_strdup( value );
239 ldap_pvt_filter_value_unescape( f->f_avvalue.bv_val );
240 f->f_avvalue.bv_len = strlen( value );
251 str2subvals( const char *in, Filter *f )
253 char *nextstar, *val, *freeme;
256 Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
258 if( in == NULL ) return 0;
260 val = freeme = ch_strdup( in );
264 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
267 ldap_pvt_filter_value_unescape( val );
269 if ( gotstar == 0 ) {
270 f->f_sub_initial = ber_bvstrdup( val );
272 } else if ( nextstar == NULL ) {
273 f->f_sub_final = ber_bvstrdup( val );
276 charray_add( (char ***) &f->f_sub_any, (char *) ber_bvstrdup( val ) );
288 * find_matching_paren - return a pointer to the right paren in s matching
289 * the left paren to which *s currently points
293 find_matching_paren( const char *s )
303 else if ( *s == ')' )
306 if ( balance == 0 ) {
309 if ( *s == '\\' && ! escape )