X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/39236c6e673c41db228275375ab7fdb0f837b292..94ff46dc2849db4d43eaaf144872decc522aafb4:/libkern/net/inet_pton.c diff --git a/libkern/net/inet_pton.c b/libkern/net/inet_pton.c index 417326d3d..19543ced5 100644 --- a/libkern/net/inet_pton.c +++ b/libkern/net/inet_pton.c @@ -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. */ -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) @@ -50,11 +50,11 @@ inet_pton(int af, const char *src, void *dst) { switch (af) { case AF_INET: - return (inet_pton4(src, dst)); + return inet_pton4(src, dst); case AF_INET6: - return (inet_pton6(src, dst)); + return inet_pton6(src, dst); default: - return (-1); + return -1; } /* NOTREACHED */ } @@ -74,7 +74,7 @@ inet_pton4(const char *src, u_char *dst) { 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; @@ -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 (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) { - if (++octets > 4) - return (0); + if (++octets > 4) { + return 0; + } saw_digit = 1; } } else if (ch == '.' && saw_digit) { - if (octets == 4) - return (0); + if (octets == 4) { + return 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); - return (1); + return 1; } /* int @@ -127,9 +133,9 @@ static int 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; @@ -139,36 +145,42 @@ inet_pton6(const char *src, u_char *dst) 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; - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) { pch = strchr((xdigits = xdigits_u), ch); + } 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) { - if (colonp) - return (0); + if (colonp) { + return 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; @@ -179,13 +191,14 @@ inet_pton6(const char *src, u_char *dst) 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 (tp + NS_INT16SZ > endp) - return (0); + if (tp + NS_INT16SZ > endp) { + return 0; + } *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; - if (tp == endp) - return (0); + if (tp == endp) { + return 0; + } for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; + endp[-i] = colonp[n - i]; colonp[n - i] = 0; } tp = endp; } - if (tp != endp) - return (0); + if (tp != endp) { + return 0; + } memcpy(dst, tmp, NS_IN6ADDRSZ); - return (1); + return 1; }