Add unicode_fixup_string() to check & reencode UTF-8 strings

This commit is contained in:
Julien BLACHE 2010-06-21 17:50:09 +02:00
parent 9186ebaa89
commit da0c10a392
2 changed files with 32 additions and 0 deletions

View File

@ -33,6 +33,9 @@
#include <limits.h> #include <limits.h>
#include <sys/param.h> #include <sys/param.h>
#include <unistr.h>
#include <uniconv.h>
#include "logger.h" #include "logger.h"
#include "misc.h" #include "misc.h"
@ -274,6 +277,32 @@ m_realpath(const char *pathname)
return ret; return ret;
} }
char *
unicode_fixup_string(char *str)
{
uint8_t *ret;
size_t len;
if (!str)
return NULL;
len = strlen(str);
/* String is valid UTF-8 */
if (!u8_check((uint8_t *)str, len))
return str;
ret = u8_conv_from_encoding("ascii", iconveh_question_mark, str, len, NULL, NULL, &len);
if (!ret)
{
DPRINTF(E_LOG, L_MISC, "Could not convert string '%s' to UTF-8: %s\n", str, strerror(errno));
return NULL;
}
return (char *)ret;
}
uint32_t uint32_t
djb_hash(void *data, size_t len) djb_hash(void *data, size_t len)
{ {

View File

@ -25,6 +25,9 @@ safe_hextou64(const char *str, uint64_t *val);
char * char *
m_realpath(const char *pathname); m_realpath(const char *pathname);
char *
unicode_fixup_string(char *str);
uint32_t uint32_t
djb_hash(void *data, size_t len); djb_hash(void *data, size_t len);