2 Add support for use of the system timezone database, rather
3 than embedding a copy. Discussed upstream but was not desired.
6 r7: improve check for valid timezone id to exclude directories
7 r6: fix fd leak in r5, fix country code/BC flag use in
8 timezone_identifiers_list() using system db,
9 fix use of PECL timezonedb to override system db,
10 r5: reverts addition of "System/Localtime" fake tzname.
11 updated for 5.3.0, parses zone.tab to pick up mapping between
12 timezone name, country code and long/lat coords
13 r4: added "System/Localtime" tzname which uses /etc/localtime
14 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
15 r2: add filesystem trawl to set up name alias index
18 --- php-5.3.1/ext/date/lib/parse_tz.c.systzdata
19 +++ php-5.3.1/ext/date/lib/parse_tz.c
24 +#ifdef HAVE_SYSTEM_TZDATA
25 +#include <sys/mman.h>
26 +#include <sys/stat.h>
31 +#include "php_scandir.h"
42 +#ifndef HAVE_SYSTEM_TZDATA
43 #include "timezonedb.h"
48 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
49 # if defined(__LITTLE_ENDIAN__)
52 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
57 + if (memcmp(tzf, "TZif", 4) == 0) {
66 tz->bc = (**tzf == '\1');
68 @@ -253,7 +273,397 @@ void timelib_dump_tzinfo(timelib_tzinfo
72 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
73 +#ifdef HAVE_SYSTEM_TZDATA
75 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
76 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
78 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
81 +/* System timezone database pointer. */
82 +static const timelib_tzdb *timezonedb_system = NULL;
84 +/* Hash table entry for the cache of the zone.tab mapping table. */
85 +struct location_info {
87 + double latitude, longitude;
90 + struct location_info *next;
93 +/* Cache of zone.tab. */
94 +static struct location_info **system_location_table;
96 +/* Size of the zone.tab hash table; a random-ish prime big enough to
97 + * prevent too many collisions. */
98 +#define LOCINFO_HASH_SIZE (1021)
100 +static uint32_t tz_hash(const char *str)
102 + const unsigned char *p = (const unsigned char *)str;
103 + uint32_t hash = 5381;
106 + while ((c = *p++) != '\0') {
107 + hash = (hash << 5) ^ hash ^ c;
110 + return hash % LOCINFO_HASH_SIZE;
113 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
114 + * parsed string on success, or NULL on parse error. On success,
115 + * writes the parsed number to *result. */
116 +static char *parse_iso6709(char *p, double *result)
124 + else if (*p == '-')
130 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
133 + /* Annoying encoding used by zone.tab has no decimal point, so use
134 + * the length to determine the format:
142 + if (len < 4 || len > 7) {
147 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
149 + if (len == 5 || len == 7)
150 + v = v * 10.0 + (*p++ - '0');
152 + v += (10.0 * (p[0] - '0')
153 + + p[1] - '0') / 60.0;
157 + v += (10.0 * (p[0] - '0')
158 + + p[1] - '0') / 3600.0;
162 + /* Round to five decimal place, not because it's a good idea,
163 + * but, because the builtin data uses rounded data, so, match
165 + *result = round(v * sign * 100000.0) / 100000.0;
170 +/* This function parses the zone.tab file to build up the mapping of
171 + * timezone to country code and geographic location, and returns a
172 + * hash table. The hash table is indexed by the function:
174 + * tz_hash(timezone-name)
176 +static struct location_info **create_location_table(void)
178 + struct location_info **li, *i;
179 + char zone_tab[PATH_MAX];
183 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
185 + fp = fopen(zone_tab, "r");
190 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
192 + while (fgets(line, sizeof line, fp)) {
193 + char *p = line, *code, *name, *comment;
195 + double latitude, longitude;
197 + while (isspace(*p))
200 + if (*p == '#' || *p == '\0' || *p == '\n')
203 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
211 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
212 + p = parse_iso6709(p, &latitude);
216 + p = parse_iso6709(p, &longitude);
221 + if (!p || *p != '\t') {
225 + /* name = string */
227 + while (*p != '\t' && *p && *p != '\n')
232 + /* comment = string */
234 + while (*p != '\t' && *p && *p != '\n')
237 + if (*p == '\n' || *p == '\t')
240 + hash = tz_hash(name);
241 + i = malloc(sizeof *i);
242 + memcpy(i->code, code, 2);
243 + strncpy(i->name, name, sizeof i->name);
244 + i->comment = strdup(comment);
245 + i->longitude = longitude;
246 + i->latitude = latitude;
247 + i->next = li[hash];
249 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
257 +/* Return location info from hash table, using given timezone name.
258 + * Returns NULL if the name could not be found. */
259 +const struct location_info *find_zone_info(struct location_info **li,
262 + uint32_t hash = tz_hash(name);
263 + const struct location_info *l;
269 + for (l = li[hash]; l; l = l->next) {
270 + if (strcasecmp(l->name, name) == 0)
277 +/* Filter out some non-tzdata files and the posix/right databases, if
279 +static int index_filter(const struct dirent *ent)
281 + return strcmp(ent->d_name, ".") != 0
282 + && strcmp(ent->d_name, "..") != 0
283 + && strcmp(ent->d_name, "posix") != 0
284 + && strcmp(ent->d_name, "posixrules") != 0
285 + && strcmp(ent->d_name, "right") != 0
286 + && strstr(ent->d_name, ".tab") == NULL;
289 +static int sysdbcmp(const void *first, const void *second)
291 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
293 + return strcmp(alpha->id, beta->id);
297 +/* Create the zone identifier index by trawling the filesystem. */
298 +static void create_zone_index(timelib_tzdb *db)
300 + size_t dirstack_size, dirstack_top;
301 + size_t index_size, index_next;
302 + timelib_tzdb_index_entry *db_index;
305 + /* LIFO stack to hold directory entries to scan; each slot is a
306 + * directory name relative to the zoneinfo prefix. */
307 + dirstack_size = 32;
308 + dirstack = malloc(dirstack_size * sizeof *dirstack);
310 + dirstack[0] = strdup("");
314 + db_index = malloc(index_size * sizeof *db_index);
318 + struct dirent **ents;
319 + char name[PATH_MAX], *top;
322 + /* Pop the top stack entry, and iterate through its contents. */
323 + top = dirstack[--dirstack_top];
324 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
326 + count = php_scandir(name, &ents, index_filter, php_alphasort);
328 + while (count > 0) {
330 + const char *leaf = ents[count - 1]->d_name;
332 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
335 + if (strlen(name) && stat(name, &st) == 0) {
336 + /* Name, relative to the zoneinfo prefix. */
337 + const char *root = top;
339 + if (root[0] == '/') root++;
341 + snprintf(name, sizeof name, "%s%s%s", root,
342 + *root ? "/": "", leaf);
344 + if (S_ISDIR(st.st_mode)) {
345 + if (dirstack_top == dirstack_size) {
346 + dirstack_size *= 2;
347 + dirstack = realloc(dirstack,
348 + dirstack_size * sizeof *dirstack);
350 + dirstack[dirstack_top++] = strdup(name);
353 + if (index_next == index_size) {
355 + db_index = realloc(db_index,
356 + index_size * sizeof *db_index);
359 + db_index[index_next++].id = strdup(name);
363 + free(ents[--count]);
366 + if (count != -1) free(ents);
368 + } while (dirstack_top);
370 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
372 + db->index = db_index;
373 + db->index_size = index_next;
378 +#define FAKE_HEADER "1234\0??\1??"
379 +#define FAKE_UTC_POS (7 - 4)
381 +/* Create a fake data segment for database 'sysdb'. */
382 +static void fake_data_segment(timelib_tzdb *sysdb,
383 + struct location_info **info)
388 + data = malloc(3 * sysdb->index_size + 7);
390 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
392 + for (n = 0; n < sysdb->index_size; n++) {
393 + const struct location_info *li;
394 + timelib_tzdb_index_entry *ent;
396 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
398 + /* Lookup the timezone name in the hash table. */
399 + if (strcmp(ent->id, "UTC") == 0) {
400 + ent->pos = FAKE_UTC_POS;
404 + li = find_zone_info(info, ent->id);
406 + /* If found, append the BC byte and the
407 + * country code; set the position for this
408 + * section of timezone data. */
409 + ent->pos = (p - data) - 4;
411 + *p++ = li->code[0];
412 + *p++ = li->code[1];
415 + /* If not found, the timezone data can
416 + * point at the header. */
421 + sysdb->data = (unsigned char *)data;
424 +/* Returns true if the passed-in stat structure describes a
425 + * probably-valid timezone file. */
426 +static int is_valid_tzfile(const struct stat *st)
428 + return S_ISREG(st->st_mode) && st->st_size > 20;
431 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
432 + * length of the mapped data is placed in *length. */
433 +static char *map_tzfile(const char *timezone, size_t *length)
435 + char fname[PATH_MAX];
440 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
444 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
446 + fd = open(fname, O_RDONLY);
449 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
454 + *length = st.st_size;
455 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
458 + return p != MAP_FAILED ? p : NULL;
463 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
465 int left = 0, right = tzdb->index_size - 1;
466 #ifdef HAVE_SETLOCALE
467 @@ -292,36 +702,125 @@ static int seek_to_tz_position(const uns
471 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
472 + char **map, size_t *maplen,
473 + const timelib_tzdb *tzdb)
475 + if (tzdb == timezonedb_system) {
478 + orig = map_tzfile(timezone, maplen);
479 + if (orig == NULL) {
483 + (*tzf) = (unsigned char *)orig ;
489 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
493 const timelib_tzdb *timelib_builtin_db(void)
495 +#ifdef HAVE_SYSTEM_TZDATA
496 + if (timezonedb_system == NULL) {
497 + timelib_tzdb *tmp = malloc(sizeof *tmp);
499 + tmp->version = "0.system";
501 + create_zone_index(tmp);
502 + system_location_table = create_location_table();
503 + fake_data_segment(tmp, system_location_table);
504 + timezonedb_system = tmp;
508 + return timezonedb_system;
510 return &timezonedb_builtin;
514 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
516 +#ifdef HAVE_SYSTEM_TZDATA
517 + *count = timezonedb_system->index_size;
518 + return timezonedb_system->index;
520 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
521 return timezonedb_idx_builtin;
525 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
527 const unsigned char *tzf;
528 - return (seek_to_tz_position(&tzf, timezone, tzdb));
530 +#ifdef HAVE_SYSTEM_TZDATA
531 + if (tzdb == timezonedb_system) {
532 + char fname[PATH_MAX];
535 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
539 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
541 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
545 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
548 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
550 const unsigned char *tzf;
551 + char *memmap = NULL;
555 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
556 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
557 tmp = timelib_tzinfo_ctor(timezone);
559 read_preamble(&tzf, tmp);
560 read_header(&tzf, tmp);
561 read_transistions(&tzf, tmp);
562 read_types(&tzf, tmp);
563 - read_location(&tzf, tmp);
565 +#ifdef HAVE_SYSTEM_TZDATA
567 + const struct location_info *li;
569 + /* TZif-style - grok the location info from the system database,
572 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
573 + tmp->location.comments = strdup(li->comment);
574 + strncpy(tmp->location.country_code, li->code, 2);
575 + tmp->location.longitude = li->longitude;
576 + tmp->location.latitude = li->latitude;
580 + strcpy(tmp->location.country_code, "??");
582 + tmp->location.comments = strdup("");
585 + /* Now done with the mmap segment - discard it. */
586 + munmap(memmap, maplen);
590 + /* PHP-style - use the embedded info. */
591 + read_location(&tzf, tmp);
596 --- php-5.3.1/ext/date/lib/timelib.m4.systzdata
597 +++ php-5.3.1/ext/date/lib/timelib.m4
598 @@ -78,3 +78,17 @@ stdlib.h
600 dnl Check for strtoll, atoll
601 AC_CHECK_FUNCS(strtoll atoll strftime)
603 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
604 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
607 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
608 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
610 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
611 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
612 + [Define for location of system timezone data])