]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/sets.c
Sync with HEAD
[openldap] / servers / slapd / sets.c
index 4f2f617d34383288c075e900abd7a4392b331bf1..7dbd19d811523149eec30a499593ee4a66ceb037 100644 (file)
@@ -1,7 +1,16 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2000-2003 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
 #include "slap.h"
 #include "sets.h"
 
-static BVarray set_join (BVarray lset, int op, BVarray rset);
-static BVarray set_chase (SLAP_SET_GATHER gatherer,
-       void *cookie, BVarray set, struct berval *attr, int closure);
+static BerVarray set_join (SetCookie *cp, BerVarray lset, int op, BerVarray rset);
+static BerVarray set_chase (SLAP_SET_GATHER gatherer,
+       SetCookie *cookie, BerVarray set, struct berval *attr, int closure);
 static int set_samedn (char *dn1, char *dn2);
 
 long
-slap_set_size (BVarray set)
+slap_set_size (BerVarray set)
 {
-       int i;
+       long    i;
 
        i = 0;
        if (set != NULL) {
                while (set[i].bv_val)
                        i++;
        }
-       return(i);
+       return i;
 }
 
 void
-slap_set_dispose (BVarray set)
+slap_set_dispose (SetCookie *cp, BerVarray set)
 {
-       bvarray_free(set);
+       ber_bvarray_free_x(set, cp->op->o_tmpmemctx);
 }
 
-static BVarray
-set_join (BVarray lset, int op, BVarray rset)
+static BerVarray
+set_join (SetCookie *cp, BerVarray lset, int op, BerVarray rset)
 {
-       BVarray set;
+       BerVarray set;
        long i, j, last;
 
        set = NULL;
@@ -47,19 +56,20 @@ set_join (BVarray lset, int op, BVarray rset)
                if (lset == NULL || lset->bv_val == NULL) {
                        if (rset == NULL) {
                                if (lset == NULL)
-                                       return(ch_calloc(1, sizeof(struct berval)));
+                                       return(cp->op->o_tmpcalloc(1, sizeof(struct berval),
+                                               cp->op->o_tmpmemctx));
                                return(lset);
                        }
-                       slap_set_dispose(lset);
+                       slap_set_dispose(cp, lset);
                        return(rset);
                }
                if (rset == NULL || rset->bv_val == NULL) {
-                       slap_set_dispose(rset);
+                       slap_set_dispose(cp, rset);
                        return(lset);
                }
 
                i = slap_set_size(lset) + slap_set_size(rset) + 1;
-               set = ch_calloc(i, sizeof(struct berval));
+               set = cp->op->o_tmpcalloc(i, sizeof(struct berval), cp->op->o_tmpmemctx);
                if (set != NULL) {
                        /* set_chase() depends on this routine to
                         * keep the first elements of the result
@@ -68,11 +78,11 @@ set_join (BVarray lset, int op, BVarray rset)
                         */
                        for (i = 0; lset[i].bv_val; i++)
                                set[i] = lset[i];
-                       ch_free(lset);
+                       cp->op->o_tmpfree(lset, cp->op->o_tmpmemctx);
                        for (i = 0; rset[i].bv_val; i++) {
                                for (j = 0; set[j].bv_val; j++) {
                                        if (set_samedn(rset[i].bv_val, set[j].bv_val)) {
-                                               ch_free(rset[i].bv_val);
+                                               cp->op->o_tmpfree(rset[i].bv_val, cp->op->o_tmpmemctx);
                                                rset[i].bv_val = NULL;
                                                break;          
                                        }       
@@ -80,14 +90,14 @@ set_join (BVarray lset, int op, BVarray rset)
                                if (rset[i].bv_val)
                                        set[j] = rset[i];
                        }
-                       ch_free(rset);
+                       cp->op->o_tmpfree(rset, cp->op->o_tmpmemctx);
                }
                return(set);
        }
 
        if (op == '&') {
                if (lset == NULL || lset->bv_val == NULL || rset == NULL || rset->bv_val == NULL) {
-                       set = ch_calloc(1, sizeof(struct berval));
+                       set = cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx);
                } else {
                        set = lset;
                        lset = NULL;
@@ -98,7 +108,7 @@ set_join (BVarray lset, int op, BVarray rset)
                                                break;
                                }
                                if (rset[j].bv_val == NULL) {
-                                       ch_free(set[i].bv_val);
+                                       cp->op->o_tmpfree(set[i].bv_val, cp->op->o_tmpmemctx);
                                        set[i] = set[last];
                                        set[last].bv_val = NULL;
                                        last--;
@@ -108,50 +118,53 @@ set_join (BVarray lset, int op, BVarray rset)
                }
        }
 
