summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/libntp/buftvtots.c
blob: 96338bc1be593c38e95426adfc771a260b602f39 (plain)
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
/*
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ntp_fp.h"
#include "ntp_unixtime.h"

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

#ifdef WORDS_BIGENDIAN
	bp = (const 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 = (const 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
	ts->l_ui = sec + (u_long)JAN_1970;
	if (usec > 999999)
	    return 0;
	TVUTOTSF(usec, ts->l_uf);
	return 1;
}
OpenPOWER on IntegriCloud