987f92
# License: MIT
987f92
# http://opensource.org/licenses/MIT
987f92
987f92
Add support for use of the system timezone database, rather
987f92
than embedding a copy.  Discussed upstream but was not desired.
987f92
987f92
History:
d2c310
r17: adapt for autotool change in 7.2.16RC1
987f92
r16: adapt for timelib 2017.06 (in 7.2.3RC1)
987f92
r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
987f92
r14: improve check for valid tz file
987f92
r13: adapt for upstream changes to use PHP allocator
987f92
r12: adapt for upstream changes for new zic
987f92
r11: use canonical names to avoid more case sensitivity issues
987f92
     round lat/long from zone.tab towards zero per builtin db
987f92
r10: make timezone case insensitive
987f92
r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
987f92
r8: fix compile error without --with-system-tzdata configured
987f92
r7: improve check for valid timezone id to exclude directories
987f92
r6: fix fd leak in r5, fix country code/BC flag use in
987f92
    timezone_identifiers_list() using system db,
987f92
    fix use of PECL timezonedb to override system db,
987f92
r5: reverts addition of "System/Localtime" fake tzname.
987f92
    updated for 5.3.0, parses zone.tab to pick up mapping between
987f92
    timezone name, country code and long/lat coords
987f92
r4: added "System/Localtime" tzname which uses /etc/localtime
987f92
r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
987f92
r2: add filesystem trawl to set up name alias index
987f92
r1: initial revision
987f92
d2c310
diff -up php-7.2.16RC1/ext/date/config0.m4.systzdata php-7.2.16RC1/ext/date/config0.m4
d2c310
--- php-7.2.16RC1/ext/date/config0.m4.systzdata	2019-02-19 11:22:22.223741585 +0100
d2c310
+++ php-7.2.16RC1/ext/date/config0.m4	2019-02-19 11:23:05.089111556 +0100
d2c310
@@ -10,6 +10,19 @@ io.h
d2c310
 dnl Check for strtoll, atoll
d2c310
 AC_CHECK_FUNCS(strtoll atoll)
d2c310
 
d2c310
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
d2c310
+[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
d2c310
+no, no)
d2c310
+
d2c310
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
d2c310
+   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
d2c310
+
d2c310
+   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
d2c310
+      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
d2c310
+                         [Define for location of system timezone data])
d2c310
+   fi
d2c310
+fi
d2c310
+
d2c310
 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
d2c310
 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
d2c310
                  lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
d2c310
diff -up php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata php-7.2.16RC1/ext/date/lib/parse_tz.c
d2c310
--- php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata	2019-02-19 11:13:22.000000000 +0100
d2c310
+++ php-7.2.16RC1/ext/date/lib/parse_tz.c	2019-02-19 11:19:40.245313535 +0100
987f92
@@ -25,8 +25,21 @@
987f92
 #include "timelib.h"
987f92
 #include "timelib_private.h"
987f92
 
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+#include <sys/mman.h>
987f92
+#include <sys/stat.h>
987f92
+#include <limits.h>
987f92
+#include <fcntl.h>
987f92
+#include <unistd.h>
987f92
+
987f92
+#include "php_scandir.h"
987f92
+
987f92
+#else
987f92
 #define TIMELIB_SUPPORTS_V2DATA
987f92
 #include "timezonedb.h"
987f92
+#endif
987f92
+
987f92
+#include <ctype.h>
987f92
 
987f92
 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
987f92
 # if defined(__LITTLE_ENDIAN__)
987f92
@@ -67,6 +80,11 @@ static int read_php_preamble(const unsig
987f92
 {
987f92
 	uint32_t version;
987f92
 
987f92
+	if (memcmp(*tzf, "TZif", 4) == 0) {
987f92
+		*tzf += 20;
987f92
+		return 0;
987f92
+	}
987f92
+
987f92
 	/* read ID */
987f92
 	version = (*tzf)[3] - '0';
987f92
 	*tzf += 4;
987f92
@@ -374,7 +392,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
987f92
 	}
987f92
 }
987f92
 
