X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcutf8.c;h=1faf14c9deb6422a459cf7d597d77cc03a93c891;hb=e9374395ec9747bddd3ea0bfd3e5a17717e97b31;hp=9b3645586057bfa9a07eca28da71bf0e6b178a39;hpb=c27a4ecc3e616e8a5ee09b8ca888ed6ff3e501f7;p=silc.git diff --git a/lib/silcutil/silcutf8.c b/lib/silcutil/silcutf8.c index 9b364558..1faf14c9 100644 --- a/lib/silcutil/silcutf8.c +++ b/lib/silcutil/silcutf8.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2004 - 2005 Pekka Riikonen + Copyright (C) 2004 - 2007 Pekka Riikonen 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 @@ -17,7 +17,7 @@ */ -#include "silcincludes.h" +#include "silc.h" #include "silcutf8.h" /* Encodes the string `bin' of which encoding is `bin_encoding' to the @@ -491,7 +491,7 @@ SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len, if (enclen + 3 > bin_size) return 0; bin[enclen] = '\\'; - snprintf(bin + enclen + 1, 3, "%02X", cv); + silc_snprintf(bin + enclen + 1, 3, "%02X", cv); } enclen += 3; continue; @@ -515,6 +515,67 @@ SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len, return enclen; } +/* UTF-8 to wide characters */ + +SilcUInt32 silc_utf8_c2w(const unsigned char *utf8, SilcUInt32 utf8_len, + SilcUInt16 *utf8_wide, SilcUInt32 utf8_wide_size) +{ + unsigned char *tmp; + SilcUInt32 tmp_len; + int i, k; + + tmp_len = silc_utf8_decoded_len(utf8, utf8_len, SILC_STRING_BMP); + if (!tmp_len) + return 0; + + if (utf8_wide_size < tmp_len / 2) + return 0; + + memset(utf8_wide, 0, utf8_wide_size * 2); + + tmp = silc_malloc(tmp_len); + if (!tmp) + return 0; + + silc_utf8_decode(utf8, utf8_len, SILC_STRING_BMP, tmp, tmp_len); + + for (i = 0, k = 0; i < tmp_len; i += 2, k++) + SILC_GET16_MSB(utf8_wide[k], tmp + i); + + silc_free(tmp); + return k + 1; +} + +/* Wide characters to UTF-8 */ + +SilcUInt32 silc_utf8_w2c(const SilcUInt16 *wide_str, + SilcUInt32 wide_str_len, + unsigned char *utf8, SilcUInt32 utf8_size) + +{ + unsigned char *tmp; + SilcUInt32 tmp_len; + int i, k; + + if (utf8_size < wide_str_len * 2) + return 0; + + memset(utf8, 0, utf8_size); + + tmp = silc_malloc(wide_str_len * 2); + if (!tmp) + return 0; + + for (i = 0, k = 0; i < wide_str_len; i += 2, k++) + SILC_PUT16_MSB(wide_str[k], tmp + i); + + tmp_len = silc_utf8_encode(tmp, wide_str_len * 2, SILC_STRING_BMP, + utf8, utf8_size); + + silc_free(tmp); + return tmp_len; +} + /* Returns the length of UTF-8 encoded string if the `bin' of encoding of `bin_encoding' is encoded with silc_utf8_encode. */ @@ -566,14 +627,14 @@ SilcBool silc_utf8_strncasecmp(const char *s1, const char *s2, SilcUInt32 n) return TRUE; /* Casefold and normalize */ - status = silc_stringprep(s1, strlen(s1), SILC_STRING_UTF8, + status = silc_stringprep(s1, n, SILC_STRING_UTF8, SILC_IDENTIFIERC_PREP, 0, &s1u, &s1u_len, SILC_STRING_UTF8); if (status != SILC_STRINGPREP_OK) return FALSE; /* Casefold and normalize */ - status = silc_stringprep(s2, strlen(s2), SILC_STRING_UTF8, + status = silc_stringprep(s2, n, SILC_STRING_UTF8, SILC_IDENTIFIERC_PREP, 0, &s2u, &s2u_len, SILC_STRING_UTF8); if (status != SILC_STRINGPREP_OK)