1 /* Copyright (C) 1991, 92, 93, 95, 96, 97, 98 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
28 #include <timezone/tzfile.h>
34 long int offset; /* Seconds east of GMT. */
35 unsigned char isdst; /* Used to set tm_isdst. */
36 unsigned char idx; /* Index into `zone_names'. */
37 unsigned char isstd; /* Transition times are in standard time. */
38 unsigned char isgmt; /* Transition times are in GMT. */
43 time_t transition; /* Time the transition takes effect. */
44 long int change; /* Seconds of correction to apply. */
47 extern char * __tzstring (const char *); /* Defined in tzset.c. */
49 static struct ttinfo *find_transition (time_t timer) internal_function;
50 static void compute_tzname_max (size_t) internal_function;
52 static size_t num_transitions;
53 static time_t *transitions = NULL;
54 static unsigned char *type_idxs = NULL;
55 static size_t num_types;
56 static struct ttinfo *types = NULL;
57 static char *zone_names = NULL;
58 static long int rule_stdoff;
59 static long int rule_dstoff;
60 static size_t num_leaps;
61 static struct leap *leaps = NULL;
66 /* Decode the four bytes at PTR as a signed integer in network byte order. */
68 decode (const void *ptr)
70 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
71 return *(const int *) ptr;
72 else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
73 return bswap_32 (*(const int *) ptr);
76 const unsigned char *p = ptr;
77 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
79 result = (result << 8) | *p++;
80 result = (result << 8) | *p++;
81 result = (result << 8) | *p++;
82 result = (result << 8) | *p++;
89 __tzfile_read (const char *file)
91 static const char default_tzdir[] = TZDIR;
92 size_t num_isstd, num_isgmt;
100 if (transitions != NULL)
101 free ((void *) transitions);
103 if (type_idxs != NULL)
104 free ((void *) type_idxs);
107 free ((void *) types);
109 if (zone_names != NULL)
110 free ((void *) zone_names);
113 free ((void *) leaps);
117 /* No user specification; use the site-wide default. */
119 else if (*file == '\0')
120 /* User specified the empty string; use UTC with no leap seconds. */
124 /* We must not allow to read an arbitrary file in a setuid
125 program. So we fail for any file which is not in the
126 directory hierachy starting at TZDIR
127 and which is not the system wide default TZDEFAULT. */
128 if (__libc_enable_secure
130 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
131 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
132 || strstr (file, "../") != NULL))
133 /* This test is certainly a bit too restrictive but it should
134 catch all critical cases. */
141 unsigned int len, tzdir_len;
144 tzdir = __secure_getenv ("TZDIR");
145 if (tzdir == NULL || *tzdir == '\0')
147 tzdir = default_tzdir;
148 tzdir_len = sizeof (default_tzdir) - 1;
151 tzdir_len = strlen (tzdir);
152 len = strlen (file) + 1;
153 new = (char *) __alloca (tzdir_len + 1 + len);
154 tmp = __mempcpy (new, tzdir, tzdir_len);
156 __mempcpy (tmp, file, len);
160 f = fopen (file, "r");
164 if (fread_unlocked ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
167 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
168 num_types = (size_t) decode (tzhead.tzh_typecnt);
169 chars = (size_t) decode (tzhead.tzh_charcnt);
170 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
171 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
172 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
174 if (num_transitions > 0)
176 transitions = (time_t *) malloc (num_transitions * sizeof (time_t));
177 if (transitions == NULL)
179 type_idxs = (unsigned char *) malloc (num_transitions);
180 if (type_idxs == NULL)
185 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
191 zone_names = (char *) malloc (chars);
192 if (zone_names == NULL)
197 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
202 if (sizeof (time_t) < 4)
205 if (fread_unlocked (transitions, 4, num_transitions, f) != num_transitions
206 || fread_unlocked (type_idxs, 1, num_transitions, f) != num_transitions)
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)
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. */
223 transitions[i] = decode ((char *) transitions + i * 4);
226 for (i = 0; i < num_types; ++i)
229 if (fread_unlocked (x, 1, 4, f) != 4
230 || fread_unlocked (&types[i].isdst, 1, 1, f) != 1
231 || fread_unlocked (&types[i].idx, 1, 1, f) != 1)
233 if (types[i].isdst > 1)
235 if (types[i].idx >= chars) /* Bogus index in data file. */
237 types[i].offset = (long int) decode (x);
240 if (fread_unlocked (zone_names, 1, chars, f) != chars)
243 for (i = 0; i < num_leaps; ++i)
246 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
248 leaps[i].transition = (time_t) decode (x);
249 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
251 leaps[i].change = (long int) decode (x);
254 for (i = 0; i < num_isstd; ++i)
256 int c = getc_unlocked (f);
259 types[i].isstd = c != 0;
261 while (i < num_types)
262 types[i++].isstd = 0;
264 for (i = 0; i < num_isgmt; ++i)
266 int c = getc_unlocked (f);
269 types[i].isgmt = c != 0;
271 while (i < num_types)
272 types[i++].isgmt = 0;
276 /* First "register" all timezone names. */
277 for (i = 0; i < num_types; ++i)
278 (void) __tzstring (&zone_names[types[i].idx]);
280 /* Find the standard and daylight time offsets used by the rule file.
281 We choose the offsets in the types of each flavor that are
282 transitioned to earliest in time. */
285 for (i = num_transitions; i > 0; )
287 int type = type_idxs[--i];
288 int dst = types[type].isdst;
289 int idx = types[type].idx;
291 if (__tzname[dst] == NULL)
293 __tzname[dst] = __tzstring (&zone_names[idx]);
295 if (__tzname[1 - dst] != NULL)
299 if (__tzname[0] == NULL)
301 /* This should only happen if there are no transition rules.
302 In this case there should be only one single type. */
303 assert (num_types == 1);
304 __tzname[0] = __tzstring (zone_names);
306 if (__tzname[1] == NULL)
307 __tzname[1] = __tzname[0];
309 compute_tzname_max (chars);
311 if (num_transitions == 0)
312 /* Use the first rule (which should also be the only one). */
313 rule_stdoff = rule_dstoff = types[0].offset;
316 rule_stdoff = rule_dstoff = 0;
317 for (i = 0; i < num_transitions; ++i)
319 if (!rule_stdoff && !types[type_idxs[i]].isdst)
320 rule_stdoff = types[type_idxs[i]].offset;
321 if (!rule_dstoff && types[type_idxs[i]].isdst)
322 rule_dstoff = types[type_idxs[i]].offset;
323 if (rule_stdoff && rule_dstoff)
328 __daylight = rule_stdoff != rule_dstoff;
329 __timezone = -rule_stdoff;
338 /* The user specified a hand-made timezone, but not its DST rules.
339 We will use the names and offsets from the user, and the rules
340 from the TZDEFRULES file. */
343 __tzfile_default (const char *std, const char *dst,
344 long int stdoff, long int dstoff)
346 size_t stdlen, dstlen, i;
349 __tzfile_read (TZDEFRULES);
359 /* Ignore the zone names read from the file. */
362 /* Use the names the user specified. */
363 stdlen = strlen (std) + 1;
364 dstlen = strlen (dst) + 1;
365 zone_names = malloc (stdlen + dstlen);
366 if (zone_names == NULL)
371 __mempcpy (__mempcpy (zone_names, std, stdlen), dst, dstlen);
373 /* Now there are only two zones, regardless of what the file contained. */
376 /* Now correct the transition times for the user-specified standard and
377 daylight offsets from GMT. */
379 for (i = 0; i < num_transitions; ++i)
381 struct ttinfo *trans_type = &types[type_idxs[i]];
383 /* We will use only types 0 (standard) and 1 (daylight).
384 Fix up this transition to point to whichever matches
385 the flavor of its original type. */
386 type_idxs[i] = trans_type->isdst;
388 if (trans_type->isgmt)
389 /* The transition time is in GMT. No correction to apply. */ ;
390 else if (isdst && !trans_type->isstd)
391 /* The type says this transition is in "local wall clock time", and
392 wall clock time as of the previous transition was DST. Correct
393 for the difference between the rule's DST offset and the user's
395 transitions[i] += dstoff - rule_dstoff;
397 /* This transition is in "local wall clock time", and wall clock
398 time as of this iteration is non-DST. Correct for the
399 difference between the rule's standard offset and the user's
401 transitions[i] += stdoff - rule_stdoff;
403 /* The DST state of "local wall clock time" for the next iteration is
404 as specified by this transition. */
405 isdst = trans_type->isdst;
408 /* Reset types 0 and 1 to describe the user's settings. */
410 types[0].offset = stdoff;
412 types[1].idx = stdlen;
413 types[1].offset = dstoff;
416 /* Reset the zone names to point to the user's names. */
417 __tzname[0] = (char *) std;
418 __tzname[1] = (char *) dst;
420 /* Set the timezone. */
421 __timezone = -types[0].offset;
423 compute_tzname_max (stdlen + dstlen);
426 static struct ttinfo *
428 find_transition (time_t timer)
432 if (num_transitions == 0 || timer < transitions[0])
434 /* TIMER is before any transition (or there are no transitions).
435 Choose the first non-DST type
436 (or the first if they're all DST types). */
438 while (i < num_types && types[i].isdst)
445 /* Find the first transition after TIMER, and
446 then pick the type of the transition before it. */
447 for (i = 1; i < num_transitions; ++i)
448 if (timer < transitions[i])
450 i = type_idxs[i - 1];
457 __tzfile_compute (time_t timer, int use_localtime,
458 long int *leap_correct, int *leap_hit,
465 struct ttinfo *info = find_transition (timer);
466 __daylight = rule_stdoff != rule_dstoff;
467 __timezone = -rule_stdoff;
470 for (i = num_transitions; i > 0; )
472 int type = type_idxs[--i];
473 int dst = types[type].isdst;
474 int idx = types[type].idx;
476 if (__tzname[dst] == NULL)
478 __tzname[dst] = __tzstring (&zone_names[idx]);
480 if (__tzname[1 - dst] != NULL)
484 if (__tzname[0] == NULL)
486 /* This should only happen if there are no transition rules.
487 In this case there should be only one single type. */
488 assert (num_types == 1);
489 __tzname[0] = __tzstring (zone_names);
491 if (__tzname[1] == NULL)
492 /* There is no daylight saving time. */
493 __tzname[1] = __tzname[0];
494 tp->tm_isdst = info->isdst;
495 tp->tm_zone = &zone_names[info->idx];
496 tp->tm_gmtoff = info->offset;
502 /* Find the last leap second correction transition time before TIMER. */
507 while (timer < leaps[i].transition);
509 /* Apply its correction. */
510 *leap_correct = leaps[i].change;
512 if (timer == leaps[i].transition && /* Exactly at the transition time. */
513 ((i == 0 && leaps[i].change > 0) ||
514 leaps[i].change > leaps[i - 1].change))
518 && leaps[i].transition == leaps[i - 1].transition + 1
519 && leaps[i].change == leaps[i - 1].change + 1)
531 compute_tzname_max (size_t chars)
533 extern size_t __tzname_cur_max; /* Defined in tzset.c. */
540 const char *start = p;
543 if ((size_t) (p - start) > __tzname_cur_max)
544 __tzname_cur_max = p - start;
546 while (++p < &zone_names[chars]);