1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2015 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Derick Rethans <derick@derickrethans.nl> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "timelib.h"
22
23 #include <stdio.h>
24
25 #ifdef HAVE_LOCALE_H
26 #include <locale.h>
27 #endif
28
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #include <strings.h>
33 #endif
34 #include "timezonedb.h"
35
36 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
37 # if defined(__LITTLE_ENDIAN__)
38 # undef WORDS_BIGENDIAN
39 # else
40 # if defined(__BIG_ENDIAN__)
41 # define WORDS_BIGENDIAN
42 # endif
43 # endif
44 #endif
45
46 #ifdef WORDS_BIGENDIAN
47 #define timelib_conv_int(l) (l)
48 #else
49 #define timelib_conv_int(l) ((l & 0x000000ff) << 24) + ((l & 0x0000ff00) << 8) + ((l & 0x00ff0000) >> 8) + ((l & 0xff000000) >> 24)
50 #endif
51
read_preamble(const unsigned char ** tzf,timelib_tzinfo * tz)52 static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
53 {
54 uint32_t version;
55
56 /* read ID */
57 version = (*tzf)[3] - '0';
58 *tzf += 4;
59
60 /* read BC flag */
61 tz->bc = (**tzf == '\1');
62 *tzf += 1;
63
64 /* read country code */
65 memcpy(tz->location.country_code, *tzf, 2);
66 tz->location.country_code[2] = '\0';
67 *tzf += 2;
68
69 /* skip rest of preamble */
70 *tzf += 13;
71
72 return version;
73 }
74
read_header(const unsigned char ** tzf,timelib_tzinfo * tz)75 static void read_header(const unsigned char **tzf, timelib_tzinfo *tz)
76 {
77 uint32_t buffer[6];
78
79 memcpy(&buffer, *tzf, sizeof(buffer));
80 tz->bit32.ttisgmtcnt = timelib_conv_int(buffer[0]);
81 tz->bit32.ttisstdcnt = timelib_conv_int(buffer[1]);
82 tz->bit32.leapcnt = timelib_conv_int(buffer[2]);
83 tz->bit32.timecnt = timelib_conv_int(buffer[3]);
84 tz->bit32.typecnt = timelib_conv_int(buffer[4]);
85 tz->bit32.charcnt = timelib_conv_int(buffer[5]);
86 *tzf += sizeof(buffer);
87 }
88
skip_64bit_transistions(const unsigned char ** tzf,timelib_tzinfo * tz)89 static void skip_64bit_transistions(const unsigned char **tzf, timelib_tzinfo *tz)
90 {
91 if (tz->bit64.timecnt) {
92 *tzf += (sizeof(int64_t) * tz->bit64.timecnt);
93 *tzf += (sizeof(unsigned char) * tz->bit64.timecnt);
94 }
95 }
96
read_transistions(const unsigned char ** tzf,timelib_tzinfo * tz)97 static void read_transistions(const unsigned char **tzf, timelib_tzinfo *tz)
98 {
99 int32_t *buffer = NULL;
100 uint32_t i;
101 unsigned char *cbuffer = NULL;
102
103 if (tz->bit32.timecnt) {
104 buffer = (int32_t*) malloc(tz->bit32.timecnt * sizeof(int32_t));
105 if (!buffer) {
106 return;
107 }
108 memcpy(buffer, *tzf, sizeof(int32_t) * tz->bit32.timecnt);
109 *tzf += (sizeof(int32_t) * tz->bit32.timecnt);
110 for (i = 0; i < tz->bit32.timecnt; i++) {
111 buffer[i] = timelib_conv_int(buffer[i]);
112 }
113
114 cbuffer = (unsigned char*) malloc(tz->bit32.timecnt * sizeof(unsigned char));
115 if (!cbuffer) {
116 free(buffer);
117 return;
118 }
119 memcpy(cbuffer, *tzf, sizeof(unsigned char) * tz->bit32.timecnt);
120 *tzf += sizeof(unsigned char) * tz->bit32.timecnt;
121 }
122
123 tz->trans = buffer;
124 tz->trans_idx = cbuffer;
125 }
126
skip_64bit_types(const unsigned char ** tzf,timelib_tzinfo * tz)127 static void skip_64bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
128 {
129 *tzf += sizeof(unsigned char) * 6 * tz->bit64.typecnt;
130 *tzf += sizeof(char) * tz->bit64.charcnt;
131 if (tz->bit64.leapcnt) {
132 *tzf += sizeof(int64_t) * tz->bit64.leapcnt * 2;
133 }
134 if (tz->bit64.ttisstdcnt) {
135 *tzf += sizeof(unsigned char) * tz->bit64.ttisstdcnt;
136 }
137 if (tz->bit64.ttisgmtcnt) {
138 *tzf += sizeof(unsigned char) * tz->bit64.ttisgmtcnt;
139 }
140 }
141
read_types(const unsigned char ** tzf,timelib_tzinfo * tz)142 static void read_types(const unsigned char **tzf, timelib_tzinfo *tz)
143 {
144 unsigned char *buffer;
145 int32_t *leap_buffer;
146 unsigned int i, j;
147
148 buffer = (unsigned char*) malloc(tz->bit32.typecnt * sizeof(unsigned char) * 6);
149 if (!buffer) {
150 return;
151 }
152 memcpy(buffer, *tzf, sizeof(unsigned char) * 6 * tz->bit32.typecnt);
153 *tzf += sizeof(unsigned char) * 6 * tz->bit32.typecnt;
154
155 tz->type = (ttinfo*) malloc(tz->bit32.typecnt * sizeof(struct ttinfo));
156 if (!tz->type) {
157 free(buffer);
158 return;
159 }
160
161 for (i = 0; i < tz->bit32.typecnt; i++) {
162 j = i * 6;
163 tz->type[i].offset = (buffer[j] * 16777216) + (buffer[j + 1] * 65536) + (buffer[j + 2] * 256) + buffer[j + 3];
164 tz->type[i].isdst = buffer[j + 4];
165 tz->type[i].abbr_idx = buffer[j + 5];
166 }
167 free(buffer);
168
169 tz->timezone_abbr = (char*) malloc(tz->bit32.charcnt);
170 if (!tz->timezone_abbr) {
171 return;
172 }
173 memcpy(tz->timezone_abbr, *tzf, sizeof(char) * tz->bit32.charcnt);
174 *tzf += sizeof(char) * tz->bit32.charcnt;
175
176 if (tz->bit32.leapcnt) {
177 leap_buffer = (int32_t *) malloc(tz->bit32.leapcnt * 2 * sizeof(int32_t));
178 if (!leap_buffer) {
179 return;
180 }
181 memcpy(leap_buffer, *tzf, sizeof(int32_t) * tz->bit32.leapcnt * 2);
182 *tzf += sizeof(int32_t) * tz->bit32.leapcnt * 2;
183
184 tz->leap_times = (tlinfo*) malloc(tz->bit32.leapcnt * sizeof(tlinfo));
185 if (!tz->leap_times) {
186 free(leap_buffer);
187 return;
188 }
189 for (i = 0; i < tz->bit32.leapcnt; i++) {
190 tz->leap_times[i].trans = timelib_conv_int(leap_buffer[i * 2]);
191 tz->leap_times[i].offset = timelib_conv_int(leap_buffer[i * 2 + 1]);
192 }
193 free(leap_buffer);
194 }
195
196 if (tz->bit32.ttisstdcnt) {
197 buffer = (unsigned char*) malloc(tz->bit32.ttisstdcnt * sizeof(unsigned char));
198 if (!buffer) {
199 return;
200 }
201 memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit32.ttisstdcnt);
202 *tzf += sizeof(unsigned char) * tz->bit32.ttisstdcnt;
203
204 for (i = 0; i < tz->bit32.ttisstdcnt; i++) {
205 tz->type[i].isstdcnt = buffer[i];
206 }
207 free(buffer);
208 }
209
210 if (tz->bit32.ttisgmtcnt) {
211 buffer = (unsigned char*) malloc(tz->bit32.ttisgmtcnt * sizeof(unsigned char));
212 if (!buffer) {
213 return;
214 }
215 memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit32.ttisgmtcnt);
216 *tzf += sizeof(unsigned char) * tz->bit32.ttisgmtcnt;
217
218 for (i = 0; i < tz->bit32.ttisgmtcnt; i++) {
219 tz->type[i].isgmtcnt = buffer[i];
220 }
221 free(buffer);
222 }
223 }
224
skip_posix_string(const unsigned char ** tzf,timelib_tzinfo * tz)225 static void skip_posix_string(const unsigned char **tzf, timelib_tzinfo *tz)
226 {
227 int n_count = 0;
228
229 do {
230 if (*tzf[0] == '\n') {
231 n_count++;
232 }
233 (*tzf)++;
234 } while (n_count < 2);
235 }
236
read_location(const unsigned char ** tzf,timelib_tzinfo * tz)237 static void read_location(const unsigned char **tzf, timelib_tzinfo *tz)
238 {
239 uint32_t buffer[3];
240 uint32_t comments_len;
241
242 memcpy(&buffer, *tzf, sizeof(buffer));
243 tz->location.latitude = timelib_conv_int(buffer[0]);
244 tz->location.latitude = (tz->location.latitude / 100000) - 90;
245 tz->location.longitude = timelib_conv_int(buffer[1]);
246 tz->location.longitude = (tz->location.longitude / 100000) - 180;
247 comments_len = timelib_conv_int(buffer[2]);
248 *tzf += sizeof(buffer);
249
250 tz->location.comments = malloc(comments_len + 1);
251 memcpy(tz->location.comments, *tzf, comments_len);
252 tz->location.comments[comments_len] = '\0';
253 *tzf += comments_len;
254 }
255
timelib_dump_tzinfo(timelib_tzinfo * tz)256 void timelib_dump_tzinfo(timelib_tzinfo *tz)
257 {
258 uint32_t i;
259
260 printf("Country Code: %s\n", tz->location.country_code);
261 printf("Geo Location: %f,%f\n", tz->location.latitude, tz->location.longitude);
262 printf("Comments:\n%s\n", tz->location.comments);
263 printf("BC: %s\n", tz->bc ? "" : "yes");
264 printf("UTC/Local count: %lu\n", (unsigned long) tz->bit32.ttisgmtcnt);
265 printf("Std/Wall count: %lu\n", (unsigned long) tz->bit32.ttisstdcnt);
266 printf("Leap.sec. count: %lu\n", (unsigned long) tz->bit32.leapcnt);
267 printf("Trans. count: %lu\n", (unsigned long) tz->bit32.timecnt);
268 printf("Local types count: %lu\n", (unsigned long) tz->bit32.typecnt);
269 printf("Zone Abbr. count: %lu\n", (unsigned long) tz->bit32.charcnt);
270
271 printf ("%8s (%12s) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
272 "", "", 0,
273 (long int) tz->type[0].offset,
274 tz->type[0].isdst,
275 tz->type[0].abbr_idx,
276 &tz->timezone_abbr[tz->type[0].abbr_idx],
277 tz->type[0].isstdcnt,
278 tz->type[0].isgmtcnt
279 );
280 for (i = 0; i < tz->bit32.timecnt; i++) {
281 printf ("%08X (%12d) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
282 tz->trans[i], tz->trans[i], tz->trans_idx[i],
283 (long int) tz->type[tz->trans_idx[i]].offset,
284 tz->type[tz->trans_idx[i]].isdst,
285 tz->type[tz->trans_idx[i]].abbr_idx,
286 &tz->timezone_abbr[tz->type[tz->trans_idx[i]].abbr_idx],
287 tz->type[tz->trans_idx[i]].isstdcnt,
288 tz->type[tz->trans_idx[i]].isgmtcnt
289 );
290 }
291 for (i = 0; i < tz->bit32.leapcnt; i++) {
292 printf ("%08X (%12ld) = %d\n",
293 tz->leap_times[i].trans,
294 (long) tz->leap_times[i].trans,
295 tz->leap_times[i].offset);
296 }
297 }
298
seek_to_tz_position(const unsigned char ** tzf,char * timezone,const timelib_tzdb * tzdb)299 static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
300 {
301 int left = 0, right = tzdb->index_size - 1;
302 #ifdef HAVE_SETLOCALE
303 char *cur_locale = NULL, *tmp;
304
305 tmp = setlocale(LC_CTYPE, NULL);
306 if (tmp) {
307 cur_locale = strdup(tmp);
308 }
309 setlocale(LC_CTYPE, "C");
310 #endif
311
312 do {
313 int mid = ((unsigned)left + right) >> 1;
314 int cmp = strcasecmp(timezone, tzdb->index[mid].id);
315
316 if (cmp < 0) {
317 right = mid - 1;
318 } else if (cmp > 0) {
319 left = mid + 1;
320 } else { /* (cmp == 0) */
321 (*tzf) = &(tzdb->data[tzdb->index[mid].pos]);
322 #ifdef HAVE_SETLOCALE
323 setlocale(LC_CTYPE, cur_locale);
324 if (cur_locale) free(cur_locale);
325 #endif
326 return 1;
327 }
328
329 } while (left <= right);
330
331 #ifdef HAVE_SETLOCALE
332 setlocale(LC_CTYPE, cur_locale);
333 if (cur_locale) free(cur_locale);
334 #endif
335 return 0;
336 }
337
timelib_builtin_db(void)338 const timelib_tzdb *timelib_builtin_db(void)
339 {
340 return &timezonedb_builtin;
341 }
342
timelib_timezone_builtin_identifiers_list(int * count)343 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
344 {
345 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
346 return timezonedb_idx_builtin;
347 }
348
timelib_timezone_id_is_valid(char * timezone,const timelib_tzdb * tzdb)349 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
350 {
351 const unsigned char *tzf;
352 return (seek_to_tz_position(&tzf, timezone, tzdb));
353 }
354
skip_64bit_preamble(const unsigned char ** tzf,timelib_tzinfo * tz)355 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
356 {
357 *tzf += 20;
358 }
359
read_64bit_header(const unsigned char ** tzf,timelib_tzinfo * tz)360 static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
361 {
362 uint32_t buffer[6];
363
364 memcpy(&buffer, *tzf, sizeof(buffer));
365 tz->bit64.ttisgmtcnt = timelib_conv_int(buffer[0]);
366 tz->bit64.ttisstdcnt = timelib_conv_int(buffer[1]);
367 tz->bit64.leapcnt = timelib_conv_int(buffer[2]);
368 tz->bit64.timecnt = timelib_conv_int(buffer[3]);
369 tz->bit64.typecnt = timelib_conv_int(buffer[4]);
370 tz->bit64.charcnt = timelib_conv_int(buffer[5]);
371 *tzf += sizeof(buffer);
372 }
373
timelib_parse_tzfile(char * timezone,const timelib_tzdb * tzdb)374 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
375 {
376 const unsigned char *tzf;
377 timelib_tzinfo *tmp;
378 int version;
379
380 if (seek_to_tz_position(&tzf, timezone, tzdb)) {
381 tmp = timelib_tzinfo_ctor(timezone);
382
383 version = read_preamble(&tzf, tmp);
384 read_header(&tzf, tmp);
385 read_transistions(&tzf, tmp);
386 read_types(&tzf, tmp);
387 if (version == 2) {
388 skip_64bit_preamble(&tzf, tmp);
389 read_64bit_header(&tzf, tmp);
390 skip_64bit_transistions(&tzf, tmp);
391 skip_64bit_types(&tzf, tmp);
392 skip_posix_string(&tzf, tmp);
393 }
394 read_location(&tzf, tmp);
395 } else {
396 tmp = NULL;
397 }
398
399 return tmp;
400 }
401
fetch_timezone_offset(timelib_tzinfo * tz,timelib_sll ts,timelib_sll * transition_time)402 static ttinfo* fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
403 {
404 uint32_t i;
405
406 /* If there is no transition time, we pick the first one, if that doesn't
407 * exist we return NULL */
408 if (!tz->bit32.timecnt || !tz->trans) {
409 *transition_time = 0;
410 if (tz->bit32.typecnt == 1) {
411 return &(tz->type[0]);
412 }
413 return NULL;
414 }
415
416 /* If the TS is lower than the first transition time, then we scan over
417 * all the transition times to find the first non-DST one, or the first
418 * one in case there are only DST entries. Not sure which smartass came up
419 * with this idea in the first though :) */
420 if (ts < tz->trans[0]) {
421 uint32_t j;
422
423 *transition_time = 0;
424 j = 0;
425 while (j < tz->bit32.timecnt && tz->type[j].isdst) {
426 ++j;
427 }
428 if (j == tz->bit32.timecnt) {
429 j = 0;
430 }
431 return &(tz->type[j]);
432 }
433
434 /* In all other cases we loop through the available transtion times to find
435 * the correct entry */
436 for (i = 0; i < tz->bit32.timecnt; i++) {
437 if (ts < tz->trans[i]) {
438 *transition_time = tz->trans[i - 1];
439 return &(tz->type[tz->trans_idx[i - 1]]);
440 }
441 }
442 *transition_time = tz->trans[tz->bit32.timecnt - 1];
443 return &(tz->type[tz->trans_idx[tz->bit32.timecnt - 1]]);
444 }
445
fetch_leaptime_offset(timelib_tzinfo * tz,timelib_sll ts)446 static tlinfo* fetch_leaptime_offset(timelib_tzinfo *tz, timelib_sll ts)
447 {
448 int i;
449
450 if (!tz->bit32.leapcnt || !tz->leap_times) {
451 return NULL;
452 }
453
454 for (i = tz->bit32.leapcnt - 1; i > 0; i--) {
455 if (ts > tz->leap_times[i].trans) {
456 return &(tz->leap_times[i]);
457 }
458 }
459 return NULL;
460 }
461
timelib_timestamp_is_in_dst(timelib_sll ts,timelib_tzinfo * tz)462 int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz)
463 {
464 ttinfo *to;
465 timelib_sll dummy;
466
467 if ((to = fetch_timezone_offset(tz, ts, &dummy))) {
468 return to->isdst;
469 }
470 return -1;
471 }
472
timelib_get_time_zone_info(timelib_sll ts,timelib_tzinfo * tz)473 timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
474 {
475 ttinfo *to;
476 tlinfo *tl;
477 int32_t offset = 0, leap_secs = 0;
478 char *abbr;
479 timelib_time_offset *tmp = timelib_time_offset_ctor();
480 timelib_sll transistion_time;
481
482 if ((to = fetch_timezone_offset(tz, ts, &transistion_time))) {
483 offset = to->offset;
484 abbr = &(tz->timezone_abbr[to->abbr_idx]);
485 tmp->is_dst = to->isdst;
486 tmp->transistion_time = transistion_time;
487 } else {
488 offset = 0;
489 abbr = tz->timezone_abbr;
490 tmp->is_dst = 0;
491 tmp->transistion_time = 0;
492 }
493
494 if ((tl = fetch_leaptime_offset(tz, ts))) {
495 leap_secs = -tl->offset;
496 }
497
498 tmp->offset = offset;
499 tmp->leap_secs = leap_secs;
500 tmp->abbr = abbr ? strdup(abbr) : strdup("GMT");
501
502 return tmp;
503 }
504
timelib_get_current_offset(timelib_time * t)505 timelib_sll timelib_get_current_offset(timelib_time *t)
506 {
507 timelib_time_offset *gmt_offset;
508 timelib_sll retval;
509
510 switch (t->zone_type) {
511 case TIMELIB_ZONETYPE_ABBR:
512 case TIMELIB_ZONETYPE_OFFSET:
513 return (t->z + t->dst) * -60;
514
515 case TIMELIB_ZONETYPE_ID:
516 gmt_offset = timelib_get_time_zone_info(t->sse, t->tz_info);
517 retval = gmt_offset->offset;
518 timelib_time_offset_dtor(gmt_offset);
519 return retval;
520
521 default:
522 return 0;
523 }
524 }
525