154359Sroberto/*
254359Sroberto * hextoint - convert an ascii string in hex to an unsigned
354359Sroberto *	      long, with error checking
454359Sroberto */
5290000Sglebius#include <config.h>
654359Sroberto#include <ctype.h>
754359Sroberto
854359Sroberto#include "ntp_stdlib.h"
954359Sroberto
1054359Srobertoint
1154359Srobertohextoint(
1254359Sroberto	const char *str,
13290000Sglebius	u_long *pu
1454359Sroberto	)
1554359Sroberto{
1654359Sroberto	register u_long u;
1754359Sroberto	register const char *cp;
1854359Sroberto
1954359Sroberto	cp = str;
2054359Sroberto
2154359Sroberto	if (*cp == '\0')
22290000Sglebius		return 0;
2354359Sroberto
2454359Sroberto	u = 0;
2554359Sroberto	while (*cp != '\0') {
26290000Sglebius		if (!isxdigit((unsigned char)*cp))
27290000Sglebius			return 0;
28290000Sglebius		if (u & 0xF0000000)
29290000Sglebius			return 0;	/* overflow */
3054359Sroberto		u <<= 4;
31290000Sglebius		if ('0' <= *cp && *cp <= '9')
32290000Sglebius			u += *cp++ - '0';
33290000Sglebius		else if ('a' <= *cp && *cp <= 'f')
34290000Sglebius			u += *cp++ - 'a' + 10;
35290000Sglebius		else if ('A' <= *cp && *cp <= 'F')
36290000Sglebius			u += *cp++ - 'A' + 10;
3754359Sroberto		else
38290000Sglebius			return 0;
3954359Sroberto	}
40290000Sglebius	*pu = u;
4154359Sroberto	return 1;
4254359Sroberto}
43