2003-05-04 Roland McGrath <roland@redhat.com>
[glibc.git] / time / tzfile.c
blob39b3c2f21180898421213fd590807a8e06a459e9
1 /* Copyright (C) 1991-1993, 1995-2001, 2003 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <assert.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdio_ext.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
28 #define NOID
29 #include <timezone/tzfile.h>
31 int __use_tzfile;
33 struct ttinfo
35 long int offset; /* Seconds east of GMT. */
36 unsigned char isdst; /* Used to set tm_isdst. */
37 unsigned char idx; /* Index into `zone_names'. */
38 unsigned char isstd; /* Transition times are in standard time. */
39 unsigned char isgmt; /* Transition times are in GMT. */
42 struct leap
44 time_t transition; /* Time the transition takes effect. */
45 long int change; /* Seconds of correction to apply. */
48 static struct ttinfo *find_transition (time_t timer) internal_function;
49 static void compute_tzname_max (size_t) internal_function;
51 static size_t num_transitions;
52 libc_freeres_ptr (static time_t *transitions);
53 static unsigned char *type_idxs;
54 static size_t num_types;
55 static struct ttinfo *types;
56 static char *zone_names;
57 static long int rule_stdoff;
58 static long int rule_dstoff;
59 static size_t num_leaps;
60 static struct leap *leaps;
62 #include <endian.h>
63 #include <byteswap.h>
65 /* Decode the four bytes at PTR as a signed integer in network byte order. */
66 static inline int
67 decode (const void *ptr)
69 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
70 return *(const int *) ptr;
71 else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
72 return bswap_32 (*(const int *) ptr);
73 else
75 const unsigned char *p = ptr;
76 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
78 result = (result << 8) | *p++;
79 result = (result << 8) | *p++;
80 result = (result << 8) | *p++;
81 result = (result << 8) | *p++;
83 return result;
87 void
88 __tzfile_read (const char *file, size_t extra, char **extrap)
90 static const char default_tzdir[] = TZDIR;
91 size_t num_isstd, num_isgmt;
92 register FILE *f;
93 struct tzhead tzhead;
94 size_t chars;
95 register size_t i;
96 size_t total_size;
97 size_t types_idx;
98 size_t leaps_idx;
100 __use_tzfile = 0;
102 if (transitions != NULL)
103 free ((void *) transitions);
104 transitions = NULL;
106 if (file == NULL)
107 /* No user specification; use the site-wide default. */
108 file = TZDEFAULT;
109 else if (*file == '\0')
110 /* User specified the empty string; use UTC with no leap seconds. */
111 return;
112 else
114 /* We must not allow to read an arbitrary file in a setuid
115 program. So we fail for any file which is not in the
116 directory hierachy starting at TZDIR
117 and which is not the system wide default TZDEFAULT. */
118 if (__libc_enable_secure
119 && ((*file == '/'
120 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
121 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
122 || strstr (file, "../") != NULL))
123 /* This test is certainly a bit too restrictive but it should
124 catch all critical cases. */
125 return;
128 if (*file != '/')
130 const char *tzdir;
131 unsigned int len, tzdir_len;
132 char *new, *tmp;
134 tzdir = getenv ("TZDIR");
135 if (tzdir == NULL || *tzdir == '\0')
137 tzdir = default_tzdir;
138 tzdir_len = sizeof (default_tzdir) - 1;
140 else
141 tzdir_len = strlen (tzdir);
142 len = strlen (file) + 1;
143 new = (char *) __alloca (tzdir_len + 1 + len);
144 tmp = __mempcpy (new, tzdir, tzdir_len);
145 *tmp++ = '/';
146 memcpy (tmp, file, len);
147 file = new;
150 f = fopen (file, "r");
151 if (f == NULL)
152 return;
154 /* No threads reading this stream. */
155 __fsetlocking (f, FSETLOCKING_BYCALLER);
157 if (fread_unlocked ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
158 goto lose;
160 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
161 num_types = (size_t) decode (tzhead.tzh_typecnt);
162 chars = (size_t) decode (tzhead.tzh_charcnt);
163 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
164 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
165 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
167 total_size = num_transitions * (sizeof (time_t) + 1);
168 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
169 & ~(__alignof__ (struct ttinfo) - 1));
170 types_idx = total_size;
171 total_size += num_types * sizeof (struct ttinfo) + chars;
172 total_size = ((total_size + __alignof__ (struct leap) - 1)
173 & ~(__alignof__ (struct leap) - 1));
174 leaps_idx = total_size;
175 total_size += num_leaps * sizeof (struct leap);
176 /* This is for the extra memory required by the caller. */
177 total_size += extra;
179 transitions = (time_t *) malloc (total_size);
180 if (transitions == NULL)
181 goto lose;
183 type_idxs = (unsigned char *) transitions + (num_transitions
184 * sizeof (time_t));
185 types = (struct ttinfo *) ((char *) transitions + types_idx);
186 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
187 leaps = (struct leap *) ((char *) transitions + leaps_idx);
188 if (extra > 0)
189 *extrap = (char *) &leaps[num_leaps];
191 if (sizeof (time_t) < 4)
192 abort ();
194 if (sizeof (time_t) == 4)
196 if (fread_unlocked (transitions, 1, (4 + 1) * num_transitions, f)
197 != (4 + 1) * num_transitions)
198 goto lose;
200 else
202 if (fread_unlocked (transitions, 4, num_transitions, f)
203 != num_transitions
204 || fread_unlocked (type_idxs, 1, num_transitions, f)
205 != num_transitions)
206 goto lose;
209 /* Check for bogus indices in the data file, so we can hereafter
210 safely use type_idxs[T] as indices into `types' and never crash. */
211 for (i = 0; i < num_transitions; ++i)
212 if (type_idxs[i] >= num_types)
213 goto lose;
215 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
217 /* Decode the transition times, stored as 4-byte integers in
218 network (big-endian) byte order. We work from the end of
219 the array so as not to clobber the next element to be
220 processed when sizeof (time_t) > 4. */
221 i = num_transitions;
222 while (i-- > 0)
223 transitions[i] = decode ((char *) transitions + i * 4);
226 for (i = 0; i < num_types; ++i)
228 unsigned char x[4];
229 int c;
230 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
231 goto lose;
232 c = getc_unlocked (f);
233 if ((unsigned int) c > 1u)
234 goto lose;
235 types[i].isdst = c;
236 c = getc_unlocked (f);
237 if ((size_t) c > chars) /* Bogus index in data file. */
238 goto lose;
239 types[i].idx = c;
240 types[i].offset = (long int) decode (x);
243 if (fread_unlocked (zone_names, 1, chars, f) != chars)
244 goto lose;
246 for (i = 0; i < num_leaps; ++i)
248 unsigned char x[4];
249 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
250 goto lose;
251 leaps[i].transition = (time_t) decode (x);
252 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
253 goto lose;
254 leaps[i].change = (long int) decode (x);
257 for (i = 0; i < num_isstd; ++i)
259 int c = getc_unlocked (f);
260 if (c == EOF)
261 goto lose;
262 types[i].isstd = c != 0;
264 while (i < num_types)
265 types[i++].isstd = 0;
267 for (i = 0; i < num_isgmt; ++i)
269 int c = getc_unlocked (f);
270 if (c == EOF)
271 goto lose;
272 types[i].isgmt = c != 0;
274 while (i < num_types)
275 types[i++].isgmt = 0;
277 fclose (f);
279 /* First "register" all timezone names. */
280 for (i = 0; i < num_types; ++i)
281 (void) __tzstring (&zone_names[types[i].idx]);
283 /* Find the standard and daylight time offsets used by the rule file.
284 We choose the offsets in the types of each flavor that are
285 transitioned to earliest in time. */
286 __tzname[0] = NULL;
287 __tzname[1] = NULL;
288 for (i = num_transitions; i > 0; )
290 int type = type_idxs[--i];
291 int dst = types[type].isdst;
293 if (__tzname[dst] == NULL)
295 int idx = types[type].idx;
297 __tzname[dst] = __tzstring (&zone_names[idx]);
299 if (__tzname[1 - dst] != NULL)
300 break;
303 if (__tzname[0] == NULL)
305 /* This should only happen if there are no transition rules.
306 In this case there should be only one single type. */
307 assert (num_types == 1);
308 __tzname[0] = __tzstring (zone_names);
310 if (__tzname[1] == NULL)
311 __tzname[1] = __tzname[0];
313 compute_tzname_max (chars);
315 if (num_transitions == 0)
316 /* Use the first rule (which should also be the only one). */
317 rule_stdoff = rule_dstoff = types[0].offset;
318 else
320 int stdoff_set = 0, dstoff_set = 0;
321 rule_stdoff = rule_dstoff = 0;
322 i = num_transitions - 1;
325 if (!stdoff_set && !types[type_idxs[i]].isdst)
327 stdoff_set = 1;
328 rule_stdoff = types[type_idxs[i]].offset;
330 else if (!dstoff_set && types[type_idxs[i]].isdst)
332 dstoff_set = 1;
333 rule_dstoff = types[type_idxs[i]].offset;
335 if (stdoff_set && dstoff_set)
336 break;
338 while (i-- > 0);
340 if (!dstoff_set)
341 rule_dstoff = rule_stdoff;
344 __daylight = rule_stdoff != rule_dstoff;
345 __timezone = -rule_stdoff;
347 __use_tzfile = 1;
348 return;
350 lose:
351 fclose (f);
354 /* The user specified a hand-made timezone, but not its DST rules.
355 We will use the names and offsets from the user, and the rules
356 from the TZDEFRULES file. */
358 void
359 __tzfile_default (const char *std, const char *dst,
360 long int stdoff, long int dstoff)
362 size_t stdlen = strlen (std) + 1;
363 size_t dstlen = strlen (dst) + 1;
364 size_t i;
365 int isdst;
366 char *cp;
368 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
369 if (!__use_tzfile)
370 return;
372 if (num_types < 2)
374 __use_tzfile = 0;
375 return;
378 /* Ignore the zone names read from the file and use the given ones
379 instead. */
380 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
381 zone_names = cp;
383 /* Now there are only two zones, regardless of what the file contained. */
384 num_types = 2;
386 /* Now correct the transition times for the user-specified standard and
387 daylight offsets from GMT. */
388 isdst = 0;
389 for (i = 0; i < num_transitions; ++i)
391 struct ttinfo *trans_type = &types[type_idxs[i]];
393 /* We will use only types 0 (standard) and 1 (daylight).
394 Fix up this transition to point to whichever matches
395 the flavor of its original type. */
396 type_idxs[i] = trans_type->isdst;
398 if (trans_type->isgmt)
399 /* The transition time is in GMT. No correction to apply. */ ;
400 else if (isdst && !trans_type->isstd)
401 /* The type says this transition is in "local wall clock time", and
402 wall clock time as of the previous transition was DST. Correct
403 for the difference between the rule's DST offset and the user's
404 DST offset. */
405 transitions[i] += dstoff - rule_dstoff;
406 else
407 /* This transition is in "local wall clock time", and wall clock
408 time as of this iteration is non-DST. Correct for the
409 difference between the rule's standard offset and the user's
410 standard offset. */
411 transitions[i] += stdoff - rule_stdoff;
413 /* The DST state of "local wall clock time" for the next iteration is
414 as specified by this transition. */
415 isdst = trans_type->isdst;
418 /* Reset types 0 and 1 to describe the user's settings. */
419 types[0].idx = 0;
420 types[0].offset = stdoff;
421 types[0].isdst = 0;
422 types[1].idx = stdlen;
423 types[1].offset = dstoff;
424 types[1].isdst = 1;
426 /* Reset the zone names to point to the user's names. */
427 __tzname[0] = (char *) std;
428 __tzname[1] = (char *) dst;
430 /* Set the timezone. */
431 __timezone = -types[0].offset;
433 compute_tzname_max (stdlen + dstlen);
436 static struct ttinfo *
437 internal_function
438 find_transition (time_t timer)
440 size_t i;
442 if (num_transitions == 0 || timer < transitions[0])
444 /* TIMER is before any transition (or there are no transitions).
445 Choose the first non-DST type
446 (or the first if they're all DST types). */
447 i = 0;
448 while (i < num_types && types[i].isdst)
449 ++i;
450 if (i == num_types)
451 i = 0;
453 else
455 /* Find the first transition after TIMER, and
456 then pick the type of the transition before it. */
457 for (i = 1; i < num_transitions; ++i)
458 if (timer < transitions[i])
459 break;
460 i = type_idxs[i - 1];
463 return &types[i];
466 void
467 __tzfile_compute (time_t timer, int use_localtime,
468 long int *leap_correct, int *leap_hit,
469 struct tm *tp)
471 register size_t i;
473 if (use_localtime)
475 struct ttinfo *info = find_transition (timer);
476 __daylight = rule_stdoff != rule_dstoff;
477 __timezone = -rule_stdoff;
478 __tzname[0] = NULL;
479 __tzname[1] = NULL;
480 for (i = num_transitions; i > 0; )
482 int type = type_idxs[--i];
483 int dst = types[type].isdst;
484 int idx = types[type].idx;
486 if (__tzname[dst] == NULL)
488 __tzname[dst] = __tzstring (&zone_names[idx]);
490 if (__tzname[1 - dst] != NULL)
491 break;
494 if (__tzname[0] == NULL)
496 /* This should only happen if there are no transition rules.
497 In this case there should be only one single type. */
498 assert (num_types == 1);
499 __tzname[0] = __tzstring (zone_names);
501 if (__tzname[1] == NULL)
502 /* There is no daylight saving time. */
503 __tzname[1] = __tzname[0];
504 tp->tm_isdst = info->isdst;
505 tp->tm_zone = __tzstring (&zone_names[info->idx]);
506 tp->tm_gmtoff = info->offset;
509 *leap_correct = 0L;
510 *leap_hit = 0;
512 /* Find the last leap second correction transition time before TIMER. */
513 i = num_leaps;
515 if (i-- == 0)
516 return;
517 while (timer < leaps[i].transition);
519 /* Apply its correction. */
520 *leap_correct = leaps[i].change;
522 if (timer == leaps[i].transition && /* Exactly at the transition time. */
523 ((i == 0 && leaps[i].change > 0) ||
524 leaps[i].change > leaps[i - 1].change))
526 *leap_hit = 1;
527 while (i > 0
528 && leaps[i].transition == leaps[i - 1].transition + 1
529 && leaps[i].change == leaps[i - 1].change + 1)
531 ++*leap_hit;
532 --i;
537 static void
538 internal_function
539 compute_tzname_max (size_t chars)
541 const char *p;
543 p = zone_names;
546 const char *start = p;
547 while (*p != '\0')
548 ++p;
549 if ((size_t) (p - start) > __tzname_cur_max)
550 __tzname_cur_max = p - start;
552 while (++p < &zone_names[chars]);