987f92
-static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+
987f92
+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
987f92
+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
987f92
+#else
987f92
+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
987f92
+#endif
987f92
+
987f92
+/* System timezone database pointer. */
987f92
+static const timelib_tzdb *timezonedb_system;
987f92
+
987f92
+/* Hash table entry for the cache of the zone.tab mapping table. */
987f92
+struct location_info {
987f92
+        char code[2];
987f92
+        double latitude, longitude;
987f92
+        char name[64];
987f92
+        char *comment;
987f92
+        struct location_info *next;
987f92
+};
987f92
+
987f92
+/* Cache of zone.tab. */
987f92
+static struct location_info **system_location_table;
987f92
+
987f92
+/* Size of the zone.tab hash table; a random-ish prime big enough to
987f92
+ * prevent too many collisions. */
987f92
+#define LOCINFO_HASH_SIZE (1021)
987f92
+
987f92
+/* Compute a case insensitive hash of str */
987f92
+static uint32_t tz_hash(const char *str)
987f92
+{
987f92
+    const unsigned char *p = (const unsigned char *)str;
987f92
+    uint32_t hash = 5381;
987f92
+    int c;
987f92
+
987f92
+    while ((c = tolower(*p++)) != '\0') {
987f92
+        hash = (hash << 5) ^ hash ^ c;
987f92
+    }
987f92
+
987f92
+    return hash % LOCINFO_HASH_SIZE;
987f92
+}
987f92
+
987f92
+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
987f92
+ * parsed string on success, or NULL on parse error.  On success,
987f92
+ * writes the parsed number to *result. */
987f92
+static char *parse_iso6709(char *p, double *result)
987f92
+{
987f92
+    double v, sign;
987f92
+    char *pend;
987f92
+    size_t len;
987f92
+
987f92
+    if (*p == '+')
987f92
+        sign = 1.0;
987f92
+    else if (*p == '-')
987f92
+        sign = -1.0;
987f92
+    else
987f92
+        return NULL;
987f92
+
987f92
+    p++;
987f92
+    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
987f92
+        ;;
987f92
+
987f92
+    /* Annoying encoding used by zone.tab has no decimal point, so use
987f92
+     * the length to determine the format:
987f92
+     * 
987f92
+     * 4 = DDMM
987f92
+     * 5 = DDDMM
987f92
+     * 6 = DDMMSS
987f92
+     * 7 = DDDMMSS
987f92
+     */
987f92
+    len = pend - p;
987f92
+    if (len < 4 || len > 7) {
987f92
+        return NULL;
987f92
+    }
987f92
+
987f92
+    /* p => [D]DD */
987f92
+    v = (p[0] - '0') * 10.0 + (p[1] - '0');
987f92
+    p += 2;
987f92
+    if (len == 5 || len == 7)
987f92
+        v = v * 10.0 + (*p++ - '0');
987f92
+    /* p => MM[SS] */
987f92
+    v += (10.0 * (p[0] - '0')
987f92
+          + p[1] - '0') / 60.0;
987f92
+    p += 2;
987f92
+    /* p => [SS] */
987f92
+    if (len > 5) {
987f92
+        v += (10.0 * (p[0] - '0')
987f92
+              + p[1] - '0') / 3600.0;
987f92
+        p += 2;
987f92
+    }
987f92
+
987f92
+    /* Round to five decimal place, not because it's a good idea,
987f92
+     * but, because the builtin data uses rounded data, so, match
987f92
+     * that. */
987f92
+    *result = trunc(v * sign * 100000.0) / 100000.0;
987f92
+
987f92
+    return p;
987f92
+}
987f92
+
987f92
+/* This function parses the zone.tab file to build up the mapping of
987f92
+ * timezone to country code and geographic location, and returns a
987f92
+ * hash table.  The hash table is indexed by the function:
987f92
+ *
987f92
+ *   tz_hash(timezone-name)
987f92
+ */
987f92
+static struct location_info **create_location_table(void)
987f92
+{
987f92
+    struct location_info **li, *i;
987f92
+    char zone_tab[PATH_MAX];
987f92
+    char line[512];
987f92
+    FILE *fp;
987f92
+
987f92
+    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
987f92
+
987f92
+    fp = fopen(zone_tab, "r");
987f92
+    if (!fp) {
987f92
+        return NULL;
987f92
+    }
987f92
+
987f92
+    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
987f92
+
987f92
+    while (fgets(line, sizeof line, fp)) {
987f92
+        char *p = line, *code, *name, *comment;
987f92
+        uint32_t hash;
987f92
+        double latitude, longitude;
987f92
+
987f92
+        while (isspace(*p))
987f92
+            p++;
987f92
+
987f92
+        if (*p == '#' || *p == '\0' || *p == '\n')
987f92
+            continue;
987f92
+        
987f92
+        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
987f92
+            continue;
987f92
+        
987f92
+        /* code => AA */
987f92
+        code = p;
987f92
+        p[2] = 0;
987f92
+        p += 3;
987f92
+
987f92
+        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
987f92
+        p = parse_iso6709(p, &latitude);
987f92
+        if (!p) {
987f92
+            continue;
987f92
+        }
987f92
+        p = parse_iso6709(p, &longitude);
987f92
+        if (!p) {
987f92
+            continue;
987f92
+        }
987f92
+
987f92
+        if (!p || *p != '\t') {
987f92
+            continue;
987f92
+        }
987f92
+
987f92
+        /* name = string */
987f92
+        name = ++p;
987f92
+        while (*p != '\t' && *p && *p != '\n')
987f92
+            p++;
987f92
+
987f92
+        *p++ = '\0';
987f92
+
987f92
+        /* comment = string */
987f92
+        comment = p;
987f92
+        while (*p != '\t' && *p && *p != '\n')
987f92
+            p++;
987f92
+
987f92
+        if (*p == '\n' || *p == '\t')
987f92
+            *p = '\0';
987f92
+        
987f92
+        hash = tz_hash(name);
987f92
+        i = malloc(sizeof *i);
987f92
+        memcpy(i->code, code, 2);
987f92
+        strncpy(i->name, name, sizeof i->name);
987f92
+        i->comment = strdup(comment);
987f92
+        i->longitude = longitude;
987f92
+        i->latitude = latitude;
987f92
+        i->next = li[hash];
987f92
+        li[hash] = i;
987f92
+        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
987f92
+    }
987f92
+
987f92
+    fclose(fp);
987f92
+
987f92
+    return li;
987f92
+}
987f92
+
987f92
+/* Return location info from hash table, using given timezone name.
987f92
+ * Returns NULL if the name could not be found. */
987f92
+const struct location_info *find_zone_info(struct location_info **li, 
987f92
+                                           const char *name)
987f92
+{
987f92
+    uint32_t hash = tz_hash(name);
987f92
+    const struct location_info *l;
987f92
+
987f92
+    if (!li) {
987f92
+        return NULL;
987f92
+    }
987f92
+
987f92
+    for (l = li[hash]; l; l = l->next) {
987f92
+        if (timelib_strcasecmp(l->name, name) == 0)
987f92
+            return l;
987f92
+    }
987f92
+
987f92
+    return NULL;
987f92
+}    
987f92
+
987f92
+/* Filter out some non-tzdata files and the posix/right databases, if
987f92
+ * present. */
987f92
+static int index_filter(const struct dirent *ent)
987f92
+{
987f92
+	return strcmp(ent->d_name, ".") != 0
987f92
+		&& strcmp(ent->d_name, "..") != 0
987f92
+		&& strcmp(ent->d_name, "posix") != 0
987f92
+		&& strcmp(ent->d_name, "posixrules") != 0
987f92
+		&& strcmp(ent->d_name, "right") != 0
987f92
+		&& strstr(ent->d_name, ".list") == NULL
987f92
+		&& strstr(ent->d_name, ".tab") == NULL;
987f92
+}
987f92
+
987f92
+static int sysdbcmp(const void *first, const void *second)
987f92
+{
987f92
+        const timelib_tzdb_index_entry *alpha = first, *beta = second;
987f92
+
987f92
+        return timelib_strcasecmp(alpha->id, beta->id);
987f92
+}
987f92
+
987f92
+
987f92
+/* Create the zone identifier index by trawling the filesystem. */
987f92
+static void create_zone_index(timelib_tzdb *db)
987f92
+{
987f92
+	size_t dirstack_size,  dirstack_top;
987f92
+	size_t index_size, index_next;
987f92
+	timelib_tzdb_index_entry *db_index;
987f92
+	char **dirstack;
987f92
+
987f92
+	/* LIFO stack to hold directory entries to scan; each slot is a
987f92
+	 * directory name relative to the zoneinfo prefix. */
987f92
+	dirstack_size = 32;
987f92
+	dirstack = malloc(dirstack_size * sizeof *dirstack);
987f92
+	dirstack_top = 1;
987f92
+	dirstack[0] = strdup("");
987f92
+	
987f92
+	/* Index array. */
987f92
+	index_size = 64;
987f92
+	db_index = malloc(index_size * sizeof *db_index);
987f92
+	index_next = 0;
987f92
+
987f92
+	do {
987f92
+		struct dirent **ents;
987f92
+		char name[PATH_MAX], *top;
987f92
+		int count;
987f92
+
987f92
+		/* Pop the top stack entry, and iterate through its contents. */
987f92
+		top = dirstack[--dirstack_top];
987f92
+		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
987f92
+
987f92
+		count = php_scandir(name, &ents, index_filter, php_alphasort);
987f92
+
987f92
+		while (count > 0) {
987f92
+			struct stat st;
987f92
+			const char *leaf = ents[count - 1]->d_name;
987f92
+
987f92
+			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
987f92
+				 top, leaf);
987f92
+			
987f92
+			if (strlen(name) && stat(name, &st) == 0) {
987f92
+				/* Name, relative to the zoneinfo prefix. */
987f92
+				const char *root = top;
987f92
+
987f92
+				if (root[0] == '/') root++;
987f92
+
987f92
+				snprintf(name, sizeof name, "%s%s%s", root, 
987f92
+					 *root ? "/": "", leaf);
987f92
+
987f92
+				if (S_ISDIR(st.st_mode)) {
987f92
+					if (dirstack_top == dirstack_size) {
987f92
+						dirstack_size *= 2;
987f92
+						dirstack = realloc(dirstack, 
987f92
+								   dirstack_size * sizeof *dirstack);
987f92
+					}
987f92
+					dirstack[dirstack_top++] = strdup(name);
987f92
+				}
987f92
+				else {
987f92
+					if (index_next == index_size) {
987f92
+						index_size *= 2;
987f92
+						db_index = realloc(db_index,
987f92
+								   index_size * sizeof *db_index);
987f92
+					}
987f92
+
987f92
+					db_index[index_next++].id = strdup(name);
987f92
+				}
987f92
+			}
987f92
+
987f92
+			free(ents[--count]);
987f92
+		}
987f92
+		
987f92
+		if (count != -1) free(ents);
987f92
+		free(top);
987f92
+	} while (dirstack_top);
987f92
+
987f92
+        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
987f92
+
987f92
+	db->index = db_index;
987f92
+	db->index_size = index_next;
987f92
+
987f92
+	free(dirstack);
987f92
+}
987f92
+
987f92
+#define FAKE_HEADER "1234\0??\1??"
987f92
+#define FAKE_UTC_POS (7 - 4)
987f92
+
987f92
+/* Create a fake data segment for database 'sysdb'. */
987f92
+static void fake_data_segment(timelib_tzdb *sysdb,
987f92
+                              struct location_info **info)
987f92
+{
987f92
+        size_t n;
987f92
+        char *data, *p;
987f92
+        
987f92
+        data = malloc(3 * sysdb->index_size + 7);
987f92
+
987f92
+        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
987f92
+
987f92
+        for (n = 0; n < sysdb->index_size; n++) {
987f92
+                const struct location_info *li;
987f92
+                timelib_tzdb_index_entry *ent;
987f92
+
987f92
+                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
987f92
+
987f92
+                /* Lookup the timezone name in the hash table. */
987f92
+                if (strcmp(ent->id, "UTC") == 0) {
987f92
+                        ent->pos = FAKE_UTC_POS;
987f92
+                        continue;
987f92
+                }
987f92
+
987f92
+                li = find_zone_info(info, ent->id);
987f92
+                if (li) {
987f92
+                        /* If found, append the BC byte and the
987f92
+                         * country code; set the position for this
987f92
+                         * section of timezone data.  */
987f92
+                        ent->pos = (p - data) - 4;
987f92
+                        *p++ = '\1';
987f92
+                        *p++ = li->code[0];
987f92
+                        *p++ = li->code[1];
987f92
+                }
987f92
+                else {
987f92
+                        /* If not found, the timezone data can
987f92
+                         * point at the header. */
987f92
+                        ent->pos = 0;
987f92
+                }
987f92
+        }
987f92
+        
987f92
+        sysdb->data = (unsigned char *)data;
987f92
+}
987f92
+
987f92
+/* Returns true if the passed-in stat structure describes a
987f92
+ * probably-valid timezone file. */
987f92
+static int is_valid_tzfile(const struct stat *st, int fd)
987f92
+{
987f92
+	if (fd) {
987f92
+		char buf[20];
987f92
+		if (read(fd, buf, 20)!=20) {
987f92
+			return 0;
987f92
+		}
987f92
+		lseek(fd, SEEK_SET, 0);
987f92
+		if (memcmp(buf, "TZif", 4)) {
987f92
+			return 0;
987f92
+		}
987f92
+	}
987f92
+	return S_ISREG(st->st_mode) && st->st_size > 20;
987f92
+}
987f92
+
987f92
+/* To allow timezone names to be used case-insensitively, find the
987f92
+ * canonical name for this timezone, if possible. */
987f92
+static const char *canonical_tzname(const char *timezone)
987f92
+{
987f92
+    if (timezonedb_system) {
987f92
+        timelib_tzdb_index_entry *ent, lookup;
987f92
+
987f92
+        lookup.id = (char *)timezone;
987f92
+
987f92
+        ent = bsearch(&lookup, timezonedb_system->index,
987f92
+                      timezonedb_system->index_size, sizeof lookup,
987f92
+                      sysdbcmp);
987f92
+        if (ent) {
987f92
+            return ent->id;
987f92
+        }
987f92
+    }
987f92
+
987f92
+    return timezone;
987f92
+}
987f92
+
987f92
+/* Return the mmap()ed tzfile if found, else NULL.  On success, the
987f92
+ * length of the mapped data is placed in *length. */
987f92
+static char *map_tzfile(const char *timezone, size_t *length)
987f92
+{
987f92
+	char fname[PATH_MAX];
987f92
+	struct stat st;
987f92
+	char *p;
987f92
+	int fd;
987f92
+	
987f92
+	if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
987f92
+		return NULL;
987f92
+	}
987f92
+
987f92
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
987f92
+
987f92
+	fd = open(fname, O_RDONLY);
987f92
+	if (fd == -1) {
987f92
+		return NULL;
987f92
+	} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
987f92
+		close(fd);
987f92
+		return NULL;
987f92
+	}
987f92
+
987f92
+	*length = st.st_size;
987f92
+	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
987f92
+	close(fd);
987f92
+	
987f92
+	return p != MAP_FAILED ? p : NULL;
987f92
+}
987f92
+
987f92
+#endif
987f92
+
987f92
+static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
987f92
 {
987f92
 	int left = 0, right = tzdb->index_size - 1;
987f92
 
987f92
@@ -400,9 +840,48 @@ static int seek_to_tz_position(const uns
987f92
 	return 0;
987f92
 }
987f92
 
987f92
+static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
987f92
+			       char **map, size_t *maplen,
987f92
+			       const timelib_tzdb *tzdb)
987f92
+{
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+	if (tzdb == timezonedb_system) {
987f92
+		char *orig;
987f92
+
987f92
+		orig = map_tzfile(timezone, maplen);
987f92
+		if (orig == NULL) {
987f92
+			return 0;
987f92
+		}
987f92
+
987f92
+		(*tzf) = (unsigned char *)orig;
987f92
+		*map = orig;
987f92
+        return 1;
987f92
+	}
987f92
+	else
987f92
+#endif
987f92
+	{
987f92
+		return inmem_seek_to_tz_position(tzf, timezone, tzdb);
987f92
+	}
987f92
+}
987f92
+
987f92
 const timelib_tzdb *timelib_builtin_db(void)
