Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/SoftEtherVPN/SoftEtherVPN_Stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordnobori <da.git@softether.co.jp>2016-11-27 11:43:14 +0300
committerdnobori <da.git@softether.co.jp>2016-11-27 11:43:14 +0300
commit4df2eb4f9c2a8cf50e0123496bc20470edc21dc4 (patch)
treec31ceaa1709f599be25917bd35ae67f489d13652 /src/Mayaqua/Kernel.c
parent0978e1a016e8de556514e51ffe1d7aa553562f19 (diff)
v4.22-9634-betav4.22-9634-beta
Diffstat (limited to 'src/Mayaqua/Kernel.c')
-rw-r--r--src/Mayaqua/Kernel.c250
1 files changed, 144 insertions, 106 deletions
diff --git a/src/Mayaqua/Kernel.c b/src/Mayaqua/Kernel.c
index 94c2a621..fec7a886 100644
--- a/src/Mayaqua/Kernel.c
+++ b/src/Mayaqua/Kernel.c
@@ -172,6 +172,109 @@ static LOCALE current_locale;
LOCK *tick_manual_lock = NULL;
UINT g_zero = 0;
+#define MONSPERYEAR 12
+#define DAYSPERNYEAR 365
+#define DAYSPERLYEAR 366
+#define SECSPERMIN 60
+#define SECSPERHOUR (60*60)
+#define SECSPERDAY (24*60*60)
+#define DAYSPERWEEK 7
+#define TM_SUNDAY 0
+#define TM_MONDAY 1
+#define TM_TUESDAY 2
+#define TM_WEDNESDAY 3
+#define TM_THURSDAY 4
+#define TM_FRIDAY 5
+#define TM_SATURDAY 6
+
+#define TM_YEAR_BASE 1900
+
+#define EPOCH_YEAR 1970
+#define EPOCH_WDAY TM_THURSDAY
+
+#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
+
+static const int mon_lengths[2][MONSPERYEAR] = {
+ { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+ { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+};
+
+static const int year_lengths[2] = {
+ DAYSPERNYEAR, DAYSPERLYEAR
+};
+
+
+/*
+ * Taken from FreeBSD src / lib / libc / stdtime / localtime.c 1.43 revision.
+ * localtime.c 7.78.
+ * tzfile.h 1.8
+ * adapted to be replacement gmtime_r.
+ */
+static void
+c_timesub(timep, offset, tmp)
+const time_64t * const timep;
+const long offset;
+struct tm * const tmp;
+{
+ INT64 days;
+ INT64 rem;
+ INT64 y;
+ int yleap;
+ const int * ip;
+
+ days = *timep / SECSPERDAY;
+ rem = *timep % SECSPERDAY;
+ rem += (offset);
+ while (rem < 0) {
+ rem += SECSPERDAY;
+ --days;
+ }
+ while (rem >= SECSPERDAY) {
+ rem -= SECSPERDAY;
+ ++days;
+ }
+ tmp->tm_hour = (int) (rem / SECSPERHOUR);
+ rem = rem % SECSPERHOUR;
+ tmp->tm_min = (int) (rem / SECSPERMIN);
+ /*
+ ** A positive leap second requires a special
+ ** representation. This uses "... ??:59:60" et seq.
+ */
+ tmp->tm_sec = (int) (rem % SECSPERMIN) ;
+ tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
+ if (tmp->tm_wday < 0)
+ tmp->tm_wday += DAYSPERWEEK;
+ y = EPOCH_YEAR;
+#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
+ while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
+ INT64 newy;
+
+ newy = y + days / DAYSPERNYEAR;
+ if (days < 0)
+ --newy;
+ days -= (newy - y) * DAYSPERNYEAR +
+ LEAPS_THRU_END_OF(newy - 1) -
+ LEAPS_THRU_END_OF(y - 1);
+ y = newy;
+ }
+ tmp->tm_year = (int)(y - TM_YEAR_BASE);
+ tmp->tm_yday = (int) days;
+ ip = mon_lengths[yleap];
+ for (tmp->tm_mon = 0; days >= (INT64) ip[tmp->tm_mon]; ++(tmp->tm_mon))
+ days = days - (INT64) ip[tmp->tm_mon];
+ tmp->tm_mday = (int) (days + 1);
+ tmp->tm_isdst = 0;
+}
+
+/*
+* Re-entrant version of gmtime.
+*/
+struct tm * c_gmtime_r(const time_64t* timep, struct tm *tm)
+{
+ c_timesub(timep, 0L, tm);
+ return tm;
+}
+
// Get the real-time system timer
UINT TickRealtime()
{
@@ -219,7 +322,14 @@ UINT64 TickGetRealtimeTickValue64()
gettimeofday(&tv, &tz);
- ret = (UINT64)tv.tv_sec * 1000ULL + (UINT64)tv.tv_usec / 1000ULL;
+ if (sizeof(tv.tv_sec) != 4)
+ {
+ ret = (UINT64)tv.tv_sec * 1000ULL + (UINT64)tv.tv_usec / 1000ULL;
+ }
+ else
+ {
+ ret = (UINT64)((UINT64)((UINT32)tv.tv_sec)) * 1000ULL + (UINT64)tv.tv_usec / 1000ULL;
+ }
return ret;
}
@@ -815,7 +925,7 @@ void GetTimeStr64(char *str, UINT size, UINT64 sec64)
// Convert to a time to be used safely in the current POSIX implementation
UINT64 SafeTime64(UINT64 sec64)
{
- return MAKESURE(sec64, 0, 2115947647000ULL);
+ return MAKESURE(sec64, 0, 4102243323123ULL);
}
// Thread pool
@@ -1694,7 +1804,7 @@ void TmToSystem(SYSTEMTIME *st, struct tm *t)
NormalizeTm(&tmp);
Zero(st, sizeof(SYSTEMTIME));
- st->wYear = MAKESURE(tmp.tm_year + 1900, 1970, 2037);
+ st->wYear = MAKESURE(tmp.tm_year + 1900, 1970, 2099);
st->wMonth = MAKESURE(tmp.tm_mon + 1, 1, 12);
st->wDay = MAKESURE(tmp.tm_mday, 1, 31);
st->wDayOfWeek = MAKESURE(tmp.tm_wday, 0, 6);
@@ -1714,7 +1824,7 @@ void SystemToTm(struct tm *t, SYSTEMTIME *st)
}
Zero(t, sizeof(struct tm));
- t->tm_year = MAKESURE(st->wYear, 1970, 2037) - 1900;
+ t->tm_year = MAKESURE(st->wYear, 1970, 2099) - 1900;
t->tm_mon = MAKESURE(st->wMonth, 1, 12) - 1;
t->tm_mday = MAKESURE(st->wDay, 1, 31);
t->tm_hour = MAKESURE(st->wHour, 0, 23);
@@ -1726,7 +1836,7 @@ void SystemToTm(struct tm *t, SYSTEMTIME *st)
}
// Convert the time_t to SYSTEMTIME
-void TimeToSystem(SYSTEMTIME *st, time_t t)
+void TimeToSystem(SYSTEMTIME *st, time_64t t)
{
struct tm tmp;
// Validate arguments
@@ -1740,7 +1850,7 @@ void TimeToSystem(SYSTEMTIME *st, time_t t)
}
// Convert the time_t to 64-bit SYSTEMTIME
-UINT64 TimeToSystem64(time_t t)
+UINT64 TimeToSystem64(time_64t t)
{
SYSTEMTIME st;
@@ -1750,7 +1860,7 @@ UINT64 TimeToSystem64(time_t t)
}
// Convert the SYSTEMTIME to time_t
-time_t SystemToTime(SYSTEMTIME *st)
+time_64t SystemToTime(SYSTEMTIME *st)
{
struct tm t;
// Validate arguments
@@ -1764,7 +1874,7 @@ time_t SystemToTime(SYSTEMTIME *st)
}
// Convert a 64-bit SYSTEMTIME to a time_t
-time_t System64ToTime(UINT64 i)
+time_64t System64ToTime(UINT64 i)
{
SYSTEMTIME st;
@@ -1774,9 +1884,9 @@ time_t System64ToTime(UINT64 i)
}
// Convert the tm to time_t
-time_t TmToTime(struct tm *t)
+time_64t TmToTime(struct tm *t)
{
- time_t tmp;
+ time_64t tmp;
// Validate arguments
if (t == NULL)
{
@@ -1784,7 +1894,7 @@ time_t TmToTime(struct tm *t)
}
tmp = c_mkgmtime(t);
- if (tmp == (time_t)-1)
+ if (tmp == (time_64t)-1)
{
return 0;
}
@@ -1792,42 +1902,22 @@ time_t TmToTime(struct tm *t)
}
// Convert time_t to tm
-void TimeToTm(struct tm *t, time_t time)
+void TimeToTm(struct tm *t, time_64t time)
{
- struct tm *ret;
// Validate arguments
if (t == NULL)
{
return;
}
-#ifndef OS_UNIX
- ret = gmtime(&time);
-#else // OS_UNIX
- ret = malloc(sizeof(struct tm));
- memset(ret, 0, sizeof(struct tm));
- gmtime_r(&time, ret);
-#endif // OS_UNIX
-
- if (ret == NULL)
- {
- Zero(t, sizeof(struct tm));
- }
- else
- {
- Copy(t, ret, sizeof(struct tm));
- }
-
-#ifdef OS_UNIX
- free(ret);
-#endif // OS_UNIX
+ Zero(t, sizeof(struct tm));
+ c_gmtime_r(&time, t);
}
// Normalize the tm
void NormalizeTm(struct tm *t)
{
- struct tm *ret;
- time_t tmp;
+ time_64t tmp;
// Validate arguments
if (t == NULL)
{
@@ -1835,31 +1925,12 @@ void NormalizeTm(struct tm *t)
}
tmp = c_mkgmtime(t);
- if (tmp == (time_t)-1)
+ if (tmp == (time_64t)-1)
{
return;
}
-#ifndef OS_UNIX
- ret = gmtime(&tmp);
-#else // OS_UNIX
- ret = malloc(sizeof(struct tm));
- memset(ret, 0, sizeof(struct tm));
- gmtime_r(&tmp, ret);
-#endif // OS_UNIX
-
- if (ret == NULL)
- {
- Zero(t, sizeof(struct tm));
- }
- else
- {
- Copy(t, ret, sizeof(struct tm));
- }
-
-#ifdef OS_UNIX
- free(ret);
-#endif // OS_UNIX
+ c_gmtime_r(&tmp, t);
}
// Normalize the SYSTEMTIME
@@ -1934,10 +2005,19 @@ INT64 GetTimeDiffEx(SYSTEMTIME *basetime, bool local_time)
Copy(&snow, basetime, sizeof(SYSTEMTIME));
+ if (sizeof(time_t) == 4)
+ {
+ if (snow.wYear >= 2038)
+ {
+ // For old systems: avoid the 2038-year problem
+ snow.wYear = 2037;
+ }
+ }
+
SystemToTm(&now, &snow);
if (local_time == false)
{
- tmp = c_mkgmtime(&now);
+ tmp = (time_t)c_mkgmtime(&now);
}
else
{
@@ -1965,54 +2045,12 @@ INT64 GetTimeDiffEx(SYSTEMTIME *basetime, bool local_time)
return ret;
}
-// Get the time difference between the local time and system time
-INT64 GetTimeDiff()
-{
- time_t tmp;
- struct tm t1, t2;
- SYSTEMTIME snow;
- struct tm now;
- SYSTEMTIME s1, s2;
- INT64 ret;
-
- static INT64 cache = INFINITE;
-
- if (cache != INFINITE)
- {
- // Returns the cache data after measured once
- return cache;
- }
-
- SystemTime(&snow);
- SystemToTm(&now, &snow);
- tmp = c_mkgmtime(&now);
- if (tmp == (time_t)-1)
- {
- return 0;
- }
-
-#ifndef OS_UNIX
- Copy(&t1, localtime(&tmp), sizeof(struct tm));
- Copy(&t2, gmtime(&tmp), sizeof(struct tm));
-#else // OS_UNIX
- localtime_r(&tmp, &t1);
- gmtime_r(&tmp, &t2);
-#endif // OS_UNIX
-
- TmToSystem(&s1, &t1);
- TmToSystem(&s2, &t2);
-
- cache = ret = (INT)SystemToUINT64(&s1) - (INT)SystemToUINT64(&s2);
-
- return ret;
-}
-
// Convert UINT64 to the SYSTEMTIME
void UINT64ToSystem(SYSTEMTIME *st, UINT64 sec64)
{
UINT64 tmp64;
UINT sec, millisec;
- time_t time;
+ time_64t time;
// Validate arguments
if (st == NULL)
{
@@ -2023,7 +2061,7 @@ void UINT64ToSystem(SYSTEMTIME *st, UINT64 sec64)
tmp64 = sec64 / (UINT64)1000;
millisec = (UINT)(sec64 - tmp64 * (UINT64)1000);
sec = (UINT)tmp64;
- time = (time_t)sec;
+ time = (time_64t)sec;
TimeToSystem(st, time);
st->wMilliseconds = (WORD)millisec;
}
@@ -2032,7 +2070,7 @@ void UINT64ToSystem(SYSTEMTIME *st, UINT64 sec64)
UINT64 SystemToUINT64(SYSTEMTIME *st)
{
UINT64 sec64;
- time_t time;
+ time_64t time;
// Validate arguments
if (st == NULL)
{
@@ -2091,7 +2129,7 @@ void SystemTime(SYSTEMTIME *st)
KS_INC(KS_GETTIME_COUNT);
}
-time_t c_mkgmtime(struct tm *tm)
+time_64t c_mkgmtime(struct tm *tm)
{
int years, months, days, hours, minutes, seconds;
@@ -2142,7 +2180,7 @@ time_t c_mkgmtime(struct tm *tm)
tm->tm_isdst = 0;
if (years < 1970)
- return (time_t)-1;
+ return (time_64t)-1;
#if (defined(TM_YEAR_MAX) && defined(TM_MON_MAX) && defined(TM_MDAY_MAX))
#if (defined(TM_HOUR_MAX) && defined(TM_MIN_MAX) && defined(TM_SEC_MAX))
@@ -2156,11 +2194,11 @@ time_t c_mkgmtime(struct tm *tm)
(hours == TM_HOUR_MAX &&
(minutes > TM_MIN_MAX ||
(minutes == TM_MIN_MAX && seconds > TM_SEC_MAX) )))))))
- return (time_t)-1;
+ return (time_64t)-1;
#endif
#endif
- return (time_t)(86400L * (unsigned long)(unsigned)days +
+ return (time_64t)(86400L * (unsigned long)(unsigned)days +
3600L * (unsigned long)hours +
(unsigned long)(60 * minutes + seconds));
}