1 /* Copyright (C) 1991-2015 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 if (__glibc_unlikely (num_isstd
> num_types
|| num_isgmt
> num_types
))
206 /* For platforms with 64-bit time_t we use the new format if available. */
207 if (sizeof (time_t) == 8 && trans_width
== 4
208 && tzhead
.tzh_version
[0] != '\0')
210 /* We use the 8-byte format. */
213 /* Position the stream before the second header. */
214 size_t to_skip
= (num_transitions
* (4 + 1)
220 if (fseek (f
, to_skip
, SEEK_CUR
) != 0)
226 if (__builtin_expect (num_transitions
227 > ((SIZE_MAX
- (__alignof__ (struct ttinfo
) - 1))
228 / (sizeof (time_t) + 1)), 0))
230 total_size
= num_transitions
* (sizeof (time_t) + 1);
231 total_size
= ((total_size
+ __alignof__ (struct ttinfo
) - 1)
232 & ~(__alignof__ (struct ttinfo
) - 1));
233 types_idx
= total_size
;
234 if (__builtin_expect (num_types
235 > (SIZE_MAX
- total_size
) / sizeof (struct ttinfo
), 0))
237 total_size
+= num_types
* sizeof (struct ttinfo
);
238 if (__glibc_unlikely (chars
> SIZE_MAX
- total_size
))
241 if (__builtin_expect (__alignof__ (struct leap
) - 1
242 > SIZE_MAX
- total_size
, 0))
244 total_size
= ((total_size
+ __alignof__ (struct leap
) - 1)
245 & ~(__alignof__ (struct leap
) - 1));
246 leaps_idx
= total_size
;
247 if (__builtin_expect (num_leaps
248 > (SIZE_MAX
- total_size
) / sizeof (struct leap
), 0))
250 total_size
+= num_leaps
* sizeof (struct leap
);
252 if (sizeof (time_t) == 8 && trans_width
== 8)
254 off_t rem
= st
.st_size
- __ftello (f
);
255 if (__builtin_expect (rem
< 0
256 || (size_t) rem
< (num_transitions
* (8 + 1)
260 tzspec_len
= (size_t) rem
- (num_transitions
* (8 + 1)
263 if (__builtin_expect (num_leaps
> SIZE_MAX
/ 12
264 || tzspec_len
< num_leaps
* 12, 0))
266 tzspec_len
-= num_leaps
* 12;
267 if (__glibc_unlikely (tzspec_len
< num_isstd
))
269 tzspec_len
-= num_isstd
;
270 if (__glibc_unlikely (tzspec_len
== 0 || tzspec_len
- 1 < num_isgmt
))
272 tzspec_len
-= num_isgmt
+ 1;
273 if (__glibc_unlikely (SIZE_MAX
- total_size
< tzspec_len
))
276 if (__glibc_unlikely (SIZE_MAX
- total_size
- tzspec_len
< extra
))
279 /* Allocate enough memory including the extra block requested by the
281 transitions
= (time_t *) malloc (total_size
+ tzspec_len
+ extra
);
282 if (transitions
== NULL
)
285 type_idxs
= (unsigned char *) transitions
+ (num_transitions
287 types
= (struct ttinfo
*) ((char *) transitions
+ types_idx
);
288 zone_names
= (char *) types
+ num_types
* sizeof (struct ttinfo
);
289 leaps
= (struct leap
*) ((char *) transitions
+ leaps_idx
);
290 if (sizeof (time_t) == 8 && trans_width
== 8)
291 tzspec
= (char *) leaps
+ num_leaps
* sizeof (struct leap
) + extra
;
295 *extrap
= (char *) &leaps
[num_leaps
];
297 if (sizeof (time_t) == 4 || __builtin_expect (trans_width
== 8, 1))
299 if (__builtin_expect (__fread_unlocked (transitions
, trans_width
+ 1,
301 != num_transitions
, 0))
306 if (__builtin_expect (__fread_unlocked (transitions
, 4,
308 != num_transitions
, 0)
309 || __builtin_expect (__fread_unlocked (type_idxs
, 1, num_transitions
,
310 f
) != num_transitions
, 0))
314 /* Check for bogus indices in the data file, so we can hereafter
315 safely use type_idxs[T] as indices into `types' and never crash. */
316 for (i
= 0; i
< num_transitions
; ++i
)
317 if (__glibc_unlikely (type_idxs
[i
] >= num_types
))
320 if ((BYTE_ORDER
!= BIG_ENDIAN
&& (sizeof (time_t) == 4 || trans_width
== 4))
321 || (BYTE_ORDER
== BIG_ENDIAN
&& sizeof (time_t) == 8
322 && trans_width
== 4))
324 /* Decode the transition times, stored as 4-byte integers in
325 network (big-endian) byte order. We work from the end of
326 the array so as not to clobber the next element to be
327 processed when sizeof (time_t) > 4. */
330 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
332 else if (BYTE_ORDER
!= BIG_ENDIAN
&& sizeof (time_t) == 8)
334 /* Decode the transition times, stored as 8-byte integers in
335 network (big-endian) byte order. */
336 for (i
= 0; i
< num_transitions
; ++i
)
337 transitions
[i
] = decode64 ((char *) transitions
+ i
* 8);
340 for (i
= 0; i
< num_types
; ++i
)
344 if (__builtin_expect (__fread_unlocked (x
, 1,
345 sizeof (x
), f
) != sizeof (x
),
348 c
= getc_unlocked (f
);
349 if (__glibc_unlikely ((unsigned int) c
> 1u))
352 c
= getc_unlocked (f
);
353 if (__glibc_unlikely ((size_t) c
> chars
))
354 /* Bogus index in data file. */
357 types
[i
].offset
= (long int) decode (x
);
360 if (__glibc_unlikely (__fread_unlocked (zone_names
, 1, chars
, f
) != chars
))
363 for (i
= 0; i
< num_leaps
; ++i
)
366 if (__builtin_expect (__fread_unlocked (x
, 1, trans_width
, f
)
369 if (sizeof (time_t) == 4 || trans_width
== 4)
370 leaps
[i
].transition
= (time_t) decode (x
);
372 leaps
[i
].transition
= (time_t) decode64 (x
);
374 if (__glibc_unlikely (__fread_unlocked (x
, 1, 4, f
) != 4))
376 leaps
[i
].change
= (long int) decode (x
);
379 for (i
= 0; i
< num_isstd
; ++i
)
381 int c
= getc_unlocked (f
);
382 if (__glibc_unlikely (c
== EOF
))
384 types
[i
].isstd
= c
!= 0;
386 while (i
< num_types
)
387 types
[i
++].isstd
= 0;
389 for (i
= 0; i
< num_isgmt
; ++i
)
391 int c
= getc_unlocked (f
);
392 if (__glibc_unlikely (c
== EOF
))
394 types
[i
].isgmt
= c
!= 0;
396 while (i
< num_types
)
397 types
[i
++].isgmt
= 0;
399 /* Read the POSIX TZ-style information if possible. */
400 if (sizeof (time_t) == 8 && tzspec
!= NULL
)
402 /* Skip over the newline first. */
403 if (getc_unlocked (f
) != '\n'
404 || (__fread_unlocked (tzspec
, 1, tzspec_len
- 1, f
)
408 tzspec
[tzspec_len
- 1] = '\0';
410 else if (sizeof (time_t) == 4 && tzhead
.tzh_version
[0] != '\0')
412 /* Get the TZ string. */
413 if (__builtin_expect (__fread_unlocked ((void *) &tzhead
,
414 sizeof (tzhead
), 1, f
) != 1, 0)
415 || (memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
))
419 size_t num_transitions2
= (size_t) decode (tzhead
.tzh_timecnt
);
420 size_t num_types2
= (size_t) decode (tzhead
.tzh_typecnt
);
421 size_t chars2
= (size_t) decode (tzhead
.tzh_charcnt
);
422 size_t num_leaps2
= (size_t) decode (tzhead
.tzh_leapcnt
);
423 size_t num_isstd2
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
424 size_t num_isgmt2
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
426 /* Position the stream before the second header. */
427 size_t to_skip
= (num_transitions2
* (8 + 1)
434 if (fseek (f
, to_skip
, SEEK_CUR
) != 0
435 || (off
= __ftello (f
)) < 0
436 || st
.st_size
< off
+ 2)
439 tzspec_len
= st
.st_size
- off
- 1;
442 char *tzstr
= malloc (tzspec_len
);
445 if (getc_unlocked (f
) != '\n'
446 || (__fread_unlocked (tzstr
, 1, tzspec_len
- 1, f
)
452 tzstr
[tzspec_len
- 1] = '\0';
453 tzspec
= __tzstring (tzstr
);
457 /* Don't use an empty TZ string. */
458 if (tzspec
!= NULL
&& tzspec
[0] == '\0')
463 /* First "register" all timezone names. */
464 for (i
= 0; i
< num_types
; ++i
)
465 (void) __tzstring (&zone_names
[types
[i
].idx
]);
467 /* Find the standard and daylight time offsets used by the rule file.
468 We choose the offsets in the types of each flavor that are
469 transitioned to earliest in time. */
472 for (i
= num_transitions
; i
> 0; )
474 int type
= type_idxs
[--i
];
475 int dst
= types
[type
].isdst
;
477 if (__tzname
[dst
] == NULL
)
479 int idx
= types
[type
].idx
;
481 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
483 if (__tzname
[1 - dst
] != NULL
)
487 if (__tzname
[0] == NULL
)
489 /* This should only happen if there are no transition rules.
490 In this case there should be only one single type. */
491 assert (num_types
== 1);
492 __tzname
[0] = __tzstring (zone_names
);
494 if (__tzname
[1] == NULL
)
495 __tzname
[1] = __tzname
[0];
497 compute_tzname_max (chars
);
499 if (num_transitions
== 0)
500 /* Use the first rule (which should also be the only one). */
501 rule_stdoff
= rule_dstoff
= types
[0].offset
;
504 int stdoff_set
= 0, dstoff_set
= 0;
505 rule_stdoff
= rule_dstoff
= 0;
506 i
= num_transitions
- 1;
509 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
512 rule_stdoff
= types
[type_idxs
[i
]].offset
;
514 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
517 rule_dstoff
= types
[type_idxs
[i
]].offset
;
519 if (stdoff_set
&& dstoff_set
)
525 rule_dstoff
= rule_stdoff
;
528 __daylight
= rule_stdoff
!= rule_dstoff
;
529 __timezone
= -rule_stdoff
;
538 ret_free_transitions
:
540 free ((void *) transitions
);
544 /* The user specified a hand-made timezone, but not its DST rules.
545 We will use the names and offsets from the user, and the rules
546 from the TZDEFRULES file. */
549 __tzfile_default (const char *std
, const char *dst
,
550 long int stdoff
, long int dstoff
)
552 size_t stdlen
= strlen (std
) + 1;
553 size_t dstlen
= strlen (dst
) + 1;
558 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
568 /* Ignore the zone names read from the file and use the given ones
570 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
573 /* Now there are only two zones, regardless of what the file contained. */
576 /* Now correct the transition times for the user-specified standard and
577 daylight offsets from GMT. */
579 for (i
= 0; i
< num_transitions
; ++i
)
581 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
583 /* We will use only types 0 (standard) and 1 (daylight).
584 Fix up this transition to point to whichever matches
585 the flavor of its original type. */
586 type_idxs
[i
] = trans_type
->isdst
;
588 if (trans_type
->isgmt
)
589 /* The transition time is in GMT. No correction to apply. */ ;
590 else if (isdst
&& !trans_type
->isstd
)
591 /* The type says this transition is in "local wall clock time", and
592 wall clock time as of the previous transition was DST. Correct
593 for the difference between the rule's DST offset and the user's
595 transitions
[i
] += dstoff
- rule_dstoff
;
597 /* This transition is in "local wall clock time", and wall clock
598 time as of this iteration is non-DST. Correct for the
599 difference between the rule's standard offset and the user's
601 transitions
[i
] += stdoff
- rule_stdoff
;
603 /* The DST state of "local wall clock time" for the next iteration is
604 as specified by this transition. */
605 isdst
= trans_type
->isdst
;
608 /* Now that we adjusted the transitions to the requested offsets,
609 reset the rule_stdoff and rule_dstoff values appropriately. They
610 are used elsewhere. */
611 rule_stdoff
= stdoff
;
612 rule_dstoff
= dstoff
;
614 /* Reset types 0 and 1 to describe the user's settings. */
616 types
[0].offset
= stdoff
;
618 types
[1].idx
= stdlen
;
619 types
[1].offset
= dstoff
;
622 /* Reset the zone names to point to the user's names. */
623 __tzname
[0] = (char *) std
;
624 __tzname
[1] = (char *) dst
;
626 /* Set the timezone. */
627 __timezone
= -types
[0].offset
;
629 compute_tzname_max (stdlen
+ dstlen
);
633 __tzfile_compute (time_t timer
, int use_localtime
,
634 long int *leap_correct
, int *leap_hit
,
644 if (__glibc_unlikely (num_transitions
== 0 || timer
< transitions
[0]))
646 /* TIMER is before any transition (or there are no transitions).
647 Choose the first non-DST type
648 (or the first if they're all DST types). */
650 while (i
< num_types
&& types
[i
].isdst
)
652 if (__tzname
[1] == NULL
)
653 __tzname
[1] = __tzstring (&zone_names
[types
[i
].idx
]);
660 __tzname
[0] = __tzstring (&zone_names
[types
[i
].idx
]);
661 if (__tzname
[1] == NULL
)
664 while (j
< num_types
)
667 __tzname
[1] = __tzstring (&zone_names
[types
[j
].idx
]);
674 else if (__glibc_unlikely (timer
>= transitions
[num_transitions
- 1]))
676 if (__glibc_unlikely (tzspec
== NULL
))
683 /* Parse the POSIX TZ-style string. */
684 __tzset_parse_tz (tzspec
);
686 /* Convert to broken down structure. If this fails do not
688 if (__glibc_unlikely (! __offtime (&timer
, 0, tp
)))
691 /* Use the rules from the TZ string to compute the change. */
692 __tz_compute (timer
, tp
, 1);
694 /* If tzspec comes from posixrules loaded by __tzfile_default,
695 override the STD and DST zone names with the ones user
696 requested in TZ envvar. */
697 if (__glibc_unlikely (zone_names
== (char *) &leaps
[num_leaps
]))
699 assert (num_types
== 2);
700 __tzname
[0] = __tzstring (zone_names
);
701 __tzname
[1] = __tzstring (&zone_names
[strlen (zone_names
) + 1]);
708 /* Find the first transition after TIMER, and
709 then pick the type of the transition before it. */
711 size_t hi
= num_transitions
- 1;
712 /* Assume that DST is changing twice a year and guess initial
714 Half of a gregorian year has on average 365.2425 * 86400 / 2
715 = 15778476 seconds. */
716 i
= (transitions
[num_transitions
- 1] - timer
) / 15778476;
717 if (i
< num_transitions
)
719 i
= num_transitions
- 1 - i
;
720 if (timer
< transitions
[i
])
722 if (i
< 10 || timer
>= transitions
[i
- 10])
725 while (timer
< transitions
[i
- 1])
733 if (i
+ 10 >= num_transitions
|| timer
< transitions
[i
+ 10])
736 while (timer
>= transitions
[i
])
745 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
749 if (timer
< transitions
[i
])
757 /* assert (timer >= transitions[i - 1]
758 && (i == num_transitions || timer < transitions[i])); */
759 __tzname
[types
[type_idxs
[i
- 1]].isdst
]
760 = __tzstring (&zone_names
[types
[type_idxs
[i
- 1]].idx
]);
762 while (j
< num_transitions
)
764 int type
= type_idxs
[j
];
765 int dst
= types
[type
].isdst
;
766 int idx
= types
[type
].idx
;
768 if (__tzname
[dst
] == NULL
)
770 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
772 if (__tzname
[1 - dst
] != NULL
)
779 if (__glibc_unlikely (__tzname
[0] == NULL
))
780 __tzname
[0] = __tzname
[1];
782 i
= type_idxs
[i
- 1];
785 struct ttinfo
*info
= &types
[i
];
786 __daylight
= rule_stdoff
!= rule_dstoff
;
787 __timezone
= -rule_stdoff
;
789 if (__tzname
[0] == NULL
)
791 /* This should only happen if there are no transition rules.
792 In this case there should be only one single type. */
793 assert (num_types
== 1);
794 __tzname
[0] = __tzstring (zone_names
);
796 if (__tzname
[1] == NULL
)
797 /* There is no daylight saving time. */
798 __tzname
[1] = __tzname
[0];
799 tp
->tm_isdst
= info
->isdst
;
800 assert (strcmp (&zone_names
[info
->idx
], __tzname
[tp
->tm_isdst
]) == 0);
801 tp
->tm_zone
= __tzname
[tp
->tm_isdst
];
802 tp
->tm_gmtoff
= info
->offset
;
809 /* Find the last leap second correction transition time before TIMER. */
814 while (timer
< leaps
[i
].transition
);
816 /* Apply its correction. */
817 *leap_correct
= leaps
[i
].change
;
819 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
820 ((i
== 0 && leaps
[i
].change
> 0) ||
821 leaps
[i
].change
> leaps
[i
- 1].change
))
825 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
826 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
836 compute_tzname_max (size_t chars
)
843 const char *start
= p
;
846 if ((size_t) (p
- start
) > __tzname_cur_max
)
847 __tzname_cur_max
= p
- start
;
849 while (++p
< &zone_names
[chars
]);