]>
Commit | Line | Data |
---|---|---|
cacce04b ER |
1 | # License: MIT |
2 | # http://opensource.org/licenses/MIT | |
3 | ||
c0240cb1 | 4 | Add support for use of the system timezone database, rather |
5 | than embedding a copy. Discussed upstream but was not desired. | |
6 | ||
7 | History: | |
539ed3cb AG |
8 | r17: adapt for timelib 2018.01 (in 7.3.2RC1) |
9 | r16: adapt for timelib 2017.06 (in 7.2.3RC1) | |
cacce04b ER |
10 | r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1) |
11 | r14: improve check for valid tz file | |
909e9211 | 12 | r13: adapt for upstream changes to use PHP allocator |
a659fa18 ER |
13 | r12: adapt for upstream changes for new zic |
14 | r11: use canonical names to avoid more case sensitivity issues | |
15 | round lat/long from zone.tab towards zero per builtin db | |
16 | r10: make timezone case insensitive | |
361fb152 ER |
17 | r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold) |
18 | r8: fix compile error without --with-system-tzdata configured | |
c0240cb1 | 19 | r7: improve check for valid timezone id to exclude directories |
a659fa18 | 20 | r6: fix fd leak in r5, fix country code/BC flag use in |
c0240cb1 | 21 | timezone_identifiers_list() using system db, |
22 | fix use of PECL timezonedb to override system db, | |
23 | r5: reverts addition of "System/Localtime" fake tzname. | |
24 | updated for 5.3.0, parses zone.tab to pick up mapping between | |
25 | timezone name, country code and long/lat coords | |
26 | r4: added "System/Localtime" tzname which uses /etc/localtime | |
27 | r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert) | |
28 | r2: add filesystem trawl to set up name alias index | |
29 | r1: initial revision | |
30 | ||
539ed3cb AG |
31 | diff -up php-7.3.2RC1/ext/date/lib/parse_tz.c.systzdata php-7.3.2RC1/ext/date/lib/parse_tz.c |
32 | --- php-7.3.2RC1/ext/date/lib/parse_tz.c.systzdata 2019-01-22 13:20:08.000000000 +0100 | |
33 | +++ php-7.3.2RC1/ext/date/lib/parse_tz.c 2019-01-22 14:30:46.655691222 +0100 | |
cacce04b | 34 | @@ -25,8 +25,21 @@ |
c0240cb1 | 35 | #include "timelib.h" |
cacce04b | 36 | #include "timelib_private.h" |
c0240cb1 | 37 | |
38 | +#ifdef HAVE_SYSTEM_TZDATA | |
39 | +#include <sys/mman.h> | |
40 | +#include <sys/stat.h> | |
41 | +#include <limits.h> | |
42 | +#include <fcntl.h> | |
43 | +#include <unistd.h> | |
44 | + | |
45 | +#include "php_scandir.h" | |
c0240cb1 | 46 | + |
cacce04b | 47 | +#else |
909e9211 | 48 | #define TIMELIB_SUPPORTS_V2DATA |
c0240cb1 | 49 | #include "timezonedb.h" |
50 | +#endif | |
51 | + | |
52 | +#include <ctype.h> | |
53 | ||
54 | #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__)) | |
55 | # if defined(__LITTLE_ENDIAN__) | |
539ed3cb | 56 | @@ -87,6 +100,11 @@ static int read_php_preamble(const unsig |
c0240cb1 | 57 | { |
a659fa18 ER |
58 | uint32_t version; |
59 | ||
60 | + if (memcmp(*tzf, "TZif", 4) == 0) { | |
61 | + *tzf += 20; | |
62 | + return 0; | |
63 | + } | |
909e9211 | 64 | + |
a659fa18 ER |
65 | /* read ID */ |
66 | version = (*tzf)[3] - '0'; | |
67 | *tzf += 4; | |
539ed3cb | 68 | @@ -411,7 +429,429 @@ void timelib_dump_tzinfo(timelib_tzinfo |
c0240cb1 | 69 | } |
70 | } | |
71 | ||
72 | -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb) | |
73 | +#ifdef HAVE_SYSTEM_TZDATA | |
74 | + | |
75 | +#ifdef HAVE_SYSTEM_TZDATA_PREFIX | |
76 | +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX | |
77 | +#else | |
78 | +#define ZONEINFO_PREFIX "/usr/share/zoneinfo" | |
79 | +#endif | |
80 | + | |
81 | +/* System timezone database pointer. */ | |
7588b36c | 82 | +static const timelib_tzdb *timezonedb_system; |
c0240cb1 | 83 | + |
84 | +/* Hash table entry for the cache of the zone.tab mapping table. */ | |
85 | +struct location_info { | |
86 | + char code[2]; | |
87 | + double latitude, longitude; | |
88 | + char name[64]; | |
89 | + char *comment; | |
90 | + struct location_info *next; | |
91 | +}; | |
92 | + | |
93 | +/* Cache of zone.tab. */ | |
94 | +static struct location_info **system_location_table; | |
95 | + | |
96 | +/* Size of the zone.tab hash table; a random-ish prime big enough to | |
97 | + * prevent too many collisions. */ | |
98 | +#define LOCINFO_HASH_SIZE (1021) | |
99 | + | |
7588b36c | 100 | +/* Compute a case insensitive hash of str */ |
c0240cb1 | 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; | |
909e9211 | 106 | + |
7588b36c | 107 | + while ((c = tolower(*p++)) != '\0') { |
c0240cb1 | 108 | + hash = (hash << 5) ^ hash ^ c; |
109 | + } | |
909e9211 | 110 | + |
c0240cb1 | 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. */ | |
a659fa18 | 166 | + *result = trunc(v * sign * 100000.0) / 100000.0; |
c0240cb1 | 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) { | |
539ed3cb | 271 | + if (timelib_strcasecmp(l->name, name) == 0) |
c0240cb1 | 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 | |
cacce04b | 287 | + && strstr(ent->d_name, ".list") == NULL |
c0240cb1 | 288 | + && strstr(ent->d_name, ".tab") == NULL; |
289 | +} | |
290 | + | |
291 | +static int sysdbcmp(const void *first, const void *second) | |
292 | +{ | |
293 | + const timelib_tzdb_index_entry *alpha = first, *beta = second; | |
294 | + | |
539ed3cb | 295 | + return timelib_strcasecmp(alpha->id, beta->id); |
c0240cb1 | 296 | +} |
297 | + | |
298 | + | |
299 | +/* Create the zone identifier index by trawling the filesystem. */ | |
300 | +static void create_zone_index(timelib_tzdb *db) | |
301 | +{ | |
302 | + size_t dirstack_size, dirstack_top; | |
303 | + size_t index_size, index_next; | |
304 | + timelib_tzdb_index_entry *db_index; | |
305 | + char **dirstack; | |
306 | + | |
307 | + /* LIFO stack to hold directory entries to scan; each slot is a | |
308 | + * directory name relative to the zoneinfo prefix. */ | |
309 | + dirstack_size = 32; | |
310 | + dirstack = malloc(dirstack_size * sizeof *dirstack); | |
311 | + dirstack_top = 1; | |
312 | + dirstack[0] = strdup(""); | |
313 | + | |
314 | + /* Index array. */ | |
315 | + index_size = 64; | |
316 | + db_index = malloc(index_size * sizeof *db_index); | |
317 | + index_next = 0; | |
318 | + | |
319 | + do { | |
320 | + struct dirent **ents; | |
321 | + char name[PATH_MAX], *top; | |
322 | + int count; | |
323 | + | |
324 | + /* Pop the top stack entry, and iterate through its contents. */ | |
325 | + top = dirstack[--dirstack_top]; | |
326 | + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top); | |
327 | + | |
328 | + count = php_scandir(name, &ents, index_filter, php_alphasort); | |
329 | + | |
330 | + while (count > 0) { | |
331 | + struct stat st; | |
332 | + const char *leaf = ents[count - 1]->d_name; | |
333 | + | |
334 | + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", | |
335 | + top, leaf); | |
336 | + | |
337 | + if (strlen(name) && stat(name, &st) == 0) { | |
338 | + /* Name, relative to the zoneinfo prefix. */ | |
339 | + const char *root = top; | |
340 | + | |
341 | + if (root[0] == '/') root++; | |
342 | + | |
343 | + snprintf(name, sizeof name, "%s%s%s", root, | |
344 | + *root ? "/": "", leaf); | |
345 | + | |
346 | + if (S_ISDIR(st.st_mode)) { | |
347 | + if (dirstack_top == dirstack_size) { | |
348 | + dirstack_size *= 2; | |
349 | + dirstack = realloc(dirstack, | |
350 | + dirstack_size * sizeof *dirstack); | |
351 | + } | |
352 | + dirstack[dirstack_top++] = strdup(name); | |
353 | + } | |
354 | + else { | |
355 | + if (index_next == index_size) { | |
356 | + index_size *= 2; | |
357 | + db_index = realloc(db_index, | |
358 | + index_size * sizeof *db_index); | |
359 | + } | |
360 | + | |
361 | + db_index[index_next++].id = strdup(name); | |
362 | + } | |
363 | + } | |
364 | + | |
365 | + free(ents[--count]); | |
366 | + } | |
367 | + | |
368 | + if (count != -1) free(ents); | |
369 | + free(top); | |
370 | + } while (dirstack_top); | |
371 | + | |
372 | + qsort(db_index, index_next, sizeof *db_index, sysdbcmp); | |
373 | + | |
374 | + db->index = db_index; | |
375 | + db->index_size = index_next; | |
376 | + | |
377 | + free(dirstack); | |
378 | +} | |
379 | + | |
380 | +#define FAKE_HEADER "1234\0??\1??" | |
381 | +#define FAKE_UTC_POS (7 - 4) | |
382 | + | |
383 | +/* Create a fake data segment for database 'sysdb'. */ | |
384 | +static void fake_data_segment(timelib_tzdb *sysdb, | |
385 | + struct location_info **info) | |
386 | +{ | |
387 | + size_t n; | |
388 | + char *data, *p; | |
389 | + | |
390 | + data = malloc(3 * sysdb->index_size + 7); | |
391 | + | |
392 | + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1); | |
393 | + | |
394 | + for (n = 0; n < sysdb->index_size; n++) { | |
395 | + const struct location_info *li; | |
396 | + timelib_tzdb_index_entry *ent; | |
397 | + | |
398 | + ent = (timelib_tzdb_index_entry *)&sysdb->index[n]; | |
399 | + | |
400 | + /* Lookup the timezone name in the hash table. */ | |
401 | + if (strcmp(ent->id, "UTC") == 0) { | |
402 | + ent->pos = FAKE_UTC_POS; | |
403 | + continue; | |
404 | + } | |
405 | + | |
406 | + li = find_zone_info(info, ent->id); | |
407 | + if (li) { | |
408 | + /* If found, append the BC byte and the | |
409 | + * country code; set the position for this | |
410 | + * section of timezone data. */ | |
411 | + ent->pos = (p - data) - 4; | |
412 | + *p++ = '\1'; | |
413 | + *p++ = li->code[0]; | |
414 | + *p++ = li->code[1]; | |
415 | + } | |
416 | + else { | |
417 | + /* If not found, the timezone data can | |
418 | + * point at the header. */ | |
419 | + ent->pos = 0; | |
420 | + } | |
421 | + } | |
422 | + | |
423 | + sysdb->data = (unsigned char *)data; | |
424 | +} | |
425 | + | |
426 | +/* Returns true if the passed-in stat structure describes a | |
427 | + * probably-valid timezone file. */ | |
cacce04b | 428 | +static int is_valid_tzfile(const struct stat *st, int fd) |
c0240cb1 | 429 | +{ |
cacce04b ER |
430 | + if (fd) { |
431 | + char buf[20]; | |
432 | + if (read(fd, buf, 20)!=20) { | |
433 | + return 0; | |
434 | + } | |
435 | + lseek(fd, SEEK_SET, 0); | |
436 | + if (memcmp(buf, "TZif", 4)) { | |
437 | + return 0; | |
438 | + } | |
439 | + } | |
c0240cb1 | 440 | + return S_ISREG(st->st_mode) && st->st_size > 20; |
441 | +} | |
442 | + | |
a659fa18 ER |
443 | +/* To allow timezone names to be used case-insensitively, find the |
444 | + * canonical name for this timezone, if possible. */ | |
445 | +static const char *canonical_tzname(const char *timezone) | |
446 | +{ | |
447 | + if (timezonedb_system) { | |
448 | + timelib_tzdb_index_entry *ent, lookup; | |
909e9211 | 449 | + |
a659fa18 | 450 | + lookup.id = (char *)timezone; |
909e9211 | 451 | + |
a659fa18 ER |
452 | + ent = bsearch(&lookup, timezonedb_system->index, |
453 | + timezonedb_system->index_size, sizeof lookup, | |
454 | + sysdbcmp); | |
455 | + if (ent) { | |
456 | + return ent->id; | |
457 | + } | |
458 | + } | |
459 | + | |
460 | + return timezone; | |
461 | +} | |
462 | + | |
c0240cb1 | 463 | +/* Return the mmap()ed tzfile if found, else NULL. On success, the |
464 | + * length of the mapped data is placed in *length. */ | |
465 | +static char *map_tzfile(const char *timezone, size_t *length) | |
466 | +{ | |
467 | + char fname[PATH_MAX]; | |
468 | + struct stat st; | |
469 | + char *p; | |
470 | + int fd; | |
471 | + | |
472 | + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { | |
473 | + return NULL; | |
474 | + } | |
475 | + | |
a659fa18 | 476 | + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone)); |
cacce04b | 477 | + |
c0240cb1 | 478 | + fd = open(fname, O_RDONLY); |
479 | + if (fd == -1) { | |
480 | + return NULL; | |
cacce04b | 481 | + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) { |
c0240cb1 | 482 | + close(fd); |
483 | + return NULL; | |
484 | + } | |
485 | + | |
486 | + *length = st.st_size; | |
487 | + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
488 | + close(fd); | |
489 | + | |
490 | + return p != MAP_FAILED ? p : NULL; | |
491 | +} | |
492 | + | |
493 | +#endif | |
494 | + | |
495 | +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb) | |
496 | { | |
497 | int left = 0, right = tzdb->index_size - 1; | |
539ed3cb AG |
498 | |
499 | @@ -437,9 +877,48 @@ static int seek_to_tz_position(const uns | |
c0240cb1 | 500 | return 0; |
501 | } | |
502 | ||
a659fa18 | 503 | +static int seek_to_tz_position(const unsigned char **tzf, char *timezone, |
c0240cb1 | 504 | + char **map, size_t *maplen, |
505 | + const timelib_tzdb *tzdb) | |
506 | +{ | |
361fb152 | 507 | +#ifdef HAVE_SYSTEM_TZDATA |
c0240cb1 | 508 | + if (tzdb == timezonedb_system) { |
509 | + char *orig; | |
510 | + | |
511 | + orig = map_tzfile(timezone, maplen); | |
512 | + if (orig == NULL) { | |
513 | + return 0; | |
514 | + } | |
a659fa18 | 515 | + |
909e9211 | 516 | + (*tzf) = (unsigned char *)orig; |
c0240cb1 | 517 | + *map = orig; |
909e9211 | 518 | + return 1; |
c0240cb1 | 519 | + } |
a659fa18 | 520 | + else |
361fb152 | 521 | +#endif |
a659fa18 | 522 | + { |
c0240cb1 | 523 | + return inmem_seek_to_tz_position(tzf, timezone, tzdb); |
524 | + } | |
525 | +} | |
526 | + | |
527 | const timelib_tzdb *timelib_builtin_db(void) | |
528 | { | |
529 | +#ifdef HAVE_SYSTEM_TZDATA | |
530 | + if (timezonedb_system == NULL) { | |
531 | + timelib_tzdb *tmp = malloc(sizeof *tmp); | |
532 | + | |
533 | + tmp->version = "0.system"; | |
534 | + tmp->data = NULL; | |
535 | + create_zone_index(tmp); | |
536 | + system_location_table = create_location_table(); | |
a659fa18 | 537 | + fake_data_segment(tmp, system_location_table); |
c0240cb1 | 538 | + timezonedb_system = tmp; |
539 | + } | |
540 | + | |
c0240cb1 | 541 | + return timezonedb_system; |
542 | +#else | |
543 | return &timezonedb_builtin; | |
544 | +#endif | |
545 | } | |
546 | ||
539ed3cb AG |
547 | const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count) |
548 | @@ -451,7 +930,30 @@ const timelib_tzdb_index_entry *timelib_ | |
c0240cb1 | 549 | int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb) |
550 | { | |
551 | const unsigned char *tzf; | |
552 | - return (seek_to_tz_position(&tzf, timezone, tzdb)); | |
553 | + | |
554 | +#ifdef HAVE_SYSTEM_TZDATA | |
a659fa18 ER |
555 | + if (tzdb == timezonedb_system) { |
556 | + char fname[PATH_MAX]; | |
557 | + struct stat st; | |
558 | + | |
559 | + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) { | |
560 | + return 0; | |
561 | + } | |
c0240cb1 | 562 | + |
a659fa18 ER |
563 | + if (system_location_table) { |
564 | + if (find_zone_info(system_location_table, timezone) != NULL) { | |
565 | + /* found in cache */ | |
566 | + return 1; | |
567 | + } | |
568 | + } | |
569 | + | |
570 | + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone)); | |
571 | + | |
cacce04b | 572 | + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0); |
a659fa18 ER |
573 | + } |
574 | +#endif | |
909e9211 | 575 | + |
c0240cb1 | 576 | + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb)); |
577 | } | |
578 | ||
cacce04b | 579 | static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz) |
539ed3cb | 580 | @@ -493,12 +995,14 @@ static timelib_tzinfo* timelib_tzinfo_ct |
cacce04b | 581 | timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code) |
c0240cb1 | 582 | { |
583 | const unsigned char *tzf; | |
584 | + char *memmap = NULL; | |
585 | + size_t maplen; | |
586 | timelib_tzinfo *tmp; | |
a659fa18 | 587 | int version; |
cacce04b ER |
588 | int transitions_result, types_result; |
589 | unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */ | |
c0240cb1 | 590 | |
591 | - if (seek_to_tz_position(&tzf, timezone, tzdb)) { | |
592 | + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) { | |
593 | tmp = timelib_tzinfo_ctor(timezone); | |
594 | ||
cacce04b | 595 | version = read_preamble(&tzf, tmp, &type); |
539ed3cb | 596 | @@ -537,11 +1041,36 @@ timelib_tzinfo *timelib_parse_tzfile(cha |
cacce04b | 597 | } |
539ed3cb AG |
598 | skip_posix_string(&tzf, tmp); |
599 | ||
c0240cb1 | 600 | +#ifdef HAVE_SYSTEM_TZDATA |
601 | + if (memmap) { | |
602 | + const struct location_info *li; | |
603 | + | |
604 | + /* TZif-style - grok the location info from the system database, | |
605 | + * if possible. */ | |
606 | + | |
607 | + if ((li = find_zone_info(system_location_table, timezone)) != NULL) { | |
909e9211 ER |
608 | + tmp->location.comments = timelib_strdup(li->comment); |
609 | + strncpy(tmp->location.country_code, li->code, 2); | |
c0240cb1 | 610 | + tmp->location.longitude = li->longitude; |
611 | + tmp->location.latitude = li->latitude; | |
612 | + tmp->bc = 1; | |
613 | + } | |
614 | + else { | |
cacce04b | 615 | + set_default_location_and_comments(&tzf, tmp); |
c0240cb1 | 616 | + } |
617 | + | |
618 | + /* Now done with the mmap segment - discard it. */ | |
619 | + munmap(memmap, maplen); | |
cacce04b ER |
620 | + } else { |
621 | +#endif | |
539ed3cb AG |
622 | if (type == TIMELIB_TZINFO_PHP) { |
623 | read_location(&tzf, tmp); | |
cacce04b ER |
624 | } else { |
625 | set_default_location_and_comments(&tzf, tmp); | |
626 | } | |
627 | +#ifdef HAVE_SYSTEM_TZDATA | |
628 | + } | |
c0240cb1 | 629 | +#endif |
c0240cb1 | 630 | } else { |
cacce04b | 631 | *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE; |
c0240cb1 | 632 | tmp = NULL; |
539ed3cb AG |
633 | diff -up php-7.3.2RC1/ext/date/lib/timelib.m4.systzdata php-7.3.2RC1/ext/date/lib/timelib.m4 |
634 | --- php-7.3.2RC1/ext/date/lib/timelib.m4.systzdata 2019-01-22 13:20:08.000000000 +0100 | |
635 | +++ php-7.3.2RC1/ext/date/lib/timelib.m4 2019-01-22 13:47:07.807374084 +0100 | |
636 | @@ -78,3 +78,16 @@ io.h | |
c0240cb1 | 637 | |
638 | dnl Check for strtoll, atoll | |
3d8669ae | 639 | AC_CHECK_FUNCS(strtoll atoll strftime gettimeofday) |
c0240cb1 | 640 | + |
641 | +PHP_ARG_WITH(system-tzdata, for use of system timezone data, | |
642 | +[ --with-system-tzdata[=DIR] to specify use of system timezone data], | |
643 | +no, no) | |
644 | + | |
645 | +if test "$PHP_SYSTEM_TZDATA" != "no"; then | |
646 | + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used]) | |
647 | + | |
648 | + if test "$PHP_SYSTEM_TZDATA" != "yes"; then | |
649 | + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA", | |
650 | + [Define for location of system timezone data]) | |
651 | + fi | |
652 | +fi |