1 /* Copyright (C) 1991-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
21 #include <stdio_ext.h>
29 #include <timezone/tzfile.h>
32 static dev_t tzfile_dev;
33 static ino64_t tzfile_ino;
34 static time_t tzfile_mtime;
38 long int offset; /* Seconds east of GMT. */
39 unsigned char isdst; /* Used to set tm_isdst. */
40 unsigned char idx; /* Index into `zone_names'. */
41 unsigned char isstd; /* Transition times are in standard time. */
42 unsigned char isgmt; /* Transition times are in GMT. */
47 time_t transition; /* Time the transition takes effect. */
48 long int change; /* Seconds of correction to apply. */
51 static void compute_tzname_max (size_t) internal_function;
53 static size_t num_transitions;
54 libc_freeres_ptr (static time_t *transitions);
55 static unsigned char *type_idxs;
56 static size_t num_types;
57 static struct ttinfo *types;
58 static char *zone_names;
59 static long int rule_stdoff;
60 static long int rule_dstoff;
61 static size_t num_leaps;
62 static struct leap *leaps;
68 /* Decode the four bytes at PTR as a signed integer in network byte order. */
70 __attribute ((always_inline))
71 decode (const void *ptr)
73 if (BYTE_ORDER == BIG_ENDIAN && sizeof (int) == 4)
74 return *(const int *) ptr;
75 if (sizeof (int) == 4)
76 return bswap_32 (*(const int *) ptr);
78 const unsigned char *p = ptr;
79 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
81 result = (result << 8) | *p++;
82 result = (result << 8) | *p++;
83 result = (result << 8) | *p++;
84 result = (result << 8) | *p++;
91 __attribute ((always_inline))
92 decode64 (const void *ptr)
94 if ((BYTE_ORDER == BIG_ENDIAN))
95 return *(const int64_t *) ptr;
97 return bswap_64 (*(const int64_t *) ptr);
102 __tzfile_read (const char *file, size_t extra, char **extrap)
104 static const char default_tzdir[] = TZDIR;
105 size_t num_isstd, num_isgmt;
107 struct tzhead tzhead;
113 int was_using_tzfile = __use_tzfile;
117 if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
123 /* No user specification; use the site-wide default. */
125 else if (*file == '\0')
126 /* User specified the empty string; use UTC with no leap seconds. */
127 goto ret_free_transitions;
130 /* We must not allow to read an arbitrary file in a setuid
131 program. So we fail for any file which is not in the
132 directory hierachy starting at TZDIR
133 and which is not the system wide default TZDEFAULT. */
134 if (__libc_enable_secure
136 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
137 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
138 || strstr (file, "../") != NULL))
139 /* This test is certainly a bit too restrictive but it should
140 catch all critical cases. */
141 goto ret_free_transitions;
147 unsigned int len, tzdir_len;
150 tzdir = getenv ("TZDIR");
151 if (tzdir == NULL || *tzdir == '\0')
153 tzdir = default_tzdir;
154 tzdir_len = sizeof (default_tzdir) - 1;
157 tzdir_len = strlen (tzdir);
158 len = strlen (file) + 1;
159 new = (char *) __alloca (tzdir_len + 1 + len);
160 tmp = __mempcpy (new, tzdir, tzdir_len);
162 memcpy (tmp, file, len);
166 /* If we were already using tzfile, check whether the file changed. */
169 && stat64 (file, &st) == 0
170 && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
171 && tzfile_mtime == st.st_mtime)
178 /* Note the file is opened with cancellation in the I/O functions
179 disabled and if available FD_CLOEXEC set. */
180 f = fopen (file, "rce");
182 goto ret_free_transitions;
184 /* Get information about the file we are actually using. */
185 if (fstat64 (fileno (f), &st) != 0)
188 goto ret_free_transitions;
191 free ((void *) transitions);
194 /* Remember the inode and device number and modification time. */
195 tzfile_dev = st.st_dev;
196 tzfile_ino = st.st_ino;
197 tzfile_mtime = st.st_mtime;
199 /* No threads reading this stream. */
200 __fsetlocking (f, FSETLOCKING_BYCALLER);
203 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
205 || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
208 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
209 num_types = (size_t) decode (tzhead.tzh_typecnt);
210 chars = (size_t) decode (tzhead.tzh_charcnt);
211 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
212 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
213 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
215 /* For platforms with 64-bit time_t we use the new format if available. */
216 if (sizeof (time_t) == 8 && trans_width == 4
217 && tzhead.tzh_version[0] != '\0')
219 /* We use the 8-byte format. */
222 /* Position the stream before the second header. */
223 size_t to_skip = (num_transitions * (4 + 1)
229 if (fseek (f, to_skip, SEEK_CUR) != 0)
235 if (__builtin_expect (num_transitions
236 > ((SIZE_MAX - (__alignof__ (struct ttinfo) - 1))
237 / (sizeof (time_t) + 1)), 0))
239 total_size = num_transitions * (sizeof (time_t) + 1);
240 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
241 & ~(__alignof__ (struct ttinfo) - 1));
242 types_idx = total_size;
243 if (__builtin_expect (num_types
244 > (SIZE_MAX - total_size) / sizeof (struct ttinfo), 0))
246 total_size += num_types * sizeof (struct ttinfo);
247 if (__builtin_expect (chars > SIZE_MAX - total_size, 0))
250 if (__builtin_expect (__alignof__ (struct leap) - 1
251 > SIZE_MAX - total_size, 0))
253 total_size = ((total_size + __alignof__ (struct leap) - 1)
254 & ~(__alignof__ (struct leap) - 1));
255 leaps_idx = total_size;
256 if (__builtin_expect (num_leaps
257 > (SIZE_MAX - total_size) / sizeof (struct leap), 0))
259 total_size += num_leaps * sizeof (struct leap);
261 if (sizeof (time_t) == 8 && trans_width == 8)
263 off_t rem = st.st_size - ftello (f);
264 if (__builtin_expect (rem < 0
265 || (size_t) rem < (num_transitions * (8 + 1)
269 tzspec_len = (size_t) rem - (num_transitions * (8 + 1)
272 if (__builtin_expect (num_leaps > SIZE_MAX / 12
273 || tzspec_len < num_leaps * 12, 0))
275 tzspec_len -= num_leaps * 12;
276 if (__builtin_expect (tzspec_len < num_isstd, 0))
278 tzspec_len -= num_isstd;
279 if (__builtin_expect (tzspec_len == 0 || tzspec_len - 1 < num_isgmt, 0))
281 tzspec_len -= num_isgmt + 1;
282 if (__builtin_expect (SIZE_MAX - total_size < tzspec_len, 0))
285 if (__builtin_expect (SIZE_MAX - total_size - tzspec_len < extra, 0))
288 /* Allocate enough memory including the extra block requested by the
290 transitions = (time_t *) malloc (total_size + tzspec_len + extra);
291 if (transitions == NULL)
294 type_idxs = (unsigned char *) transitions + (num_transitions
296 types = (struct ttinfo *) ((char *) transitions + types_idx);
297 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
298 leaps = (struct leap *) ((char *) transitions + leaps_idx);
299 if (sizeof (time_t) == 8 && trans_width == 8)
300 tzspec = (char *) leaps + num_leaps * sizeof (struct leap) + extra;
304 *extrap = (char *) &leaps[num_leaps];
306 if (sizeof (time_t) == 4 || __builtin_expect (trans_width == 8, 1))
308 if (__builtin_expect (fread_unlocked (transitions, trans_width + 1,
310 != num_transitions, 0))
315 if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
316 != num_transitions, 0)
317 || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
318 f) != num_transitions, 0))
322 /* Check for bogus indices in the data file, so we can hereafter
323 safely use type_idxs[T] as indices into `types' and never crash. */
324 for (i = 0; i < num_transitions; ++i)
325 if (__builtin_expect (type_idxs[i] >= num_types, 0))
328 if ((BYTE_ORDER != BIG_ENDIAN && (sizeof (time_t) == 4 || trans_width == 4))
329 || (BYTE_ORDER == BIG_ENDIAN && sizeof (time_t) == 8
330 && trans_width == 4))
332 /* Decode the transition times, stored as 4-byte integers in
333 network (big-endian) byte order. We work from the end of
334 the array so as not to clobber the next element to be
335 processed when sizeof (time_t) > 4. */
338 transitions[i] = decode ((char *) transitions + i * 4);
340 else if (BYTE_ORDER != BIG_ENDIAN && sizeof (time_t) == 8)
342 /* Decode the transition times, stored as 8-byte integers in
343 network (big-endian) byte order. */
344 for (i = 0; i < num_transitions; ++i)
345 transitions[i] = decode64 ((char *) transitions + i * 8);
348 for (i = 0; i < num_types; ++i)
352 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
355 c = getc_unlocked (f);
356 if (__builtin_expect ((unsigned int) c > 1u, 0))
359 c = getc_unlocked (f);
360 if (__builtin_expect ((size_t) c > chars, 0))
361 /* Bogus index in data file. */
364 types[i].offset = (long int) decode (x);
367 if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
370 for (i = 0; i < num_leaps; ++i)
373 if (__builtin_expect (fread_unlocked (x, 1, trans_width, f)
376 if (sizeof (time_t) == 4 || trans_width == 4)
377 leaps[i].transition = (time_t) decode (x);
379 leaps[i].transition = (time_t) decode64 (x);
381 if (__builtin_expect (fread_unlocked (x, 1, 4, f) != 4, 0))
383 leaps[i].change = (long int) decode (x);
386 for (i = 0; i < num_isstd; ++i)
388 int c = getc_unlocked (f);
389 if (__builtin_expect (c == EOF, 0))
391 types[i].isstd = c != 0;
393 while (i < num_types)
394 types[i++].isstd = 0;
396 for (i = 0; i < num_isgmt; ++i)
398 int c = getc_unlocked (f);
399 if (__builtin_expect (c == EOF, 0))
401 types[i].isgmt = c != 0;
403 while (i < num_types)
404 types[i++].isgmt = 0;
406 /* Read the POSIX TZ-style information if possible. */
407 if (sizeof (time_t) == 8 && tzspec != NULL)
409 /* Skip over the newline first. */
410 if (getc_unlocked (f) != '\n'
411 || (fread_unlocked (tzspec, 1, tzspec_len - 1, f)
415 tzspec[tzspec_len - 1] = '\0';
417 else if (sizeof (time_t) == 4 && tzhead.tzh_version[0] != '\0')
419 /* Get the TZ string. */
420 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
422 || (memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic))
426 size_t num_transitions2 = (size_t) decode (tzhead.tzh_timecnt);
427 size_t num_types2 = (size_t) decode (tzhead.tzh_typecnt);
428 size_t chars2 = (size_t) decode (tzhead.tzh_charcnt);
429 size_t num_leaps2 = (size_t) decode (tzhead.tzh_leapcnt);
430 size_t num_isstd2 = (size_t) decode (tzhead.tzh_ttisstdcnt);
431 size_t num_isgmt2 = (size_t) decode (tzhead.tzh_ttisgmtcnt);
433 /* Position the stream before the second header. */
434 size_t to_skip = (num_transitions2 * (8 + 1)
441 if (fseek (f, to_skip, SEEK_CUR) != 0
442 || (off = ftello (f)) < 0
443 || st.st_size < off + 2)
446 tzspec_len = st.st_size - off - 1;
447 char *tzstr = alloca (tzspec_len);
448 if (getc_unlocked (f) != '\n'
449 || (fread_unlocked (tzstr, 1, tzspec_len - 1, f) != tzspec_len - 1))
451 tzstr[tzspec_len - 1] = '\0';
452 tzspec = __tzstring (tzstr);
455 /* Don't use an empty TZ string. */
456 if (tzspec != NULL && tzspec[0] == '\0')
461 /* First "register" all timezone names. */
462 for (i = 0; i < num_types; ++i)
463 (void) __tzstring (&zone_names[types[i].idx]);
465 /* Find the standard and daylight time offsets used by the rule file.
466 We choose the offsets in the types of each flavor that are
467 transitioned to earliest in time. */
470 for (i = num_transitions; i > 0; )
472 int type = type_idxs[--i];
473 int dst = types[type].isdst;
475 if (__tzname[dst] == NULL)
477 int idx = types[type].idx;
479 __tzname[dst] = __tzstring (&zone_names[idx]);
481 if (__tzname[1 - dst] != NULL)
485 if (__tzname[0] == NULL)
487 /* This should only happen if there are no transition rules.
488 In this case there should be only one single type. */
489 assert (num_types == 1);
490 __tzname[0] = __tzstring (zone_names);
492 if (__tzname[1] == NULL)
493 __tzname[1] = __tzname[0];
495 compute_tzname_max (chars);
497 if (num_transitions == 0)
498 /* Use the first rule (which should also be the only one). */
499 rule_stdoff = rule_dstoff = types[0].offset;
502 int stdoff_set = 0, dstoff_set = 0;
503 rule_stdoff = rule_dstoff = 0;
504 i = num_transitions - 1;
507 if (!stdoff_set && !types[type_idxs[i]].isdst)
510 rule_stdoff = types[type_idxs[i]].offset;
512 else if (!dstoff_set && types[type_idxs[i]].isdst)
515 rule_dstoff = types[type_idxs[i]].offset;
517 if (stdoff_set && dstoff_set)
523 rule_dstoff = rule_stdoff;
526 __daylight = rule_stdoff != rule_dstoff;
527 __timezone = -rule_stdoff;
534 ret_free_transitions:
535 free ((void *) transitions);
539 /* The user specified a hand-made timezone, but not its DST rules.
540 We will use the names and offsets from the user, and the rules
541 from the TZDEFRULES file. */
544 __tzfile_default (const char *std, const char *dst,
545 long int stdoff, long int dstoff)
547 size_t stdlen = strlen (std) + 1;
548 size_t dstlen = strlen (dst) + 1;
553 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
563 /* Ignore the zone names read from the file and use the given ones
565 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
568 /* Now there are only two zones, regardless of what the file contained. */
571 /* Now correct the transition times for the user-specified standard and
572 daylight offsets from GMT. */
574 for (i = 0; i < num_transitions; ++i)
576 struct ttinfo *trans_type = &types[type_idxs[i]];
578 /* We will use only types 0 (standard) and 1 (daylight).
579 Fix up this transition to point to whichever matches
580 the flavor of its original type. */
581 type_idxs[i] = trans_type->isdst;
583 if (trans_type->isgmt)
584 /* The transition time is in GMT. No correction to apply. */ ;
585 else if (isdst && !trans_type->isstd)
586 /* The type says this transition is in "local wall clock time", and
587 wall clock time as of the previous transition was DST. Correct
588 for the difference between the rule's DST offset and the user's
590 transitions[i] += dstoff - rule_dstoff;
592 /* This transition is in "local wall clock time", and wall clock
593 time as of this iteration is non-DST. Correct for the
594 difference between the rule's standard offset and the user's
596 transitions[i] += stdoff - rule_stdoff;
598 /* The DST state of "local wall clock time" for the next iteration is
599 as specified by this transition. */
600 isdst = trans_type->isdst;
603 /* Now that we adjusted the transitions to the requested offsets,
604 reset the rule_stdoff and rule_dstoff values appropriately. They
605 are used elsewhere. */
606 rule_stdoff = stdoff;
607 rule_dstoff = dstoff;
609 /* Reset types 0 and 1 to describe the user's settings. */
611 types[0].offset = stdoff;
613 types[1].idx = stdlen;
614 types[1].offset = dstoff;
617 /* Reset the zone names to point to the user's names. */
618 __tzname[0] = (char *) std;
619 __tzname[1] = (char *) dst;
621 /* Set the timezone. */
622 __timezone = -types[0].offset;
624 compute_tzname_max (stdlen + dstlen);
628 __tzfile_compute (time_t timer, int use_localtime,
629 long int *leap_correct, int *leap_hit,
639 if (__builtin_expect (num_transitions == 0 || timer < transitions[0], 0))
641 /* TIMER is before any transition (or there are no transitions).
642 Choose the first non-DST type
643 (or the first if they're all DST types). */
645 while (i < num_types && types[i].isdst)
647 if (__tzname[1] == NULL)
648 __tzname[1] = __tzstring (&zone_names[types[i].idx]);
655 __tzname[0] = __tzstring (&zone_names[types[i].idx]);
656 if (__tzname[1] == NULL)
659 while (j < num_types)
662 __tzname[1] = __tzstring (&zone_names[types[j].idx]);
669 else if (__builtin_expect (timer >= transitions[num_transitions - 1], 0))
671 if (__builtin_expect (tzspec == NULL, 0))
678 /* Parse the POSIX TZ-style string. */
679 __tzset_parse_tz (tzspec);
681 /* Convert to broken down structure. If this fails do not
683 if (__builtin_expect (! __offtime (&timer, 0, tp), 0))
686 /* Use the rules from the TZ string to compute the change. */
687 __tz_compute (timer, tp, 1);
689 /* If tzspec comes from posixrules loaded by __tzfile_default,
690 override the STD and DST zone names with the ones user
691 requested in TZ envvar. */
692 if (__builtin_expect (zone_names == (char *) &leaps[num_leaps], 0))
694 assert (num_types == 2);
695 __tzname[0] = __tzstring (zone_names);
696 __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]);
703 /* Find the first transition after TIMER, and
704 then pick the type of the transition before it. */
706 size_t hi = num_transitions - 1;
707 /* Assume that DST is changing twice a year and guess initial
709 Half of a gregorian year has on average 365.2425 * 86400 / 2
710 = 15778476 seconds. */
711 i = (transitions[num_transitions - 1] - timer) / 15778476;
712 if (i < num_transitions)
714 i = num_transitions - 1 - i;
715 if (timer < transitions[i])
717 if (i < 10 || timer >= transitions[i - 10])
720 while (timer < transitions[i - 1])
728 if (i + 10 >= num_transitions || timer < transitions[i + 10])
731 while (timer >= transitions[i])
740 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
744 if (timer < transitions[i])
752 /* assert (timer >= transitions[i - 1]
753 && (i == num_transitions || timer < transitions[i])); */
754 __tzname[types[type_idxs[i - 1]].isdst]
755 = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
757 while (j < num_transitions)
759 int type = type_idxs[j];
760 int dst = types[type].isdst;
761 int idx = types[type].idx;
763 if (__tzname[dst] == NULL)
765 __tzname[dst] = __tzstring (&zone_names[idx]);
767 if (__tzname[1 - dst] != NULL)
774 if (__builtin_expect (__tzname[0] == NULL, 0))
775 __tzname[0] = __tzname[1];
777 i = type_idxs[i - 1];
780 struct ttinfo *info = &types[i];
781 __daylight = rule_stdoff != rule_dstoff;
782 __timezone = -rule_stdoff;
784 if (__tzname[0] == NULL)
786 /* This should only happen if there are no transition rules.
787 In this case there should be only one single type. */
788 assert (num_types == 1);
789 __tzname[0] = __tzstring (zone_names);
791 if (__tzname[1] == NULL)
792 /* There is no daylight saving time. */
793 __tzname[1] = __tzname[0];
794 tp->tm_isdst = info->isdst;
795 assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
796 tp->tm_zone = __tzname[tp->tm_isdst];
797 tp->tm_gmtoff = info->offset;
804 /* Find the last leap second correction transition time before TIMER. */
809 while (timer < leaps[i].transition);
811 /* Apply its correction. */
812 *leap_correct = leaps[i].change;
814 if (timer == leaps[i].transition && /* Exactly at the transition time. */
815 ((i == 0 && leaps[i].change > 0) ||
816 leaps[i].change > leaps[i - 1].change))
820 && leaps[i].transition == leaps[i - 1].transition + 1
821 && leaps[i].change == leaps[i - 1].change + 1)
831 compute_tzname_max (size_t chars)
838 const char *start = p;
841 if ((size_t) (p - start) > __tzname_cur_max)
842 __tzname_cur_max = p - start;
844 while (++p < &zone_names[chars]);