Blame SOURCES/php-7.2.3-systzdata-v16.patch

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