Remove a bit of unused code
[glib.git] / glib / ghostutils.c
blob99afe9a7e84fa52a348232c03e7187a9b9832fea
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GLIB - Library of useful routines for C programming
4 * Copyright (C) 2008 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "config.h"
24 #include <string.h>
26 #include "ghostutils.h"
28 #include "garray.h"
29 #include "gmem.h"
30 #include "gstring.h"
31 #include "gstrfuncs.h"
32 #include "glibintl.h"
35 /**
36 * SECTION:ghostutils
37 * @short_description: Internet hostname utilities
39 * Functions for manipulating internet hostnames; in particular, for
40 * converting between Unicode and ASCII-encoded forms of
41 * Internationalized Domain Names (IDNs).
43 * The <ulink
44 * url="http://www.ietf.org/rfc/rfc3490.txt">Internationalized Domain
45 * Names for Applications (IDNA)</ulink> standards allow for the use
46 * of Unicode domain names in applications, while providing
47 * backward-compatibility with the old ASCII-only DNS, by defining an
48 * ASCII-Compatible Encoding of any given Unicode name, which can be
49 * used with non-IDN-aware applications and protocols. (For example,
50 * "Παν語.org" maps to "xn--4wa8awb4637h.org".)
51 **/
53 #define IDNA_ACE_PREFIX "xn--"
54 #define IDNA_ACE_PREFIX_LEN 4
56 /* Punycode constants, from RFC 3492. */
58 #define PUNYCODE_BASE 36
59 #define PUNYCODE_TMIN 1
60 #define PUNYCODE_TMAX 26
61 #define PUNYCODE_SKEW 38
62 #define PUNYCODE_DAMP 700
63 #define PUNYCODE_INITIAL_BIAS 72
64 #define PUNYCODE_INITIAL_N 0x80
66 #define PUNYCODE_IS_BASIC(cp) ((guint)(cp) < 0x80)
68 /* Encode/decode a single base-36 digit */
69 static inline gchar
70 encode_digit (guint dig)
72 if (dig < 26)
73 return dig + 'a';
74 else
75 return dig - 26 + '0';
78 static inline guint
79 decode_digit (gchar dig)
81 if (dig >= 'A' && dig <= 'Z')
82 return dig - 'A';
83 else if (dig >= 'a' && dig <= 'z')
84 return dig - 'a';
85 else if (dig >= '0' && dig <= '9')
86 return dig - '0' + 26;
87 else
88 return G_MAXUINT;
91 /* Punycode bias adaptation algorithm, RFC 3492 section 6.1 */
92 static guint
93 adapt (guint delta,
94 guint numpoints,
95 gboolean firsttime)
97 guint k;
99 delta = firsttime ? delta / PUNYCODE_DAMP : delta / 2;
100 delta += delta / numpoints;
102 k = 0;
103 while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2)
105 delta /= PUNYCODE_BASE - PUNYCODE_TMIN;
106 k += PUNYCODE_BASE;
109 return k + ((PUNYCODE_BASE - PUNYCODE_TMIN + 1) * delta /
110 (delta + PUNYCODE_SKEW));
113 /* Punycode encoder, RFC 3492 section 6.3. The algorithm is
114 * sufficiently bizarre that it's not really worth trying to explain
115 * here.
117 static gboolean
118 punycode_encode (const gchar *input_utf8,
119 gsize input_utf8_length,
120 GString *output)
122 guint delta, handled_chars, num_basic_chars, bias, j, q, k, t, digit;
123 gunichar n, m, *input;
124 glong input_length;
125 gboolean success = FALSE;
127 /* Convert from UTF-8 to Unicode code points */
128 input = g_utf8_to_ucs4 (input_utf8, input_utf8_length, NULL,
129 &input_length, NULL);
130 if (!input)
131 return FALSE;
133 /* Copy basic chars */
134 for (j = num_basic_chars = 0; j < input_length; j++)
136 if (PUNYCODE_IS_BASIC (input[j]))
138 g_string_append_c (output, g_ascii_tolower (input[j]));
139 num_basic_chars++;
142 if (num_basic_chars)
143 g_string_append_c (output, '-');
145 handled_chars = num_basic_chars;
147 /* Encode non-basic chars */
148 delta = 0;
149 bias = PUNYCODE_INITIAL_BIAS;
150 n = PUNYCODE_INITIAL_N;
151 while (handled_chars < input_length)
153 /* let m = the minimum {non-basic} code point >= n in the input */
154 for (m = G_MAXUINT, j = 0; j < input_length; j++)
156 if (input[j] >= n && input[j] < m)
157 m = input[j];
160 if (m - n > (G_MAXUINT - delta) / (handled_chars + 1))
161 goto fail;
162 delta += (m - n) * (handled_chars + 1);
163 n = m;
165 for (j = 0; j < input_length; j++)
167 if (input[j] < n)
169 if (++delta == 0)
170 goto fail;
172 else if (input[j] == n)
174 q = delta;
175 for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
177 if (k <= bias)
178 t = PUNYCODE_TMIN;
179 else if (k >= bias + PUNYCODE_TMAX)
180 t = PUNYCODE_TMAX;
181 else
182 t = k - bias;
183 if (q < t)
184 break;
185 digit = t + (q - t) % (PUNYCODE_BASE - t);
186 g_string_append_c (output, encode_digit (digit));
187 q = (q - t) / (PUNYCODE_BASE - t);
190 g_string_append_c (output, encode_digit (q));
191 bias = adapt (delta, handled_chars + 1, handled_chars == num_basic_chars);
192 delta = 0;
193 handled_chars++;
197 delta++;
198 n++;
201 success = TRUE;
203 fail:
204 g_free (input);
205 return success;
208 /* From RFC 3454, Table B.1 */
209 #define idna_is_junk(ch) ((ch) == 0x00AD || (ch) == 0x1806 || (ch) == 0x200B || (ch) == 0x2060 || (ch) == 0xFEFF || (ch) == 0x034F || (ch) == 0x180B || (ch) == 0x180C || (ch) == 0x180D || (ch) == 0x200C || (ch) == 0x200D || ((ch) >= 0xFE00 && (ch) <= 0xFE0F))
211 /* Scan @str for "junk" and return a cleaned-up string if any junk
212 * is found. Else return %NULL.
214 static gchar *
215 remove_junk (const gchar *str,
216 gint len)
218 GString *cleaned = NULL;
219 const gchar *p;
220 gunichar ch;
222 for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
224 ch = g_utf8_get_char (p);
225 if (idna_is_junk (ch))
227 if (!cleaned)
229 cleaned = g_string_new (NULL);
230 g_string_append_len (cleaned, str, p - str);
233 else if (cleaned)
234 g_string_append_unichar (cleaned, ch);
237 if (cleaned)
238 return g_string_free (cleaned, FALSE);
239 else
240 return NULL;
243 static inline gboolean
244 contains_uppercase_letters (const gchar *str,
245 gint len)
247 const gchar *p;
249 for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
251 if (g_unichar_isupper (g_utf8_get_char (p)))
252 return TRUE;
254 return FALSE;
257 static inline gboolean
258 contains_non_ascii (const gchar *str,
259 gint len)
261 const gchar *p;
263 for (p = str; len == -1 ? *p : p < str + len; p++)
265 if ((guchar)*p > 0x80)
266 return TRUE;
268 return FALSE;
271 /* RFC 3454, Appendix C. ish. */
272 static inline gboolean
273 idna_is_prohibited (gunichar ch)
275 switch (g_unichar_type (ch))
277 case G_UNICODE_CONTROL:
278 case G_UNICODE_FORMAT:
279 case G_UNICODE_UNASSIGNED:
280 case G_UNICODE_PRIVATE_USE:
281 case G_UNICODE_SURROGATE:
282 case G_UNICODE_LINE_SEPARATOR:
283 case G_UNICODE_PARAGRAPH_SEPARATOR:
284 case G_UNICODE_SPACE_SEPARATOR:
285 return TRUE;
287 case G_UNICODE_OTHER_SYMBOL:
288 if (ch == 0xFFFC || ch == 0xFFFD ||
289 (ch >= 0x2FF0 && ch <= 0x2FFB))
290 return TRUE;
291 return FALSE;
293 case G_UNICODE_NON_SPACING_MARK:
294 if (ch == 0x0340 || ch == 0x0341)
295 return TRUE;
296 return FALSE;
298 default:
299 return FALSE;
303 /* RFC 3491 IDN cleanup algorithm. */
304 static gchar *
305 nameprep (const gchar *hostname,
306 gint len,
307 gboolean *is_unicode)
309 gchar *name, *tmp = NULL, *p;
311 /* It would be nice if we could do this without repeatedly
312 * allocating strings and converting back and forth between
313 * gunichars and UTF-8... The code does at least avoid doing most of
314 * the sub-operations when they would just be equivalent to a
315 * g_strdup().
318 /* Remove presentation-only characters */
319 name = remove_junk (hostname, len);
320 if (name)
322 tmp = name;
323 len = -1;
325 else
326 name = (gchar *)hostname;
328 /* Convert to lowercase */
329 if (contains_uppercase_letters (name, len))
331 name = g_utf8_strdown (name, len);
332 g_free (tmp);
333 tmp = name;
334 len = -1;
337 /* If there are no UTF8 characters, we're done. */
338 if (!contains_non_ascii (name, len))
340 *is_unicode = FALSE;
341 if (name == (gchar *)hostname)
342 return len == -1 ? g_strdup (hostname) : g_strndup (hostname, len);
343 else
344 return name;
347 *is_unicode = TRUE;
349 /* Normalize */
350 name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC);
351 g_free (tmp);
352 tmp = name;
354 if (!name)
355 return NULL;
357 /* KC normalization may have created more capital letters (eg,
358 * angstrom -> capital A with ring). So we have to lowercasify a
359 * second time. (This is more-or-less how the nameprep algorithm
360 * does it. If tolower(nfkc(tolower(X))) is guaranteed to be the
361 * same as tolower(nfkc(X)), then we could skip the first tolower,
362 * but I'm not sure it is.)
364 if (contains_uppercase_letters (name, -1))
366 name = g_utf8_strdown (name, -1);
367 g_free (tmp);
368 tmp = name;
371 /* Check for prohibited characters */
372 for (p = name; *p; p = g_utf8_next_char (p))
374 if (idna_is_prohibited (g_utf8_get_char (p)))
376 name = NULL;
377 g_free (tmp);
378 goto done;
382 /* FIXME: We're supposed to verify certain constraints on bidi
383 * characters, but glib does not appear to have that information.
386 done:
387 return name;
390 /* RFC 3490, section 3.1 says '.', 0x3002, 0xFF0E, and 0xFF61 count as
391 * label-separating dots. @str must be '\0'-terminated.
393 #define idna_is_dot(str) ( \
394 ((guchar)(str)[0] == '.') || \
395 ((guchar)(str)[0] == 0xE3 && (guchar)(str)[1] == 0x80 && (guchar)(str)[2] == 0x82) || \
396 ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBC && (guchar)(str)[2] == 0x8E) || \
397 ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBD && (guchar)(str)[2] == 0xA1) )
399 static const gchar *
400 idna_end_of_label (const gchar *str)
402 for (; *str; str = g_utf8_next_char (str))
404 if (idna_is_dot (str))
405 return str;
407 return str;
411 * g_hostname_to_ascii:
412 * @hostname: a valid UTF-8 or ASCII hostname
414 * Converts @hostname to its canonical ASCII form; an ASCII-only
415 * string containing no uppercase letters and not ending with a
416 * trailing dot.
418 * Return value: an ASCII hostname, which must be freed, or %NULL if
419 * @hostname is in some way invalid.
421 * Since: 2.22
423 gchar *
424 g_hostname_to_ascii (const gchar *hostname)
426 gchar *name, *label, *p;
427 GString *out;
428 gssize llen, oldlen;
429 gboolean unicode;
431 label = name = nameprep (hostname, -1, &unicode);
432 if (!name || !unicode)
433 return name;
435 out = g_string_new (NULL);
439 unicode = FALSE;
440 for (p = label; *p && !idna_is_dot (p); p++)
442 if ((guchar)*p > 0x80)
443 unicode = TRUE;
446 oldlen = out->len;
447 llen = p - label;
448 if (unicode)
450 if (!strncmp (label, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
451 goto fail;
453 g_string_append (out, IDNA_ACE_PREFIX);
454 if (!punycode_encode (label, llen, out))
455 goto fail;
457 else
458 g_string_append_len (out, label, llen);
460 if (out->len - oldlen > 63)
461 goto fail;
463 label += llen;
464 if (*label)
465 label = g_utf8_next_char (label);
466 if (*label)
467 g_string_append_c (out, '.');
469 while (*label);
471 g_free (name);
472 return g_string_free (out, FALSE);
474 fail:
475 g_free (name);
476 g_string_free (out, TRUE);
477 return NULL;
481 * g_hostname_is_non_ascii:
482 * @hostname: a hostname
484 * Tests if @hostname contains Unicode characters. If this returns
485 * %TRUE, you need to encode the hostname with g_hostname_to_ascii()
486 * before using it in non-IDN-aware contexts.
488 * Note that a hostname might contain a mix of encoded and unencoded
489 * segments, and so it is possible for g_hostname_is_non_ascii() and
490 * g_hostname_is_ascii_encoded() to both return %TRUE for a name.
492 * Return value: %TRUE if @hostname contains any non-ASCII characters
494 * Since: 2.22
496 gboolean
497 g_hostname_is_non_ascii (const gchar *hostname)
499 return contains_non_ascii (hostname, -1);
502 /* Punycode decoder, RFC 3492 section 6.2. As with punycode_encode(),
503 * read the RFC if you want to understand what this is actually doing.
505 static gboolean
506 punycode_decode (const gchar *input,
507 gsize input_length,
508 GString *output)
510 GArray *output_chars;
511 gunichar n;
512 guint i, bias;
513 guint oldi, w, k, digit, t;
514 const gchar *split;
516 n = PUNYCODE_INITIAL_N;
517 i = 0;
518 bias = PUNYCODE_INITIAL_BIAS;
520 split = input + input_length - 1;
521 while (split > input && *split != '-')
522 split--;
523 if (split > input)
525 output_chars = g_array_sized_new (FALSE, FALSE, sizeof (gunichar),
526 split - input);
527 input_length -= (split - input) + 1;
528 while (input < split)
530 gunichar ch = (gunichar)*input++;
531 if (!PUNYCODE_IS_BASIC (ch))
532 goto fail;
533 g_array_append_val (output_chars, ch);
535 input++;
537 else
538 output_chars = g_array_new (FALSE, FALSE, sizeof (gunichar));
540 while (input_length)
542 oldi = i;
543 w = 1;
544 for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
546 if (!input_length--)
547 goto fail;
548 digit = decode_digit (*input++);
549 if (digit >= PUNYCODE_BASE)
550 goto fail;
551 if (digit > (G_MAXUINT - i) / w)
552 goto fail;
553 i += digit * w;
554 if (k <= bias)
555 t = PUNYCODE_TMIN;
556 else if (k >= bias + PUNYCODE_TMAX)
557 t = PUNYCODE_TMAX;
558 else
559 t = k - bias;
560 if (digit < t)
561 break;
562 if (w > G_MAXUINT / (PUNYCODE_BASE - t))
563 goto fail;
564 w *= (PUNYCODE_BASE - t);
567 bias = adapt (i - oldi, output_chars->len + 1, oldi == 0);
569 if (i / (output_chars->len + 1) > G_MAXUINT - n)
570 goto fail;
571 n += i / (output_chars->len + 1);
572 i %= (output_chars->len + 1);
574 g_array_insert_val (output_chars, i++, n);
577 for (i = 0; i < output_chars->len; i++)
578 g_string_append_unichar (output, g_array_index (output_chars, gunichar, i));
579 g_array_free (output_chars, TRUE);
580 return TRUE;
582 fail:
583 g_array_free (output_chars, TRUE);
584 return FALSE;
588 * g_hostname_to_unicode:
589 * @hostname: a valid UTF-8 or ASCII hostname
591 * Converts @hostname to its canonical presentation form; a UTF-8
592 * string in Unicode normalization form C, containing no uppercase
593 * letters, no forbidden characters, and no ASCII-encoded segments,
594 * and not ending with a trailing dot.
596 * Of course if @hostname is not an internationalized hostname, then
597 * the canonical presentation form will be entirely ASCII.
599 * Return value: a UTF-8 hostname, which must be freed, or %NULL if
600 * @hostname is in some way invalid.
602 * Since: 2.22
604 gchar *
605 g_hostname_to_unicode (const gchar *hostname)
607 GString *out;
608 gssize llen;
610 out = g_string_new (NULL);
614 llen = idna_end_of_label (hostname) - hostname;
615 if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
617 hostname += IDNA_ACE_PREFIX_LEN;
618 llen -= IDNA_ACE_PREFIX_LEN;
619 if (!punycode_decode (hostname, llen, out))
621 g_string_free (out, TRUE);
622 return NULL;
625 else
627 gboolean unicode;
628 gchar *canonicalized = nameprep (hostname, llen, &unicode);
630 if (!canonicalized)
632 g_string_free (out, TRUE);
633 return NULL;
635 g_string_append (out, canonicalized);
636 g_free (canonicalized);
639 hostname += llen;
640 if (*hostname)
641 hostname = g_utf8_next_char (hostname);
642 if (*hostname)
643 g_string_append_c (out, '.');
645 while (*hostname);
647 return g_string_free (out, FALSE);
651 * g_hostname_is_ascii_encoded:
652 * @hostname: a hostname
654 * Tests if @hostname contains segments with an ASCII-compatible
655 * encoding of an Internationalized Domain Name. If this returns
656 * %TRUE, you should decode the hostname with g_hostname_to_unicode()
657 * before displaying it to the user.
659 * Note that a hostname might contain a mix of encoded and unencoded
660 * segments, and so it is possible for g_hostname_is_non_ascii() and
661 * g_hostname_is_ascii_encoded() to both return %TRUE for a name.
663 * Return value: %TRUE if @hostname contains any ASCII-encoded
664 * segments.
666 * Since: 2.22
668 gboolean
669 g_hostname_is_ascii_encoded (const gchar *hostname)
671 while (1)
673 if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
674 return TRUE;
675 hostname = idna_end_of_label (hostname);
676 if (*hostname)
677 hostname = g_utf8_next_char (hostname);
678 if (!*hostname)
679 return FALSE;
684 * g_hostname_is_ip_address:
685 * @hostname: a hostname (or IP address in string form)
687 * Tests if @hostname is the string form of an IPv4 or IPv6 address.
688 * (Eg, "192.168.0.1".)
690 * Return value: %TRUE if @hostname is an IP address
692 * Since: 2.22
694 gboolean
695 g_hostname_is_ip_address (const gchar *hostname)
697 gchar *p, *end;
698 gint nsegments, octet;
700 /* On Linux we could implement this using inet_pton, but the Windows
701 * equivalent of that requires linking against winsock, so we just
702 * figure this out ourselves. Tested by tests/hostutils.c.
705 p = (char *)hostname;
707 if (strchr (p, ':'))
709 gboolean skipped;
711 /* If it contains a ':', it's an IPv6 address (assuming it's an
712 * IP address at all). This consists of eight ':'-separated
713 * segments, each containing a 1-4 digit hex number, except that
714 * optionally: (a) the last two segments can be replaced by an
715 * IPv4 address, and (b) a single span of 1 to 8 "0000" segments
716 * can be replaced with just "::".
719 nsegments = 0;
720 skipped = FALSE;
721 while (*p && nsegments < 8)
723 /* Each segment after the first must be preceded by a ':'.
724 * (We also handle half of the "string starts with ::" case
725 * here.)
727 if (p != (char *)hostname || (p[0] == ':' && p[1] == ':'))
729 if (*p != ':')
730 return FALSE;
731 p++;
734 /* If there's another ':', it means we're skipping some segments */
735 if (*p == ':' && !skipped)
737 skipped = TRUE;
738 nsegments++;
740 /* Handle the "string ends with ::" case */
741 if (!p[1])
742 p++;
744 continue;
747 /* Read the segment, make sure it's valid. */
748 for (end = p; g_ascii_isxdigit (*end); end++)
750 if (end == p || end > p + 4)
751 return FALSE;
753 if (*end == '.')
755 if ((nsegments == 6 && !skipped) || (nsegments <= 6 && skipped))
756 goto parse_ipv4;
757 else
758 return FALSE;
761 nsegments++;
762 p = end;
765 return !*p && (nsegments == 8 || skipped);
768 parse_ipv4:
770 /* Parse IPv4: N.N.N.N, where each N <= 255 and doesn't have leading 0s. */
771 for (nsegments = 0; nsegments < 4; nsegments++)
773 if (nsegments != 0)
775 if (*p != '.')
776 return FALSE;
777 p++;
780 /* Check the segment; a little tricker than the IPv6 case since
781 * we can't allow extra leading 0s, and we can't assume that all
782 * strings of valid length are within range.
784 octet = 0;
785 if (*p == '0')
786 end = p + 1;
787 else
789 for (end = p; g_ascii_isdigit (*end); end++)
790 octet = 10 * octet + (*end - '0');
792 if (end == p || end > p + 3 || octet > 255)
793 return FALSE;
795 p = end;
798 /* If there's nothing left to parse, then it's ok. */
799 return !*p;