2010-03-09 Zoltan Varga <vargaz@gmail.com>
[mono/afaerber.git] / eglib / src / gunicode.c
blobf7e156cac81478ca215f1a7a1d04de825342547d
1 /*
2 * gunicode.c: Some Unicode routines
4 * Author:
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
9 * utf8 validation code came from:
10 * libxml2-2.6.26 licensed under the MIT X11 license
12 * Authors credit in libxml's string.c:
13 * William Brack <wbrack@mmm.com.hk>
14 * daniel@veillard.com
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #include <config.h>
37 #include <stdio.h>
38 #include <glib.h>
39 #include <unicode-data.h>
40 #include <errno.h>
41 #ifdef _MSC_VER
42 /* FIXME */
43 #define CODESET 1
44 #include <Windows.h>
46 typedef int iconv_t;
47 #else
48 #ifdef HAVE_LANGINFO_H
49 #include <langinfo.h>
50 #endif
51 #ifdef HAVE_ICONV_H
52 #include <iconv.h>
53 #endif
54 #endif
56 static char *my_charset;
57 static gboolean is_utf8;
60 * Character set conversion
63 * Index into the table below with the first byte of a UTF-8 sequence to
64 * get the number of trailing bytes that are supposed to follow it.
65 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
66 * left as-is for anyone who may want to do such conversion, which was
67 * allowed in earlier algorithms.
69 const gchar g_trailingBytesForUTF8 [256] = {
70 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
71 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
72 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
73 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
74 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
75 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
76 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
77 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,0,0
81 * Magic values subtracted from a buffer value during UTF8 conversion.
82 * This table contains as many values as there might be trailing bytes
83 * in a UTF-8 sequence.
85 static const gulong offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
86 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
88 GUnicodeType
89 g_unichar_type (gunichar c)
91 int i;
93 guint16 cp = (guint16) c;
94 for (i = 0; i < unicode_category_ranges_count; i++) {
95 if (cp < unicode_category_ranges [i].start)
96 continue;
97 if (unicode_category_ranges [i].end <= cp)
98 continue;
99 return unicode_category [i] [cp - unicode_category_ranges [i].start];
103 // 3400-4DB5: OtherLetter
104 // 4E00-9FC3: OtherLetter
105 // AC00-D7A3: OtherLetter
106 // D800-DFFF: OtherSurrogate
107 // E000-F8FF: OtherPrivateUse
108 // 20000-2A6D6 OtherLetter
109 // F0000-FFFFD OtherPrivateUse
110 // 100000-10FFFD OtherPrivateUse
112 if (0x3400 <= cp && cp < 0x4DB5)
113 return G_UNICODE_OTHER_LETTER;
114 if (0x4E00 <= cp && cp < 0x9FC3)
115 return G_UNICODE_OTHER_LETTER;
116 if (0xAC00<= cp && cp < 0xD7A3)
117 return G_UNICODE_OTHER_LETTER;
118 if (0xD800 <= cp && cp < 0xDFFF)
119 return G_UNICODE_SURROGATE;
120 if (0xE000 <= cp && cp < 0xF8FF)
121 return G_UNICODE_PRIVATE_USE;
122 /* since the argument is UTF-16, we cannot check beyond FFFF */
124 /* It should match any of above */
125 return 0;
128 gunichar
129 g_unichar_case (gunichar c, gboolean upper)
131 gint8 i, i2;
132 guint32 cp = (guint32) c, v;
134 for (i = 0; i < simple_case_map_ranges_count; i++) {
135 if (cp < simple_case_map_ranges [i].start)
136 return c;
137 if (simple_case_map_ranges [i].end <= cp)
138 continue;
139 if (c < 0x10000) {
140 const guint16 *tab = upper ? simple_upper_case_mapping_lowarea [i] : simple_lower_case_mapping_lowarea [i];
141 v = tab [cp - simple_case_map_ranges [i].start];
142 } else {
143 const guint32 *tab;
144 i2 = (gint8)(i - (upper ? simple_upper_case_mapping_lowarea_table_count : simple_lower_case_mapping_lowarea_table_count));
145 tab = upper ? simple_upper_case_mapping_higharea [i2] : simple_lower_case_mapping_higharea [i2];
146 v = tab [cp - simple_case_map_ranges [i].start];
148 return v != 0 ? (gunichar) v : c;
150 return c;
153 gunichar
154 g_unichar_toupper (gunichar c)
156 return g_unichar_case (c, TRUE);
159 gunichar
160 g_unichar_tolower (gunichar c)
162 return g_unichar_case (c, FALSE);
165 gunichar
166 g_unichar_totitle (gunichar c)
168 guint8 i;
169 guint32 cp;
171 cp = (guint32) c;
172 for (i = 0; i < simple_titlecase_mapping_count; i++) {
173 if (simple_titlecase_mapping [i].codepoint == cp)
174 return simple_titlecase_mapping [i].title;
175 if (simple_titlecase_mapping [i].codepoint > cp)
176 /* it is ordered, hence no more match */
177 break;
179 return g_unichar_toupper (c);
182 gboolean
183 g_unichar_isxdigit (gunichar c)
185 return (g_unichar_xdigit_value (c) != -1);
189 gint
190 g_unichar_xdigit_value (gunichar c)
192 if (c >= 0x30 && c <= 0x39) /*0-9*/
193 return (c - 0x30);
194 if (c >= 0x41 && c <= 0x46) /*A-F*/
195 return (c - 0x37);
196 if (c >= 0x61 && c <= 0x66) /*a-f*/
197 return (c - 0x57);
198 return -1;
201 gchar *
202 g_convert (const gchar *str, gssize len,
203 const gchar *to_codeset, const gchar *from_codeset,
204 gsize *bytes_read, gsize *bytes_written, GError **error)
206 char *result = NULL;
207 #ifdef G_OS_WIN32
208 #elif HAVE_ICONV_H
209 iconv_t convertor;
210 char *buffer, *output;
211 const char *strptr = (const char *) str;
212 size_t str_len = len == -1 ? strlen (str) : len;
213 size_t buffer_size;
214 size_t left, out_left;
216 convertor = iconv_open (to_codeset, from_codeset);
217 if (convertor == (iconv_t) -1){
218 *bytes_written = 0;
219 *bytes_read = 0;
220 return NULL;
223 buffer_size = str_len + 1 + 8;
224 buffer = g_malloc (buffer_size);
225 out_left = str_len;
226 output = buffer;
227 left = str_len;
228 while (left > 0){
229 int res = iconv (convertor, (char **) &strptr, &left, &output, &out_left);
230 if (res == (size_t) -1){
231 if (errno == E2BIG){
232 char *n;
233 size_t extra_space = 8 + left;
234 size_t output_used = output - buffer;
236 buffer_size += extra_space;
238 n = g_realloc (buffer, buffer_size);
240 if (n == NULL){
241 if (error != NULL)
242 *error = g_error_new (NULL, G_CONVERT_ERROR_FAILED, "No memory left");
243 g_free (buffer);
244 result = NULL;
245 goto leave;
247 buffer = n;
248 out_left += extra_space;
249 output = buffer + output_used;
250 } else if (errno == EILSEQ){
251 if (error != NULL)
252 *error = g_error_new (NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "Invalid multi-byte sequence on input");
253 result = NULL;
254 g_free (buffer);
255 goto leave;
256 } else if (errno == EINVAL){
257 if (error != NULL)
258 *error = g_error_new (NULL, G_CONVERT_ERROR_PARTIAL_INPUT, "Partial character sequence");
259 result = NULL;
260 g_free (buffer);
261 goto leave;
265 if (bytes_read != NULL)
266 *bytes_read = strptr - str;
267 if (bytes_written != NULL)
268 *bytes_written = output - buffer;
269 *output = 0;
270 result = buffer;
271 leave:
272 iconv_close (convertor);
273 #endif
274 return result;
278 * This is broken, and assumes an UTF8 system, but will do for eglib's first user
280 gchar *
281 g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
283 char *res;
285 if (len == -1)
286 len = strlen (utf8string);
288 res = g_malloc (len + 1);
289 g_strlcpy (res, utf8string, len + 1);
290 return res;
293 gboolean
294 g_get_charset (G_CONST_RETURN char **charset)
296 #ifdef G_OS_WIN32
297 static char buf[14];
298 sprintf (buf, "CP%u", GetACP ());
299 *charset = buf;
300 is_utf8 = FALSE;
301 #else
302 if (my_charset == NULL){
303 #if HAVE_LANGINFO_H
304 my_charset = g_strdup (nl_langinfo (CODESET));
305 #else
306 my_charset = g_strdup ("UTF-8");
307 #endif
308 is_utf8 = strcmp (my_charset, "UTF-8") == 0;
311 if (charset != NULL)
312 *charset = my_charset;
314 #endif
315 return is_utf8;
318 gchar *
319 g_locale_to_utf8 (const gchar *opsysstring, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
321 g_get_charset (NULL);
323 return g_convert (opsysstring, len, "UTF-8", my_charset, bytes_read, bytes_written, error);
326 gchar *
327 g_locale_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
329 g_get_charset (NULL);
331 return g_convert (utf8string, len, my_charset, "UTF-8", bytes_read, bytes_written, error);
334 * g_utf8_validate
335 * @utf: Pointer to putative UTF-8 encoded string.
337 * Checks @utf for being valid UTF-8. @utf is assumed to be
338 * null-terminated. This function is not super-strict, as it will
339 * allow longer UTF-8 sequences than necessary. Note that Java is
340 * capable of producing these sequences if provoked. Also note, this
341 * routine checks for the 4-byte maximum size, but does not check for
342 * 0x10ffff maximum value.
344 * Return value: true if @utf is valid.
346 gboolean
347 g_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
349 gssize byteCount = 0;
350 gboolean retVal = TRUE;
351 gboolean lastRet = TRUE;
352 guchar* ptr = (guchar*) str;
353 guint length;
354 guchar a;
355 guchar* srcPtr;
356 if (max_len == 0)
357 return 0;
358 else if (max_len < 0)
359 byteCount = max_len;
360 while (*ptr != 0 && byteCount <= max_len) {
361 length = g_trailingBytesForUTF8 [*ptr] + 1;
362 srcPtr = (guchar*) ptr + length;
363 switch (length) {
364 default: retVal = FALSE;
365 /* Everything else falls through when "TRUE"... */
366 case 4: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
367 if ((a == (guchar) 0xBF || a == (guchar) 0xBE) && *(srcPtr-1) == (guchar) 0xBF) {
368 if (*(srcPtr-2) == (guchar) 0x8F || *(srcPtr-2) == (guchar) 0x9F ||
369 *(srcPtr-2) == (guchar) 0xAF || *(srcPtr-2) == (guchar) 0xBF)
370 retVal = FALSE;
372 case 3: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
373 case 2: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
375 switch (*ptr) {
376 /* no fall-through in this inner switch */
377 case 0xE0: if (a < (guchar) 0xA0) retVal = FALSE; break;
378 case 0xED: if (a > (guchar) 0x9F) retVal = FALSE; break;
379 case 0xEF: if (a == (guchar)0xB7 && (*(srcPtr+1) > (guchar) 0x8F && *(srcPtr+1) < 0xB0)) retVal = FALSE;
380 if (a == (guchar)0xBF && (*(srcPtr+1) == (guchar) 0xBE || *(srcPtr+1) == 0xBF)) retVal = FALSE; break;
381 case 0xF0: if (a < (guchar) 0x90) retVal = FALSE; break;
382 case 0xF4: if (a > (guchar) 0x8F) retVal = FALSE; break;
383 default: if (a < (guchar) 0x80) retVal = FALSE;
386 case 1: if (*ptr >= (guchar ) 0x80 && *ptr < (guchar) 0xC2) retVal = FALSE;
388 if (*ptr > (guchar) 0xF4)
389 retVal = FALSE;
390 //If the string is invalid, set the end to the invalid byte.
391 if (!retVal && lastRet) {
392 if (end != NULL)
393 *end = (gchar*) ptr;
394 lastRet = FALSE;
396 ptr += length;
397 if(max_len > 0)
398 byteCount += length;
400 if (retVal && end != NULL)
401 *end = (gchar*) ptr;
402 return retVal;
405 * g_utf8_get_char
406 * @src: Pointer to UTF-8 encoded character.
408 * Return value: UTF-16 value of @src
410 gunichar
411 g_utf8_get_char (const gchar *src)
413 gunichar ch = 0;
414 guchar* ptr = (guchar*) src;
415 gushort extraBytesToRead = g_trailingBytesForUTF8 [*ptr];
417 switch (extraBytesToRead) {
418 case 5: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
419 case 4: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
420 case 3: ch += *ptr++; ch <<= 6;
421 case 2: ch += *ptr++; ch <<= 6;
422 case 1: ch += *ptr++; ch <<= 6;
423 case 0: ch += *ptr;
425 ch -= offsetsFromUTF8 [extraBytesToRead];
426 return ch;
428 glong
429 g_utf8_strlen (const gchar *str, gssize max)
431 gssize byteCount = 0;
432 guchar* ptr = (guchar*) str;
433 glong length = 0;
434 if (max == 0)
435 return 0;
436 else if (max < 0)
437 byteCount = max;
438 while (*ptr != 0 && byteCount <= max) {
439 gssize cLen = g_trailingBytesForUTF8 [*ptr] + 1;
440 if (max > 0 && (byteCount + cLen) > max)
441 return length;
442 ptr += cLen;
443 length++;
444 if (max > 0)
445 byteCount += cLen;
447 return length;