1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-08 17:42:12 +03:00

inet_pton: Reformat in GNU style

Generated machine code is identical on x86-64.
This commit is contained in:
Florian Weimer
2017-05-11 14:48:51 +02:00
parent 8ec69bb7ec
commit d53b865288
2 changed files with 158 additions and 149 deletions

View File

@@ -1,3 +1,8 @@
2017-05-11 Florian Weimer <fweimer@redhat.com>
* resolv/inet_pton.c: Reformat in GNU style. Remove
internal_function on static functions.
2017-05-11 Florian Weimer <fweimer@redhat.com> 2017-05-11 Florian Weimer <fweimer@redhat.com>
* support/support_format_addrinfo.c (format_ai_flags_1): Renamed * support/support_format_addrinfo.c (format_ai_flags_1): Renamed

View File

@@ -25,18 +25,12 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
/* static int inet_pton4 (const char *src, unsigned char *dst);
* WARNING: Don't even consider trying to compile this on a system where static int inet_pton6 (const char *src, unsigned char *dst);
* 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) internal_function; /* Convert from presentation format (which usually means ASCII printable)
static int inet_pton6 (const char *src, u_char *dst) internal_function;
/* int
* inet_pton(af, src, dst)
* convert from presentation format (which usually means ASCII printable)
* to network format (which is usually some kind of binary format). * to network format (which is usually some kind of binary format).
*
* return: * return:
* 1 if the address was valid for the specified address family * 1 if the address was valid for the specified address family
* 0 if the address wasn't valid (`dst' is untouched in this case) * 0 if the address wasn't valid (`dst' is untouched in this case)
@@ -47,25 +41,23 @@ static int inet_pton6 (const char *src, u_char *dst) internal_function;
int int
__inet_pton (int af, const char *src, void *dst) __inet_pton (int af, const char *src, void *dst)
{ {
switch (af) { switch (af)
{
case AF_INET: 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:
__set_errno (EAFNOSUPPORT); __set_errno (EAFNOSUPPORT);
return (-1); return -1;
} }
/* NOTREACHED */
} }
libc_hidden_def (__inet_pton) libc_hidden_def (__inet_pton)
weak_alias (__inet_pton, inet_pton) weak_alias (__inet_pton, inet_pton)
libc_hidden_weak (inet_pton) libc_hidden_weak (inet_pton)
/* int /* Like inet_atonbut without all the hexadecimal, octal and shorthand.
* inet_pton4(src, dst) *
* like inet_aton() but without all the hexadecimal, octal (with the
* exception of 0) and shorthand.
* return: * return:
* 1 if `src' is a valid dotted quad, else 0. * 1 if `src' is a valid dotted quad, else 0.
* notice: * notice:
@@ -74,47 +66,50 @@ libc_hidden_weak (inet_pton)
* Paul Vixie, 1996. * Paul Vixie, 1996.
*/ */
static int static int
internal_function inet_pton4 (const char *src, unsigned char *dst)
inet_pton4 (const char *src, u_char *dst)
{ {
int saw_digit, octets, ch; int saw_digit, octets, ch;
u_char tmp[NS_INADDRSZ], *tp; unsigned char tmp[NS_INADDRSZ], *tp;
saw_digit = 0; saw_digit = 0;
octets = 0; octets = 0;
*(tp = tmp) = 0; *(tp = tmp) = 0;
while ((ch = *src++) != '\0') { while ((ch = *src++) != '\0')
{
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9')
u_int new = *tp * 10 + (ch - '0'); {
unsigned int new = *tp * 10 + (ch - '0');
if (saw_digit && *tp == 0) if (saw_digit && *tp == 0)
return (0); return 0;
if (new > 255) if (new > 255)
return (0); return 0;
*tp = new; *tp = new;
if (! saw_digit) { if (! saw_digit)
{
if (++octets > 4) if (++octets > 4)
return (0); return 0;
saw_digit = 1; saw_digit = 1;
} }
} else if (ch == '.' && saw_digit) { }
else if (ch == '.' && saw_digit)
{
if (octets == 4) if (octets == 4)
return (0); return 0;
*++tp = 0; *++tp = 0;
saw_digit = 0; saw_digit = 0;
} else }
return (0); else
return 0;
} }
if (octets < 4) if (octets < 4)
return (0); return 0;
memcpy (dst, tmp, NS_INADDRSZ); memcpy (dst, tmp, NS_INADDRSZ);
return (1); return 1;
} }
/* int /* Cconvert presentation level address to network order binary form.
* inet_pton6(src, dst) *
* convert presentation level address to network order binary form.
* return: * return:
* 1 if `src' is a valid [RFC1884 2.2] address, else 0. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
* notice: * notice:
@@ -126,14 +121,13 @@ inet_pton4 (const char *src, u_char *dst)
* Paul Vixie, 1996. * Paul Vixie, 1996.
*/ */
static int static int
internal_function inet_pton6 (const char *src, unsigned char *dst)
inet_pton6 (const char *src, u_char *dst)
{ {
static const char xdigits[] = "0123456789abcdef"; static const char xdigits[] = "0123456789abcdef";
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *curtok; const char *curtok;
int ch, saw_xdigit; int ch, saw_xdigit;
u_int val; unsigned int val;
tp = memset (tmp, '\0', NS_IN6ADDRSZ); tp = memset (tmp, '\0', NS_IN6ADDRSZ);
endp = tp + NS_IN6ADDRSZ; endp = tp + NS_IN6ADDRSZ;
@@ -141,72 +135,82 @@ inet_pton6 (const char *src, u_char *dst)
/* Leading :: requires some special handling. */ /* Leading :: requires some special handling. */
if (*src == ':') if (*src == ':')
if (*++src != ':') if (*++src != ':')
return (0); return 0;
curtok = src; curtok = src;
saw_xdigit = 0; saw_xdigit = 0;
val = 0; val = 0;
while ((ch = tolower (*src++)) != '\0') { while ((ch = tolower (*src++)) != '\0')
{
const char *pch; const char *pch;
pch = strchr (xdigits, ch); pch = strchr (xdigits, ch);
if (pch != NULL) { if (pch != NULL)
{
val <<= 4; val <<= 4;
val |= (pch - xdigits); val |= (pch - xdigits);
if (val > 0xffff) if (val > 0xffff)
return (0); return 0;
saw_xdigit = 1; saw_xdigit = 1;
continue; continue;
} }
if (ch == ':') { if (ch == ':')
{
curtok = src; curtok = src;
if (!saw_xdigit) { if (!saw_xdigit)
{
if (colonp) if (colonp)
return (0); return 0;
colonp = tp; colonp = tp;
continue; continue;
} else if (*src == '\0') { }
return (0); else if (*src == '\0')
{
return 0;
} }
if (tp + NS_INT16SZ > endp) if (tp + NS_INT16SZ > endp)
return (0); return 0;
*tp++ = (u_char) (val >> 8) & 0xff; *tp++ = (unsigned char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff; *tp++ = (unsigned char) val & 0xff;
saw_xdigit = 0; saw_xdigit = 0;
val = 0; val = 0;
continue; continue;
} }
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
inet_pton4(curtok, tp) > 0) { inet_pton4 (curtok, tp) > 0)
{
tp += NS_INADDRSZ; tp += NS_INADDRSZ;
saw_xdigit = 0; saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */ break; /* '\0' was seen by inet_pton4. */
} }
return (0); return 0;
} }
if (saw_xdigit) { if (saw_xdigit)
{
if (tp + NS_INT16SZ > endp) if (tp + NS_INT16SZ > endp)
return (0); return 0;
*tp++ = (u_char) (val >> 8) & 0xff; *tp++ = (unsigned char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff; *tp++ = (unsigned char) val & 0xff;
} }
if (colonp != NULL) { if (colonp != NULL)
{
/* /*
* Since some memmove()'s erroneously fail to handle * Since some memmove's erroneously fail to handle
* overlapping regions, we'll do the shift by hand. * overlapping regions, we'll do the shift by hand.
*/ */
const int n = tp - colonp; const int n = tp - colonp;
int i; int i;
if (tp == endp) if (tp == endp)
return (0); 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; colonp[n - i] = 0;
} }
tp = endp; tp = endp;
} }
if (tp != endp) if (tp != endp)
return (0); return 0;
memcpy (dst, tmp, NS_IN6ADDRSZ); memcpy (dst, tmp, NS_IN6ADDRSZ);
return (1); return 1;
} }