You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
18 KiB

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