1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
|
/*** tzraw.c -- reader for olson database zoneinfo files
*
* Copyright (C) 2009-2024 Sebastian Freundt
*
* Author: Sebastian Freundt <freundt@ga-group.nl>
*
* This file is part of uterus and dateutils.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
***/
/* implementation part of tzraw.h */
#if !defined INCLUDED_tzraw_c_
#define INCLUDED_tzraw_c_
#if defined HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#if defined MAP_ANON_NEEDS_DARWIN_SOURCE
# define _DARWIN_C_SOURCE
#endif /* MAP_ANON_NEEDS_DARWIN_SOURCE */
#if defined MAP_ANON_NEEDS_ALL_SOURCE
# define _ALL_SOURCE
#endif /* MAP_ANON_NEEDS_ALL_SOURCE */
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <assert.h>
#if defined HAVE_TZFILE_H
# include <tzfile.h>
#endif /* HAVE_TZFILE_H */
/* for be/le conversions */
#include "boops.h"
/* for LIKELY/UNLIKELY/etc. */
#include "nifty.h"
/* me own header, innit */
#include "tzraw.h"
/* for leap corrections */
#include "leap-seconds.h"
#if !defined DEFUN
# define DEFUN
#endif /* !DEFUN */
#if !defined MAP_ANONYMOUS && defined MAP_ANON
# define MAP_ANONYMOUS (MAP_ANON)
#elif !defined MAP_ANON
# define MAP_ANON (0x1000U)
#endif /* MAP_ANON->MAP_ANONYMOUS */
typedef uint8_t zty_t;
typedef int zof_t;
/* this is tzhead but better */
struct zih_s {
/* magic */
char tzh_magic[4];
/* must be '2' now, as of 2005 */
char tzh_version[1];
/* reserved--must be zero */
char tzh_reserved[15];
/* number of transition time flags in gmt */
unsigned char tzh_ttisgmtcnt[4];
/* number of transition time flags in local time */
unsigned char tzh_ttisstdcnt[4];
/* number of recorded leap seconds */
unsigned char tzh_leapcnt[4];
/* number of recorded transition times */
unsigned char tzh_timecnt[4];
/* number of local time type */
unsigned char tzh_typecnt[4];
/* number of abbreviation chars */
unsigned char tzh_charcnt[4];
};
/* this one must be packed to account for the packed file layout */
struct ztrdtl_s {
int32_t offs;
uint8_t dstp;
uint8_t abbr;
} __attribute__((packed));
/* convenience struct where we copy all the good things into one */
struct zspec_s {
stamp_t since;
unsigned int offs:31;
unsigned int dstp:1;
char *name;
} __attribute__((packed, aligned(16)));
/* for leap second transitions */
struct zlp_s {
/* cut-off stamp */
stamp_t t;
/* cumulative correction since T */
int32_t corr;
};
/* leap second support missing as we do our own, see leaps.[ch] */
struct zif_s {
size_t ntr;
size_t nty;
size_t nlp;
/* NTR transitions */
stamp_t *trs;
/* NTR types */
zty_t *tys;
/* NTY type array, transition details */
zof_t *ofs;
/* leaps */
struct zlp_s *lps;
/* for special zones */
coord_zone_t cz;
/* zone caching, between PREV and NEXT the offset is OFFS */
struct zrng_s cache;
stamp_t data[0] __attribute__((aligned(16)));
};
#if defined TZDIR
static const char tzdir[] = TZDIR;
#else /* !TZDIR */
static const char tzdir[] = "/usr/share/zoneinfo";
#endif
#define PROT_MEMMAP PROT_READ | PROT_WRITE
#define MAP_MEMMAP MAP_PRIVATE | MAP_ANON
/* special zone names */
static const char coord_zones[][4] = {
"",
"UTC",
"TAI",
"GPS",
};
static inline uint32_t
RDU32(const unsigned char *x)
{
uint32_t r = 0U;
r ^= x[0U] << 24U;
r ^= x[1U] << 16U;
r ^= x[2U] << 8U;
r ^= x[3U] << 0;
return r;
}
static inline int32_t
RDI32(const unsigned char *x)
{
int32_t r = 0U;
r ^= x[0U] << 24U;
r ^= x[1U] << 16U;
r ^= x[2U] << 8U;
r ^= x[3U] << 0;
return r;
}
static inline int64_t
RDI64(const unsigned char *x)
{
int64_t r = 0U;
r ^= (uint64_t)x[0U] << 56U;
r ^= (uint64_t)x[1U] << 48U;
r ^= (uint64_t)x[2U] << 40U;
r ^= (uint64_t)x[3U] << 32U;
r ^= (uint64_t)x[4U] << 24U;
r ^= (uint64_t)x[5U] << 16U;
r ^= (uint64_t)x[6U] << 8U;
r ^= (uint64_t)x[7U] << 0;
return r;
}
/**
* Return the transition time stamp of the N-th transition in Z. */
static inline stamp_t
zif_trans(const struct zif_s z[static 1U], int n)
{
size_t ntr = z->ntr;
if (UNLIKELY(!ntr || n < 0)) {
/* return earliest possible stamp */
return STAMP_MIN;
} else if (UNLIKELY(n >= (ssize_t)ntr)) {
/* return last known stamp */
return z->trs[ntr - 1U];
}
/* otherwise return n-th stamp */
return z->trs[n];
}
/**
* Return the transition type index of the N-th transition in Z. */
static inline uint8_t
_zif_type(const struct zif_s z[static 1U], int n)
{
size_t ntr = z->ntr;
if (UNLIKELY(!ntr || n < 0)) {
/* return unknown type */
return 0;
} else if (UNLIKELY(n >= (ssize_t)ntr)) {
/* return last known type */
return z->tys[ntr - 1U];
}
/* otherwise return n-th type */
return z->tys[n];
}
/**
* Return the gmt offset after the N-th transition in Z. */
static inline int
_zif_troffs(const struct zif_s z[static 1U], int n)
{
/* no bound check! */
uint8_t idx = _zif_type(z, n);
return z->ofs[idx];
}
/**
* Return the gmt offset after the N-th transition in Z. */
DEFUN int
zif_troffs(zif_t z, int n)
{
/* no bound check! */
uint8_t idx = _zif_type(z, n);
return z->ofs[idx];
}
/**
* Return the transition time stamp of the N-th transition in Z. */
DEFUN inline size_t
zif_ntrans(zif_t z)
{
return z->ntr;
}
static coord_zone_t
coord_zone(const char *zone)
{
for (coord_zone_t i = TZCZ_UTC; i < TZCZ_NZONE; i++) {
if (strcmp(zone, coord_zones[i]) == 0) {
return i;
}
}
return TZCZ_UNK;
}
static int
__open_zif(const char *file)
{
size_t len;
if (UNLIKELY(file == NULL || file[0] == '\0')) {
return -1;
} else if (UNLIKELY((len = strlen(file)) >= 3071U)) {
return -1;
} else if (file[0] != '/') {
/* not an absolute file name */
size_t tzd_len = sizeof(tzdir) - 1;
char new[tzd_len + 1U + len + 1U];
char *tmp = new + tzd_len;
memcpy(new, tzdir, tzd_len);
*tmp++ = '/';
memcpy(tmp, file, len + 1U);
return open(new, O_RDONLY, 0644);
}
/* absolute file name, just try with that one then */
return open(file, O_RDONLY, 0644);
}
DEFUN void
zif_close(zif_t z)
{
if (!z->cz) {
free(z);
}
return;
}
DEFUN zif_t
zif_open(const char *file)
{
struct stat st;
coord_zone_t cz;
int fd;
struct zif_s tmp;
struct zif_s *res;
unsigned char *map;
const unsigned char *hdr, *beef;
size_t real_ntr = 0U;
/* check for special time zones */
if ((cz = coord_zone(file)) > TZCZ_UNK) {
/* we used to go to the UTC file hoping they're coordinated well,
* however seeing as we're dealing with leap seconds ourself we
* can just skip the file reading and return a static instance */
static struct zif_s coord_zifs[] = {
[TZCZ_UTC] = {.cz = TZCZ_UTC, .ofs = (void*)&coord_zifs},
[TZCZ_TAI] = {.cz = TZCZ_TAI, .ofs = (void*)&coord_zifs},
[TZCZ_GPS] = {.cz = TZCZ_GPS, .ofs = (void*)&coord_zifs},
};
return coord_zifs + cz;
}
if (UNLIKELY((fd = __open_zif(file)) < STDIN_FILENO)) {
return NULL;
} else if (fstat(fd, &st) < 0) {
goto cout;
} else if (st.st_size <= 20) {
goto cout;
}
map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
goto cout;
}
if (UNLIKELY(memcmp(map, TZ_MAGIC, 4U))) {
goto unmp;
}
/* read hdr with undefined alignment */
hdr = map;
switch (hdr[offsetof(struct zih_s, tzh_version)]) {
const unsigned char *hds;
case '2':
/*@fallthrough@*/
case '3':
hds = hdr;
tmp.nlp = RDU32(hdr + offsetof(struct zih_s, tzh_leapcnt));
tmp.ntr = RDU32(hdr + offsetof(struct zih_s, tzh_timecnt));
tmp.nty = RDU32(hdr + offsetof(struct zih_s, tzh_typecnt));
hds += sizeof(struct zih_s);
hds += tmp.ntr * 4U;
hds += tmp.ntr;
hds += tmp.nty * (4U + 1U + 1U);
hds += RDU32(hdr + offsetof(struct zih_s, tzh_charcnt));
hds += tmp.nlp * (4U + 4U);
hds += RDU32(hdr + offsetof(struct zih_s, tzh_ttisstdcnt));
hds += RDU32(hdr + offsetof(struct zih_s, tzh_ttisgmtcnt));
if (UNLIKELY(memcmp(hds, TZ_MAGIC, 4U))) {
goto unmp;
}
hdr = hds;
case '\0':
tmp.nlp = RDU32(hdr + offsetof(struct zih_s, tzh_leapcnt));
tmp.ntr = RDU32(hdr + offsetof(struct zih_s, tzh_timecnt));
tmp.nty = RDU32(hdr + offsetof(struct zih_s, tzh_typecnt));
break;
default:
goto unmp;
}
/* alloc space, don't read leaps just transitions and types */
res = malloc(sizeof(*res) +
tmp.ntr * sizeof(*res->trs) +
tmp.nty * sizeof(*res->ofs) +
tmp.ntr * sizeof(*res->tys) +
0);
if (UNLIKELY(res == NULL)) {
goto unmp;
}
/* otherwise fill */
*res = tmp;
res->trs = (stamp_t*)(res->data + 0);
res->ofs = (zof_t*)(res->trs + tmp.ntr);
res->tys = (zty_t*)(res->ofs + tmp.nty);
res->lps = NULL;
res->cz = cz;
res->cache = (struct zrng_s){0};
/* copy data (and bring to host order) */
beef = hdr + sizeof(struct zih_s);
switch (hdr[offsetof(struct zih_s, tzh_version)]) {
case '2':
/*@fallthrough@*/
case '3':
for (size_t i = 0U; i < tmp.ntr; i++) {
res->trs[i] = RDI64(beef + 8U * i);
}
beef += 8U * tmp.ntr;
memcpy(res->tys, beef, tmp.ntr);
beef += tmp.ntr;
for (size_t i = 0U; i < tmp.nty; i++) {
res->ofs[i] = RDI32(beef + 6U * i);
}
break;
case '\0':
for (size_t i = 0U; i < tmp.ntr; i++) {
res->trs[i] = RDI32(beef + 4U * i);
}
beef += 4U * tmp.ntr;
memcpy(res->tys, beef, tmp.ntr);
beef += tmp.ntr;
for (size_t i = 0U; i < tmp.nty; i++) {
res->ofs[i] = RDI32(beef + 6U * i);
}
break;
}
/* clean up */
munmap(map, st.st_size);
close(fd);
/* compactify, we disallow transitions to the same type */
real_ntr += res->ntr > 0U;
for (size_t i = 1U; i < res->ntr; i++) {
if (res->tys[i - 1U] != res->tys[i - 0U]) {
res->trs[real_ntr] = res->trs[i];
res->tys[real_ntr] = res->tys[i];
real_ntr++;
}
}
res->ntr = real_ntr;
return res;
unmp:
munmap(map, st.st_size);
cout:
close(fd);
return NULL;
}
DEFUN zif_t
zif_copy(zif_t z)
{
/* copy Z into a newly allocated zif_t object
* if applicable also perform byte-order conversions */
struct zif_s *res;
res = malloc(sizeof(*z) +
z->ntr * sizeof(*z->trs) +
z->nty * sizeof(*z->ofs) +
z->ntr * sizeof(*z->tys) +
0);
if (UNLIKELY(res == NULL)) {
/* no need to bother */
return NULL;
}
/* initialise */
res->ntr = z->ntr;
res->nty = z->nty;
res->nlp = z->nlp;
res->trs = (stamp_t*)(res->data + 0);
res->ofs = (zof_t*)(res->trs + z->ntr);
res->tys = (zty_t*)(res->ofs + z->nty);
res->lps = NULL;
res->cz = z->cz;
res->cache = (struct zrng_s){0};
/* ... and copy */
memcpy(res->trs, z->trs, z->ntr * sizeof(*z->trs));
memcpy(res->ofs, z->ofs, z->nty * sizeof(*z->ofs));
memcpy(res->tys, z->tys, z->ntr * sizeof(*z->tys));
return res;
}
/* for leap corrections */
#include "leap-seconds.def"
static inline int
__find_trno(const struct zif_s z[static 1U], stamp_t t, int min, int max)
{
/* find the last transition before T, T is expected to be UTC
* if T is before any known transition return -1 */
if (UNLIKELY(max == 0)) {
/* special case */
return -1;
} else if (UNLIKELY(t < zif_trans(z, min))) {
return -1;
} else if (UNLIKELY(t > zif_trans(z, max))) {
return max - 1;
}
do {
stamp_t tl, tu;
int this = (min + max) / 2;
tl = zif_trans(z, this);
tu = zif_trans(z, this + 1);
if (t >= tl && t < tu) {
/* found him */
return this;
} else if (t >= tu) {
min = this;
} else if (t < tl) {
max = this;
}
} while (true);
/* not reached */
}
DEFUN inline int
zif_find_trans(zif_t z, stamp_t t)
{
/* find the last transition before T, T is expected to be UTC
* if T is before any known transition return -1 */
int max = z->ntr;
int min = 0;
return __find_trno(z, t, min, max);
}
static struct zrng_s
__find_zrng(const struct zif_s z[static 1U], stamp_t t, int min, int max)
{
struct zrng_s res;
int trno;
trno = __find_trno(z, t, min, max);
res.prev = zif_trans(z, trno);
if (UNLIKELY(trno <= 0 && t < res.prev)) {
res.trno = 0U;
res.prev = STAMP_MIN;
/* assume the first offset has always been there */
res.next = res.prev;
} else if (UNLIKELY(trno < 0)) {
/* special case where no transitions are recorded */
res.trno = 0U;
res.prev = STAMP_MIN;
res.next = STAMP_MAX;
} else {
res.trno = (uint8_t)trno;
if (LIKELY(trno + 1U < z->ntr)) {
res.next = zif_trans(z, trno + 1U);
} else {
res.next = STAMP_MAX;
}
}
res.offs = _zif_troffs(z, res.trno);
return res;
}
DEFUN struct zrng_s
zif_find_zrng(zif_t z, stamp_t t)
{
/* find the last transition before time, time is expected to be UTC */
int max = z->ntr;
int min = 0;
return __find_zrng(z, t, min, max);
}
static stamp_t
__tai_offs(stamp_t t)
{
/* difference of TAI and UTC at epoch instant */
zidx_t zi = leaps_before_si32(leaps_s, nleaps_corr, t);
return leaps_corr[zi];
}
static stamp_t
__gps_offs(stamp_t t)
{
/* TAI - GPS = 19 on 1980-01-06, so use that identity here */
const stamp_t gps_offs_epoch = 19;
if (UNLIKELY(t < 315964800)) {
return 0;
}
return __tai_offs(t) - gps_offs_epoch;
}
static stamp_t
__offs(struct zif_s z[static 1U], stamp_t t)
{
/* return the offset of T in Z and cache the result. */
int min;
size_t max;
switch (z->cz) {
default:
case TZCZ_UNK:
break;
case TZCZ_UTC:
return 0;
case TZCZ_TAI:
return __tai_offs(t);
case TZCZ_GPS:
return __gps_offs(t);
}
/* use the classic code */
if (LIKELY(t >= z->cache.prev && t < z->cache.next)) {
/* use the cached offset */
return z->cache.offs;
} else if (t >= z->cache.next) {
min = z->cache.trno + 1;
max = z->ntr;
} else if (t < z->cache.prev) {
max = z->cache.trno;
min = 0;
} else {
/* we shouldn't end up here at all */
min = 0;
max = 0;
}
return (z->cache = __find_zrng(z, t, min, max)).offs;
}
DEFUN stamp_t
zif_utc_time(zif_t z, stamp_t t)
{
/* here's the setup, given t in local time, we denote the corresponding
* UTC time by t' = t - x' where x' is the true offset
* however, since we do not know the offset in advance, we have to solve
* for an estimate of the offset x:
* t - x + x' = t, or equivalently t - x = t' or as a root finding problem
* x' - x = 0.
* To make this iterative we just solve:
* x_{i+1} - x_i = 0, where x_{i+1} = o(t - x_i) and o maps a given
* time stamp to an offset. */
/* make me use the cache please! */
/* let's go */
stamp_t xi = 0;
stamp_t xj;
stamp_t old = -1;
/* jump off the cliff if Z is nought */
if (UNLIKELY(z == NULL)) {
return t;
}
while ((xj = __offs(z, t - xi)) != xi && xi != old) {
old = xi = xj;
}
return t - xj;
}
/* convert utc to local */
DEFUN stamp_t
zif_local_time(zif_t z, stamp_t t)
{
/* jump off the cliff if Z is nought */
if (UNLIKELY(z == NULL)) {
return t;
}
return t + __offs(z, t);
}
#endif /* INCLUDED_tzraw_c_ */
#if defined STANDALONE
#include <stdio.h>
int
main(int argc, char *argv[])
{
int rc = 0;
for (int i = 1; i < argc; i++) {
zif_t z = zif_open(argv[i]);
if (z == NULL) {
rc++;
continue;
}
puts(argv[i]);
printf(" ntr\t%zu\n", z->ntr);
printf(" nty\t%zu\n", z->nty);
printf(" nlp\t%zu\n", z->nlp);
for (size_t j = 0U; j < z->ntr; j++) {
printf(" tr[%zu]\t%lld\t%hhu\n", j, z->trs[j], z->tys[j]);
}
for (size_t j = 0U; j < z->nty; j++) {
printf(" of[%zu]\t%d\n", j, z->ofs[j]);
}
zif_close(z);
}
return rc;
}
#endif /* STANDALONE */
/* tzraw.c ends here */
|