From 69ff87b7502fe2edb5ca04b3050b8bf9903e5332 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Fri, 14 Apr 2006 22:38:00 +0000 Subject: Move gmt_mktime.c from lib/krb5/os to util/support, and rename the function. Stop exporting gmt_mktime from libkrb5, and export krb5int_gmt_mktime from libkrb5support. Updated the one caller, asn1_decode.c. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@17921 dc483132-0cff-0310-8789-dd5450dbe970 --- src/include/k5-int.h | 2 +- src/lib/krb5/asn.1/asn1_decode.c | 4 +- src/lib/krb5/libkrb5.exports | 1 - src/lib/krb5/os/Makefile.in | 14 ---- src/lib/krb5/os/gmt_mktime.c | 127 -------------------------------- src/util/support/Makefile.in | 8 ++ src/util/support/gmt_mktime.c | 127 ++++++++++++++++++++++++++++++++ src/util/support/libkrb5support.exports | 1 + 8 files changed, 139 insertions(+), 145 deletions(-) delete mode 100644 src/lib/krb5/os/gmt_mktime.c create mode 100644 src/util/support/gmt_mktime.c (limited to 'src') diff --git a/src/include/k5-int.h b/src/include/k5-int.h index dcb13af5c..d80db9dc7 100644 --- a/src/include/k5-int.h +++ b/src/include/k5-int.h @@ -749,7 +749,7 @@ krb5_error_code krb5_crypto_us_timeofday (krb5_int32 *, krb5_int32 *); -time_t gmt_mktime (struct tm *); +time_t krb5int_gmt_mktime (struct tm *); #endif /* KRB5_OLD_CRYPTO */ diff --git a/src/lib/krb5/asn.1/asn1_decode.c b/src/lib/krb5/asn.1/asn1_decode.c index d31ce3e3b..aa4be3263 100644 --- a/src/lib/krb5/asn.1/asn1_decode.c +++ b/src/lib/krb5/asn.1/asn1_decode.c @@ -55,7 +55,7 @@ if(asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\ #define cleanup()\ return 0 -time_t gmt_mktime (struct tm *); +extern time_t krb5int_gmt_mktime (struct tm *); asn1_error_code asn1_decode_integer(asn1buf *buf, long int *val) { @@ -250,7 +250,7 @@ asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val) ts.tm_min = 10*c2i(s[10]) + c2i(s[11]); ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]); ts.tm_isdst = -1; - t = gmt_mktime(&ts); + t = krb5int_gmt_mktime(&ts); free(s); if(t == -1) return ASN1_BAD_TIMEFORMAT; diff --git a/src/lib/krb5/libkrb5.exports b/src/lib/krb5/libkrb5.exports index 8831bf781..afd7846f7 100644 --- a/src/lib/krb5/libkrb5.exports +++ b/src/lib/krb5/libkrb5.exports @@ -239,7 +239,6 @@ et_kdb5_error_table et_krb5_error_table et_kv5m_error_table et_prof_error_table -gmt_mktime initialize_asn1_error_table initialize_k524_error_table initialize_kdb5_error_table diff --git a/src/lib/krb5/os/Makefile.in b/src/lib/krb5/os/Makefile.in index 55ea53a02..eab550d55 100644 --- a/src/lib/krb5/os/Makefile.in +++ b/src/lib/krb5/os/Makefile.in @@ -28,7 +28,6 @@ STLIBOBJS= \ gen_port.o \ genaddrs.o \ gen_rname.o \ - gmt_mktime.o \ hostaddr.o \ hst_realm.o \ init_os_ctx.o \ @@ -74,7 +73,6 @@ OBJS= \ $(OUTPRE)gen_port.$(OBJEXT) \ $(OUTPRE)genaddrs.$(OBJEXT) \ $(OUTPRE)gen_rname.$(OBJEXT) \ - $(OUTPRE)gmt_mktime.$(OBJEXT) \ $(OUTPRE)hostaddr.$(OBJEXT) \ $(OUTPRE)hst_realm.$(OBJEXT) \ $(OUTPRE)init_os_ctx.$(OBJEXT) \ @@ -120,7 +118,6 @@ SRCS= \ $(srcdir)/gen_port.c \ $(srcdir)/genaddrs.c \ $(srcdir)/gen_rname.c \ - $(srcdir)/gmt_mktime.c \ $(srcdir)/hostaddr.c \ $(srcdir)/hst_realm.c \ $(srcdir)/init_os_ctx.c \ @@ -180,9 +177,6 @@ t_an_to_ln: $(T_AN_TO_LN_OBJS) $(KRB5_BASE_DEPLIBS) t_realm_iter: $(T_REALM_ITER_OBJS) $(KRB5_BASE_DEPLIBS) $(CC_LINK) -o t_realm_iter $(T_REALM_ITER_OBJS) $(KRB5_BASE_LIBS) -t_mktime: gmt_mktime.c - $(CC) $(ALL_CFLAGS) -DTEST_LEAP -o t_mktime $(srcdir)/gmt_mktime.c - t_localaddr: localaddr.c $(CC_LINK) $(ALL_CFLAGS) -DTEST -o t_localaddr $(srcdir)/localaddr.c $(KRB5_BASE_LIBS) $(LIBS) @@ -372,14 +366,6 @@ gen_rname.so gen_rname.po $(OUTPRE)gen_rname.$(OBJEXT): \ $(SRCTOP)/include/k5-plugin.h $(SRCTOP)/include/k5-thread.h \ $(SRCTOP)/include/port-sockets.h $(SRCTOP)/include/socket-utils.h \ gen_rname.c os-proto.h -gmt_mktime.so gmt_mktime.po $(OUTPRE)gmt_mktime.$(OBJEXT): \ - $(BUILDTOP)/include/autoconf.h $(BUILDTOP)/include/krb5.h \ - $(BUILDTOP)/include/osconf.h $(BUILDTOP)/include/profile.h \ - $(COM_ERR_DEPS) $(SRCTOP)/include/k5-err.h $(SRCTOP)/include/k5-int.h \ - $(SRCTOP)/include/k5-locate.h $(SRCTOP)/include/k5-platform.h \ - $(SRCTOP)/include/k5-plugin.h $(SRCTOP)/include/k5-thread.h \ - $(SRCTOP)/include/port-sockets.h $(SRCTOP)/include/socket-utils.h \ - gmt_mktime.c hostaddr.so hostaddr.po $(OUTPRE)hostaddr.$(OBJEXT): \ $(BUILDTOP)/include/autoconf.h $(BUILDTOP)/include/krb5.h \ $(BUILDTOP)/include/osconf.h $(BUILDTOP)/include/profile.h \ diff --git a/src/lib/krb5/os/gmt_mktime.c b/src/lib/krb5/os/gmt_mktime.c deleted file mode 100644 index 3d10e1a4e..000000000 --- a/src/lib/krb5/os/gmt_mktime.c +++ /dev/null @@ -1,127 +0,0 @@ -/* This code placed in the public domain by Mark W. Eichin */ - -#include -#include "k5-int.h" - -#ifdef HAVE_SYS_TYPES_H -#include -#endif -#ifdef HAVE_SYS_TIME_H -#include -#ifdef TIME_WITH_SYS_TIME -#include -#endif -#else -#include -#endif - -/* take a struct tm, return seconds from GMT epoch */ -/* like mktime, this ignores tm_wday and tm_yday. */ -/* unlike mktime, this does not set them... it only passes a return value. */ - -static const int days_in_month[12] = { -0, /* jan 31 */ -31, /* feb 28 */ -59, /* mar 31 */ -90, /* apr 30 */ -120, /* may 31 */ -151, /* jun 30 */ -181, /* jul 31 */ -212, /* aug 31 */ -243, /* sep 30 */ -273, /* oct 31 */ -304, /* nov 30 */ -334 /* dec 31 */ -}; - -#define hasleapday(year) (year%400?(year%100?(year%4?0:1):0):1) - -time_t gmt_mktime(struct tm *t) -{ - time_t accum; - -#define assert_time(cnd) if(!(cnd)) return (time_t) -1 - - /* - * For 32-bit signed time_t centered on 1/1/1970, the range is: - * time 0x80000000 -> Fri Dec 13 16:45:52 1901 - * time 0x7fffffff -> Mon Jan 18 22:14:07 2038 - * - * So years 1901 and 2038 are allowable, but we can't encode all - * dates in those years, and we're not doing overflow/underflow - * checking for such cases. - */ - assert_time(t->tm_year>=1); - assert_time(t->tm_year<=138); - - assert_time(t->tm_mon>=0); - assert_time(t->tm_mon<=11); - assert_time(t->tm_mday>=1); - assert_time(t->tm_mday<=31); - assert_time(t->tm_hour>=0); - assert_time(t->tm_hour<=23); - assert_time(t->tm_min>=0); - assert_time(t->tm_min<=59); - assert_time(t->tm_sec>=0); - assert_time(t->tm_sec<=62); - -#undef assert_time - - - accum = t->tm_year - 70; - accum *= 365; /* 365 days/normal year */ - - /* add in leap day for all previous years */ - if (t->tm_year >= 70) - accum += (t->tm_year - 69) / 4; - else - accum -= (72 - t->tm_year) / 4; - /* add in leap day for this year */ - if(t->tm_mon >= 2) /* march or later */ - if(hasleapday((t->tm_year + 1900))) accum += 1; - - accum += days_in_month[t->tm_mon]; - accum += t->tm_mday-1; /* days of month are the only 1-based field */ - accum *= 24; /* 24 hour/day */ - accum += t->tm_hour; - accum *= 60; /* 60 minute/hour */ - accum += t->tm_min; - accum *= 60; /* 60 seconds/minute */ - accum += t->tm_sec; - - return accum; -} - -#ifdef TEST_LEAP -int -main (int argc, char *argv[]) -{ - int yr; - time_t t; - struct tm tm = { - .tm_mon = 0, .tm_mday = 1, - .tm_hour = 0, .tm_min = 0, .tm_sec = 0, - }; - for (yr = 60; yr <= 104; yr++) - { - printf ("1/1/%d%c -> ", 1900 + yr, hasleapday((1900+yr)) ? '*' : ' '); - tm.tm_year = yr; - t = gmt_mktime (&tm); - if (t == (time_t) -1) - printf ("-1\n"); - else - { - long u; - if (t % (24 * 60 * 60)) - printf ("(not integral multiple of days) "); - u = t / (24 * 60 * 60); - printf ("%3ld*365%+ld\t0x%08lx\n", - (long) (u / 365), (long) (u % 365), - (long) t); - } - } - t = 0x80000000, printf ("time 0x%lx -> %s", t, ctime (&t)); - t = 0x7fffffff, printf ("time 0x%lx -> %s", t, ctime (&t)); - return 0; -} -#endif diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in index 5217cd72c..4b0ea81bb 100644 --- a/src/util/support/Makefile.in +++ b/src/util/support/Makefile.in @@ -30,6 +30,7 @@ STLIBOBJS= \ init-addrinfo.o \ plugins.o \ errors.o \ + gmt_mktime.o \ fake-addrinfo.o LIBOBJS= \ @@ -37,6 +38,7 @@ LIBOBJS= \ $(OUTPRE)init-addrinfo.$(OBJEXT) \ $(OUTPRE)plugins.$(OBJEXT) \ $(OUTPRE)errors.$(OBJEXT) \ + $(OUTPRE)gmt_mktime.$(OBJEXT) \ $(OUTPRE)fake-addrinfo.$(OBJEXT) STOBJLISTS=OBJS.ST @@ -49,6 +51,7 @@ SRCS=\ $(srcdir)/threads.c \ $(srcdir)/init-addrinfo.c \ $(srcdir)/errors.c \ + $(srcdir)/gmt_mktime.c \ $(srcdir)/fake-addrinfo.c SHLIB_EXPDEPS = # Add -lm if dumping thread stats, for sqrt. @@ -70,6 +73,9 @@ $(BUILDTOP)/include/autoconf.h: $(SRCTOP)/include/autoconf.h.in (cd $(BUILDTOP)/include; $(MAKE) autoconf.h) ##DOS##!endif +t_mktime: gmt_mktime.c + $(CC) $(ALL_CFLAGS) -DTEST_LEAP -o t_mktime $(srcdir)/gmt_mktime.c + # @lib_frag@ # @libobj_frag@ @@ -90,6 +96,8 @@ init-addrinfo.so init-addrinfo.po $(OUTPRE)init-addrinfo.$(OBJEXT): \ errors.so errors.po $(OUTPRE)errors.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \ $(SRCTOP)/include/k5-err.h $(SRCTOP)/include/k5-platform.h \ $(SRCTOP)/include/k5-thread.h errors.c +gmt_mktime.so gmt_mktime.po $(OUTPRE)gmt_mktime.$(OBJEXT): \ + $(BUILDTOP)/include/autoconf.h gmt_mktime.c fake-addrinfo.so fake-addrinfo.po $(OUTPRE)fake-addrinfo.$(OBJEXT): \ $(BUILDTOP)/include/autoconf.h $(SRCTOP)/include/fake-addrinfo.h \ $(SRCTOP)/include/k5-platform.h $(SRCTOP)/include/k5-thread.h \ diff --git a/src/util/support/gmt_mktime.c b/src/util/support/gmt_mktime.c new file mode 100644 index 000000000..65ab87349 --- /dev/null +++ b/src/util/support/gmt_mktime.c @@ -0,0 +1,127 @@ +/* This code placed in the public domain by Mark W. Eichin */ + +#include +#include "autoconf.h" + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_TIME_H +#include +#ifdef TIME_WITH_SYS_TIME +#include +#endif +#else +#include +#endif + +/* take a struct tm, return seconds from GMT epoch */ +/* like mktime, this ignores tm_wday and tm_yday. */ +/* unlike mktime, this does not set them... it only passes a return value. */ + +static const int days_in_month[12] = { +0, /* jan 31 */ +31, /* feb 28 */ +59, /* mar 31 */ +90, /* apr 30 */ +120, /* may 31 */ +151, /* jun 30 */ +181, /* jul 31 */ +212, /* aug 31 */ +243, /* sep 30 */ +273, /* oct 31 */ +304, /* nov 30 */ +334 /* dec 31 */ +}; + +#define hasleapday(year) (year%400?(year%100?(year%4?0:1):0):1) + +time_t krb5int_gmt_mktime(struct tm *t) +{ + time_t accum; + +#define assert_time(cnd) if(!(cnd)) return (time_t) -1 + + /* + * For 32-bit signed time_t centered on 1/1/1970, the range is: + * time 0x80000000 -> Fri Dec 13 16:45:52 1901 + * time 0x7fffffff -> Mon Jan 18 22:14:07 2038 + * + * So years 1901 and 2038 are allowable, but we can't encode all + * dates in those years, and we're not doing overflow/underflow + * checking for such cases. + */ + assert_time(t->tm_year>=1); + assert_time(t->tm_year<=138); + + assert_time(t->tm_mon>=0); + assert_time(t->tm_mon<=11); + assert_time(t->tm_mday>=1); + assert_time(t->tm_mday<=31); + assert_time(t->tm_hour>=0); + assert_time(t->tm_hour<=23); + assert_time(t->tm_min>=0); + assert_time(t->tm_min<=59); + assert_time(t->tm_sec>=0); + assert_time(t->tm_sec<=62); + +#undef assert_time + + + accum = t->tm_year - 70; + accum *= 365; /* 365 days/normal year */ + + /* add in leap day for all previous years */ + if (t->tm_year >= 70) + accum += (t->tm_year - 69) / 4; + else + accum -= (72 - t->tm_year) / 4; + /* add in leap day for this year */ + if(t->tm_mon >= 2) /* march or later */ + if(hasleapday((t->tm_year + 1900))) accum += 1; + + accum += days_in_month[t->tm_mon]; + accum += t->tm_mday-1; /* days of month are the only 1-based field */ + accum *= 24; /* 24 hour/day */ + accum += t->tm_hour; + accum *= 60; /* 60 minute/hour */ + accum += t->tm_min; + accum *= 60; /* 60 seconds/minute */ + accum += t->tm_sec; + + return accum; +} + +#ifdef TEST_LEAP +int +main (int argc, char *argv[]) +{ + int yr; + time_t t; + struct tm tm = { + .tm_mon = 0, .tm_mday = 1, + .tm_hour = 0, .tm_min = 0, .tm_sec = 0, + }; + for (yr = 60; yr <= 104; yr++) + { + printf ("1/1/%d%c -> ", 1900 + yr, hasleapday((1900+yr)) ? '*' : ' '); + tm.tm_year = yr; + t = gmt_mktime (&tm); + if (t == (time_t) -1) + printf ("-1\n"); + else + { + long u; + if (t % (24 * 60 * 60)) + printf ("(not integral multiple of days) "); + u = t / (24 * 60 * 60); + printf ("%3ld*365%+ld\t0x%08lx\n", + (long) (u / 365), (long) (u % 365), + (long) t); + } + } + t = 0x80000000, printf ("time 0x%lx -> %s", t, ctime (&t)); + t = 0x7fffffff, printf ("time 0x%lx -> %s", t, ctime (&t)); + return 0; +} +#endif diff --git a/src/util/support/libkrb5support.exports b/src/util/support/libkrb5support.exports index 00ce5c3e3..a7b6c1c98 100644 --- a/src/util/support/libkrb5support.exports +++ b/src/util/support/libkrb5support.exports @@ -31,3 +31,4 @@ krb5int_get_error krb5int_free_error krb5int_clear_error krb5int_set_error_info_callout_fn +krb5int_gmt_mktime -- cgit