[rs-commit] r35 - /redwax-tool/trunk/redwax_openssl.c

rs-commit at redwax.eu rs-commit at redwax.eu
Wed Nov 17 10:59:52 CET 2021


Author: minfrin at redwax.eu
Date: Wed Nov 17 10:59:52 2021
New Revision: 35

Log:
Provide OPENSSL_gmtime_adj workaround for openssl 1.0.x.

Modified:
    redwax-tool/trunk/redwax_openssl.c

Modified: redwax-tool/trunk/redwax_openssl.c
==============================================================================
--- redwax-tool/trunk/redwax_openssl.c	(original)
+++ redwax-tool/trunk/redwax_openssl.c	Wed Nov 17 10:59:52 2021
@@ -87,6 +87,108 @@
  * havoc.
  */
 #if !HAVE_ASN1_TIME_DIFF
+
+#define SECS_PER_DAY (24 * 60 * 60)
+
+/*
+ * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
+ */
+static long date_to_julian(int y, int m, int d)
+{
+    return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
+        (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
+        (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
+}
+
+static void julian_to_date(long jd, int *y, int *m, int *d)
+{
+    long L = jd + 68569;
+    long n = (4 * L) / 146097;
+    long i, j;
+
+    L = L - (146097 * n + 3) / 4;
+    i = (4000 * (L + 1)) / 1461001;
+    L = L - (1461 * i) / 4 + 31;
+    j = (80 * L) / 2447;
+    *d = L - (2447 * j) / 80;
+    L = j / 11;
+    *m = j + 2 - (12 * L);
+    *y = 100 * (n - 49) + i + L;
+}
+
+/* Convert tm structure and offset into julian day and seconds */
+static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
+                      long *pday, int *psec)
+{
+    int offset_hms, offset_day;
+    long time_jd;
+    int time_year, time_month, time_day;
+    /* split offset into days and day seconds */
+    offset_day = offset_sec / SECS_PER_DAY;
+    /* Avoid sign issues with % operator */
+    offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
+    offset_day += off_day;
+    /* Add current time seconds to offset */
+    offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
+    /* Adjust day seconds if overflow */
+    if (offset_hms >= SECS_PER_DAY) {
+        offset_day++;
+        offset_hms -= SECS_PER_DAY;
+    } else if (offset_hms < 0) {
+        offset_day--;
+        offset_hms += SECS_PER_DAY;
+    }
+
+    /*
+     * Convert date of time structure into a Julian day number.
+     */
+
+    time_year = tm->tm_year + 1900;
+    time_month = tm->tm_mon + 1;
+    time_day = tm->tm_mday;
+
+    time_jd = date_to_julian(time_year, time_month, time_day);
+
+    /* Work out Julian day of new date */
+    time_jd += offset_day;
+
+    if (time_jd < 0)
+        return 0;
+
+    *pday = time_jd;
+    *psec = offset_hms;
+    return 1;
+}
+
+int redwax_OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
+{
+    int time_sec, time_year, time_month, time_day;
+    long time_jd;
+
+    /* Convert time and offset into Julian day and seconds */
+    if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
+        return 0;
+
+    /* Convert Julian day back to date */
+
+    julian_to_date(time_jd, &time_year, &time_month, &time_day);
+
+    if (time_year < 1900 || time_year > 9999)
+        return 0;
+
+    /* Update tm structure */
+
+    tm->tm_year = time_year - 1900;
+    tm->tm_mon = time_month - 1;
+    tm->tm_mday = time_day;
+
+    tm->tm_hour = time_sec / 3600;
+    tm->tm_min = (time_sec / 60) % 60;
+    tm->tm_sec = time_sec % 60;
+
+    return 1;
+
+}
 
 struct tm *redwax_OPENSSL_gmtime(const time_t *timer, struct tm *result)
 {
@@ -389,60 +491,6 @@
     return asn1_time_to_tm(tm, s);
 }
 
-/*
- * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
- */
-static long date_to_julian(int y, int m, int d)
-{
-    return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
-        (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
-        (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
-}
-
-/* Convert tm structure and offset into julian day and seconds */
-static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
-                      long *pday, int *psec)
-{
-    int offset_hms, offset_day;
-    long time_jd;
-    int time_year, time_month, time_day;
-    /* split offset into days and day seconds */
-    offset_day = offset_sec / SECS_PER_DAY;
-    /* Avoid sign issues with % operator */
-    offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
-    offset_day += off_day;
-    /* Add current time seconds to offset */
-    offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
-    /* Adjust day seconds if overflow */
-    if (offset_hms >= SECS_PER_DAY) {
-        offset_day++;
-        offset_hms -= SECS_PER_DAY;
-    } else if (offset_hms < 0) {
-        offset_day--;
-        offset_hms += SECS_PER_DAY;
-    }
-
-    /*
-     * Convert date of time structure into a Julian day number.
-     */
-
-    time_year = tm->tm_year + 1900;
-    time_month = tm->tm_mon + 1;
-    time_day = tm->tm_mday;
-
-    time_jd = date_to_julian(time_year, time_month, time_day);
-
-    /* Work out Julian day of new date */
-    time_jd += offset_day;
-
-    if (time_jd < 0)
-        return 0;
-
-    *pday = time_jd;
-    *psec = offset_hms;
-    return 1;
-}
-
 int redwax_OPENSSL_gmtime_diff(int *pday, int *psec,
                         const struct tm *from, const struct tm *to)
 {



More information about the rs-commit mailing list