Blame SOURCES/php-5.6.13-systzdata-v12.patch

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