1 /* Copyright (C) 1991-1993,1995-2001,2003,2004 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>
30 #include <timezone/tzfile.h>
33 static dev_t tzfile_dev
;
34 static ino64_t tzfile_ino
;
38 long int offset
; /* Seconds east of GMT. */
39 unsigned char isdst
; /* Used to set tm_isdst. */
40 unsigned char idx
; /* Index into `zone_names'. */
41 unsigned char isstd
; /* Transition times are in standard time. */
42 unsigned char isgmt
; /* Transition times are in GMT. */
47 time_t transition
; /* Time the transition takes effect. */
48 long int change
; /* Seconds of correction to apply. */
51 static struct ttinfo
*find_transition (time_t timer
) internal_function
;
52 static void compute_tzname_max (size_t) internal_function
;
54 static size_t num_transitions
;
55 libc_freeres_ptr (static time_t *transitions
);
56 static unsigned char *type_idxs
;
57 static size_t num_types
;
58 static struct ttinfo
*types
;
59 static char *zone_names
;
60 static long int rule_stdoff
;
61 static long int rule_dstoff
;
62 static size_t num_leaps
;
63 static struct leap
*leaps
;
68 /* Decode the four bytes at PTR as a signed integer in network byte order. */
70 __attribute ((always_inline
))
71 decode (const void *ptr
)
73 if ((BYTE_ORDER
== BIG_ENDIAN
) && sizeof (int) == 4)
74 return *(const int *) ptr
;
75 else if (BYTE_ORDER
== LITTLE_ENDIAN
&& sizeof (int) == 4)
76 return bswap_32 (*(const int *) ptr
);
79 const unsigned char *p
= ptr
;
80 int result
= *p
& (1 << (CHAR_BIT
- 1)) ? ~0 : 0;
82 result
= (result
<< 8) | *p
++;
83 result
= (result
<< 8) | *p
++;
84 result
= (result
<< 8) | *p
++;
85 result
= (result
<< 8) | *p
++;
92 __tzfile_read (const char *file
, size_t extra
, char **extrap
)
94 static const char default_tzdir
[] = TZDIR
;
95 size_t num_isstd
, num_isgmt
;
103 int was_using_tzfile
= __use_tzfile
;
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. */
112 goto ret_free_transitions
;
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. */
126 goto ret_free_transitions
;
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 /* Note the file is opened with cancellation in the I/O functions
153 f
= fopen (file
, "rc");
155 goto ret_free_transitions
;
157 /* Get information about the file. */
159 if (fstat64 (fileno (f
), &st
) != 0)
162 goto ret_free_transitions
;
164 if (was_using_tzfile
&& tzfile_ino
== st
.st_ino
&& tzfile_dev
== st
.st_dev
)
166 /* It's the same file. No further work needed. */
172 free ((void *) transitions
);
175 /* Remember the inode and device number. */
176 tzfile_dev
= st
.st_dev
;
177 tzfile_ino
= st
.st_ino
;
179 /* No threads reading this stream. */
180 __fsetlocking (f
, FSETLOCKING_BYCALLER
);
182 if (__builtin_expect (fread_unlocked ((void *) &tzhead
, sizeof (tzhead
),
186 num_transitions
= (size_t) decode (tzhead
.tzh_timecnt
);
187 num_types
= (size_t) decode (tzhead
.tzh_typecnt
);
188 chars
= (size_t) decode (tzhead
.tzh_charcnt
);
189 num_leaps
= (size_t) decode (tzhead
.tzh_leapcnt
);
190 num_isstd
= (size_t) decode (tzhead
.tzh_ttisstdcnt
);
191 num_isgmt
= (size_t) decode (tzhead
.tzh_ttisgmtcnt
);
193 total_size
= num_transitions
* (sizeof (time_t) + 1);
194 total_size
= ((total_size
+ __alignof__ (struct ttinfo
) - 1)
195 & ~(__alignof__ (struct ttinfo
) - 1));
196 types_idx
= total_size
;
197 total_size
+= num_types
* sizeof (struct ttinfo
) + chars
;
198 total_size
= ((total_size
+ __alignof__ (struct leap
) - 1)
199 & ~(__alignof__ (struct leap
) - 1));
200 leaps_idx
= total_size
;
201 total_size
+= num_leaps
* sizeof (struct leap
);
202 /* This is for the extra memory required by the caller. */
205 transitions
= (time_t *) malloc (total_size
);
206 if (transitions
== NULL
)
209 type_idxs
= (unsigned char *) transitions
+ (num_transitions
211 types
= (struct ttinfo
*) ((char *) transitions
+ types_idx
);
212 zone_names
= (char *) types
+ num_types
* sizeof (struct ttinfo
);
213 leaps
= (struct leap
*) ((char *) transitions
+ leaps_idx
);
215 *extrap
= (char *) &leaps
[num_leaps
];
217 if (sizeof (time_t) < 4)
220 if (sizeof (time_t) == 4)
222 if (__builtin_expect (fread_unlocked (transitions
, 1,
223 (4 + 1) * num_transitions
, f
)
224 != (4 + 1) * num_transitions
, 0))
229 if (__builtin_expect (fread_unlocked (transitions
, 4, num_transitions
, f
)
230 != num_transitions
, 0)
231 || __builtin_expect (fread_unlocked (type_idxs
, 1, num_transitions
,
232 f
) != num_transitions
, 0))
236 /* Check for bogus indices in the data file, so we can hereafter
237 safely use type_idxs[T] as indices into `types' and never crash. */
238 for (i
= 0; i
< num_transitions
; ++i
)
239 if (__builtin_expect (type_idxs
[i
] >= num_types
, 0))
242 if (BYTE_ORDER
!= BIG_ENDIAN
|| sizeof (time_t) != 4)
244 /* Decode the transition times, stored as 4-byte integers in
245 network (big-endian) byte order. We work from the end of
246 the array so as not to clobber the next element to be
247 processed when sizeof (time_t) > 4. */
250 transitions
[i
] = decode ((char *) transitions
+ i
* 4);
253 for (i
= 0; i
< num_types
; ++i
)
257 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
260 c
= getc_unlocked (f
);
261 if (__builtin_expect ((unsigned int) c
> 1u, 0))
264 c
= getc_unlocked (f
);
265 if (__builtin_expect ((size_t) c
> chars
, 0))
266 /* Bogus index in data file. */
269 types
[i
].offset
= (long int) decode (x
);
272 if (__builtin_expect (fread_unlocked (zone_names
, 1, chars
, f
) != chars
, 0))
275 for (i
= 0; i
< num_leaps
; ++i
)
278 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
281 leaps
[i
].transition
= (time_t) decode (x
);
282 if (__builtin_expect (fread_unlocked (x
, 1, sizeof (x
), f
) != sizeof (x
),
285 leaps
[i
].change
= (long int) decode (x
);
288 for (i
= 0; i
< num_isstd
; ++i
)
290 int c
= getc_unlocked (f
);
291 if (__builtin_expect (c
== EOF
, 0))
293 types
[i
].isstd
= c
!= 0;
295 while (i
< num_types
)
296 types
[i
++].isstd
= 0;
298 for (i
= 0; i
< num_isgmt
; ++i
)
300 int c
= getc_unlocked (f
);
301 if (__builtin_expect (c
== EOF
, 0))
303 types
[i
].isgmt
= c
!= 0;
305 while (i
< num_types
)
306 types
[i
++].isgmt
= 0;
310 /* First "register" all timezone names. */
311 for (i
= 0; i
< num_types
; ++i
)
312 (void) __tzstring (&zone_names
[types
[i
].idx
]);
314 /* Find the standard and daylight time offsets used by the rule file.
315 We choose the offsets in the types of each flavor that are
316 transitioned to earliest in time. */
319 for (i
= num_transitions
; i
> 0; )
321 int type
= type_idxs
[--i
];
322 int dst
= types
[type
].isdst
;
324 if (__tzname
[dst
] == NULL
)
326 int idx
= types
[type
].idx
;
328 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
330 if (__tzname
[1 - dst
] != NULL
)
334 if (__tzname
[0] == NULL
)
336 /* This should only happen if there are no transition rules.
337 In this case there should be only one single type. */
338 assert (num_types
== 1);
339 __tzname
[0] = __tzstring (zone_names
);
341 if (__tzname
[1] == NULL
)
342 __tzname
[1] = __tzname
[0];
344 compute_tzname_max (chars
);
346 if (num_transitions
== 0)
347 /* Use the first rule (which should also be the only one). */
348 rule_stdoff
= rule_dstoff
= types
[0].offset
;
351 int stdoff_set
= 0, dstoff_set
= 0;
352 rule_stdoff
= rule_dstoff
= 0;
353 i
= num_transitions
- 1;
356 if (!stdoff_set
&& !types
[type_idxs
[i
]].isdst
)
359 rule_stdoff
= types
[type_idxs
[i
]].offset
;
361 else if (!dstoff_set
&& types
[type_idxs
[i
]].isdst
)
364 rule_dstoff
= types
[type_idxs
[i
]].offset
;
366 if (stdoff_set
&& dstoff_set
)
372 rule_dstoff
= rule_stdoff
;
375 __daylight
= rule_stdoff
!= rule_dstoff
;
376 __timezone
= -rule_stdoff
;
383 ret_free_transitions
:
384 free ((void *) transitions
);
388 /* The user specified a hand-made timezone, but not its DST rules.
389 We will use the names and offsets from the user, and the rules
390 from the TZDEFRULES file. */
393 __tzfile_default (const char *std
, const char *dst
,
394 long int stdoff
, long int dstoff
)
396 size_t stdlen
= strlen (std
) + 1;
397 size_t dstlen
= strlen (dst
) + 1;
402 __tzfile_read (TZDEFRULES
, stdlen
+ dstlen
, &cp
);
412 /* Ignore the zone names read from the file and use the given ones
414 __mempcpy (__mempcpy (cp
, std
, stdlen
), dst
, dstlen
);
417 /* Now there are only two zones, regardless of what the file contained. */
420 /* Now correct the transition times for the user-specified standard and
421 daylight offsets from GMT. */
423 for (i
= 0; i
< num_transitions
; ++i
)
425 struct ttinfo
*trans_type
= &types
[type_idxs
[i
]];
427 /* We will use only types 0 (standard) and 1 (daylight).
428 Fix up this transition to point to whichever matches
429 the flavor of its original type. */
430 type_idxs
[i
] = trans_type
->isdst
;
432 if (trans_type
->isgmt
)
433 /* The transition time is in GMT. No correction to apply. */ ;
434 else if (isdst
&& !trans_type
->isstd
)
435 /* The type says this transition is in "local wall clock time", and
436 wall clock time as of the previous transition was DST. Correct
437 for the difference between the rule's DST offset and the user's
439 transitions
[i
] += dstoff
- rule_dstoff
;
441 /* This transition is in "local wall clock time", and wall clock
442 time as of this iteration is non-DST. Correct for the
443 difference between the rule's standard offset and the user's
445 transitions
[i
] += stdoff
- rule_stdoff
;
447 /* The DST state of "local wall clock time" for the next iteration is
448 as specified by this transition. */
449 isdst
= trans_type
->isdst
;
452 /* Now that we adjusted the transitions to the requested offsets,
453 reset the rule_stdoff and rule_dstoff values appropriately. They
454 are used elsewhere. */
455 rule_stdoff
= stdoff
;
456 rule_dstoff
= dstoff
;
458 /* Reset types 0 and 1 to describe the user's settings. */
460 types
[0].offset
= stdoff
;
462 types
[1].idx
= stdlen
;
463 types
[1].offset
= dstoff
;
466 /* Reset the zone names to point to the user's names. */
467 __tzname
[0] = (char *) std
;
468 __tzname
[1] = (char *) dst
;
470 /* Set the timezone. */
471 __timezone
= -types
[0].offset
;
473 compute_tzname_max (stdlen
+ dstlen
);
476 static struct ttinfo
*
478 find_transition (time_t timer
)
482 if (num_transitions
== 0 || timer
< transitions
[0])
484 /* TIMER is before any transition (or there are no transitions).
485 Choose the first non-DST type
486 (or the first if they're all DST types). */
488 while (i
< num_types
&& types
[i
].isdst
)
495 /* Find the first transition after TIMER, and
496 then pick the type of the transition before it. */
497 for (i
= 1; i
< num_transitions
; ++i
)
498 if (timer
< transitions
[i
])
500 i
= type_idxs
[i
- 1];
507 __tzfile_compute (time_t timer
, int use_localtime
,
508 long int *leap_correct
, int *leap_hit
,
515 struct ttinfo
*info
= find_transition (timer
);
516 __daylight
= rule_stdoff
!= rule_dstoff
;
517 __timezone
= -rule_stdoff
;
520 for (i
= num_transitions
; i
> 0; )
522 int type
= type_idxs
[--i
];
523 int dst
= types
[type
].isdst
;
524 int idx
= types
[type
].idx
;
526 if (__tzname
[dst
] == NULL
)
528 __tzname
[dst
] = __tzstring (&zone_names
[idx
]);
530 if (__tzname
[1 - dst
] != NULL
)
534 if (__tzname
[0] == NULL
)
536 /* This should only happen if there are no transition rules.
537 In this case there should be only one single type. */
538 assert (num_types
== 1);
539 __tzname
[0] = __tzstring (zone_names
);
541 if (__tzname
[1] == NULL
)
542 /* There is no daylight saving time. */
543 __tzname
[1] = __tzname
[0];
544 tp
->tm_isdst
= info
->isdst
;
545 tp
->tm_zone
= __tzstring (&zone_names
[info
->idx
]);
546 tp
->tm_gmtoff
= info
->offset
;
552 /* Find the last leap second correction transition time before TIMER. */
557 while (timer
< leaps
[i
].transition
);
559 /* Apply its correction. */
560 *leap_correct
= leaps
[i
].change
;
562 if (timer
== leaps
[i
].transition
&& /* Exactly at the transition time. */
563 ((i
== 0 && leaps
[i
].change
> 0) ||
564 leaps
[i
].change
> leaps
[i
- 1].change
))
568 && leaps
[i
].transition
== leaps
[i
- 1].transition
+ 1
569 && leaps
[i
].change
== leaps
[i
- 1].change
+ 1)
579 compute_tzname_max (size_t chars
)
586 const char *start
= p
;
589 if ((size_t) (p
- start
) > __tzname_cur_max
)
590 __tzname_cur_max
= p
- start
;
592 while (++p
< &zone_names
[chars
]);