From 17c8862153e4602dabdc21d622e2301477e79605 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Sat, 24 Apr 2004 22:38:30 +0000 Subject: [PATCH] Handle the somewhat common fixed case of time value 0 more efficiently * asn1_decode.c (asn1_decode_generaltime): If the input string is the magic UNIX time zero, bypass all the arithmetic and return 0. * asn1_encode.c (asn1_encode_generaltime): If the input time value is the UNIX epoch, use a hardcoded string instead of doing the math. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16272 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/asn.1/ChangeLog | 8 +++++++ src/lib/krb5/asn.1/asn1_decode.c | 5 ++++ src/lib/krb5/asn.1/asn1_encode.c | 39 +++++++++++++++++++------------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/lib/krb5/asn.1/ChangeLog b/src/lib/krb5/asn.1/ChangeLog index 520999d7f..b8b4d1025 100644 --- a/src/lib/krb5/asn.1/ChangeLog +++ b/src/lib/krb5/asn.1/ChangeLog @@ -1,3 +1,11 @@ +2004-04-24 Ken Raeburn + + * asn1_decode.c (asn1_decode_generaltime): If the input string is + the magic UNIX time zero, bypass all the arithmetic and return 0. + * asn1_encode.c (asn1_encode_generaltime): If the input time + value is the UNIX epoch, use a hardcoded string instead of doing + the math. + 2003-10-08 Tom Yu * asn1_k_encode.c (asn1_encode_krb_saved_safe_body): New function; diff --git a/src/lib/krb5/asn.1/asn1_decode.c b/src/lib/krb5/asn.1/asn1_decode.c index 65863202a..60ae08802 100644 --- a/src/lib/krb5/asn.1/asn1_decode.c +++ b/src/lib/krb5/asn.1/asn1_decode.c @@ -236,6 +236,10 @@ asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val) free(s); return ASN1_BAD_FORMAT; } + if(s[0] == '1' && !memcmp("19700101000000Z", s, 15)) { + t = 0; + goto done; + } #define c2i(c) ((c)-'0') ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3]) - 1900; @@ -250,6 +254,7 @@ asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val) if(t == -1) return ASN1_BAD_TIMEFORMAT; +done: *val = t; cleanup(); } diff --git a/src/lib/krb5/asn.1/asn1_encode.c b/src/lib/krb5/asn.1/asn1_encode.c index 6bd40dfe7..df3c4ec71 100644 --- a/src/lib/krb5/asn.1/asn1_encode.c +++ b/src/lib/krb5/asn.1/asn1_encode.c @@ -226,28 +226,35 @@ asn1_error_code asn1_encode_generaltime(asn1buf *buf, time_t val, { asn1_error_code retval; struct tm *gtime; - char s[16]; + char s[16], *sp; unsigned int length, sum=0; time_t gmt_time = val; - gtime = gmtime(&gmt_time); - /* * Time encoding: YYYYMMDDhhmmssZ - * - * Sanity check this just to be paranoid, as gmtime can return NULL, - * and some bogus implementations might overrun on the sprintf. */ - if (gtime == NULL || - gtime->tm_year > 8099 || gtime->tm_mon > 11 || - gtime->tm_mday > 31 || gtime->tm_hour > 23 || - gtime->tm_min > 59 || gtime->tm_sec > 59) - return ASN1_BAD_GMTIME; - sprintf(s, "%04d%02d%02d%02d%02d%02dZ", - 1900+gtime->tm_year, gtime->tm_mon+1, gtime->tm_mday, - gtime->tm_hour, gtime->tm_min, gtime->tm_sec); - - retval = asn1buf_insert_charstring(buf,15,s); + if (gmt_time == 0) { + sp = "19700101000000Z"; + } else { + + /* + * Sanity check this just to be paranoid, as gmtime can return NULL, + * and some bogus implementations might overrun on the sprintf. + */ + gtime = gmtime(&gmt_time); + + if (gtime == NULL || + gtime->tm_year > 8099 || gtime->tm_mon > 11 || + gtime->tm_mday > 31 || gtime->tm_hour > 23 || + gtime->tm_min > 59 || gtime->tm_sec > 59) + return ASN1_BAD_GMTIME; + sprintf(s, "%04d%02d%02d%02d%02d%02dZ", + 1900+gtime->tm_year, gtime->tm_mon+1, gtime->tm_mday, + gtime->tm_hour, gtime->tm_min, gtime->tm_sec); + sp = s; + } + + retval = asn1buf_insert_charstring(buf,15,sp); if(retval) return retval; sum = 15; -- 2.26.2