11556Srgrimes/*
21556Srgrimes * hextoint - convert an ascii string in hex to an unsigned
31556Srgrimes *	      long, with error checking
41556Srgrimes */
51556Srgrimes#include <config.h>
61556Srgrimes#include <ctype.h>
71556Srgrimes
81556Srgrimes#include "ntp_stdlib.h"
91556Srgrimes
101556Srgrimesint
111556Srgrimeshextoint(
121556Srgrimes	const char *str,
131556Srgrimes	u_long *pu
141556Srgrimes	)
151556Srgrimes{
161556Srgrimes	register u_long u;
171556Srgrimes	register const char *cp;
181556Srgrimes
191556Srgrimes	cp = str;
201556Srgrimes
211556Srgrimes	if (*cp == '\0')
221556Srgrimes		return 0;
231556Srgrimes
241556Srgrimes	u = 0;
251556Srgrimes	while (*cp != '\0') {
261556Srgrimes		if (!isxdigit((unsigned char)*cp))
271556Srgrimes			return 0;
281556Srgrimes		if (u & 0xF0000000)
291556Srgrimes			return 0;	/* overflow */
301556Srgrimes		u <<= 4;
311556Srgrimes		if ('0' <= *cp && *cp <= '9')
321556Srgrimes			u += *cp++ - '0';
331556Srgrimes		else if ('a' <= *cp && *cp <= 'f')
341556Srgrimes			u += *cp++ - 'a' + 10;
351556Srgrimes		else if ('A' <= *cp && *cp <= 'F')
361556Srgrimes			u += *cp++ - 'A' + 10;
371556Srgrimes		else
3850471Speter			return 0;
391556Srgrimes	}
401556Srgrimes	*pu = u;
411556Srgrimes	return 1;
421556Srgrimes}
431556Srgrimes