Handle the somewhat common fixed case of time value 0 more efficiently
authorKen Raeburn <raeburn@mit.edu>
Sat, 24 Apr 2004 22:38:30 +0000 (22:38 +0000)
committerKen Raeburn <raeburn@mit.edu>
Sat, 24 Apr 2004 22:38:30 +0000 (22:38 +0000)
* 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
src/lib/krb5/asn.1/asn1_decode.c
src/lib/krb5/asn.1/asn1_encode.c

index 520999d7fc4cc613d09b507cf0423256a4a57053..b8b4d1025e1a913f2616dfd12b243240dca70e35 100644 (file)
@@ -1,3 +1,11 @@
+2004-04-24  Ken Raeburn  <raeburn@mit.edu>
+
+       * 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  <tlyu@mit.edu>
 
        * asn1_k_encode.c (asn1_encode_krb_saved_safe_body): New function;
index 65863202aab129da693849ae44ca0b4f91a7b64e..60ae08802d2d4869f06c49ffb4eba5678886d97e 100644 (file)
@@ -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();
 }
index 6bd40dfe7beadcd4de7dbc52a48c20479d6394fc..df3c4ec713fbbe8f03b2ef101cdf17e52e0f770e 100644 (file)
@@ -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;