From 6104f900863c688707809d42c5429a42d654d5fb Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 4 Feb 2006 07:57:57 +0000 Subject: [PATCH] r13335: Fix the build and add an utf8 safe ldb_hadler_fold function based on ldb_casefold --- source/lib/db_wrap.c | 4 +- source/lib/ldb/common/attrib_handlers.c | 56 ++++++++++++++++++------- source/lib/ldb/include/ldb.h | 1 + source/lib/ldb/ldb_tdb/ldb_index.c | 2 +- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/source/lib/db_wrap.c b/source/lib/db_wrap.c index f3358bff98b..8524f0da108 100644 --- a/source/lib/db_wrap.c +++ b/source/lib/db_wrap.c @@ -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); } diff --git a/source/lib/ldb/common/attrib_handlers.c b/source/lib/ldb/common/attrib_handlers.c index 2109e9b2da7..4b6a7af1eed 100644 --- a/source/lib/ldb/common/attrib_handlers.c +++ b/source/lib/ldb/common/attrib_handlers.c @@ -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, diff --git a/source/lib/ldb/include/ldb.h b/source/lib/ldb/include/ldb.h index 7c39aeeeb95..4e457b028fa 100644 --- a/source/lib/ldb/include/ldb.h +++ b/source/lib/ldb/include/ldb.h @@ -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); diff --git a/source/lib/ldb/ldb_tdb/ldb_index.c b/source/lib/ldb/ldb_tdb/ldb_index.c index ac3063ef283..5c601f9ea8a 100644 --- a/source/lib/ldb/ldb_tdb/ldb_index.c +++ b/source/lib/ldb/ldb_tdb/ldb_index.c @@ -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; } -- 2.34.1