zic.c (46c599340f187db577b9212ab18022f3c7380c68) zic.c (a979394afeb5c20fc58c5f5b005d51eb8f92f666)
1/* Compile .zi time zone data into TZif binary files. */
2
3/*
4** This file is in the public domain, so clarified as of
5** 2006-07-17 by Arthur David Olson.
6*/
7
8/* Use the system 'time' function, instead of any private replacement.

--- 458 unchanged lines hidden (view full) ---

467}
468
469ATTRIBUTE_NORETURN static void
470size_overflow(void)
471{
472 memory_exhausted(_("size overflow"));
473}
474
1/* Compile .zi time zone data into TZif binary files. */
2
3/*
4** This file is in the public domain, so clarified as of
5** 2006-07-17 by Arthur David Olson.
6*/
7
8/* Use the system 'time' function, instead of any private replacement.

--- 458 unchanged lines hidden (view full) ---

467}
468
469ATTRIBUTE_NORETURN static void
470size_overflow(void)
471{
472 memory_exhausted(_("size overflow"));
473}
474
475ATTRIBUTE_REPRODUCIBLE static ptrdiff_t
475ATTRIBUTE_PURE_114833 static ptrdiff_t
476size_sum(size_t a, size_t b)
477{
478#ifdef ckd_add
479 ptrdiff_t sum;
480 if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
481 return sum;
482#else
483 if (a <= INDEX_MAX && b <= INDEX_MAX - a)
484 return a + b;
485#endif
486 size_overflow();
487}
488
476size_sum(size_t a, size_t b)
477{
478#ifdef ckd_add
479 ptrdiff_t sum;
480 if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
481 return sum;
482#else
483 if (a <= INDEX_MAX && b <= INDEX_MAX - a)
484 return a + b;
485#endif
486 size_overflow();
487}
488
489ATTRIBUTE_REPRODUCIBLE static ptrdiff_t
489ATTRIBUTE_PURE_114833 static ptrdiff_t
490size_product(ptrdiff_t nitems, ptrdiff_t itemsize)
491{
492#ifdef ckd_mul
493 ptrdiff_t product;
494 if (!ckd_mul(&product, nitems, itemsize) && product <= INDEX_MAX)
495 return product;
496#else
497 ptrdiff_t nitems_max = INDEX_MAX / itemsize;
498 if (nitems <= nitems_max)
499 return nitems * itemsize;
500#endif
501 size_overflow();
502}
503
490size_product(ptrdiff_t nitems, ptrdiff_t itemsize)
491{
492#ifdef ckd_mul
493 ptrdiff_t product;
494 if (!ckd_mul(&product, nitems, itemsize) && product <= INDEX_MAX)
495 return product;
496#else
497 ptrdiff_t nitems_max = INDEX_MAX / itemsize;
498 if (nitems <= nitems_max)
499 return nitems * itemsize;
500#endif
501 size_overflow();
502}
503
504ATTRIBUTE_REPRODUCIBLE static ptrdiff_t
504ATTRIBUTE_PURE_114833 static ptrdiff_t
505align_to(ptrdiff_t size, ptrdiff_t alignment)
506{
507 size_t lo_bits = alignment - 1, sum = size_sum(size, lo_bits);
508 return sum & ~lo_bits;
509}
510
511#if !HAVE_STRDUP
512static char *

--- 7 unchanged lines hidden (view full) ---

520static void *
521memcheck(void *ptr)
522{
523 if (ptr == NULL)
524 memory_exhausted(strerror(HAVE_MALLOC_ERRNO ? errno : ENOMEM));
525 return ptr;
526}
527
505align_to(ptrdiff_t size, ptrdiff_t alignment)
506{
507 size_t lo_bits = alignment - 1, sum = size_sum(size, lo_bits);
508 return sum & ~lo_bits;
509}
510
511#if !HAVE_STRDUP
512static char *

--- 7 unchanged lines hidden (view full) ---

520static void *
521memcheck(void *ptr)
522{
523 if (ptr == NULL)
524 memory_exhausted(strerror(HAVE_MALLOC_ERRNO ? errno : ENOMEM));
525 return ptr;
526}
527
528ATTRIBUTE_MALLOC static void *
528static void *
529emalloc(size_t size)
530{
531 return memcheck(malloc(size));
532}
533
534static void *
535erealloc(void *ptr, size_t size)
536{
537 return memcheck(realloc(ptr, size));
538}
539
529emalloc(size_t size)
530{
531 return memcheck(malloc(size));
532}
533
534static void *
535erealloc(void *ptr, size_t size)
536{
537 return memcheck(realloc(ptr, size));
538}
539
540ATTRIBUTE_MALLOC static char *
540static char *
541estrdup(char const *str)
542{
543 return memcheck(strdup(str));
544}
545
546static ptrdiff_t
547grow_nitems_alloc(ptrdiff_t *nitems_alloc, ptrdiff_t itemsize)
548{

--- 921 unchanged lines hidden (view full) ---

1470 memmove(result + 3 * dotdots, f + dir_len, taillen + 1);
1471 }
1472 return result;
1473}
1474
1475/* Return true if A and B must have the same parent dir if A and B exist.
1476 Return false if this is not necessarily true (though it might be true).
1477 Keep it simple, and do not inspect the file system. */
541estrdup(char const *str)
542{
543 return memcheck(strdup(str));
544}
545
546static ptrdiff_t
547grow_nitems_alloc(ptrdiff_t *nitems_alloc, ptrdiff_t itemsize)
548{

--- 921 unchanged lines hidden (view full) ---

1470 memmove(result + 3 * dotdots, f + dir_len, taillen + 1);
1471 }
1472 return result;
1473}
1474
1475/* Return true if A and B must have the same parent dir if A and B exist.
1476 Return false if this is not necessarily true (though it might be true).
1477 Keep it simple, and do not inspect the file system. */
1478static bool
1478ATTRIBUTE_PURE_114833 static bool
1479same_parent_dirs(char const *a, char const *b)
1480{
1481 for (; *a == *b; a++, b++)
1482 if (!*a)
1483 return true;
1484 return ! (strchr(a, '/') || strchr(b, '/'));
1485}
1486

