0013-Add-support-for-use-of-the-system-timezone-database.patch (18744B)
1 From: Joe Orton <jorton@redhat.com> 2 Date: Thu, 20 Oct 2016 11:44:14 +0200 3 Subject: Add support for use of the system timezone database 4 5 Add support for use of the system timezone database, rather 6 than embedding a copy. Discussed upstream but was not desired. 7 8 History: 9 r14: improve check for valid tz file 10 r13: adapt for upstream changes to use PHP allocator 11 r12: adapt for upstream changes for new zic 12 r11: use canonical names to avoid more case sensitivity issues 13 round lat/long from zone.tab towards zero per builtin db 14 r10: make timezone case insensitive 15 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold) 16 r8: fix compile error without --with-system-tzdata configured 17 r7: improve check for valid timezone id to exclude directories 18 r6: fix fd leak in r5, fix country code/BC flag use in 19 timezone_identifiers_list() using system db, 20 fix use of PECL timezonedb to override system db, 21 r5: reverts addition of "System/Localtime" fake tzname. 22 updated for 5.3.0, parses zone.tab to pick up mapping between 23 timezone name, country code and long/lat coords 24 r4: added "System/Localtime" tzname which uses /etc/localtime 25 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert) 26 r2: add filesystem trawl to set up name alias index 27 r1: initial revision 28 --- 29 ext/date/lib/parse_tz.c | 560 +++++++++++++++++++++++++++++++++++++++++++++++- 30 ext/date/lib/timelib.m4 | 13 ++ 31 2 files changed, 562 insertions(+), 11 deletions(-) 32 33 diff --git a/ext/date/lib/parse_tz.c b/ext/date/lib/parse_tz.c 34 index 20d7eea..ed7717e 100644 35 --- a/ext/date/lib/parse_tz.c 36 +++ b/ext/date/lib/parse_tz.c 37 @@ -24,6 +24,16 @@ 38 39 #include "timelib.h" 40 41 +#ifdef HAVE_SYSTEM_TZDATA 42 +#include <sys/mman.h> 43 +#include <sys/stat.h> 44 +#include <limits.h> 45 +#include <fcntl.h> 46 +#include <unistd.h> 47 + 48 +#include "php_scandir.h" 49 +#endif 50 + 51 #include <stdio.h> 52 53 #ifdef HAVE_LOCALE_H 54 @@ -36,8 +46,12 @@ 55 #include <strings.h> 56 #endif 57 58 +#ifndef HAVE_SYSTEM_TZDATA 59 #define TIMELIB_SUPPORTS_V2DATA 60 #include "timezonedb.h" 61 +#endif 62 + 63 +#include <ctype.h> 64 65 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__)) 66 # if defined(__LITTLE_ENDIAN__) 67 @@ -59,6 +73,11 @@ static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz) 68 { 69 uint32_t version; 70 71 + if (memcmp(*tzf, "TZif", 4) == 0) { 72 + *tzf += 20; 73 + return 0; 74 + } 75 + 76 /* read ID */ 77 version = (*tzf)[3] - '0'; 78 *tzf += 4; 79 @@ -302,7 +321,429 @@ void timelib_dump_tzinfo(timelib_tzinfo *tz) 80 } 81 } 82 83 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb) 84 +#ifdef HAVE_SYSTEM_TZDATA 85 + 86 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX 87 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX 88 +#else 89 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo" 90 +#endif 91 + 92 +/* System timezone database pointer. */ 93 +static const timelib_tzdb *timezonedb_system; 94 + 95 +/* Hash table entry for the cache of the zone.tab mapping table. */ 96 +struct location_info { 97 + char code[2]; 98 + double latitude, longitude; 99 + char name[64]; 100 + char *comment; 101 + struct location_info *next; 102 +}; 103 + 104 +/* Cache of zone.tab. */ 105 +static struct location_info **system_location_table; 106 + 107 +/* Size of the zone.tab hash table; a random-ish prime big enough to 108 + * prevent too many collisions. */ 109 +#define LOCINFO_HASH_SIZE (1021) 110 + 111 +/* Compute a case insensitive hash of str */ 112 +static uint32_t tz_hash(const char *str) 113 +{ 114 + const unsigned char *p = (const unsigned char *)str; 115 + uint32_t hash = 5381; 116 + int c; 117 + 118 + while ((c = tolower(*p++)) != '\0') { 119 + hash = (hash << 5) ^ hash ^ c; 120 + } 121 + 122 + return hash % LOCINFO_HASH_SIZE; 123 +} 124 + 125 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the 126 + * parsed string on success, or NULL on parse error. On success, 127 + * writes the parsed number to *result. */ 128 +static char *parse_iso6709(char *p, double *result) 129 +{ 130 + double v, sign; 131 + char *pend; 132 + size_t len; 133 + 134 + if (*p == '+') 135 + sign = 1.0; 136 + else if (*p == '-') 137 + sign = -1.0; 138 + else 139 + return NULL; 140 + 141 + p++; 142 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++) 143 + ;; 144 + 145 + /* Annoying encoding used by zone.tab has no decimal point, so use 146 + * the length to determine the format: 147 + * 148 + * 4 = DDMM 149 + * 5 = DDDMM 150 + * 6 = DDMMSS 151 + * 7 = DDDMMSS 152 + */ 153 + len = pend - p; 154 + if (len < 4 || len > 7) { 155 + return NULL; 156 + } 157 + 158 + /* p => [D]DD */ 159 + v = (p[0] - '0') * 10.0 + (p[1] - '0'); 160 + p += 2; 161 + if (len == 5 || len == 7) 162 + v = v * 10.0 + (*p++ - '0'); 163 + /* p => MM[SS] */ 164 + v += (10.0 * (p[0] - '0') 165 + + p[1] - '0') / 60.0; 166 + p += 2; 167 + /* p => [SS] */ 168 + if (len > 5) { 169 + v += (10.0 * (p[0] - '0') 170 + + p[1] - '0') / 3600.0; 171 + p += 2; 172 + } 173 + 174 + /* Round to five decimal place, not because it's a good idea, 175 + * but, because the builtin data uses rounded data, so, match 176 + * that. */ 177 + *result = trunc(v * sign * 100000.0) / 100000.0; 178 + 179 + return p; 180 +} 181 + 182 +/* This function parses the zone.tab file to build up the mapping of 183 + * timezone to country code and geographic location, and returns a 184 + * hash table. The hash table is indexed by the function: 185 + * 186 + * tz_hash(timezone-name) 187 + */ 188 +static struct location_info **create_location_table(void) 189 +{ 190 + struct location_info **li, *i; 191 + char zone_tab[PATH_MAX]; 192 + char line[512]; 193 + FILE *fp; 194 + 195 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab); 196 + 197 + fp = fopen(zone_tab, "r"); 198 + if (!fp) { 199 + return NULL; 200 + } 201 + 202 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li); 203 + 204 + while (fgets(line, sizeof line, fp)) { 205 + char *p = line, *code, *name, *comment; 206 + uint32_t hash; 207 + double latitude, longitude; 208 + 209 + while (isspace(*p)) 210 + p++; 211 + 212 + if (*p == '#' || *p == '\0' || *p == '\n') 213 + continue; 214 + 215 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t') 216 + continue; 217 + 218 + /* code => AA */ 219 + code = p; 220 + p[2] = 0; 221 + p += 3; 222 + 223 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */ 224 + p = parse_iso6709(p, &latitude); 225 + if (!p) { 226 + continue; 227 + } 228 + p = parse_iso6709(p, &longitude); 229 + if (!p) { 230 + continue; 231 + } 232 + 233 + if (!p || *p != '\t') { 234 + continue; 235 + } 236 + 237 + /* name = string */ 238 + name = ++p; 239 + while (*p != '\t' && *p && *p != '\n') 240 + p++; 241 + 242 + *p++ = '\0'; 243 + 244 + /* comment = string */ 245 + comment = p; 246 + while (*p != '\t' && *p && *p != '\n') 247 + p++; 248 + 249 + if (*p == '\n' || *p == '\t') 250 + *p = '\0'; 251 + 252 + hash = tz_hash(name); 253 + i = malloc(sizeof *i); 254 + memcpy(i->code, code, 2); 255 + strncpy(i->name, name, sizeof i->name); 256 + i->comment = strdup(comment); 257 + i->longitude = longitude; 258 + i->latitude = latitude; 259 + i->next = li[hash]; 260 + li[hash] = i; 261 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */ 262 + } 263 + 264 + fclose(fp); 265 + 266 + return li; 267 +} 268 + 269 +/* Return location info from hash table, using given timezone name. 270 + * Returns NULL if the name could not be found. */ 271 +const struct location_info *find_zone_info(struct location_info **li, 272 + const char *name) 273 +{ 274 + uint32_t hash = tz_hash(name); 275 + const struct location_info *l; 276 + 277 + if (!li) { 278 + return NULL; 279 + } 280 + 281 + for (l = li[hash]; l; l = l->next) { 282 + if (strcasecmp(l->name, name) == 0) 283 + return l; 284 + } 285 + 286 + return NULL; 287 +} 288 + 289 +/* Filter out some non-tzdata files and the posix/right databases, if 290 + * present. */ 291 +static int index_filter(const struct dirent *ent) 292 +{ 293 + return strcmp(ent->d_name, ".") != 0 294 + && strcmp(ent->d_name, "..") != 0 295 + && strcmp(ent->d_name, "posix") != 0 296 + && strcmp(ent->d_name, "posixrules") != 0 297 + && strcmp(ent->d_name, "right") != 0 298 + && strstr(ent->d_name, ".list") == NULL 299 + && strstr(ent->d_name, ".tab") == NULL; 300 +} 301 + 302 +static int sysdbcmp(const void *first, const void *second) 303 +{ 304 + const timelib_tzdb_index_entry *alpha = first, *beta = second; 305 + 306 + return strcasecmp(alpha->id, beta->id); 307 +} 308 + 309 + 310 +/* Create the zone identifier index by trawling the filesystem. */ 311 +static void create_zone_index(timelib_tzdb *db) 312 +{ 313 + size_t dirstack_size, dirstack_top; 314 + size_t index_size, index_next; 315 + timelib_tzdb_index_entry *db_index; 316 + char **dirstack; 317 + 318 + /* LIFO stack to hold directory entries to scan; each slot is a 319 + * directory name relative to the zoneinfo prefix. */ 320 + dirstack_size = 32; 321 + dirstack = malloc(dirstack_size * sizeof *dirstack); 322 + dirstack_top = 1; 323 + dirstack[0] = strdup(""); 324 + 325 + /* Index array. */ 326 + index_size = 64; 327 + db_index = malloc(index_size * sizeof *db_index); 328 + index_next = 0; 329 + 330 + do { 331 + struct dirent **ents; 332 + char name[PATH_MAX], *top; 333 + int count; 334 + 335 + /* Pop the top stack entry, and iterate through its contents. */ 336 + top = dirstack[--dirstack_top]; 337 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top); 338 + 339 + count = php_scandir(name, &ents, index_filter, php_alphasort); 340 + 341 + while (count > 0) { 342 + struct stat st; 343 + const char *leaf = ents[count - 1]->d_name; 344 + 345 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 346 + top, leaf); 347 + 348 + if (strlen(name) && stat(name, &st) == 0) { 349 + /* Name, relative to the zoneinfo prefix. */ 350 + const char *root = top; 351 + 352 + if (root[0] == '/') root++; 353 + 354 + snprintf(name, sizeof name, "%s%s%s", root, 355 + *root ? "/": "", leaf); 356 + 357 + if (S_ISDIR(st.st_mode)) { 358 + if (dirstack_top == dirstack_size) { 359 + dirstack_size *= 2; 360 + dirstack = realloc(dirstack, 361 + dirstack_size * sizeof *dirstack); 362 + } 363 + dirstack[dirstack_top++] = strdup(name); 364 + } 365 + else { 366 + if (index_next == index_size) { 367 + index_size *= 2; 368 + db_index = realloc(db_index, 369 + index_size * sizeof *db_index); 370 + } 371 + 372 + db_index[index_next++].id = strdup(name); 373 + } 374 + } 375 + 376 + free(ents[--count]); 377 + } 378 + 379 + if (count != -1) free(ents); 380 + free(top); 381 + } while (dirstack_top); 382 + 383 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp); 384 + 385 + db->index = db_index; 386 + db->index_size = index_next; 387 + 388 + free(dirstack); 389 +} 390 + 391 +#define FAKE_HEADER "1234\0??\1??" 392 +#define FAKE_UTC_POS (7 - 4) 393 + 394 +/* Create a fake data segment for database 'sysdb'. */ 395 +static void fake_data_segment(timelib_tzdb *sysdb, 396 + struct location_info **info) 397 +{ 398 + size_t n; 399 + char *data, *p; 400 + 401 + data = malloc(3 * sysdb->index_size + 7); 402 + 403 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1); 404 + 405 + for (n = 0; n < sysdb->index_size; n++) { 406 + const struct location_info *li; 407 + timelib_tzdb_index_entry *ent; 408 + 409 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n]; 410 + 411 + /* Lookup the timezone name in the hash table. */ 412 + if (strcmp(ent->id, "UTC") == 0) { 413 + ent->pos = FAKE_UTC_POS; 414 + continue; 415 + } 416 + 417 + li = find_zone_info(info, ent->id); 418 + if (li) { 419 + /* If found, append the BC byte and the 420 + * country code; set the position for this 421 + * section of timezone data. */ 422 + ent->pos = (p - data) - 4; 423 + *p++ = '\1'; 424 + *p++ = li->code[0]; 425 + *p++ = li->code[1]; 426 + } 427 + else { 428 + /* If not found, the timezone data can 429 + * point at the header. */ 430 + ent->pos = 0; 431 + } 432 + } 433 + 434 + sysdb->data = (unsigned char *)data; 435 +} 436 + 437 +/* Returns true if the passed-in stat structure describes a 438 + * probably-valid timezone file. */ 439 +static int is_valid_tzfile(const struct stat *st, int fd) 440 +{ 441 + if (fd) { 442 + char buf[20]; 443 + if (read(fd, buf, 20)!=20) { 444 + return 0; 445 + } 446 + lseek(fd, SEEK_SET, 0); 447 + if (memcmp(buf, "TZif", 4)) { 448 + return 0; 449 + } 450 + } 451 + return S_ISREG(st->st_mode) && st->st_size > 20; 452 +} 453 + 454 +/* To allow timezone names to be used case-insensitively, find the 455 + * canonical name for this timezone, if possible. */ 456 +static const char *canonical_tzname(const char *timezone) 457 +{ 458 + if (timezonedb_system) { 459 + timelib_tzdb_index_entry *ent, lookup; 460 + 461 + lookup.id = (char *)timezone; 462 + 463 + ent = bsearch(&lookup, timezonedb_system->index, 464 + timezonedb_system->index_size, sizeof lookup, 465 + sysdbcmp); 466 + if (ent) { 467 + return ent->id; 468 + } 469 + } 470 + 471 + return timezone; 472 +} 473 + 474 +/* Return the mmap()ed tzfile if found, else NULL. On success, the 475 + * length of the mapped data is placed in *length. */ 476 +static char *map_tzfile(const char *timezone, size_t *length) 477 +{ 478 + char fname[PATH_MAX]; 479 + struct stat st; 480 + char *p; 481 + int fd; 482 + 483 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { 484 + return NULL; 485 + } 486 + 487 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone)); 488 + 489 + fd = open(fname, O_RDONLY); 490 + if (fd == -1) { 491 + return NULL; 492 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) { 493 + close(fd); 494 + return NULL; 495 + } 496 + 497 + *length = st.st_size; 498 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 499 + close(fd); 500 + 501 + return p != MAP_FAILED ? p : NULL; 502 +} 503 + 504 +#endif 505 + 506 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb) 507 { 508 int left = 0, right = tzdb->index_size - 1; 509 #ifdef HAVE_SETLOCALE 510 @@ -341,21 +782,88 @@ static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const 511 return 0; 512 } 513 514 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone, 515 + char **map, size_t *maplen, 516 + const timelib_tzdb *tzdb) 517 +{ 518 +#ifdef HAVE_SYSTEM_TZDATA 519 + if (tzdb == timezonedb_system) { 520 + char *orig; 521 + 522 + orig = map_tzfile(timezone, maplen); 523 + if (orig == NULL) { 524 + return 0; 525 + } 526 + 527 + (*tzf) = (unsigned char *)orig; 528 + *map = orig; 529 + return 1; 530 + } 531 + else 532 +#endif 533 + { 534 + return inmem_seek_to_tz_position(tzf, timezone, tzdb); 535 + } 536 +} 537 + 538 const timelib_tzdb *timelib_builtin_db(void) 539 { 540 +#ifdef HAVE_SYSTEM_TZDATA 541 + if (timezonedb_system == NULL) { 542 + timelib_tzdb *tmp = malloc(sizeof *tmp); 543 + 544 + tmp->version = "0.system"; 545 + tmp->data = NULL; 546 + create_zone_index(tmp); 547 + system_location_table = create_location_table(); 548 + fake_data_segment(tmp, system_location_table); 549 + timezonedb_system = tmp; 550 + } 551 + 552 + return timezonedb_system; 553 +#else 554 return &timezonedb_builtin; 555 +#endif 556 } 557 558 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count) 559 { 560 +#ifdef HAVE_SYSTEM_TZDATA 561 + *count = timezonedb_system->index_size; 562 + return timezonedb_system->index; 563 +#else 564 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin); 565 return timezonedb_idx_builtin; 566 +#endif 567 } 568 569 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb) 570 { 571 const unsigned char *tzf; 572 - return (seek_to_tz_position(&tzf, timezone, tzdb)); 573 + 574 +#ifdef HAVE_SYSTEM_TZDATA 575 + if (tzdb == timezonedb_system) { 576 + char fname[PATH_MAX]; 577 + struct stat st; 578 + 579 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { 580 + return 0; 581 + } 582 + 583 + if (system_location_table) { 584 + if (find_zone_info(system_location_table, timezone) != NULL) { 585 + /* found in cache */ 586 + return 1; 587 + } 588 + } 589 + 590 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone)); 591 + 592 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0); 593 + } 594 +#endif 595 + 596 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb)); 597 } 598 599 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz) 600 @@ -380,24 +888,54 @@ static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz) 601 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb) 602 { 603 const unsigned char *tzf; 604 + char *memmap = NULL; 605 + size_t maplen; 606 timelib_tzinfo *tmp; 607 int version; 608 609 - if (seek_to_tz_position(&tzf, timezone, tzdb)) { 610 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) { 611 tmp = timelib_tzinfo_ctor(timezone); 612 613 version = read_preamble(&tzf, tmp); 614 read_header(&tzf, tmp); 615 read_transistions(&tzf, tmp); 616 read_types(&tzf, tmp); 617 - if (version == 2) { 618 - skip_64bit_preamble(&tzf, tmp); 619 - read_64bit_header(&tzf, tmp); 620 - skip_64bit_transistions(&tzf, tmp); 621 - skip_64bit_types(&tzf, tmp); 622 - skip_posix_string(&tzf, tmp); 623 - } 624 - read_location(&tzf, tmp); 625 + 626 +#ifdef HAVE_SYSTEM_TZDATA 627 + if (memmap) { 628 + const struct location_info *li; 629 + 630 + /* TZif-style - grok the location info from the system database, 631 + * if possible. */ 632 + 633 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) { 634 + tmp->location.comments = timelib_strdup(li->comment); 635 + strncpy(tmp->location.country_code, li->code, 2); 636 + tmp->location.longitude = li->longitude; 637 + tmp->location.latitude = li->latitude; 638 + tmp->bc = 1; 639 + } 640 + else { 641 + strcpy(tmp->location.country_code, "??"); 642 + tmp->bc = 0; 643 + tmp->location.comments = timelib_strdup(""); 644 + } 645 + 646 + /* Now done with the mmap segment - discard it. */ 647 + munmap(memmap, maplen); 648 + } else 649 +#endif 650 + { 651 + /* PHP-style - use the embedded info. */ 652 + if (version == 2) { 653 + skip_64bit_preamble(&tzf, tmp); 654 + read_64bit_header(&tzf, tmp); 655 + skip_64bit_transistions(&tzf, tmp); 656 + skip_64bit_types(&tzf, tmp); 657 + skip_posix_string(&tzf, tmp); 658 + } 659 + read_location(&tzf, tmp); 660 + } 661 } else { 662 tmp = NULL; 663 } 664 diff --git a/ext/date/lib/timelib.m4 b/ext/date/lib/timelib.m4 665 index 99bf9fa..4bf7e46 100644 666 --- a/ext/date/lib/timelib.m4 667 +++ b/ext/date/lib/timelib.m4 668 @@ -78,3 +78,16 @@ stdlib.h 669 670 dnl Check for strtoll, atoll 671 AC_CHECK_FUNCS(strtoll atoll strftime gettimeofday) 672 + 673 +PHP_ARG_WITH(system-tzdata, for use of system timezone data, 674 +[ --with-system-tzdata[=DIR] to specify use of system timezone data], 675 +no, no) 676 + 677 +if test "$PHP_SYSTEM_TZDATA" != "no"; then 678 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used]) 679 + 680 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then 681 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA", 682 + [Define for location of system timezone data]) 683 + fi 684 +fi