1From 3f0580f2546de8be7acf1bc78a55a257bc638ebe Mon Sep 17 00:00:00 2001
2From: Bartosz Brachaczek <b.brachaczek@gmail.com>
3Date: Tue, 12 Nov 2019 14:31:08 +0100
4Subject: [PATCH] Make HgfsConvertFromNtTimeNsec aware of 64-bit time_t on i386
5
6I verified that this function behaves as expected on x86_64, i386 with
732-bit time_t, and i386 with 64-bit time_t for the following values of
8ntTtime:
9
10UNIX_EPOCH-1, UNIX_EPOCH, UNIX_EPOCH+1, UNIX_S32_MAX-1, UNIX_S32_MAX,
11UNIX_S32_MAX+1, UNIX_S32_MAX*2+1
12
13I did not verify whether the use of Div643264 is optimal, performance
14wise.
15
16Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
17---
18 lib/hgfs/hgfsUtil.c | 34 +++++++++++++++++++---------------
19 1 file changed, 19 insertions(+), 15 deletions(-)
20
21diff --git a/lib/hgfs/hgfsUtil.c b/lib/hgfs/hgfsUtil.c
22index cc580ab8..49b10040 100644
23--- a/lib/hgfs/hgfsUtil.c
24+++ b/lib/hgfs/hgfsUtil.c
25@@ -110,23 +110,21 @@ HgfsConvertFromNtTimeNsec(struct timespec *unixTime, // OUT: Time in UNIX format
26 			  uint64 ntTime) // IN: Time in Windows NT format
27 {
28 #ifdef __i386__
29-   uint32 sec;
30-   uint32 nsec;
31+   uint64 sec64;
32+   uint32 sec32, nsec;
33+#endif
34
35    ASSERT(unixTime);
36-   /* We assume that time_t is 32bit */
37-   ASSERT_ON_COMPILE(sizeof (unixTime->tv_sec) == 4);
38
39-   /* Cap NT time values that are outside of Unix time's range */
40+   if (sizeof (unixTime->tv_sec) == 4) {
41+      /* Cap NT time values that are outside of Unix time's range */
42
43-   if (ntTime >= UNIX_S32_MAX) {
44-      unixTime->tv_sec = 0x7FFFFFFF;
45-      unixTime->tv_nsec = 0;
46-      return 1;
47+      if (ntTime >= UNIX_S32_MAX) {
48+         unixTime->tv_sec = 0x7FFFFFFF;
49+         unixTime->tv_nsec = 0;
50+         return 1;
51+      }
52    }
53-#else
54-   ASSERT(unixTime);
55-#endif
56
57    if (ntTime < UNIX_EPOCH) {
58       unixTime->tv_sec = 0;
59@@ -135,9 +133,15 @@ HgfsConvertFromNtTimeNsec(struct timespec *unixTime, // OUT: Time in UNIX format
60    }
61
62 #ifdef __i386__
63-   Div643232(ntTime - UNIX_EPOCH, 10000000, &sec, &nsec);
64-   unixTime->tv_sec = sec;
65-   unixTime->tv_nsec = nsec * 100;
66+   if (sizeof (unixTime->tv_sec) == 4) {
67+      Div643232(ntTime - UNIX_EPOCH, 10000000, &sec32, &nsec);
68+      unixTime->tv_sec = sec32;
69+      unixTime->tv_nsec = nsec * 100;
70+   } else {
71+      Div643264(ntTime - UNIX_EPOCH, 10000000, &sec64, &nsec);
72+      unixTime->tv_sec = sec64;
73+      unixTime->tv_nsec = nsec * 100;
74+   }
75 #else
76    unixTime->tv_sec = (ntTime - UNIX_EPOCH) / 10000000;
77    unixTime->tv_nsec = ((ntTime - UNIX_EPOCH) % 10000000) * 100;
78--
792.25.1
80
81