]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - resolv/inet_net_pton.c
iconv, localedef: avoid floating point rounding differences [BZ #24372]
[thirdparty/glibc.git] / resolv / inet_net_pton.c
index ea9b2d4b55277545f106c9aa35c81c32066f4fc8..aab9b7b58228bb2db15cb41182666ec95122d581 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996 by Internet Software Consortium.
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * SOFTWARE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static const char rcsid[] = "$Id$";
-#endif
-
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -37,8 +33,8 @@ static const char rcsid[] = "$Id$";
 # define SPRINTF(x) ((size_t)sprintf x)
 #endif
 
-static int     inet_net_pton_ipv4 __P((const char *src, u_char *dst,
-                                       size_t size));
+static int     inet_net_pton_ipv4 (const char *src, u_char *dst,
+                                   size_t size) __THROW;
 
 /*
  * static int
@@ -54,17 +50,13 @@ static int  inet_net_pton_ipv4 __P((const char *src, u_char *dst,
  *     Paul Vixie (ISC), June 1996
  */
 int
-inet_net_pton(af, src, dst, size)
-       int af;
-       const char *src;
-       void *dst;
-       size_t size;
+inet_net_pton (int af, const char *src, void *dst, size_t size)
 {
        switch (af) {
        case AF_INET:
                return (inet_net_pton_ipv4(src, dst, size));
        default:
-               errno = EAFNOSUPPORT;
+               __set_errno (EAFNOSUPPORT);
                return (-1);
        }
 }
@@ -81,61 +73,59 @@ inet_net_pton(af, src, dst, size)
  *     not an IPv4 network specification.
  * note:
  *     network byte order assumed.  this means 192.5.5.240/28 has
- *     0x11110000 in its fourth octet.
+ *     0b11110000 in its fourth octet.
  * author:
  *     Paul Vixie (ISC), June 1996
  */
 static int
-inet_net_pton_ipv4(src, dst, size)
-       const char *src;
-       u_char *dst;
-       size_t size;
+inet_net_pton_ipv4 (const char *src, u_char *dst, size_t size)
 {
-       static const char
-               xdigits[] = "0123456789abcdef",
-               digits[] = "0123456789";
+       static const char xdigits[] = "0123456789abcdef";
        int n, ch, tmp, dirty, bits;
        const u_char *odst = dst;
 
-       bits = -1;
        ch = *src++;
-
        if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
            && isascii(src[1]) && isxdigit(src[1])) {
                /* Hexadecimal: Eat nybble string. */
                if (size <= 0)
                        goto emsgsize;
-               *dst = 0, dirty = 0;
+               dirty = 0;
+               tmp = 0;        /* To calm down gcc.  */
                src++;  /* skip x or X. */
-               while ((ch = *src++) != '\0' &&
-                      isascii(ch) && isxdigit(ch)) {
-                       if (isupper(ch))
-                               ch = tolower(ch);
-                       n = strchr(xdigits, ch) - xdigits;
+               while (isxdigit((ch = *src++))) {
+                       ch = _tolower(ch);
+                       n = (const char *) __rawmemchr(xdigits, ch) - xdigits;
                        assert(n >= 0 && n <= 15);
-                       *dst |= n;
-                       if (!dirty++)
-                               *dst <<= 4;
-                       else if (size-- > 0)
-                               *++dst = 0, dirty = 0;
+                       if (dirty == 0)
+                               tmp = n;
                        else
+                               tmp = (tmp << 4) | n;
+                       if (++dirty == 2) {
+                               if (size-- <= 0)
+                                       goto emsgsize;
+                               *dst++ = (u_char) tmp;
+                               dirty = 0;
+                       }
+               }
+               if (dirty) {  /* Odd trailing nybble? */
+                       if (size-- <= 0)
                                goto emsgsize;
+                       *dst++ = (u_char) (tmp << 4);
                }
-               if (dirty)
-                       size--;
        } else if (isascii(ch) && isdigit(ch)) {
                /* Decimal: eat dotted digit string. */
                for (;;) {
                        tmp = 0;
                        do {
-                               n = strchr(digits, ch) - digits;
+                               n = ((const char *) __rawmemchr(xdigits, ch)
+                                    - xdigits);
                                assert(n >= 0 && n <= 9);
                                tmp *= 10;
                                tmp += n;
                                if (tmp > 255)
                                        goto enoent;
-                       } while ((ch = *src++) != '\0' &&
-                                isascii(ch) && isdigit(ch));
+                       } while (isascii((ch = *src++)) && isdigit(ch));
                        if (size-- <= 0)
                                goto emsgsize;
                        *dst++ = (u_char) tmp;
@@ -150,19 +140,21 @@ inet_net_pton_ipv4(src, dst, size)
        } else
                goto enoent;
 
+       bits = -1;
        if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) {
                /* CIDR width specifier.  Nothing can follow it. */
                ch = *src++;    /* Skip over the /. */
                bits = 0;
                do {
-                       n = strchr(digits, ch) - digits;
+                       n = (const char *) __rawmemchr(xdigits, ch) - xdigits;
                        assert(n >= 0 && n <= 9);
                        bits *= 10;
                        bits += n;
-               } while ((ch = *src++) != '\0' &&
-                        isascii(ch) && isdigit(ch));
+               } while (isascii((ch = *src++)) && isdigit(ch));
                if (ch != '\0')
                        goto enoent;
+               if (bits > 32)
+                       goto emsgsize;
        }
 
        /* Firey death and destruction unless we prefetched EOS. */
@@ -174,14 +166,19 @@ inet_net_pton_ipv4(src, dst, size)
                goto enoent;
        /* If no CIDR spec was given, infer width from net class. */
        if (bits == -1) {
-               if (*odst >= 224)
+               if (*odst >= 240)       /* Class E */
+                       bits = 32;
+               else if (*odst >= 224)  /* Class D */
                        bits = 4;
-               else if (*odst >= 192)
+               else if (*odst >= 192)  /* Class C */
                        bits = 24;
-               else if (*odst >= 128)
+               else if (*odst >= 128)  /* Class B */
                        bits = 16;
-               else
+               else                    /* Class A */
                        bits = 8;
+               /* If imputed mask is narrower than specified octets, widen. */
+               if (bits >= 8 && bits < ((dst - odst) * 8))
+                       bits = (dst - odst) * 8;
        }
        /* Extend network to cover the actual mask. */
        while (bits > ((dst - odst) * 8)) {
@@ -192,10 +189,10 @@ inet_net_pton_ipv4(src, dst, size)
        return (bits);
 
  enoent:
-       errno = ENOENT;
+       __set_errno (ENOENT);
        return (-1);
 
  emsgsize:
-       errno = EMSGSIZE;
+       __set_errno (EMSGSIZE);
        return (-1);
 }