r13335: Fix the build and add an utf8 safe ldb_hadler_fold function
authorSimo Sorce <idra@samba.org>
Sat, 4 Feb 2006 07:57:57 +0000 (07:57 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:51:45 +0000 (13:51 -0500)
based on ldb_casefold
(This used to be commit 6104f900863c688707809d42c5429a42d654d5fb)

source4/lib/db_wrap.c
source4/lib/ldb/common/attrib_handlers.c
source4/lib/ldb/include/ldb.h
source4/lib/ldb/ldb_tdb/ldb_index.c

index f3358bff98bbdee279cc1966afcab727eb2fadfd..8524f0da10849cbb9bcb5e6d550c22d50649d4c7 100644 (file)
@@ -55,12 +55,12 @@ static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
        free(s);
 }
 
-static int wrap_caseless_cmp(void *context, const char *s1, const char *s2)
+int wrap_caseless_cmp(void *context, const char *s1, const char *s2)
 {
        return strcasecmp_m(s1, s2);
 }
 
-static char *wrap_casefold(void *context, void *mem_ctx, const char *s)
+char *wrap_casefold(void *context, void *mem_ctx, const char *s)
 {
        return strupper_talloc(mem_ctx, s);
 }
index 2109e9b2da7776462e362e60995972be9443b25a..4b6a7af1eedd6f4dc52c48b67c5623c13c31b386 100644 (file)
@@ -46,32 +46,60 @@ int ldb_handler_copy(struct ldb_context *ldb, void *mem_ctx,
 /*
   a case folding copy handler, removing leading and trailing spaces and
   multiple internal spaces
+
+  We exploit the fact that utf8 never uses the space octet except for
+  the space itself
 */
 static int ldb_handler_fold(struct ldb_context *ldb, void *mem_ctx,
                            const struct ldb_val *in, struct ldb_val *out)
 {
-       uint8_t *s1, *s2;
-       out->data = talloc_size(mem_ctx, strlen((char *)in->data)+1);
+       char *s, *t;
+       int l;
+       if (!in || !out || !(in->data)) {
+               return -1;
+       }
+
+       out->data = (uint8_t *)ldb_casefold(ldb, mem_ctx, (const char *)(in->data));
        if (out->data == NULL) {
-               ldb_oom(ldb);
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_handler_fold: unable to casefold string [%s]", in->data);
                return -1;
        }
-       s1 = in->data;
-       s2 = out->data;
-       while (*s1 == ' ') s1++;
-       while (*s1) {
-               *s2 = toupper(*s1);
-               if (s1[0] == ' ') {
-                       while (s1[0] == s1[1]) s1++;
+
+       s = (char *)(out->data);
+       
+       /* remove trailing spaces if any */
+       l = strlen(s);
+       while (s[l - 1] == ' ') l--;
+       s[l] = '\0';
+       
+       /* remove leading spaces if any */
+       if (*s == ' ') {
+               for (t = s; *s == ' '; s++) ;
+
+               /* remove leading spaces by moving down the string */
+               memmove(t, s, l);
+
+               s = t;
+       }
+
+       /* check middle spaces */
+       while ((t = strchr(s, ' ')) != NULL) {
+               for (s = t; *s == ' '; s++) ;
+
+               if ((s - t) > 1) {
+                       l = strlen(s);
+
+                       /* remove all spaces but one by moving down the string */
+                       memmove(t + 1, s, l);
                }
-               s2++; s1++;
        }
-       *s2 = 0;
+
        out->length = strlen((char *)out->data);
        return 0;
 }
 
 
+
 /*
   canonicalise a ldap Integer
   rfc2252 specifies it should be in decimal form
@@ -114,8 +142,8 @@ int ldb_comparison_binary(struct ldb_context *ldb, void *mem_ctx,
 }
 
 /*
-  compare two case insensitive strings, ignoring multiple whitespace
-  and leading and trailing whitespace
+  compare two case insensitive strings, ignoring multiple whitespaces
+  and leading and trailing whitespaces
   see rfc2252 section 8.1
 */
 static int ldb_comparison_fold(struct ldb_context *ldb, void *mem_ctx,
index 7c39aeeeb9569bc97ed5afbdd67b5f0bbf8943b3..4e457b028fa2763e0694118432e5e86c8dfdfdb6 100644 (file)
@@ -970,6 +970,7 @@ int ldb_dn_cmp(struct ldb_context *ldb, const char *dn1, const char *dn2);
    case; non-zero if there are any differences
 */
 int ldb_attr_cmp(const char *attr1, const char *attr2);
+char *ldb_attr_casefold(void *mem_ctx, const char *s);
 int ldb_attr_dn(const char *attr);
 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value);
 
index ac3063ef28323824030baf2149e019322066fbac..5c601f9ea8a1ef20339a9d79024a38b9196d34e6 100644 (file)
@@ -106,7 +106,7 @@ static struct ldb_dn *ldb_dn_key(struct ldb_context *ldb,
        const struct ldb_attrib_handler *h;
        char *attr_folded;
 
-       attr_folded = ldb_attr_casefold(ldb, ldb, attr);
+       attr_folded = ldb_attr_casefold(ldb, attr);
        if (!attr_folded) {
                return NULL;
        }