1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015-2019 Derick Rethans
5 * Copyright (c) 2018 MongoDB, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include "timelib.h"
27 #include "timelib_private.h"
28
29 #define TIMELIB_SUPPORTS_V2DATA
30 #include "timezonedb.h"
31
32 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
33 # if defined(__LITTLE_ENDIAN__)
34 # undef WORDS_BIGENDIAN
35 # else
36 # if defined(__BIG_ENDIAN__)
37 # define WORDS_BIGENDIAN
38 # endif
39 # endif
40 #endif
41
42 #if (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN))
43 # if __BYTE_ORDER == __BIG_ENDIAN
44 # define WORDS_BIGENDIAN
45 # endif
46 #endif
47
48 #if defined(__s390__)
49 # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
50 # define WORDS_BIGENDIAN
51 # else
52 # undef WORDS_BIGENDIAN
53 # endif
54 #endif
55
56 #ifdef WORDS_BIGENDIAN
timelib_conv_int_unsigned(uint32_t value)57 static inline uint32_t timelib_conv_int_unsigned(uint32_t value)
58 {
59 return value;
60 }
61
timelib_conv_int64_unsigned(uint64_t value)62 static inline uint64_t timelib_conv_int64_unsigned(uint64_t value)
63 {
64 return value;
65 }
66 #else
timelib_conv_int_unsigned(uint32_t value)67 static inline uint32_t timelib_conv_int_unsigned(uint32_t value)
68 {
69 return
70 ((value & 0x000000ff) << 24) +
71 ((value & 0x0000ff00) << 8) +
72 ((value & 0x00ff0000) >> 8) +
73 ((value & 0xff000000) >> 24);
74 }
75
timelib_conv_int64_unsigned(uint64_t value)76 static inline uint64_t timelib_conv_int64_unsigned(uint64_t value)
77 {
78 return
79 ((value & 0x00000000000000ff) << 56) +
80 ((value & 0x000000000000ff00) << 40) +
81 ((value & 0x0000000000ff0000) << 24) +
82 ((value & 0x00000000ff000000) << 8) +
83 ((value & 0x000000ff00000000) >> 8) +
84 ((value & 0x0000ff0000000000) >> 24) +
85 ((value & 0x00ff000000000000) >> 40) +
86 ((value & 0xff00000000000000) >> 56);
87 }
88 #endif
89
90 #define timelib_conv_int_signed(value) ((int32_t) timelib_conv_int_unsigned((int32_t) value))
91 #define timelib_conv_int64_signed(value) ((int64_t) timelib_conv_int64_unsigned((int64_t) value))
92
read_php_preamble(const unsigned char ** tzf,timelib_tzinfo * tz)93 static int read_php_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
94 {
95 uint32_t version;
96
97 /* read ID */
98 version = (*tzf)[3] - '0';
99 *tzf += 4;
100
101 /* read BC flag */
102 tz->bc = (**tzf == '\1');
103 *tzf += 1;
104
105 /* read country code */
106 memcpy(tz->location.country_code, *tzf, 2);
107 tz->location.country_code[2] = '\0';
108 *tzf += 2;
109
110 /* skip rest of preamble */
111 *tzf += 13;
112
113 return version;
114 }
115
read_tzif_preamble(const unsigned char ** tzf,timelib_tzinfo * tz)116 static int read_tzif_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
117 {
118 uint32_t version;
119
120 /* read ID */
121 switch ((*tzf)[4]) {
122 case '\0':
123 version = 0;
124 break;
125 case '2':
126 version = 2;
127 break;
128 case '3':
129 version = 3;
130 break;
131 default:
132 return -1;
133 }
134 *tzf += 5;
135
136 /* set BC flag and country code to default */
137 tz->bc = 0;
138 tz->location.country_code[0] = '?';
139 tz->location.country_code[1] = '?';
140 tz->location.country_code[2] = '\0';
141
142 /* skip rest of preamble */
143 *tzf += 15;
144
145 return version;
146 }
147
read_preamble(const unsigned char ** tzf,timelib_tzinfo * tz,unsigned int * type)148 static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz, unsigned int *type)
149 {
150 /* read marker (TZif) or (PHP) */
151 if (memcmp(*tzf, "PHP", 3) == 0) {
152 *type = TIMELIB_TZINFO_PHP;
153 return read_php_preamble(tzf, tz);
154 } else if (memcmp(*tzf, "TZif", 4) == 0) {
155 *type = TIMELIB_TZINFO_ZONEINFO;
156 return read_tzif_preamble(tzf, tz);
157 } else {
158 return -1;
159 }
160 }
161
read_32bit_header(const unsigned char ** tzf,timelib_tzinfo * tz)162 static void read_32bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
163 {
164 uint32_t buffer[6];
165
166 memcpy(&buffer, *tzf, sizeof(buffer));
167 tz->_bit32.ttisgmtcnt = timelib_conv_int_unsigned(buffer[0]);
168 tz->_bit32.ttisstdcnt = timelib_conv_int_unsigned(buffer[1]);
169 tz->_bit32.leapcnt = timelib_conv_int_unsigned(buffer[2]);
170 tz->_bit32.timecnt = timelib_conv_int_unsigned(buffer[3]);
171 tz->_bit32.typecnt = timelib_conv_int_unsigned(buffer[4]);
172 tz->_bit32.charcnt = timelib_conv_int_unsigned(buffer[5]);
173
174 *tzf += sizeof(buffer);
175 }
176
detect_slim_file(timelib_tzinfo * tz)177 static int detect_slim_file(timelib_tzinfo *tz)
178 {
179 if (
180 (tz->_bit32.ttisgmtcnt == 0) &&
181 (tz->_bit32.ttisstdcnt == 0) &&
182 (tz->_bit32.leapcnt == 0) &&
183 (tz->_bit32.timecnt == 0) &&
184 (tz->_bit32.typecnt == 1) &&
185 (tz->_bit32.charcnt == 1)
186 ) {
187 return 1;
188 }
189
190 return 0;
191 }
192
read_64bit_transitions(const unsigned char ** tzf,timelib_tzinfo * tz)193 static int read_64bit_transitions(const unsigned char **tzf, timelib_tzinfo *tz)
194 {
195 int64_t *buffer = NULL;
196 uint32_t i;
197 unsigned char *cbuffer = NULL;
198
199 if (tz->bit64.timecnt) {
200 buffer = (int64_t*) timelib_malloc(tz->bit64.timecnt * sizeof(int64_t));
201 if (!buffer) {
202 return TIMELIB_ERROR_CANNOT_ALLOCATE;
203 }
204 memcpy(buffer, *tzf, sizeof(int64_t) * tz->bit64.timecnt);
205 *tzf += (sizeof(int64_t) * tz->bit64.timecnt);
206 for (i = 0; i < tz->bit64.timecnt; i++) {
207 buffer[i] = timelib_conv_int64_signed(buffer[i]);
208 /* Sanity check to see whether TS is just increasing */
209 if (i > 0 && !(buffer[i] > buffer[i - 1])) {
210 return TIMELIB_ERROR_CORRUPT_TRANSITIONS_DONT_INCREASE;
211 }
212 }
213
214 cbuffer = (unsigned char*) timelib_malloc(tz->bit64.timecnt * sizeof(unsigned char));
215 if (!cbuffer) {
216 timelib_free(buffer);
217 return TIMELIB_ERROR_CANNOT_ALLOCATE;
218 }
219 memcpy(cbuffer, *tzf, sizeof(unsigned char) * tz->bit64.timecnt);
220 *tzf += sizeof(unsigned char) * tz->bit64.timecnt;
221 }
222
223 tz->trans = buffer;
224 tz->trans_idx = cbuffer;
225
226 return 0;
227 }
228
skip_32bit_transitions(const unsigned char ** tzf,timelib_tzinfo * tz)229 static void skip_32bit_transitions(const unsigned char **tzf, timelib_tzinfo *tz)
230 {
231 if (tz->_bit32.timecnt) {
232 *tzf += (sizeof(int32_t) * tz->_bit32.timecnt);
233 *tzf += sizeof(unsigned char) * tz->_bit32.timecnt;
234 }
235 }
236
read_64bit_types(const unsigned char ** tzf,timelib_tzinfo * tz)237 static int read_64bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
238 {
239 unsigned char *buffer;
240 int32_t *leap_buffer;
241 unsigned int i, j;
242
243 /* Offset Types */
244 buffer = (unsigned char*) timelib_malloc(tz->bit64.typecnt * sizeof(unsigned char) * 6);
245 if (!buffer) {
246 return TIMELIB_ERROR_CANNOT_ALLOCATE;
247 }
248 memcpy(buffer, *tzf, sizeof(unsigned char) * 6 * tz->bit64.typecnt);
249 *tzf += sizeof(unsigned char) * 6 * tz->bit64.typecnt;
250
251 tz->type = (ttinfo*) timelib_calloc(1, tz->bit64.typecnt * sizeof(ttinfo));
252 if (!tz->type) {
253 timelib_free(buffer);
254 return TIMELIB_ERROR_CANNOT_ALLOCATE;
255 }
256
257 for (i = 0; i < tz->bit64.typecnt; i++) {
258 j = i * 6;
259 tz->type[i].offset = 0;
260 tz->type[i].offset += (int32_t) (((uint32_t) buffer[j]) << 24) + (buffer[j + 1] << 16) + (buffer[j + 2] << 8) + tz->type[i].offset + buffer[j + 3];
261 tz->type[i].isdst = buffer[j + 4];
262 tz->type[i].abbr_idx = buffer[j + 5];
263 }
264 timelib_free(buffer);
265
266 /* Abbreviations */
267 tz->timezone_abbr = (char*) timelib_malloc(tz->bit64.charcnt);
268 if (!tz->timezone_abbr) {
269 return TIMELIB_ERROR_CORRUPT_NO_ABBREVIATION;
270 }
271 memcpy(tz->timezone_abbr, *tzf, sizeof(char) * tz->bit64.charcnt);
272 *tzf += sizeof(char) * tz->bit64.charcnt;
273
274 /* Leap seconds (only use in 'right/') format */
275 if (tz->bit64.leapcnt) {
276 leap_buffer = (int32_t *) timelib_malloc(tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t)));
277 if (!leap_buffer) {
278 return TIMELIB_ERROR_CANNOT_ALLOCATE;
279 }
280 memcpy(leap_buffer, *tzf, tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t)));
281 *tzf += tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t));
282
283 tz->leap_times = (tlinfo*) timelib_malloc(tz->bit64.leapcnt * sizeof(tlinfo));
284 if (!tz->leap_times) {
285 timelib_free(leap_buffer);
286 return TIMELIB_ERROR_CANNOT_ALLOCATE;
287 }
288 for (i = 0; i < tz->bit64.leapcnt; i++) {
289 tz->leap_times[i].trans = timelib_conv_int64_signed(leap_buffer[i * 3 + 1] * 4294967296 + leap_buffer[i * 3]);
290 tz->leap_times[i].offset = timelib_conv_int_signed(leap_buffer[i * 3 + 2]);
291 }
292 timelib_free(leap_buffer);
293 }
294
295 /* Standard/Wall Indicators (unused) */
296 if (tz->bit64.ttisstdcnt) {
297 buffer = (unsigned char*) timelib_malloc(tz->bit64.ttisstdcnt * sizeof(unsigned char));
298 if (!buffer) {
299 return TIMELIB_ERROR_CANNOT_ALLOCATE;
300 }
301 memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit64.ttisstdcnt);
302 *tzf += sizeof(unsigned char) * tz->bit64.ttisstdcnt;
303
304 for (i = 0; i < tz->bit64.ttisstdcnt; i++) {
305 tz->type[i].isstdcnt = buffer[i];
306 }
307 timelib_free(buffer);
308 }
309
310 /* UT/Local Time Indicators (unused) */
311 if (tz->bit64.ttisgmtcnt) {
312 buffer = (unsigned char*) timelib_malloc(tz->bit64.ttisgmtcnt * sizeof(unsigned char));
313 if (!buffer) {
314 return TIMELIB_ERROR_CANNOT_ALLOCATE;
315 }
316 memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit64.ttisgmtcnt);
317 *tzf += sizeof(unsigned char) * tz->bit64.ttisgmtcnt;
318
319 for (i = 0; i < tz->bit64.ttisgmtcnt; i++) {
320 tz->type[i].isgmtcnt = buffer[i];
321 }
322 timelib_free(buffer);
323 }
324
325 return 0;
326 }
327
skip_32bit_types(const unsigned char ** tzf,timelib_tzinfo * tz)328 static void skip_32bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
329 {
330 /* Offset Types */
331 *tzf += sizeof(unsigned char) * 6 * tz->_bit32.typecnt;
332
333 /* Abbreviations */
334 *tzf += sizeof(char) * tz->_bit32.charcnt;
335
336 /* Leap seconds (only use in 'right/') format */
337 if (tz->_bit32.leapcnt) {
338 *tzf += sizeof(int32_t) * tz->_bit32.leapcnt * 2;
339 }
340
341 /* Standard/Wall Indicators (unused) */
342 if (tz->_bit32.ttisstdcnt) {
343 *tzf += sizeof(unsigned char) * tz->_bit32.ttisstdcnt;
344 }
345
346 /* UT/Local Time Indicators (unused) */
347 if (tz->_bit32.ttisgmtcnt) {
348 *tzf += sizeof(unsigned char) * tz->_bit32.ttisgmtcnt;
349 }
350 }
351
skip_posix_string(const unsigned char ** tzf,timelib_tzinfo * tz)352 static void skip_posix_string(const unsigned char **tzf, timelib_tzinfo *tz)
353 {
354 int n_count = 0;
355
356 do {
357 if (*tzf[0] == '\n') {
358 n_count++;
359 }
360 (*tzf)++;
361 } while (n_count < 2);
362 }
363
read_location(const unsigned char ** tzf,timelib_tzinfo * tz)364 static void read_location(const unsigned char **tzf, timelib_tzinfo *tz)
365 {
366 uint32_t buffer[3];
367 uint32_t comments_len;
368
369 memcpy(&buffer, *tzf, sizeof(buffer));
370 tz->location.latitude = timelib_conv_int_unsigned(buffer[0]);
371 tz->location.latitude = (tz->location.latitude / 100000) - 90;
372 tz->location.longitude = timelib_conv_int_unsigned(buffer[1]);
373 tz->location.longitude = (tz->location.longitude / 100000) - 180;
374 comments_len = timelib_conv_int_unsigned(buffer[2]);
375 *tzf += sizeof(buffer);
376
377 tz->location.comments = timelib_malloc(comments_len + 1);
378 memcpy(tz->location.comments, *tzf, comments_len);
379 tz->location.comments[comments_len] = '\0';
380 *tzf += comments_len;
381 }
382
set_default_location_and_comments(const unsigned char ** tzf,timelib_tzinfo * tz)383 static void set_default_location_and_comments(const unsigned char **tzf, timelib_tzinfo *tz)
384 {
385 tz->location.latitude = 0;
386 tz->location.longitude = 0;
387 tz->location.comments = timelib_malloc(2);
388 tz->location.comments[0] = '?';
389 tz->location.comments[1] = '\0';
390 }
391
timelib_dump_tzinfo(timelib_tzinfo * tz)392 void timelib_dump_tzinfo(timelib_tzinfo *tz)
393 {
394 uint32_t i;
395
396 printf("Country Code: %s\n", tz->location.country_code);
397 printf("Geo Location: %f,%f\n", tz->location.latitude, tz->location.longitude);
398 printf("Comments:\n%s\n", tz->location.comments);
399 printf("BC: %s\n", tz->bc ? "" : "yes");
400 printf("Slim File: %s\n", detect_slim_file(tz) ? "yes" : "no");
401
402 printf("\n64-bit:\n");
403 printf("UTC/Local count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.ttisgmtcnt);
404 printf("Std/Wall count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.ttisstdcnt);
405 printf("Leap.sec. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.leapcnt);
406 printf("Trans. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.timecnt);
407 printf("Local types count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.typecnt);
408 printf("Zone Abbr. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.charcnt);
409
410 printf ("%16s (%20s) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
411 "", "", 0,
412 (long int) tz->type[0].offset,
413 tz->type[0].isdst,
414 tz->type[0].abbr_idx,
415 &tz->timezone_abbr[tz->type[0].abbr_idx],
416 tz->type[0].isstdcnt,
417 tz->type[0].isgmtcnt
418 );
419 for (i = 0; i < tz->bit64.timecnt; i++) {
420 printf ("%016" PRIX64 " (%20" PRId64 ") = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
421 tz->trans[i], tz->trans[i], tz->trans_idx[i],
422 (long int) tz->type[tz->trans_idx[i]].offset,
423 tz->type[tz->trans_idx[i]].isdst,
424 tz->type[tz->trans_idx[i]].abbr_idx,
425 &tz->timezone_abbr[tz->type[tz->trans_idx[i]].abbr_idx],
426 tz->type[tz->trans_idx[i]].isstdcnt,
427 tz->type[tz->trans_idx[i]].isgmtcnt
428 );
429 }
430 for (i = 0; i < tz->bit64.leapcnt; i++) {
431 printf ("%016" PRIX64 " (%20ld) = %d\n",
432 tz->leap_times[i].trans,
433 (long) tz->leap_times[i].trans,
434 tz->leap_times[i].offset);
435 }
436 }
437
seek_to_tz_position(const unsigned char ** tzf,const char * timezone,const timelib_tzdb * tzdb)438 static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
439 {
440 int left = 0, right = tzdb->index_size - 1;
441
442 if (tzdb->index_size == 0) {
443 return 0;
444 }
445
446 do {
447 int mid = ((unsigned)left + right) >> 1;
448 int cmp = timelib_strcasecmp(timezone, tzdb->index[mid].id);
449
450 if (cmp < 0) {
451 right = mid - 1;
452 } else if (cmp > 0) {
453 left = mid + 1;
454 } else { /* (cmp == 0) */
455 (*tzf) = &(tzdb->data[tzdb->index[mid].pos]);
456 return 1;
457 }
458
459 } while (left <= right);
460
461 return 0;
462 }
463
timelib_builtin_db(void)464 const timelib_tzdb *timelib_builtin_db(void)
465 {
466 return &timezonedb_builtin;
467 }
468
timelib_timezone_identifiers_list(const timelib_tzdb * tzdb,int * count)469 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
470 {
471 *count = tzdb->index_size;
472 return tzdb->index;
473 }
474
timelib_timezone_id_is_valid(const char * timezone,const timelib_tzdb * tzdb)475 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
476 {
477 const unsigned char *tzf;
478 return (seek_to_tz_position(&tzf, timezone, tzdb));
479 }
480
skip_64bit_preamble(const unsigned char ** tzf,timelib_tzinfo * tz)481 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
482 {
483 if (memcmp(*tzf, "TZif2", 5) == 0) {
484 *tzf += 20;
485 return 1;
486 } else if (memcmp(*tzf, "TZif3", 5) == 0) {
487 *tzf += 20;
488 return 1;
489 } else {
490 return 0;
491 }
492 }
493
read_64bit_header(const unsigned char ** tzf,timelib_tzinfo * tz)494 static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
495 {
496 uint32_t buffer[6];
497
498 memcpy(&buffer, *tzf, sizeof(buffer));
499 tz->bit64.ttisgmtcnt = timelib_conv_int_unsigned(buffer[0]);
500 tz->bit64.ttisstdcnt = timelib_conv_int_unsigned(buffer[1]);
501 tz->bit64.leapcnt = timelib_conv_int_unsigned(buffer[2]);
502 tz->bit64.timecnt = timelib_conv_int_unsigned(buffer[3]);
503 tz->bit64.typecnt = timelib_conv_int_unsigned(buffer[4]);
504 tz->bit64.charcnt = timelib_conv_int_unsigned(buffer[5]);
505 *tzf += sizeof(buffer);
506 }
507
timelib_tzinfo_ctor(const char * name)508 static timelib_tzinfo* timelib_tzinfo_ctor(const char *name)
509 {
510 timelib_tzinfo *t;
511 t = timelib_calloc(1, sizeof(timelib_tzinfo));
512 t->name = timelib_strdup(name);
513
514 return t;
515 }
516
timelib_parse_tzfile(const char * timezone,const timelib_tzdb * tzdb,int * error_code)517 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
518 {
519 const unsigned char *tzf;
520 timelib_tzinfo *tmp;
521 int version;
522 int transitions_result, types_result;
523 unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
524
525 *error_code = TIMELIB_ERROR_NO_ERROR;
526
527 if (seek_to_tz_position(&tzf, timezone, tzdb)) {
528 tmp = timelib_tzinfo_ctor(timezone);
529
530 version = read_preamble(&tzf, tmp, &type);
531 if (version < 2 || version > 3) {
532 *error_code = TIMELIB_ERROR_UNSUPPORTED_VERSION;
533 timelib_tzinfo_dtor(tmp);
534 return NULL;
535 }
536 //printf("- timezone: %s, version: %0d\n", timezone, version);
537
538 read_32bit_header(&tzf, tmp);
539 skip_32bit_transitions(&tzf, tmp);
540 skip_32bit_types(&tzf, tmp);
541
542 if (detect_slim_file(tmp)) {
543 *error_code = TIMELIB_ERROR_SLIM_FILE;
544 }
545
546 if (!skip_64bit_preamble(&tzf, tmp)) {
547 /* 64 bit preamble is not in place */
548 *error_code = TIMELIB_ERROR_CORRUPT_NO_64BIT_PREAMBLE;
549 timelib_tzinfo_dtor(tmp);
550 return NULL;
551 }
552 read_64bit_header(&tzf, tmp);
553 if ((transitions_result = read_64bit_transitions(&tzf, tmp)) != 0) {
554 /* Corrupt file as transitions do not increase */
555 *error_code = transitions_result;
556 timelib_tzinfo_dtor(tmp);
557 return NULL;
558 }
559 if ((types_result = read_64bit_types(&tzf, tmp)) != 0) {
560 *error_code = types_result;
561 timelib_tzinfo_dtor(tmp);
562 return NULL;
563 }
564 skip_posix_string(&tzf, tmp);
565
566 if (type == TIMELIB_TZINFO_PHP) {
567 read_location(&tzf, tmp);
568 } else {
569 set_default_location_and_comments(&tzf, tmp);
570 }
571 } else {
572 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
573 tmp = NULL;
574 }
575
576 return tmp;
577 }
578
timelib_tzinfo_dtor(timelib_tzinfo * tz)579 void timelib_tzinfo_dtor(timelib_tzinfo *tz)
580 {
581 TIMELIB_TIME_FREE(tz->name);
582 TIMELIB_TIME_FREE(tz->trans);
583 TIMELIB_TIME_FREE(tz->trans_idx);
584 TIMELIB_TIME_FREE(tz->type);
585 TIMELIB_TIME_FREE(tz->timezone_abbr);
586 TIMELIB_TIME_FREE(tz->leap_times);
587 TIMELIB_TIME_FREE(tz->location.comments);
588 TIMELIB_TIME_FREE(tz);
589 tz = NULL;
590 }
591
timelib_tzinfo_clone(timelib_tzinfo * tz)592 timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
593 {
594 timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
595 tmp->_bit32.ttisgmtcnt = tz->_bit32.ttisgmtcnt;
596 tmp->_bit32.ttisstdcnt = tz->_bit32.ttisstdcnt;
597 tmp->_bit32.leapcnt = tz->_bit32.leapcnt;
598 tmp->_bit32.timecnt = tz->_bit32.timecnt;
599 tmp->_bit32.typecnt = tz->_bit32.typecnt;
600 tmp->_bit32.charcnt = tz->_bit32.charcnt;
601 tmp->bit64.ttisgmtcnt = tz->bit64.ttisgmtcnt;
602 tmp->bit64.ttisstdcnt = tz->bit64.ttisstdcnt;
603 tmp->bit64.leapcnt = tz->bit64.leapcnt;
604 tmp->bit64.timecnt = tz->bit64.timecnt;
605 tmp->bit64.typecnt = tz->bit64.typecnt;
606 tmp->bit64.charcnt = tz->bit64.charcnt;
607
608 if (tz->bit64.timecnt) {
609 tmp->trans = (int64_t *) timelib_malloc(tz->bit64.timecnt * sizeof(int64_t));
610 tmp->trans_idx = (unsigned char*) timelib_malloc(tz->bit64.timecnt * sizeof(unsigned char));
611 memcpy(tmp->trans, tz->trans, tz->bit64.timecnt * sizeof(int64_t));
612 memcpy(tmp->trans_idx, tz->trans_idx, tz->bit64.timecnt * sizeof(unsigned char));
613 }
614
615 tmp->type = (ttinfo*) timelib_malloc(tz->bit64.typecnt * sizeof(ttinfo));
616 memcpy(tmp->type, tz->type, tz->bit64.typecnt * sizeof(ttinfo));
617
618 tmp->timezone_abbr = (char*) timelib_malloc(tz->bit64.charcnt);
619 memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->bit64.charcnt);
620
621 if (tz->bit64.leapcnt) {
622 tmp->leap_times = (tlinfo*) timelib_malloc(tz->bit64.leapcnt * sizeof(tlinfo));
623 memcpy(tmp->leap_times, tz->leap_times, tz->bit64.leapcnt * sizeof(tlinfo));
624 }
625
626 return tmp;
627 }
628
fetch_timezone_offset(timelib_tzinfo * tz,timelib_sll ts,timelib_sll * transition_time)629 static ttinfo* fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
630 {
631 uint32_t i;
632
633 /* If there is no transition time, we pick the first one, if that doesn't
634 * exist we return NULL */
635 if (!tz->bit64.timecnt || !tz->trans) {
636 if (tz->bit64.typecnt == 1) {
637 *transition_time = INT64_MIN;
638 return &(tz->type[0]);
639 }
640 return NULL;
641 }
642
643 /* If the TS is lower than the first transition time, then we scan over
644 * all the transition times to find the first non-DST one, or the first
645 * one in case there are only DST entries. Not sure which smartass came up
646 * with this idea in the first though :) */
647 if (ts < tz->trans[0]) {
648 *transition_time = INT64_MIN;
649 return &(tz->type[0]);
650 }
651
652 /* In all other cases we loop through the available transition times to find
653 * the correct entry */
654 for (i = 0; i < tz->bit64.timecnt; i++) {
655 if (ts < tz->trans[i]) {
656 *transition_time = tz->trans[i - 1];
657 return &(tz->type[tz->trans_idx[i - 1]]);
658 }
659 }
660 *transition_time = tz->trans[tz->bit64.timecnt - 1];
661 return &(tz->type[tz->trans_idx[tz->bit64.timecnt - 1]]);
662 }
663
fetch_leaptime_offset(timelib_tzinfo * tz,timelib_sll ts)664 static tlinfo* fetch_leaptime_offset(timelib_tzinfo *tz, timelib_sll ts)
665 {
666 int i;
667
668 if (!tz->bit64.leapcnt || !tz->leap_times) {
669 return NULL;
670 }
671
672 for (i = tz->bit64.leapcnt - 1; i > 0; i--) {
673 if (ts > tz->leap_times[i].trans) {
674 return &(tz->leap_times[i]);
675 }
676 }
677 return NULL;
678 }
679
timelib_timestamp_is_in_dst(timelib_sll ts,timelib_tzinfo * tz)680 int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz)
681 {
682 ttinfo *to;
683 timelib_sll dummy;
684
685 if ((to = fetch_timezone_offset(tz, ts, &dummy))) {
686 return to->isdst;
687 }
688 return -1;
689 }
690
timelib_get_time_zone_info(timelib_sll ts,timelib_tzinfo * tz)691 timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
692 {
693 ttinfo *to;
694 tlinfo *tl;
695 int32_t offset = 0, leap_secs = 0;
696 char *abbr;
697 timelib_time_offset *tmp = timelib_time_offset_ctor();
698 timelib_sll transition_time;
699
700 if ((to = fetch_timezone_offset(tz, ts, &transition_time))) {
701 offset = to->offset;
702 abbr = &(tz->timezone_abbr[to->abbr_idx]);
703 tmp->is_dst = to->isdst;
704 tmp->transition_time = transition_time;
705 } else {
706 offset = 0;
707 abbr = tz->timezone_abbr;
708 tmp->is_dst = 0;
709 tmp->transition_time = 0;
710 }
711
712 if ((tl = fetch_leaptime_offset(tz, ts))) {
713 leap_secs = -tl->offset;
714 }
715
716 tmp->offset = offset;
717 tmp->leap_secs = leap_secs;
718 tmp->abbr = abbr ? timelib_strdup(abbr) : timelib_strdup("GMT");
719
720 return tmp;
721 }
722
timelib_get_current_offset(timelib_time * t)723 timelib_sll timelib_get_current_offset(timelib_time *t)
724 {
725 timelib_time_offset *gmt_offset;
726 timelib_sll retval;
727
728 switch (t->zone_type) {
729 case TIMELIB_ZONETYPE_ABBR:
730 case TIMELIB_ZONETYPE_OFFSET:
731 return t->z + (t->dst * 3600);
732
733 case TIMELIB_ZONETYPE_ID:
734 gmt_offset = timelib_get_time_zone_info(t->sse, t->tz_info);
735 retval = gmt_offset->offset;
736 timelib_time_offset_dtor(gmt_offset);
737 return retval;
738
739 default:
740 return 0;
741 }
742 }
743