987f92
 {
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+	if (timezonedb_system == NULL) {
987f92
+		timelib_tzdb *tmp = malloc(sizeof *tmp);
987f92
+
987f92
+		tmp->version = "0.system";
987f92
+		tmp->data = NULL;
987f92
+		create_zone_index(tmp);
987f92
+		system_location_table = create_location_table();
987f92
+		fake_data_segment(tmp, system_location_table);
987f92
+		timezonedb_system = tmp;
987f92
+	}
987f92
+
987f92
+	return timezonedb_system;
987f92
+#else
987f92
 	return &timezonedb_builtin;
987f92
+#endif
987f92
 }
987f92
 
987f92
 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
987f92
@@ -414,7 +893,30 @@ const timelib_tzdb_index_entry *timelib_
987f92
 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
987f92
 {
987f92
 	const unsigned char *tzf;
987f92
-	return (seek_to_tz_position(&tzf, timezone, tzdb));
987f92
+
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+	if (tzdb == timezonedb_system) {
987f92
+		char fname[PATH_MAX];
987f92
+		struct stat st;
987f92
+
987f92
+		if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
987f92
+			return 0;
987f92
+		}
987f92
+
987f92
+		if (system_location_table) {
987f92
+			if (find_zone_info(system_location_table, timezone) != NULL) {
987f92
+				/* found in cache */
987f92
+				return 1;
987f92
+			}
987f92
+		}
987f92
+
987f92
+		snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
987f92
+
987f92
+		return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
987f92
+	}
987f92
+#endif
987f92
+
987f92
+	return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
987f92
 }
