Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulse / utf8.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 /* This file is based on the GLIB utf8 validation functions. The
26  * original license text follows. */
27
28 /* gutf8.c - Operations on UTF-8 strings.
29  *
30  * Copyright (C) 1999 Tom Tromey
31  * Copyright (C) 2000 Red Hat, Inc.
32  *
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.
37  *
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.
42  *
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.
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #include <assert.h>
54 #include <errno.h>
55 #include <stdlib.h>
56 #include <inttypes.h>
57 #include <string.h>
58
59 #ifdef HAVE_ICONV
60 #include <iconv.h>
61 #endif
62
63 #include "utf8.h"
64 #include "xmalloc.h"
65
66 #define FILTER_CHAR '_'
67
68 static inline int is_unicode_valid(uint32_t ch) {
69     if (ch >= 0x110000) /* End of unicode space */
70         return 0;
71     if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
72         return 0;
73     if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
74         return 0;
75     if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
76         return 0;
77     return 1;
78 }
79
80 static inline int is_continuation_char(uint8_t ch) {
81     if ((ch & 0xc0) != 0x80) /* 10xxxxxx */
82         return 0;
83     return 1;
84 }
85
86 static inline void merge_continuation_char(uint32_t *u_ch, uint8_t ch) {
87     *u_ch <<= 6;
88     *u_ch |= ch & 0x3f;
89 }
90
91 static char* utf8_validate(const char *str, char *output) {
92     uint32_t val = 0;
93     uint32_t min = 0;
94     const uint8_t *p, *last;
95     int size;
96     uint8_t *o;
97
98     o = (uint8_t*) output;
99     for (p = (const uint8_t*) str; *p; p++) {
100         if (*p < 128) {
101             if (o)
102                 *o = *p;
103         } else {
104             last = p;
105
106             if ((*p & 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
107                 size = 2;
108                 min = 128;
109                 val = *p & 0x1e;
110                 goto ONE_REMAINING;
111             } else if ((*p & 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
112                 size = 3;
113                 min = (1 << 11);
114                 val = *p & 0x0f;
115                 goto TWO_REMAINING;
116             } else if ((*p & 0xf8) == 0xf0) { /* 11110xxx four-char seq */
117                 size = 4;
118                 min = (1 << 16);
119                 val = *p & 0x07;
120             } else {
121                 size = 1;
122                 goto error;
123             }
124
125             p++;
126             if (!is_continuation_char(*p))
127                 goto error;
128             merge_continuation_char(&val, *p);
129
130 TWO_REMAINING:
131             p++;
132             if (!is_continuation_char(*p))
133                 goto error;
134             merge_continuation_char(&val, *p);
135
136 ONE_REMAINING:
137             p++;
138             if (!is_continuation_char(*p))
139                 goto error;
140             merge_continuation_char(&val, *p);
141
142             if (val < min)
143                 goto error;
144
145             if (!is_unicode_valid(val))
146                 goto error;
147
148             if (o) {
149                 memcpy(o, last, size);
150                 o += size - 1;
151             }
152
153             if (o)
154                 o++;
155
156             continue;
157
158 error:
159             if (o) {
160                 *o = FILTER_CHAR;
161                 p = last; /* We retry at the next character */
162             } else
163                 goto failure;
164         }
165
166         if (o)
167             o++;
168     }
169
170     if (o) {
171         *o = '\0';
172         return output;
173     }
174
175     return (char*) str;
176
177 failure:
178     return NULL;
179 }
180
181 const char* pa_utf8_valid (const char *str) {
182     return utf8_validate(str, NULL);
183 }
184
185 char* pa_utf8_filter (const char *str) {
186     char *new_str;
187
188     new_str = pa_xnew(char, strlen(str) + 1);
189
190     return utf8_validate(str, new_str);
191 }
192
193 #ifdef HAVE_ICONV
194
195 static char* iconv_simple(const char *str, const char *to, const char *from) {
196     char *new_str;
197     size_t len, inlen;
198
199     iconv_t cd;
200     ICONV_CONST char *inbuf;
201     char *outbuf;
202     size_t res, inbytes, outbytes;
203
204     cd = iconv_open(to, from);
205     if (cd == (iconv_t)-1)
206         return NULL;
207
208     inlen = len = strlen(str) + 1;
209     new_str = pa_xmalloc(len);
210     assert(new_str);
211
212     while (1) {
213         inbuf = (ICONV_CONST char*)str; /* Brain dead prototype for iconv() */
214         inbytes = inlen;
215         outbuf = new_str;
216         outbytes = len;
217
218         res = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
219
220         if (res != (size_t)-1)
221             break;
222
223         if (errno != E2BIG) {
224             pa_xfree(new_str);
225             new_str = NULL;
226             break;
227         }
228
229         assert(inbytes != 0);
230
231         len += inbytes;
232         new_str = pa_xrealloc(new_str, len);
233         assert(new_str);
234     }
235
236     iconv_close(cd);
237
238     return new_str;
239 }
240
241 char* pa_utf8_to_locale (const char *str) {
242     return iconv_simple(str, "", "UTF-8");
243 }
244
245 char* pa_locale_to_utf8 (const char *str) {
246     return iconv_simple(str, "UTF-8", "");
247 }
248
249 #else
250
251 char* pa_utf8_to_locale (const char *str) {
252     return NULL;
253 }
254
255 char* pa_locale_to_utf8 (const char *str) {
256     return NULL;
257 }
258
259 #endif