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
58 * @to_codeset: destination codeset
59 * @from_codeset: source codeset
61 * Same as the standard UNIX routine iconv_open(), but
62 * may be implemented via libiconv on UNIX flavors that lack
63 * a native implementation.
65 * GLib provides g_convert() and g_locale_to_utf8() which are likely
66 * more convenient than the raw iconv wrappers.
68 * Return value: a "conversion descriptor"
71 g_iconv_open (const gchar *to_codeset,
72 const gchar *from_codeset)
74 iconv_t cd = iconv_open (to_codeset, from_codeset);
81 * @converter: conversion descriptor from g_iconv_open()
82 * @inbuf: bytes to convert
83 * @inbytes_left: inout parameter, bytes remaining to convert in @inbuf
84 * @outbuf: converted output bytes
85 * @outbytes_left: inout parameter, bytes available to fill in @outbuf
87 * Same as the standard UNIX routine iconv(), but
88 * may be implemented via libiconv on UNIX flavors that lack
89 * a native implementation.
91 * GLib provides g_convert() and g_locale_to_utf8() which are likely
92 * more convenient than the raw iconv wrappers.
94 * Return value: count of non-reversible conversions, or -1 on error
97 g_iconv (GIConv converter,
101 gsize *outbytes_left)
103 iconv_t cd = (iconv_t)converter;
105 return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
110 * @converter: a conversion descriptor from g_iconv_open()
112 * Same as the standard UNIX routine iconv_close(), but
113 * may be implemented via libiconv on UNIX flavors that lack
114 * a native implementation. Should be called to clean up
115 * the conversion descriptor from iconv_open() when
116 * you are done converting things.
118 * GLib provides g_convert() and g_locale_to_utf8() which are likely
119 * more convenient than the raw iconv wrappers.
121 * Return value: -1 on error, 0 on success
124 g_iconv_close (GIConv converter)
126 iconv_t cd = (iconv_t)converter;
128 return iconv_close (cd);
132 open_converter (const gchar *to_codeset,
133 const gchar *from_codeset,
136 GIConv cd = g_iconv_open (to_codeset, from_codeset);
138 if (cd == (iconv_t) -1)
140 /* Something went wrong. */
142 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
143 _("Conversion from character set `%s' to `%s' is not supported"),
144 from_codeset, to_codeset);
146 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
147 _("Could not open converter from `%s' to `%s': %s"),
148 from_codeset, to_codeset, strerror (errno));
157 * @str: the string to convert
158 * @len: the length of the string
159 * @to_codeset: name of character set into which to convert @str
160 * @from_codeset: character set of @str.
161 * @bytes_read: location to store the number of bytes in the
162 * input string that were successfully converted, or %NULL.
163 * Even if the conversion was succesful, this may be
164 * less than len if there were partial characters
165 * at the end of the input. If the error
166 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
167 * stored will the byte fofset after the last valid
169 * @bytes_written: the stored in the output buffer (not including the
171 * @error: location to store the error occuring, or %NULL to ignore
172 * errors. Any of the errors in #GConvertError may occur.
174 * Convert a string from one character set to another.
176 * Return value: If the conversion was successful, a newly allocated
177 * NUL-terminated string, which must be freed with
178 * g_free. Otherwise %NULL and @error will be set.
181 g_convert (const gchar *str,
183 const gchar *to_codeset,
184 const gchar *from_codeset,
186 gsize *bytes_written,
192 g_return_val_if_fail (str != NULL, NULL);
193 g_return_val_if_fail (to_codeset != NULL, NULL);
194 g_return_val_if_fail (from_codeset != NULL, NULL);
196 cd = open_converter (to_codeset, from_codeset, error);
198 if (cd == (GIConv) -1)
209 res = g_convert_with_iconv (str, len, cd,
210 bytes_read, bytes_written,
219 * g_convert_with_iconv:
220 * @str: the string to convert
221 * @len: the length of the string
222 * @converter: conversion descriptor from g_iconv_open()
223 * @bytes_read: location to store the number of bytes in the
224 * input string that were successfully converted, or %NULL.
225 * Even if the conversion was succesful, this may be
226 * less than len if there were partial characters
227 * at the end of the input. If the error
228 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
229 * stored will the byte fofset after the last valid
231 * @bytes_written: the stored in the output buffer (not including the
233 * @error: location to store the error occuring, or %NULL to ignore
234 * errors. Any of the errors in #GConvertError may occur.
236 * Convert a string from one character set to another.
238 * Return value: If the conversion was successful, a newly allocated
239 * NUL-terminated string, which must be freed with
240 * g_free. Otherwise %NULL and @error will be set.
243 g_convert_with_iconv (const gchar *str,
247 gsize *bytes_written,
253 gsize inbytes_remaining;
254 gsize outbytes_remaining;
257 gboolean have_error = FALSE;
259 g_return_val_if_fail (str != NULL, NULL);
260 g_return_val_if_fail (converter != (GIConv) -1, NULL);
266 inbytes_remaining = len;
267 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
269 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
270 outp = dest = g_malloc (outbuf_size);
274 err = g_iconv (converter, (char **)&p, &inbytes_remaining, &outp, &outbytes_remaining);
276 if (err == (size_t) -1)
281 /* Incomplete text, do not report an error */
285 size_t used = outp - dest;
288 dest = g_realloc (dest, outbuf_size);
291 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
296 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
297 _("Invalid byte sequence in conversion input"));
301 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
302 _("Error during conversion: %s"),
312 *bytes_read = p - str;
315 if ((p - str) != len)
319 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
320 _("Partial character sequence at end of input"));
327 *bytes_written = outp - dest; /* Doesn't include '\0' */
339 * g_convert_with_fallback:
340 * @str: the string to convert
341 * @len: the length of the string
342 * @to_codeset: name of character set into which to convert @str
343 * @from_codeset: character set of @str.
344 * @fallback: UTF-8 string to use in place of character not
345 * present in the target encoding. (This must be
346 * in the target encoding), if %NULL, characters
347 * not in the target encoding will be represented
348 * as Unicode escapes \x{XXXX} or \x{XXXXXX}.
349 * @bytes_read: location to store the number of bytes in the
350 * input string that were successfully converted, or %NULL.
351 * Even if the conversion was succesful, this may be
352 * less than len if there were partial characters
353 * at the end of the input.
354 * @bytes_written: the stored in the output buffer (not including the
356 * @error: location to store the error occuring, or %NULL to ignore
357 * errors. Any of the errors in #GConvertError may occur.
359 * Convert a string from one character set to another, possibly
360 * including fallback sequences for characters not representable
361 * in the output. Note that it is not guaranteed that the specification
362 * for the fallback sequences in @fallback will be honored. Some
363 * systems may do a approximate conversion from @from_codeset
364 * to @to_codeset in their iconv() functions, in which case GLib
365 * will simply return that approximate conversion.
367 * Return value: If the conversion was successful, a newly allocated
368 * NUL-terminated string, which must be freed with
369 * g_free. Otherwise %NULL and @error will be set.
372 g_convert_with_fallback (const gchar *str,
374 const gchar *to_codeset,
375 const gchar *from_codeset,
378 gsize *bytes_written,
384 const gchar *insert_str = NULL;
386 gsize inbytes_remaining;
387 const gchar *save_p = NULL;
388 gsize save_inbytes = 0;
389 gsize outbytes_remaining;
393 gboolean have_error = FALSE;
394 gboolean done = FALSE;
396 GError *local_error = NULL;
398 g_return_val_if_fail (str != NULL, NULL);
399 g_return_val_if_fail (to_codeset != NULL, NULL);
400 g_return_val_if_fail (from_codeset != NULL, NULL);
405 /* Try an exact conversion; we only proceed if this fails
406 * due to an illegal sequence in the input string.
408 dest = g_convert (str, len, to_codeset, from_codeset,
409 bytes_read, bytes_written, &local_error);
413 if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
415 g_propagate_error (error, local_error);
419 g_error_free (local_error);
423 /* No go; to proceed, we need a converter from "UTF-8" to
424 * to_codeset, and the string as UTF-8.
426 cd = open_converter (to_codeset, "UTF-8", error);
427 if (cd == (GIConv) -1)
438 utf8 = g_convert (str, len, "UTF-8", from_codeset,
439 bytes_read, &inbytes_remaining, error);
443 /* Now the heart of the code. We loop through the UTF-8 string, and
444 * whenever we hit an offending character, we form fallback, convert
445 * the fallback to the target codeset, and then go back to
446 * converting the original string after finishing with the fallback.
448 * The variables save_p and save_inbytes store the input state
449 * for the original string while we are converting the fallback
453 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
454 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
455 outp = dest = g_malloc (outbuf_size);
457 while (!done && !have_error)
459 size_t inbytes_tmp = inbytes_remaining;
460 err = g_iconv (cd, (char **)&p, &inbytes_tmp, &outp, &outbytes_remaining);
461 inbytes_remaining = inbytes_tmp;
463 if (err == (size_t) -1)
468 g_assert_not_reached();
472 size_t used = outp - dest;
475 dest = g_realloc (dest, outbuf_size);
478 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
485 /* Error converting fallback string - fatal
487 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
488 _("Cannot convert fallback '%s' to codeset '%s'"),
489 insert_str, to_codeset);
497 gunichar ch = g_utf8_get_char (p);
498 insert_str = g_strdup_printf ("\\x{%0*X}",
499 (ch < 0x10000) ? 4 : 6,
503 insert_str = fallback;
505 save_p = g_utf8_next_char (p);
506 save_inbytes = inbytes_remaining - (save_p - p);
508 inbytes_remaining = strlen (p);
512 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
513 _("Error during conversion: %s"),
524 g_free ((gchar *)insert_str);
526 inbytes_remaining = save_inbytes;
541 *bytes_written = outp - str; /* Doesn't include '\0' */
547 if (save_p && !fallback)
548 g_free ((gchar *)insert_str);
563 strdup_len (const gchar *string,
565 gsize *bytes_written,
572 if (!g_utf8_validate (string, -1, NULL))
579 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
580 _("Invalid byte sequence in conversion input"));
585 real_len = strlen (string);
590 while (real_len < len && string[real_len])
595 *bytes_read = real_len;
597 *bytes_written = real_len;
599 return g_strndup (string, real_len);
604 * @opsysstring: a string in the encoding of the current locale
605 * @len: the length of the string, or -1 if the string is
607 * @bytes_read: location to store the number of bytes in the
608 * input string that were successfully converted, or %NULL.
609 * Even if the conversion was succesful, this may be
610 * less than len if there were partial characters
611 * at the end of the input. If the error
612 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
613 * stored will the byte fofset after the last valid
615 * @bytes_written: the stored in the output buffer (not including the
617 * @error: location to store the error occuring, or %NULL to ignore
618 * errors. Any of the errors in #GConvertError may occur.
620 * Converts a string which is in the encoding used for strings by
621 * the C runtime (usually the same as that used by the operating
622 * system) in the current locale into a UTF-8 string.
624 * Return value: The converted string, or %NULL on an error.
627 g_locale_to_utf8 (const gchar *opsysstring,
630 gsize *bytes_written,
633 #ifdef G_PLATFORM_WIN32
635 gint i, clen, total_len, wclen, first;
641 len = strlen (opsysstring);
643 wcs = g_new (wchar_t, len);
644 wclen = MultiByteToWideChar (CP_ACP, 0, opsysstring, len, wcs, len);
648 for (i = 0; i < wclen; i++)
656 else if (wc < 0x10000)
658 else if (wc < 0x200000)
660 else if (wc < 0x4000000)
666 result = g_malloc (total_len + 1);
670 for (i = 0; i < wclen; i++)
684 else if (wc < 0x10000)
689 else if (wc < 0x200000)
694 else if (wc < 0x4000000)
708 case 6: bp[5] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
709 case 5: bp[4] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
710 case 4: bp[3] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
711 case 3: bp[2] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
712 case 2: bp[1] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
713 case 1: bp[0] = wc | first;
725 *bytes_written = total_len;
729 #else /* !G_PLATFORM_WIN32 */
733 if (g_get_charset (&charset))
734 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
736 return g_convert (opsysstring, len,
737 "UTF-8", charset, bytes_read, bytes_written, error);
739 #endif /* !G_PLATFORM_WIN32 */
743 * g_locale_from_utf8:
744 * @utf8string: a UTF-8 encoded string
745 * @len: the length of the string, or -1 if the string is
747 * @bytes_read: location to store the number of bytes in the
748 * input string that were successfully converted, or %NULL.
749 * Even if the conversion was succesful, this may be
750 * less than len if there were partial characters
751 * at the end of the input. If the error
752 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
753 * stored will the byte fofset after the last valid
755 * @bytes_written: the stored in the output buffer (not including the
757 * @error: location to store the error occuring, or %NULL to ignore
758 * errors. Any of the errors in #GConvertError may occur.
760 * Converts a string from UTF-8 to the encoding used for strings by
761 * the C runtime (usually the same as that used by the operating
762 * system) in the current locale.
764 * Return value: The converted string, or %NULL on an error.
767 g_locale_from_utf8 (const gchar *utf8string,
770 gsize *bytes_written,
773 #ifdef G_PLATFORM_WIN32
775 gint i, mask, clen, mblen;
782 len = strlen (utf8string);
784 /* First convert to wide chars */
785 cp = (guchar *) utf8string;
788 wcs = g_new (wchar_t, len + 1);
800 else if ((c & 0xe0) == 0xc0)
805 else if ((c & 0xf0) == 0xe0)
810 else if ((c & 0xf8) == 0xf0)
815 else if ((c & 0xfc) == 0xf8)
820 else if ((c & 0xfc) == 0xfc)
837 *wcp = (cp[0] & mask);
838 for (i = 1; i < clen; i++)
840 if ((cp[i] & 0xc0) != 0x80)
846 *wcp |= (cp[i] & 0x3f);
859 /* n is the number of wide chars constructed */
861 /* Convert to a string in the current ANSI codepage */
863 result = g_new (gchar, 3 * n + 1);
864 mblen = WideCharToMultiByte (CP_ACP, 0, wcs, n, result, 3*n, NULL, NULL);
871 *bytes_written = mblen;
875 #else /* !G_PLATFORM_WIN32 */
877 const gchar *charset;
879 if (g_get_charset (&charset))
880 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
882 return g_convert (utf8string, len,
883 charset, "UTF-8", bytes_read, bytes_written, error);
885 #endif /* !G_PLATFORM_WIN32 */
889 * g_filename_to_utf8:
890 * @opsysstring: a string in the encoding for filenames
891 * @len: the length of the string, or -1 if the string is
893 * @bytes_read: location to store the number of bytes in the
894 * input string that were successfully converted, or %NULL.
895 * Even if the conversion was succesful, this may be
896 * less than len if there were partial characters
897 * at the end of the input. If the error
898 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
899 * stored will the byte fofset after the last valid
901 * @bytes_written: the stored in the output buffer (not including the
903 * @error: location to store the error occuring, or %NULL to ignore
904 * errors. Any of the errors in #GConvertError may occur.
906 * Converts a string which is in the encoding used for filenames
907 * into a UTF-8 string.
909 * Return value: The converted string, or %NULL on an error.
912 g_filename_to_utf8 (const gchar *opsysstring,
915 gsize *bytes_written,
918 #ifdef G_PLATFORM_WIN32
919 return g_locale_to_utf8 (opsysstring, len,
920 bytes_read, bytes_written,
922 #else /* !G_PLATFORM_WIN32 */
924 if (getenv ("G_BROKEN_FILENAMES"))
925 return g_locale_to_utf8 (opsysstring, len,
926 bytes_read, bytes_written,
929 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
930 #endif /* !G_PLATFORM_WIN32 */
934 * g_filename_from_utf8:
935 * @utf8string: a UTF-8 encoded string
936 * @len: the length of the string, or -1 if the string is
938 * @bytes_read: location to store the number of bytes in the
939 * input string that were successfully converted, or %NULL.
940 * Even if the conversion was succesful, this may be
941 * less than len if there were partial characters
942 * at the end of the input. If the error
943 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
944 * stored will the byte fofset after the last valid
946 * @bytes_written: the stored in the output buffer (not including the
948 * @error: location to store the error occuring, or %NULL to ignore
949 * errors. Any of the errors in #GConvertError may occur.
951 * Converts a string from UTF-8 to the encoding used for filenames.
953 * Return value: The converted string, or %NULL on an error.
956 g_filename_from_utf8 (const gchar *utf8string,
959 gsize *bytes_written,
962 #ifdef G_PLATFORM_WIN32
963 return g_locale_from_utf8 (utf8string, len,
964 bytes_read, bytes_written,
966 #else /* !G_PLATFORM_WIN32 */
967 if (getenv ("G_BROKEN_FILENAMES"))
968 return g_locale_from_utf8 (utf8string, len,
969 bytes_read, bytes_written,
972 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
973 #endif /* !G_PLATFORM_WIN32 */
976 /* Test of haystack has the needle prefix, comparing case
977 * insensitive. haystack may be UTF-8, but needle must
978 * contain only ascii. */
980 has_case_prefix (const gchar *haystack, const gchar *needle)
984 /* Eat one character at a time. */
989 g_ascii_tolower (*n) == g_ascii_tolower (*h))
999 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
1000 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
1001 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
1002 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
1003 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
1004 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
1005 } UnsafeCharacterSet;
1007 static const guchar acceptable[96] = {
1008 /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
1009 0x00,0x3F,0x20,0x20,0x20,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x22,0x20,0x3F,0x3F,0x1C, /* 2X !"#$%&'()*+,-./ */
1010 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x2C, /* 3X 0123456789:;<=>? */
1011 0x30,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, /* 4X @ABCDEFGHIJKLMNO */
1012 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
1013 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, /* 6X `abcdefghijklmno */
1014 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20 /* 7X pqrstuvwxyz{|}~DEL */
1017 static const gchar hex[16] = "0123456789ABCDEF";
1019 /* Note: This escape function works on file: URIs, but if you want to
1020 * escape something else, please read RFC-2396 */
1022 g_escape_uri_string (const gchar *string,
1023 UnsafeCharacterSet mask)
1025 #define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
1032 UnsafeCharacterSet use_mask;
1034 g_return_val_if_fail (mask == UNSAFE_ALL
1035 || mask == UNSAFE_ALLOW_PLUS
1036 || mask == UNSAFE_PATH
1037 || mask == UNSAFE_DOS_PATH
1038 || mask == UNSAFE_HOST
1039 || mask == UNSAFE_SLASHES, NULL);
1043 for (p = string; *p != '\0'; p++)
1046 if (!ACCEPTABLE (c))
1050 result = g_malloc (p - string + unacceptable * 2 + 1);
1053 for (q = result, p = string; *p != '\0'; p++)
1055 c = (unsigned char)*p;
1057 if (!ACCEPTABLE (c))
1059 *q++ = '%'; /* means hex coming */
1074 g_escape_file_uri (const gchar *hostname,
1075 const gchar *pathname)
1077 char *escaped_hostname = NULL;
1081 if (hostname && *hostname != '\0')
1083 escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
1086 escaped_path = g_escape_uri_string (pathname, UNSAFE_DOS_PATH);
1088 res = g_strconcat ("file://",
1089 (escaped_hostname) ? escaped_hostname : "",
1090 (*escaped_path != '/') ? "/" : "",
1094 g_free (escaped_hostname);
1095 g_free (escaped_path);
1101 unescape_character (const char *scanner)
1106 first_digit = g_ascii_xdigit_value (*scanner++);
1108 if (first_digit < 0)
1111 second_digit = g_ascii_xdigit_value (*scanner++);
1112 if (second_digit < 0)
1115 return (first_digit << 4) | second_digit;
1119 g_unescape_uri_string (const gchar *escaped,
1120 const gchar *illegal_characters,
1123 const gchar *in, *in_end;
1124 gchar *out, *result;
1127 if (escaped == NULL)
1131 len = strlen (escaped);
1133 result = g_malloc (len + 1);
1136 for (in = escaped, in_end = escaped + len; in < in_end && *in != '\0'; in++)
1139 if (character == '%')
1141 character = unescape_character (in + 1);
1143 /* Check for an illegal character. We consider '\0' illegal here. */
1145 || (illegal_characters != NULL
1146 && strchr (illegal_characters, (char)character) != NULL))
1158 g_assert (out - result <= strlen (escaped));
1160 if (!g_utf8_validate (result, -1, NULL))
1170 * g_filename_from_uri:
1171 * @uri: a uri describing a filename (escaped, encoded in UTF-8)
1172 * @hostname: Location to store hostname for the URI, or %NULL.
1173 * If there is no hostname in the URI, %NULL will be
1174 * stored in this location.
1175 * @error: location to store the error occuring, or %NULL to ignore
1176 * errors. Any of the errors in #GConvertError may occur.
1178 * Converts an escaped UTF-8 encoded URI to a local filename in the
1179 * encoding used for filenames.
1181 * Return value: a newly allocated string holding the resulting
1182 * filename, or %NULL on an error.
1185 g_filename_from_uri (const char *uri,
1189 const char *path_part;
1190 const char *host_part;
1191 char *unescaped_hostname;
1199 if (!has_case_prefix (uri, "file:/"))
1201 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI,
1202 _("The URI `%s' is not an absolute URI using the file scheme"),
1207 path_part = uri + strlen ("file:");
1209 if (strchr (path_part, '#') != NULL)
1211 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_INVALID_URI,
1212 _("The local file URI `%s' may not include a `#'"),
1217 if (has_case_prefix (path_part, "///"))
1219 else if (has_case_prefix (path_part, "//"))
1222 host_part = path_part;
1224 path_part = strchr (path_part, '/');
1226 if (path_part == NULL)
1228 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_INVALID_URI,
1229 _("The URI `%s' is invalid"),
1234 unescaped_hostname = g_unescape_uri_string (host_part, "", path_part - host_part);
1235 if (unescaped_hostname == NULL)
1237 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_INVALID_URI,
1238 _("The hostname of the URI `%s' contains invalidly escaped characters"),
1244 *hostname = unescaped_hostname;
1246 g_free (unescaped_hostname);
1249 filename = g_unescape_uri_string (path_part, "/", -1);
1251 if (filename == NULL)
1253 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_INVALID_URI,
1254 _("The URI `%s' contains invalidly escaped characters"),
1259 /* DOS uri's are like "file://host/c:\foo", so we need to check if we need to
1260 * drop the initial slash */
1262 if (g_path_is_absolute (filename+1))
1265 result = g_filename_from_utf8 (filename + offs, -1, NULL, NULL, error);
1272 * g_filename_to_uri:
1273 * @filename: an absolute filename specified in the encoding
1274 * used for filenames by the operating system.
1275 * @hostname: A UTF-8 encoded hostname, or %NULL for none.
1276 * @error: location to store the error occuring, or %NULL to ignore
1277 * errors. Any of the errors in #GConvertError may occur.
1279 * Converts an absolute filename to an escaped UTF-8 encoded URI.
1281 * Return value: a newly allocated string holding the resulting
1282 * URI, or %NULL on an error.
1285 g_filename_to_uri (const char *filename,
1290 char *utf8_filename;
1292 g_return_val_if_fail (filename != NULL, NULL);
1294 if (!g_path_is_absolute (filename))
1296 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
1297 _("The pathname '%s' is not an absolute path"),
1302 utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, error);
1303 if (utf8_filename == NULL)
1307 !g_utf8_validate (hostname, -1, NULL))
1309 g_free (utf8_filename);
1310 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1311 _("Invalid byte sequence in hostname"));
1315 escaped_uri = g_escape_file_uri (hostname,
1317 g_free (utf8_filename);