aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/lib/hextolfp.c
blob: 6473c7c3dbca69ac044cf49af348b748bb994869 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* hextolfp.c,v 3.1 1993/07/06 01:08:22 jbj Exp
 * hextolfp - convert an ascii hex string to an l_fp number
 */
#include <stdio.h>
#include <ctype.h>

#include "ntp_fp.h"
#include "ntp_string.h"
#include "ntp_stdlib.h"

int
hextolfp(str, lfp)
	const char *str;
	l_fp *lfp;
{
	register const char *cp;
	register const char *cpstart;
	register U_LONG dec_i;
	register U_LONG dec_f;
	char *ind = NULL;
	static char *digits = "0123456789abcdefABCDEF";

	dec_i = dec_f = 0;
	cp = str;

	/*
	 * We understand numbers of the form:
	 *
	 * [spaces]8_hex_digits[.]8_hex_digits[spaces|\n|\0]
	 */
	while (isspace(*cp))
		cp++;
	
	cpstart = cp;
	while (*cp != '\0' && (cp - cpstart) < 8 &&
	    (ind = strchr(digits, *cp)) != NULL) {
		dec_i = dec_i << 4;	/* multiply by 16 */
		dec_i += ((ind - digits) > 15) ? (ind - digits) - 6
		    : (ind - digits);
		cp++;
	}

	if ((cp - cpstart) < 8 || ind == NULL)
		return 0;
	if (*cp == '.')
		cp++;

	cpstart = cp;
	while (*cp != '\0' && (cp - cpstart) < 8 &&
	    (ind = strchr(digits, *cp)) != NULL) {
		dec_f = dec_f << 4;	/* multiply by 16 */
		dec_f += ((ind - digits) > 15) ? (ind - digits) - 6
		    : (ind - digits);
		cp++;
	}

	if ((cp - cpstart) < 8 || ind == NULL)
		return 0;
	
	if (*cp != '\0' && !isspace(*cp))
		return 0;

	lfp->l_ui = dec_i;
	lfp->l_uf = dec_f;
	return 1;
}