diff options
author | Brent Cook <bcook@openbsd.org> | 2015-10-15 14:26:24 -0500 |
---|---|---|
committer | Brent Cook <bcook@openbsd.org> | 2015-10-15 14:26:24 -0500 |
commit | c02436645dafd0e97ee7e91ab8afd70bb997453c (patch) | |
tree | 3c439f141e7313cbeab4361cfcb14bbfd3a50d85 | |
parent | ea4658b46ae6a98bb1fa09ffd24ec08440d53185 (diff) | |
download | portable-c02436645dafd0e97ee7e91ab8afd70bb997453c.tar.gz portable-c02436645dafd0e97ee7e91ab8afd70bb997453c.tar.bz2 portable-c02436645dafd0e97ee7e91ab8afd70bb997453c.zip |
use timegm from musl
-rw-r--r-- | crypto/compat/timegm.c | 251 |
1 files changed, 191 insertions, 60 deletions
diff --git a/crypto/compat/timegm.c b/crypto/compat/timegm.c index 2007140..81286bd 100644 --- a/crypto/compat/timegm.c +++ b/crypto/compat/timegm.c | |||
@@ -1,71 +1,202 @@ | |||
1 | /* | 1 | /* |
2 | * timegm shims based on example code from in the glibc timegm manpage. | 2 | * ---------------------------------------------------------------------- |
3 | * Copyright © 2005-2014 Rich Felker, et al. | ||
3 | * | 4 | * |
4 | * These should be replaced with lockless versions that do not require | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
5 | * modifying global state. | 6 | * a copy of this software and associated documentation files (the |
7 | * "Software"), to deal in the Software without restriction, including | ||
8 | * without limitation the rights to use, copy, modify, merge, publish, | ||
9 | * distribute, sublicense, and/or sell copies of the Software, and to | ||
10 | * permit persons to whom the Software is furnished to do so, subject to | ||
11 | * the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be | ||
14 | * included in all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
23 | * ---------------------------------------------------------------------- | ||
6 | */ | 24 | */ |
7 | 25 | ||
26 | #include <errno.h> | ||
27 | #include <limits.h> | ||
8 | #include <time.h> | 28 | #include <time.h> |
9 | #include <stdlib.h> | 29 | |
10 | #include <stdio.h> | 30 | /* 2000-03-01 (mod 400 year, immediately after feb29 */ |
11 | #include <string.h> | 31 | #define LEAPOCH (946684800LL + 86400*(31+29)) |
12 | 32 | ||
13 | #ifdef _WIN32 | 33 | #define DAYS_PER_400Y (365*400 + 97) |
14 | #include <windows.h> | 34 | #define DAYS_PER_100Y (365*100 + 24) |
15 | time_t | 35 | #define DAYS_PER_4Y (365*4 + 1) |
16 | timegm(struct tm *tm) | 36 | |
37 | int __month_to_secs(int month, int is_leap) | ||
17 | { | 38 | { |
18 | time_t ret; | 39 | static const int secs_through_month[] = { |
19 | char *tz, *buf; | 40 | 0, 31*86400, 59*86400, 90*86400, |
20 | static volatile HANDLE mtx = NULL; | 41 | 120*86400, 151*86400, 181*86400, 212*86400, |
21 | 42 | 243*86400, 273*86400, 304*86400, 334*86400 }; | |
22 | if (!mtx) { | 43 | int t = secs_through_month[month]; |
23 | HANDLE p = CreateMutex(NULL, FALSE, NULL); | 44 | if (is_leap && month >= 2) t+=86400; |
24 | if (InterlockedCompareExchangePointer( | 45 | return t; |
25 | (void **)&mtx, (void *)p, NULL)) | 46 | } |
26 | CloseHandle(p); | 47 | |
48 | long long __year_to_secs(long long year, int *is_leap) | ||
49 | { | ||
50 | if (year-2ULL <= 136) { | ||
51 | int y = year; | ||
52 | int leaps = (y-68)>>2; | ||
53 | if (!((y-68)&3)) { | ||
54 | leaps--; | ||
55 | if (is_leap) *is_leap = 1; | ||
56 | } else if (is_leap) *is_leap = 0; | ||
57 | return 31536000*(y-70) + 86400*leaps; | ||
58 | } | ||
59 | |||
60 | int cycles, centuries, leaps, rem; | ||
61 | |||
62 | if (!is_leap) is_leap = &(int){0}; | ||
63 | cycles = (year-100) / 400; | ||
64 | rem = (year-100) % 400; | ||
65 | if (rem < 0) { | ||
66 | cycles--; | ||
67 | rem += 400; | ||
68 | } | ||
69 | if (!rem) { | ||
70 | *is_leap = 1; | ||
71 | centuries = 0; | ||
72 | leaps = 0; | ||
73 | } else { | ||
74 | if (rem >= 200) { | ||
75 | if (rem >= 300) centuries = 3, rem -= 300; | ||
76 | else centuries = 2, rem -= 200; | ||
77 | } else { | ||
78 | if (rem >= 100) centuries = 1, rem -= 100; | ||
79 | else centuries = 0; | ||
80 | } | ||
81 | if (!rem) { | ||
82 | *is_leap = 0; | ||
83 | leaps = 0; | ||
84 | } else { | ||
85 | leaps = rem / 4U; | ||
86 | rem %= 4U; | ||
87 | *is_leap = !rem; | ||
88 | } | ||
27 | } | 89 | } |
28 | WaitForSingleObject(mtx, INFINITE); | 90 | |
29 | tz = getenv("TZ"); | 91 | leaps += 97*cycles + 24*centuries - *is_leap; |
30 | if (tz) { | 92 | |
31 | if (asprintf(&buf, "TZ=%s", tz) == -1) | 93 | return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400; |
32 | buf = NULL; | 94 | } |
95 | |||
96 | long long __tm_to_secs(const struct tm *tm) | ||
97 | { | ||
98 | int is_leap; | ||
99 | long long year = tm->tm_year; | ||
100 | int month = tm->tm_mon; | ||
101 | if (month >= 12 || month < 0) { | ||
102 | int adj = month / 12; | ||
103 | month %= 12; | ||
104 | if (month < 0) { | ||
105 | adj--; | ||
106 | month += 12; | ||
107 | } | ||
108 | year += adj; | ||
33 | } | 109 | } |
34 | putenv("TZ=UTC"); | 110 | long long t = __year_to_secs(year, &is_leap); |
35 | tzset(); | 111 | t += __month_to_secs(month, is_leap); |
36 | ret = mktime(tm); | 112 | t += 86400LL * (tm->tm_mday-1); |
37 | if (buf) { | 113 | t += 3600LL * tm->tm_hour; |
38 | putenv(buf); | 114 | t += 60LL * tm->tm_min; |
39 | free(buf); | 115 | t += tm->tm_sec; |
40 | } else | 116 | return t; |
41 | putenv("TZ="); | ||
42 | tzset(); | ||
43 | ReleaseMutex(mtx); | ||
44 | return ret; | ||
45 | } | 117 | } |
46 | #else | 118 | |
47 | #include <pthread.h> | 119 | int __secs_to_tm(long long t, struct tm *tm) |
48 | time_t | ||
49 | timegm(struct tm *tm) | ||
50 | { | 120 | { |
51 | time_t ret; | 121 | long long days, secs; |
52 | char *tz; | 122 | int remdays, remsecs, remyears; |
53 | static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; | 123 | int qc_cycles, c_cycles, q_cycles; |
54 | 124 | int years, months; | |
55 | pthread_mutex_lock(&mtx); | 125 | int wday, yday, leap; |
56 | tz = getenv("TZ"); | 126 | static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29}; |
57 | if (tz) | 127 | |
58 | tz = strdup(tz); | 128 | /* Reject time_t values whose year would overflow int */ |
59 | setenv("TZ", "", 1); | 129 | if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) |
60 | tzset(); | 130 | return -1; |
61 | ret = mktime(tm); | 131 | |
62 | if (tz) { | 132 | secs = t - LEAPOCH; |
63 | setenv("TZ", tz, 1); | 133 | days = secs / 86400; |
64 | free(tz); | 134 | remsecs = secs % 86400; |
65 | } else | 135 | if (remsecs < 0) { |
66 | unsetenv("TZ"); | 136 | remsecs += 86400; |
67 | tzset(); | 137 | days--; |
68 | pthread_mutex_unlock(&mtx); | 138 | } |
69 | return ret; | 139 | |
140 | wday = (3+days)%7; | ||
141 | if (wday < 0) wday += 7; | ||
142 | |||
143 | qc_cycles = days / DAYS_PER_400Y; | ||
144 | remdays = days % DAYS_PER_400Y; | ||
145 | if (remdays < 0) { | ||
146 | remdays += DAYS_PER_400Y; | ||
147 | qc_cycles--; | ||
148 | } | ||
149 | |||
150 | c_cycles = remdays / DAYS_PER_100Y; | ||
151 | if (c_cycles == 4) c_cycles--; | ||
152 | remdays -= c_cycles * DAYS_PER_100Y; | ||
153 | |||
154 | q_cycles = remdays / DAYS_PER_4Y; | ||
155 | if (q_cycles == 25) q_cycles--; | ||
156 | remdays -= q_cycles * DAYS_PER_4Y; | ||
157 | |||
158 | remyears = remdays / 365; | ||
159 | if (remyears == 4) remyears--; | ||
160 | remdays -= remyears * 365; | ||
161 | |||
162 | leap = !remyears && (q_cycles || !c_cycles); | ||
163 | yday = remdays + 31 + 28 + leap; | ||
164 | if (yday >= 365+leap) yday -= 365+leap; | ||
165 | |||
166 | years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles; | ||
167 | |||
168 | for (months=0; days_in_month[months] <= remdays; months++) | ||
169 | remdays -= days_in_month[months]; | ||
170 | |||
171 | if (years+100 > INT_MAX || years+100 < INT_MIN) | ||
172 | return -1; | ||
173 | |||
174 | tm->tm_year = years + 100; | ||
175 | tm->tm_mon = months + 2; | ||
176 | if (tm->tm_mon >= 12) { | ||
177 | tm->tm_mon -=12; | ||
178 | tm->tm_year++; | ||
179 | } | ||
180 | tm->tm_mday = remdays + 1; | ||
181 | tm->tm_wday = wday; | ||
182 | tm->tm_yday = yday; | ||
183 | |||
184 | tm->tm_hour = remsecs / 3600; | ||
185 | tm->tm_min = remsecs / 60 % 60; | ||
186 | tm->tm_sec = remsecs % 60; | ||
187 | |||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | time_t timegm(struct tm *tm) | ||
192 | { | ||
193 | struct tm new; | ||
194 | long long t = __tm_to_secs(tm); | ||
195 | if (__secs_to_tm(t, &new) < 0) { | ||
196 | errno = EOVERFLOW; | ||
197 | return -1; | ||
198 | } | ||
199 | *tm = new; | ||
200 | tm->tm_isdst = 0; | ||
201 | return t; | ||
70 | } | 202 | } |
71 | #endif | ||