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.

671 lines
18 KiB

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