99afe9a7e84fa52a348232c03e7187a9b9832fea
[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 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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #include "ghostutils.h"
27
28 #include "garray.h"
29 #include "gmem.h"
30 #include "gstring.h"
31 #include "gstrfuncs.h"
32 #include "glibintl.h"
33
34
35 /**
36  * SECTION:ghostutils
37  * @short_description: Internet hostname utilities
38  *
39  * Functions for manipulating internet hostnames; in particular, for
40  * converting between Unicode and ASCII-encoded forms of
41  * Internationalized Domain Names (IDNs).
42  *
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  **/
52
53 #define IDNA_ACE_PREFIX     "xn--"
54 #define IDNA_ACE_PREFIX_LEN 4
55
56 /* Punycode constants, from RFC 3492. */
57
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
65
66 #define PUNYCODE_IS_BASIC(cp) ((guint)(cp) < 0x80)
67
68 /* Encode/decode a single base-36 digit */
69 static inline gchar
70 encode_digit (guint dig)
71 {
72   if (dig < 26)
73     return dig + 'a';
74   else
75     return dig - 26 + '0';
76 }
77
78 static inline guint
79 decode_digit (gchar dig)
80 {
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;
89 }
90
91 /* Punycode bias adaptation algorithm, RFC 3492 section 6.1 */
92 static guint
93 adapt (guint    delta,
94        guint    numpoints,
95        gboolean firsttime)
96 {
97   guint k;
98
99   delta = firsttime ? delta / PUNYCODE_DAMP : delta / 2;
100   delta += delta / numpoints;
101
102   k = 0;
103   while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2)
104     {
105       delta /= PUNYCODE_BASE - PUNYCODE_TMIN;
106       k += PUNYCODE_BASE;
107     }
108
109   return k + ((PUNYCODE_BASE - PUNYCODE_TMIN + 1) * delta /
110               (delta + PUNYCODE_SKEW));
111 }
112
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.
116  */
117 static gboolean
118 punycode_encode (const gchar *input_utf8,
119                  gsize        input_utf8_length,
120                  GString     *output)
121 {
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;
126
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;
132
133   /* Copy basic chars */
134   for (j = num_basic_chars = 0; j < input_length; j++)
135     {
136       if (PUNYCODE_IS_BASIC (input[j]))
137         {
138           g_string_append_c (output, g_ascii_tolower (input[j]));
139           num_basic_chars++;
140         }
141     }
142   if (num_basic_chars)
143     g_string_append_c (output, '-');
144
145   handled_chars = num_basic_chars;
146
147   /* Encode non-basic chars */
148   delta = 0;
149   bias = PUNYCODE_INITIAL_BIAS;
150   n = PUNYCODE_INITIAL_N;
151   while (handled_chars < input_length)
152     {
153       /* let m = the minimum {non-basic} code point >= n in the input */
154       for (m = G_MAXUINT, j = 0; j < input_length; j++)
155         {
156           if (input[j] >= n && input[j] < m)
157             m = input[j];
158         }
159
160       if (m - n > (G_MAXUINT - delta) / (handled_chars + 1))
161         goto fail;
162       delta += (m - n) * (handled_chars + 1);
163       n = m;
164
165       for (j = 0; j < input_length; j++)
166         {
167           if (input[j] < n)
168             {
169               if (++delta == 0)
170                 goto fail;
171             }
172           else if (input[j] == n)
173             {
174               q = delta;
175               for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
176                 {
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);
188                 }
189
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++;
194             }
195         }
196
197       delta++;
198       n++;
199     }
200
201   success = TRUE;
202
203  fail:
204   g_free (input);
205   return success;
206 }
207
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))
210
211 /* Scan @str for "junk" and return a cleaned-up string if any junk
212  * is found. Else return %NULL.
213  */
214 static gchar *
215 remove_junk (const gchar *str,
216              gint         len)
217 {
218   GString *cleaned = NULL;
219   const gchar *p;
220   gunichar ch;
221
222   for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
223     {
224       ch = g_utf8_get_char (p);
225       if (idna_is_junk (ch))
226         {
227           if (!cleaned)
228             {
229               cleaned = g_string_new (NULL);
230               g_string_append_len (cleaned, str, p - str);
231             }
232         }
233       else if (cleaned)
234         g_string_append_unichar (cleaned, ch);
235     }
236
237   if (cleaned)
238     return g_string_free (cleaned, FALSE);
239   else
240     return NULL;
241 }
242
243 static inline gboolean
244 contains_uppercase_letters (const gchar *str,
245                             gint         len)
246 {
247   const gchar *p;
248
249   for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
250     {
251       if (g_unichar_isupper (g_utf8_get_char (p)))
252         return TRUE;
253     }
254   return FALSE;
255 }
256
257 static inline gboolean
258 contains_non_ascii (const gchar *str,
259                     gint         len)
260 {
261   const gchar *p;
262
263   for (p = str; len == -1 ? *p : p < str + len; p++)
264     {
265       if ((guchar)*p > 0x80)
266         return TRUE;
267     }
268   return FALSE;
269 }
270
271 /* RFC 3454, Appendix C. ish. */
272 static inline gboolean
273 idna_is_prohibited (gunichar ch)
274 {
275   switch (g_unichar_type (ch))
276     {
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;
286
287     case G_UNICODE_OTHER_SYMBOL:
288       if (ch == 0xFFFC || ch == 0xFFFD ||
289           (ch >= 0x2FF0 && ch <= 0x2FFB))
290         return TRUE;
291       return FALSE;
292
293     case G_UNICODE_NON_SPACING_MARK:
294       if (ch == 0x0340 || ch == 0x0341)
295         return TRUE;
296       return FALSE;
297
298     default:
299       return FALSE;
300     }
301 }
302
303 /* RFC 3491 IDN cleanup algorithm. */
304 static gchar *
305 nameprep (const gchar *hostname,
306           gint         len,
307           gboolean    *is_unicode)
308 {
309   gchar *name, *tmp = NULL, *p;
310
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().
316    */
317
318   /* Remove presentation-only characters */
319   name = remove_junk (hostname, len);
320   if (name)
321     {
322       tmp = name;
323       len = -1;
324     }
325   else
326     name = (gchar *)hostname;
327
328   /* Convert to lowercase */
329   if (contains_uppercase_letters (name, len))
330     {
331       name = g_utf8_strdown (name, len);
332       g_free (tmp);
333       tmp = name;
334       len = -1;
335     }
336
337   /* If there are no UTF8 characters, we're done. */
338   if (!contains_non_ascii (name, len))
339     {
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;
345     }
346
347   *is_unicode = TRUE;
348
349   /* Normalize */
350   name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC);
351   g_free (tmp);
352   tmp = name;
353
354   if (!name)
355     return NULL;
356
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.)
363    */
364   if (contains_uppercase_letters (name, -1))
365     {
366       name = g_utf8_strdown (name, -1);
367       g_free (tmp);
368       tmp = name;
369     }
370
371   /* Check for prohibited characters */
372   for (p = name; *p; p = g_utf8_next_char (p))
373     {
374       if (idna_is_prohibited (g_utf8_get_char (p)))
375         {
376           name = NULL;
377           g_free (tmp);
378           goto done;
379         }
380     }
381
382   /* FIXME: We're supposed to verify certain constraints on bidi
383    * characters, but glib does not appear to have that information.
384    */
385
386  done:
387   return name;
388 }
389
390 /* RFC 3490, section 3.1 says '.', 0x3002, 0xFF0E, and 0xFF61 count as
391  * label-separating dots. @str must be '\0'-terminated.
392  */
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) )
398
399 static const gchar *
400 idna_end_of_label (const gchar *str)
401 {
402   for (; *str; str = g_utf8_next_char (str))
403     {
404       if (idna_is_dot (str))
405         return str;
406     }
407   return str;
408 }
409
410 /**
411  * g_hostname_to_ascii:
412  * @hostname: a valid UTF-8 or ASCII hostname
413  *
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.
417  *
418  * Return value: an ASCII hostname, which must be freed, or %NULL if
419  * @hostname is in some way invalid.
420  *
421  * Since: 2.22
422  **/
423 gchar *
424 g_hostname_to_ascii (const gchar *hostname)
425 {
426   gchar *name, *label, *p;
427   GString *out;
428   gssize llen, oldlen;
429   gboolean unicode;
430
431   label = name = nameprep (hostname, -1, &unicode);
432   if (!name || !unicode)
433     return name;
434
435   out = g_string_new (NULL);
436
437   do
438     {
439       unicode = FALSE;
440       for (p = label; *p && !idna_is_dot (p); p++)
441         {
442           if ((guchar)*p > 0x80)
443             unicode = TRUE;
444         }
445
446       oldlen = out->len;
447       llen = p - label;
448       if (unicode)
449         {
450           if (!strncmp (label, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
451             goto fail;
452
453           g_string_append (out, IDNA_ACE_PREFIX);
454           if (!punycode_encode (label, llen, out))
455             goto fail;
456         }
457       else
458         g_string_append_len (out, label, llen);
459
460       if (out->len - oldlen > 63)
461         goto fail;
462
463       label += llen;
464       if (*label)
465         label = g_utf8_next_char (label);
466       if (*label)
467         g_string_append_c (out, '.');
468     }
469   while (*label);
470
471   g_free (name);
472   return g_string_free (out, FALSE);
473
474  fail:
475   g_free (name);
476   g_string_free (out, TRUE);
477   return NULL;
478 }
479
480 /**
481  * g_hostname_is_non_ascii:
482  * @hostname: a hostname
483  *
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.
487  *
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.
491  *
492  * Return value: %TRUE if @hostname contains any non-ASCII characters
493  *
494  * Since: 2.22
495  **/
496 gboolean
497 g_hostname_is_non_ascii (const gchar *hostname)
498 {
499   return contains_non_ascii (hostname, -1);
500 }
501
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.
504  */
505 static gboolean
506 punycode_decode (const gchar *input,
507                  gsize        input_length,
508                  GString     *output)
509 {
510   GArray *output_chars;
511   gunichar n;
512   guint i, bias;
513   guint oldi, w, k, digit, t;
514   const gchar *split;
515
516   n = PUNYCODE_INITIAL_N;
517   i = 0;
518   bias = PUNYCODE_INITIAL_BIAS;
519
520   split = input + input_length - 1;
521   while (split > input && *split != '-')
522     split--;
523   if (split > input)
524     {
525       output_chars = g_array_sized_new (FALSE, FALSE, sizeof (gunichar),
526                                         split - input);
527       input_length -= (split - input) + 1;
528       while (input < split)
529         {
530           gunichar ch = (gunichar)*input++;
531           if (!PUNYCODE_IS_BASIC (ch))
532             goto fail;
533           g_array_append_val (output_chars, ch);
534         }
535       input++;
536     }
537   else
538     output_chars = g_array_new (FALSE, FALSE, sizeof (gunichar));
539
540   while (input_length)
541     {
542       oldi = i;
543       w = 1;
544       for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE)
545         {
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);
565         }
566
567       bias = adapt (i - oldi, output_chars->len + 1, oldi == 0);
568
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);
573
574       g_array_insert_val (output_chars, i++, n);
575     }
576
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;
581
582  fail:
583   g_array_free (output_chars, TRUE);
584   return FALSE;
585 }
586
587 /**
588  * g_hostname_to_unicode:
589  * @hostname: a valid UTF-8 or ASCII hostname
590  *
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.
595  *
596  * Of course if @hostname is not an internationalized hostname, then
597  * the canonical presentation form will be entirely ASCII.
598  *
599  * Return value: a UTF-8 hostname, which must be freed, or %NULL if
600  * @hostname is in some way invalid.
601  *
602  * Since: 2.22
603  **/
604 gchar *
605 g_hostname_to_unicode (const gchar *hostname)
606 {
607   GString *out;
608   gssize llen;
609
610   out = g_string_new (NULL);
611
612   do
613     {
614       llen = idna_end_of_label (hostname) - hostname;
615       if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN))
616         {
617           hostname += IDNA_ACE_PREFIX_LEN;
618           llen -= IDNA_ACE_PREFIX_LEN;
619           if (!punycode_decode (hostname, llen, out))
620             {
621               g_string_free (out, TRUE);
622               return NULL;
623             }
624         }
625       else
626         {
627           gboolean unicode;
628           gchar *canonicalized = nameprep (hostname, llen, &unicode);
629
630           if (!canonicalized)
631             {
632               g_string_free (out, TRUE);
633               return NULL;
634             }
635           g_string_append (out, canonicalized);
636           g_free (canonicalized);
637         }
638
639       hostname += llen;
640       if (*hostname)
641         hostname = g_utf8_next_char (hostname);
642       if (*hostname)
643         g_string_append_c (out, '.');
644     }
645   while (*hostname);
646
647   return g_string_free (out, FALSE);
648 }
649
650 /**
651  * g_hostname_is_ascii_encoded:
652  * @hostname: a hostname
653  *
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.
658  *
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.
662  *
663  * Return value: %TRUE if @hostname contains any ASCII-encoded
664  * segments.
665  *
666  * Since: 2.22
667  **/
668 gboolean
669 g_hostname_is_ascii_encoded (const gchar *hostname)
670 {
671   while (1)
672     {
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;
680     }
681 }
682
683 /**
684  * g_hostname_is_ip_address:
685  * @hostname: a hostname (or IP address in string form)
686  *
687  * Tests if @hostname is the string form of an IPv4 or IPv6 address.
688  * (Eg, "192.168.0.1".)
689  *
690  * Return value: %TRUE if @hostname is an IP address
691  *
692  * Since: 2.22
693  **/
694 gboolean
695 g_hostname_is_ip_address (const gchar *hostname)
696 {
697   gchar *p, *end;
698   gint nsegments, octet;
699
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.
703    */
704
705   p = (char *)hostname;
706
707   if (strchr (p, ':'))
708     {
709       gboolean skipped;
710
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 "::".
717        */
718
719       nsegments = 0;
720       skipped = FALSE;
721       while (*p && nsegments < 8)
722         {
723           /* Each segment after the first must be preceded by a ':'.
724            * (We also handle half of the "string starts with ::" case
725            * here.)
726            */
727           if (p != (char *)hostname || (p[0] == ':' && p[1] == ':'))
728             {
729               if (*p != ':')
730                 return FALSE;
731               p++;
732             }
733
734           /* If there's another ':', it means we're skipping some segments */
735           if (*p == ':' && !skipped)
736             {
737               skipped = TRUE;
738               nsegments++;
739
740               /* Handle the "string ends with ::" case */
741               if (!p[1])
742                 p++;
743
744               continue;
745             }
746
747           /* Read the segment, make sure it's valid. */
748           for (end = p; g_ascii_isxdigit (*end); end++)
749             ;
750           if (end == p || end > p + 4)
751             return FALSE;
752
753           if (*end == '.')
754             {
755               if ((nsegments == 6 && !skipped) || (nsegments <= 6 && skipped))
756                 goto parse_ipv4;
757               else
758                 return FALSE;
759             }
760
761           nsegments++;
762           p = end;
763         }
764
765       return !*p && (nsegments == 8 || skipped);
766     }
767
768  parse_ipv4:
769
770   /* Parse IPv4: N.N.N.N, where each N <= 255 and doesn't have leading 0s. */
771   for (nsegments = 0; nsegments < 4; nsegments++)
772     {
773       if (nsegments != 0)
774         {
775           if (*p != '.')
776             return FALSE;
777           p++;
778         }
779
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.
783        */
784       octet = 0;
785       if (*p == '0')
786         end = p + 1;
787       else
788         {
789           for (end = p; g_ascii_isdigit (*end); end++)
790             octet = 10 * octet + (*end - '0');
791         }
792       if (end == p || end > p + 3 || octet > 255)
793         return FALSE;
794
795       p = end;
796     }
797
798   /* If there's nothing left to parse, then it's ok. */
799   return !*p;
800 }