-       slap_set_dispose(lset);
-       slap_set_dispose(rset);
+       slap_set_dispose(cp, lset);
+       slap_set_dispose(cp, rset);
        return(set);
 }
 
-static BVarray
+static BerVarray
 set_chase (SLAP_SET_GATHER gatherer,
-       void *cookie, BVarray set, struct berval *attr, int closure)
+       SetCookie *cp, BerVarray set, struct berval *attr, int closure)
 {
-       BVarray vals, nset;
+       BerVarray vals, nset;
        char attrstr[32];
-       struct berval bv = {attr->bv_len, attrstr};
+       struct berval bv;
        int i;
 
+       bv.bv_len = attr->bv_len;
+       bv.bv_val = attrstr;
+
        if (set == NULL)
-               return(ch_calloc(1, sizeof(struct berval)));
+               return(cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx));
 
        if (set->bv_val == NULL)
                return(set);
 
        if (attr->bv_len > (sizeof(attrstr) - 1)) {
-               slap_set_dispose(set);
+               slap_set_dispose(cp, set);
                return(NULL);
        }
        AC_MEMCPY(attrstr, attr->bv_val, attr->bv_len);
        attrstr[attr->bv_len] = 0;
 
-       nset = ch_calloc(1, sizeof(struct berval));
+       nset = cp->op->o_tmpcalloc(1, sizeof(struct berval), cp->op->o_tmpmemctx);
        if (nset == NULL) {
-               slap_set_dispose(set);
+               slap_set_dispose(cp, set);
                return(NULL);
        }
        for (i = 0; set[i].bv_val; i++) {
-               vals = (gatherer)(cookie, set[i].bv_val, &bv);
+               vals = (gatherer)(cp, &set[i], &bv);
                if (vals != NULL)
-                       nset = set_join(nset, '|', vals);
+                       nset = set_join(cp, nset, '|', vals);
        }
