aboutsummaryrefslogtreecommitdiff
path: root/libntp/hextoint.c
blob: 980a43f9d8998b66d18f4fe77177d842e72d5987 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * hextoint - convert an ascii string in hex to an unsigned
 *	      long, with error checking
 */
#include <config.h>
#include <ctype.h>

#include "ntp_stdlib.h"

int
hextoint(
	const char *str,
	u_long *pu
	)
{
	register u_long u;
	register const char *cp;

	cp = str;

	if (*cp == '\0')
		return 0;

	u = 0;
	while (*cp != '\0') {
		if (!isxdigit((unsigned char)*cp))
			return 0;
		if (u & 0xF0000000)
			return 0;	/* overflow */
		u <<= 4;
		if ('0' <= *cp && *cp <= '9')
			u += *cp++ - '0';
		else if ('a' <= *cp && *cp <= 'f')
			u += *cp++ - 'a' + 10;
		else if ('A' <= *cp && *cp <= 'F')
			u += *cp++ - 'A' + 10;
		else
			return 0;
	}
	*pu = u;
	return 1;
}