1 /* Copyright (C) 1991-2014 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>
30 #include <timezone/tzfile.h>
33 static dev_t tzfile_dev
;
34 static ino64_t tzfile_ino
;
35 static time_t tzfile_mtime
;
39 long int offset
; /* Seconds east of GMT. */
40 unsigned char isdst
; /* Used to set tm_isdst. */
41 unsigned char idx
; /* Index into `zone_names'. */
42 unsigned char isstd
; /* Transition times are in standard time. */
43 unsigned char isgmt
; /* Transition times are in GMT. */
48 time_t transition
; /* Time the transition takes effect. */
49 long int change
; /* Seconds of correction to apply. */
52 static void compute_tzname_max (size_t) internal_function
;
54 static size_t num_transitions
;
55 libc_freeres_ptr (static time_t *transitions
);
56 static unsigned char *type_idxs
;
57 static size_t num_types
;
58 static struct ttinfo
*types
;
59 static char *zone_names
;
60 static long int rule_stdoff
;
61 static long int rule_dstoff
;
62 static size_t num_leaps
;
63 static struct leap
*leaps
;
69 /* Decode the four bytes at PTR as a signed integer in network byte order. */
71 __attribute ((always_inline
))
72 decode (const void *ptr
)
74 if (BYTE_ORDER
== BIG_ENDIAN
&& sizeof (int) == 4)
75 return *(const int *) ptr
;
76 if (sizeof (int) == 4)
77 return bswap_32 (*(const int *) ptr
);
79 const unsigned char *p
= ptr
;
80 int result
= *p
& (1 << (CHAR_BIT
- 1)) ? ~0 : 0;
82 result
= (result
<< 8) | *p
++;
83 result
= (result
<< 8) | *p
++;
84 result
= (result
<< 8) | *p
++;
85 result
= (result
<< 8) | *p
++;
92 __attribute ((always_inline
))
93 decode64 (const void *ptr
)
95 if ((BYTE_ORDER
== BIG_ENDIAN
))
96 return *(const int64_t *) ptr
;
98 return bswap_64 (*(const int64_t *) ptr
);
103 __tzfile_read (const char *file
, size_t extra
, char **extrap
)
105 static const char default_tzdir
[] = TZDIR
;
106 size_t num_isstd
, num_isgmt
;
108 struct tzhead tzhead
;
114 int was_using_tzfile
= __use_tzfile
;
119 if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
125 /* No user specification; use the site-wide default. */
127 else if (*file
== '\0')
128 /* User specified the empty string; use UTC with no leap seconds. */
129 goto ret_free_transitions
;
132 /* We must not allow to read an arbitrary file in a setuid
133 program. So we fail for any file which is not in the
134 directory hierachy starting at TZDIR
135 and which is not the system wide default TZDEFAULT. */
136 if (__libc_enable_secure
138 && memcmp (file
, TZDEFAULT
, sizeof TZDEFAULT
)
139 && memcmp (file
, default_tzdir
, sizeof (default_tzdir
) - 1))
140 || strstr (file
, "../") != NULL
))
141 /* This test is certainly a bit too restrictive but it should
142 catch all critical cases. */
143 goto ret_free_transitions
;
150 tzdir
= getenv ("TZDIR");
151 if (tzdir
== NULL
|| *tzdir
== '\0')
152 tzdir
= default_tzdir
;
153 if (__asprintf (&new, "%s/%s", tzdir
, file
) == -1)
154 goto ret_free_transitions
;
158 /* If we were already using tzfile, check whether the file changed. */
161 && stat64 (file
, &st
) == 0
162 && tzfile_ino
== st
.st_ino
&& tzfile_dev
== st
.st_dev
163 && tzfile_mtime
== st
.st_mtime
)
164 goto done
; /* Nothing to do. */
166 /* Note the file is opened with cancellation in the I/O functions
167 disabled and if available FD_CLOEXEC set. */
168 f
= fopen (file
, "rce");
170 goto ret_free_transitions
;
172 /* Get information about the file we are actually using. */
173 if (fstat64 (__fileno (f
), &st
) != 0)
176 goto ret_free_transitions
;
179 free ((void *) transitions
);
182 /* Remember the inode and device number and modification time. */
183 tzfile_dev
= st
.st_dev
;
184 tzfile_ino
= st
.st_ino
;
185 tzfile_mtime
= st
.st_mtime
;
187 /* No threads reading this stream. */
188 __fsetlocking (f
, FSETLOCKING_BYCALLER
);
191 if (__builtin_expect (__fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
193 || memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
)) != 0)
196 num_transitions
= (size_t) decode (tzhead
.tzh_timecnt
);
197 num_types
= (size_t) decode (tzhead
.tzh_typecnt
);
198 chars
= (size_t) decode (tzhead
.tzh_charcnt
);
199 num_leaps
= (size_t) decode (tzhead
.tzh_leapcnt
);
200 num_isstd
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
201 num_isgmt
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
203 /* For platforms with 64-bit time_t we use the new format if available. */
204 if (sizeof (time_t) == 8 && trans_width
== 4
205 && tzhead
.tzh_version
[0] != '\0')
207 /* We use the 8-byte format. */
210 /* Position the stream before the second header. */
211 size_t to_skip
= (num_transitions
* (4 + 1)
217 if (fseek (f
, to_skip
, SEEK_CUR
) != 0)
223 if (__builtin_expect (num_transitions
224 > ((SIZE_MAX
- (__alignof__ (struct ttinfo
) - 1))
225 / (sizeof (time_t) + 1)), 0))
227 total_size
= num_transitions
* (sizeof (time_t) + 1);
228 total_size
= ((total_size
+ __alignof__ (struct ttinfo
) - 1)
229 & ~(__alignof__ (struct ttinfo
) - 1));
230 types_idx
= total_size
;
231 if (__builtin_expect (num_types
232 > (SIZE_MAX
- total_size
) / sizeof (struct ttinfo
), 0))
234 total_size
+= num_types
* sizeof (struct ttinfo
);
235 if (__glibc_unlikely (chars
> SIZE_MAX
- total_size
))
238 if (__builtin_expect (__alignof__ (struct leap
) - 1
239 > SIZE_MAX
- total_size
, 0))
241 total_size
= ((total_size
+ __alignof__ (struct leap
) - 1)
242 & ~(__alignof__ (struct leap
) - 1));
243 leaps_idx
= total_size
;
244 if (__builtin_expect (num_leaps
245 > (SIZE_MAX
- total_size
) / sizeof (struct leap
), 0))
247 total_size
+= num_leaps
* sizeof (struct leap
);
249 if (sizeof (time_t) == 8 && trans_width
== 8)
251 off_t rem
= st
.st_size
- __ftello (f
);
252 if (__builtin_expect (rem
< 0
253 || (size_t) rem
< (num_transitions
* (8 + 1)
257 tzspec_len
= (size_t) rem
- (num_transitions
* (8 + 1)
260 if (__builtin_expect (num_leaps
> SIZE_MAX
/ 12
261 || tzspec_len
< num_leaps
* 12, 0))
263 tzspec_len
-= num_leaps
* 12;
264 if (__glibc_unlikely (tzspec_len
< num_isstd
))
266 tzspec_len
-= num_isstd
;
267 if (__glibc_unlikely (tzspec_len
== 0 || tzspec_len
- 1 < num_isgmt
))
269 tzspec_len
-= num_isgmt
+ 1;
270 if (__glibc_unlikely (SIZE_MAX
- total_size
< tzspec_len
))
273 if (__glibc_unlikely (SIZE_MAX
- total_size
- tzspec_len
< extra
))
276 /* Allocate enough memory including the extra block requested by the
278 transitions
= (time_t *) malloc (total_size
+ tzspec_len
+ extra
);
279 if (transitions
== NULL
)
282 type_idxs
= (unsigned char *) transitions
+ (num_transitions
284 types
= (struct ttinfo
*) ((char *) transitions
+ types_idx
);
285 zone_names
= (char *) types
+ num_types
* sizeof (struct ttinfo
);
286 leaps
= (struct leap
*) ((char *) transitions
+ leaps_idx
);
287 if (sizeof (time_t) == 8 && trans_width
== 8)
288 tzspec
= (char *) leaps
+ num_leaps
* sizeof (struct leap
) + extra
;
292 *extrap
= (char *) &leaps
[num_leaps
];
294 if (sizeof (time_t) == 4 || __builtin_expect (trans_width
== 8, 1))
296 if (__builtin_expect (__fread_unlocked (transitions
, trans_width
+ 1,
298 != num_transitions
, 0))
303 if (__builtin_expect (__fread_unlocked (transitions
, 4,
305 != num_transitions
, 0)
306 || __builtin_expect (__fread_unlocked (type_idxs
, 1, num_transitions
,
307 f
) != num_transitions
, 0))
311 /* Check for bogus indices in the data file, so we can hereafter
312 safely use type_idxs[T] as indices into `types' and never crash. */
313 for (i
= 0; i
< num_transitions
; ++i
)
314 if (__glibc_unlikely (type_idxs
[i
] >= num_types
))
317 if ((BYTE_ORDER
!= BIG_ENDIAN
&& (sizeof (time_t) == 4 || trans_width
== 4))
318 || (BYTE_ORDER
== BIG_ENDIAN
&& sizeof (time_t) == 8
319 && trans_width
== 4))
321 /* Decode the transition times, stored as 4-byte integers in
322 network (big-endian) byte order. We work from the end of
323 the array so as not to clobber the next element to be
324 processed when sizeof (time_t) > 4. */
327 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
329 else if (BYTE_ORDER
!= BIG_ENDIAN
&& sizeof (time_t) == 8)
331 /* Decode the transition times, stored as 8-byte integers in
332 network (big-endian) byte order. */
333 for (i
= 0; i
< num_transitions
; ++i
)
334 transitions
[i
] = decode64 ((char *) transitions
+ i
* 8);
337 for (i
= 0; i
< num_types
; ++i
)
341 if (__builtin_expect (__fread_unlocked (x
, 1,
342 sizeof (x
), f
) != sizeof (x
),
345 c
= getc_unlocked (f
);
346 if (__glibc_unlikely ((unsigned int) c
> 1u))
349 c
= getc_unlocked (f
);
350 if (__glibc_unlikely ((size_t) c
> chars
))
351 /* Bogus index in data file. */
354 types
[i
].offset
= (long int) decode (x
);
357 if (__glibc_unlikely (__fread_unlocked (zone_names
, 1, chars
, f
) != chars
))
360 for (i
= 0; i
< num_leaps
; ++i
)
363 if (__builtin_expect (__fread_unlocked (x
, 1, trans_width
, f
)
366 if (sizeof (time_t) == 4 || trans_width
== 4)
367 leaps
[i
].transition
= (time_t) decode (x
);
369 leaps
[i
].transition
= (time_t) decode64 (x
);
371 if (__glibc_unlikely (__fread_unlocked (x
, 1, 4, f
) != 4))
373 leaps
[i
].change
= (long int) decode (x
);
376 for (i
= 0; i
< num_isstd
; ++i
)
378 int c
= getc_unlocked (f
);
379 if (__glibc_unlikely (c
== EOF
))
381 types
[i
].isstd
= c
!= 0;
383 while (i
< num_types
)
384 types
[i
++].isstd
= 0;
386 for (i
= 0; i
< num_isgmt
; ++i
)
388 int c
= getc_unlocked (f
);
389 if (__glibc_unlikely (c
== EOF
))
391 types
[i
].isgmt
= c
!= 0;
393 while (i
< num_types
)
394 types
[i
++].isgmt
= 0;
396 /* Read the POSIX TZ-style information if possible. */
397 if (sizeof (time_t) == 8 && tzspec
!= NULL
)
399 /* Skip over the newline first. */
400 if (getc_unlocked (f
) != '\n'
401 || (__fread_unlocked (tzspec
, 1, tzspec_len
- 1, f
)
405 tzspec
[tzspec_len
- 1] = '\0';
407 else if (sizeof (time_t) == 4 && tzhead
.tzh_version
[0] != '\0')
409 /* Get the TZ string. */
410 if (__builtin_expect (__fread_unlocked ((void *) &tzhead
,
411 sizeof (tzhead
), 1, f
) != 1, 0)
412 || (memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
))
416 size_t num_transitions2
= (size_t) decode (tzhead
.tzh_timecnt
);
417 size_t num_types2
= (size_t) decode (tzhead
.tzh_typecnt
);
418 size_t chars2
= (size_t) decode (tzhead
.tzh_charcnt
);
419 size_t num_leaps2
= (size_t) decode (tzhead
.tzh_leapcnt
);
420 size_t num_isstd2
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
421 size_t num_isgmt2
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
423 /* Position the stream before the second header. */
424 size_t to_skip
= (num_transitions2
* (8 + 1)
431 if (fseek (f
, to_skip
, SEEK_CUR
) != 0
432 || (off
= __ftello (f
)) < 0
433 || st
.st_size
< off
+ 2)
436 tzspec_len
= st
.st_size
- off
- 1;
437 char *tzstr
= alloca (tzspec_len
);
438 if (getc_unlocked (f
) != '\n'
439 || (__fread_unlocked (tzstr
, 1, tzspec_len
- 1, f
)
442 tzstr
[tzspec_len
- 1] = '\0';
443 tzspec
= __tzstring (tzstr
);
446 /* Don't use an empty TZ string. */
447 if (tzspec
!= NULL
&& tzspec
[0] == '\0')
452 /* First "register" all timezone names. */
453 for (i
= 0; i
< num_types
; ++i
)
454 (void) __tzstring (&zone_names
[types
[i
].idx
]);
456 /* Find the standard and daylight time offsets used by the rule file.
457 We choose the offsets in the types of each flavor that are
458 transitioned to earliest in time. */
461 for (i
= num_transitions
; i
> 0; )
463 int type
= type_idxs
[--i
];
464 int dst
= types
[type
].isdst
;
466 if (__tzname
[dst
] == NULL
)
468 int idx
= types
[type
].idx
;
470 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
472 if (__tzname
[1 - dst
] != NULL
)
476 if (__tzname
[0] == NULL
)
478 /* This should only happen if there are no transition rules.
479 In this case there should be only one single type. */
480 assert (num_types
== 1);
481 __tzname
[0] = __tzstring (zone_names
);
483 if (__tzname
[1] == NULL
)
484 __tzname
[1] = __tzname
[0];
486 compute_tzname_max (chars
);
488 if (num_transitions
== 0)
489 /* Use the first rule (which should also be the only one). */
490 rule_stdoff
= rule_dstoff
= types
[0].offset
;
493 int stdoff_set
= 0, dstoff_set
= 0;
494 rule_stdoff
= rule_dstoff
= 0;
495 i
= num_transitions
- 1;
498 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
501 rule_stdoff
= types
[type_idxs
[i
]].offset
;
503 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
506 rule_dstoff
= types
[type_idxs
[i
]].offset
;
508 if (stdoff_set
&& dstoff_set
)
514 rule_dstoff
= rule_stdoff
;
517 __daylight
= rule_stdoff
!= rule_dstoff
;
518 __timezone
= -rule_stdoff
;
527 ret_free_transitions
:
529 free ((void *) transitions
);
533 /* The user specified a hand-made timezone, but not its DST rules.
534 We will use the names and offsets from the user, and the rules
535 from the TZDEFRULES file. */
538 __tzfile_default (const char *std
, const char *dst
,
539 long int stdoff
, long int dstoff
)
541 size_t stdlen
= strlen (std
) + 1;
542 size_t dstlen
= strlen (dst
) + 1;
547 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
557 /* Ignore the zone names read from the file and use the given ones
559 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
562 /* Now there are only two zones, regardless of what the file contained. */
565 /* Now correct the transition times for the user-specified standard and
566 daylight offsets from GMT. */
568 for (i
= 0; i
< num_transitions
; ++i
)
570 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
572 /* We will use only types 0 (standard) and 1 (daylight).
573 Fix up this transition to point to whichever matches
574 the flavor of its original type. */
575 type_idxs
[i
] = trans_type
->isdst
;
577 if (trans_type
->isgmt
)
578 /* The transition time is in GMT. No correction to apply. */ ;
579 else if (isdst
&& !trans_type
->isstd
)
580 /* The type says this transition is in "local wall clock time", and
581 wall clock time as of the previous transition was DST. Correct
582 for the difference between the rule's DST offset and the user's
584 transitions
[i
] += dstoff
- rule_dstoff
;
586 /* This transition is in "local wall clock time", and wall clock
587 time as of this iteration is non-DST. Correct for the
588 difference between the rule's standard offset and the user's
590 transitions
[i
] += stdoff
- rule_stdoff
;
592 /* The DST state of "local wall clock time" for the next iteration is
593 as specified by this transition. */
594 isdst
= trans_type
->isdst
;
597 /* Now that we adjusted the transitions to the requested offsets,
598 reset the rule_stdoff and rule_dstoff values appropriately. They
599 are used elsewhere. */
600 rule_stdoff
= stdoff
;
601 rule_dstoff
= dstoff
;
603 /* Reset types 0 and 1 to describe the user's settings. */
605 types
[0].offset
= stdoff
;
607 types
[1].idx
= stdlen
;
608 types
[1].offset
= dstoff
;
611 /* Reset the zone names to point to the user's names. */
612 __tzname
[0] = (char *) std
;
613 __tzname
[1] = (char *) dst
;
615 /* Set the timezone. */
616 __timezone
= -types
[0].offset
;
618 compute_tzname_max (stdlen
+ dstlen
);
622 __tzfile_compute (time_t timer
, int use_localtime
,
623 long int *leap_correct
, int *leap_hit
,
633 if (__glibc_unlikely (num_transitions
== 0 || timer
< transitions
[0]))
635 /* TIMER is before any transition (or there are no transitions).
636 Choose the first non-DST type
637 (or the first if they're all DST types). */
639 while (i
< num_types
&& types
[i
].isdst
)
641 if (__tzname
[1] == NULL
)
642 __tzname
[1] = __tzstring (&zone_names
[types
[i
].idx
]);
649 __tzname
[0] = __tzstring (&zone_names
[types
[i
].idx
]);
650 if (__tzname
[1] == NULL
)
653 while (j
< num_types
)
656 __tzname
[1] = __tzstring (&zone_names
[types
[j
].idx
]);
663 else if (__glibc_unlikely (timer
>= transitions
[num_transitions
- 1]))
665 if (__glibc_unlikely (tzspec
== NULL
))
672 /* Parse the POSIX TZ-style string. */
673 __tzset_parse_tz (tzspec
);
675 /* Convert to broken down structure. If this fails do not
677 if (__glibc_unlikely (! __offtime (&timer
, 0, tp
)))
680 /* Use the rules from the TZ string to compute the change. */
681 __tz_compute (timer
, tp
, 1);
683 /* If tzspec comes from posixrules loaded by __tzfile_default,
684 override the STD and DST zone names with the ones user
685 requested in TZ envvar. */
686 if (__glibc_unlikely (zone_names
== (char *) &leaps
[num_leaps
]))
688 assert (num_types
== 2);
689 __tzname
[0] = __tzstring (zone_names
);
690 __tzname
[1] = __tzstring (&zone_names
[strlen (zone_names
) + 1]);
697 /* Find the first transition after TIMER, and
698 then pick the type of the transition before it. */
700 size_t hi
= num_transitions
- 1;
701 /* Assume that DST is changing twice a year and guess initial
703 Half of a gregorian year has on average 365.2425 * 86400 / 2
704 = 15778476 seconds. */
705 i
= (transitions
[num_transitions
- 1] - timer
) / 15778476;
706 if (i
< num_transitions
)
708 i
= num_transitions
- 1 - i
;
709 if (timer
< transitions
[i
])
711 if (i
< 10 || timer
>= transitions
[i
- 10])
714 while (timer
< transitions
[i
- 1])
722 if (i
+ 10 >= num_transitions
|| timer
< transitions
[i
+ 10])
725 while (timer
>= transitions
[i
])
734 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
738 if (timer
< transitions
[i
])
746 /* assert (timer >= transitions[i - 1]
747 && (i == num_transitions || timer < transitions[i])); */
748 __tzname
[types
[type_idxs
[i
- 1]].isdst
]
749 = __tzstring (&zone_names
[types
[type_idxs
[i
- 1]].idx
]);
751 while (j
< num_transitions
)
753 int type
= type_idxs
[j
];
754 int dst
= types
[type
].isdst
;
755 int idx
= types
[type
].idx
;
757 if (__tzname
[dst
] == NULL
)
759 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
761 if (__tzname
[1 - dst
] != NULL
)
768 if (__glibc_unlikely (__tzname
[0] == NULL
))
769 __tzname
[0] = __tzname
[1];
771 i
= type_idxs
[i
- 1];
774 struct ttinfo
*info
= &types
[i
];
775 __daylight
= rule_stdoff
!= rule_dstoff
;
776 __timezone
= -rule_stdoff
;
778 if (__tzname
[0] == NULL
)
780 /* This should only happen if there are no transition rules.
781 In this case there should be only one single type. */
782 assert (num_types
== 1);
783 __tzname
[0] = __tzstring (zone_names
);
785 if (__tzname
[1] == NULL
)
786 /* There is no daylight saving time. */
787 __tzname
[1] = __tzname
[0];
788 tp
->tm_isdst
= info
->isdst
;
789 assert (strcmp (&zone_names
[info
->idx
], __tzname
[tp
->tm_isdst
]) == 0);
790 tp
->tm_zone
= __tzname
[tp
->tm_isdst
];
791 tp
->tm_gmtoff
= info
->offset
;
798 /* Find the last leap second correction transition time before TIMER. */
803 while (timer
< leaps
[i
].transition
);
805 /* Apply its correction. */
806 *leap_correct
= leaps
[i
].change
;
808 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
809 ((i
== 0 && leaps
[i
].change
> 0) ||
810 leaps
[i
].change
> leaps
[i
- 1].change
))
814 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
815 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
825 compute_tzname_max (size_t chars
)
832 const char *start
= p
;
835 if ((size_t) (p
- start
) > __tzname_cur_max
)
836 __tzname_cur_max
= p
- start
;
838 while (++p
< &zone_names
[chars
]);