]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/net/inet_pton.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / libkern / net / inet_pton.c
index 417326d3d1d6b3a71696f208a4ec8f0b556c940a..19543ced527e2b5e77c32aa907c205decdcd4d8e 100644 (file)
@@ -31,8 +31,8 @@ static const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 mar
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
-static int     inet_pton4(const char *src, u_char *dst);
-static int     inet_pton6(const char *src, u_char *dst);
+static int      inet_pton4(const char *src, u_char *dst);
+static int      inet_pton6(const char *src, u_char *dst);
 
 /* int
  * inet_pton(af, src, dst)
 
 /* int
  * inet_pton(af, src, dst)
@@ -50,11 +50,11 @@ inet_pton(int af, const char *src, void *dst)
 {
        switch (af) {
        case AF_INET:
 {
        switch (af) {
        case AF_INET:
-               return (inet_pton4(src, dst));
+               return inet_pton4(src, dst);
        case AF_INET6:
        case AF_INET6:
-               return (inet_pton6(src, dst));
+               return inet_pton6(src, dst);
        default:
        default:
-               return (-1);
+               return -1;
        }
        /* NOTREACHED */
 }
        }
        /* NOTREACHED */
 }
@@ -74,7 +74,7 @@ inet_pton4(const char *src, u_char *dst)
 {
        static const char digits[] = "0123456789";
        int saw_digit, octets, ch;
 {
        static const char digits[] = "0123456789";
        int saw_digit, octets, ch;
-#define NS_INADDRSZ    4
+#define NS_INADDRSZ     4
        u_char tmp[NS_INADDRSZ], *tp;
 
        saw_digit = 0;
        u_char tmp[NS_INADDRSZ], *tp;
 
        saw_digit = 0;
@@ -86,28 +86,34 @@ inet_pton4(const char *src, u_char *dst)
                if ((pch = strchr(digits, ch)) != NULL) {
                        u_int new = *tp * 10 + (pch - digits);
 
                if ((pch = strchr(digits, ch)) != NULL) {
                        u_int new = *tp * 10 + (pch - digits);
 
-                       if (saw_digit && *tp == 0)
-                               return (0);
-                       if (new > 255)
-                               return (0);
+                       if (saw_digit && *tp == 0) {
+                               return 0;
+                       }
+                       if (new > 255) {
+                               return 0;
+                       }
                        *tp = new;
                        if (!saw_digit) {
                        *tp = new;
                        if (!saw_digit) {
-                               if (++octets > 4)
-                                       return (0);
+                               if (++octets > 4) {
+                                       return 0;
+                               }
                                saw_digit = 1;
                        }
                } else if (ch == '.' && saw_digit) {
                                saw_digit = 1;
                        }
                } else if (ch == '.' && saw_digit) {
-                       if (octets == 4)
-                               return (0);
+                       if (octets == 4) {
+                               return 0;
+                       }
                        *++tp = 0;
                        saw_digit = 0;
                        *++tp = 0;
                        saw_digit = 0;
-               } else
-                       return (0);
+               } else {
+                       return 0;
+               }
+       }
+       if (octets < 4) {
+               return 0;
        }
        }
-       if (octets < 4)
-               return (0);
        memcpy(dst, tmp, NS_INADDRSZ);
        memcpy(dst, tmp, NS_INADDRSZ);
-       return (1);
+       return 1;
 }
 
 /* int
 }
 
 /* int
@@ -127,9 +133,9 @@ static int
 inet_pton6(const char *src, u_char *dst)
 {
        static const char xdigits_l[] = "0123456789abcdef",
 inet_pton6(const char *src, u_char *dst)
 {
        static const char xdigits_l[] = "0123456789abcdef",
-                         xdigits_u[] = "0123456789ABCDEF";
-#define NS_IN6ADDRSZ   16
-#define NS_INT16SZ     2
+           xdigits_u[] = "0123456789ABCDEF";
+#define NS_IN6ADDRSZ    16
+#define NS_INT16SZ      2
        u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
        const char *xdigits, *curtok;
        int ch, seen_xdigits;
        u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
        const char *xdigits, *curtok;
        int ch, seen_xdigits;
@@ -139,36 +145,42 @@ inet_pton6(const char *src, u_char *dst)
        endp = tp + NS_IN6ADDRSZ;
        colonp = NULL;
        /* Leading :: requires some special handling. */
        endp = tp + NS_IN6ADDRSZ;
        colonp = NULL;
        /* Leading :: requires some special handling. */
