2 * gunicode.c: Some Unicode routines
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>
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.
39 #include <unicode-data.h>
42 #if defined(_MSC_VER) || defined(G_OS_WIN32)
50 # ifdef HAVE_LANGINFO_H
51 # include <langinfo.h>
56 # ifdef HAVE_LOCALCHARSET_H
57 # include <localcharset.h>
61 static char *my_charset;
62 static gboolean is_utf8;
65 * Character set conversion
68 * Index into the table below with the first byte of a UTF-8 sequence to
69 * get the number of trailing bytes that are supposed to follow it.
70 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
71 * left as-is for anyone who may want to do such conversion, which was
72 * allowed in earlier algorithms.
74 const gchar g_trailingBytesForUTF8 [256] = {
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 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,
77 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,
78 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,
79 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,
80 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,
81 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,
82 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
86 * Magic values subtracted from a buffer value during UTF8 conversion.
87 * This table contains as many values as there might be trailing bytes
88 * in a UTF-8 sequence.
90 static const gulong offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
91 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
94 g_unichar_type (gunichar c)
98 guint16 cp = (guint16) c;
99 for (i = 0; i < unicode_category_ranges_count; i++) {
100 if (cp < unicode_category_ranges [i].start)
102 if (unicode_category_ranges [i].end <= cp)
104 return unicode_category [i] [cp - unicode_category_ranges [i].start];
108 // 3400-4DB5: OtherLetter
109 // 4E00-9FC3: OtherLetter
110 // AC00-D7A3: OtherLetter
111 // D800-DFFF: OtherSurrogate
112 // E000-F8FF: OtherPrivateUse
113 // 20000-2A6D6 OtherLetter
114 // F0000-FFFFD OtherPrivateUse
115 // 100000-10FFFD OtherPrivateUse
117 if (0x3400 <= cp && cp < 0x4DB5)
118 return G_UNICODE_OTHER_LETTER;
119 if (0x4E00 <= cp && cp < 0x9FC3)
120 return G_UNICODE_OTHER_LETTER;
121 if (0xAC00<= cp && cp < 0xD7A3)
122 return G_UNICODE_OTHER_LETTER;
123 if (0xD800 <= cp && cp < 0xDFFF)
124 return G_UNICODE_SURROGATE;
125 if (0xE000 <= cp && cp < 0xF8FF)
126 return G_UNICODE_PRIVATE_USE;
127 /* since the argument is UTF-16, we cannot check beyond FFFF */
129 /* It should match any of above */
134 g_unichar_case (gunichar c, gboolean upper)
137 guint32 cp = (guint32) c, v;
139 for (i = 0; i < simple_case_map_ranges_count; i++) {
140 if (cp < simple_case_map_ranges [i].start)
142 if (simple_case_map_ranges [i].end <= cp)
145 const guint16 *tab = upper ? simple_upper_case_mapping_lowarea [i] : simple_lower_case_mapping_lowarea [i];
146 v = tab [cp - simple_case_map_ranges [i].start];
149 i2 = (gint8)(i - (upper ? simple_upper_case_mapping_lowarea_table_count : simple_lower_case_mapping_lowarea_table_count));
150 tab = upper ? simple_upper_case_mapping_higharea [i2] : simple_lower_case_mapping_higharea [i2];
151 v = tab [cp - simple_case_map_ranges [i].start];
153 return v != 0 ? (gunichar) v : c;
159 g_unichar_toupper (gunichar c)
161 return g_unichar_case (c, TRUE);
165 g_unichar_tolower (gunichar c)
167 return g_unichar_case (c, FALSE);
171 g_unichar_totitle (gunichar c)
177 for (i = 0; i < simple_titlecase_mapping_count; i++) {
178 if (simple_titlecase_mapping [i].codepoint == cp)
179 return simple_titlecase_mapping [i].title;
180 if (simple_titlecase_mapping [i].codepoint > cp)
181 /* it is ordered, hence no more match */
184 return g_unichar_toupper (c);
188 g_unichar_isxdigit (gunichar c)
190 return (g_unichar_xdigit_value (c) != -1);
195 g_unichar_xdigit_value (gunichar c)
197 if (c >= 0x30 && c <= 0x39) /*0-9*/
199 if (c >= 0x41 && c <= 0x46) /*A-F*/
201 if (c >= 0x61 && c <= 0x66) /*a-f*/
207 g_convert (const gchar *str, gssize len,
208 const gchar *to_codeset, const gchar *from_codeset,
209 gsize *bytes_read, gsize *bytes_written, GError **error)
215 char *buffer, *output;
216 const char *strptr = (const char *) str;
217 size_t str_len = len == -1 ? strlen (str) : len;
219 size_t left, out_left;
221 convertor = iconv_open (to_codeset, from_codeset);
222 if (convertor == (iconv_t) -1){
230 buffer_size = str_len + 1 + 8;
231 buffer = g_malloc (buffer_size);
236 int res = iconv (convertor, (char **) &strptr, &left, &output, &out_left);
237 if (res == (size_t) -1){
240 size_t extra_space = 8 + left;
241 size_t output_used = output - buffer;
243 buffer_size += extra_space;
245 n = g_realloc (buffer, buffer_size);
249 *error = g_error_new (NULL, G_CONVERT_ERROR_FAILED, "No memory left");
255 out_left += extra_space;
256 output = buffer + output_used;
257 } else if (errno == EILSEQ){
259 *error = g_error_new (NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "Invalid multi-byte sequence on input");
263 } else if (errno == EINVAL){
265 *error = g_error_new (NULL, G_CONVERT_ERROR_PARTIAL_INPUT, "Partial character sequence");
272 if (bytes_read != NULL)
273 *bytes_read = strptr - str;
274 if (bytes_written != NULL)
275 *bytes_written = output - buffer;
279 iconv_close (convertor);
285 * This is broken, and assumes an UTF8 system, but will do for eglib's first user
288 g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
293 len = strlen (utf8string);
295 res = g_malloc (len + 1);
296 g_strlcpy (res, utf8string, len + 1);
301 g_get_charset (G_CONST_RETURN char **charset)
305 sprintf (buf, "CP%u", GetACP ());
309 if (my_charset == NULL){
310 /* These shouldn't be heap allocated */
311 #if HAVE_LOCALCHARSET_H
312 my_charset = locale_charset ();
313 #elif defined(HAVE_LANGINFO_H)
314 my_charset = nl_langinfo (CODESET);
316 my_charset = "UTF-8";
318 is_utf8 = strcmp (my_charset, "UTF-8") == 0;
322 *charset = my_charset;
329 g_locale_to_utf8 (const gchar *opsysstring, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
331 g_get_charset (NULL);
333 return g_convert (opsysstring, len, "UTF-8", my_charset, bytes_read, bytes_written, error);
337 g_locale_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
339 g_get_charset (NULL);
341 return g_convert (utf8string, len, my_charset, "UTF-8", bytes_read, bytes_written, error);
345 * @utf: Pointer to putative UTF-8 encoded string.
347 * Checks @utf for being valid UTF-8. @utf is assumed to be
348 * null-terminated. This function is not super-strict, as it will
349 * allow longer UTF-8 sequences than necessary. Note that Java is
350 * capable of producing these sequences if provoked. Also note, this
351 * routine checks for the 4-byte maximum size, but does not check for
352 * 0x10ffff maximum value.
354 * Return value: true if @utf is valid.
357 g_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
359 gssize byteCount = 0;
360 gboolean retVal = TRUE;
361 gboolean lastRet = TRUE;
362 guchar* ptr = (guchar*) str;
368 else if (max_len < 0)
370 while (*ptr != 0 && byteCount <= max_len) {
371 length = g_trailingBytesForUTF8 [*ptr] + 1;
372 srcPtr = (guchar*) ptr + length;
374 default: retVal = FALSE;
375 /* Everything else falls through when "TRUE"... */
376 case 4: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
377 if ((a == (guchar) 0xBF || a == (guchar) 0xBE) && *(srcPtr-1) == (guchar) 0xBF) {
378 if (*(srcPtr-2) == (guchar) 0x8F || *(srcPtr-2) == (guchar) 0x9F ||
379 *(srcPtr-2) == (guchar) 0xAF || *(srcPtr-2) == (guchar) 0xBF)
382 case 3: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
383 case 2: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
386 /* no fall-through in this inner switch */
387 case 0xE0: if (a < (guchar) 0xA0) retVal = FALSE; break;
388 case 0xED: if (a > (guchar) 0x9F) retVal = FALSE; break;
389 case 0xEF: if (a == (guchar)0xB7 && (*(srcPtr+1) > (guchar) 0x8F && *(srcPtr+1) < 0xB0)) retVal = FALSE;
390 if (a == (guchar)0xBF && (*(srcPtr+1) == (guchar) 0xBE || *(srcPtr+1) == 0xBF)) retVal = FALSE; break;
391 case 0xF0: if (a < (guchar) 0x90) retVal = FALSE; break;
392 case 0xF4: if (a > (guchar) 0x8F) retVal = FALSE; break;
393 default: if (a < (guchar) 0x80) retVal = FALSE;
396 case 1: if (*ptr >= (guchar ) 0x80 && *ptr < (guchar) 0xC2) retVal = FALSE;
398 if (*ptr > (guchar) 0xF4)
400 //If the string is invalid, set the end to the invalid byte.
401 if (!retVal && lastRet) {
410 if (retVal && end != NULL)
416 * @src: Pointer to UTF-8 encoded character.
418 * Return value: UTF-16 value of @src
421 g_utf8_get_char (const gchar *src)
424 guchar* ptr = (guchar*) src;
425 gushort extraBytesToRead = g_trailingBytesForUTF8 [*ptr];
427 switch (extraBytesToRead) {
428 case 5: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
429 case 4: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
430 case 3: ch += *ptr++; ch <<= 6;
431 case 2: ch += *ptr++; ch <<= 6;
432 case 1: ch += *ptr++; ch <<= 6;
435 ch -= offsetsFromUTF8 [extraBytesToRead];
439 g_utf8_strlen (const gchar *str, gssize max)
441 gssize byteCount = 0;
442 guchar* ptr = (guchar*) str;
448 while (*ptr != 0 && byteCount <= max) {
449 gssize cLen = g_trailingBytesForUTF8 [*ptr] + 1;
450 if (max > 0 && (byteCount + cLen) > max)