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