1/*
2 * hextoint - convert an ascii string in hex to an unsigned
3 *	      long, with error checking
4 */
5#include <ctype.h>
6
7#include "ntp_stdlib.h"
8
9int
10hextoint(
11	const char *str,
12	u_long *pu
13	)
14{
15	register u_long u;
16	register const char *cp;
17
18	cp = str;
19
20	if (*cp == '\0')
21		return 0;
22
23	u = 0;
24	while (*cp != '\0') {
25		if (!isxdigit(*cp))
26			return 0;
27		if (u & 0xF0000000)
28			return 0;	/* overflow */
29		u <<= 4;
30		if ('0' <= *cp && *cp <= '9')
31			u += *cp++ - '0';
32		else if ('a' <= *cp && *cp <= 'f')
33			u += *cp++ - 'a' + 10;
34		else if ('A' <= *cp && *cp <= 'F')
35			u += *cp++ - 'A' + 10;
36		else
37			return 0;
38	}
39	*pu = u;
40	return 1;
41}
42