Blame SOURCES/php-7.1.0-systzdata-v14.patch

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