hextoint.c revision 54360
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 *ival
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((int)*cp))
26		    return 0;
27		if (u >= 0x10000000)
28		    return 0;	/* overflow */
29		u <<= 4;
30		if (*cp <= '9')		/* very ascii dependent */
31		    u += *cp++ - '0';
32		else if (*cp >= 'a')
33		    u += *cp++ - 'a' + 10;
34		else
35		    u += *cp++ - 'A' + 10;
36	}
37	*ival = u;
38	return 1;
39}
40