aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/lib/buftvtots.c
blob: d9b484db2df0529d5cedec30467f429933b05cd3 (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
/* buftvtots.c,v 3.1 1993/07/06 01:07:59 jbj Exp
 * buftvtots - pull a Unix-format (struct timeval) time stamp out of
 *	       an octet stream and convert it to a l_fp time stamp.
 *	       This is useful when using the clock line discipline.
 */
#include "ntp_fp.h"
#include "ntp_unixtime.h"

int
buftvtots(bufp, ts)
	const char *bufp;
	l_fp *ts;
{
	register const u_char *bp;
	register U_LONG sec;
	register U_LONG usec;

#ifdef XNTP_BIG_ENDIAN
	bp = (u_char *)bufp;

	sec = (U_LONG)*bp++ & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp++ & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp++ & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp++ & 0xff;

	usec = (U_LONG)*bp++ & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp++ & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp++ & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp & 0xff;
#else
	bp = (u_char *)bufp + 7;

	usec = (U_LONG)*bp-- & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp-- & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp-- & 0xff;
	usec <<= 8;
	usec += (U_LONG)*bp-- & 0xff;

	sec = (U_LONG)*bp-- & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp-- & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp-- & 0xff;
	sec <<= 8;
	sec += (U_LONG)*bp & 0xff;
#endif
	if (usec > 999999)
		return 0;

	ts->l_ui = sec + (U_LONG)JAN_1970;
	TVUTOTSF(usec, ts->l_uf);
	return 1;
}