3 * string encoding conversions
6 * Dick Porter (dick@ximian.com)
8 * (C) 2003 Ximian, Inc.
17 static const char trailingBytesForUTF8[256] = {
18 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,
19 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,
20 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,
21 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,
22 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,
23 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,
24 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,
25 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
29 * mono_unicode_from_external:
30 * \param in pointers to the buffer.
31 * \param bytes number of bytes in the string.
32 * Tries to turn a NULL-terminated string into UTF-16.
34 * First, see if it's valid UTF-8, in which case just turn it directly
35 * into UTF-16. Next, run through the colon-separated encodings in
36 * \c MONO_EXTERNAL_ENCODINGS and do an \c iconv conversion on each,
37 * returning the first successful conversion to UTF-16. If no
38 * conversion succeeds, return NULL.
40 * Callers must free the returned string if not NULL. \p bytes holds the number
41 * of bytes in the returned string, not including the terminator.
44 mono_unicode_from_external (const gchar *in, gsize *bytes)
56 encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
57 if(encoding_list==NULL) {
58 encoding_list = g_strdup("");
61 encodings=g_strsplit (encoding_list, ":", 0);
62 g_free (encoding_list);
63 for(i=0;encodings[i]!=NULL; i++) {
64 /* "default_locale" is a special case encoding */
65 if(!strcmp (encodings[i], "default_locale")) {
66 gchar *utf8=g_locale_to_utf8 (in, -1, NULL, NULL, NULL);
68 res=(gchar *) g_utf8_to_utf16 (utf8, -1, NULL, &lbytes, NULL);
69 *bytes = (gsize) lbytes;
73 /* Don't use UTF16 here. It returns the <FF FE> prepended to the string */
74 res = g_convert (in, strlen (in), "UTF8", encodings[i], NULL, bytes, NULL);
77 res = (gchar *) g_utf8_to_utf16 (res, -1, NULL, &lbytes, NULL);
78 *bytes = (gsize) lbytes;
84 g_strfreev (encodings);
86 return((gunichar2 *)res);
90 g_strfreev (encodings);
92 if(g_utf8_validate (in, -1, NULL)) {
93 gunichar2 *unires=g_utf8_to_utf16 (in, -1, NULL, (glong *)bytes, NULL);
102 * mono_utf8_from_external:
103 * \param in pointer to the string buffer.
104 * Tries to turn a NULL-terminated string into UTF8.
106 * First, see if it's valid UTF-8, in which case there's nothing more
107 * to be done. Next, run through the colon-separated encodings in
108 * \c MONO_EXTERNAL_ENCODINGS and do an \c iconv conversion on each,
109 * returning the first successful conversion to UTF-8. If no
110 * conversion succeeds, return NULL.
112 * Callers must free the returned string if not NULL.
114 * This function is identical to \c mono_unicode_from_external, apart
115 * from returning UTF-8 not UTF-16; it's handy in a few places to work
118 gchar *mono_utf8_from_external (const gchar *in)
122 gchar *encoding_list;
129 encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
130 if(encoding_list==NULL) {
131 encoding_list = g_strdup("");
134 encodings=g_strsplit (encoding_list, ":", 0);
135 g_free (encoding_list);
136 for(i=0;encodings[i]!=NULL; i++) {
138 /* "default_locale" is a special case encoding */
139 if(!strcmp (encodings[i], "default_locale")) {
140 res=g_locale_to_utf8 (in, -1, NULL, NULL, NULL);
141 if(res!=NULL && !g_utf8_validate (res, -1, NULL)) {
146 res=g_convert (in, -1, "UTF8", encodings[i], NULL,
151 g_strfreev (encodings);
156 g_strfreev (encodings);
158 if(g_utf8_validate (in, -1, NULL)) {
159 return(g_strdup (in));
166 * mono_unicode_to_external:
167 * \param uni a UTF-16 string to convert to an external representation.
168 * Turns NULL-terminated UTF-16 into either UTF-8, or the first
169 * working item in \c MONO_EXTERNAL_ENCODINGS if set. If no conversions
170 * work, then UTF-8 is returned.
171 * Callers must free the returned string.
173 gchar *mono_unicode_to_external (const gunichar2 *uni)
176 gchar *encoding_list;
178 /* Turn the unicode into utf8 to start with, because its
179 * easier to work with gchar * than gunichar2 *
181 utf8=g_utf16_to_utf8 (uni, -1, NULL, NULL, NULL);
182 g_assert (utf8!=NULL);
184 encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
185 if(encoding_list==NULL) {
189 gchar *res, **encodings;
192 encodings=g_strsplit (encoding_list, ":", 0);
193 g_free (encoding_list);
194 for(i=0; encodings[i]!=NULL; i++) {
195 if(!strcmp (encodings[i], "default_locale")) {
196 res=g_locale_from_utf8 (utf8, -1, NULL, NULL,
199 res=g_convert (utf8, -1, encodings[i], "UTF8",
205 g_strfreev (encodings);
211 g_strfreev (encodings);
214 /* Nothing else worked, so just return the utf8 */
219 * mono_utf8_validate_and_len
220 * \param source Pointer to putative UTF-8 encoded string.
221 * Checks \p source for being valid UTF-8. \p utf is assumed to be
223 * \returns TRUE if \p source is valid.
224 * \p oEnd will equal the null terminator at the end of the string if valid.
225 * if not valid, it will equal the first charater of the invalid sequence.
226 * \p oLength will equal the length to \p oEnd
229 mono_utf8_validate_and_len (const gchar *source, glong* oLength, const gchar** oEnd)
231 gboolean retVal = TRUE;
232 gboolean lastRet = TRUE;
233 guchar* ptr = (guchar*) source;
239 length = trailingBytesForUTF8 [*ptr] + 1;
240 srcPtr = (guchar*) ptr + length;
242 default: retVal = FALSE;
243 /* Everything else falls through when "TRUE"... */
244 case 4: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
245 if ((a == (guchar) 0xBF || a == (guchar) 0xBE) && *(srcPtr-1) == (guchar) 0xBF) {
246 if (*(srcPtr-2) == (guchar) 0x8F || *(srcPtr-2) == (guchar) 0x9F ||
247 *(srcPtr-2) == (guchar) 0xAF || *(srcPtr-2) == (guchar) 0xBF)
250 case 3: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
251 case 2: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
254 /* no fall-through in this inner switch */
255 case 0xE0: if (a < (guchar) 0xA0) retVal = FALSE; break;
256 case 0xED: if (a > (guchar) 0x9F) retVal = FALSE; break;
257 case 0xEF: if (a == (guchar)0xB7 && (*(srcPtr+1) > (guchar) 0x8F && *(srcPtr+1) < 0xB0)) retVal = FALSE;
258 if (a == (guchar)0xBF && (*(srcPtr+1) == (guchar) 0xBE || *(srcPtr+1) == 0xBF)) retVal = FALSE; break;
259 case 0xF0: if (a < (guchar) 0x90) retVal = FALSE; break;
260 case 0xF4: if (a > (guchar) 0x8F) retVal = FALSE; break;
261 default: if (a < (guchar) 0x80) retVal = FALSE;
264 case 1: if (*ptr >= (guchar ) 0x80 && *ptr < (guchar) 0xC2) retVal = FALSE;
266 if (*ptr > (guchar) 0xF4)
268 //If the string is invalid, set the end to the invalid byte.
269 if (!retVal && lastRet) {
271 *oEnd = (gchar*) ptr;
277 if (retVal && oEnd != NULL)
278 *oEnd = (gchar*) ptr;
284 * mono_utf8_validate_and_len_with_bounds
285 * \param source: Pointer to putative UTF-8 encoded string.
286 * \param max_bytes: Max number of bytes that can be decoded.
288 * Checks \p source for being valid UTF-8. \p utf is assumed to be
291 * This function returns FALSE if it needs to decode characters beyond \p max_bytes.
293 * \returns TRUE if \p source is valid.
294 * \p oEnd will equal the null terminator at the end of the string if valid.
295 * if not valid, it will equal the first charater of the invalid sequence.
296 * \p oLength will equal the length to \p oEnd
299 mono_utf8_validate_and_len_with_bounds (const gchar *source, glong max_bytes, glong* oLength, const gchar** oEnd)
301 gboolean retVal = TRUE;
302 gboolean lastRet = TRUE;
303 guchar* ptr = (guchar*) source;
304 guchar *end = ptr + max_bytes;
312 *oEnd = (gchar*) ptr;
317 length = trailingBytesForUTF8 [*ptr] + 1;
318 srcPtr = (guchar*) ptr + length;
320 /* since *ptr is not zero we must ensure that we can decode the current char + the byte after
321 srcPtr points to the first byte after the current char.*/
327 default: retVal = FALSE;
328 /* Everything else falls through when "TRUE"... */
329 case 4: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
330 if ((a == (guchar) 0xBF || a == (guchar) 0xBE) && *(srcPtr-1) == (guchar) 0xBF) {
331 if (*(srcPtr-2) == (guchar) 0x8F || *(srcPtr-2) == (guchar) 0x9F ||
332 *(srcPtr-2) == (guchar) 0xAF || *(srcPtr-2) == (guchar) 0xBF)
335 case 3: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
336 case 2: if ((a = (*--srcPtr)) < (guchar) 0x80 || a > (guchar) 0xBF) retVal = FALSE;
339 /* no fall-through in this inner switch */
340 case 0xE0: if (a < (guchar) 0xA0) retVal = FALSE; break;
341 case 0xED: if (a > (guchar) 0x9F) retVal = FALSE; break;
342 case 0xEF: if (a == (guchar)0xB7 && (*(srcPtr+1) > (guchar) 0x8F && *(srcPtr+1) < 0xB0)) retVal = FALSE;
343 if (a == (guchar)0xBF && (*(srcPtr+1) == (guchar) 0xBE || *(srcPtr+1) == 0xBF)) retVal = FALSE; break;
344 case 0xF0: if (a < (guchar) 0x90) retVal = FALSE; break;
345 case 0xF4: if (a > (guchar) 0x8F) retVal = FALSE; break;
346 default: if (a < (guchar) 0x80) retVal = FALSE;
349 case 1: if (*ptr >= (guchar ) 0x80 && *ptr < (guchar) 0xC2) retVal = FALSE;
351 if (*ptr > (guchar) 0xF4)
353 //If the string is invalid, set the end to the invalid byte.
354 if (!retVal && lastRet) {
356 *oEnd = (gchar*) ptr;
362 if (retVal && oEnd != NULL)
363 *oEnd = (gchar*) ptr;