xref: /freebsd/contrib/ntp/libntp/buftvtots.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
1 /*
2  * buftvtots - pull a Unix-format (struct timeval) time stamp out of
3  *	       an octet stream and convert it to a l_fp time stamp.
4  *	       This is useful when using the clock line discipline.
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 #include "ntp_fp.h"
11 #include "ntp_unixtime.h"
12 
13 int
14 buftvtots(
15 	const char *bufp,
16 	l_fp *ts
17 	)
18 {
19 	register const u_char *bp;
20 	register u_long sec;
21 	register u_long usec;
22 
23 #ifdef WORDS_BIGENDIAN
24 	bp = (const u_char *)bufp;
25 
26 	sec = (u_long)*bp++ & 0xff;
27 	sec <<= 8;
28 	sec += (u_long)*bp++ & 0xff;
29 	sec <<= 8;
30 	sec += (u_long)*bp++ & 0xff;
31 	sec <<= 8;
32 	sec += (u_long)*bp++ & 0xff;
33 
34 	usec = (u_long)*bp++ & 0xff;
35 	usec <<= 8;
36 	usec += (u_long)*bp++ & 0xff;
37 	usec <<= 8;
38 	usec += (u_long)*bp++ & 0xff;
39 	usec <<= 8;
40 	usec += (u_long)*bp & 0xff;
41 #else
42 	bp = (const u_char *)bufp + 7;
43 
44 	usec = (u_long)*bp-- & 0xff;
45 	usec <<= 8;
46 	usec += (u_long)*bp-- & 0xff;
47 	usec <<= 8;
48 	usec += (u_long)*bp-- & 0xff;
49 	usec <<= 8;
50 	usec += (u_long)*bp-- & 0xff;
51 
52 	sec = (u_long)*bp-- & 0xff;
53 	sec <<= 8;
54 	sec += (u_long)*bp-- & 0xff;
55 	sec <<= 8;
56 	sec += (u_long)*bp-- & 0xff;
57 	sec <<= 8;
58 	sec += (u_long)*bp & 0xff;
59 #endif
60 	ts->l_ui = sec + (u_long)JAN_1970;
61 	if (usec > 999999)
62 	    return 0;
63 	TVUTOTSF(usec, ts->l_uf);
64 	return 1;
65 }
66