--- 1542 unchanged lines hidden (view full) ---

3029 return a->r_hiyear < b->r_hiyear ? -1 : 1;
3030 if (a->r_hiyear == ZIC_MAX)
3031 return 0;
3032 if (a->r_month - b->r_month != 0)
3033 return a->r_month - b->r_month;
3034 return a->r_dayofmonth - b->r_dayofmonth;
3035}
3036
1479same_parent_dirs(char const *a, char const *b)
1480{
1481 for (; *a == *b; a++, b++)
1482 if (!*a)
1483 return true;
1484 return ! (strchr(a, '/') || strchr(b, '/'));
1485}
1486

--- 1542 unchanged lines hidden (view full) ---

3029 return a->r_hiyear < b->r_hiyear ? -1 : 1;
3030 if (a->r_hiyear == ZIC_MAX)
3031 return 0;
3032 if (a->r_month - b->r_month != 0)
3033 return a->r_month - b->r_month;
3034 return a->r_dayofmonth - b->r_dayofmonth;
3035}
3036
3037/* Store into RESULT a POSIX.1-2017 TZ string that represent the future
3037/* Store into RESULT a proleptic TZ string that represent the future
3038 predictions for the zone ZPFIRST with ZONECOUNT entries. Return a
3039 compatibility indicator (a TZDB release year) if successful, a
3038 predictions for the zone ZPFIRST with ZONECOUNT entries. Return a
3039 compatibility indicator (a TZDB release year) if successful, a
3040 negative integer if no such TZ string exissts. */
3040 negative integer if no such TZ string exists. */
3041static int
3042stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
3043{
3044 register const struct zone * zp;
3045 register struct rule * rp;
3046 register struct rule * stdrp;
3047 register struct rule * dstrp;
3048 register ptrdiff_t i;

--- 175 unchanged lines hidden (view full) ---

3224 ** Generate lots of data if a rule can't cover all future times.
3225 */
3226 compat = stringzone(envvar, zpfirst, zonecount);
3227 version = compat < 2013 ? '2' : '3';
3228 do_extend = compat < 0;
3229 if (noise) {
3230 if (!*envvar)
3231 warning("%s %s",
3041static int
3042stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
3043{
3044 register const struct zone * zp;
3045 register struct rule * rp;
3046 register struct rule * stdrp;
3047 register struct rule * dstrp;
3048 register ptrdiff_t i;

--- 175 unchanged lines hidden (view full) ---

3224 ** Generate lots of data if a rule can't cover all future times.
3225 */
3226 compat = stringzone(envvar, zpfirst, zonecount);
3227 version = compat < 2013 ? '2' : '3';
3228 do_extend = compat < 0;
3229 if (noise) {
3230 if (!*envvar)
3231 warning("%s %s",
3232 _("no POSIX.1-2017 environment variable"
3233 " for zone"),
3232 _("no proleptic TZ string for zone"),
3234 zpfirst->z_name);
3235 else if (compat != 0) {
3236 /* Circa-COMPAT clients, and earlier clients, might
3237 not work for this zone when given dates before
3238 1970 or after 2038. */
3239 warning(_("%s: pre-%d clients may mishandle"
3240 " distant timestamps"),
3241 zpfirst->z_name, compat);

--- 247 unchanged lines hidden (view full) ---

3489 attypes[j].type = attypes[i].type;
3490 j++;
3491 }
3492 timecnt = j;
3493 }
3494 if (do_extend) {
3495 /*
3496 ** If we're extending the explicitly listed observations for
3233 zpfirst->z_name);
3234 else if (compat != 0) {
3235 /* Circa-COMPAT clients, and earlier clients, might
3236 not work for this zone when given dates before
3237 1970 or after 2038. */
3238 warning(_("%s: pre-%d clients may mishandle"
3239 " distant timestamps"),
3240 zpfirst->z_name, compat);

--- 247 unchanged lines hidden (view full) ---

3488 attypes[j].type = attypes[i].type;
3489 j++;
3490 }
3491 timecnt = j;
3492 }
3493 if (do_extend) {
3494 /*
3495 ** If we're extending the explicitly listed observations for
3497 ** 400 years because we can't fill the POSIX.1-2017 TZ field,
3496 ** 400 years because we can't fill the proleptic TZ field,
3498 ** check whether we actually ended up explicitly listing
3499 ** observations through that period. If there aren't any
3500 ** near the end of the 400-year period, add a redundant
3501 ** one at the end of the final year, to make it clear
3502 ** that we are claiming to have definite knowledge of
3503 ** the lack of transitions up to that point.
3504 */
3505 struct rule xr;

--- 168 unchanged lines hidden (view full) ---

3674 case 'P': return 'p'; case 'Q': return 'q'; case 'R': return 'r';
3675 case 'S': return 's'; case 'T': return 't'; case 'U': return 'u';
3676 case 'V': return 'v'; case 'W': return 'w'; case 'X': return 'x';
3677 case 'Y': return 'y'; case 'Z': return 'z';
3678 }
3679}
3680
3681/* case-insensitive equality */
3497 ** check whether we actually ended up explicitly listing
3498 ** observations through that period. If there aren't any
3499 ** near the end of the 400-year period, add a redundant
3500 ** one at the end of the final year, to make it clear
3501 ** that we are claiming to have definite knowledge of
3502 ** the lack of transitions up to that point.
3503 */
3504 struct rule xr;

--- 168 unchanged lines hidden (view full) ---

3673 case 'P': return 'p'; case 'Q': return 'q'; case 'R': return 'r';
3674 case 'S': return 's'; case 'T': return 't'; case 'U': return 'u';
3675 case 'V': return 'v'; case 'W': return 'w'; case 'X': return 'x';
3676 case 'Y': return 'y'; case 'Z': return 'z';
3677 }
3678}
3679
3680/* case-insensitive equality */
3682ATTRIBUTE_REPRODUCIBLE static bool
3681ATTRIBUTE_PURE_114833 static bool
3683ciequal(register const char *ap, register const char *bp)
3684{
3685 while (lowerit(*ap) == lowerit(*bp++))
3686 if (*ap++ == '\0')
3687 return true;
3688 return false;
3689}
3690
3682ciequal(register const char *ap, register const char *bp)
3683{
3684 while (lowerit(*ap) == lowerit(*bp++))
3685 if (*ap++ == '\0')
3686 return true;
3687 return false;
3688}
3689
3691ATTRIBUTE_REPRODUCIBLE static bool
3690ATTRIBUTE_PURE_114833 static bool
3692itsabbr(register const char *abbr, register const char *word)
3693{
3694 if (lowerit(*abbr) != lowerit(*word))
3695 return false;
3696 ++word;
3697 while (*++abbr != '\0')
3698 do {
3699 if (*word == '\0')
3700 return false;
3701 } while (lowerit(*word++) != lowerit(*abbr));
3702 return true;
3703}
3704
3705/* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
3706
3691itsabbr(register const char *abbr, register const char *word)
3692{
3693 if (lowerit(*abbr) != lowerit(*word))
3694 return false;
3695 ++word;
3696 while (*++abbr != '\0')
3697 do {
3698 if (*word == '\0')
3699 return false;
3700 } while (lowerit(*word++) != lowerit(*abbr));
3701 return true;
3702}
3703
3704/* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
3705
3707ATTRIBUTE_REPRODUCIBLE static bool
3706ATTRIBUTE_PURE_114833 static bool
3708ciprefix(char const *abbr, char const *word)
3709{
3710 do
3711 if (!*abbr)
3712 return true;
3713 while (lowerit(*abbr++) == lowerit(*word++));
3714
3715 return false;

--- 93 unchanged lines hidden (view full) ---

3809
3810ATTRIBUTE_NORETURN static void
3811time_overflow(void)
3812{
3813 error(_("time overflow"));
3814 exit(EXIT_FAILURE);
3815}
3816
3707ciprefix(char const *abbr, char const *word)
3708{
3709 do
3710 if (!*abbr)
3711 return true;
3712 while (lowerit(*abbr++) == lowerit(*word++));
3713
3714 return false;

--- 93 unchanged lines hidden (view full) ---

3808
3809ATTRIBUTE_NORETURN static void
3810time_overflow(void)
3811{
3812 error(_("time overflow"));
3813 exit(EXIT_FAILURE);
3814}
3815
3817ATTRIBUTE_REPRODUCIBLE static zic_t
3816ATTRIBUTE_PURE_114833 static zic_t
3818oadd(zic_t t1, zic_t t2)
3819{
3820#ifdef ckd_add
3821 zic_t sum;
3822 if (!ckd_add(&sum, t1, t2))
3823 return sum;
3824#else
3825 if (t1 < 0 ? ZIC_MIN - t1 <= t2 : t2 <= ZIC_MAX - t1)
3826 return t1 + t2;
3827#endif
3828 time_overflow();
3829}
3830
3817oadd(zic_t t1, zic_t t2)
3818{
3819#ifdef ckd_add
3820 zic_t sum;
3821 if (!ckd_add(&sum, t1, t2))
3822 return sum;
3823#else
3824 if (t1 < 0 ? ZIC_MIN - t1 <= t2 : t2 <= ZIC_MAX - t1)
3825 return t1 + t2;
3826#endif
3827 time_overflow();
3828}
3829
3831ATTRIBUTE_REPRODUCIBLE static zic_t
3830ATTRIBUTE_PURE_114833 static zic_t
3832tadd(zic_t t1, zic_t t2)
3833{
3834#ifdef ckd_add
3835 zic_t sum;
3836 if (!ckd_add(&sum, t1, t2) && min_time <= sum && sum <= max_time)
3837 return sum;
3838#else
3839 if (t1 < 0 ? min_time - t1 <= t2 : t2 <= max_time - t1)

--- 242 unchanged lines hidden ---
3831tadd(zic_t t1, zic_t t2)
3832{
3833#ifdef ckd_add
3834 zic_t sum;
3835 if (!ckd_add(&sum, t1, t2) && min_time <= sum && sum <= max_time)
3836 return sum;
3837#else
3838 if (t1 < 0 ? min_time - t1 <= t2 : t2 <= max_time - t1)

--- 242 unchanged lines hidden ---