Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/o_time.c
Line
Count
Source
1
/*
2
 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/e_os2.h>
11
#include <string.h>
12
#include <openssl/crypto.h>
13
14
struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
15
136k
{
16
136k
    struct tm *ts = NULL;
17
18
#if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
19
    {
20
        /*
21
         * On VMS, gmtime_r() takes a 32-bit pointer as second argument.
22
         * Since we can't know that |result| is in a space that can easily
23
         * translate to a 32-bit pointer, we must store temporarily on stack
24
         * and copy the result.  The stack is always reachable with 32-bit
25
         * pointers.
26
         */
27
#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE
28
#pragma pointer_size save
29
#pragma pointer_size 32
30
#endif
31
        struct tm data, *ts2 = &data;
32
#if defined OPENSSL_SYS_VMS && __INITIAL_POINTER_SIZE
33
#pragma pointer_size restore
34
#endif
35
        if (gmtime_r(timer, ts2) == NULL)
36
            return NULL;
37
        memcpy(result, ts2, sizeof(struct tm));
38
        ts = result;
39
    }
40
#elif defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_MACOSX)
41
136k
    if (gmtime_r(timer, result) == NULL)
42
0
        return NULL;
43
136k
    ts = result;
44
#elif defined(OPENSSL_SYS_WINDOWS) && defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_WIN32_WCE)
45
    if (gmtime_s(result, timer))
46
        return NULL;
47
    ts = result;
48
#else
49
    ts = gmtime(timer);
50
    if (ts == NULL)
51
        return NULL;
52
53
    memcpy(result, ts, sizeof(struct tm));
54
    ts = result;
55
#endif
56
136k
    return ts;
57
136k
}
58
59
/*
60
 * Take a tm structure and add an offset to it. This avoids any OS issues
61
 * with restricted date types and overflows which cause the year 2038
62
 * problem.
63
 */
64
65
394k
#define SECS_PER_DAY (24 * 60 * 60)
66
67
static long date_to_julian(int y, int m, int d);
68
static void julian_to_date(long jd, int *y, int *m, int *d);
69
static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
70
    long *pday, int *psec);
71
72
int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
73
86.2k
{
74
86.2k
    int time_sec, time_year, time_month, time_day;
75
86.2k
    long time_jd;
76
77
    /* Convert time and offset into Julian day and seconds */
78
86.2k
    if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
79
15.1k
        return 0;
80
81
    /* Convert Julian day back to date */
82
83
71.1k
    julian_to_date(time_jd, &time_year, &time_month, &time_day);
84
85
71.1k
    if (time_year < 1900 || time_year > 9999)
86
19.9k
        return 0;
87
88
    /* Update tm structure */
89
90
51.1k
    tm->tm_year = time_year - 1900;
91
51.1k
    tm->tm_mon = time_month - 1;
92
51.1k
    tm->tm_mday = time_day;
93
94
51.1k
    tm->tm_hour = time_sec / 3600;
95
51.1k
    tm->tm_min = (time_sec / 60) % 60;
96
51.1k
    tm->tm_sec = time_sec % 60;
97
98
51.1k
    return 1;
99
71.1k
}
100
101
int OPENSSL_gmtime_diff(int *pday, int *psec,
102
    const struct tm *from, const struct tm *to)
103
17.5k
{
104
17.5k
    int from_sec, to_sec, diff_sec;
105
17.5k
    long from_jd, to_jd, diff_day;
106
17.5k
    if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
107
0
        return 0;
108
17.5k
    if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
109
0
        return 0;
110
17.5k
    diff_day = to_jd - from_jd;
111
17.5k
    diff_sec = to_sec - from_sec;
112
    /* Adjust differences so both positive or both negative */
113
17.5k
    if (diff_day > 0 && diff_sec < 0) {
114
12.8k
        diff_day--;
115
12.8k
        diff_sec += SECS_PER_DAY;
116
12.8k
    }
117
17.5k
    if (diff_day < 0 && diff_sec > 0) {
118
240
        diff_day++;
119
240
        diff_sec -= SECS_PER_DAY;
120
240
    }
121
122
17.5k
    if (pday)
123
17.5k
        *pday = (int)diff_day;
124
17.5k
    if (psec)
125
17.5k
        *psec = diff_sec;
126
127
17.5k
    return 1;
128
17.5k
}
129
130
/* Convert tm structure and offset into julian day and seconds */
131
static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
132
    long *pday, int *psec)
133
121k
{
134
121k
    int offset_hms;
135
121k
    long offset_day, time_jd;
136
121k
    int time_year, time_month, time_day;
137
    /* split offset into days and day seconds */
138
121k
    offset_day = offset_sec / SECS_PER_DAY;
139
    /* Avoid sign issues with % operator */
140
121k
    offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
141
121k
    offset_day += off_day;
142
    /* Add current time seconds to offset */
143
121k
    offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
144
    /* Adjust day seconds if overflow */
145
121k
    if (offset_hms >= SECS_PER_DAY) {
146
8.37k
        offset_day++;
147
8.37k
        offset_hms -= SECS_PER_DAY;
148
112k
    } else if (offset_hms < 0) {
149
8.55k
        offset_day--;
150
8.55k
        offset_hms += SECS_PER_DAY;
151
8.55k
    }
152
153
    /*
154
     * Convert date of time structure into a Julian day number.
155
     */
156
157
121k
    time_year = tm->tm_year + 1900;
158
121k
    time_month = tm->tm_mon + 1;
159
121k
    time_day = tm->tm_mday;
160
161
121k
    time_jd = date_to_julian(time_year, time_month, time_day);
162
163
    /* Work out Julian day of new date */
164
121k
    time_jd += offset_day;
165
166
121k
    if (time_jd < 0)
167
15.1k
        return 0;
168
169
106k
    *pday = time_jd;
170
106k
    *psec = offset_hms;
171
106k
    return 1;
172
121k
}
173
174
/*
175
 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
176
 */
177
static long date_to_julian(int y, int m, int d)
178
121k
{
179
121k
    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;
180
121k
}
181
182
static void julian_to_date(long jd, int *y, int *m, int *d)
183
71.1k
{
184
71.1k
    long L = jd + 68569;
185
71.1k
    long n = (4 * L) / 146097;
186
71.1k
    long i, j;
187
188
71.1k
    L = L - (146097 * n + 3) / 4;
189
71.1k
    i = (4000 * (L + 1)) / 1461001;
190
71.1k
    L = L - (1461 * i) / 4 + 31;
191
71.1k
    j = (80 * L) / 2447;
192
71.1k
    *d = L - (2447 * j) / 80;
193
71.1k
    L = j / 11;
194
71.1k
    *m = j + 2 - (12 * L);
195
71.1k
    *y = 100 * (n - 49) + i + L;
196
71.1k
}