+
+#endif
+
+/* Test of haystack has the needle prefix, comparing case
+ * insensitive. haystack may be UTF-8, but needle must
+ * contain only ascii. */
+static gboolean
+has_case_prefix (const gchar *haystack, const gchar *needle)
+{
+ const gchar *h, *n;
+
+ /* Eat one character at a time. */
+ h = haystack;
+ n = needle;
+
+ while (*n && *h &&
+ g_ascii_tolower (*n) == g_ascii_tolower (*h))
+ {
+ n++;
+ h++;
+ }
+
+ return *n == '\0';
+}
+
+typedef enum {
+ UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
+ UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
+ UNSAFE_PATH = 0x8, /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
+ UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
+ UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
+} UnsafeCharacterSet;
+
+static const guchar acceptable[96] = {
+ /* A table of the ASCII chars from space (32) to DEL (127) */
+ /* ! " # $ % & ' ( ) * + , - . / */
+ 0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
+ /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
+ 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
+ /* @ A B C D E F G H I J K L M N O */
+ 0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
+ /* P Q R S T U V W X Y Z [ \ ] ^ _ */
+ 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
+ /* ` a b c d e f g h i j k l m n o */
+ 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
+ /* p q r s t u v w x y z { | } ~ DEL */
+ 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
+};
+
+static const gchar hex[16] = "0123456789ABCDEF";
+
+/* Note: This escape function works on file: URIs, but if you want to
+ * escape something else, please read RFC-2396 */
+static gchar *
+g_escape_uri_string (const gchar *string,
+ UnsafeCharacterSet mask)
+{
+#define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
+
+ const gchar *p;
+ gchar *q;
+ gchar *result;
+ int c;
+ gint unacceptable;
+ UnsafeCharacterSet use_mask;
+
+ g_return_val_if_fail (mask == UNSAFE_ALL
+ || mask == UNSAFE_ALLOW_PLUS
+ || mask == UNSAFE_PATH
+ || mask == UNSAFE_HOST
+ || mask == UNSAFE_SLASHES, NULL);
+
+ unacceptable = 0;
+ use_mask = mask;
+ for (p = string; *p != '\0'; p++)
+ {
+ c = (guchar) *p;
+ if (!ACCEPTABLE (c))
+ unacceptable++;
+ }
+
+ result = g_malloc (p - string + unacceptable * 2 + 1);
+
+ use_mask = mask;
+ for (q = result, p = string; *p != '\0'; p++)
+ {
+ c = (guchar) *p;
+
+ if (!ACCEPTABLE (c))
+ {
+ *q++ = '%'; /* means hex coming */
+ *q++ = hex[c >> 4];
+ *q++ = hex[c & 15];
+ }
+ else
+ *q++ = *p;
+ }
+
+ *q = '\0';
+
+ return result;
+}
+
+
+static gchar *
+g_escape_file_uri (const gchar *hostname,
+ const gchar *pathname)
+{
+ char *escaped_hostname = NULL;
+ char *escaped_path;
+ char *res;
+
+#ifdef G_OS_WIN32
+ char *p, *backslash;
+
+ /* Turn backslashes into forward slashes. That's what Netscape
+ * does, and they are actually more or less equivalent in Windows.
+ */
+
+ pathname = g_strdup (pathname);
+ p = (char *) pathname;
+
+ while ((backslash = strchr (p, '\\')) != NULL)
+ {
+ *backslash = '/';
+ p = backslash + 1;
+ }
+#endif
+
+ if (hostname && *hostname != '\0')
+ {
+ escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
+ }
+
+ escaped_path = g_escape_uri_string (pathname, UNSAFE_PATH);
+
+ res = g_strconcat ("file://",
+ (escaped_hostname) ? escaped_hostname : "",
+ (*escaped_path != '/') ? "/" : "",
+ escaped_path,
+ NULL);
+
+#ifdef G_OS_WIN32
+ g_free ((char *) pathname);
+#endif
+
+ g_free (escaped_hostname);
+ g_free (escaped_path);
+
+ return res;
+}
+
+static int
+unescape_character (const char *scanner)
+{
+ int first_digit;
+ int second_digit;
+
+ first_digit = g_ascii_xdigit_value (scanner[0]);
+ if (first_digit < 0)
+ return -1;
+
+ second_digit = g_ascii_xdigit_value (scanner[1]);
+ if (second_digit < 0)
+ return -1;
+
+ return (first_digit << 4) | second_digit;
+}
+
+static gchar *
+g_unescape_uri_string (const char *escaped,
+ int len,
+ const char *illegal_escaped_characters,
+ gboolean ascii_must_not_be_escaped)
+{
+ const gchar *in, *in_end;
+ gchar *out, *result;
+ int c;
+
+ if (escaped == NULL)
+ return NULL;
+
+ if (len < 0)
+ len = strlen (escaped);
+
+ result = g_malloc (len + 1);
+
+ out = result;
+ for (in = escaped, in_end = escaped + len; in < in_end; in++)
+ {
+ c = *in;
+
+ if (c == '%')
+ {
+ /* catch partial escape sequences past the end of the substring */
+ if (in + 3 > in_end)
+ break;
+
+ c = unescape_character (in + 1);
+
+ /* catch bad escape sequences and NUL characters */
+ if (c <= 0)
+ break;
+
+ /* catch escaped ASCII */
+ if (ascii_must_not_be_escaped && c <= 0x7F)
+ break;
+
+ /* catch other illegal escaped characters */
+ if (strchr (illegal_escaped_characters, c) != NULL)
+ break;
+
+ in += 2;
+ }
+
+ *out++ = c;
+ }
+
+ g_assert (out - result <= len);
+ *out = '\0';
+
+ if (in != in_end)
+ {
+ g_free (result);
+ return NULL;
+ }
+
+ return result;
+}
+
+static gboolean
+is_asciialphanum (gunichar c)
+{
+ return c <= 0x7F && g_ascii_isalnum (c);
+}
+
+static gboolean
+is_asciialpha (gunichar c)
+{
+ return c <= 0x7F && g_ascii_isalpha (c);
+}
+
+/* allows an empty string */
+static gboolean
+hostname_validate (const char *hostname)
+{
+ const char *p;
+ gunichar c, first_char, last_char;
+
+ p = hostname;
+ if (*p == '\0')
+ return TRUE;
+ do
+ {
+ /* read in a label */
+ c = g_utf8_get_char (p);
+ p = g_utf8_next_char (p);
+ if (!is_asciialphanum (c))
+ return FALSE;
+ first_char = c;
+ do
+ {
+ last_char = c;
+ c = g_utf8_get_char (p);
+ p = g_utf8_next_char (p);
+ }
+ while (is_asciialphanum (c) || c == '-');
+ if (last_char == '-')
+ return FALSE;
+
+ /* if that was the last label, check that it was a toplabel */
+ if (c == '\0' || (c == '.' && *p == '\0'))
+ return is_asciialpha (first_char);
+ }
+ while (c == '.');
+ return FALSE;
+}
+
+/**
+ * g_filename_from_uri:
+ * @uri: a uri describing a filename (escaped, encoded in ASCII).
+ * @hostname: Location to store hostname for the URI, or %NULL.
+ * If there is no hostname in the URI, %NULL will be
+ * stored in this location.
+ * @error: location to store the error occuring, or %NULL to ignore
+ * errors. Any of the errors in #GConvertError may occur.
+ *
+ * Converts an escaped ASCII-encoded URI to a local filename in the
+ * encoding used for filenames.
+ *
+ * Return value: a newly-allocated string holding the resulting
+ * filename, or %NULL on an error.
+ **/
+gchar *
+g_filename_from_uri (const gchar *uri,
+ gchar **hostname,
+ GError **error)
+{
+ const char *path_part;
+ const char *host_part;
+ char *unescaped_hostname;
+ char *result;
+ char *filename;
+ int offs;
+#ifdef G_OS_WIN32
+ char *p, *slash;
+#endif
+
+ if (hostname)
+ *hostname = NULL;
+
+ if (!has_case_prefix (uri, "file:/"))
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
+ _("The URI '%s' is not an absolute URI using the \"file\" scheme"),
+ uri);
+ return NULL;
+ }
+
+ path_part = uri + strlen ("file:");
+
+ if (strchr (path_part, '#') != NULL)
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
+ _("The local file URI '%s' may not include a '#'"),
+ uri);
+ return NULL;
+ }
+
+ if (has_case_prefix (path_part, "///"))
+ path_part += 2;
+ else if (has_case_prefix (path_part, "//"))
+ {
+ path_part += 2;
+ host_part = path_part;
+
+ path_part = strchr (path_part, '/');
+
+ if (path_part == NULL)
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
+ _("The URI '%s' is invalid"),
+ uri);
+ return NULL;
+ }
+
+ unescaped_hostname = g_unescape_uri_string (host_part, path_part - host_part, "", TRUE);
+
+ if (unescaped_hostname == NULL ||
+ !hostname_validate (unescaped_hostname))
+ {
+ g_free (unescaped_hostname);
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
+ _("The hostname of the URI '%s' is invalid"),
+ uri);
+ return NULL;
+ }
+
+ if (hostname)
+ *hostname = unescaped_hostname;
+ else
+ g_free (unescaped_hostname);
+ }
+
+ filename = g_unescape_uri_string (path_part, -1, "/", FALSE);
+
+ if (filename == NULL)
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
+ _("The URI '%s' contains invalidly escaped characters"),
+ uri);
+ return NULL;
+ }
+
+ offs = 0;
+#ifdef G_OS_WIN32
+ /* Drop localhost */
+ if (hostname && *hostname != NULL &&
+ g_ascii_strcasecmp (*hostname, "localhost") == 0)
+ {
+ g_free (*hostname);
+ *hostname = NULL;
+ }
+
+ /* Turn slashes into backslashes, because that's the canonical spelling */
+ p = filename;
+ while ((slash = strchr (p, '/')) != NULL)
+ {
+ *slash = '\\';
+ p = slash + 1;
+ }
+
+ /* Windows URIs with a drive letter can be like "file://host/c:/foo"
+ * or "file://host/c|/foo" (some Netscape versions). In those cases, start
+ * the filename from the drive letter.
+ */
+ if (g_ascii_isalpha (filename[1]))
+ {
+ if (filename[2] == ':')
+ offs = 1;
+ else if (filename[2] == '|')
+ {
+ filename[2] = ':';
+ offs = 1;
+ }
+ }
+#endif
+
+ result = g_strdup (filename + offs);
+ g_free (filename);
+
+ return result;
+}
+
+#ifdef G_OS_WIN32
+
+#undef g_filename_from_uri
+
+gchar *
+g_filename_from_uri (const gchar *uri,
+ gchar **hostname,
+ GError **error)
+{
+ gchar *utf8_filename;
+ gchar *retval = NULL;
+
+ utf8_filename = g_filename_from_uri_utf8 (uri, hostname, error);
+ if (utf8_filename)
+ {
+ retval = g_locale_from_utf8 (utf8_filename, -1, NULL, NULL, error);
+ g_free (utf8_filename);
+ }
+ return retval;
+}
+
+#endif
+
+/**
+ * g_filename_to_uri:
+ * @filename: an absolute filename specified in the GLib file name encoding,
+ * which is the on-disk file name bytes on Unix, and UTF-8 on
+ * Windows
+ * @hostname: A UTF-8 encoded hostname, or %NULL for none.
+ * @error: location to store the error occuring, or %NULL to ignore
+ * errors. Any of the errors in #GConvertError may occur.
+ *
+ * Converts an absolute filename to an escaped ASCII-encoded URI.
+ *
+ * Return value: a newly-allocated string holding the resulting
+ * URI, or %NULL on an error.
+ **/
+gchar *
+g_filename_to_uri (const gchar *filename,
+ const gchar *hostname,
+ GError **error)
+{
+ char *escaped_uri;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ if (!g_path_is_absolute (filename))
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
+ _("The pathname '%s' is not an absolute path"),
+ filename);
+ return NULL;
+ }
+
+ if (hostname &&
+ !(g_utf8_validate (hostname, -1, NULL)
+ && hostname_validate (hostname)))
+ {
+ g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid hostname"));
+ return NULL;
+ }
+
+#ifdef G_OS_WIN32
+ /* Don't use localhost unnecessarily */
+ if (hostname && g_ascii_strcasecmp (hostname, "localhost") == 0)
+ hostname = NULL;
+#endif
+
+ escaped_uri = g_escape_file_uri (hostname, filename);
+
+ return escaped_uri;
+}
+
+#ifdef G_OS_WIN32
+
+#undef g_filename_to_uri
+
+gchar *
+g_filename_to_uri (const gchar *filename,
+ const gchar *hostname,
+ GError **error)
+{
+ gchar *utf8_filename;
+ gchar *retval = NULL;
+
+ utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
+
+ if (utf8_filename)
+ {
+ retval = g_filename_to_uri_utf8 (utf8_filename, hostname, error);
+ g_free (utf8_filename);
+ }
+
+ return retval;
+}
+
+#endif
+
+/**
+ * g_uri_list_extract_uris:
+ * @uri_list: an URI list
+ *
+ * Splits an URI list conforming to the text/uri-list
+ * mime type defined in RFC 2483 into individual URIs,
+ * discarding any comments. The URIs are not validated.
+ *
+ * Returns: a newly allocated %NULL-terminated list of
+ * strings holding the individual URIs. The array should
+ * be freed with g_strfreev().
+ *
+ * Since: 2.6
+ */
+gchar **
+g_uri_list_extract_uris (const gchar *uri_list)
+{
+ GSList *uris, *u;
+ const gchar *p, *q;
+ gchar **result;
+ gint n_uris = 0;
+
+ uris = NULL;
+
+ p = uri_list;
+
+ /* We don't actually try to validate the URI according to RFC
+ * 2396, or even check for allowed characters - we just ignore
+ * comments and trim whitespace off the ends. We also
+ * allow LF delimination as well as the specified CRLF.
+ *
+ * We do allow comments like specified in RFC 2483.
+ */
+ while (p)
+ {
+ if (*p != '#')
+ {
+ while (g_ascii_isspace (*p))
+ p++;
+
+ q = p;
+ while (*q && (*q != '\n') && (*q != '\r'))
+ q++;
+
+ if (q > p)
+ {
+ q--;
+ while (q > p && g_ascii_isspace (*q))
+ q--;
+
+ if (q > p)
+ {
+ uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
+ n_uris++;
+ }
+ }
+ }
+ p = strchr (p, '\n');
+ if (p)
+ p++;
+ }
+
+ result = g_new (gchar *, n_uris + 1);
+
+ result[n_uris--] = NULL;
+ for (u = uris; u; u = u->next)
+ result[n_uris--] = u->data;
+
+ g_slist_free (uris);
+
+ return result;
+}
+
+static gchar *
+make_valid_utf8 (const gchar *name)
+{
+ GString *string;
+ const gchar *remainder, *invalid;
+ gint remaining_bytes, valid_bytes;
+
+ string = NULL;
+ remainder = name;
+ remaining_bytes = strlen (name);
+
+ while (remaining_bytes != 0)
+ {
+ if (g_utf8_validate (remainder, remaining_bytes, &invalid))
+ break;
+ valid_bytes = invalid - remainder;
+
+ if (string == NULL)
+ string = g_string_sized_new (remaining_bytes);
+
+ g_string_append_len (string, remainder, valid_bytes);
+ /* append U+FFFD REPLACEMENT CHARACTER */
+ g_string_append (string, "\357\277\275");
+
+ remaining_bytes -= valid_bytes + 1;
+ remainder = invalid + 1;
+ }
+
+ if (string == NULL)
+ return g_strdup (name);
+
+ g_string_append (string, remainder);
+
+ g_assert (g_utf8_validate (string->str, -1, NULL));
+
+ return g_string_free (string, FALSE);
+}
+
+/**
+ * g_filename_display_basename:
+ * @filename: an absolute pathname in the GLib file name encoding
+ *
+ * Returns the display basename for the particular filename, guaranteed
+ * to be valid UTF-8. The display name might not be identical to the filename,
+ * for instance there might be problems converting it to UTF-8, and some files
+ * can be translated in the display.
+ *
+ * If GLib can not make sense of the encoding of @filename, as a last resort it
+ * replaces unknown characters with U+FFFD, the Unicode replacement character.
+ * You can search the result for the UTF-8 encoding of this character (which is
+ * "\357\277\275" in octal notation) to find out if @filename was in an invalid
+ * encoding.
+ *
+ * You must pass the whole absolute pathname to this functions so that
+ * translation of well known locations can be done.
+ *
+ * This function is preferred over g_filename_display_name() if you know the
+ * whole path, as it allows translation.
+ *
+ * Return value: a newly allocated string containing
+ * a rendition of the basename of the filename in valid UTF-8
+ *
+ * Since: 2.6
+ **/
+gchar *
+g_filename_display_basename (const gchar *filename)
+{
+ char *basename;
+ char *display_name;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ basename = g_path_get_basename (filename);
+ display_name = g_filename_display_name (basename);
+ g_free (basename);
+ return display_name;
+}
+
+/**
+ * g_filename_display_name:
+ * @filename: a pathname hopefully in the GLib file name encoding
+ *
+ * Converts a filename into a valid UTF-8 string. The conversion is
+ * not necessarily reversible, so you should keep the original around
+ * and use the return value of this function only for display purposes.
+ * Unlike g_filename_to_utf8(), the result is guaranteed to be non-%NULL
+ * even if the filename actually isn't in the GLib file name encoding.
+ *
+ * If GLib can not make sense of the encoding of @filename, as a last resort it
+ * replaces unknown characters with U+FFFD, the Unicode replacement character.
+ * You can search the result for the UTF-8 encoding of this character (which is
+ * "\357\277\275" in octal notation) to find out if @filename was in an invalid
+ * encoding.
+ *
+ * If you know the whole pathname of the file you should use
+ * g_filename_display_basename(), since that allows location-based
+ * translation of filenames.
+ *
+ * Return value: a newly allocated string containing
+ * a rendition of the filename in valid UTF-8
+ *
+ * Since: 2.6
+ **/
+gchar *
+g_filename_display_name (const gchar *filename)
+{
+ gint i;
+ const gchar **charsets;
+ gchar *display_name = NULL;
+ gboolean is_utf8;
+
+ is_utf8 = g_get_filename_charsets (&charsets);
+
+ if (is_utf8)
+ {
+ if (g_utf8_validate (filename, -1, NULL))
+ display_name = g_strdup (filename);
+ }
+
+ if (!display_name)
+ {
+ /* Try to convert from the filename charsets to UTF-8.
+ * Skip the first charset if it is UTF-8.
+ */
+ for (i = is_utf8 ? 1 : 0; charsets[i]; i++)
+ {
+ display_name = g_convert (filename, -1, "UTF-8", charsets[i],
+ NULL, NULL, NULL);
+
+ if (display_name)
+ break;
+ }
+ }
+
+ /* if all conversions failed, we replace invalid UTF-8
+ * by a question mark
+ */
+ if (!display_name)
+ display_name = make_valid_utf8 (filename);
+
+ return display_name;
+}
+
+#define __G_CONVERT_C__
+#include "galiasdef.c"