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>
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 (__builtin_expect (chars
> SIZE_MAX
- total_size
, 0))
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 (__builtin_expect (tzspec_len
< num_isstd
, 0))
266 tzspec_len
-= num_isstd
;
267 if (__builtin_expect (tzspec_len
== 0 || tzspec_len
- 1 < num_isgmt
, 0))
269 tzspec_len
-= num_isgmt
+ 1;
270 if (__builtin_expect (SIZE_MAX
- total_size
< tzspec_len
, 0))
273 if (__builtin_expect (SIZE_MAX
- total_size
- tzspec_len
< extra
, 0))
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, num_transitions
, f
)
304 != num_transitions
, 0)
305 || __builtin_expect (fread_unlocked (type_idxs
, 1, num_transitions
,
306 f
) != num_transitions
, 0))
310 /* Check for bogus indices in the data file, so we can hereafter
311 safely use type_idxs[T] as indices into `types' and never crash. */
312 for (i
= 0; i
< num_transitions
; ++i
)
313 if (__builtin_expect (type_idxs
[i
] >= num_types
, 0))
316 if ((BYTE_ORDER
!= BIG_ENDIAN
&& (sizeof (time_t) == 4 || trans_width
== 4))
317 || (BYTE_ORDER
== BIG_ENDIAN
&& sizeof (time_t) == 8
318 && trans_width
== 4))
320 /* Decode the transition times, stored as 4-byte integers in
321 network (big-endian) byte order. We work from the end of
322 the array so as not to clobber the next element to be
323 processed when sizeof (time_t) > 4. */
326 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
328 else if (BYTE_ORDER
!= BIG_ENDIAN
&& sizeof (time_t) == 8)
330 /* Decode the transition times, stored as 8-byte integers in
331 network (big-endian) byte order. */
332 for (i
= 0; i
< num_transitions
; ++i
)
333 transitions
[i
] = decode64 ((char *) transitions
+ i
* 8);
336 for (i
= 0; i
< num_types
; ++i
)
340 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
343 c
= getc_unlocked (f
);
344 if (__builtin_expect ((unsigned int) c
> 1u, 0))
347 c
= getc_unlocked (f
);
348 if (__builtin_expect ((size_t) c
> chars
, 0))
349 /* Bogus index in data file. */
352 types
[i
].offset
= (long int) decode (x
);
355 if (__builtin_expect (fread_unlocked (zone_names
, 1, chars
, f
) != chars
, 0))
358 for (i
= 0; i
< num_leaps
; ++i
)
361 if (__builtin_expect (fread_unlocked (x
, 1, trans_width
, f
)
364 if (sizeof (time_t) == 4 || trans_width
== 4)
365 leaps
[i
].transition
= (time_t) decode (x
);
367 leaps
[i
].transition
= (time_t) decode64 (x
);
369 if (__builtin_expect (fread_unlocked (x
, 1, 4, f
) != 4, 0))
371 leaps
[i
].change
= (long int) decode (x
);
374 for (i
= 0; i
< num_isstd
; ++i
)
376 int c
= getc_unlocked (f
);
377 if (__builtin_expect (c
== EOF
, 0))
379 types
[i
].isstd
= c
!= 0;
381 while (i
< num_types
)
382 types
[i
++].isstd
= 0;
384 for (i
= 0; i
< num_isgmt
; ++i
)
386 int c
= getc_unlocked (f
);
387 if (__builtin_expect (c
== EOF
, 0))
389 types
[i
].isgmt
= c
!= 0;
391 while (i
< num_types
)
392 types
[i
++].isgmt
= 0;
394 /* Read the POSIX TZ-style information if possible. */
395 if (sizeof (time_t) == 8 && tzspec
!= NULL
)
397 /* Skip over the newline first. */
398 if (getc_unlocked (f
) != '\n'
399 || (fread_unlocked (tzspec
, 1, tzspec_len
- 1, f
)
403 tzspec
[tzspec_len
- 1] = '\0';
405 else if (sizeof (time_t) == 4 && tzhead
.tzh_version
[0] != '\0')
407 /* Get the TZ string. */
408 if (__builtin_expect (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
410 || (memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
))
414 size_t num_transitions2
= (size_t) decode (tzhead
.tzh_timecnt
);
415 size_t num_types2
= (size_t) decode (tzhead
.tzh_typecnt
);
416 size_t chars2
= (size_t) decode (tzhead
.tzh_charcnt
);
417 size_t num_leaps2
= (size_t) decode (tzhead
.tzh_leapcnt
);
418 size_t num_isstd2
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
419 size_t num_isgmt2
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
421 /* Position the stream before the second header. */
422 size_t to_skip
= (num_transitions2
* (8 + 1)
429 if (fseek (f
, to_skip
, SEEK_CUR
) != 0
430 || (off
= ftello (f
)) < 0
431 || st
.st_size
< off
+ 2)
434 tzspec_len
= st
.st_size
- off
- 1;
435 char *tzstr
= alloca (tzspec_len
);
436 if (getc_unlocked (f
) != '\n'
437 || (fread_unlocked (tzstr
, 1, tzspec_len
- 1, f
) != tzspec_len
- 1))
439 tzstr
[tzspec_len
- 1] = '\0';
440 tzspec
= __tzstring (tzstr
);
443 /* Don't use an empty TZ string. */
444 if (tzspec
!= NULL
&& tzspec
[0] == '\0')
449 /* First "register" all timezone names. */
450 for (i
= 0; i
< num_types
; ++i
)
451 (void) __tzstring (&zone_names
[types
[i
].idx
]);
453 /* Find the standard and daylight time offsets used by the rule file.
454 We choose the offsets in the types of each flavor that are
455 transitioned to earliest in time. */
458 for (i
= num_transitions
; i
> 0; )
460 int type
= type_idxs
[--i
];
461 int dst
= types
[type
].isdst
;
463 if (__tzname
[dst
] == NULL
)
465 int idx
= types
[type
].idx
;
467 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
469 if (__tzname
[1 - dst
] != NULL
)
473 if (__tzname
[0] == NULL
)
475 /* This should only happen if there are no transition rules.
476 In this case there should be only one single type. */
477 assert (num_types
== 1);
478 __tzname
[0] = __tzstring (zone_names
);
480 if (__tzname
[1] == NULL
)
481 __tzname
[1] = __tzname
[0];
483 compute_tzname_max (chars
);
485 if (num_transitions
== 0)
486 /* Use the first rule (which should also be the only one). */
487 rule_stdoff
= rule_dstoff
= types
[0].offset
;
490 int stdoff_set
= 0, dstoff_set
= 0;
491 rule_stdoff
= rule_dstoff
= 0;
492 i
= num_transitions
- 1;
495 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
498 rule_stdoff
= types
[type_idxs
[i
]].offset
;
500 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
503 rule_dstoff
= types
[type_idxs
[i
]].offset
;
505 if (stdoff_set
&& dstoff_set
)
511 rule_dstoff
= rule_stdoff
;
514 __daylight
= rule_stdoff
!= rule_dstoff
;
515 __timezone
= -rule_stdoff
;
524 ret_free_transitions
:
526 free ((void *) transitions
);
530 /* The user specified a hand-made timezone, but not its DST rules.
531 We will use the names and offsets from the user, and the rules
532 from the TZDEFRULES file. */
535 __tzfile_default (const char *std
, const char *dst
,
536 long int stdoff
, long int dstoff
)
538 size_t stdlen
= strlen (std
) + 1;
539 size_t dstlen
= strlen (dst
) + 1;
544 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
554 /* Ignore the zone names read from the file and use the given ones
556 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
559 /* Now there are only two zones, regardless of what the file contained. */
562 /* Now correct the transition times for the user-specified standard and
563 daylight offsets from GMT. */
565 for (i
= 0; i
< num_transitions
; ++i
)
567 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
569 /* We will use only types 0 (standard) and 1 (daylight).
570 Fix up this transition to point to whichever matches
571 the flavor of its original type. */
572 type_idxs
[i
] = trans_type
->isdst
;
574 if (trans_type
->isgmt
)
575 /* The transition time is in GMT. No correction to apply. */ ;
576 else if (isdst
&& !trans_type
->isstd
)
577 /* The type says this transition is in "local wall clock time", and
578 wall clock time as of the previous transition was DST. Correct
579 for the difference between the rule's DST offset and the user's
581 transitions
[i
] += dstoff
- rule_dstoff
;
583 /* This transition is in "local wall clock time", and wall clock
584 time as of this iteration is non-DST. Correct for the
585 difference between the rule's standard offset and the user's
587 transitions
[i
] += stdoff
- rule_stdoff
;
589 /* The DST state of "local wall clock time" for the next iteration is
590 as specified by this transition. */
591 isdst
= trans_type
->isdst
;
594 /* Now that we adjusted the transitions to the requested offsets,
595 reset the rule_stdoff and rule_dstoff values appropriately. They
596 are used elsewhere. */
597 rule_stdoff
= stdoff
;
598 rule_dstoff
= dstoff
;
600 /* Reset types 0 and 1 to describe the user's settings. */
602 types
[0].offset
= stdoff
;
604 types
[1].idx
= stdlen
;
605 types
[1].offset
= dstoff
;
608 /* Reset the zone names to point to the user's names. */
609 __tzname
[0] = (char *) std
;
610 __tzname
[1] = (char *) dst
;
612 /* Set the timezone. */
613 __timezone
= -types
[0].offset
;
615 compute_tzname_max (stdlen
+ dstlen
);
619 __tzfile_compute (time_t timer
, int use_localtime
,
620 long int *leap_correct
, int *leap_hit
,
630 if (__builtin_expect (num_transitions
== 0 || timer
< transitions
[0], 0))
632 /* TIMER is before any transition (or there are no transitions).
633 Choose the first non-DST type
634 (or the first if they're all DST types). */
636 while (i
< num_types
&& types
[i
].isdst
)
638 if (__tzname
[1] == NULL
)
639 __tzname
[1] = __tzstring (&zone_names
[types
[i
].idx
]);
646 __tzname
[0] = __tzstring (&zone_names
[types
[i
].idx
]);
647 if (__tzname
[1] == NULL
)
650 while (j
< num_types
)
653 __tzname
[1] = __tzstring (&zone_names
[types
[j
].idx
]);
660 else if (__builtin_expect (timer
>= transitions
[num_transitions
- 1], 0))
662 if (__builtin_expect (tzspec
== NULL
, 0))
669 /* Parse the POSIX TZ-style string. */
670 __tzset_parse_tz (tzspec
);
672 /* Convert to broken down structure. If this fails do not
674 if (__builtin_expect (! __offtime (&timer
, 0, tp
), 0))
677 /* Use the rules from the TZ string to compute the change. */
678 __tz_compute (timer
, tp
, 1);
680 /* If tzspec comes from posixrules loaded by __tzfile_default,
681 override the STD and DST zone names with the ones user
682 requested in TZ envvar. */
683 if (__builtin_expect (zone_names
== (char *) &leaps
[num_leaps
], 0))
685 assert (num_types
== 2);
686 __tzname
[0] = __tzstring (zone_names
);
687 __tzname
[1] = __tzstring (&zone_names
[strlen (zone_names
) + 1]);
694 /* Find the first transition after TIMER, and
695 then pick the type of the transition before it. */
697 size_t hi
= num_transitions
- 1;
698 /* Assume that DST is changing twice a year and guess initial
700 Half of a gregorian year has on average 365.2425 * 86400 / 2
701 = 15778476 seconds. */
702 i
= (transitions
[num_transitions
- 1] - timer
) / 15778476;
703 if (i
< num_transitions
)
705 i
= num_transitions
- 1 - i
;
706 if (timer
< transitions
[i
])
708 if (i
< 10 || timer
>= transitions
[i
- 10])
711 while (timer
< transitions
[i
- 1])
719 if (i
+ 10 >= num_transitions
|| timer
< transitions
[i
+ 10])
722 while (timer
>= transitions
[i
])
731 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
735 if (timer
< transitions
[i
])
743 /* assert (timer >= transitions[i - 1]
744 && (i == num_transitions || timer < transitions[i])); */
745 __tzname
[types
[type_idxs
[i
- 1]].isdst
]
746 = __tzstring (&zone_names
[types
[type_idxs
[i
- 1]].idx
]);
748 while (j
< num_transitions
)
750 int type
= type_idxs
[j
];
751 int dst
= types
[type
].isdst
;
752 int idx
= types
[type
].idx
;
754 if (__tzname
[dst
] == NULL
)
756 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
758 if (__tzname
[1 - dst
] != NULL
)
765 if (__builtin_expect (__tzname
[0] == NULL
, 0))
766 __tzname
[0] = __tzname
[1];
768 i
= type_idxs
[i
- 1];
771 struct ttinfo
*info
= &types
[i
];
772 __daylight
= rule_stdoff
!= rule_dstoff
;
773 __timezone
= -rule_stdoff
;
775 if (__tzname
[0] == NULL
)
777 /* This should only happen if there are no transition rules.
778 In this case there should be only one single type. */
779 assert (num_types
== 1);
780 __tzname
[0] = __tzstring (zone_names
);
782 if (__tzname
[1] == NULL
)
783 /* There is no daylight saving time. */
784 __tzname
[1] = __tzname
[0];
785 tp
->tm_isdst
= info
->isdst
;
786 assert (strcmp (&zone_names
[info
->idx
], __tzname
[tp
->tm_isdst
]) == 0);
787 tp
->tm_zone
= __tzname
[tp
->tm_isdst
];
788 tp
->tm_gmtoff
= info
->offset
;
795 /* Find the last leap second correction transition time before TIMER. */
800 while (timer
< leaps
[i
].transition
);
802 /* Apply its correction. */
803 *leap_correct
= leaps
[i
].change
;
805 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
806 ((i
== 0 && leaps
[i
].change
> 0) ||
807 leaps
[i
].change
> leaps
[i
- 1].change
))
811 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
812 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
822 compute_tzname_max (size_t chars
)
829 const char *start
= p
;
832 if ((size_t) (p
- start
) > __tzname_cur_max
)
833 __tzname_cur_max
= p
- start
;
835 while (++p
< &zone_names
[chars
]);