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
22 #include <stdio_ext.h>
29 #include <timezone/tzfile.h>
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. */
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
;
65 /* Decode the four bytes at PTR as a signed integer in network byte order. */
67 __attribute ((always_inline
))
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
, size_t extra
, char **extrap
)
91 static const char default_tzdir
[] = TZDIR
;
92 size_t num_isstd
, num_isgmt
;
103 if (transitions
!= NULL
)
104 free ((void *) transitions
);
108 /* No user specification; use the site-wide default. */
110 else if (*file
== '\0')
111 /* User specified the empty string; use UTC with no leap seconds. */
115 /* We must not allow to read an arbitrary file in a setuid
116 program. So we fail for any file which is not in the
117 directory hierachy starting at TZDIR
118 and which is not the system wide default TZDEFAULT. */
119 if (__libc_enable_secure
121 && memcmp (file
, TZDEFAULT
, sizeof TZDEFAULT
)
122 && memcmp (file
, default_tzdir
, sizeof (default_tzdir
) - 1))
123 || strstr (file
, "../") != NULL
))
124 /* This test is certainly a bit too restrictive but it should
125 catch all critical cases. */
132 unsigned int len
, tzdir_len
;
135 tzdir
= getenv ("TZDIR");
136 if (tzdir
== NULL
|| *tzdir
== '\0')
138 tzdir
= default_tzdir
;
139 tzdir_len
= sizeof (default_tzdir
) - 1;
142 tzdir_len
= strlen (tzdir
);
143 len
= strlen (file
) + 1;
144 new = (char *) __alloca (tzdir_len
+ 1 + len
);
145 tmp
= __mempcpy (new, tzdir
, tzdir_len
);
147 memcpy (tmp
, file
, len
);
151 f
= fopen (file
, "r");
155 /* No threads reading this stream. */
156 __fsetlocking (f
, FSETLOCKING_BYCALLER
);
158 if (__builtin_expect (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
162 num_transitions
= (size_t) decode (tzhead
.tzh_timecnt
);
163 num_types
= (size_t) decode (tzhead
.tzh_typecnt
);
164 chars
= (size_t) decode (tzhead
.tzh_charcnt
);
165 num_leaps
= (size_t) decode (tzhead
.tzh_leapcnt
);
166 num_isstd
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
167 num_isgmt
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
169 total_size
= num_transitions
* (sizeof (time_t) + 1);
170 total_size
= ((total_size
+ __alignof__ (struct ttinfo
) - 1)
171 & ~(__alignof__ (struct ttinfo
) - 1));
172 types_idx
= total_size
;
173 total_size
+= num_types
* sizeof (struct ttinfo
) + chars
;
174 total_size
= ((total_size
+ __alignof__ (struct leap
) - 1)
175 & ~(__alignof__ (struct leap
) - 1));
176 leaps_idx
= total_size
;
177 total_size
+= num_leaps
* sizeof (struct leap
);
178 /* This is for the extra memory required by the caller. */
181 transitions
= (time_t *) malloc (total_size
);
182 if (transitions
== NULL
)
185 type_idxs
= (unsigned char *) transitions
+ (num_transitions
187 types
= (struct ttinfo
*) ((char *) transitions
+ types_idx
);
188 zone_names
= (char *) types
+ num_types
* sizeof (struct ttinfo
);
189 leaps
= (struct leap
*) ((char *) transitions
+ leaps_idx
);
191 *extrap
= (char *) &leaps
[num_leaps
];
193 if (sizeof (time_t) < 4)
196 if (sizeof (time_t) == 4)
198 if (__builtin_expect (fread_unlocked (transitions
, 1,
199 (4 + 1) * num_transitions
, f
)
200 != (4 + 1) * num_transitions
, 0))
205 if (__builtin_expect (fread_unlocked (transitions
, 4, num_transitions
, f
)
206 != num_transitions
, 0)
207 || __builtin_expect (fread_unlocked (type_idxs
, 1, num_transitions
,
208 f
) != num_transitions
, 0))
212 /* Check for bogus indices in the data file, so we can hereafter
213 safely use type_idxs[T] as indices into `types' and never crash. */
214 for (i
= 0; i
< num_transitions
; ++i
)
215 if (__builtin_expect (type_idxs
[i
] >= num_types
, 0))
218 if (BYTE_ORDER
!= BIG_ENDIAN
|| sizeof (time_t) != 4)
220 /* Decode the transition times, stored as 4-byte integers in
221 network (big-endian) byte order. We work from the end of
222 the array so as not to clobber the next element to be
223 processed when sizeof (time_t) > 4. */
226 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
229 for (i
= 0; i
< num_types
; ++i
)
233 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
236 c
= getc_unlocked (f
);
237 if (__builtin_expect ((unsigned int) c
> 1u, 0))
240 c
= getc_unlocked (f
);
241 if (__builtin_expect ((size_t) c
> chars
, 0))
242 /* Bogus index in data file. */
245 types
[i
].offset
= (long int) decode (x
);
248 if (__builtin_expect (fread_unlocked (zone_names
, 1, chars
, f
) != chars
, 0))
251 for (i
= 0; i
< num_leaps
; ++i
)
254 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
257 leaps
[i
].transition
= (time_t) decode (x
);
258 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
261 leaps
[i
].change
= (long int) decode (x
);
264 for (i
= 0; i
< num_isstd
; ++i
)
266 int c
= getc_unlocked (f
);
267 if (__builtin_expect (c
== EOF
, 0))
269 types
[i
].isstd
= c
!= 0;
271 while (i
< num_types
)
272 types
[i
++].isstd
= 0;
274 for (i
= 0; i
< num_isgmt
; ++i
)
276 int c
= getc_unlocked (f
);
277 if (__builtin_expect (c
== EOF
, 0))
279 types
[i
].isgmt
= c
!= 0;
281 while (i
< num_types
)
282 types
[i
++].isgmt
= 0;
286 /* First "register" all timezone names. */
287 for (i
= 0; i
< num_types
; ++i
)
288 (void) __tzstring (&zone_names
[types
[i
].idx
]);
290 /* Find the standard and daylight time offsets used by the rule file.
291 We choose the offsets in the types of each flavor that are
292 transitioned to earliest in time. */
295 for (i
= num_transitions
; i
> 0; )
297 int type
= type_idxs
[--i
];
298 int dst
= types
[type
].isdst
;
300 if (__tzname
[dst
] == NULL
)
302 int idx
= types
[type
].idx
;
304 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
306 if (__tzname
[1 - dst
] != NULL
)
310 if (__tzname
[0] == NULL
)
312 /* This should only happen if there are no transition rules.
313 In this case there should be only one single type. */
314 assert (num_types
== 1);
315 __tzname
[0] = __tzstring (zone_names
);
317 if (__tzname
[1] == NULL
)
318 __tzname
[1] = __tzname
[0];
320 compute_tzname_max (chars
);
322 if (num_transitions
== 0)
323 /* Use the first rule (which should also be the only one). */
324 rule_stdoff
= rule_dstoff
= types
[0].offset
;
327 int stdoff_set
= 0, dstoff_set
= 0;
328 rule_stdoff
= rule_dstoff
= 0;
329 i
= num_transitions
- 1;
332 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
335 rule_stdoff
= types
[type_idxs
[i
]].offset
;
337 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
340 rule_dstoff
= types
[type_idxs
[i
]].offset
;
342 if (stdoff_set
&& dstoff_set
)
348 rule_dstoff
= rule_stdoff
;
351 __daylight
= rule_stdoff
!= rule_dstoff
;
352 __timezone
= -rule_stdoff
;
361 /* The user specified a hand-made timezone, but not its DST rules.
362 We will use the names and offsets from the user, and the rules
363 from the TZDEFRULES file. */
366 __tzfile_default (const char *std
, const char *dst
,
367 long int stdoff
, long int dstoff
)
369 size_t stdlen
= strlen (std
) + 1;
370 size_t dstlen
= strlen (dst
) + 1;
375 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
385 /* Ignore the zone names read from the file and use the given ones
387 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
390 /* Now there are only two zones, regardless of what the file contained. */
393 /* Now correct the transition times for the user-specified standard and
394 daylight offsets from GMT. */
396 for (i
= 0; i
< num_transitions
; ++i
)
398 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
400 /* We will use only types 0 (standard) and 1 (daylight).
401 Fix up this transition to point to whichever matches
402 the flavor of its original type. */
403 type_idxs
[i
] = trans_type
->isdst
;
405 if (trans_type
->isgmt
)
406 /* The transition time is in GMT. No correction to apply. */ ;
407 else if (isdst
&& !trans_type
->isstd
)
408 /* The type says this transition is in "local wall clock time", and
409 wall clock time as of the previous transition was DST. Correct
410 for the difference between the rule's DST offset and the user's
412 transitions
[i
] += dstoff
- rule_dstoff
;
414 /* This transition is in "local wall clock time", and wall clock
415 time as of this iteration is non-DST. Correct for the
416 difference between the rule's standard offset and the user's
418 transitions
[i
] += stdoff
- rule_stdoff
;
420 /* The DST state of "local wall clock time" for the next iteration is
421 as specified by this transition. */
422 isdst
= trans_type
->isdst
;
425 /* Reset types 0 and 1 to describe the user's settings. */
427 types
[0].offset
= stdoff
;
429 types
[1].idx
= stdlen
;
430 types
[1].offset
= dstoff
;
433 /* Reset the zone names to point to the user's names. */
434 __tzname
[0] = (char *) std
;
435 __tzname
[1] = (char *) dst
;
437 /* Set the timezone. */
438 __timezone
= -types
[0].offset
;
440 compute_tzname_max (stdlen
+ dstlen
);
443 static struct ttinfo
*
445 find_transition (time_t timer
)
449 if (num_transitions
== 0 || timer
< transitions
[0])
451 /* TIMER is before any transition (or there are no transitions).
452 Choose the first non-DST type
453 (or the first if they're all DST types). */
455 while (i
< num_types
&& types
[i
].isdst
)
462 /* Find the first transition after TIMER, and
463 then pick the type of the transition before it. */
464 for (i
= 1; i
< num_transitions
; ++i
)
465 if (timer
< transitions
[i
])
467 i
= type_idxs
[i
- 1];
474 __tzfile_compute (time_t timer
, int use_localtime
,
475 long int *leap_correct
, int *leap_hit
,
482 struct ttinfo
*info
= find_transition (timer
);
483 __daylight
= rule_stdoff
!= rule_dstoff
;
484 __timezone
= -rule_stdoff
;
487 for (i
= num_transitions
; i
> 0; )
489 int type
= type_idxs
[--i
];
490 int dst
= types
[type
].isdst
;
491 int idx
= types
[type
].idx
;
493 if (__tzname
[dst
] == NULL
)
495 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
497 if (__tzname
[1 - dst
] != NULL
)
501 if (__tzname
[0] == NULL
)
503 /* This should only happen if there are no transition rules.
504 In this case there should be only one single type. */
505 assert (num_types
== 1);
506 __tzname
[0] = __tzstring (zone_names
);
508 if (__tzname
[1] == NULL
)
509 /* There is no daylight saving time. */
510 __tzname
[1] = __tzname
[0];
511 tp
->tm_isdst
= info
->isdst
;
512 tp
->tm_zone
= __tzstring (&zone_names
[info
->idx
]);
513 tp
->tm_gmtoff
= info
->offset
;
519 /* Find the last leap second correction transition time before TIMER. */
524 while (timer
< leaps
[i
].transition
);
526 /* Apply its correction. */
527 *leap_correct
= leaps
[i
].change
;
529 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
530 ((i
== 0 && leaps
[i
].change
> 0) ||
531 leaps
[i
].change
> leaps
[i
- 1].change
))
535 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
536 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
546 compute_tzname_max (size_t chars
)
553 const char *start
= p
;
556 if ((size_t) (p
- start
) > __tzname_cur_max
)
557 __tzname_cur_max
= p
- start
;
559 while (++p
< &zone_names
[chars
]);