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. */
27 #include <timezone/tzfile.h>
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 extern char * __tzstring (const char *); /* Defined in tzset.c. */
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 static time_t *transitions
= NULL
;
53 static unsigned char *type_idxs
= NULL
;
54 static size_t num_types
;
55 static struct ttinfo
*types
= NULL
;
56 static char *zone_names
= NULL
;
57 static long int rule_stdoff
;
58 static long int rule_dstoff
;
59 static size_t num_leaps
;
60 static struct leap
*leaps
= NULL
;
65 /* Decode the four bytes at PTR as a signed integer in network byte order. */
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
);
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
++;
88 __tzfile_read (const char *file
)
90 static const char default_tzdir
[] = TZDIR
;
91 size_t num_isstd
, num_isgmt
;
99 if (transitions
!= NULL
)
100 free ((void *) transitions
);
102 if (type_idxs
!= NULL
)
103 free ((void *) type_idxs
);
106 free ((void *) types
);
108 if (zone_names
!= NULL
)
109 free ((void *) zone_names
);
112 free ((void *) leaps
);
116 /* No user specification; use the site-wide default. */
118 else if (*file
== '\0')
119 /* User specified the empty string; use UTC with no leap seconds. */
123 /* We must not allow to read an arbitrary file in a setuid
124 program. So we fail for any file which is not in the
125 directory hierachy starting at TZDIR
126 and which is not the system wide default TZDEFAULT. */
127 if (__libc_enable_secure
129 && memcmp (file
, TZDEFAULT
, sizeof TZDEFAULT
)
130 && memcmp (file
, default_tzdir
, sizeof (default_tzdir
) - 1))
131 || strstr (file
, "../") != NULL
))
132 /* This test is certainly a bit too restrictive but it should
133 catch all critical cases. */
140 unsigned int len
, tzdir_len
;
143 tzdir
= __secure_getenv ("TZDIR");
144 if (tzdir
== NULL
|| *tzdir
== '\0')
146 tzdir
= default_tzdir
;
147 tzdir_len
= sizeof (default_tzdir
) - 1;
150 tzdir_len
= strlen (tzdir
);
151 len
= strlen (file
) + 1;
152 new = (char *) __alloca (tzdir_len
+ 1 + len
);
153 tmp
= __mempcpy (new, tzdir
, tzdir_len
);
155 __mempcpy (tmp
, file
, len
);
159 f
= fopen (file
, "r");
163 if (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
), 1, f
) != 1)
166 num_transitions
= (size_t) decode (tzhead
.tzh_timecnt
);
167 num_types
= (size_t) decode (tzhead
.tzh_typecnt
);
168 chars
= (size_t) decode (tzhead
.tzh_charcnt
);
169 num_leaps
= (size_t) decode (tzhead
.tzh_leapcnt
);
170 num_isstd
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
171 num_isgmt
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
173 if (num_transitions
> 0)
175 transitions
= (time_t *) malloc (num_transitions
* sizeof(time_t));
176 if (transitions
== NULL
)
178 type_idxs
= (unsigned char *) malloc (num_transitions
);
179 if (type_idxs
== NULL
)
184 types
= (struct ttinfo
*) malloc (num_types
* sizeof (struct ttinfo
));
190 zone_names
= (char *) malloc (chars
);
191 if (zone_names
== NULL
)
196 leaps
= (struct leap
*) malloc (num_leaps
* sizeof (struct leap
));
201 if (sizeof (time_t) < 4)
204 if (fread_unlocked (transitions
, 4, num_transitions
, f
) != num_transitions
205 || fread_unlocked (type_idxs
, 1, num_transitions
, f
) != num_transitions
)
208 /* Check for bogus indices in the data file, so we can hereafter
209 safely use type_idxs[T] as indices into `types' and never crash. */
210 for (i
= 0; i
< num_transitions
; ++i
)
211 if (type_idxs
[i
] >= num_types
)
214 if (BYTE_ORDER
!= BIG_ENDIAN
|| sizeof (time_t) != 4)
216 /* Decode the transition times, stored as 4-byte integers in
217 network (big-endian) byte order. We work from the end of
218 the array so as not to clobber the next element to be
219 processed when sizeof (time_t) > 4. */
222 transitions
[i
] = decode ((char *) transitions
+ i
*4);
225 for (i
= 0; i
< num_types
; ++i
)
228 if (fread_unlocked (x
, 1, 4, f
) != 4
229 || fread_unlocked (&types
[i
].isdst
, 1, 1, f
) != 1
230 || fread_unlocked (&types
[i
].idx
, 1, 1, f
) != 1)
232 if (types
[i
].idx
>= chars
) /* Bogus index in data file. */
234 types
[i
].offset
= (long int) decode (x
);
237 if (fread_unlocked (zone_names
, 1, chars
, f
) != chars
)
240 for (i
= 0; i
< num_leaps
; ++i
)
243 if (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
))
245 leaps
[i
].transition
= (time_t) decode (x
);
246 if (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
))
248 leaps
[i
].change
= (long int) decode (x
);
251 for (i
= 0; i
< num_isstd
; ++i
)
253 int c
= getc_unlocked (f
);
256 types
[i
].isstd
= c
!= 0;
258 while (i
< num_types
)
259 types
[i
++].isstd
= 0;
261 for (i
= 0; i
< num_isgmt
; ++i
)
263 int c
= getc_unlocked (f
);
266 types
[i
].isgmt
= c
!= 0;
268 while (i
< num_types
)
269 types
[i
++].isgmt
= 0;
273 /* Find the standard and daylight time offsets used by the rule file.
274 We choose the offsets in the types of each flavor that are
275 transitioned to earliest in time. */
277 for (i
= 0; i
< num_types
; ++i
)
278 __tzname
[types
[i
].isdst
] = __tzstring (&zone_names
[types
[i
].idx
]);
279 if (__tzname
[1] == NULL
)
280 __tzname
[1] = __tzname
[0];
282 compute_tzname_max (chars
);
284 if (num_transitions
== 0)
285 /* Use the first rule (which should also be the only one. */
286 rule_stdoff
= rule_dstoff
= types
[0].offset
;
289 rule_stdoff
= rule_dstoff
= 0;
290 for (i
= 0; i
< num_transitions
; ++i
)
292 if (!rule_stdoff
&& !types
[type_idxs
[i
]].isdst
)
293 rule_stdoff
= types
[type_idxs
[i
]].offset
;
294 if (!rule_dstoff
&& types
[type_idxs
[i
]].isdst
)
295 rule_dstoff
= types
[type_idxs
[i
]].offset
;
296 if (rule_stdoff
&& rule_dstoff
)
301 __daylight
= rule_stdoff
!= rule_dstoff
;
302 __timezone
= -rule_stdoff
;
311 /* The user specified a hand-made timezone, but not its DST rules.
312 We will use the names and offsets from the user, and the rules
313 from the TZDEFRULES file. */
316 __tzfile_default (const char *std
, const char *dst
,
317 long int stdoff
, long int dstoff
)
319 size_t stdlen
, dstlen
, i
;
322 __tzfile_read (TZDEFRULES
);
332 /* Ignore the zone names read from the file. */
335 /* Use the names the user specified. */
336 stdlen
= strlen (std
) + 1;
337 dstlen
= strlen (dst
) + 1;
338 zone_names
= malloc (stdlen
+ dstlen
);
339 if (zone_names
== NULL
)
344 __mempcpy (__mempcpy (zone_names
, std
, stdlen
), dst
, dstlen
);
346 /* Now correct the transition times for the user-specified standard and
347 daylight offsets from GMT. */
349 for (i
= 0; i
< num_transitions
; ++i
)
351 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
353 /* We will use only types 0 (standard) and 1 (daylight).
354 Fix up this transition to point to whichever matches
355 the flavor of its original type. */
356 type_idxs
[i
] = trans_type
->isdst
;
358 if (trans_type
->isgmt
)
359 /* The transition time is in GMT. No correction to apply. */ ;
360 else if (isdst
&& !trans_type
->isstd
)
361 /* The type says this transition is in "local wall clock time", and
362 wall clock time as of the previous transition was DST. Correct
363 for the difference between the rule's DST offset and the user's
365 transitions
[i
] += dstoff
- rule_dstoff
;
367 /* This transition is in "local wall clock time", and wall clock
368 time as of this iteration is non-DST. Correct for the
369 difference between the rule's standard offset and the user's
371 transitions
[i
] += stdoff
- rule_stdoff
;
373 /* The DST state of "local wall clock time" for the next iteration is
374 as specified by this transition. */
375 isdst
= trans_type
->isdst
;
378 /* Reset types 0 and 1 to describe the user's settings. */
380 types
[0].offset
= stdoff
;
382 types
[1].idx
= stdlen
;
383 types
[1].offset
= dstoff
;
386 /* Reset the zone names to point to the user's names. */
387 __tzname
[0] = (char *) std
;
388 __tzname
[1] = (char *) dst
;
390 /* Set the timezone. */
391 __timezone
= -types
[0].offset
;
393 compute_tzname_max (stdlen
+ dstlen
);
396 static struct ttinfo
*
398 find_transition (time_t timer
)
402 if (num_transitions
== 0 || timer
< transitions
[0])
404 /* TIMER is before any transition (or there are no transitions).
405 Choose the first non-DST type
406 (or the first if they're all DST types). */
408 while (i
< num_types
&& types
[i
].isdst
)
415 /* Find the first transition after TIMER, and
416 then pick the type of the transition before it. */
417 for (i
= 1; i
< num_transitions
; ++i
)
418 if (timer
< transitions
[i
])
420 i
= type_idxs
[i
- 1];
427 __tzfile_compute (time_t timer
, int use_localtime
,
428 long int *leap_correct
, int *leap_hit
,
435 struct ttinfo
*info
= find_transition (timer
);
436 __daylight
= rule_stdoff
!= rule_dstoff
;
437 __timezone
= -rule_stdoff
;
439 for (i
= 0; i
< num_types
; ++i
)
440 __tzname
[types
[i
].isdst
] = __tzstring (&zone_names
[types
[i
].idx
]);
441 if (__tzname
[1] == NULL
)
442 /* There is no daylight saving time. */
443 __tzname
[1] = __tzname
[0];
444 tp
->tm_isdst
= info
->isdst
;
445 tp
->tm_zone
= &zone_names
[info
->idx
];
446 tp
->tm_gmtoff
= info
->offset
;
452 /* Find the last leap second correction transition time before TIMER. */
457 while (timer
< leaps
[i
].transition
);
459 /* Apply its correction. */
460 *leap_correct
= leaps
[i
].change
;
462 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
463 ((i
== 0 && leaps
[i
].change
> 0) ||
464 leaps
[i
].change
> leaps
[i
- 1].change
))
468 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
469 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
481 compute_tzname_max (size_t chars
)
483 extern size_t __tzname_cur_max
; /* Defined in tzset.c. */
490 const char *start
= p
;
493 if ((size_t) (p
- start
) > __tzname_cur_max
)
494 __tzname_cur_max
= p
- start
;
496 while (++p
< &zone_names
[chars
]);