]> git.pld-linux.org Git - packages/php.git/blame - php-systzdata.patch
cleanups
[packages/php.git] / php-systzdata.patch
CommitLineData
528a1414
ER
1Add support for use of the system timezone database, rather
2than embedding a copy. Discussed upstream but was not desired.
3
4History:
fc674d5f 5r10 : make timezone case insensitive
203a025a
ER
6r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
7r8: fix compile error without --with-system-tzdata configured
528a1414
ER
8r7: improve check for valid timezone id to exclude directories
9r6: fix fd leak in r5, fix country code/BC flag use in
10 timezone_identifiers_list() using system db,
11 fix use of PECL timezonedb to override system db,
12r5: reverts addition of "System/Localtime" fake tzname.
13 updated for 5.3.0, parses zone.tab to pick up mapping between
14 timezone name, country code and long/lat coords
15r4: added "System/Localtime" tzname which uses /etc/localtime
16r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
17r2: add filesystem trawl to set up name alias index
18r1: initial revision
19
fc674d5f
ER
20--- php5.orig/ext/date/lib/parse_tz.c
21+++ php5/ext/date/lib/parse_tz.c
203a025a 22@@ -24,6 +24,16 @@
528a1414
ER
23
24 #include "timelib.h"
25
26+#ifdef HAVE_SYSTEM_TZDATA
27+#include <sys/mman.h>
28+#include <sys/stat.h>
29+#include <limits.h>
30+#include <fcntl.h>
31+#include <unistd.h>
32+
33+#include "php_scandir.h"
34+#endif
35+
36 #include <stdio.h>
37
38 #ifdef HAVE_LOCALE_H
203a025a 39@@ -35,7 +45,12 @@
528a1414
ER
40 #else
41 #include <strings.h>
42 #endif
43+
44+#ifndef HAVE_SYSTEM_TZDATA
45 #include "timezonedb.h"
46+#endif
47+
48+#include <ctype.h>
49
50 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
51 # if defined(__LITTLE_ENDIAN__)
203a025a 52@@ -55,9 +70,14 @@
528a1414
ER
53
54 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
55 {
56- /* skip ID */
57- *tzf += 4;
58-
59+ if (memcmp(tzf, "TZif", 4) == 0) {
60+ *tzf += 20;
61+ return;
62+ }
63+
64+ /* skip ID */
65+ *tzf += 4;
66+
67 /* read BC flag */
68 tz->bc = (**tzf == '\1');
69 *tzf += 1;
fc674d5f 70@@ -260,7 +280,405 @@ void timelib_dump_tzinfo(timelib_tzinfo
528a1414
ER
71 }
72 }
73
74-static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
75+#ifdef HAVE_SYSTEM_TZDATA
76+
77+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
78+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
79+#else
80+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
81+#endif
82+
83+/* System timezone database pointer. */
fc674d5f 84+static const timelib_tzdb *timezonedb_system;
528a1414
ER
85+
86+/* Hash table entry for the cache of the zone.tab mapping table. */
87+struct location_info {
88+ char code[2];
89+ double latitude, longitude;
90+ char name[64];
91+ char *comment;
92+ struct location_info *next;
93+};
94+
95+/* Cache of zone.tab. */
96+static struct location_info **system_location_table;
97+
98+/* Size of the zone.tab hash table; a random-ish prime big enough to
99+ * prevent too many collisions. */
100+#define LOCINFO_HASH_SIZE (1021)
101+
fc674d5f 102+/* Compute a case insensitive hash of str */
528a1414
ER
103+static uint32_t tz_hash(const char *str)
104+{
105+ const unsigned char *p = (const unsigned char *)str;
106+ uint32_t hash = 5381;
107+ int c;
108+
fc674d5f 109+ while ((c = tolower(*p++)) != '\0') {
528a1414
ER
110+ hash = (hash << 5) ^ hash ^ c;
111+ }
112+
113+ return hash % LOCINFO_HASH_SIZE;
114+}
115+
116+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
117+ * parsed string on success, or NULL on parse error. On success,
118+ * writes the parsed number to *result. */
119+static char *parse_iso6709(char *p, double *result)
120+{
121+ double v, sign;
122+ char *pend;
123+ size_t len;
124+
125+ if (*p == '+')
126+ sign = 1.0;
127+ else if (*p == '-')
128+ sign = -1.0;
129+ else
130+ return NULL;
131+
132+ p++;
133+ for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
134+ ;;
135+
136+ /* Annoying encoding used by zone.tab has no decimal point, so use
137+ * the length to determine the format:
138+ *
139+ * 4 = DDMM
140+ * 5 = DDDMM
141+ * 6 = DDMMSS
142+ * 7 = DDDMMSS
143+ */
144+ len = pend - p;
145+ if (len < 4 || len > 7) {
146+ return NULL;
147+ }
148+
149+ /* p => [D]DD */
150+ v = (p[0] - '0') * 10.0 + (p[1] - '0');
151+ p += 2;
152+ if (len == 5 || len == 7)
153+ v = v * 10.0 + (*p++ - '0');
154+ /* p => MM[SS] */
155+ v += (10.0 * (p[0] - '0')
156+ + p[1] - '0') / 60.0;
157+ p += 2;
158+ /* p => [SS] */
159+ if (len > 5) {
160+ v += (10.0 * (p[0] - '0')
161+ + p[1] - '0') / 3600.0;
162+ p += 2;
163+ }
164+
165+ /* Round to five decimal place, not because it's a good idea,
166+ * but, because the builtin data uses rounded data, so, match
167+ * that. */
168+ *result = round(v * sign * 100000.0) / 100000.0;
169+
170+ return p;
171+}
172+
173+/* This function parses the zone.tab file to build up the mapping of
174+ * timezone to country code and geographic location, and returns a
175+ * hash table. The hash table is indexed by the function:
176+ *
177+ * tz_hash(timezone-name)
178+ */
179+static struct location_info **create_location_table(void)
180+{
181+ struct location_info **li, *i;
182+ char zone_tab[PATH_MAX];
183+ char line[512];
184+ FILE *fp;
185+
186+ strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
187+
188+ fp = fopen(zone_tab, "r");
189+ if (!fp) {
190+ return NULL;
191+ }
192+
193+ li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
194+
195+ while (fgets(line, sizeof line, fp)) {
196+ char *p = line, *code, *name, *comment;
197+ uint32_t hash;
198+ double latitude, longitude;
199+
200+ while (isspace(*p))
201+ p++;
202+
203+ if (*p == '#' || *p == '\0' || *p == '\n')
204+ continue;
205+
206+ if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
207+ continue;
208+
209+ /* code => AA */
210+ code = p;
211+ p[2] = 0;
212+ p += 3;
213+
214+ /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
215+ p = parse_iso6709(p, &latitude);
216+ if (!p) {
217+ continue;
218+ }
219+ p = parse_iso6709(p, &longitude);
220+ if (!p) {
221+ continue;
222+ }
223+
224+ if (!p || *p != '\t') {
225+ continue;
226+ }
227+
228+ /* name = string */
229+ name = ++p;
230+ while (*p != '\t' && *p && *p != '\n')
231+ p++;
232+
233+ *p++ = '\0';
234+
235+ /* comment = string */
236+ comment = p;
237+ while (*p != '\t' && *p && *p != '\n')
238+ p++;
239+
240+ if (*p == '\n' || *p == '\t')
241+ *p = '\0';
242+
243+ hash = tz_hash(name);
244+ i = malloc(sizeof *i);
245+ memcpy(i->code, code, 2);
246+ strncpy(i->name, name, sizeof i->name);
247+ i->comment = strdup(comment);
248+ i->longitude = longitude;
249+ i->latitude = latitude;
250+ i->next = li[hash];
251+ li[hash] = i;
252+ /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
253+ }
254+
255+ fclose(fp);
256+
257+ return li;
258+}
259+
260+/* Return location info from hash table, using given timezone name.
261+ * Returns NULL if the name could not be found. */
262+const struct location_info *find_zone_info(struct location_info **li,
263+ const char *name)
264+{
265+ uint32_t hash = tz_hash(name);
266+ const struct location_info *l;
267+
268+ if (!li) {
269+ return NULL;
270+ }
271+
272+ for (l = li[hash]; l; l = l->next) {
273+ if (strcasecmp(l->name, name) == 0)
274+ return l;
275+ }
276+
277+ return NULL;
278+}
279+
280+/* Filter out some non-tzdata files and the posix/right databases, if
281+ * present. */
282+static int index_filter(const struct dirent *ent)
283+{
284+ return strcmp(ent->d_name, ".") != 0
285+ && strcmp(ent->d_name, "..") != 0
286+ && strcmp(ent->d_name, "posix") != 0
287+ && strcmp(ent->d_name, "posixrules") != 0
288+ && strcmp(ent->d_name, "right") != 0
289+ && strstr(ent->d_name, ".tab") == NULL;
290+}
291+
292+static int sysdbcmp(const void *first, const void *second)
293+{
294+ const timelib_tzdb_index_entry *alpha = first, *beta = second;
295+
296+ return strcmp(alpha->id, beta->id);
297+}
298+
299+
300+/* Create the zone identifier index by trawling the filesystem. */
301+static void create_zone_index(timelib_tzdb *db)
302+{
303+ size_t dirstack_size, dirstack_top;
304+ size_t index_size, index_next;
305+ timelib_tzdb_index_entry *db_index;
306+ char **dirstack;
307+
308+ /* LIFO stack to hold directory entries to scan; each slot is a
309+ * directory name relative to the zoneinfo prefix. */
310+ dirstack_size = 32;
311+ dirstack = malloc(dirstack_size * sizeof *dirstack);
312+ dirstack_top = 1;
313+ dirstack[0] = strdup("");
314+
315+ /* Index array. */
316+ index_size = 64;
317+ db_index = malloc(index_size * sizeof *db_index);
318+ index_next = 0;
319+
320+ do {
321+ struct dirent **ents;
322+ char name[PATH_MAX], *top;
323+ int count;
324+
325+ /* Pop the top stack entry, and iterate through its contents. */
326+ top = dirstack[--dirstack_top];
327+ snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
328+
329+ count = php_scandir(name, &ents, index_filter, php_alphasort);
330+
331+ while (count > 0) {
332+ struct stat st;
333+ const char *leaf = ents[count - 1]->d_name;
334+
335+ snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
336+ top, leaf);
337+
338+ if (strlen(name) && stat(name, &st) == 0) {
339+ /* Name, relative to the zoneinfo prefix. */
340+ const char *root = top;
341+
342+ if (root[0] == '/') root++;
343+
344+ snprintf(name, sizeof name, "%s%s%s", root,
345+ *root ? "/": "", leaf);
346+
347+ if (S_ISDIR(st.st_mode)) {
348+ if (dirstack_top == dirstack_size) {
349+ dirstack_size *= 2;
350+ dirstack = realloc(dirstack,
351+ dirstack_size * sizeof *dirstack);
352+ }
353+ dirstack[dirstack_top++] = strdup(name);
354+ }
355+ else {
356+ if (index_next == index_size) {
357+ index_size *= 2;
358+ db_index = realloc(db_index,
359+ index_size * sizeof *db_index);
360+ }
361+
362+ db_index[index_next++].id = strdup(name);
363+ }
364+ }
365+
366+ free(ents[--count]);
367+ }
368+
369+ if (count != -1) free(ents);
370+ free(top);
371+ } while (dirstack_top);
372+
373+ qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
374+
375+ db->index = db_index;
376+ db->index_size = index_next;
377+
378+ free(dirstack);
379+}
380+
381+#define FAKE_HEADER "1234\0??\1??"
382+#define FAKE_UTC_POS (7 - 4)
383+
384+/* Create a fake data segment for database 'sysdb'. */
385+static void fake_data_segment(timelib_tzdb *sysdb,
386+ struct location_info **info)
387+{
388+ size_t n;
389+ char *data, *p;
390+
391+ data = malloc(3 * sysdb->index_size + 7);
392+
393+ p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
394+
395+ for (n = 0; n < sysdb->index_size; n++) {
396+ const struct location_info *li;
397+ timelib_tzdb_index_entry *ent;
398+
399+ ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
400+
401+ /* Lookup the timezone name in the hash table. */
402+ if (strcmp(ent->id, "UTC") == 0) {
403+ ent->pos = FAKE_UTC_POS;
404+ continue;
405+ }
406+
407+ li = find_zone_info(info, ent->id);
408+ if (li) {
409+ /* If found, append the BC byte and the
410+ * country code; set the position for this
411+ * section of timezone data. */
412+ ent->pos = (p - data) - 4;
413+ *p++ = '\1';
414+ *p++ = li->code[0];
415+ *p++ = li->code[1];
416+ }
417+ else {
418+ /* If not found, the timezone data can
419+ * point at the header. */
420+ ent->pos = 0;
421+ }
422+ }
423+
424+ sysdb->data = (unsigned char *)data;
425+}
426+
427+/* Returns true if the passed-in stat structure describes a
428+ * probably-valid timezone file. */
429+static int is_valid_tzfile(const struct stat *st)
430+{
431+ return S_ISREG(st->st_mode) && st->st_size > 20;
432+}
433+
434+/* Return the mmap()ed tzfile if found, else NULL. On success, the
435+ * length of the mapped data is placed in *length. */
436+static char *map_tzfile(const char *timezone, size_t *length)
437+{
438+ char fname[PATH_MAX];
439+ struct stat st;
440+ char *p;
441+ int fd;
442+
443+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
444+ return NULL;
445+ }
446+
fc674d5f
ER
447+ if (system_location_table) {
448+ const struct location_info *li;
449+ if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
450+ /* Use the stored name to avoid case issue */
451+ timezone = li->name;
452+ }
453+ }
528a1414
ER
454+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
455+
456+ fd = open(fname, O_RDONLY);
457+ if (fd == -1) {
458+ return NULL;
459+ } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
460+ close(fd);
461+ return NULL;
462+ }
463+
464+ *length = st.st_size;
465+ p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
466+ close(fd);
467+
468+ return p != MAP_FAILED ? p : NULL;
469+}
470+
471+#endif
472+
473+static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
474 {
475 int left = 0, right = tzdb->index_size - 1;
476 #ifdef HAVE_SETLOCALE
fc674d5f 477@@ -299,36 +717,135 @@ static int seek_to_tz_position(const uns
528a1414
ER
478 return 0;
479 }
480
481+static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
482+ char **map, size_t *maplen,
483+ const timelib_tzdb *tzdb)
484+{
203a025a 485+#ifdef HAVE_SYSTEM_TZDATA
528a1414
ER
486+ if (tzdb == timezonedb_system) {
487+ char *orig;
488+
489+ orig = map_tzfile(timezone, maplen);
490+ if (orig == NULL) {
491+ return 0;
492+ }
493+
494+ (*tzf) = (unsigned char *)orig ;
495+ *map = orig;
496+
497+ return 1;
498+ }
203a025a
ER
499+ else
500+#endif
501+ {
528a1414
ER
502+ return inmem_seek_to_tz_position(tzf, timezone, tzdb);
503+ }
504+}
505+
506 const timelib_tzdb *timelib_builtin_db(void)
507 {
508+#ifdef HAVE_SYSTEM_TZDATA
509+ if (timezonedb_system == NULL) {
510+ timelib_tzdb *tmp = malloc(sizeof *tmp);
511+
512+ tmp->version = "0.system";
513+ tmp->data = NULL;
514+ create_zone_index(tmp);
515+ system_location_table = create_location_table();
516+ fake_data_segment(tmp, system_location_table);
517+ timezonedb_system = tmp;
518+ }
519+
520+
521+ return timezonedb_system;
522+#else
523 return &timezonedb_builtin;
524+#endif
525 }
526
527 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
528 {
529+#ifdef HAVE_SYSTEM_TZDATA
530+ *count = timezonedb_system->index_size;
531+ return timezonedb_system->index;
532+#else
533 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
534 return timezonedb_idx_builtin;
535+#endif
536 }
537
538 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
539 {
540 const unsigned char *tzf;
541- return (seek_to_tz_position(&tzf, timezone, tzdb));
542+
543+#ifdef HAVE_SYSTEM_TZDATA
544+ if (tzdb == timezonedb_system) {
545+ char fname[PATH_MAX];
546+ struct stat st;
547+
548+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
fc674d5f
ER
549+ return 0;
550+ }
551+
552+ if (system_location_table) {
553+ if (find_zone_info(system_location_table, timezone) != NULL) {
554+ /* found in cache */
555+ return 1;
556+ }
528a1414
ER
557+ }
558+
559+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
560+
561+ return stat(fname, &st) == 0 && is_valid_tzfile(&st);
562+ }
563+#endif
564+
565+ return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
566 }
567
568 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
569 {
570 const unsigned char *tzf;
571+ char *memmap = NULL;
572+ size_t maplen;
573 timelib_tzinfo *tmp;
574
575- if (seek_to_tz_position(&tzf, timezone, tzdb)) {
576+ if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
577 tmp = timelib_tzinfo_ctor(timezone);
578
579 read_preamble(&tzf, tmp);
580 read_header(&tzf, tmp);
581 read_transistions(&tzf, tmp);
582 read_types(&tzf, tmp);
583- read_location(&tzf, tmp);
584+
585+#ifdef HAVE_SYSTEM_TZDATA
586+ if (memmap) {
587+ const struct location_info *li;
588+
589+ /* TZif-style - grok the location info from the system database,
590+ * if possible. */
591+
592+ if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
593+ tmp->location.comments = strdup(li->comment);
594+ strncpy(tmp->location.country_code, li->code, 2);
595+ tmp->location.longitude = li->longitude;
596+ tmp->location.latitude = li->latitude;
597+ tmp->bc = 1;
598+ }
599+ else {
600+ strcpy(tmp->location.country_code, "??");
601+ tmp->bc = 0;
602+ tmp->location.comments = strdup("");
603+ }
604+
605+ /* Now done with the mmap segment - discard it. */
606+ munmap(memmap, maplen);
203a025a 607+ } else
528a1414 608+#endif
203a025a 609+ {
528a1414
ER
610+ /* PHP-style - use the embedded info. */
611+ read_location(&tzf, tmp);
612+ }
613 } else {
614 tmp = NULL;
615 }
fc674d5f
ER
616--- php5.orig/ext/date/lib/timelib.m4
617+++ php5/ext/date/lib/timelib.m4
528a1414
ER
618@@ -78,3 +78,17 @@ stdlib.h
619
620 dnl Check for strtoll, atoll
621 AC_CHECK_FUNCS(strtoll atoll strftime)
622+
623+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
624+[ --with-system-tzdata[=DIR] to specify use of system timezone data],
625+no, no)
626+
627+if test "$PHP_SYSTEM_TZDATA" != "no"; then
628+ AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
629+
630+ if test "$PHP_SYSTEM_TZDATA" != "yes"; then
631+ AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
632+ [Define for location of system timezone data])
633+ fi
634+fi
635+
This page took 0.135394 seconds and 4 git commands to generate.