1 /* Copyright (C) 1991, 92, 93, 95, 96, 97 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. */
33 long int offset; /* Seconds east of GMT. */
34 unsigned char isdst; /* Used to set tm_isdst. */
35 unsigned char idx; /* Index into `zone_names'. */
36 unsigned char isstd; /* Transition times are in standard time. */
37 unsigned char isgmt; /* Transition times are in GMT. */
42 time_t transition; /* Time the transition takes effect. */
43 long int change; /* Seconds of correction to apply. */
46 static struct ttinfo *find_transition (time_t timer);
47 static void compute_tzname_max (size_t);
49 static size_t num_transitions;
50 static time_t *transitions = NULL;
51 static unsigned char *type_idxs = NULL;
52 static size_t num_types;
53 static struct ttinfo *types = NULL;
54 static char *zone_names = NULL;
55 static size_t num_leaps;
56 static struct leap *leaps = NULL;
60 /* Decode the four bytes at PTR as a signed integer in network byte order. */
62 decode (const void *ptr)
64 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
65 return *(const int *) ptr;
68 const unsigned char *p = ptr;
69 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
71 result = (result << 8) | *p++;
72 result = (result << 8) | *p++;
73 result = (result << 8) | *p++;
74 result = (result << 8) | *p++;
81 __tzfile_read (const char *file)
83 static const char default_tzdir[] = TZDIR;
84 size_t num_isstd, num_isgmt;
93 if (transitions != NULL)
94 free ((void *) transitions);
96 if (type_idxs != NULL)
97 free ((void *) type_idxs);
100 free ((void *) types);
102 if (zone_names != NULL)
103 free ((void *) zone_names);
106 free ((void *) leaps);
110 /* No user specification; use the site-wide default. */
112 else if (*file == '\0')
113 /* User specified the empty string; use UTC explicitly. */
117 /* We must not allow to read an arbitrary file in a setuid
118 program. So we fail for any file which is not in the
119 directory hierachy starting at TZDIR
120 and which is not the system wide default TZDEFAULT. */
121 if (__libc_enable_secure
123 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
124 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
125 || strstr (file, "../") != NULL))
126 /* This test is certainly a bit too restrictive but it should
127 catch all critical cases. */
134 unsigned int len, tzdir_len;
137 tzdir = __secure_getenv ("TZDIR");
138 if (tzdir == NULL || *tzdir == '\0')
140 tzdir = default_tzdir;
141 tzdir_len = sizeof (default_tzdir) - 1;
144 tzdir_len = strlen (tzdir);
145 len = strlen (file) + 1;
146 new = (char *) __alloca (tzdir_len + 1 + len);
147 memcpy (new, tzdir, tzdir_len);
148 new[tzdir_len] = '/';
149 memcpy (&new[tzdir_len + 1], file, len);
153 f = fopen (file, "r");
157 if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
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 if (num_transitions > 0)
169 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
170 if (transitions == NULL)
172 type_idxs = (unsigned char *) malloc (num_transitions);
173 if (type_idxs == NULL)
178 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
184 zone_names = (char *) malloc (chars);
185 if (zone_names == NULL)
190 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
195 if (sizeof (time_t) < 4)
198 if (fread(transitions, 4, num_transitions, f) != num_transitions ||
199 fread(type_idxs, 1, num_transitions, f) != num_transitions)
202 /* Check for bogus indices in the data file, so we can hereafter
203 safely use type_idxs[T] as indices into `types' and never crash. */
204 for (i = 0; i < num_transitions; ++i)
205 if (type_idxs[i] >= num_types)
208 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
210 /* Decode the transition times, stored as 4-byte integers in
211 network (big-endian) byte order. We work from the end of
212 the array so as not to clobber the next element to be
213 processed when sizeof (time_t) > 4. */
216 transitions[i] = decode ((char *) transitions + i*4);
219 for (i = 0; i < num_types; ++i)
222 if (fread (x, 1, 4, f) != 4 ||
223 fread (&types[i].isdst, 1, 1, f) != 1 ||
224 fread (&types[i].idx, 1, 1, f) != 1)
226 if (types[i].idx >= chars) /* Bogus index in data file. */
228 types[i].offset = (long int) decode (x);
231 if (fread (zone_names, 1, chars, f) != chars)
234 for (i = 0; i < num_leaps; ++i)
237 if (fread (x, 1, sizeof (x), f) != sizeof (x))
239 leaps[i].transition = (time_t) decode (x);
240 if (fread (x, 1, sizeof (x), f) != sizeof (x))
242 leaps[i].change = (long int) decode (x);
245 for (i = 0; i < num_isstd; ++i)
250 types[i].isstd = c != 0;
252 while (i < num_types)
253 types[i++].isstd = 0;
255 for (i = 0; i < num_isgmt; ++i)
260 types[i].isgmt = c != 0;
262 while (i < num_types)
263 types[i++].isgmt = 0;
267 info = find_transition (0);
268 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
270 __tzname[types[i].isdst] = &zone_names[types[i].idx];
271 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
272 __tzname[info->isdst] = &zone_names[info->idx];
274 compute_tzname_max (chars);
283 /* The user specified a hand-made timezone, but not its DST rules.
284 We will use the names and offsets from the user, and the rules
285 from the TZDEFRULES file. */
288 __tzfile_default (char *std, char *dst, long int stdoff, long int dstoff)
290 size_t stdlen, dstlen, i;
291 long int rule_offset, rule_stdoff, rule_dstoff;
294 __tzfile_read (TZDEFRULES);
304 /* Ignore the zone names read from the file. */
307 /* Use the names the user specified. */
308 stdlen = strlen (std) + 1;
309 dstlen = strlen (dst) + 1;
310 zone_names = malloc (stdlen + dstlen);
311 if (zone_names == NULL)
316 memcpy (zone_names, std, stdlen);
317 memcpy (&zone_names[stdlen], dst, dstlen);
319 /* Find the standard and daylight time offsets used by the rule file.
320 We choose the offsets in the types of each flavor that are
321 transitioned to earliest in time. */
322 rule_stdoff = rule_dstoff = 0;
323 for (i = 0; i < num_transitions; ++i)
325 if (!rule_stdoff && !types[type_idxs[i]].isdst)
326 rule_stdoff = types[type_idxs[i]].offset;
327 if (!rule_dstoff && types[type_idxs[i]].isdst)
328 rule_dstoff = types[type_idxs[i]].offset;
329 if (rule_stdoff && rule_dstoff)
333 /* Now correct the transition times for the user-specified standard and
334 daylight offsets from GMT. */
336 rule_offset = rule_offset;
337 for (i = 0; i < num_transitions; ++i)
339 struct ttinfo *trans_type = &types[type_idxs[i]];
341 /* We will use only types 0 (standard) and 1 (daylight).
342 Fix up this transition to point to whichever matches
343 the flavor of its original type. */
344 type_idxs[i] = trans_type->isdst;
346 if (trans_type->isgmt)
347 /* The transition time is in GMT. No correction to apply. */ ;
348 else if (isdst && !trans_type->isstd)
349 /* The type says this transition is in "local wall clock time", and
350 wall clock time as of the previous transition was DST. Correct
351 for the difference between the rule's DST offset and the user's
353 transitions[i] += dstoff - rule_dstoff;
355 /* This transition is in "local wall clock time", and wall clock
356 time as of this iteration is non-DST. Correct for the
357 difference between the rule's standard offset and the user's
359 transitions[i] += stdoff - rule_stdoff;
361 /* The DST state of "local wall clock time" for the next iteration is
362 as specified by this transition. */
363 isdst = trans_type->isdst;
366 /* Reset types 0 and 1 to describe the user's settings. */
368 types[0].offset = stdoff;
370 types[1].idx = stdlen;
371 types[1].offset = dstoff;
374 /* Reset the zone names to point to the user's names. */
375 __tzname[0] = &zone_names[0];
376 __tzname[1] = &zone_names[stdlen];
378 compute_tzname_max (stdlen + dstlen);
381 static struct ttinfo *
382 find_transition (time_t timer)
386 if (num_transitions == 0 || timer < transitions[0])
388 /* TIMER is before any transition (or there are no transitions).
389 Choose the first non-DST type
390 (or the first if they're all DST types). */
392 while (i < num_types && types[i].isdst)
399 /* Find the first transition after TIMER, and
400 then pick the type of the transition before it. */
401 for (i = 1; i < num_transitions; ++i)
402 if (timer < transitions[i])
404 i = type_idxs[i - 1];
411 __tzfile_compute (time_t timer, long int *leap_correct, int *leap_hit)
416 info = find_transition (timer);
417 __daylight = info->isdst;
418 __timezone = info->offset;
419 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
421 __tzname[types[i].isdst] = &zone_names[types[i].idx];
422 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
423 __tzname[info->isdst] = &zone_names[info->idx];
428 /* Find the last leap second correction transition time before TIMER. */
433 while (timer < leaps[i].transition);
435 /* Apply its correction. */
436 *leap_correct = leaps[i].change;
438 if (timer == leaps[i].transition && /* Exactly at the transition time. */
439 ((i == 0 && leaps[i].change > 0) ||
440 leaps[i].change > leaps[i - 1].change))
444 leaps[i].transition == leaps[i - 1].transition + 1 &&
445 leaps[i].change == leaps[i - 1].change + 1)
456 compute_tzname_max (size_t chars)
458 extern size_t __tzname_cur_max; /* Defined in __tzset.c. */
465 const char *start = p;
468 if ((size_t) (p - start) > __tzname_cur_max)
469 __tzname_cur_max = p - start;
470 } while (++p < &zone_names[chars]);