* sysdeps/x86_64/memset.S: Jump from bzero to memset using
[glibc/pb-stable.git] / time / tzfile.c
blob44d66147718219ab8570f6a27d8a83695a36bfd0
1 /* Copyright (C) 1991-1993,1995-2001,2003,2004,2006, 2007
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
30 #define NOID
31 #include <timezone/tzfile.h>
33 int __use_tzfile;
34 static dev_t tzfile_dev;
35 static ino64_t tzfile_ino;
36 static time_t tzfile_mtime;
38 struct ttinfo
40 long int offset; /* Seconds east of GMT. */
41 unsigned char isdst; /* Used to set tm_isdst. */
42 unsigned char idx; /* Index into `zone_names'. */
43 unsigned char isstd; /* Transition times are in standard time. */
44 unsigned char isgmt; /* Transition times are in GMT. */
47 struct leap
49 time_t transition; /* Time the transition takes effect. */
50 long int change; /* Seconds of correction to apply. */
53 static void compute_tzname_max (size_t) internal_function;
55 static size_t num_transitions;
56 libc_freeres_ptr (static time_t *transitions);
57 static unsigned char *type_idxs;
58 static size_t num_types;
59 static struct ttinfo *types;
60 static char *zone_names;
61 static long int rule_stdoff;
62 static long int rule_dstoff;
63 static size_t num_leaps;
64 static struct leap *leaps;
65 static char *tzspec;
67 #include <endian.h>
68 #include <byteswap.h>
70 /* Decode the four bytes at PTR as a signed integer in network byte order. */
71 static inline int
72 __attribute ((always_inline))
73 decode (const void *ptr)
75 if (BYTE_ORDER == BIG_ENDIAN && sizeof (int) == 4)
76 return *(const int *) ptr;
77 if (sizeof (int) == 4)
78 return bswap_32 (*(const int *) ptr);
80 const unsigned char *p = ptr;
81 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
83 result = (result << 8) | *p++;
84 result = (result << 8) | *p++;
85 result = (result << 8) | *p++;
86 result = (result << 8) | *p++;
88 return result;
92 static inline int64_t
93 __attribute ((always_inline))
94 decode64 (const void *ptr)
96 if ((BYTE_ORDER == BIG_ENDIAN))
97 return *(const int64_t *) ptr;
99 return bswap_64 (*(const int64_t *) ptr);
103 void
104 __tzfile_read (const char *file, size_t extra, char **extrap)
106 static const char default_tzdir[] = TZDIR;
107 size_t num_isstd, num_isgmt;
108 register FILE *f;
109 struct tzhead tzhead;
110 size_t chars;
111 register size_t i;
112 size_t total_size;
113 size_t types_idx;
114 size_t leaps_idx;
115 int was_using_tzfile = __use_tzfile;
116 int trans_width = 4;
117 size_t tzspec_len;
119 if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
120 abort ();
122 __use_tzfile = 0;
124 if (file == NULL)
125 /* No user specification; use the site-wide default. */
126 file = TZDEFAULT;
127 else if (*file == '\0')
128 /* User specified the empty string; use UTC with no leap seconds. */
129 goto ret_free_transitions;
130 else
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
137 && ((*file == '/'
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;
146 if (*file != '/')
148 const char *tzdir;
149 unsigned int len, tzdir_len;
150 char *new, *tmp;
152 tzdir = getenv ("TZDIR");
153 if (tzdir == NULL || *tzdir == '\0')
155 tzdir = default_tzdir;
156 tzdir_len = sizeof (default_tzdir) - 1;
158 else
159 tzdir_len = strlen (tzdir);
160 len = strlen (file) + 1;
161 new = (char *) __alloca (tzdir_len + 1 + len);
162 tmp = __mempcpy (new, tzdir, tzdir_len);
163 *tmp++ = '/';
164 memcpy (tmp, file, len);
165 file = new;
168 /* If we were already using tzfile, check whether the file changed. */
169 struct stat64 st;
170 if (was_using_tzfile
171 && stat64 (file, &st) == 0
172 && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
173 && tzfile_mtime == st.st_mtime)
175 /* Nothing to do. */
176 __use_tzfile = 1;
177 return;
180 /* Note the file is opened with cancellation in the I/O functions
181 disabled. */
182 f = fopen (file, "rc");
183 if (f == NULL)
184 goto ret_free_transitions;
186 /* Get information about the file we are actually using. */
187 if (fstat64 (fileno (f), &st) != 0)
189 fclose (f);
190 goto ret_free_transitions;
193 free ((void *) transitions);
194 transitions = NULL;
196 /* Remember the inode and device number and modification time. */
197 tzfile_dev = st.st_dev;
198 tzfile_ino = st.st_ino;
199 tzfile_mtime = st.st_mtime;
201 /* No threads reading this stream. */
202 __fsetlocking (f, FSETLOCKING_BYCALLER);
204 read_again:
205 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
206 1, f) != 1, 0)
207 || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
208 goto lose;
210 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
211 num_types = (size_t) decode (tzhead.tzh_typecnt);
212 chars = (size_t) decode (tzhead.tzh_charcnt);
213 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
214 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
215 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
217 /* For platforms with 64-bit time_t we use the new format if available. */
218 if (sizeof (time_t) == 8 && trans_width == 4
219 && tzhead.tzh_version[0] != '\0')
221 /* We use the 8-byte format. */
222 trans_width = 8;
224 /* Position the stream before the second header. */
225 size_t to_skip = (num_transitions * (4 + 1)
226 + num_types * 6
227 + chars
228 + num_leaps * 8
229 + num_isstd
230 + num_isgmt);
231 if (fseek (f, to_skip, SEEK_CUR) != 0)
232 goto lose;
234 goto read_again;
237 total_size = num_transitions * (sizeof (time_t) + 1);
238 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
239 & ~(__alignof__ (struct ttinfo) - 1));
240 types_idx = total_size;
241 total_size += num_types * sizeof (struct ttinfo) + chars;
242 total_size = ((total_size + __alignof__ (struct leap) - 1)
243 & ~(__alignof__ (struct leap) - 1));
244 leaps_idx = total_size;
245 total_size += num_leaps * sizeof (struct leap);
246 tzspec_len = (trans_width == 8
247 ? st.st_size - (ftello (f)
248 + num_transitions * (8 + 1)
249 + num_types * 6
250 + chars
251 + num_leaps * 8
252 + num_isstd
253 + num_isgmt) - 1 : 0);
255 /* Allocate enough memory including the extra block requested by the
256 caller. */
257 transitions = (time_t *) malloc (total_size + tzspec_len + extra);
258 if (transitions == NULL)
259 goto lose;
261 type_idxs = (unsigned char *) transitions + (num_transitions
262 * sizeof (time_t));
263 types = (struct ttinfo *) ((char *) transitions + types_idx);
264 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
265 leaps = (struct leap *) ((char *) transitions + leaps_idx);
266 if (trans_width == 8)
267 tzspec = (char *) leaps + num_leaps * sizeof (struct leap);
268 else
269 tzspec = NULL;
270 if (extra > 0)
271 *extrap = (char *) &leaps[num_leaps];
273 if (sizeof (time_t) == 4 || trans_width == 8)
275 if (__builtin_expect (fread_unlocked (transitions, trans_width + 1,
276 num_transitions, f)
277 != num_transitions, 0))
278 goto lose;
280 else
282 if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
283 != num_transitions, 0)
284 || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
285 f) != num_transitions, 0))
286 goto lose;
289 /* Check for bogus indices in the data file, so we can hereafter
290 safely use type_idxs[T] as indices into `types' and never crash. */
291 for (i = 0; i < num_transitions; ++i)
292 if (__builtin_expect (type_idxs[i] >= num_types, 0))
293 goto lose;
295 if ((BYTE_ORDER != BIG_ENDIAN && (sizeof (time_t) == 4 || trans_width == 4))
296 || (BYTE_ORDER == BIG_ENDIAN && sizeof (time_t) == 8
297 && trans_width == 4))
299 /* Decode the transition times, stored as 4-byte integers in
300 network (big-endian) byte order. We work from the end of
301 the array so as not to clobber the next element to be
302 processed when sizeof (time_t) > 4. */
303 i = num_transitions;
304 while (i-- > 0)
305 transitions[i] = decode ((char *) transitions + i * 4);
307 else if (BYTE_ORDER != BIG_ENDIAN && sizeof (time_t) == 8)
309 /* Decode the transition times, stored as 8-byte integers in
310 network (big-endian) byte order. */
311 for (i = 0; i < num_transitions; ++i)
312 transitions[i] = decode64 ((char *) transitions + i * 8);
315 for (i = 0; i < num_types; ++i)
317 unsigned char x[4];
318 int c;
319 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
321 goto lose;
322 c = getc_unlocked (f);
323 if (__builtin_expect ((unsigned int) c > 1u, 0))
324 goto lose;
325 types[i].isdst = c;
326 c = getc_unlocked (f);
327 if (__builtin_expect ((size_t) c > chars, 0))
328 /* Bogus index in data file. */
329 goto lose;
330 types[i].idx = c;
331 types[i].offset = (long int) decode (x);
334 if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
335 goto lose;
337 for (i = 0; i < num_leaps; ++i)
339 unsigned char x[8];
340 if (__builtin_expect (fread_unlocked (x, 1, trans_width, f)
341 != trans_width, 0))
342 goto lose;
343 if (sizeof (time_t) == 4 || trans_width == 4)
344 leaps[i].transition = (time_t) decode (x);
345 else
346 leaps[i].transition = (time_t) decode64 (x);
348 if (__builtin_expect (fread_unlocked (x, 1, 4, f) != 4, 0))
349 goto lose;
350 leaps[i].change = (long int) decode (x);
353 for (i = 0; i < num_isstd; ++i)
355 int c = getc_unlocked (f);
356 if (__builtin_expect (c == EOF, 0))
357 goto lose;
358 types[i].isstd = c != 0;
360 while (i < num_types)
361 types[i++].isstd = 0;
363 for (i = 0; i < num_isgmt; ++i)
365 int c = getc_unlocked (f);
366 if (__builtin_expect (c == EOF, 0))
367 goto lose;
368 types[i].isgmt = c != 0;
370 while (i < num_types)
371 types[i++].isgmt = 0;
373 /* Read the POSIX TZ-style information if possible. */
374 if (tzspec != NULL)
376 /* Skip over the newline first. */
377 if (getc_unlocked (f) != '\n'
378 || fread_unlocked (tzspec, 1, tzspec_len - 1, f) != tzspec_len - 1)
379 tzspec = NULL;
380 else
381 tzspec[tzspec_len - 1] = '\0';
384 fclose (f);
386 /* First "register" all timezone names. */
387 for (i = 0; i < num_types; ++i)
388 (void) __tzstring (&zone_names[types[i].idx]);
390 /* Find the standard and daylight time offsets used by the rule file.
391 We choose the offsets in the types of each flavor that are
392 transitioned to earliest in time. */
393 __tzname[0] = NULL;
394 __tzname[1] = NULL;
395 for (i = num_transitions; i > 0; )
397 int type = type_idxs[--i];
398 int dst = types[type].isdst;
400 if (__tzname[dst] == NULL)
402 int idx = types[type].idx;
404 __tzname[dst] = __tzstring (&zone_names[idx]);
406 if (__tzname[1 - dst] != NULL)
407 break;
410 if (__tzname[0] == NULL)
412 /* This should only happen if there are no transition rules.
413 In this case there should be only one single type. */
414 assert (num_types == 1);
415 __tzname[0] = __tzstring (zone_names);
417 if (__tzname[1] == NULL)
418 __tzname[1] = __tzname[0];
420 compute_tzname_max (chars);
422 if (num_transitions == 0)
423 /* Use the first rule (which should also be the only one). */
424 rule_stdoff = rule_dstoff = types[0].offset;
425 else
427 int stdoff_set = 0, dstoff_set = 0;
428 rule_stdoff = rule_dstoff = 0;
429 i = num_transitions - 1;
432 if (!stdoff_set && !types[type_idxs[i]].isdst)
434 stdoff_set = 1;
435 rule_stdoff = types[type_idxs[i]].offset;
437 else if (!dstoff_set && types[type_idxs[i]].isdst)
439 dstoff_set = 1;
440 rule_dstoff = types[type_idxs[i]].offset;
442 if (stdoff_set && dstoff_set)
443 break;
445 while (i-- > 0);
447 if (!dstoff_set)
448 rule_dstoff = rule_stdoff;
451 __daylight = rule_stdoff != rule_dstoff;
452 __timezone = -rule_stdoff;
454 __use_tzfile = 1;
455 return;
457 lose:
458 fclose (f);
459 ret_free_transitions:
460 free ((void *) transitions);
461 transitions = NULL;
464 /* The user specified a hand-made timezone, but not its DST rules.
465 We will use the names and offsets from the user, and the rules
466 from the TZDEFRULES file. */
468 void
469 __tzfile_default (const char *std, const char *dst,
470 long int stdoff, long int dstoff)
472 size_t stdlen = strlen (std) + 1;
473 size_t dstlen = strlen (dst) + 1;
474 size_t i;
475 int isdst;
476 char *cp;
478 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
479 if (!__use_tzfile)
480 return;
482 if (num_types < 2)
484 __use_tzfile = 0;
485 return;
488 /* Ignore the zone names read from the file and use the given ones
489 instead. */
490 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
491 zone_names = cp;
493 /* Now there are only two zones, regardless of what the file contained. */
494 num_types = 2;
496 /* Now correct the transition times for the user-specified standard and
497 daylight offsets from GMT. */
498 isdst = 0;
499 for (i = 0; i < num_transitions; ++i)
501 struct ttinfo *trans_type = &types[type_idxs[i]];
503 /* We will use only types 0 (standard) and 1 (daylight).
504 Fix up this transition to point to whichever matches
505 the flavor of its original type. */
506 type_idxs[i] = trans_type->isdst;
508 if (trans_type->isgmt)
509 /* The transition time is in GMT. No correction to apply. */ ;
510 else if (isdst && !trans_type->isstd)
511 /* The type says this transition is in "local wall clock time", and
512 wall clock time as of the previous transition was DST. Correct
513 for the difference between the rule's DST offset and the user's
514 DST offset. */
515 transitions[i] += dstoff - rule_dstoff;
516 else
517 /* This transition is in "local wall clock time", and wall clock
518 time as of this iteration is non-DST. Correct for the
519 difference between the rule's standard offset and the user's
520 standard offset. */
521 transitions[i] += stdoff - rule_stdoff;
523 /* The DST state of "local wall clock time" for the next iteration is
524 as specified by this transition. */
525 isdst = trans_type->isdst;
528 /* Now that we adjusted the transitions to the requested offsets,
529 reset the rule_stdoff and rule_dstoff values appropriately. They
530 are used elsewhere. */
531 rule_stdoff = stdoff;
532 rule_dstoff = dstoff;
534 /* Reset types 0 and 1 to describe the user's settings. */
535 types[0].idx = 0;
536 types[0].offset = stdoff;
537 types[0].isdst = 0;
538 types[1].idx = stdlen;
539 types[1].offset = dstoff;
540 types[1].isdst = 1;
542 /* Reset the zone names to point to the user's names. */
543 __tzname[0] = (char *) std;
544 __tzname[1] = (char *) dst;
546 /* Set the timezone. */
547 __timezone = -types[0].offset;
549 compute_tzname_max (stdlen + dstlen);
552 void
553 __tzfile_compute (time_t timer, int use_localtime,
554 long int *leap_correct, int *leap_hit,
555 struct tm *tp)
557 register size_t i;
559 if (use_localtime)
561 __tzname[0] = NULL;
562 __tzname[1] = NULL;
564 if (num_transitions == 0 || timer < transitions[0])
566 /* TIMER is before any transition (or there are no transitions).
567 Choose the first non-DST type
568 (or the first if they're all DST types). */
569 i = 0;
570 while (i < num_types && types[i].isdst)
572 if (__tzname[1] == NULL)
573 __tzname[1] = __tzstring (&zone_names[types[i].idx]);
575 ++i;
578 if (i == num_types)
579 i = 0;
580 __tzname[0] = __tzstring (&zone_names[types[i].idx]);
581 if (__tzname[1] == NULL)
583 size_t j = i;
584 while (j < num_types)
585 if (types[j].isdst)
587 __tzname[1] = __tzstring (&zone_names[types[j].idx]);
588 break;
590 else
591 ++j;
594 else if (timer >= transitions[num_transitions - 1])
596 if (tzspec == NULL)
598 use_last:
599 i = num_transitions - 1;
600 goto found;
603 /* Parse the POSIX TZ-style string. */
604 __tzset_parse_tz (tzspec);
606 /* Convert to broken down structure. If this fails do not
607 use the string. */
608 if (! __offtime (&timer, 0, tp))
609 goto use_last;
611 /* Use the rules from the TZ string to compute the change. */
612 __tz_compute (timer, tp, 1);
614 *leap_correct = 0L;
615 *leap_hit = 0;
616 return;
618 else
620 /* Find the first transition after TIMER, and
621 then pick the type of the transition before it. */
622 size_t lo = 0;
623 size_t hi = num_transitions - 1;
624 /* Assume that DST is changing twice a year and guess initial
625 search spot from it.
626 Half of a gregorian year has on average 365.2425 * 86400 / 2
627 = 15778476 seconds. */
628 i = (transitions[num_transitions - 1] - timer) / 15778476;
629 if (i < num_transitions)
631 i = num_transitions - 1 - i;
632 if (timer < transitions[i])
634 if (i < 10 || timer >= transitions[i - 10])
636 /* Linear search. */
637 while (timer < transitions[i - 1])
638 --i;
639 goto found;
641 hi = i - 10;
643 else
645 if (i + 10 >= num_transitions || timer < transitions[i + 10])
647 /* Linear search. */
648 while (timer >= transitions[i])
649 ++i;
650 goto found;
652 lo = i + 10;
656 /* Binary search. */
657 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
658 while (lo + 1 < hi)
660 i = (lo + hi) / 2;
661 if (timer < transitions[i])
662 hi = i;
663 else
664 lo = i;
666 i = hi;
668 found:
669 /* assert (timer >= transitions[i - 1] && timer < transitions[i]); */
670 __tzname[types[type_idxs[i - 1]].isdst]
671 = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
672 size_t j = i;
673 while (j < num_transitions)
675 int type = type_idxs[j];
676 int dst = types[type].isdst;
677 int idx = types[type].idx;
679 if (__tzname[dst] == NULL)
681 __tzname[dst] = __tzstring (&zone_names[idx]);
683 if (__tzname[1 - dst] != NULL)
684 break;
687 ++j;
690 i = type_idxs[i - 1];
693 struct ttinfo *info = &types[i];
694 __daylight = rule_stdoff != rule_dstoff;
695 __timezone = -rule_stdoff;
697 if (__tzname[0] == NULL)
699 /* This should only happen if there are no transition rules.
700 In this case there should be only one single type. */
701 assert (num_types == 1);
702 __tzname[0] = __tzstring (zone_names);
704 if (__tzname[1] == NULL)
705 /* There is no daylight saving time. */
706 __tzname[1] = __tzname[0];
707 tp->tm_isdst = info->isdst;
708 assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
709 tp->tm_zone = __tzname[tp->tm_isdst];
710 tp->tm_gmtoff = info->offset;
713 *leap_correct = 0L;
714 *leap_hit = 0;
716 /* Find the last leap second correction transition time before TIMER. */
717 i = num_leaps;
719 if (i-- == 0)
720 return;
721 while (timer < leaps[i].transition);
723 /* Apply its correction. */
724 *leap_correct = leaps[i].change;
726 if (timer == leaps[i].transition && /* Exactly at the transition time. */
727 ((i == 0 && leaps[i].change > 0) ||
728 leaps[i].change > leaps[i - 1].change))
730 *leap_hit = 1;
731 while (i > 0
732 && leaps[i].transition == leaps[i - 1].transition + 1
733 && leaps[i].change == leaps[i - 1].change + 1)
735 ++*leap_hit;
736 --i;
741 static void
742 internal_function
743 compute_tzname_max (size_t chars)
745 const char *p;
747 p = zone_names;
750 const char *start = p;
751 while (*p != '\0')
752 ++p;
753 if ((size_t) (p - start) > __tzname_cur_max)
754 __tzname_cur_max = p - start;
756 while (++p < &zone_names[chars]);