2007-02-13 15:35:19 +00:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
Copyright 2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
PulseAudio 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
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2006-05-14 16:02:09 +00:00
|
|
|
/* This file is based on the GLIB utf8 validation functions. The
|
|
|
|
|
* original license text follows. */
|
|
|
|
|
|
|
|
|
|
/* gutf8.c - Operations on UTF-8 strings.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 1999 Tom Tromey
|
|
|
|
|
* Copyright (C) 2000 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-28 19:13:50 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2006-05-14 16:02:09 +00:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-18 08:19:07 +00:00
|
|
|
#include <errno.h>
|
2006-05-14 16:02:09 +00:00
|
|
|
#include <stdlib.h>
|
2006-05-15 12:44:44 +00:00
|
|
|
#include <inttypes.h>
|
2006-05-15 13:04:13 +00:00
|
|
|
#include <string.h>
|
2006-05-14 16:02:09 +00:00
|
|
|
|
2006-06-12 14:18:19 +00:00
|
|
|
#ifdef HAVE_ICONV
|
2006-05-18 08:19:07 +00:00
|
|
|
#include <iconv.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
|
2006-05-14 16:02:09 +00:00
|
|
|
#include "utf8.h"
|
|
|
|
|
|
2006-05-15 13:04:13 +00:00
|
|
|
#define FILTER_CHAR '_'
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
static inline bool is_unicode_valid(uint32_t ch) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-05-15 12:44:44 +00:00
|
|
|
if (ch >= 0x110000) /* End of unicode space */
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2006-05-15 12:44:44 +00:00
|
|
|
if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2006-05-15 12:44:44 +00:00
|
|
|
if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2006-05-15 12:44:44 +00:00
|
|
|
if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
return true;
|
2006-05-15 12:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
static inline bool is_continuation_char(uint8_t ch) {
|
2006-05-15 12:44:44 +00:00
|
|
|
if ((ch & 0xc0) != 0x80) /* 10xxxxxx */
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
|
|
|
|
return true;
|
2006-05-15 12:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void merge_continuation_char(uint32_t *u_ch, uint8_t ch) {
|
|
|
|
|
*u_ch <<= 6;
|
|
|
|
|
*u_ch |= ch & 0x3f;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 00:44:47 +00:00
|
|
|
static char* utf8_validate(const char *str, char *output) {
|
2006-05-15 12:44:44 +00:00
|
|
|
uint32_t val = 0;
|
|
|
|
|
uint32_t min = 0;
|
|
|
|
|
const uint8_t *p, *last;
|
2006-05-15 13:04:13 +00:00
|
|
|
int size;
|
|
|
|
|
uint8_t *o;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(str);
|
|
|
|
|
|
2006-05-16 00:04:14 +00:00
|
|
|
o = (uint8_t*) output;
|
|
|
|
|
for (p = (const uint8_t*) str; *p; p++) {
|
2006-05-15 13:04:13 +00:00
|
|
|
if (*p < 128) {
|
|
|
|
|
if (o)
|
2006-05-16 00:04:14 +00:00
|
|
|
*o = *p;
|
2006-05-15 13:04:13 +00:00
|
|
|
} else {
|
2006-05-15 12:44:44 +00:00
|
|
|
last = p;
|
|
|
|
|
|
|
|
|
|
if ((*p & 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
|
2006-05-15 13:04:13 +00:00
|
|
|
size = 2;
|
2006-05-15 12:44:44 +00:00
|
|
|
min = 128;
|
2008-08-19 22:39:54 +02:00
|
|
|
val = (uint32_t) (*p & 0x1e);
|
2006-05-15 12:44:44 +00:00
|
|
|
goto ONE_REMAINING;
|
|
|
|
|
} else if ((*p & 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
|
2006-05-15 13:04:13 +00:00
|
|
|
size = 3;
|
2006-05-15 12:44:44 +00:00
|
|
|
min = (1 << 11);
|
2008-08-19 22:39:54 +02:00
|
|
|
val = (uint32_t) (*p & 0x0f);
|
2006-05-15 12:44:44 +00:00
|
|
|
goto TWO_REMAINING;
|
|
|
|
|
} else if ((*p & 0xf8) == 0xf0) { /* 11110xxx four-char seq */
|
2006-05-15 13:04:13 +00:00
|
|
|
size = 4;
|
2006-05-15 12:44:44 +00:00
|
|
|
min = (1 << 16);
|
2008-08-19 22:39:54 +02:00
|
|
|
val = (uint32_t) (*p & 0x07);
|
2009-09-08 23:46:23 +02:00
|
|
|
} else
|
2006-05-15 12:44:44 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
if (!is_continuation_char(*p))
|
|
|
|
|
goto error;
|
|
|
|
|
merge_continuation_char(&val, *p);
|
|
|
|
|
|
|
|
|
|
TWO_REMAINING:
|
|
|
|
|
p++;
|
|
|
|
|
if (!is_continuation_char(*p))
|
|
|
|
|
goto error;
|
|
|
|
|
merge_continuation_char(&val, *p);
|
|
|
|
|
|
|
|
|
|
ONE_REMAINING:
|
|
|
|
|
p++;
|
|
|
|
|
if (!is_continuation_char(*p))
|
|
|
|
|
goto error;
|
|
|
|
|
merge_continuation_char(&val, *p);
|
|
|
|
|
|
|
|
|
|
if (val < min)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (!is_unicode_valid(val))
|
|
|
|
|
goto error;
|
2006-05-15 13:04:13 +00:00
|
|
|
|
|
|
|
|
if (o) {
|
2008-08-19 22:39:54 +02:00
|
|
|
memcpy(o, last, (size_t) size);
|
2009-09-08 23:49:42 +02:00
|
|
|
o += size;
|
2006-05-15 13:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (o) {
|
|
|
|
|
*o = FILTER_CHAR;
|
2006-05-16 00:44:47 +00:00
|
|
|
p = last; /* We retry at the next character */
|
2006-05-15 13:04:13 +00:00
|
|
|
} else
|
|
|
|
|
goto failure;
|
2006-05-15 12:44:44 +00:00
|
|
|
}
|
2006-05-16 00:04:14 +00:00
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
|
o++;
|
2006-05-14 16:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-15 13:04:13 +00:00
|
|
|
if (o) {
|
|
|
|
|
*o = '\0';
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 00:44:47 +00:00
|
|
|
return (char*) str;
|
2006-05-15 12:44:44 +00:00
|
|
|
|
2006-05-15 13:04:13 +00:00
|
|
|
failure:
|
2006-05-15 12:44:44 +00:00
|
|
|
return NULL;
|
2006-05-14 16:02:09 +00:00
|
|
|
}
|
2006-05-15 13:04:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
char* pa_utf8_valid (const char *str) {
|
2006-05-15 13:04:13 +00:00
|
|
|
return utf8_validate(str, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 00:44:47 +00:00
|
|
|
char* pa_utf8_filter (const char *str) {
|
2006-05-15 13:04:13 +00:00
|
|
|
char *new_str;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(str);
|
2008-08-19 22:39:54 +02:00
|
|
|
new_str = pa_xmalloc(strlen(str) + 1);
|
2006-05-15 13:04:13 +00:00
|
|
|
return utf8_validate(str, new_str);
|
|
|
|
|
}
|
2006-05-18 08:19:07 +00:00
|
|
|
|
2006-06-12 14:18:19 +00:00
|
|
|
#ifdef HAVE_ICONV
|
2006-05-18 08:19:07 +00:00
|
|
|
|
|
|
|
|
static char* iconv_simple(const char *str, const char *to, const char *from) {
|
|
|
|
|
char *new_str;
|
|
|
|
|
size_t len, inlen;
|
|
|
|
|
iconv_t cd;
|
2006-06-12 14:18:19 +00:00
|
|
|
ICONV_CONST char *inbuf;
|
|
|
|
|
char *outbuf;
|
2006-05-18 08:19:07 +00:00
|
|
|
size_t res, inbytes, outbytes;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(str);
|
|
|
|
|
pa_assert(to);
|
|
|
|
|
pa_assert(from);
|
|
|
|
|
|
2006-05-18 08:19:07 +00:00
|
|
|
cd = iconv_open(to, from);
|
|
|
|
|
if (cd == (iconv_t)-1)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
inlen = len = strlen(str) + 1;
|
2008-08-19 22:39:54 +02:00
|
|
|
new_str = pa_xmalloc(len);
|
2006-05-18 08:19:07 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (;;) {
|
|
|
|
|
inbuf = (ICONV_CONST char*) str; /* Brain dead prototype for iconv() */
|
2006-05-18 08:19:07 +00:00
|
|
|
inbytes = inlen;
|
|
|
|
|
outbuf = new_str;
|
|
|
|
|
outbytes = len;
|
|
|
|
|
|
|
|
|
|
res = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
|
|
|
|
|
|
|
|
|
|
if (res != (size_t)-1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (errno != E2BIG) {
|
|
|
|
|
pa_xfree(new_str);
|
|
|
|
|
new_str = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(inbytes != 0);
|
2006-05-18 08:19:07 +00:00
|
|
|
|
|
|
|
|
len += inbytes;
|
|
|
|
|
new_str = pa_xrealloc(new_str, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
|
|
|
|
return new_str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* pa_utf8_to_locale (const char *str) {
|
|
|
|
|
return iconv_simple(str, "", "UTF-8");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* pa_locale_to_utf8 (const char *str) {
|
|
|
|
|
return iconv_simple(str, "UTF-8", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
char* pa_utf8_to_locale (const char *str) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(str);
|
2011-10-10 09:27:16 +02:00
|
|
|
|
|
|
|
|
return pa_ascii_filter(str);
|
2006-05-18 08:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* pa_locale_to_utf8 (const char *str) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(str);
|
2011-10-10 09:27:16 +02:00
|
|
|
|
|
|
|
|
if (pa_utf8_valid(str))
|
|
|
|
|
return pa_xstrdup(str);
|
|
|
|
|
|
2006-05-18 08:19:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
2009-02-04 17:11:56 +01:00
|
|
|
|
|
|
|
|
char *pa_ascii_valid(const char *str) {
|
|
|
|
|
const char *p;
|
|
|
|
|
pa_assert(str);
|
|
|
|
|
|
|
|
|
|
for (p = str; *p; p++)
|
|
|
|
|
if ((unsigned char) *p >= 128)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return (char*) str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pa_ascii_filter(const char *str) {
|
|
|
|
|
char *r, *s, *d;
|
|
|
|
|
pa_assert(str);
|
|
|
|
|
|
|
|
|
|
r = pa_xstrdup(str);
|
|
|
|
|
|
|
|
|
|
for (s = r, d = r; *s; s++)
|
|
|
|
|
if ((unsigned char) *s < 128)
|
|
|
|
|
*(d++) = *s;
|
|
|
|
|
|
|
|
|
|
*d = 0;
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|