987f92
 
987f92
 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
987f92
@@ -456,12 +958,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
987f92
 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code)
987f92
 {
987f92
 	const unsigned char *tzf;
987f92
+	char *memmap = NULL;
987f92
+	size_t maplen;
987f92
 	timelib_tzinfo *tmp;
987f92
 	int version;
987f92
 	int transitions_result, types_result;
987f92
 	unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
987f92
 
987f92
-	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
987f92
+	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
987f92
 		tmp = timelib_tzinfo_ctor(timezone);
987f92
 
987f92
 		version = read_preamble(&tzf, tmp, &type);
987f92
@@ -484,6 +988,29 @@ timelib_tzinfo *timelib_parse_tzfile(cha
987f92
 			timelib_tzinfo_dtor(tmp);
987f92
 			return NULL;
987f92
 		}
987f92
+
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+		if (memmap) {
987f92
+			const struct location_info *li;
987f92
+
987f92
+			/* TZif-style - grok the location info from the system database,
987f92
+			 * if possible. */
987f92
+
987f92
+			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
987f92
+				tmp->location.comments = timelib_strdup(li->comment);
987f92
+				strncpy(tmp->location.country_code, li->code, 2);
987f92
+				tmp->location.longitude = li->longitude;
987f92
+				tmp->location.latitude = li->latitude;
987f92
+				tmp->bc = 1;
987f92
+			}
987f92
+			else {
987f92
+				set_default_location_and_comments(&tzf, tmp);
987f92
+			}
987f92
+
987f92
+			/* Now done with the mmap segment - discard it. */
987f92
+			munmap(memmap, maplen);
987f92
+		} else {
987f92
+#endif
987f92
 		if (version == 2 || version == 3) {
987f92
 			if (!skip_64bit_preamble(&tzf, tmp)) {
987f92
 				/* 64 bit preamble is not in place */
987f92
@@ -501,6 +1028,9 @@ timelib_tzinfo *timelib_parse_tzfile(cha
987f92
 		} else {
987f92
 			set_default_location_and_comments(&tzf, tmp);
987f92
 		}
987f92
+#ifdef HAVE_SYSTEM_TZDATA
987f92
+		}
987f92
+#endif
987f92
 	} else {
987f92
 		*error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
987f92
 		tmp = NULL;