1 /* GLIB - Library of useful routines for C programming
3 * gconvert.c: Convert between character sets using iconv
4 * Copyright Red Hat Inc., 2000
5 * Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #ifdef G_PLATFORM_WIN32
40 g_convert_error_quark()
44 quark = g_quark_from_static_string ("g_convert_error");
49 #if defined(USE_LIBICONV) && !defined (_LIBICONV_H)
50 #error libiconv in use but included iconv.h not from libiconv
52 #if !defined(USE_LIBICONV) && defined (_LIBICONV_H)
53 #error libiconv not in use but included iconv.h is from libiconv
57 try_conversion (const char *to_codeset,
58 const char *from_codeset,
61 *cd = iconv_open (to_codeset, from_codeset);
63 if (*cd == (iconv_t)-1 && errno == EINVAL)
70 try_to_aliases (const char **to_aliases,
71 const char *from_codeset,
76 const char **p = to_aliases;
79 if (try_conversion (*p, from_codeset, cd))
89 extern const char **_g_charset_get_aliases (const char *canonical_name);
93 * @to_codeset: destination codeset
94 * @from_codeset: source codeset
96 * Same as the standard UNIX routine <function>iconv_open()</function>, but
97 * may be implemented via libiconv on UNIX flavors that lack
98 * a native implementation.
100 * GLib provides g_convert() and g_locale_to_utf8() which are likely
101 * more convenient than the raw iconv wrappers.
103 * Return value: a "conversion descriptor"
106 g_iconv_open (const gchar *to_codeset,
107 const gchar *from_codeset)
111 if (!try_conversion (to_codeset, from_codeset, &cd))
113 const char **to_aliases = _g_charset_get_aliases (to_codeset);
114 const char **from_aliases = _g_charset_get_aliases (from_codeset);
118 const char **p = from_aliases;
121 if (try_conversion (to_codeset, *p, &cd))
124 if (try_to_aliases (to_aliases, *p, &cd))
131 if (try_to_aliases (to_aliases, from_codeset, &cd))
140 * @converter: conversion descriptor from g_iconv_open()
141 * @inbuf: bytes to convert
142 * @inbytes_left: inout parameter, bytes remaining to convert in @inbuf
143 * @outbuf: converted output bytes
144 * @outbytes_left: inout parameter, bytes available to fill in @outbuf
146 * Same as the standard UNIX routine <function>iconv()</function>, but
147 * may be implemented via libiconv on UNIX flavors that lack
148 * a native implementation.
150 * GLib provides g_convert() and g_locale_to_utf8() which are likely
151 * more convenient than the raw iconv wrappers.
153 * Return value: count of non-reversible conversions, or -1 on error
156 g_iconv (GIConv converter,
160 gsize *outbytes_left)
162 iconv_t cd = (iconv_t)converter;
164 return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
169 * @converter: a conversion descriptor from g_iconv_open()
171 * Same as the standard UNIX routine <function>iconv_close()</function>, but
172 * may be implemented via libiconv on UNIX flavors that lack
173 * a native implementation. Should be called to clean up
174 * the conversion descriptor from g_iconv_open() when
175 * you are done converting things.
177 * GLib provides g_convert() and g_locale_to_utf8() which are likely
178 * more convenient than the raw iconv wrappers.
180 * Return value: -1 on error, 0 on success
183 g_iconv_close (GIConv converter)
185 iconv_t cd = (iconv_t)converter;
187 return iconv_close (cd);
191 open_converter (const gchar *to_codeset,
192 const gchar *from_codeset,
195 GIConv cd = g_iconv_open (to_codeset, from_codeset);
197 if (cd == (iconv_t) -1)
199 /* Something went wrong. */
201 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
202 _("Conversion from character set '%s' to '%s' is not supported"),
203 from_codeset, to_codeset);
205 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
206 _("Could not open converter from '%s' to '%s': %s"),
207 from_codeset, to_codeset, strerror (errno));
216 * @str: the string to convert
217 * @len: the length of the string
218 * @to_codeset: name of character set into which to convert @str
219 * @from_codeset: character set of @str.
220 * @bytes_read: location to store the number of bytes in the
221 * input string that were successfully converted, or %NULL.
222 * Even if the conversion was successful, this may be
223 * less than @len if there were partial characters
224 * at the end of the input. If the error
225 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
226 * stored will the byte offset after the last valid
228 * @bytes_written: the number of bytes stored in the output buffer (not
229 * including the terminating nul).
230 * @error: location to store the error occuring, or %NULL to ignore
231 * errors. Any of the errors in #GConvertError may occur.
233 * Converts a string from one character set to another.
235 * Return value: If the conversion was successful, a newly allocated
236 * nul-terminated string, which must be freed with
237 * g_free(). Otherwise %NULL and @error will be set.
240 g_convert (const gchar *str,
242 const gchar *to_codeset,
243 const gchar *from_codeset,
245 gsize *bytes_written,
251 g_return_val_if_fail (str != NULL, NULL);
252 g_return_val_if_fail (to_codeset != NULL, NULL);
253 g_return_val_if_fail (from_codeset != NULL, NULL);
255 cd = open_converter (to_codeset, from_codeset, error);
257 if (cd == (GIConv) -1)
268 res = g_convert_with_iconv (str, len, cd,
269 bytes_read, bytes_written,
278 * g_convert_with_iconv:
279 * @str: the string to convert
280 * @len: the length of the string
281 * @converter: conversion descriptor from g_iconv_open()
282 * @bytes_read: location to store the number of bytes in the
283 * input string that were successfully converted, or %NULL.
284 * Even if the conversion was successful, this may be
285 * less than @len if there were partial characters
286 * at the end of the input. If the error
287 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
288 * stored will the byte offset after the last valid
290 * @bytes_written: the number of bytes stored in the output buffer (not
291 * including the terminating nul).
292 * @error: location to store the error occuring, or %NULL to ignore
293 * errors. Any of the errors in #GConvertError may occur.
295 * Converts a string from one character set to another.
297 * Return value: If the conversion was successful, a newly allocated
298 * nul-terminated string, which must be freed with
299 * g_free(). Otherwise %NULL and @error will be set.
302 g_convert_with_iconv (const gchar *str,
306 gsize *bytes_written,
312 gsize inbytes_remaining;
313 gsize outbytes_remaining;
316 gboolean have_error = FALSE;
318 g_return_val_if_fail (str != NULL, NULL);
319 g_return_val_if_fail (converter != (GIConv) -1, NULL);
325 inbytes_remaining = len;
326 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
328 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
329 outp = dest = g_malloc (outbuf_size);
333 err = g_iconv (converter, (char **)&p, &inbytes_remaining, &outp, &outbytes_remaining);
335 if (err == (size_t) -1)
340 /* Incomplete text, do not report an error */
344 size_t used = outp - dest;
347 dest = g_realloc (dest, outbuf_size);
350 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
355 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
356 _("Invalid byte sequence in conversion input"));
360 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
361 _("Error during conversion: %s"),
371 *bytes_read = p - str;
374 if ((p - str) != len)
378 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
379 _("Partial character sequence at end of input"));
386 *bytes_written = outp - dest; /* Doesn't include '\0' */
398 * g_convert_with_fallback:
399 * @str: the string to convert
400 * @len: the length of the string
401 * @to_codeset: name of character set into which to convert @str
402 * @from_codeset: character set of @str.
403 * @fallback: UTF-8 string to use in place of character not
404 * present in the target encoding. (This must be
405 * in the target encoding), if %NULL, characters
406 * not in the target encoding will be represented
407 * as Unicode escapes \x{XXXX} or \x{XXXXXX}.
408 * @bytes_read: location to store the number of bytes in the
409 * input string that were successfully converted, or %NULL.
410 * Even if the conversion was successful, this may be
411 * less than @len if there were partial characters
412 * at the end of the input.
413 * @bytes_written: the number of bytes stored in the output buffer (not
414 * including the terminating nul).
415 * @error: location to store the error occuring, or %NULL to ignore
416 * errors. Any of the errors in #GConvertError may occur.
418 * Converts a string from one character set to another, possibly
419 * including fallback sequences for characters not representable
420 * in the output. Note that it is not guaranteed that the specification
421 * for the fallback sequences in @fallback will be honored. Some
422 * systems may do a approximate conversion from @from_codeset
423 * to @to_codeset in their <function>iconv()</function> functions,
424 * in which case GLib will simply return that approximate conversion.
426 * Return value: If the conversion was successful, a newly allocated
427 * nul-terminated string, which must be freed with
428 * g_free(). Otherwise %NULL and @error will be set.
431 g_convert_with_fallback (const gchar *str,
433 const gchar *to_codeset,
434 const gchar *from_codeset,
437 gsize *bytes_written,
443 const gchar *insert_str = NULL;
445 gsize inbytes_remaining;
446 const gchar *save_p = NULL;
447 gsize save_inbytes = 0;
448 gsize outbytes_remaining;
452 gboolean have_error = FALSE;
453 gboolean done = FALSE;
455 GError *local_error = NULL;
457 g_return_val_if_fail (str != NULL, NULL);
458 g_return_val_if_fail (to_codeset != NULL, NULL);
459 g_return_val_if_fail (from_codeset != NULL, NULL);
464 /* Try an exact conversion; we only proceed if this fails
465 * due to an illegal sequence in the input string.
467 dest = g_convert (str, len, to_codeset, from_codeset,
468 bytes_read, bytes_written, &local_error);
472 if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
474 g_propagate_error (error, local_error);
478 g_error_free (local_error);
482 /* No go; to proceed, we need a converter from "UTF-8" to
483 * to_codeset, and the string as UTF-8.
485 cd = open_converter (to_codeset, "UTF-8", error);
486 if (cd == (GIConv) -1)
497 utf8 = g_convert (str, len, "UTF-8", from_codeset,
498 bytes_read, &inbytes_remaining, error);
507 /* Now the heart of the code. We loop through the UTF-8 string, and
508 * whenever we hit an offending character, we form fallback, convert
509 * the fallback to the target codeset, and then go back to
510 * converting the original string after finishing with the fallback.
512 * The variables save_p and save_inbytes store the input state
513 * for the original string while we are converting the fallback
517 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
518 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
519 outp = dest = g_malloc (outbuf_size);
521 while (!done && !have_error)
523 size_t inbytes_tmp = inbytes_remaining;
524 err = g_iconv (cd, (char **)&p, &inbytes_tmp, &outp, &outbytes_remaining);
525 inbytes_remaining = inbytes_tmp;
527 if (err == (size_t) -1)
532 g_assert_not_reached();
536 size_t used = outp - dest;
539 dest = g_realloc (dest, outbuf_size);
542 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
549 /* Error converting fallback string - fatal
551 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
552 _("Cannot convert fallback '%s' to codeset '%s'"),
553 insert_str, to_codeset);
561 gunichar ch = g_utf8_get_char (p);
562 insert_str = g_strdup_printf ("\\x{%0*X}",
563 (ch < 0x10000) ? 4 : 6,
567 insert_str = fallback;
569 save_p = g_utf8_next_char (p);
570 save_inbytes = inbytes_remaining - (save_p - p);
572 inbytes_remaining = strlen (p);
576 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
577 _("Error during conversion: %s"),
588 g_free ((gchar *)insert_str);
590 inbytes_remaining = save_inbytes;
605 *bytes_written = outp - dest; /* Doesn't include '\0' */
611 if (save_p && !fallback)
612 g_free ((gchar *)insert_str);
627 strdup_len (const gchar *string,
629 gsize *bytes_written,
636 if (!g_utf8_validate (string, -1, NULL))
643 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
644 _("Invalid byte sequence in conversion input"));
649 real_len = strlen (string);
654 while (real_len < len && string[real_len])
659 *bytes_read = real_len;
661 *bytes_written = real_len;
663 return g_strndup (string, real_len);
668 * @opsysstring: a string in the encoding of the current locale
669 * @len: the length of the string, or -1 if the string is
671 * @bytes_read: location to store the number of bytes in the
672 * input string that were successfully converted, or %NULL.
673 * Even if the conversion was successful, this may be
674 * less than @len if there were partial characters
675 * at the end of the input. If the error
676 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
677 * stored will the byte offset after the last valid
679 * @bytes_written: the number of bytes stored in the output buffer (not
680 * including the terminating nul).
681 * @error: location to store the error occuring, or %NULL to ignore
682 * errors. Any of the errors in #GConvertError may occur.
684 * Converts a string which is in the encoding used for strings by
685 * the C runtime (usually the same as that used by the operating
686 * system) in the current locale into a UTF-8 string.
688 * Return value: The converted string, or %NULL on an error.
691 g_locale_to_utf8 (const gchar *opsysstring,
694 gsize *bytes_written,
697 #ifdef G_PLATFORM_WIN32
699 gint i, clen, total_len, wclen, first;
705 len = strlen (opsysstring);
707 wcs = g_new (wchar_t, len);
708 wclen = MultiByteToWideChar (CP_ACP, 0, opsysstring, len, wcs, len);
712 for (i = 0; i < wclen; i++)
720 else if (wc < 0x10000)
722 else if (wc < 0x200000)
724 else if (wc < 0x4000000)
730 result = g_malloc (total_len + 1);
734 for (i = 0; i < wclen; i++)
748 else if (wc < 0x10000)
753 else if (wc < 0x200000)
758 else if (wc < 0x4000000)
772 case 6: bp[5] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
773 case 5: bp[4] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
774 case 4: bp[3] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
775 case 3: bp[2] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
776 case 2: bp[1] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
777 case 1: bp[0] = wc | first;
789 *bytes_written = total_len;
793 #else /* !G_PLATFORM_WIN32 */
797 if (g_get_charset (&charset))
798 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
800 return g_convert (opsysstring, len,
801 "UTF-8", charset, bytes_read, bytes_written, error);
803 #endif /* !G_PLATFORM_WIN32 */
807 * g_locale_from_utf8:
808 * @utf8string: a UTF-8 encoded string
809 * @len: the length of the string, or -1 if the string is
811 * @bytes_read: location to store the number of bytes in the
812 * input string that were successfully converted, or %NULL.
813 * Even if the conversion was successful, this may be
814 * less than @len if there were partial characters
815 * at the end of the input. If the error
816 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
817 * stored will the byte offset after the last valid
819 * @bytes_written: the number of bytes stored in the output buffer (not
820 * including the terminating nul).
821 * @error: location to store the error occuring, or %NULL to ignore
822 * errors. Any of the errors in #GConvertError may occur.
824 * Converts a string from UTF-8 to the encoding used for strings by
825 * the C runtime (usually the same as that used by the operating
826 * system) in the current locale.
828 * Return value: The converted string, or %NULL on an error.
831 g_locale_from_utf8 (const gchar *utf8string,
834 gsize *bytes_written,
837 #ifdef G_PLATFORM_WIN32
839 gint i, mask, clen, mblen;
846 len = strlen (utf8string);
848 /* First convert to wide chars */
849 cp = (guchar *) utf8string;
852 wcs = g_new (wchar_t, len + 1);
864 else if ((c & 0xe0) == 0xc0)
869 else if ((c & 0xf0) == 0xe0)
874 else if ((c & 0xf8) == 0xf0)
879 else if ((c & 0xfc) == 0xf8)
884 else if ((c & 0xfc) == 0xfc)
901 *wcp = (cp[0] & mask);
902 for (i = 1; i < clen; i++)
904 if ((cp[i] & 0xc0) != 0x80)
910 *wcp |= (cp[i] & 0x3f);
923 /* n is the number of wide chars constructed */
925 /* Convert to a string in the current ANSI codepage */
927 result = g_new (gchar, 3 * n + 1);
928 mblen = WideCharToMultiByte (CP_ACP, 0, wcs, n, result, 3*n, NULL, NULL);
935 *bytes_written = mblen;
939 #else /* !G_PLATFORM_WIN32 */
941 const gchar *charset;
943 if (g_get_charset (&charset))
944 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
946 return g_convert (utf8string, len,
947 charset, "UTF-8", bytes_read, bytes_written, error);
949 #endif /* !G_PLATFORM_WIN32 */
953 * g_filename_to_utf8:
954 * @opsysstring: a string in the encoding for filenames
955 * @len: the length of the string, or -1 if the string is
957 * @bytes_read: location to store the number of bytes in the
958 * input string that were successfully converted, or %NULL.
959 * Even if the conversion was successful, this may be
960 * less than @len if there were partial characters
961 * at the end of the input. If the error
962 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
963 * stored will the byte offset after the last valid
965 * @bytes_written: the number of bytes stored in the output buffer (not
966 * including the terminating nul).
967 * @error: location to store the error occuring, or %NULL to ignore
968 * errors. Any of the errors in #GConvertError may occur.
970 * Converts a string which is in the encoding used for filenames
971 * into a UTF-8 string.
973 * Return value: The converted string, or %NULL on an error.
976 g_filename_to_utf8 (const gchar *opsysstring,
979 gsize *bytes_written,
982 #ifdef G_PLATFORM_WIN32
983 return g_locale_to_utf8 (opsysstring, len,
984 bytes_read, bytes_written,
986 #else /* !G_PLATFORM_WIN32 */
988 if (getenv ("G_BROKEN_FILENAMES"))
989 return g_locale_to_utf8 (opsysstring, len,
990 bytes_read, bytes_written,
993 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
994 #endif /* !G_PLATFORM_WIN32 */
998 * g_filename_from_utf8:
999 * @utf8string: a UTF-8 encoded string.
1000 * @len: the length of the string, or -1 if the string is
1002 * @bytes_read: location to store the number of bytes in the
1003 * input string that were successfully converted, or %NULL.
1004 * Even if the conversion was successful, this may be
1005 * less than @len if there were partial characters
1006 * at the end of the input. If the error
1007 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
1008 * stored will the byte offset after the last valid
1010 * @bytes_written: the number of bytes stored in the output buffer (not
1011 * including the terminating nul).
1012 * @error: location to store the error occuring, or %NULL to ignore
1013 * errors. Any of the errors in #GConvertError may occur.
1015 * Converts a string from UTF-8 to the encoding used for filenames.
1017 * Return value: The converted string, or %NULL on an error.
1020 g_filename_from_utf8 (const gchar *utf8string,
1023 gsize *bytes_written,
1026 #ifdef G_PLATFORM_WIN32
1027 return g_locale_from_utf8 (utf8string, len,
1028 bytes_read, bytes_written,
1030 #else /* !G_PLATFORM_WIN32 */
1031 if (getenv ("G_BROKEN_FILENAMES"))
1032 return g_locale_from_utf8 (utf8string, len,
1033 bytes_read, bytes_written,
1036 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
1037 #endif /* !G_PLATFORM_WIN32 */
1040 /* Test of haystack has the needle prefix, comparing case
1041 * insensitive. haystack may be UTF-8, but needle must
1042 * contain only ascii. */
1044 has_case_prefix (const gchar *haystack, const gchar *needle)
1048 /* Eat one character at a time. */
1053 g_ascii_tolower (*n) == g_ascii_tolower (*h))
1063 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
1064 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
1065 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
1066 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
1067 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
1068 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
1069 } UnsafeCharacterSet;
1071 static const guchar acceptable[96] = {
1072 /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
1073 0x00,0x3F,0x20,0x20,0x20,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x22,0x20,0x3F,0x3F,0x1C, /* 2X !"#$%&'()*+,-./ */
1074 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x2C, /* 3X 0123456789:;<=>? */
1075 0x30,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, /* 4X @ABCDEFGHIJKLMNO */
1076 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
1077 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, /* 6X `abcdefghijklmno */
1078 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20 /* 7X pqrstuvwxyz{|}~DEL */
1081 static const gchar hex[16] = "0123456789ABCDEF";
1083 /* Note: This escape function works on file: URIs, but if you want to
1084 * escape something else, please read RFC-2396 */
1086 g_escape_uri_string (const gchar *string,
1087 UnsafeCharacterSet mask)
1089 #define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
1096 UnsafeCharacterSet use_mask;
1098 g_return_val_if_fail (mask == UNSAFE_ALL
1099 || mask == UNSAFE_ALLOW_PLUS
1100 || mask == UNSAFE_PATH
1101 || mask == UNSAFE_DOS_PATH
1102 || mask == UNSAFE_HOST
1103 || mask == UNSAFE_SLASHES, NULL);
1107 for (p = string; *p != '\0'; p++)
1110 if (!ACCEPTABLE (c))
1114 result = g_malloc (p - string + unacceptable * 2 + 1);
1117 for (q = result, p = string; *p != '\0'; p++)
1119 c = (unsigned char)*p;
1121 if (!ACCEPTABLE (c))
1123 *q++ = '%'; /* means hex coming */
1138 g_escape_file_uri (const gchar *hostname,
1139 const gchar *pathname)
1141 char *escaped_hostname = NULL;
1145 if (hostname && *hostname != '\0')
1147 escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
1150 escaped_path = g_escape_uri_string (pathname, UNSAFE_DOS_PATH);
1152 res = g_strconcat ("file://",
1153 (escaped_hostname) ? escaped_hostname : "",
1154 (*escaped_path != '/') ? "/" : "",
1158 g_free (escaped_hostname);
1159 g_free (escaped_path);
1165 unescape_character (const char *scanner)
1170 first_digit = g_ascii_xdigit_value (*scanner++);
1172 if (first_digit < 0)
1175 second_digit = g_ascii_xdigit_value (*scanner++);
1176 if (second_digit < 0)
1179 return (first_digit << 4) | second_digit;
1183 g_unescape_uri_string (const gchar *escaped,
1184 const gchar *illegal_characters,
1187 const gchar *in, *in_end;
1188 gchar *out, *result;
1191 if (escaped == NULL)
1195 len = strlen (escaped);
1197 result = g_malloc (len + 1);
1200 for (in = escaped, in_end = escaped + len; in < in_end && *in != '\0'; in++)
1203 if (character == '%')
1205 character = unescape_character (in + 1);
1207 /* Check for an illegal character. We consider '\0' illegal here. */
1209 || (illegal_characters != NULL
1210 && strchr (illegal_characters, (char)character) != NULL))
1222 g_assert (out - result <= strlen (escaped));
1224 if (!g_utf8_validate (result, -1, NULL))
1234 * g_filename_from_uri:
1235 * @uri: a uri describing a filename (escaped, encoded in UTF-8).
1236 * @hostname: Location to store hostname for the URI, or %NULL.
1237 * If there is no hostname in the URI, %NULL will be
1238 * stored in this location.
1239 * @error: location to store the error occuring, or %NULL to ignore
1240 * errors. Any of the errors in #GConvertError may occur.
1242 * Converts an escaped UTF-8 encoded URI to a local filename in the
1243 * encoding used for filenames.
1245 * Return value: a newly-allocated string holding the resulting
1246 * filename, or %NULL on an error.
1249 g_filename_from_uri (const char *uri,
1253 const char *path_part;
1254 const char *host_part;
1255 char *unescaped_hostname;
1263 if (!has_case_prefix (uri, "file:/"))
1265 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1266 _("The URI '%s' is not an absolute URI using the file scheme"),
1271 path_part = uri + strlen ("file:");
1273 if (strchr (path_part, '#') != NULL)
1275 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1276 _("The local file URI '%s' may not include a '#'"),
1281 if (has_case_prefix (path_part, "///"))
1283 else if (has_case_prefix (path_part, "//"))
1286 host_part = path_part;
1288 path_part = strchr (path_part, '/');
1290 if (path_part == NULL)
1292 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1293 _("The URI '%s' is invalid"),
1298 unescaped_hostname = g_unescape_uri_string (host_part, "", path_part - host_part);
1299 if (unescaped_hostname == NULL)
1301 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1302 _("The hostname of the URI '%s' contains invalidly escaped characters"),
1308 *hostname = unescaped_hostname;
1310 g_free (unescaped_hostname);
1313 filename = g_unescape_uri_string (path_part, "/", -1);
1315 if (filename == NULL)
1317 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1318 _("The URI '%s' contains invalidly escaped characters"),
1323 /* DOS uri's are like "file://host/c:\foo", so we need to check if we need to
1324 * drop the initial slash */
1326 if (g_path_is_absolute (filename+1))
1329 result = g_filename_from_utf8 (filename + offs, -1, NULL, NULL, error);
1336 * g_filename_to_uri:
1337 * @filename: an absolute filename specified in the encoding
1338 * used for filenames by the operating system.
1339 * @hostname: A UTF-8 encoded hostname, or %NULL for none.
1340 * @error: location to store the error occuring, or %NULL to ignore
1341 * errors. Any of the errors in #GConvertError may occur.
1343 * Converts an absolute filename to an escaped UTF-8 encoded URI.
1345 * Return value: a newly-allocated string holding the resulting
1346 * URI, or %NULL on an error.
1349 g_filename_to_uri (const char *filename,
1354 char *utf8_filename;
1356 g_return_val_if_fail (filename != NULL, NULL);
1358 if (!g_path_is_absolute (filename))
1360 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
1361 _("The pathname '%s' is not an absolute path"),
1366 utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, error);
1367 if (utf8_filename == NULL)
1371 !g_utf8_validate (hostname, -1, NULL))
1373 g_free (utf8_filename);
1374 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1375 _("Invalid byte sequence in hostname"));
1379 escaped_uri = g_escape_file_uri (hostname,
1381 g_free (utf8_filename);