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