1 /* Copyright (C) 1991-1993,1995-2001,2003,2004,2006,2007,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #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
;
118 if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
124 /* No user specification; use the site-wide default. */
126 else if (*file
== '\0')
127 /* User specified the empty string; use UTC with no leap seconds. */
128 goto ret_free_transitions
;
131 /* We must not allow to read an arbitrary file in a setuid
132 program. So we fail for any file which is not in the
133 directory hierachy starting at TZDIR
134 and which is not the system wide default TZDEFAULT. */
135 if (__libc_enable_secure
137 && memcmp (file
, TZDEFAULT
, sizeof TZDEFAULT
)
138 && memcmp (file
, default_tzdir
, sizeof (default_tzdir
) - 1))
139 || strstr (file
, "../") != NULL
))
140 /* This test is certainly a bit too restrictive but it should
141 catch all critical cases. */
142 goto ret_free_transitions
;
148 unsigned int len
, tzdir_len
;
151 tzdir
= getenv ("TZDIR");
152 if (tzdir
== NULL
|| *tzdir
== '\0')
154 tzdir
= default_tzdir
;
155 tzdir_len
= sizeof (default_tzdir
) - 1;
158 tzdir_len
= strlen (tzdir
);
159 len
= strlen (file
) + 1;
160 new = (char *) __alloca (tzdir_len
+ 1 + len
);
161 tmp
= __mempcpy (new, tzdir
, tzdir_len
);
163 memcpy (tmp
, file
, len
);
167 /* If we were already using tzfile, check whether the file changed. */
170 && stat64 (file
, &st
) == 0
171 && tzfile_ino
== st
.st_ino
&& tzfile_dev
== st
.st_dev
172 && tzfile_mtime
== st
.st_mtime
)
179 /* Note the file is opened with cancellation in the I/O functions
180 disabled and if available FD_CLOEXEC set. */
181 f
= fopen (file
, "rce");
183 goto ret_free_transitions
;
185 /* Get information about the file we are actually using. */
186 if (fstat64 (fileno (f
), &st
) != 0)
189 goto ret_free_transitions
;
192 free ((void *) transitions
);
195 /* Remember the inode and device number and modification time. */
196 tzfile_dev
= st
.st_dev
;
197 tzfile_ino
= st
.st_ino
;
198 tzfile_mtime
= st
.st_mtime
;
200 /* No threads reading this stream. */
201 __fsetlocking (f
, FSETLOCKING_BYCALLER
);
204 if (__builtin_expect (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
206 || memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
)) != 0)
209 num_transitions
= (size_t) decode (tzhead
.tzh_timecnt
);
210 num_types
= (size_t) decode (tzhead
.tzh_typecnt
);
211 chars
= (size_t) decode (tzhead
.tzh_charcnt
);
212 num_leaps
= (size_t) decode (tzhead
.tzh_leapcnt
);
213 num_isstd
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
214 num_isgmt
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
216 /* For platforms with 64-bit time_t we use the new format if available. */
217 if (sizeof (time_t) == 8 && trans_width
== 4
218 && tzhead
.tzh_version
[0] != '\0')
220 /* We use the 8-byte format. */
223 /* Position the stream before the second header. */
224 size_t to_skip
= (num_transitions
* (4 + 1)
230 if (fseek (f
, to_skip
, SEEK_CUR
) != 0)
236 if (__builtin_expect (num_transitions
237 > ((SIZE_MAX
- (__alignof__ (struct ttinfo
) - 1))
238 / (sizeof (time_t) + 1)), 0))
240 total_size
= num_transitions
* (sizeof (time_t) + 1);
241 total_size
= ((total_size
+ __alignof__ (struct ttinfo
) - 1)
242 & ~(__alignof__ (struct ttinfo
) - 1));
243 types_idx
= total_size
;
244 if (__builtin_expect (num_types
245 > (SIZE_MAX
- total_size
) / sizeof (struct ttinfo
), 0))
247 total_size
+= num_types
* sizeof (struct ttinfo
);
248 if (__builtin_expect (chars
> SIZE_MAX
- total_size
, 0))
251 if (__builtin_expect (__alignof__ (struct leap
) - 1
252 > SIZE_MAX
- total_size
, 0))
254 total_size
= ((total_size
+ __alignof__ (struct leap
) - 1)
255 & ~(__alignof__ (struct leap
) - 1));
256 leaps_idx
= total_size
;
257 if (__builtin_expect (num_leaps
258 > (SIZE_MAX
- total_size
) / sizeof (struct leap
), 0))
260 total_size
+= num_leaps
* sizeof (struct leap
);
262 if (sizeof (time_t) == 8 && trans_width
== 8)
264 off_t rem
= st
.st_size
- ftello (f
);
265 if (__builtin_expect (rem
< 0
266 || (size_t) rem
< (num_transitions
* (8 + 1)
270 tzspec_len
= (size_t) rem
- (num_transitions
* (8 + 1)
273 if (__builtin_expect (num_leaps
> SIZE_MAX
/ 12
274 || tzspec_len
< num_leaps
* 12, 0))
276 tzspec_len
-= num_leaps
* 12;
277 if (__builtin_expect (tzspec_len
< num_isstd
, 0))
279 tzspec_len
-= num_isstd
;
280 if (__builtin_expect (tzspec_len
== 0 || tzspec_len
- 1 < num_isgmt
, 0))
282 tzspec_len
-= num_isgmt
+ 1;
283 if (__builtin_expect (SIZE_MAX
- total_size
< tzspec_len
, 0))
286 if (__builtin_expect (SIZE_MAX
- total_size
- tzspec_len
< extra
, 0))
289 /* Allocate enough memory including the extra block requested by the
291 transitions
= (time_t *) malloc (total_size
+ tzspec_len
+ extra
);
292 if (transitions
== NULL
)
295 type_idxs
= (unsigned char *) transitions
+ (num_transitions
297 types
= (struct ttinfo
*) ((char *) transitions
+ types_idx
);
298 zone_names
= (char *) types
+ num_types
* sizeof (struct ttinfo
);
299 leaps
= (struct leap
*) ((char *) transitions
+ leaps_idx
);
300 if (sizeof (time_t) == 8 && trans_width
== 8)
301 tzspec
= (char *) leaps
+ num_leaps
* sizeof (struct leap
) + extra
;
305 *extrap
= (char *) &leaps
[num_leaps
];
307 if (sizeof (time_t) == 4 || __builtin_expect (trans_width
== 8, 1))
309 if (__builtin_expect (fread_unlocked (transitions
, trans_width
+ 1,
311 != num_transitions
, 0))
316 if (__builtin_expect (fread_unlocked (transitions
, 4, num_transitions
, f
)
317 != num_transitions
, 0)
318 || __builtin_expect (fread_unlocked (type_idxs
, 1, num_transitions
,
319 f
) != num_transitions
, 0))
323 /* Check for bogus indices in the data file, so we can hereafter
324 safely use type_idxs[T] as indices into `types' and never crash. */
325 for (i
= 0; i
< num_transitions
; ++i
)
326 if (__builtin_expect (type_idxs
[i
] >= num_types
, 0))
329 if ((BYTE_ORDER
!= BIG_ENDIAN
&& (sizeof (time_t) == 4 || trans_width
== 4))
330 || (BYTE_ORDER
== BIG_ENDIAN
&& sizeof (time_t) == 8
331 && trans_width
== 4))
333 /* Decode the transition times, stored as 4-byte integers in
334 network (big-endian) byte order. We work from the end of
335 the array so as not to clobber the next element to be
336 processed when sizeof (time_t) > 4. */
339 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
341 else if (BYTE_ORDER
!= BIG_ENDIAN
&& sizeof (time_t) == 8)
343 /* Decode the transition times, stored as 8-byte integers in
344 network (big-endian) byte order. */
345 for (i
= 0; i
< num_transitions
; ++i
)
346 transitions
[i
] = decode64 ((char *) transitions
+ i
* 8);
349 for (i
= 0; i
< num_types
; ++i
)
353 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
356 c
= getc_unlocked (f
);
357 if (__builtin_expect ((unsigned int) c
> 1u, 0))
360 c
= getc_unlocked (f
);
361 if (__builtin_expect ((size_t) c
> chars
, 0))
362 /* Bogus index in data file. */
365 types
[i
].offset
= (long int) decode (x
);
368 if (__builtin_expect (fread_unlocked (zone_names
, 1, chars
, f
) != chars
, 0))
371 for (i
= 0; i
< num_leaps
; ++i
)
374 if (__builtin_expect (fread_unlocked (x
, 1, trans_width
, f
)
377 if (sizeof (time_t) == 4 || trans_width
== 4)
378 leaps
[i
].transition
= (time_t) decode (x
);
380 leaps
[i
].transition
= (time_t) decode64 (x
);
382 if (__builtin_expect (fread_unlocked (x
, 1, 4, f
) != 4, 0))
384 leaps
[i
].change
= (long int) decode (x
);
387 for (i
= 0; i
< num_isstd
; ++i
)
389 int c
= getc_unlocked (f
);
390 if (__builtin_expect (c
== EOF
, 0))
392 types
[i
].isstd
= c
!= 0;
394 while (i
< num_types
)
395 types
[i
++].isstd
= 0;
397 for (i
= 0; i
< num_isgmt
; ++i
)
399 int c
= getc_unlocked (f
);
400 if (__builtin_expect (c
== EOF
, 0))
402 types
[i
].isgmt
= c
!= 0;
404 while (i
< num_types
)
405 types
[i
++].isgmt
= 0;
407 /* Read the POSIX TZ-style information if possible. */
408 if (sizeof (time_t) == 8 && tzspec
!= NULL
)
410 /* Skip over the newline first. */
411 if (getc_unlocked (f
) != '\n'
412 || (fread_unlocked (tzspec
, 1, tzspec_len
- 1, f
)
416 tzspec
[tzspec_len
- 1] = '\0';
418 else if (sizeof (time_t) == 4 && tzhead
.tzh_version
[0] != '\0')
420 /* Get the TZ string. */
421 if (__builtin_expect (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
423 || (memcmp (tzhead
.tzh_magic
, TZ_MAGIC
, sizeof (tzhead
.tzh_magic
))
427 size_t num_transitions2
= (size_t) decode (tzhead
.tzh_timecnt
);
428 size_t num_types2
= (size_t) decode (tzhead
.tzh_typecnt
);
429 size_t chars2
= (size_t) decode (tzhead
.tzh_charcnt
);
430 size_t num_leaps2
= (size_t) decode (tzhead
.tzh_leapcnt
);
431 size_t num_isstd2
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
432 size_t num_isgmt2
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
434 /* Position the stream before the second header. */
435 size_t to_skip
= (num_transitions2
* (8 + 1)
442 if (fseek (f
, to_skip
, SEEK_CUR
) != 0
443 || (off
= ftello (f
)) < 0
444 || st
.st_size
< off
+ 2)
447 tzspec_len
= st
.st_size
- off
- 1;
448 char *tzstr
= alloca (tzspec_len
);
449 if (getc_unlocked (f
) != '\n'
450 || (fread_unlocked (tzstr
, 1, tzspec_len
- 1, f
) != tzspec_len
- 1))
452 tzstr
[tzspec_len
- 1] = '\0';
453 tzspec
= __tzstring (tzstr
);
456 /* Don't use an empty TZ string. */
457 if (tzspec
!= NULL
&& tzspec
[0] == '\0')
462 /* First "register" all timezone names. */
463 for (i
= 0; i
< num_types
; ++i
)
464 (void) __tzstring (&zone_names
[types
[i
].idx
]);
466 /* Find the standard and daylight time offsets used by the rule file.
467 We choose the offsets in the types of each flavor that are
468 transitioned to earliest in time. */
471 for (i
= num_transitions
; i
> 0; )
473 int type
= type_idxs
[--i
];
474 int dst
= types
[type
].isdst
;
476 if (__tzname
[dst
] == NULL
)
478 int idx
= types
[type
].idx
;
480 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
482 if (__tzname
[1 - dst
] != NULL
)
486 if (__tzname
[0] == NULL
)
488 /* This should only happen if there are no transition rules.
489 In this case there should be only one single type. */
490 assert (num_types
== 1);
491 __tzname
[0] = __tzstring (zone_names
);
493 if (__tzname
[1] == NULL
)
494 __tzname
[1] = __tzname
[0];
496 compute_tzname_max (chars
);
498 if (num_transitions
== 0)
499 /* Use the first rule (which should also be the only one). */
500 rule_stdoff
= rule_dstoff
= types
[0].offset
;
503 int stdoff_set
= 0, dstoff_set
= 0;
504 rule_stdoff
= rule_dstoff
= 0;
505 i
= num_transitions
- 1;
508 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
511 rule_stdoff
= types
[type_idxs
[i
]].offset
;
513 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
516 rule_dstoff
= types
[type_idxs
[i
]].offset
;
518 if (stdoff_set
&& dstoff_set
)
524 rule_dstoff
= rule_stdoff
;
527 __daylight
= rule_stdoff
!= rule_dstoff
;
528 __timezone
= -rule_stdoff
;
535 ret_free_transitions
:
536 free ((void *) transitions
);
540 /* The user specified a hand-made timezone, but not its DST rules.
541 We will use the names and offsets from the user, and the rules
542 from the TZDEFRULES file. */
545 __tzfile_default (const char *std
, const char *dst
,
546 long int stdoff
, long int dstoff
)
548 size_t stdlen
= strlen (std
) + 1;
549 size_t dstlen
= strlen (dst
) + 1;
554 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
564 /* Ignore the zone names read from the file and use the given ones
566 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
569 /* Now there are only two zones, regardless of what the file contained. */
572 /* Now correct the transition times for the user-specified standard and
573 daylight offsets from GMT. */
575 for (i
= 0; i
< num_transitions
; ++i
)
577 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
579 /* We will use only types 0 (standard) and 1 (daylight).
580 Fix up this transition to point to whichever matches
581 the flavor of its original type. */
582 type_idxs
[i
] = trans_type
->isdst
;
584 if (trans_type
->isgmt
)
585 /* The transition time is in GMT. No correction to apply. */ ;
586 else if (isdst
&& !trans_type
->isstd
)
587 /* The type says this transition is in "local wall clock time", and
588 wall clock time as of the previous transition was DST. Correct
589 for the difference between the rule's DST offset and the user's
591 transitions
[i
] += dstoff
- rule_dstoff
;
593 /* This transition is in "local wall clock time", and wall clock
594 time as of this iteration is non-DST. Correct for the
595 difference between the rule's standard offset and the user's
597 transitions
[i
] += stdoff
- rule_stdoff
;
599 /* The DST state of "local wall clock time" for the next iteration is
600 as specified by this transition. */
601 isdst
= trans_type
->isdst
;
604 /* Now that we adjusted the transitions to the requested offsets,
605 reset the rule_stdoff and rule_dstoff values appropriately. They
606 are used elsewhere. */
607 rule_stdoff
= stdoff
;
608 rule_dstoff
= dstoff
;
610 /* Reset types 0 and 1 to describe the user's settings. */
612 types
[0].offset
= stdoff
;
614 types
[1].idx
= stdlen
;
615 types
[1].offset
= dstoff
;
618 /* Reset the zone names to point to the user's names. */
619 __tzname
[0] = (char *) std
;
620 __tzname
[1] = (char *) dst
;
622 /* Set the timezone. */
623 __timezone
= -types
[0].offset
;
625 compute_tzname_max (stdlen
+ dstlen
);
629 __tzfile_compute (time_t timer
, int use_localtime
,
630 long int *leap_correct
, int *leap_hit
,
640 if (__builtin_expect (num_transitions
== 0 || timer
< transitions
[0], 0))
642 /* TIMER is before any transition (or there are no transitions).
643 Choose the first non-DST type
644 (or the first if they're all DST types). */
646 while (i
< num_types
&& types
[i
].isdst
)
648 if (__tzname
[1] == NULL
)
649 __tzname
[1] = __tzstring (&zone_names
[types
[i
].idx
]);
656 __tzname
[0] = __tzstring (&zone_names
[types
[i
].idx
]);
657 if (__tzname
[1] == NULL
)
660 while (j
< num_types
)
663 __tzname
[1] = __tzstring (&zone_names
[types
[j
].idx
]);
670 else if (__builtin_expect (timer
>= transitions
[num_transitions
- 1], 0))
672 if (__builtin_expect (tzspec
== NULL
, 0))
679 /* Parse the POSIX TZ-style string. */
680 __tzset_parse_tz (tzspec
);
682 /* Convert to broken down structure. If this fails do not
684 if (__builtin_expect (! __offtime (&timer
, 0, tp
), 0))
687 /* Use the rules from the TZ string to compute the change. */
688 __tz_compute (timer
, tp
, 1);
690 /* If tzspec comes from posixrules loaded by __tzfile_default,
691 override the STD and DST zone names with the ones user
692 requested in TZ envvar. */
693 if (__builtin_expect (zone_names
== (char *) &leaps
[num_leaps
], 0))
695 assert (num_types
== 2);
696 __tzname
[0] = __tzstring (zone_names
);
697 __tzname
[1] = __tzstring (&zone_names
[strlen (zone_names
) + 1]);
704 /* Find the first transition after TIMER, and
705 then pick the type of the transition before it. */
707 size_t hi
= num_transitions
- 1;
708 /* Assume that DST is changing twice a year and guess initial
710 Half of a gregorian year has on average 365.2425 * 86400 / 2
711 = 15778476 seconds. */
712 i
= (transitions
[num_transitions
- 1] - timer
) / 15778476;
713 if (i
< num_transitions
)
715 i
= num_transitions
- 1 - i
;
716 if (timer
< transitions
[i
])
718 if (i
< 10 || timer
>= transitions
[i
- 10])
721 while (timer
< transitions
[i
- 1])
729 if (i
+ 10 >= num_transitions
|| timer
< transitions
[i
+ 10])
732 while (timer
>= transitions
[i
])
741 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
745 if (timer
< transitions
[i
])
753 /* assert (timer >= transitions[i - 1]
754 && (i == num_transitions || timer < transitions[i])); */
755 __tzname
[types
[type_idxs
[i
- 1]].isdst
]
756 = __tzstring (&zone_names
[types
[type_idxs
[i
- 1]].idx
]);
758 while (j
< num_transitions
)
760 int type
= type_idxs
[j
];
761 int dst
= types
[type
].isdst
;
762 int idx
= types
[type
].idx
;
764 if (__tzname
[dst
] == NULL
)
766 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
768 if (__tzname
[1 - dst
] != NULL
)
775 if (__builtin_expect (__tzname
[0] == NULL
, 0))
776 __tzname
[0] = __tzname
[1];
778 i
= type_idxs
[i
- 1];
781 struct ttinfo
*info
= &types
[i
];
782 __daylight
= rule_stdoff
!= rule_dstoff
;
783 __timezone
= -rule_stdoff
;
785 if (__tzname
[0] == NULL
)
787 /* This should only happen if there are no transition rules.
788 In this case there should be only one single type. */
789 assert (num_types
== 1);
790 __tzname
[0] = __tzstring (zone_names
);
792 if (__tzname
[1] == NULL
)
793 /* There is no daylight saving time. */
794 __tzname
[1] = __tzname
[0];
795 tp
->tm_isdst
= info
->isdst
;
796 assert (strcmp (&zone_names
[info
->idx
], __tzname
[tp
->tm_isdst
]) == 0);
797 tp
->tm_zone
= __tzname
[tp
->tm_isdst
];
798 tp
->tm_gmtoff
= info
->offset
;
805 /* Find the last leap second correction transition time before TIMER. */
810 while (timer
< leaps
[i
].transition
);
812 /* Apply its correction. */
813 *leap_correct
= leaps
[i
].change
;
815 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
816 ((i
== 0 && leaps
[i
].change
> 0) ||
817 leaps
[i
].change
> leaps
[i
- 1].change
))
821 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
822 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
832 compute_tzname_max (size_t chars
)
839 const char *start
= p
;
842 if ((size_t) (p
- start
) > __tzname_cur_max
)
843 __tzname_cur_max
= p
- start
;
845 while (++p
< &zone_names
[chars
]);