]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/time.h
Copyright year updates
[thirdparty/openssl.git] / include / internal / time.h
1 /*
2 * Copyright 2022-2023 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 #ifndef OSSL_INTERNAL_TIME_H
11 # define OSSL_INTERNAL_TIME_H
12 # pragma once
13
14 # include <openssl/e_os2.h> /* uint64_t */
15 # include "internal/e_os.h" /* for struct timeval */
16 # include "internal/safe_math.h"
17
18 /*
19 * Internal type defining a time.
20 * This should be treated as an opaque structure.
21 *
22 * The time datum is Unix's 1970 and at nanosecond precision, this gives
23 * a range of 584 years roughly.
24 */
25 typedef struct {
26 uint64_t t; /* Ticks since the epoch */
27 } OSSL_TIME;
28
29 /* The precision of times allows this many values per second */
30 # define OSSL_TIME_SECOND ((uint64_t)1000000000)
31
32 /* One millisecond. */
33 # define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
34
35 /* One microsecond. */
36 # define OSSL_TIME_US (OSSL_TIME_MS / 1000)
37
38 /* One nanosecond. */
39 # define OSSL_TIME_NS (OSSL_TIME_US / 1000)
40
41 #define ossl_seconds2time(s) ossl_ticks2time((s) * OSSL_TIME_SECOND)
42 #define ossl_time2seconds(t) (ossl_time2ticks(t) / OSSL_TIME_SECOND)
43 #define ossl_ms2time(ms) ossl_ticks2time((ms) * OSSL_TIME_MS)
44 #define ossl_time2ms(t) (ossl_time2ticks(t) / OSSL_TIME_MS)
45 #define ossl_us2time(us) ossl_ticks2time((us) * OSSL_TIME_US)
46 #define ossl_time2us(t) (ossl_time2ticks(t) / OSSL_TIME_US)
47
48 /* Convert a tick count into a time */
49 static ossl_unused ossl_inline
50 OSSL_TIME ossl_ticks2time(uint64_t ticks)
51 {
52 OSSL_TIME r;
53
54 r.t = ticks;
55 return r;
56 }
57
58 /* Convert a time to a tick count */
59 static ossl_unused ossl_inline
60 uint64_t ossl_time2ticks(OSSL_TIME t)
61 {
62 return t.t;
63 }
64
65 /* Get current time */
66 OSSL_TIME ossl_time_now(void);
67
68 /* The beginning and end of the time range */
69 static ossl_unused ossl_inline
70 OSSL_TIME ossl_time_zero(void)
71 {
72 return ossl_ticks2time(0);
73 }
74
75 static ossl_unused ossl_inline
76 OSSL_TIME ossl_time_infinite(void)
77 {
78 return ossl_ticks2time(~(uint64_t)0);
79 }
80
81
82 /* Convert time to timeval */
83 static ossl_unused ossl_inline
84 struct timeval ossl_time_to_timeval(OSSL_TIME t)
85 {
86 struct timeval tv;
87
88 #ifdef _WIN32
89 tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
90 #else
91 tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
92 #endif
93 tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
94 return tv;
95 }
96
97 /* Convert timeval to time */
98 static ossl_unused ossl_inline
99 OSSL_TIME ossl_time_from_timeval(struct timeval tv)
100 {
101 OSSL_TIME t;
102
103 #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
104 if (tv.tv_sec < 0)
105 return ossl_time_zero();
106 #endif
107 t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
108 return t;
109 }
110
111 /* Convert OSSL_TIME to time_t */
112 static ossl_unused ossl_inline
113 time_t ossl_time_to_time_t(OSSL_TIME t)
114 {
115 return (time_t)(t.t / OSSL_TIME_SECOND);
116 }
117
118 /* Convert time_t to OSSL_TIME */
119 static ossl_unused ossl_inline
120 OSSL_TIME ossl_time_from_time_t(time_t t)
121 {
122 OSSL_TIME ot;
123
124 ot.t = t;
125 ot.t *= OSSL_TIME_SECOND;
126 return ot;
127 }
128
129 /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
130 static ossl_unused ossl_inline
131 int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
132 {
133 if (a.t > b.t)
134 return 1;
135 if (a.t < b.t)
136 return -1;
137 return 0;
138 }
139
140 /* Returns true if an OSSL_TIME is ossl_time_zero(). */
141 static ossl_unused ossl_inline
142 int ossl_time_is_zero(OSSL_TIME t)
143 {
144 return ossl_time_compare(t, ossl_time_zero()) == 0;
145 }
146
147 /* Returns true if an OSSL_TIME is ossl_time_infinite(). */
148 static ossl_unused ossl_inline
149 int ossl_time_is_infinite(OSSL_TIME t)
150 {
151 return ossl_time_compare(t, ossl_time_infinite()) == 0;
152 }
153
154 /*
155 * Arithmetic operations on times.
156 * These operations are saturating, in that an overflow or underflow returns
157 * the largest or smallest value respectively.
158 */
159 OSSL_SAFE_MATH_UNSIGNED(time, uint64_t)
160
161 static ossl_unused ossl_inline
162 OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
163 {
164 OSSL_TIME r;
165 int err = 0;
166
167 r.t = safe_add_time(a.t, b.t, &err);
168 return err ? ossl_time_infinite() : r;
169 }
170
171 static ossl_unused ossl_inline
172 OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
173 {
174 OSSL_TIME r;
175 int err = 0;
176
177 r.t = safe_sub_time(a.t, b.t, &err);
178 return err ? ossl_time_zero() : r;
179 }
180
181 /* Returns |a - b|. */
182 static ossl_unused ossl_inline
183 OSSL_TIME ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
184 {
185 return a.t > b.t ? ossl_time_subtract(a, b)
186 : ossl_time_subtract(b, a);
187 }
188
189 static ossl_unused ossl_inline
190 OSSL_TIME ossl_time_multiply(OSSL_TIME a, uint64_t b)
191 {
192 OSSL_TIME r;
193 int err = 0;
194
195 r.t = safe_mul_time(a.t, b, &err);
196 return err ? ossl_time_infinite() : r;
197 }
198
199 static ossl_unused ossl_inline
200 OSSL_TIME ossl_time_divide(OSSL_TIME a, uint64_t b)
201 {
202 OSSL_TIME r;
203 int err = 0;
204
205 r.t = safe_div_time(a.t, b, &err);
206 return err ? ossl_time_zero() : r;
207 }
208
209 static ossl_unused ossl_inline
210 OSSL_TIME ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
211 {
212 OSSL_TIME r;
213 int err = 0;
214
215 r.t = safe_muldiv_time(a.t, b, c, &err);
216 return err ? ossl_time_zero() : r;
217 }
218
219 /* Return higher of the two given time values. */
220 static ossl_unused ossl_inline
221 OSSL_TIME ossl_time_max(OSSL_TIME a, OSSL_TIME b)
222 {
223 return a.t > b.t ? a : b;
224 }
225
226 /* Return the lower of the two given time values. */
227 static ossl_unused ossl_inline
228 OSSL_TIME ossl_time_min(OSSL_TIME a, OSSL_TIME b)
229 {
230 return a.t < b.t ? a : b;
231 }
232
233 #endif