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