3 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
15 static char **set_join (char **lset, int op, char **rset);
16 static char **set_chase (SET_GATHER gatherer,
17 void *cookie, char **set, char *attr, int attrlen, int closure);
18 static int set_samedn (char *dn1, char *dn2);
34 set_dispose (char **set)
39 for (i = 0; set[i]; i++)
46 set_join (char **lset, int op, char **rset)
53 if (lset == NULL || *lset == NULL) {
56 return(ch_calloc(1, sizeof(char *)));
62 if (rset == NULL || *rset == NULL) {
67 i = set_size(lset) + set_size(rset) + 1;
68 set = ch_calloc(i, sizeof(char *));
70 /* set_chase() depends on this routine to
71 * keep the first elements of the result
72 * set the same (and in the same order)
75 for (i = 0; lset[i]; i++)
78 for (i = 0; rset[i]; i++) {
79 for (j = 0; set[j]; j++) {
80 if (set_samedn(rset[i], set[j])) {
95 if (lset == NULL || *lset == NULL || rset == NULL || *rset == NULL) {
96 set = ch_calloc(1, sizeof(char *));
100 last = set_size(set) - 1;
101 for (i = 0; set[i]; i++) {
102 for (j = 0; rset[j]; j++) {
103 if (set_samedn(set[i], rset[j]))
106 if (rset[j] == NULL) {
123 set_chase (SET_GATHER gatherer,
124 void *cookie, char **set, char *attr, int attrlen, int closure)
131 return(ch_calloc(1, sizeof(char *)));
136 if (attrlen > (sizeof(attrstr) - 1)) {
140 AC_MEMCPY(attrstr, attr, attrlen);
141 attrstr[attrlen] = 0;
143 nset = ch_calloc(1, sizeof(char *));
148 for (i = 0; set[i]; i++) {
149 vals = (gatherer)(cookie, set[i], attrstr);
151 nset = set_join(nset, '|', vals);
156 for (i = 0; nset[i]; i++) {
157 vals = (gatherer)(cookie, nset[i], attrstr);
159 nset = set_join(nset, '|', vals);
169 set_samedn (char *dn1, char *dn2)
173 while (*dn1 == ' ') dn1++;
174 while (*dn2 == ' ') dn2++;
175 while (*dn1 || *dn2) {
176 if (*dn1 != '=' && *dn1 != ','
177 && *dn2 != '=' && *dn2 != ',')
181 if (c1 >= 'a' && c1 <= 'z')
183 if (c2 >= 'a' && c2 <= 'z')
188 while (*dn1 == ' ') dn1++;
189 while (*dn2 == ' ') dn2++;
190 if (*dn1++ != *dn2++)
192 while (*dn1 == ' ') dn1++;
193 while (*dn2 == ' ') dn2++;
200 set_filter (SET_GATHER gatherer,
201 void *cookie, char *filter, char *user, char *this, char ***results)
203 #define IS_SET(x) ( (long)(x) >= 256 )
204 #define IS_OP(x) ( (long)(x) < 256 )
205 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
206 #define SF_TOP() ( (char **)( (stp < 0) ? 0 : stack[stp] ) )
207 #define SF_POP() ( (char **)( (stp < 0) ? 0 : stack[stp--] ) )
208 #define SF_PUSH(x) do { \
209 if (stp >= 63) SF_ERROR(overflow); \
210 stack[++stp] = (char **)(long)(x); \
215 int len, op, rc, stp;
222 while (c = *filter++) {
232 if (IS_SET(SF_TOP()))
241 if (SF_TOP() == (void *)'(') {
245 } else if (IS_OP(SF_TOP())) {
249 set = set_join(lset, op, set);
264 if (SF_TOP() == 0 || SF_TOP() == (void *)'(') {
267 } else if (IS_OP(SF_TOP())) {
270 set = set_join(lset, op, set);
282 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
285 (c = *filter++) && (c != ']');
291 set = ch_calloc(2, sizeof(char *));
294 *set = ch_calloc(len + 1, sizeof(char));
297 AC_MEMCPY(*set, &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 = ch_calloc(2, sizeof(char *));
337 *set = ch_strdup(this);
341 && memcmp("user", filter, len) == 0)
343 if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
345 set = ch_calloc(2, sizeof(char *));
348 *set = ch_strdup(user);
351 } else if (SF_TOP() != (void *)'/') {
355 set = set_chase(gatherer,
356 cookie, SF_POP(), filter, len, c == '*');
374 } else if (IS_OP(SF_TOP())) {
377 set = set_join(lset, op, set);
393 while (set = SF_POP()) {