diff options
author | Rodrigo Moya <rodrigo@gnome-db.org> | 2011-01-11 15:59:14 +0100 |
---|---|---|
committer | Rodrigo Moya <rodrigo@gnome-db.org> | 2011-01-11 15:59:14 +0100 |
commit | d75842ff4c8c9d78dbd5462409ab2c93600fa9a6 (patch) | |
tree | 86def8b54a1cb6db27a3bff6c6e6caada203ad13 /glib/ghostutils.c | |
parent | 65bd1f526d86ce08d6fa4d7d6fc1140198c9aef1 (diff) | |
parent | fabf506b8d5baf8f59fca563e6f1a62be5148112 (diff) | |
download | glib-wip/gsettings-list.tar.gz |
Merge branch 'master' into wip/gsettings-listwip/gsettings-list
Diffstat (limited to 'glib/ghostutils.c')
-rw-r--r-- | glib/ghostutils.c | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/glib/ghostutils.c b/glib/ghostutils.c index c0361958f..99afe9a7e 100644 --- a/glib/ghostutils.c +++ b/glib/ghostutils.c @@ -303,7 +303,8 @@ idna_is_prohibited (gunichar ch) /* RFC 3491 IDN cleanup algorithm. */ static gchar * nameprep (const gchar *hostname, - gint len) + gint len, + gboolean *is_unicode) { gchar *name, *tmp = NULL, *p; @@ -336,12 +337,15 @@ nameprep (const gchar *hostname, /* If there are no UTF8 characters, we're done. */ if (!contains_non_ascii (name, len)) { + *is_unicode = FALSE; if (name == (gchar *)hostname) return len == -1 ? g_strdup (hostname) : g_strndup (hostname, len); else return name; } + *is_unicode = TRUE; + /* Normalize */ name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC); g_free (tmp); @@ -383,6 +387,26 @@ nameprep (const gchar *hostname, return name; } +/* RFC 3490, section 3.1 says '.', 0x3002, 0xFF0E, and 0xFF61 count as + * label-separating dots. @str must be '\0'-terminated. + */ +#define idna_is_dot(str) ( \ + ((guchar)(str)[0] == '.') || \ + ((guchar)(str)[0] == 0xE3 && (guchar)(str)[1] == 0x80 && (guchar)(str)[2] == 0x82) || \ + ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBC && (guchar)(str)[2] == 0x8E) || \ + ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBD && (guchar)(str)[2] == 0xA1) ) + +static const gchar * +idna_end_of_label (const gchar *str) +{ + for (; *str; str = g_utf8_next_char (str)) + { + if (idna_is_dot (str)) + return str; + } + return str; +} + /** * g_hostname_to_ascii: * @hostname: a valid UTF-8 or ASCII hostname @@ -404,16 +428,16 @@ g_hostname_to_ascii (const gchar *hostname) gssize llen, oldlen; gboolean unicode; - label = name = nameprep (hostname, -1); - if (!name) - return NULL; + label = name = nameprep (hostname, -1, &unicode); + if (!name || !unicode) + return name; out = g_string_new (NULL); do { unicode = FALSE; - for (p = label; *p && *p != '.'; p++) + for (p = label; *p && !idna_is_dot (p); p++) { if ((guchar)*p > 0x80) unicode = TRUE; @@ -437,7 +461,9 @@ g_hostname_to_ascii (const gchar *hostname) goto fail; label += llen; - if (*label && *++label) + if (*label) + label = g_utf8_next_char (label); + if (*label) g_string_append_c (out, '.'); } while (*label); @@ -585,7 +611,7 @@ g_hostname_to_unicode (const gchar *hostname) do { - llen = strcspn (hostname, "."); + llen = idna_end_of_label (hostname) - hostname; if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN)) { hostname += IDNA_ACE_PREFIX_LEN; @@ -598,7 +624,8 @@ g_hostname_to_unicode (const gchar *hostname) } else { - gchar *canonicalized = nameprep (hostname, llen); + gboolean unicode; + gchar *canonicalized = nameprep (hostname, llen, &unicode); if (!canonicalized) { @@ -610,7 +637,9 @@ g_hostname_to_unicode (const gchar *hostname) } hostname += llen; - if (*hostname && *++hostname) + if (*hostname) + hostname = g_utf8_next_char (hostname); + if (*hostname) g_string_append_c (out, '.'); } while (*hostname); @@ -643,8 +672,10 @@ g_hostname_is_ascii_encoded (const gchar *hostname) { if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN)) return TRUE; - hostname = strchr (hostname, '.'); - if (!hostname++) + hostname = idna_end_of_label (hostname); + if (*hostname) + hostname = g_utf8_next_char (hostname); + if (!*hostname) return FALSE; } } |