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