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.
38 #include <unicode-data.h>
51 static char *my_charset;
52 static gboolean is_utf8;
55 * Character set conversion
58 * Index into the table below with the first byte of a UTF-8 sequence to
59 * get the number of trailing bytes that are supposed to follow it.
60 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
61 * left as-is for anyone who may want to do such conversion, which was
62 * allowed in earlier algorithms.
64 const gchar g_trailingBytesForUTF8 [256] = {
65 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,
66 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,
67 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,
68 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,
69 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,
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 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,
72 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
76 * Magic values subtracted from a buffer value during UTF8 conversion.
77 * This table contains as many values as there might be trailing bytes
78 * in a UTF-8 sequence.
80 static const gulong offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
81 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
84 g_unichar_type (gunichar c)
88 guint16 cp = (guint16) c;
89 for (i = 0; i < unicode_category_ranges_count; i++) {
90 if (cp < unicode_category_ranges [i].start)
92 if (unicode_category_ranges [i].end <= cp)
94 return unicode_category [i] [cp - unicode_category_ranges [i].start];
98 // 3400-4DB5: OtherLetter
99 // 4E00-9FC3: OtherLetter
100 // AC00-D7A3: OtherLetter
101 // D800-DFFF: OtherSurrogate
102 // E000-F8FF: OtherPrivateUse
103 // 20000-2A6D6 OtherLetter
104 // F0000-FFFFD OtherPrivateUse
105 // 100000-10FFFD OtherPrivateUse
107 if (0x3400 <= cp && cp < 0x4DB5)
108 return G_UNICODE_OTHER_LETTER;
109 if (0x4E00 <= cp && cp < 0x9FC3)
110 return G_UNICODE_OTHER_LETTER;
111 if (0xAC00<= cp && cp < 0xD7A3)
112 return G_UNICODE_OTHER_LETTER;
113 if (0xD800 <= cp && cp < 0xDFFF)
114 return G_UNICODE_SURROGATE;
115 if (0xE000 <= cp && cp < 0xF8FF)
116 return G_UNICODE_PRIVATE_USE;
117 /* since the argument is UTF-16, we cannot check beyond FFFF */
119 /* It should match any of above */
124 g_unichar_case (gunichar c, gboolean upper)
127 guint32 cp = (guint32) c, v;
129 for (i = 0; i < simple_case_map_ranges_count; i++) {
130 if (cp < simple_case_map_ranges [i].start)
132 if (simple_case_map_ranges [i].end <= cp)
135 const guint16 *tab = upper ? simple_upper_case_mapping_lowarea [i] : simple_lower_case_mapping_lowarea [i];
136 v = tab [cp - simple_case_map_ranges [i].start];
139 i2 = (gint8)(i - (upper ? simple_upper_case_mapping_lowarea_table_count : simple_lower_case_mapping_lowarea_table_count));
140 tab = upper ? simple_upper_case_mapping_higharea [i2] : simple_lower_case_mapping_higharea [i2];
141 v = tab [cp - simple_case_map_ranges [i].start];
143 return v != 0 ? (gunichar) v : c;
149 g_unichar_toupper (gunichar c)
151 return g_unichar_case (c, TRUE);
155 g_unichar_tolower (gunichar c)
157 return g_unichar_case (c, FALSE);
161 g_unichar_totitle (gunichar c)
167 for (i = 0; i < simple_titlecase_mapping_count; i++) {
168 if (simple_titlecase_mapping [i].codepoint == cp)
169 return simple_titlecase_mapping [i].title;
170 if (simple_titlecase_mapping [i].codepoint > cp)
171 /* it is ordered, hence no more match */
174 return g_unichar_toupper (c);
178 g_unichar_isxdigit (gunichar c)
180 return (g_unichar_xdigit_value (c) != -1);
185 g_unichar_xdigit_value (gunichar c)
187 if (c >= 0x30 && c <= 0x39) /*0-9*/
189 if (c >= 0x41 && c <= 0x46) /*A-F*/
191 if (c >= 0x61 && c <= 0x66) /*a-f*/
197 g_convert (const gchar *str, gssize len,
198 const gchar *to_codeset, const gchar *from_codeset,
199 gsize *bytes_read, gsize *bytes_written, GError **error)
205 char *buffer, *output;
206 const char *strptr = (const char *) str;
207 size_t str_len = len == -1 ? strlen (str) : len;
209 size_t left, out_left;
211 convertor = iconv_open (to_codeset, from_codeset);
212 if (convertor == (iconv_t) -1){
218 buffer_size = str_len + 1 + 8;
219 buffer = g_malloc (buffer_size);
224 int res = iconv (convertor, (char **) &strptr, &left, &output, &out_left);
225 if (res == (size_t) -1){
228 size_t extra_space = 8 + left;
229 size_t output_used = output - buffer;
231 buffer_size += extra_space;
233 n = g_realloc (buffer, buffer_size);
237 *error = g_error_new (NULL, G_CONVERT_ERROR_FAILED, "No memory left");
243 out_left += extra_space;
244 output = buffer + output_used;
245 } else if (errno == EILSEQ){
247 *error = g_error_new (NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "Invalid multi-byte sequence on input");
251 } else if (errno == EINVAL){
253 *error = g_error_new (NULL, G_CONVERT_ERROR_PARTIAL_INPUT, "Partial character sequence");
260 if (bytes_read != NULL)
261 *bytes_read = strptr - str;
262 if (bytes_written != NULL)
263 *bytes_written = output - buffer;
267 iconv_close (convertor);
273 * This is broken, and assumes an UTF8 system, but will do for eglib's first user
276 g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
281 len = strlen (utf8string);
283 res = g_malloc (len + 1);
284 g_strlcpy (res, utf8string, len + 1);
289 g_get_charset (G_CONST_RETURN char **charset)
293 sprintf (buf, "CP%u", GetACP ());
297 if (my_charset == NULL){
298 my_charset = g_strdup (nl_langinfo (CODESET));
299 is_utf8 = strcmp (my_charset, "UTF-8") == 0;
303 *charset = my_charset;
310 g_locale_to_utf8 (const gchar *opsysstring, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
312 g_get_charset (NULL);
314 return g_convert (opsysstring, len, "UTF-8", my_charset, bytes_read, bytes_written, error);
318 g_locale_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
320 g_get_charset (NULL);
322 return g_convert (utf8string, len, my_charset, "UTF-8", bytes_read, bytes_written, error);
326 * @utf: Pointer to putative UTF-8 encoded string.
328 * Checks @utf for being valid UTF-8. @utf is assumed to be
329 * null-terminated. This function is not super-strict, as it will
330 * allow longer UTF-8 sequences than necessary. Note that Java is
331 * capable of producing these sequences if provoked. Also note, this
332 * routine checks for the 4-byte maximum size, but does not check for
333 * 0x10ffff maximum value.
335 * Return value: true if @utf is valid.
338 g_utf8_validate (const gchar *str, gssize max_len, const gchar **end)
340 gssize byteCount = 0;
341 gboolean retVal = TRUE;
342 gboolean lastRet = TRUE;
343 guchar* ptr = (guchar*) str;
349 else if (max_len < 0)
351 while (*ptr != 0 && byteCount <= max_len) {
352 length = g_trailingBytesForUTF8 [*ptr] + 1;
353 srcPtr = (guchar*) ptr + length;
355 default: retVal = FALSE;
356 /* Everything else falls through when "TRUE"... */
357 case 4: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
358 if ((a == (guchar) 0xBF || a == (guchar) 0xBE) && *(srcPtr-1) == (guchar) 0xBF) {
359 if (*(srcPtr-2) == (guchar) 0x8F || *(srcPtr-2) == (guchar) 0x9F ||
360 *(srcPtr-2) == (guchar) 0xAF || *(srcPtr-2) == (guchar) 0xBF)
363 case 3: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
364 case 2: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
367 /* no fall-through in this inner switch */
368 case 0xE0: if (a < (guchar) 0xA0) retVal = FALSE; break;
369 case 0xED: if (a > (guchar) 0x9F) retVal = FALSE; break;
370 case 0xEF: if (a == (guchar)0xB7 && (*(srcPtr+1) > (guchar) 0x8F && *(srcPtr+1) < 0xB0)) retVal = FALSE;
371 if (a == (guchar)0xBF && (*(srcPtr+1) == (guchar) 0xBE || *(srcPtr+1) == 0xBF)) retVal = FALSE; break;
372 case 0xF0: if (a < (guchar) 0x90) retVal = FALSE; break;
373 case 0xF4: if (a > (guchar) 0x8F) retVal = FALSE; break;
374 default: if (a < (guchar) 0x80) retVal = FALSE;
377 case 1: if (*ptr >= (guchar ) 0x80 && *ptr < (guchar) 0xC2) retVal = FALSE;
379 if (*ptr > (guchar) 0xF4)
381 //If the string is invalid, set the end to the invalid byte.
382 if (!retVal && lastRet) {
391 if (retVal && end != NULL)
397 * @src: Pointer to UTF-8 encoded character.
399 * Return value: UTF-16 value of @src
402 g_utf8_get_char (const gchar *src)
405 guchar* ptr = (guchar*) src;
406 gushort extraBytesToRead = g_trailingBytesForUTF8 [*ptr];
408 switch (extraBytesToRead) {
409 case 5: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
410 case 4: ch += *ptr++; ch <<= 6; // remember, illegal UTF-8
411 case 3: ch += *ptr++; ch <<= 6;
412 case 2: ch += *ptr++; ch <<= 6;
413 case 1: ch += *ptr++; ch <<= 6;
416 ch -= offsetsFromUTF8 [extraBytesToRead];
420 g_utf8_strlen (const gchar *str, gssize max)
422 gssize byteCount = 0;
423 guchar* ptr = (guchar*) str;
429 while (*ptr != 0 && byteCount <= max) {
430 gssize cLen = g_trailingBytesForUTF8 [*ptr] + 1;
431 if (max > 0 && (byteCount + cLen) > max)