carlwgeorge / rpms / php

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