3 * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static BVarray set_join (BVarray lset, int op, BVarray rset);
16 static BVarray set_chase (SLAP_SET_GATHER gatherer,
17 void *cookie, BVarray set, struct berval *attr, int closure);
18 static int set_samedn (char *dn1, char *dn2);
21 slap_set_size (BVarray set)
34 slap_set_dispose (BVarray set)
40 set_join (BVarray lset, int op, BVarray rset)
47 if (lset == NULL || lset->bv_val == NULL) {
50 return(ch_calloc(1, sizeof(struct berval)));
53 slap_set_dispose(lset);
56 if (rset == NULL || rset->bv_val == NULL) {
57 slap_set_dispose(rset);
61 i = slap_set_size(lset) + slap_set_size(rset) + 1;
62 set = ch_calloc(i, sizeof(struct berval));
64 /* set_chase() depends on this routine to
65 * keep the first elements of the result
66 * set the same (and in the same order)
69 for (i = 0; lset[i].bv_val; i++)
72 for (i = 0; rset[i].bv_val; i++) {
73 for (j = 0; set[j].bv_val; j++) {
74 if (set_samedn(rset[i].bv_val, set[j].bv_val)) {
75 ch_free(rset[i].bv_val);
76 rset[i].bv_val = NULL;
89 if (lset == NULL || lset->bv_val == NULL || rset == NULL || rset->bv_val == NULL) {
90 set = ch_calloc(1, sizeof(struct berval));
94 last = slap_set_size(set) - 1;
95 for (i = 0; set[i].bv_val; i++) {
96 for (j = 0; rset[j].bv_val; j++) {
97 if (set_samedn(set[i].bv_val, rset[j].bv_val))
100 if (rset[j].bv_val == NULL) {
101 ch_free(set[i].bv_val);
103 set[last].bv_val = NULL;
111 slap_set_dispose(lset);
112 slap_set_dispose(rset);
117 set_chase (SLAP_SET_GATHER gatherer,
118 void *cookie, BVarray set, struct berval *attr, int closure)
122 struct berval bv = {attr->bv_len, attrstr};
126 return(ch_calloc(1, sizeof(struct berval)));
128 if (set->bv_val == NULL)
131 if (attr->bv_len > (sizeof(attrstr) - 1)) {
132 slap_set_dispose(set);
135 AC_MEMCPY(attrstr, attr->bv_val, attr->bv_len);
136 attrstr[attr->bv_len] = 0;
138 nset = ch_calloc(1, sizeof(struct berval));
140 slap_set_dispose(set);
143 for (i = 0; set[i].bv_val; i++) {
144 vals = (gatherer)(cookie, set[i].bv_val, &bv);
146 nset = set_join(nset, '|', vals);
148 slap_set_dispose(set);
151 for (i = 0; nset[i].bv_val; i++) {
152 vals = (gatherer)(cookie, nset[i].bv_val, &bv);
154 nset = set_join(nset, '|', vals);
164 set_samedn (char *dn1, char *dn2)
168 while (*dn1 == ' ') dn1++;
169 while (*dn2 == ' ') dn2++;
170 while (*dn1 || *dn2) {
171 if (*dn1 != '=' && *dn1 != ','
172 && *dn2 != '=' && *dn2 != ',')
176 if (c1 >= 'a' && c1 <= 'z')
178 if (c2 >= 'a' && c2 <= 'z')
183 while (*dn1 == ' ') dn1++;
184 while (*dn2 == ' ') dn2++;
185 if (*dn1++ != *dn2++)
187 while (*dn1 == ' ') dn1++;
188 while (*dn2 == ' ') dn2++;
195 slap_set_filter (SLAP_SET_GATHER gatherer,
196 void *cookie, struct berval *fbv,
197 char *user, char *this, BVarray *results)
199 #define IS_SET(x) ( (long)(x) >= 256 )
200 #define IS_OP(x) ( (long)(x) < 256 )
201 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
202 #define SF_TOP() ( (BVarray)( (stp < 0) ? 0 : stack[stp] ) )
203 #define SF_POP() ( (BVarray)( (stp < 0) ? 0 : stack[stp--] ) )
204 #define SF_PUSH(x) do { \
205 if (stp >= 63) SF_ERROR(overflow); \
206 stack[++stp] = (BVarray)(long)(x); \
211 int len, op, rc, stp;
212 char c, *filter = fbv->bv_val;
218 while ((c = *filter++)) {
228 if (IS_SET(SF_TOP()))
237 if (SF_TOP() == (void *)'(') {
241 } else if (IS_OP(SF_TOP())) {
245 set = set_join(lset, op, set);
260 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
263 } else if (IS_OP(SF_TOP())) {
266 set = set_join(lset, op, set);
278 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
281 (c = *filter++) && (c != ']');
287 set = ch_calloc(2, sizeof(struct berval));
290 set->bv_val = ch_calloc(len + 1, sizeof(char));
291 if (set->bv_val == NULL)
293 AC_MEMCPY(set->bv_val, &filter[-len - 1], len);
303 /* fall through to next case */
313 && (c < 'A' || c > 'Z')
314 && (c < 'a' || c > 'z'))
321 && ((c >= '0' && c <= '9')
322 || (c >= 'A' && c <= 'Z')
323 || (c >= 'a' && c <= 'z'));
327 && memcmp("this", filter, len) == 0)
329 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
331 set = ch_calloc(2, sizeof(struct berval));
334 ber_str2bv( this, 0, 1, set );
335 if (set->bv_val == NULL)
338 && memcmp("user", filter, len) == 0)
340 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
342 set = ch_calloc(2, sizeof(struct berval));
345 ber_str2bv( user, 0, 1, set );
346 if (set->bv_val == NULL)
348 } else if (SF_TOP() != (void *)'/') {
355 set = set_chase(gatherer,
356 cookie, SF_POP(), &fb2, c == '*');
374 } else if (IS_OP(SF_TOP())) {
377 set = set_join(lset, op, set);
384 rc = slap_set_size(set);
392 slap_set_dispose(set);
393 while ((set = SF_POP())) {
395 slap_set_dispose(set);