3 * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static BerVarray set_join (BerVarray lset, int op, BerVarray rset);
16 static BerVarray set_chase (SLAP_SET_GATHER gatherer,
17 void *cookie, BerVarray set, struct berval *attr, int closure);
18 static int set_samedn (char *dn1, char *dn2);
21 slap_set_size (BerVarray set)
34 slap_set_dispose (BerVarray set)
36 ber_bvarray_free(set);
40 set_join (BerVarray lset, int op, BerVarray rset)
47 if (lset == NULL || lset->bv_val == NULL) {
50 return(SLAP_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 = SLAP_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 = SLAP_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, BerVarray set, struct berval *attr, int closure)
120 BerVarray vals, nset;
125 bv.bv_len = attr->bv_len;
129 return(SLAP_CALLOC(1, sizeof(struct berval)));
131 if (set->bv_val == NULL)
134 if (attr->bv_len > (sizeof(attrstr) - 1)) {
135 slap_set_dispose(set);
138 AC_MEMCPY(attrstr, attr->bv_val, attr->bv_len);
139 attrstr[attr->bv_len] = 0;
141 nset = SLAP_CALLOC(1, sizeof(struct berval));
143 slap_set_dispose(set);
146 for (i = 0; set[i].bv_val; i++) {
147 vals = (gatherer)(cookie, &set[i], &bv);
149 nset = set_join(nset, '|', vals);
151 slap_set_dispose(set);
154 for (i = 0; nset[i].bv_val; i++) {
155 vals = (gatherer)(cookie, &nset[i], &bv);
157 nset = set_join(nset, '|', vals);
167 set_samedn (char *dn1, char *dn2)
171 while (*dn1 == ' ') dn1++;
172 while (*dn2 == ' ') dn2++;
173 while (*dn1 || *dn2) {
174 if (*dn1 != '=' && *dn1 != ','
175 && *dn2 != '=' && *dn2 != ',')
179 if (c1 >= 'a' && c1 <= 'z')
181 if (c2 >= 'a' && c2 <= 'z')
186 while (*dn1 == ' ') dn1++;
187 while (*dn2 == ' ') dn2++;
188 if (*dn1++ != *dn2++)
190 while (*dn1 == ' ') dn1++;
191 while (*dn2 == ' ') dn2++;
198 slap_set_filter (SLAP_SET_GATHER gatherer,
199 void *cookie, struct berval *fbv,
200 struct berval *user, struct berval *this, BerVarray *results)
202 #define IS_SET(x) ( (long)(x) >= 256 )
203 #define IS_OP(x) ( (long)(x) < 256 )
204 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
205 #define SF_TOP() ( (BerVarray)( (stp < 0) ? 0 : stack[stp] ) )
206 #define SF_POP() ( (BerVarray)( (stp < 0) ? 0 : stack[stp--] ) )
207 #define SF_PUSH(x) do { \
208 if (stp >= 63) SF_ERROR(overflow); \
209 stack[++stp] = (BerVarray)(long)(x); \
214 int len, op, rc, stp;
215 char c, *filter = fbv->bv_val;
221 while ((c = *filter++)) {
231 if (IS_SET(SF_TOP()))
240 if (SF_TOP() == (void *)'(') {
244 } else if (IS_OP(SF_TOP())) {
248 set = set_join(lset, op, set);
263 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
266 } else if (IS_OP(SF_TOP())) {
269 set = set_join(lset, op, set);
281 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
284 (c = *filter++) && (c != ']');
290 set = SLAP_CALLOC(2, sizeof(struct berval));
293 set->bv_val = SLAP_CALLOC(len + 1, sizeof(char));
294 if (set->bv_val == NULL)
296 AC_MEMCPY(set->bv_val, &filter[-len - 1], len);
306 /* fall through to next case */
316 && (c < 'A' || c > 'Z')
317 && (c < 'a' || c > 'z'))
324 && ((c >= '0' && c <= '9')
325 || (c >= 'A' && c <= 'Z')
326 || (c >= 'a' && c <= 'z'));
330 && memcmp("this", filter, len) == 0)
332 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
334 set = SLAP_CALLOC(2, sizeof(struct berval));
337 ber_dupbv( set, this );
338 if (set->bv_val == NULL)
341 && memcmp("user", filter, len) == 0)
343 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
345 set = SLAP_CALLOC(2, sizeof(struct berval));
348 ber_dupbv( set, user );
349 if (set->bv_val == NULL)
351 } else if (SF_TOP() != (void *)'/') {
358 set = set_chase(gatherer,
359 cookie, SF_POP(), &fb2, c == '*');
377 } else if (IS_OP(SF_TOP())) {
380 set = set_join(lset, op, set);
387 rc = slap_set_size(set);
395 slap_set_dispose(set);
396 while ((set = SF_POP())) {
398 slap_set_dispose(set);