-       slap_set_dispose(set);
+       slap_set_dispose(cp, set);
 
        if (closure) {
                for (i = 0; nset[i].bv_val; i++) {
-                       vals = (gatherer)(cookie, nset[i].bv_val, &bv);
+                       vals = (gatherer)(cp, &nset[i], &bv);
                        if (vals != NULL) {
-                               nset = set_join(nset, '|', vals);
+                               nset = set_join(cp, nset, '|', vals);
                                if (nset == NULL)
                                        break;
                        }
@@ -193,21 +206,21 @@ set_samedn (char *dn1, char *dn2)
 
 int
 slap_set_filter (SLAP_SET_GATHER gatherer,
-       void *cookie, struct berval *fbv,
-       char *user, char *this, BVarray *results)
+       SetCookie *cp, struct berval *fbv,
+       struct berval *user, struct berval *this, BerVarray *results)
 {
 #define IS_SET(x)      ( (long)(x) >= 256 )
 #define IS_OP(x)       ( (long)(x) < 256 )
 #define SF_ERROR(x)    do { rc = -1; goto _error; } while (0)
-#define SF_TOP()       ( (BVarray)( (stp < 0) ? 0 : stack[stp] ) )
-#define SF_POP()       ( (BVarray)( (stp < 0) ? 0 : stack[stp--] ) )
+#define SF_TOP()       ( (BerVarray)( (stp < 0) ? 0 : stack[stp] ) )
+#define SF_POP()       ( (BerVarray)( (stp < 0) ? 0 : stack[stp--] ) )
 #define SF_PUSH(x)     do { \
                if (stp >= 63) SF_ERROR(overflow); \
-               stack[++stp] = (BVarray)(long)(x); \
+               stack[++stp] = (BerVarray)(long)(x); \
        } while (0)
 
-       BVarray set, lset;
-       BVarray stack[64];
+       BerVarray set, lset;
+       BerVarray stack[64];
        int len, op, rc, stp;
        char c, *filter = fbv->bv_val;
 
@@ -242,7 +255,7 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                                op = (long)SF_POP();
                                lset = SF_POP();
                                SF_POP();
-                               set = set_join(lset, op, set);
+                               set = set_join(cp, lset, op, set);
                                if (set == NULL)
                                        SF_ERROR(memory);
                                SF_PUSH(set);
@@ -263,7 +276,7 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                        } else if (IS_OP(SF_TOP())) {
                                op = (long)SF_POP();
                                lset = SF_POP();
-                               set = set_join(lset, op, set);
+                               set = set_join(cp, lset, op, set);
                                if (set == NULL)
                                        SF_ERROR(memory);
                                SF_PUSH(set);
@@ -284,10 +297,10 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                        if (c == 0)
                                SF_ERROR(syntax);
                        
-                       set = ch_calloc(2, sizeof(struct berval));
+                       set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
                        if (set == NULL)
                                SF_ERROR(memory);
-                       set->bv_val = ch_calloc(len + 1, sizeof(char));
+                       set->bv_val = cp->op->o_tmpcalloc(len + 1, sizeof(char), cp->op->o_tmpmemctx);
                        if (set->bv_val == NULL)
                                SF_ERROR(memory);
                        AC_MEMCPY(set->bv_val, &filter[-len - 1], len);
@@ -328,10 +341,10 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                        {
                                if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
                                        SF_ERROR(syntax);
-                               set = ch_calloc(2, sizeof(struct berval));
+                               set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
                                if (set == NULL)
                                        SF_ERROR(memory);
-                               ber_str2bv( this, 0, 1, set );
+                               ber_dupbv_x( set, this, cp->op->o_tmpmemctx );
                                if (set->bv_val == NULL)
                                        SF_ERROR(memory);
                        } else if (len == 4
@@ -339,10 +352,10 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                        {
                                if ((SF_TOP() == (void *)'/') || IS_SET(SF_TOP()))
                                        SF_ERROR(syntax);
-                               set = ch_calloc(2, sizeof(struct berval));
+                               set = cp->op->o_tmpcalloc(2, sizeof(struct berval), cp->op->o_tmpmemctx);
                                if (set == NULL)
                                        SF_ERROR(memory);
-                               ber_str2bv( user, 0, 1, set );
+                               ber_dupbv_x( set, user, cp->op->o_tmpmemctx );
                                if (set->bv_val == NULL)
                                        SF_ERROR(memory);
                        } else if (SF_TOP() != (void *)'/') {
@@ -353,7 +366,7 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
                                fb2.bv_val = filter;
                                fb2.bv_len = len;
                                set = set_chase(gatherer,
-                                       cookie, SF_POP(), &fb2, c == '*');
+                                       cp, SF_POP(), &fb2, c == '*');
                                if (set == NULL)
                                        SF_ERROR(memory);
                                if (c == '*')
@@ -374,14 +387,14 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
        } else if (IS_OP(SF_TOP())) {
                op = (long)SF_POP();
                lset = SF_POP();
-               set = set_join(lset, op, set);
+               set = set_join(cp, lset, op, set);
                if (set == NULL)
                        SF_ERROR(memory);
        } else {
                SF_ERROR(syntax);
        }
 
-       rc = slap_set_size(set);
+       rc = slap_set_size(set) > 0 ? 1 : 0;
        if (results) {
                *results = set;
                set = NULL;
@@ -389,10 +402,10 @@ slap_set_filter (SLAP_SET_GATHER gatherer,
 
 _error:
        if (IS_SET(set))
-               slap_set_dispose(set);
+               slap_set_dispose(cp, set);
        while ((set = SF_POP())) {
                if (IS_SET(set))
-                       slap_set_dispose(set);
+                       slap_set_dispose(cp, set);
        }
        return(rc);
 }