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
19 KiB

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