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