s4:ldb Add a helper function for 'canonicalise' both strings base compares
authorAndrew Bartlett <abartlet@samba.org>
Thu, 5 Nov 2009 05:56:05 +0000 (16:56 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 11 Nov 2009 21:11:16 +0000 (08:11 +1100)
This will help simplify boilerplate comparison functions where we
don't have a shortcut way to compare.

Andrew Bartlett

source4/lib/ldb/common/attrib_handlers.c
source4/lib/ldb/include/ldb_module.h

index ba21fcac9b0e0f525193766164d579c6962d69d9..1c08741f7d963f4fba06e6b3b3b5d4297528f9e0 100644 (file)
@@ -2,6 +2,7 @@
    ldb database library
 
    Copyright (C) Andrew Tridgell  2005
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2009
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -430,3 +431,29 @@ const struct ldb_schema_syntax *ldb_standard_syntax_by_name(struct ldb_context *
        }
        return NULL;
 }
+
+int ldb_any_comparison(struct ldb_context *ldb, void *mem_ctx, 
+                      ldb_attr_handler_t canonicalise_fn, 
+                      const struct ldb_val *v1,
+                      const struct ldb_val *v2)
+{
+       int ret, ret1, ret2;
+       struct ldb_val v1_canon, v2_canon;
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+
+       /* I could try and bail if tmp_ctx was NULL, but what return
+        * value would I use?
+        *
+        * It seems easier to continue on the NULL context 
+        */
+       ret1 = canonicalise_fn(ldb, tmp_ctx, v1, &v1_canon);
+       ret2 = canonicalise_fn(ldb, tmp_ctx, v2, &v2_canon);
+
+       if (ret1 == LDB_SUCCESS && ret2 == LDB_SUCCESS) {
+               ret = ldb_comparison_binary(ldb, mem_ctx, &v1_canon, &v2_canon);
+       } else {
+               ret = ldb_comparison_binary(ldb, mem_ctx, v1, v2);
+       }
+       talloc_free(tmp_ctx);
+       return ret;
+}
index 977d485adab5da459ce74356c318951b2539da20..7c18683201efaee75faa1c823bee26a81461f668 100644 (file)
@@ -99,6 +99,12 @@ void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
                                               ldb_attribute_handler_override_fn_t override,
                                               void *private_data);
 
+/* A useful function to build comparison functions with */
+int ldb_any_comparison(struct ldb_context *ldb, void *mem_ctx, 
+                      ldb_attr_handler_t canonicalise_fn, 
+                      const struct ldb_val *v1,
+                      const struct ldb_val *v2);
+
 /* The following definitions come from lib/ldb/common/ldb_controls.c  */
 struct ldb_control *get_control_from_list(struct ldb_control **controls, const char *oid);
 int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver);