From dbea04f585a30d001b574317c068cd03a4fa332b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 6 Dec 2011 14:57:58 +0100 Subject: sss_utf8_tolower utility function+unit tests --- src/util/sss_tc_utf8.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util/sss_utf8.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/util/sss_utf8.h | 5 +++++ src/util/util.c | 1 + src/util/util.h | 6 ++++++ 5 files changed, 123 insertions(+) create mode 100644 src/util/sss_tc_utf8.c (limited to 'src/util') diff --git a/src/util/sss_tc_utf8.c b/src/util/sss_tc_utf8.c new file mode 100644 index 00000000..6a976211 --- /dev/null +++ b/src/util/sss_tc_utf8.c @@ -0,0 +1,57 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2011 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "util/sss_utf8.h" + +char * +sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s) +{ + size_t nlen; + uint8_t *ret; + + ret = sss_tc_utf8_tolower(mem_ctx, (const uint8_t *) s, strlen(s), &nlen); + if (!ret) return NULL; + + ret = talloc_realloc(mem_ctx, ret, uint8_t, nlen+1); + if (!ret) return NULL; + + ret[nlen] = '\0'; + return (char *) ret; +} + +uint8_t * +sss_tc_utf8_tolower(TALLOC_CTX *mem_ctx, const uint8_t *s, size_t len, size_t *_nlen) +{ + uint8_t *lower; + uint8_t *ret; + size_t nlen; + + lower = sss_utf8_tolower(s, len, &nlen); + if (!lower) return NULL; + + ret = talloc_memdup(mem_ctx, lower, nlen); + sss_utf8_free(lower); + if (!ret) return NULL; + + *_nlen = nlen; + return ret; +} + diff --git a/src/util/sss_utf8.c b/src/util/sss_utf8.c index 4a98233b..7997a6df 100644 --- a/src/util/sss_utf8.c +++ b/src/util/sss_utf8.c @@ -23,6 +23,60 @@ #include "util/util.h" #include "sss_utf8.h" +#ifdef HAVE_LIBUNISTRING +void sss_utf8_free(void *ptr) +{ + return free(ptr); +} +#elif HAVE_GLIB2 +void sss_utf8_free(void *ptr) +{ + return g_free(ptr); +} +#else +#error No unicode library +#endif + +#ifdef HAVE_LIBUNISTRING +uint8_t *sss_utf8_tolower(const uint8_t *s, size_t len, size_t *_nlen) +{ + size_t llen; + uint8_t *lower; + + lower = u8_tolower(s, len, NULL, NULL, NULL, &llen); + if (!lower) return NULL; + + if (_nlen) *_nlen = llen; + return lower; +} +#elif HAVE_GLIB2 +uint8_t *sss_utf8_tolower(const uint8_t *s, size_t len, size_t *_nlen) +{ + gchar *glower; + size_t nlen; + uint8_t *lower; + + glower = g_utf8_strdown((const gchar *) s, len); + if (!glower) return NULL; + + /* strlen() is safe here because g_utf8_strdown() always null-terminates */ + nlen = strlen(glower); + + lower = g_malloc(nlen); + if (!lower) { + g_free(glower); + return NULL; + } + + memcpy(lower, glower, nlen); + g_free(glower); + if (_nlen) *_nlen = nlen; + return (uint8_t *) lower; +} +#else +#error No unicode library +#endif + #ifdef HAVE_LIBUNISTRING bool sss_utf8_check(const uint8_t *s, size_t n) { diff --git a/src/util/sss_utf8.h b/src/util/sss_utf8.h index 37dcff95..b7da7621 100644 --- a/src/util/sss_utf8.h +++ b/src/util/sss_utf8.h @@ -35,6 +35,11 @@ #define ENOMATCH -1 #endif +void sss_utf8_free(void *ptr); + +/* The result must be freed with sss_utf8_free() */ +uint8_t *sss_utf8_tolower(const uint8_t *s, size_t len, size_t *nlen); + bool sss_utf8_check(const uint8_t *s, size_t n); errno_t sss_utf8_case_eq(const uint8_t *s1, const uint8_t *s2); diff --git a/src/util/util.c b/src/util/util.c index b4b1b124..f525c915 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -23,6 +23,7 @@ #include "talloc.h" #include "util/util.h" +#include "util/sss_utf8.h" #include "dhash.h" /* split a string into an allocated array of strings. diff --git a/src/util/util.h b/src/util/util.h index 9a006471..4ff112b7 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -502,4 +502,10 @@ errno_t sss_filter_sanitize(TALLOC_CTX *mem_ctx, char * sss_escape_ip_address(TALLOC_CTX *mem_ctx, int family, const char *addr); +/* from sss_tc_utf8.c */ +char * +sss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s); +uint8_t * +sss_tc_utf8_tolower(TALLOC_CTX *mem_ctx, const uint8_t *s, size_t len, size_t *_nlen); + #endif /* __SSSD_UTIL_H__ */ -- cgit