]>
code.delx.au - pulseaudio/blob - src/pulse/utf8.c
2ac2d10635e2f68a950a6ec61f111a3e3094836b
4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 /* This file is based on the GLIB utf8 validation functions. The
26 * original license text follows. */
28 /* gutf8.c - Operations on UTF-8 strings.
30 * Copyright (C) 1999 Tom Tromey
31 * Copyright (C) 2000 Red Hat, Inc.
33 * This library is free software; you can redistribute it and/or
34 * modify it under the terms of the GNU Lesser General Public
35 * License as published by the Free Software Foundation; either
36 * version 2 of the License, or (at your option) any later version.
38 * This library is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * Lesser General Public License for more details.
43 * You should have received a copy of the GNU Lesser General Public
44 * License along with this library; if not, write to the
45 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
46 * Boston, MA 02111-1307, USA.
66 #define FILTER_CHAR '_'
68 static inline int is_unicode_valid(uint32_t ch
) {
69 if (ch
>= 0x110000) /* End of unicode space */
71 if ((ch
& 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
73 if ((ch
>= 0xFDD0) && (ch
<= 0xFDEF)) /* Reserved */
75 if ((ch
& 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
80 static inline int is_continuation_char(uint8_t ch
) {
81 if ((ch
& 0xc0) != 0x80) /* 10xxxxxx */
86 static inline void merge_continuation_char(uint32_t *u_ch
, uint8_t ch
) {
91 static char* utf8_validate(const char *str
, char *output
) {
94 const uint8_t *p
, *last
;
98 o
= (uint8_t*) output
;
99 for (p
= (const uint8_t*) str
; *p
; p
++) {
106 if ((*p
& 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
111 } else if ((*p
& 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
116 } else if ((*p
& 0xf8) == 0xf0) { /* 11110xxx four-char seq */
126 if (!is_continuation_char(*p
))
128 merge_continuation_char(&val
, *p
);
132 if (!is_continuation_char(*p
))
134 merge_continuation_char(&val
, *p
);
138 if (!is_continuation_char(*p
))
140 merge_continuation_char(&val
, *p
);
145 if (!is_unicode_valid(val
))
149 memcpy(o
, last
, size
);
161 p
= last
; /* We retry at the next character */
181 const char* pa_utf8_valid (const char *str
) {
182 return utf8_validate(str
, NULL
);
185 char* pa_utf8_filter (const char *str
) {
188 new_str
= pa_xnew(char, strlen(str
) + 1);
190 return utf8_validate(str
, new_str
);
195 static char* iconv_simple(const char *str
, const char *to
, const char *from
) {
200 ICONV_CONST
char *inbuf
;
202 size_t res
, inbytes
, outbytes
;
204 cd
= iconv_open(to
, from
);
205 if (cd
== (iconv_t
)-1)
208 inlen
= len
= strlen(str
) + 1;
209 new_str
= pa_xmalloc(len
);
213 inbuf
= (ICONV_CONST
char*)str
; /* Brain dead prototype for iconv() */
218 res
= iconv(cd
, &inbuf
, &inbytes
, &outbuf
, &outbytes
);
220 if (res
!= (size_t)-1)
223 if (errno
!= E2BIG
) {
229 assert(inbytes
!= 0);
232 new_str
= pa_xrealloc(new_str
, len
);
241 char* pa_utf8_to_locale (const char *str
) {
242 return iconv_simple(str
, "", "UTF-8");
245 char* pa_locale_to_utf8 (const char *str
) {
246 return iconv_simple(str
, "UTF-8", "");
251 char* pa_utf8_to_locale (const char *str
) {
255 char* pa_locale_to_utf8 (const char *str
) {