-       if (*src == ':')
-               if (*++src != ':')
-                       return (0);
+       if (*src == ':') {
+               if (*++src != ':') {
+                       return 0;
+               }
+       }
        curtok = src;
        seen_xdigits = 0;
        val = 0;
        while ((ch = *src++) != '\0') {
                const char *pch;
 
        curtok = src;
        seen_xdigits = 0;
        val = 0;
        while ((ch = *src++) != '\0') {
                const char *pch;
 
-               if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
+               if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) {
                        pch = strchr((xdigits = xdigits_u), ch);
                        pch = strchr((xdigits = xdigits_u), ch);
+               }
                if (pch != NULL) {
                        val <<= 4;
                        val |= (pch - xdigits);
                if (pch != NULL) {
                        val <<= 4;
                        val |= (pch - xdigits);
-                       if (++seen_xdigits > 4)
-                               return (0);
+                       if (++seen_xdigits > 4) {
+                               return 0;
+                       }
                        continue;
                }
                if (ch == ':') {
                        curtok = src;
                        if (!seen_xdigits) {
                        continue;
                }
                if (ch == ':') {
                        curtok = src;
                        if (!seen_xdigits) {
-                               if (colonp)
-                                       return (0);
+                               if (colonp) {
+                                       return 0;
+                               }
                                colonp = tp;
                                continue;
                        } else if (*src == '\0') {
                                colonp = tp;
                                continue;
                        } else if (*src == '\0') {
-                               return (0);
+                               return 0;
+                       }
+                       if (tp + NS_INT16SZ > endp) {
+                               return 0;
                        }
                        }
-                       if (tp + NS_INT16SZ > endp)
-                               return (0);
                        *tp++ = (u_char) (val >> 8) & 0xff;
                        *tp++ = (u_char) val & 0xff;
                        seen_xdigits = 0;
                        *tp++ = (u_char) (val >> 8) & 0xff;
                        *tp++ = (u_char) val & 0xff;
                        seen_xdigits = 0;
@@ -179,13 +191,14 @@ inet_pton6(const char *src, u_char *dst)
                    inet_pton4(curtok, tp) > 0) {
                        tp += NS_INADDRSZ;
                        seen_xdigits = 0;
                    inet_pton4(curtok, tp) > 0) {
                        tp += NS_INADDRSZ;
                        seen_xdigits = 0;
-                       break;  /*%< '\\0' was seen by inet_pton4(). */
+                       break;  /*%< '\\0' was seen by inet_pton4(). */
                }
                }
-               return (0);
+               return 0;
        }
        if (seen_xdigits) {
        }
        if (seen_xdigits) {
-               if (tp + NS_INT16SZ > endp)
-                       return (0);
+               if (tp + NS_INT16SZ > endp) {
+                       return 0;
+               }
                *tp++ = (u_char) (val >> 8) & 0xff;
                *tp++ = (u_char) val & 0xff;
        }
                *tp++ = (u_char) (val >> 8) & 0xff;
                *tp++ = (u_char) val & 0xff;
        }
@@ -197,16 +210,18 @@ inet_pton6(const char *src, u_char *dst)
                const int n = tp - colonp;
                int i;
 
                const int n = tp - colonp;
                int i;
 
-               if (tp == endp)
-                       return (0);
+               if (tp == endp) {
+                       return 0;
+               }
                for (i = 1; i <= n; i++) {
                for (i = 1; i <= n; i++) {
-                       endp[- i] = colonp[n - i];
+                       endp[-i] = colonp[n - i];
                        colonp[n - i] = 0;
                }
                tp = endp;
        }
                        colonp[n - i] = 0;
                }
                tp = endp;
        }
-       if (tp != endp)
-               return (0);
+       if (tp != endp) {
+               return 0;
+       }
        memcpy(dst, tmp, NS_IN6ADDRSZ);
        memcpy(dst, tmp, NS_IN6ADDRSZ);
-       return (1);
+       return 1;
 }
 }