24a7d37bbda962f00da996d0106e1ff7ef0686c7
[platform/upstream/glib.git] / glib / ghostutils.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GLIB - Library of useful routines for C programming
4  * Copyright (C) 2008 Red Hat, Inc.
5  *
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.1 of the License, or (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21 #include "glibconfig.h"
22
23 #include <string.h>
24
25 #ifdef G_OS_UNIX
26 #include <unistd.h>
27 #endif
28
29 #include "ghostutils.h"
30
31 #include "garray.h"
32 #include "gmem.h"
33 #include "gstring.h"
34 #include "gstrfuncs.h"
35 #include "glibintl.h"
36
37 #ifdef G_PLATFORM_WIN32
38 #include <windows.h>
39 #endif
40
41
42 /**
43  * SECTION:ghostutils
44  * @short_description: Internet hostname utilities
45  *
46  * Functions for manipulating internet hostnames; in particular, for
47  * converting between Unicode and ASCII-encoded forms of
48  * Internationalized Domain Names (IDNs).
49  *
50  * The
51  * [Internationalized Domain Names for Applications (IDNA)](http://www.ietf.org/rfc/rfc3490.txt)
52  * standards allow for the use
53  * of Unicode domain names in applications, while providing
54  * backward-compatibility with the old ASCII-only DNS, by defining an
55  * ASCII-Compatible Encoding of any given Unicode name, which can be
56  * used with non-IDN-aware applications and protocols. (For example,
57  * "Παν語.org" maps to "xn--4wa8awb4637h.org".)
58  **/
59
60 #define IDNA_ACE_PREFIX     "xn--"
61 #define IDNA_ACE_PREFIX_LEN 4
62
63 /* Punycode constants, from RFC 3492. */
64
65 #define PUNYCODE_BASE          36
66 #define PUNYCODE_TMIN           1
67 #define PUNYCODE_TMAX          26
68 #define PUNYCODE_SKEW          38
69 #define PUNYCODE_DAMP         700
70 #define PUNYCODE_INITIAL_BIAS  72
71 #define PUNYCODE_INITIAL_N   0x80
72
73 #define PUNYCODE_IS_BASIC(cp) ((guint)(cp) < 0x80)
74
75 /* Encode/decode a single base-36 digit */
76 static inline gchar
77 encode_digit (guint dig)
78 {
79   if (dig < 26)
80     return dig + 'a';
81   else
82     return dig - 26 + '0';
83 }
84
85 static inline guint
86 decode_digit (gchar dig)
87 {
88   if (dig >= 'A' && dig <= 'Z')
89     return dig - 'A';
90   else if (dig >= 'a' && dig <= 'z')
91     return dig - 'a';
92   else if (dig >= '0' && dig <= '9')
93     return dig - '0' + 26;
94   else
95     return G_MAXUINT;
96 }
97
98 /* Punycode bias adaptation algorithm, RFC 3492 section 6.1 */
99 static guint
100 adapt (guint    delta,
101        guint    numpoints,
102        gboolean firsttime)
103 {
104   guint k;
105
106   delta = firsttime ? delta / PUNYCODE_DAMP : delta / 2;
107   delta += delta / numpoints;
108
109   k = 0;
110   while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2)
111     {
112       delta /= PUNYCODE_BASE - PUNYCODE_TMIN;
113       k += PUNYCODE_BASE;
114     }
115
116   return k + ((PUNYCODE_BASE - PUNYCODE_TMIN + 1) * delta /
117               (delta + PUNYCODE_SKEW));
118 }
119
120 /* Punycode encoder, RFC 3492 section 6.3. The algorithm is
121  * sufficiently bizarre that it's not really worth trying to explain
122  * here.
123  */
124 static gboolean
125 punycode_encode (const gchar *input_utf8,
126                  gsize        input_utf8_length,
127                  GString     *output)
128 {
129   guint delta, handled_chars, num_basic_chars, bias, j, q, k, t, digit;
130   gunichar n, m, *input;
131   glong written_chars;
132   gsize input_length;
133   gboolean success = FALSE;
134
135   /* Convert from UTF-8 to Unicode code points */
136   input = g_utf8_to_ucs4 (input_utf8, input_utf8_length, NULL,
137                           &written_chars, NULL);
138   if (!input)
139     return FALSE;
140
141   input_length = (gsize) (written_chars > 0 ? written_chars : 0);
142
143   /* Copy basic chars */
144   for (j = num_basic_chars = 0; j < input_length; j++)
145     {
146       if (PUNYCODE_IS_BASIC (input[j]))
147         {
148           g_string_append_c (output, g_ascii_tolower (input[j]));
149           num_basic_chars++;
150         }
151     }
152   if (num_basic_chars)
153     g_string_append_c (output, '-');
154
155   handled_chars = num_basic_chars;
156
157   /* Encode non-basic chars */
158   delta = 0;
159   bias = PUNYCODE_INITIAL_BIAS;
160   n = PUNYCODE_INITIAL_N;
161   while (handled_chars < input_length)
162     {
163       /* let m = the minimum {non-basic} code point >= n in the input */
164       for (m = G_MAXUINT, j = 0; j < input_length; j++)
165         {
166           if (input[j] >= n && input[j] < m)
167             m = input[j];
168         }
169
170       if (m - n > (G_MAXUINT - delta) / (handled_chars + 1))
171         goto fail;
172       delta += (m - n) * (handled_chars + 1);
173       n = m;
174
175       for (j = 0; j < input_length; j++)
176         {
177           if (input[j] < n)
178             {
179               if (++delta == 0)
180                 goto fail;
181             }
182           else if (input[j] == n)
183             {
184               q = delta;
185               for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
186                 {
187                   if (k <= bias)
188                     t = PUNYCODE_TMIN;
189                   else if (k >= bias + PUNYCODE_TMAX)
190                     t = PUNYCODE_TMAX;
191                   else
192                     t = k - bias;
193                   if (q < t)
194                     break;
195                   digit = t + (q - t) % (PUNYCODE_BASE - t);
196                   g_string_append_c (output, encode_digit (digit));
197                   q = (q - t) / (PUNYCODE_BASE - t);
198                 }
199
200               g_string_append_c (output, encode_digit (q));
201               bias = adapt (delta, handled_chars + 1, handled_chars == num_basic_chars);
202               delta = 0;
203               handled_chars++;
204             }
205         }
206
207       delta++;
208       n++;
209     }
210
211   success = TRUE;
212
213  fail:
214   g_free (input);
215   return success;
216 }
217
218 /* From RFC 3454, Table B.1 */
219 #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))
220
221 /* Scan @str for "junk" and return a cleaned-up string if any junk
222  * is found. Else return %NULL.
223  */
224 static gchar *
225 remove_junk (const gchar *str,
226              gint         len)
227 {
228   GString *cleaned = NULL;
229   const gchar *p;
230   gunichar ch;
231
232   for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
233     {
234       ch = g_utf8_get_char (p);
235       if (idna_is_junk (ch))
236         {
237           if (!cleaned)
238             {
239               cleaned = g_string_new (NULL);
240               g_string_append_len (cleaned, str, p - str);
241             }
242         }
243       else if (cleaned)
244         g_string_append_unichar (cleaned, ch);
245     }
246
247   if (cleaned)
248     return g_string_free (cleaned, FALSE);
249   else
250     return NULL;
251 }
252
253 static inline gboolean
254 contains_uppercase_letters (const gchar *str,
255                             gint         len)
256 {
257   const gchar *p;
258
259   for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
260     {
261       if (g_unichar_isupper (g_utf8_get_char (p)))
262         return TRUE;
263     }
264   return FALSE;
265 }
266
267 static inline gboolean
268 contains_non_ascii (const gchar *str,
269                     gint         len)
270 {
271   const gchar *p;
272
273   for (p = str; len == -1 ? *p : p < str + len; p++)
274     {
275       if ((guchar)*p > 0x80)
276         return TRUE;
277     }
278   return FALSE;
279 }
280
281 /* RFC 3454, Appendix C. ish. */
282 static inline gboolean
283 idna_is_prohibited (gunichar ch)
284 {
285   switch (g_unichar_type (ch))
286     {
287     case G_UNICODE_CONTROL:
288     case G_UNICODE_FORMAT:
289     case G_UNICODE_UNASSIGNED:
290     case G_UNICODE_PRIVATE_USE:
291     case G_UNICODE_SURROGATE:
292     case G_UNICODE_LINE_SEPARATOR:
293     case G_UNICODE_PARAGRAPH_SEPARATOR:
294     case G_UNICODE_SPACE_SEPARATOR:
295       return TRUE;
296
297     case G_UNICODE_OTHER_SYMBOL:
298       if (ch == 0xFFFC || ch == 0xFFFD ||
299           (ch >= 0x2FF0 && ch <= 0x2FFB))
300         return TRUE;
301       return FALSE;
302
303     case G_UNICODE_NON_SPACING_MARK:
304       if (ch == 0x0340 || ch == 0x0341)
305         return TRUE;
306       return FALSE;
307
308     default:
309       return FALSE;
310     }
311 }
312
313 /* RFC 3491 IDN cleanup algorithm. */
314 static gchar *
315 nameprep (const gchar *hostname,
316           gint         len,
317           gboolean    *is_unicode)
318 {
319   gchar *name, *tmp = NULL, *p;
320
321   /* It would be nice if we could do this without repeatedly
322    * allocating strings and converting back and forth between
323    * gunichars and UTF-8... The code does at least avoid doing most of
324    * the sub-operations when they would just be equivalent to a
325    * g_strdup().
326    */
327
328   /* Remove presentation-only characters */
329   name = remove_junk (hostname, len);
330   if (name)
331     {
332       tmp = name;
333       len = -1;
334     }
335   else
336     name = (gchar *)hostname;
337
338   /* Convert to lowercase */
339   if (contains_uppercase_letters (name, len))
340     {
341       name = g_utf8_strdown (name, len);
342       g_free (tmp);
343       tmp = name;
344       len = -1;
345     }
346
347   /* If there are no UTF8 characters, we're done. */
348   if (!contains_non_ascii (name, len))
349     {
350       *is_unicode = FALSE;
351       if (name == (gchar *)hostname)
352         return len == -1 ? g_strdup (hostname) : g_strndup (hostname, len);
353       else
354         return name;
355     }
356
357   *is_unicode = TRUE;
358
359   /* Normalize */
360   name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC);
361   g_free (tmp);
362   tmp = name;
363
364   if (!name)
365     return NULL;
366
367   /* KC normalization may have created more capital letters (eg,
368    * angstrom -> capital A with ring). So we have to lowercasify a
369    * second time. (This is more-or-less how the nameprep algorithm
370    * does it. If tolower(nfkc(tolower(X))) is guaranteed to be the
371    * same as tolower(nfkc(X)), then we could skip the first tolower,
372    * but I'm not sure it is.)
373    */
374   if (contains_uppercase_letters (name, -1))
375     {
376       name = g_utf8_strdown (name, -1);
377       g_free (tmp);
378       tmp = name;
379     }
380
381   /* Check for prohibited characters */
382   for (p = name; *p; p = g_utf8_next_char (p))
383     {
384       if (idna_is_prohibited (g_utf8_get_char (p)))
385         {
386           name = NULL;
387           g_free (tmp);
388           goto done;
389         }
390     }
391
392   /* FIXME: We're supposed to verify certain constraints on bidi
393    * characters, but glib does not appear to have that information.
394    */
395
396  done:
397   return name;
398 }
399
400 /* RFC 3490, section 3.1 says '.', 0x3002, 0xFF0E, and 0xFF61 count as
401  * label-separating dots. @str must be '\0'-terminated.
402  */
403 #define idna_is_dot(str) ( \
404   ((guchar)(str)[0] == '.') ||                                                 \
405   ((guchar)(str)[0] == 0xE3 && (guchar)(str)[1] == 0x80 && (guchar)(str)[2] == 0x82) || \
406   ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBC && (guchar)(str)[2] == 0x8E) || \
407   ((guchar)(str)[0] == 0xEF && (guchar)(str)[1] == 0xBD && (guchar)(str)[2] == 0xA1) )
408
409 static const gchar *
410 idna_end_of_label (const gchar *str)
411 {
412   for (; *str; str = g_utf8_next_char (str))
413     {
414       if (idna_is_dot (str))
415         return str;
416     }
417   return str;
418 }
419
420 static gsize
421 get_hostname_max_length_bytes (void)
422 {
423 #if defined(G_OS_WIN32)
424   wchar_t tmp[MAX_COMPUTERNAME_LENGTH];
425   return sizeof (tmp) / sizeof (tmp[0]);
426 #elif defined(_SC_HOST_NAME_MAX)
427   glong max = sysconf (_SC_HOST_NAME_MAX);
428   if (max > 0)
429     return (gsize) max;
430
431 #ifdef HOST_NAME_MAX
432   return HOST_NAME_MAX;
433 #else
434   return _POSIX_HOST_NAME_MAX;
435 #endif /* HOST_NAME_MAX */
436 #else
437   /* Fallback to some reasonable value
438    * See https://stackoverflow.com/questions/8724954/what-is-the-maximum-number-of-characters-for-a-host-name-in-unix/28918017#28918017 */
439   return 255;
440 #endif
441 }
442
443 /* Returns %TRUE if `strlen (str) > comparison_length`, but without actually
444  * running `strlen(str)`, as that would take a very long time for long
445  * (untrusted) input strings. */
446 static gboolean
447 strlen_greater_than (const gchar *str,
448                      gsize        comparison_length)
449 {
450   gsize i;
451
452   for (i = 0; str[i] != '\0'; i++)
453     if (i > comparison_length)
454       return TRUE;
455
456   return FALSE;
457 }
458
459 /**
460  * g_hostname_to_ascii:
461  * @hostname: a valid UTF-8 or ASCII hostname
462  *
463  * Converts @hostname to its canonical ASCII form; an ASCII-only
464  * string containing no uppercase letters and not ending with a
465  * trailing dot.
466  *
467  * Returns: (nullable) (transfer full): an ASCII hostname, which must be freed,
468  *    or %NULL if @hostname is in some way invalid.
469  *
470  * Since: 2.22
471  **/
472 gchar *
473 g_hostname_to_ascii (const gchar *hostname)
474 {
475   gchar *name, *label, *p;
476   GString *out;
477   gssize llen, oldlen;
478   gboolean unicode;
479   gsize hostname_max_length_bytes = get_hostname_max_length_bytes ();
480
481   /* Do an initial check on the hostname length, as overlong hostnames take a
482    * long time in the IDN cleanup algorithm in nameprep(). The ultimate
483    * restriction is that the IDN-decoded (i.e. pure ASCII) hostname cannot be
484    * longer than 255 bytes. That’s the least restrictive limit on hostname
485    * length of all the ways hostnames can be interpreted. Typically, the
486    * hostname will be an FQDN, which is limited to 253 bytes long. POSIX
487    * hostnames are limited to `get_hostname_max_length_bytes()` (typically 255
488    * bytes).
489    *
490    * See https://stackoverflow.com/a/28918017/2931197
491    *
492    * It’s possible for a hostname to be %-encoded, in which case its decoded
493    * length will be as much as 3× shorter.
494    *
495    * It’s also possible for a hostname to use overlong UTF-8 encodings, in which
496    * case its decoded length will be as much as 4× shorter.
497    *
498    * Note: This check is not intended as an absolute guarantee that a hostname
499    * is the right length and will be accepted by other systems. It’s intended to
500    * stop wildly-invalid hostnames from taking forever in nameprep().
501    */
502   if (hostname_max_length_bytes <= G_MAXSIZE / 4 &&
503       strlen_greater_than (hostname, 4 * MAX (255, hostname_max_length_bytes)))
504     return NULL;
505
506   label = name = nameprep (hostname, -1, &unicode);
507   if (!name || !unicode)
508     return name;
509
510   out = g_string_new (NULL);
511
512   do
513     {
514       unicode = FALSE;
515       for (p = label; *p && !idna_is_dot (p); p++)
516         {
517           if ((guchar)*p > 0x80)
518             unicode = TRUE;
519         }
520
521       oldlen = out->len;
522       llen = p - label;
523       if (unicode)
524         {
525           if (!strncmp (label, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
526             goto fail;
527
528           g_string_append (out, IDNA_ACE_PREFIX);
529           if (!punycode_encode (label, llen, out))
530             goto fail;
531         }
532       else
533         g_string_append_len (out, label, llen);
534
535       if (out->len - oldlen > 63)
536         goto fail;
537
538       label += llen;
539       if (*label)
540         label = g_utf8_next_char (label);
541       if (*label)
542         g_string_append_c (out, '.');
543     }
544   while (*label);
545
546   g_free (name);
547   return g_string_free (out, FALSE);
548
549  fail:
550   g_free (name);
551   g_string_free (out, TRUE);
552   return NULL;
553 }
554
555 /**
556  * g_hostname_is_non_ascii:
557  * @hostname: a hostname
558  *
559  * Tests if @hostname contains Unicode characters. If this returns
560  * %TRUE, you need to encode the hostname with g_hostname_to_ascii()
561  * before using it in non-IDN-aware contexts.
562  *
563  * Note that a hostname might contain a mix of encoded and unencoded
564  * segments, and so it is possible for g_hostname_is_non_ascii() and
565  * g_hostname_is_ascii_encoded() to both return %TRUE for a name.
566  *
567  * Returns: %TRUE if @hostname contains any non-ASCII characters
568  *
569  * Since: 2.22
570  **/
571 gboolean
572 g_hostname_is_non_ascii (const gchar *hostname)
573 {
574   return contains_non_ascii (hostname, -1);
575 }
576
577 /* Punycode decoder, RFC 3492 section 6.2. As with punycode_encode(),
578  * read the RFC if you want to understand what this is actually doing.
579  */
580 static gboolean
581 punycode_decode (const gchar *input,
582                  gsize        input_length,
583                  GString     *output)
584 {
585   GArray *output_chars;
586   gunichar n;
587   guint i, bias;
588   guint oldi, w, k, digit, t;
589   const gchar *split;
590
591   n = PUNYCODE_INITIAL_N;
592   i = 0;
593   bias = PUNYCODE_INITIAL_BIAS;
594
595   split = input + input_length - 1;
596   while (split > input && *split != '-')
597     split--;
598   if (split > input)
599     {
600       output_chars = g_array_sized_new (FALSE, FALSE, sizeof (gunichar),
601                                         split - input);
602       input_length -= (split - input) + 1;
603       while (input < split)
604         {
605           gunichar ch = (gunichar)*input++;
606           if (!PUNYCODE_IS_BASIC (ch))
607             goto fail;
608           g_array_append_val (output_chars, ch);
609         }
610       input++;
611     }
612   else
613     output_chars = g_array_new (FALSE, FALSE, sizeof (gunichar));
614
615   while (input_length)
616     {
617       oldi = i;
618       w = 1;
619       for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
620         {
621           if (!input_length--)
622             goto fail;
623           digit = decode_digit (*input++);
624           if (digit >= PUNYCODE_BASE)
625             goto fail;
626           if (digit > (G_MAXUINT - i) / w)
627             goto fail;
628           i += digit * w;
629           if (k <= bias)
630             t = PUNYCODE_TMIN;
631           else if (k >= bias + PUNYCODE_TMAX)
632             t = PUNYCODE_TMAX;
633           else
634             t = k - bias;
635           if (digit < t)
636             break;
637           if (w > G_MAXUINT / (PUNYCODE_BASE - t))
638             goto fail;
639           w *= (PUNYCODE_BASE - t);
640         }
641
642       bias = adapt (i - oldi, output_chars->len + 1, oldi == 0);
643
644       if (i / (output_chars->len + 1) > G_MAXUINT - n)
645         goto fail;
646       n += i / (output_chars->len + 1);
647       i %= (output_chars->len + 1);
648
649       g_array_insert_val (output_chars, i++, n);
650     }
651
652   for (i = 0; i < output_chars->len; i++)
653     g_string_append_unichar (output, g_array_index (output_chars, gunichar, i));
654   g_array_free (output_chars, TRUE);
655   return TRUE;
656
657  fail:
658   g_array_free (output_chars, TRUE);
659   return FALSE;
660 }
661
662 /**
663  * g_hostname_to_unicode:
664  * @hostname: a valid UTF-8 or ASCII hostname
665  *
666  * Converts @hostname to its canonical presentation form; a UTF-8
667  * string in Unicode normalization form C, containing no uppercase
668  * letters, no forbidden characters, and no ASCII-encoded segments,
669  * and not ending with a trailing dot.
670  *
671  * Of course if @hostname is not an internationalized hostname, then
672  * the canonical presentation form will be entirely ASCII.
673  *
674  * Returns: (nullable) (transfer full): a UTF-8 hostname, which must be freed,
675  *    or %NULL if @hostname is in some way invalid.
676  *
677  * Since: 2.22
678  **/
679 gchar *
680 g_hostname_to_unicode (const gchar *hostname)
681 {
682   GString *out;
683   gssize llen;
684   gsize hostname_max_length_bytes = get_hostname_max_length_bytes ();
685
686   /* See the comment at the top of g_hostname_to_ascii(). */
687   if (hostname_max_length_bytes <= G_MAXSIZE / 4 &&
688       strlen_greater_than (hostname, 4 * MAX (255, hostname_max_length_bytes)))
689     return NULL;
690
691   out = g_string_new (NULL);
692
693   do
694     {
695       llen = idna_end_of_label (hostname) - hostname;
696       if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
697         {
698           hostname += IDNA_ACE_PREFIX_LEN;
699           llen -= IDNA_ACE_PREFIX_LEN;
700           if (!punycode_decode (hostname, llen, out))
701             {
702               g_string_free (out, TRUE);
703               return NULL;
704             }
705         }
706       else
707         {
708           gboolean unicode;
709           gchar *canonicalized = nameprep (hostname, llen, &unicode);
710
711           if (!canonicalized)
712             {
713               g_string_free (out, TRUE);
714               return NULL;
715             }
716           g_string_append (out, canonicalized);
717           g_free (canonicalized);
718         }
719
720       hostname += llen;
721       if (*hostname)
722         hostname = g_utf8_next_char (hostname);
723       if (*hostname)
724         g_string_append_c (out, '.');
725     }
726   while (*hostname);
727
728   return g_string_free (out, FALSE);
729 }
730
731 /**
732  * g_hostname_is_ascii_encoded:
733  * @hostname: a hostname
734  *
735  * Tests if @hostname contains segments with an ASCII-compatible
736  * encoding of an Internationalized Domain Name. If this returns
737  * %TRUE, you should decode the hostname with g_hostname_to_unicode()
738  * before displaying it to the user.
739  *
740  * Note that a hostname might contain a mix of encoded and unencoded
741  * segments, and so it is possible for g_hostname_is_non_ascii() and
742  * g_hostname_is_ascii_encoded() to both return %TRUE for a name.
743  *
744  * Returns: %TRUE if @hostname contains any ASCII-encoded
745  * segments.
746  *
747  * Since: 2.22
748  **/
749 gboolean
750 g_hostname_is_ascii_encoded (const gchar *hostname)
751 {
752   while (1)
753     {
754       if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
755         return TRUE;
756       hostname = idna_end_of_label (hostname);
757       if (*hostname)
758         hostname = g_utf8_next_char (hostname);
759       if (!*hostname)
760         return FALSE;
761     }
762 }
763
764 /**
765  * g_hostname_is_ip_address:
766  * @hostname: a hostname (or IP address in string form)
767  *
768  * Tests if @hostname is the string form of an IPv4 or IPv6 address.
769  * (Eg, "192.168.0.1".)
770  *
771  * Since 2.66, IPv6 addresses with a zone-id are accepted (RFC6874).
772  *
773  * Returns: %TRUE if @hostname is an IP address
774  *
775  * Since: 2.22
776  **/
777 gboolean
778 g_hostname_is_ip_address (const gchar *hostname)
779 {
780   gchar *p, *end;
781   gint nsegments, octet;
782
783   /* On Linux we could implement this using inet_pton, but the Windows
784    * equivalent of that requires linking against winsock, so we just
785    * figure this out ourselves. Tested by tests/hostutils.c.
786    */
787
788   p = (char *)hostname;
789
790   if (strchr (p, ':'))
791     {
792       gboolean skipped;
793
794       /* If it contains a ':', it's an IPv6 address (assuming it's an
795        * IP address at all). This consists of eight ':'-separated
796        * segments, each containing a 1-4 digit hex number, except that
797        * optionally: (a) the last two segments can be replaced by an
798        * IPv4 address, and (b) a single span of 1 to 8 "0000" segments
799        * can be replaced with just "::".
800        */
801
802       nsegments = 0;
803       skipped = FALSE;
804       while (*p && *p != '%' && nsegments < 8)
805         {
806           /* Each segment after the first must be preceded by a ':'.
807            * (We also handle half of the "string starts with ::" case
808            * here.)
809            */
810           if (p != (char *)hostname || (p[0] == ':' && p[1] == ':'))
811             {
812               if (*p != ':')
813                 return FALSE;
814               p++;
815             }
816
817           /* If there's another ':', it means we're skipping some segments */
818           if (*p == ':' && !skipped)
819             {
820               skipped = TRUE;
821               nsegments++;
822
823               /* Handle the "string ends with ::" case */
824               if (!p[1])
825                 p++;
826
827               continue;
828             }
829
830           /* Read the segment, make sure it's valid. */
831           for (end = p; g_ascii_isxdigit (*end); end++)
832             ;
833           if (end == p || end > p + 4)
834             return FALSE;
835
836           if (*end == '.')
837             {
838               if ((nsegments == 6 && !skipped) || (nsegments <= 6 && skipped))
839                 goto parse_ipv4;
840               else
841                 return FALSE;
842             }
843
844           nsegments++;
845           p = end;
846         }
847
848       return (!*p || (p[0] == '%' && p[1])) && (nsegments == 8 || skipped);
849     }
850
851  parse_ipv4:
852
853   /* Parse IPv4: N.N.N.N, where each N <= 255 and doesn't have leading 0s. */
854   for (nsegments = 0; nsegments < 4; nsegments++)
855     {
856       if (nsegments != 0)
857         {
858           if (*p != '.')
859             return FALSE;
860           p++;
861         }
862
863       /* Check the segment; a little tricker than the IPv6 case since
864        * we can't allow extra leading 0s, and we can't assume that all
865        * strings of valid length are within range.
866        */
867       octet = 0;
868       if (*p == '0')
869         end = p + 1;
870       else
871         {
872           for (end = p; g_ascii_isdigit (*end); end++)
873             {
874               octet = 10 * octet + (*end - '0');
875
876               if (octet > 255)
877                 break;
878             }
879         }
880       if (end == p || end > p + 3 || octet > 255)
881         return FALSE;
882
883       p = end;
884     }
885
886   /* If there's nothing left to parse, then it's ok. */
887   return !*p;
888 }