dns response handling: ignore presence of wrong-type RRs
[musl] / src / time / __tz.c
1 #include "time_impl.h"
2 #include <stdint.h>
3 #include <limits.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <ctype.h>
8 #include "libc.h"
9 #include "lock.h"
10 #include "fork_impl.h"
11
12 #define malloc __libc_malloc
13 #define calloc undef
14 #define realloc undef
15 #define free undef
16
17 long  __timezone = 0;
18 int   __daylight = 0;
19 char *__tzname[2] = { 0, 0 };
20
21 weak_alias(__timezone, timezone);
22 weak_alias(__daylight, daylight);
23 weak_alias(__tzname, tzname);
24
25 static char std_name[TZNAME_MAX+1];
26 static char dst_name[TZNAME_MAX+1];
27 const char __utc[] = "UTC";
28
29 static int dst_off;
30 static int r0[5], r1[5];
31
32 static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
33 static size_t map_size;
34
35 static char old_tz_buf[32];
36 static char *old_tz = old_tz_buf;
37 static size_t old_tz_size = sizeof old_tz_buf;
38
39 static volatile int lock[1];
40 volatile int *const __timezone_lockptr = lock;
41
42 static int getint(const char **p)
43 {
44         unsigned x;
45         for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
46         return x;
47 }
48
49 static int getoff(const char **p)
50 {
51         int neg = 0;
52         if (**p == '-') {
53                 ++*p;
54                 neg = 1;
55         } else if (**p == '+') {
56                 ++*p;
57         }
58         int off = 3600*getint(p);
59         if (**p == ':') {
60                 ++*p;
61                 off += 60*getint(p);
62                 if (**p == ':') {
63                         ++*p;
64                         off += getint(p);
65                 }
66         }
67         return neg ? -off : off;
68 }
69
70 static void getrule(const char **p, int rule[5])
71 {
72         int r = rule[0] = **p;
73
74         if (r!='M') {
75                 if (r=='J') ++*p;
76                 else rule[0] = 0;
77                 rule[1] = getint(p);
78         } else {
79                 ++*p; rule[1] = getint(p);
80                 ++*p; rule[2] = getint(p);
81                 ++*p; rule[3] = getint(p);
82         }
83
84         if (**p=='/') {
85                 ++*p;
86                 rule[4] = getoff(p);
87         } else {
88                 rule[4] = 7200;
89         }
90 }
91
92 static void getname(char *d, const char **p)
93 {
94         int i;
95         if (**p == '<') {
96                 ++*p;
97                 for (i=0; (*p)[i] && (*p)[i]!='>'; i++)
98                         if (i<TZNAME_MAX) d[i] = (*p)[i];
99                 if ((*p)[i]) ++*p;
100         } else {
101                 for (i=0; ((*p)[i]|32)-'a'<26U; i++)
102                         if (i<TZNAME_MAX) d[i] = (*p)[i];
103         }
104         *p += i;
105         d[i<TZNAME_MAX?i:TZNAME_MAX] = 0;
106 }
107
108 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
109
110 static uint32_t zi_read32(const unsigned char *z)
111 {
112         return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
113 }
114
115 static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
116 {
117         size_t y;
118         uint32_t x;
119         for (y=0; n; n--, z+=4, v++) {
120                 x = zi_read32(z);
121                 y += x * *v;
122         }
123         return y;
124 }
125
126 static void do_tzset()
127 {
128         char buf[NAME_MAX+25], *pathname=buf+24;
129         const char *try, *s, *p;
130         const unsigned char *map = 0;
131         size_t i;
132         static const char search[] =
133                 "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
134
135         s = getenv("TZ");
136         if (!s) s = "/etc/localtime";
137         if (!*s) s = __utc;
138
139         if (old_tz && !strcmp(s, old_tz)) return;
140
141         for (i=0; i<5; i++) r0[i] = r1[i] = 0;
142
143         if (zi) __munmap((void *)zi, map_size);
144
145         /* Cache the old value of TZ to check if it has changed. Avoid
146          * free so as not to pull it into static programs. Growth
147          * strategy makes it so free would have minimal benefit anyway. */
148         i = strlen(s);
149         if (i > PATH_MAX+1) s = __utc, i = 3;
150         if (i >= old_tz_size) {
151                 old_tz_size *= 2;
152                 if (i >= old_tz_size) old_tz_size = i+1;
153                 if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
154                 old_tz = malloc(old_tz_size);
155         }
156         if (old_tz) memcpy(old_tz, s, i+1);
157
158         int posix_form = 0;
159         if (*s != ':') {
160                 p = s;
161                 char dummy_name[TZNAME_MAX+1];
162                 getname(dummy_name, &p);
163                 if (p!=s && (*p == '+' || *p == '-' || isdigit(*p)
164                              || !strcmp(dummy_name, "UTC")
165                              || !strcmp(dummy_name, "GMT")))
166                         posix_form = 1;
167         }       
168
169         /* Non-suid can use an absolute tzfile pathname or a relative
170          * pathame beginning with "."; in secure mode, only the
171          * standard path will be searched. */
172         if (!posix_form) {
173                 if (*s == ':') s++;
174                 if (*s == '/' || *s == '.') {
175                         if (!libc.secure || !strcmp(s, "/etc/localtime"))
176                                 map = __map_file(s, &map_size);
177                 } else {
178                         size_t l = strlen(s);
179                         if (l <= NAME_MAX && !strchr(s, '.')) {
180                                 memcpy(pathname, s, l+1);
181                                 pathname[l] = 0;
182                                 for (try=search; !map && *try; try+=l+1) {
183                                         l = strlen(try);
184                                         memcpy(pathname-l, try, l);
185                                         map = __map_file(pathname-l, &map_size);
186                                 }
187                         }
188                 }
189                 if (!map) s = __utc;
190         }
191         if (map && (map_size < 44 || memcmp(map, "TZif", 4))) {
192                 __munmap((void *)map, map_size);
193                 map = 0;
194                 s = __utc;
195         }
196
197         zi = map;
198         if (map) {
199                 int scale = 2;
200                 if (map[4]!='1') {
201                         size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
202                         trans = zi+skip+44+44;
203                         scale++;
204                 } else {
205                         trans = zi+44;
206                 }
207                 index = trans + (zi_read32(trans-12) << scale);
208                 types = index + zi_read32(trans-12);
209                 abbrevs = types + 6*zi_read32(trans-8);
210                 abbrevs_end = abbrevs + zi_read32(trans-4);
211                 if (zi[map_size-1] == '\n') {
212                         for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
213                         s++;
214                 } else {
215                         const unsigned char *p;
216                         __tzname[0] = __tzname[1] = 0;
217                         __daylight = __timezone = dst_off = 0;
218                         for (p=types; p<abbrevs; p+=6) {
219                                 if (!p[4] && !__tzname[0]) {
220                                         __tzname[0] = (char *)abbrevs + p[5];
221                                         __timezone = -zi_read32(p);
222                                 }
223                                 if (p[4] && !__tzname[1]) {
224                                         __tzname[1] = (char *)abbrevs + p[5];
225                                         dst_off = -zi_read32(p);
226                                         __daylight = 1;
227                                 }
228                         }
229                         if (!__tzname[0]) __tzname[0] = __tzname[1];
230                         if (!__tzname[0]) __tzname[0] = (char *)__utc;
231                         if (!__daylight) {
232                                 __tzname[1] = __tzname[0];
233                                 dst_off = __timezone;
234                         }
235                         return;
236                 }
237         }
238
239         if (!s) s = __utc;
240         getname(std_name, &s);
241         __tzname[0] = std_name;
242         __timezone = getoff(&s);
243         getname(dst_name, &s);
244         __tzname[1] = dst_name;
245         if (dst_name[0]) {
246                 __daylight = 1;
247                 if (*s == '+' || *s=='-' || *s-'0'<10U)
248                         dst_off = getoff(&s);
249                 else
250                         dst_off = __timezone - 3600;
251         } else {
252                 __daylight = 0;
253                 dst_off = __timezone;
254         }
255
256         if (*s == ',') s++, getrule(&s, r0);
257         if (*s == ',') s++, getrule(&s, r1);
258 }
259
260 /* Search zoneinfo rules to find the one that applies to the given time,
261  * and determine alternate opposite-DST-status rule that may be needed. */
262
263 static size_t scan_trans(long long t, int local, size_t *alt)
264 {
265         int scale = 3 - (trans == zi+44);
266         uint64_t x;
267         int off = 0;
268
269         size_t a = 0, n = (index-trans)>>scale, m;
270
271         if (!n) {
272                 if (alt) *alt = 0;
273                 return 0;
274         }
275
276         /* Binary search for 'most-recent rule before t'. */
277         while (n > 1) {
278                 m = a + n/2;
279                 x = zi_read32(trans + (m<<scale));
280                 if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
281                 else x = (int32_t)x;
282                 if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
283                 if (t - off < (int64_t)x) {
284                         n /= 2;
285                 } else {
286                         a = m;
287                         n -= n/2;
288                 }
289         }
290
291         /* First and last entry are special. First means to use lowest-index
292          * non-DST type. Last means to apply POSIX-style rule if available. */
293         n = (index-trans)>>scale;
294         if (a == n-1) return -1;
295         if (a == 0) {
296                 x = zi_read32(trans);
297                 if (scale == 3) x = x<<32 | zi_read32(trans + 4);
298                 else x = (int32_t)x;
299                 /* Find the lowest non-DST type, or 0 if none. */
300                 size_t j = 0;
301                 for (size_t i=abbrevs-types; i; i-=6) {
302                         if (!types[i-6+4]) j = i-6;
303                 }
304                 if (local) off = (int32_t)zi_read32(types + j);
305                 /* If t is before first transition, use the above-found type
306                  * and the index-zero (after transition) type as the alt. */
307                 if (t - off < (int64_t)x) {
308                         if (alt) *alt = index[0];
309                         return j/6;
310                 }
311         }
312
313         /* Try to find a neighboring opposite-DST-status rule. */
314         if (alt) {
315                 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
316                         *alt = index[a-1];
317                 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
318                         *alt = index[a+1];
319                 else
320                         *alt = index[a];
321         }
322
323         return index[a];
324 }
325
326 static int days_in_month(int m, int is_leap)
327 {
328         if (m==2) return 28+is_leap;
329         else return 30+((0xad5>>(m-1))&1);
330 }
331
332 /* Convert a POSIX DST rule plus year to seconds since epoch. */
333
334 static long long rule_to_secs(const int *rule, int year)
335 {
336         int is_leap;
337         long long t = __year_to_secs(year, &is_leap);
338         int x, m, n, d;
339         if (rule[0]!='M') {
340                 x = rule[1];
341                 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
342                 t += 86400 * x;
343         } else {
344                 m = rule[1];
345                 n = rule[2];
346                 d = rule[3];
347                 t += __month_to_secs(m-1, is_leap);
348                 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
349                 int days = d - wday;
350                 if (days < 0) days += 7;
351                 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
352                 t += 86400 * (days + 7*(n-1));
353         }
354         t += rule[4];
355         return t;
356 }
357
358 /* Determine the time zone in effect for a given time in seconds since the
359  * epoch. It can be given in local or universal time. The results will
360  * indicate whether DST is in effect at the queried time, and will give both
361  * the GMT offset for the active zone/DST rule and the opposite DST. This
362  * enables a caller to efficiently adjust for the case where an explicit
363  * DST specification mismatches what would be in effect at the time. */
364
365 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
366 {
367         LOCK(lock);
368
369         do_tzset();
370
371         if (zi) {
372                 size_t alt, i = scan_trans(t, local, &alt);
373                 if (i != -1) {
374                         *isdst = types[6*i+4];
375                         *offset = (int32_t)zi_read32(types+6*i);
376                         *zonename = (const char *)abbrevs + types[6*i+5];
377                         if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
378                         UNLOCK(lock);
379                         return;
380                 }
381         }
382
383         if (!__daylight) goto std;
384
385         /* FIXME: may be broken if DST changes right at year boundary?
386          * Also, this could be more efficient.*/
387         long long y = t / 31556952 + 70;
388         while (__year_to_secs(y, 0) > t) y--;
389         while (__year_to_secs(y+1, 0) < t) y++;
390
391         long long t0 = rule_to_secs(r0, y);
392         long long t1 = rule_to_secs(r1, y);
393
394         if (!local) {
395                 t0 += __timezone;
396                 t1 += dst_off;
397         }
398         if (t0 < t1) {
399                 if (t >= t0 && t < t1) goto dst;
400                 goto std;
401         } else {
402                 if (t >= t1 && t < t0) goto std;
403                 goto dst;
404         }
405 std:
406         *isdst = 0;
407         *offset = -__timezone;
408         if (oppoff) *oppoff = -dst_off;
409         *zonename = __tzname[0];
410         UNLOCK(lock);
411         return;
412 dst:
413         *isdst = 1;
414         *offset = -dst_off;
415         if (oppoff) *oppoff = -__timezone;
416         *zonename = __tzname[1];
417         UNLOCK(lock);
418 }
419
420 static void __tzset()
421 {
422         LOCK(lock);
423         do_tzset();
424         UNLOCK(lock);
425 }
426
427 weak_alias(__tzset, tzset);
428
429 const char *__tm_to_tzname(const struct tm *tm)
430 {
431         const void *p = tm->__tm_zone;
432         LOCK(lock);
433         do_tzset();
434         if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
435             (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
436                 p = "";
437         UNLOCK(lock);
438         return p;
439 }