Update.
[glibc.git] / time / tzfile.c
blobe723892405dad54c816a35b31a4d8a7753a42bd8
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. */
19 #include <assert.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
27 #define NOID
28 #include <timezone/tzfile.h>
30 int __use_tzfile = 0;
32 struct ttinfo
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. */
41 struct leap
43 time_t transition; /* Time the transition takes effect. */
44 long int change; /* Seconds of correction to apply. */
47 static struct ttinfo *find_transition (time_t timer) internal_function;
48 static void compute_tzname_max (size_t) internal_function;
50 static size_t num_transitions;
51 static time_t *transitions = NULL;
52 static unsigned char *type_idxs = NULL;
53 static size_t num_types;
54 static struct ttinfo *types = NULL;
55 static char *zone_names = NULL;
56 static long int rule_stdoff;
57 static long int rule_dstoff;
58 static size_t num_leaps;
59 static struct leap *leaps = NULL;
61 #include <endian.h>
62 #include <byteswap.h>
64 /* Decode the four bytes at PTR as a signed integer in network byte order. */
65 static inline int
66 decode (const void *ptr)
68 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
69 return *(const int *) ptr;
70 else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
71 return bswap_32 (*(const int *) ptr);
72 else
74 const unsigned char *p = ptr;
75 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
77 result = (result << 8) | *p++;
78 result = (result << 8) | *p++;
79 result = (result << 8) | *p++;
80 result = (result << 8) | *p++;
82 return result;
86 void
87 __tzfile_read (const char *file)
89 static const char default_tzdir[] = TZDIR;
90 size_t num_isstd, num_isgmt;
91 register FILE *f;
92 struct tzhead tzhead;
93 size_t chars;
94 register size_t i;
96 __use_tzfile = 0;
98 if (transitions != NULL)
99 free ((void *) transitions);
100 transitions = NULL;
101 if (type_idxs != NULL)
102 free ((void *) type_idxs);
103 type_idxs = NULL;
104 if (types != NULL)
105 free ((void *) types);
106 types = NULL;
107 if (zone_names != NULL)
108 free ((void *) zone_names);
109 zone_names = NULL;
110 if (leaps != NULL)
111 free ((void *) leaps);
112 leaps = NULL;
114 if (file == NULL)
115 /* No user specification; use the site-wide default. */
116 file = TZDEFAULT;
117 else if (*file == '\0')
118 /* User specified the empty string; use UTC with no leap seconds. */
119 return;
120 else
122 /* We must not allow to read an arbitrary file in a setuid
123 program. So we fail for any file which is not in the
124 directory hierachy starting at TZDIR
125 and which is not the system wide default TZDEFAULT. */
126 if (__libc_enable_secure
127 && ((*file == '/'
128 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
129 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
130 || strstr (file, "../") != NULL))
131 /* This test is certainly a bit too restrictive but it should
132 catch all critical cases. */
133 return;
136 if (*file != '/')
138 const char *tzdir;
139 unsigned int len, tzdir_len;
140 char *new, *tmp;
142 tzdir = __secure_getenv ("TZDIR");
143 if (tzdir == NULL || *tzdir == '\0')
145 tzdir = default_tzdir;
146 tzdir_len = sizeof (default_tzdir) - 1;
148 else
149 tzdir_len = strlen (tzdir);
150 len = strlen (file) + 1;
151 new = (char *) __alloca (tzdir_len + 1 + len);
152 tmp = __mempcpy (new, tzdir, tzdir_len);
153 *tmp++ = '/';
154 __mempcpy (tmp, file, len);
155 file = new;
158 f = fopen (file, "r");
159 if (f == NULL)
160 return;
162 if (fread_unlocked ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
163 goto lose;
165 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
166 num_types = (size_t) decode (tzhead.tzh_typecnt);
167 chars = (size_t) decode (tzhead.tzh_charcnt);
168 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
169 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
170 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
172 if (num_transitions > 0)
174 transitions = (time_t *) malloc (num_transitions * sizeof (time_t));
175 if (transitions == NULL)
176 goto lose;
177 type_idxs = (unsigned char *) malloc (num_transitions);
178 if (type_idxs == NULL)
179 goto lose;
181 if (num_types > 0)
183 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
184 if (types == NULL)
185 goto lose;
187 if (chars > 0)
189 zone_names = (char *) malloc (chars);
190 if (zone_names == NULL)
191 goto lose;
193 if (num_leaps > 0)
195 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
196 if (leaps == NULL)
197 goto lose;
200 if (sizeof (time_t) < 4)
201 abort ();
203 if (fread_unlocked (transitions, 4, num_transitions, f) != num_transitions
204 || fread_unlocked (type_idxs, 1, num_transitions, f) != num_transitions)
205 goto lose;
207 /* Check for bogus indices in the data file, so we can hereafter
208 safely use type_idxs[T] as indices into `types' and never crash. */
209 for (i = 0; i < num_transitions; ++i)
210 if (type_idxs[i] >= num_types)
211 goto lose;
213 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
215 /* Decode the transition times, stored as 4-byte integers in
216 network (big-endian) byte order. We work from the end of
217 the array so as not to clobber the next element to be
218 processed when sizeof (time_t) > 4. */
219 i = num_transitions;
220 while (i-- > 0)
221 transitions[i] = decode ((char *) transitions + i * 4);
224 for (i = 0; i < num_types; ++i)
226 unsigned char x[4];
227 if (fread_unlocked (x, 1, 4, f) != 4
228 || fread_unlocked (&types[i].isdst, 1, 1, f) != 1
229 || fread_unlocked (&types[i].idx, 1, 1, f) != 1)
230 goto lose;
231 if (types[i].isdst > 1)
232 goto lose;
233 if (types[i].idx >= chars) /* Bogus index in data file. */
234 goto lose;
235 types[i].offset = (long int) decode (x);
238 if (fread_unlocked (zone_names, 1, chars, f) != chars)
239 goto lose;
241 for (i = 0; i < num_leaps; ++i)
243 unsigned char x[4];
244 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
245 goto lose;
246 leaps[i].transition = (time_t) decode (x);
247 if (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x))
248 goto lose;
249 leaps[i].change = (long int) decode (x);
252 for (i = 0; i < num_isstd; ++i)
254 int c = getc_unlocked (f);
255 if (c == EOF)
256 goto lose;
257 types[i].isstd = c != 0;
259 while (i < num_types)
260 types[i++].isstd = 0;
262 for (i = 0; i < num_isgmt; ++i)
264 int c = getc_unlocked (f);
265 if (c == EOF)
266 goto lose;
267 types[i].isgmt = c != 0;
269 while (i < num_types)
270 types[i++].isgmt = 0;
272 fclose (f);
274 /* First "register" all timezone names. */
275 for (i = 0; i < num_types; ++i)
276 (void) __tzstring (&zone_names[types[i].idx]);
278 /* Find the standard and daylight time offsets used by the rule file.
279 We choose the offsets in the types of each flavor that are
280 transitioned to earliest in time. */
281 __tzname[0] = NULL;
282 __tzname[1] = NULL;
283 for (i = num_transitions; i > 0; )
285 int type = type_idxs[--i];
286 int dst = types[type].isdst;
287 int idx = types[type].idx;
289 if (__tzname[dst] == NULL)
291 __tzname[dst] = __tzstring (&zone_names[idx]);
293 if (__tzname[1 - dst] != NULL)
294 break;
297 if (__tzname[0] == NULL)
299 /* This should only happen if there are no transition rules.
300 In this case there should be only one single type. */
301 assert (num_types == 1);
302 __tzname[0] = __tzstring (zone_names);
304 if (__tzname[1] == NULL)
305 __tzname[1] = __tzname[0];
307 compute_tzname_max (chars);
309 if (num_transitions == 0)
310 /* Use the first rule (which should also be the only one). */
311 rule_stdoff = rule_dstoff = types[0].offset;
312 else
314 rule_stdoff = rule_dstoff = 0;
315 for (i = 0; i < num_transitions; ++i)
317 if (!rule_stdoff && !types[type_idxs[i]].isdst)
318 rule_stdoff = types[type_idxs[i]].offset;
319 if (!rule_dstoff && types[type_idxs[i]].isdst)
320 rule_dstoff = types[type_idxs[i]].offset;
321 if (rule_stdoff && rule_dstoff)
322 break;
326 __daylight = rule_stdoff != rule_dstoff;
327 __timezone = -rule_stdoff;
329 __use_tzfile = 1;
330 return;
332 lose:
333 fclose (f);
336 /* The user specified a hand-made timezone, but not its DST rules.
337 We will use the names and offsets from the user, and the rules
338 from the TZDEFRULES file. */
340 void
341 __tzfile_default (const char *std, const char *dst,
342 long int stdoff, long int dstoff)
344 size_t stdlen, dstlen, i;
345 int isdst;
347 __tzfile_read (TZDEFRULES);
348 if (!__use_tzfile)
349 return;
351 if (num_types < 2)
353 __use_tzfile = 0;
354 return;
357 /* Ignore the zone names read from the file. */
358 free (zone_names);
360 /* Use the names the user specified. */
361 stdlen = strlen (std) + 1;
362 dstlen = strlen (dst) + 1;
363 zone_names = malloc (stdlen + dstlen);
364 if (zone_names == NULL)
366 __use_tzfile = 0;
367 return;
369 __mempcpy (__mempcpy (zone_names, std, stdlen), dst, dstlen);
371 /* Now there are only two zones, regardless of what the file contained. */
372 num_types = 2;
374 /* Now correct the transition times for the user-specified standard and
375 daylight offsets from GMT. */
376 isdst = 0;
377 for (i = 0; i < num_transitions; ++i)
379 struct ttinfo *trans_type = &types[type_idxs[i]];
381 /* We will use only types 0 (standard) and 1 (daylight).
382 Fix up this transition to point to whichever matches
383 the flavor of its original type. */
384 type_idxs[i] = trans_type->isdst;
386 if (trans_type->isgmt)
387 /* The transition time is in GMT. No correction to apply. */ ;
388 else if (isdst && !trans_type->isstd)
389 /* The type says this transition is in "local wall clock time", and
390 wall clock time as of the previous transition was DST. Correct
391 for the difference between the rule's DST offset and the user's
392 DST offset. */
393 transitions[i] += dstoff - rule_dstoff;
394 else
395 /* This transition is in "local wall clock time", and wall clock
396 time as of this iteration is non-DST. Correct for the
397 difference between the rule's standard offset and the user's
398 standard offset. */
399 transitions[i] += stdoff - rule_stdoff;
401 /* The DST state of "local wall clock time" for the next iteration is
402 as specified by this transition. */
403 isdst = trans_type->isdst;
406 /* Reset types 0 and 1 to describe the user's settings. */
407 types[0].idx = 0;
408 types[0].offset = stdoff;
409 types[0].isdst = 0;
410 types[1].idx = stdlen;
411 types[1].offset = dstoff;
412 types[1].isdst = 1;
414 /* Reset the zone names to point to the user's names. */
415 __tzname[0] = (char *) std;
416 __tzname[1] = (char *) dst;
418 /* Set the timezone. */
419 __timezone = -types[0].offset;
421 compute_tzname_max (stdlen + dstlen);
424 static struct ttinfo *
425 internal_function
426 find_transition (time_t timer)
428 size_t i;
430 if (num_transitions == 0 || timer < transitions[0])
432 /* TIMER is before any transition (or there are no transitions).
433 Choose the first non-DST type
434 (or the first if they're all DST types). */
435 i = 0;
436 while (i < num_types && types[i].isdst)
437 ++i;
438 if (i == num_types)
439 i = 0;
441 else
443 /* Find the first transition after TIMER, and
444 then pick the type of the transition before it. */
445 for (i = 1; i < num_transitions; ++i)
446 if (timer < transitions[i])
447 break;
448 i = type_idxs[i - 1];
451 return &types[i];
455 __tzfile_compute (time_t timer, int use_localtime,
456 long int *leap_correct, int *leap_hit,
457 struct tm *tp)
459 register size_t i;
461 if (use_localtime)
463 struct ttinfo *info = find_transition (timer);
464 __daylight = rule_stdoff != rule_dstoff;
465 __timezone = -rule_stdoff;
466 __tzname[0] = NULL;
467 __tzname[1] = NULL;
468 for (i = num_transitions; i > 0; )
470 int type = type_idxs[--i];
471 int dst = types[type].isdst;
472 int idx = types[type].idx;
474 if (__tzname[dst] == NULL)
476 __tzname[dst] = __tzstring (&zone_names[idx]);
478 if (__tzname[1 - dst] != NULL)
479 break;
482 if (__tzname[0] == NULL)
484 /* This should only happen if there are no transition rules.
485 In this case there should be only one single type. */
486 assert (num_types == 1);
487 __tzname[0] = __tzstring (zone_names);
489 if (__tzname[1] == NULL)
490 /* There is no daylight saving time. */
491 __tzname[1] = __tzname[0];
492 tp->tm_isdst = info->isdst;
493 tp->tm_zone = &zone_names[info->idx];
494 tp->tm_gmtoff = info->offset;
497 *leap_correct = 0L;
498 *leap_hit = 0;
500 /* Find the last leap second correction transition time before TIMER. */
501 i = num_leaps;
503 if (i-- == 0)
504 return 1;
505 while (timer < leaps[i].transition);
507 /* Apply its correction. */
508 *leap_correct = leaps[i].change;
510 if (timer == leaps[i].transition && /* Exactly at the transition time. */
511 ((i == 0 && leaps[i].change > 0) ||
512 leaps[i].change > leaps[i - 1].change))
514 *leap_hit = 1;
515 while (i > 0
516 && leaps[i].transition == leaps[i - 1].transition + 1
517 && leaps[i].change == leaps[i - 1].change + 1)
519 ++*leap_hit;
520 --i;
524 return 1;
527 static void
528 internal_function
529 compute_tzname_max (size_t chars)
531 const char *p;
533 p = zone_names;
536 const char *start = p;
537 while (*p != '\0')
538 ++p;
539 if ((size_t) (p - start) > __tzname_cur_max)
540 __tzname_cur_max = p - start;
542 while (++p < &zone_names[chars]);