]>
git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/time.h
2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_INTERNAL_TIME_H
11 # define OSSL_INTERNAL_TIME_H
14 # include <openssl/e_os2.h> /* uint64_t */
15 # include "internal/e_os.h" /* for struct timeval */
16 # include "internal/safe_math.h"
19 * Internal type defining a time.
20 * This should be treated as an opaque structure.
22 * The time datum is Unix's 1970 and at nanosecond precision, this gives
23 * a range of 584 years roughly.
26 uint64_t t
; /* Ticks since the epoch */
29 /* The precision of times allows this many values per second */
30 # define OSSL_TIME_SECOND ((uint64_t)1000000000)
32 /* One millisecond. */
33 # define OSSL_TIME_MS (OSSL_TIME_SECOND / 1000)
35 /* One microsecond. */
36 # define OSSL_TIME_US (OSSL_TIME_MS / 1000)
39 # define OSSL_TIME_NS (OSSL_TIME_US / 1000)
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)
48 /* Convert a tick count into a time */
49 static ossl_unused ossl_inline
50 OSSL_TIME
ossl_ticks2time(uint64_t ticks
)
58 /* Convert a time to a tick count */
59 static ossl_unused ossl_inline
60 uint64_t ossl_time2ticks(OSSL_TIME t
)
65 /* Get current time */
66 OSSL_TIME
ossl_time_now(void);
68 /* The beginning and end of the time range */
69 static ossl_unused ossl_inline
70 OSSL_TIME
ossl_time_zero(void)
72 return ossl_ticks2time(0);
75 static ossl_unused ossl_inline
76 OSSL_TIME
ossl_time_infinite(void)
78 return ossl_ticks2time(~(uint64_t)0);
82 /* Convert time to timeval */
83 static ossl_unused ossl_inline
84 struct timeval
ossl_time_to_timeval(OSSL_TIME t
)
89 tv
.tv_sec
= (long int)(t
.t
/ OSSL_TIME_SECOND
);
91 tv
.tv_sec
= (time_t)(t
.t
/ OSSL_TIME_SECOND
);
93 tv
.tv_usec
= (t
.t
% OSSL_TIME_SECOND
) / OSSL_TIME_US
;
97 /* Convert timeval to time */
98 static ossl_unused ossl_inline
99 OSSL_TIME
ossl_time_from_timeval(struct timeval tv
)
103 #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
105 return ossl_time_zero();
107 t
.t
= tv
.tv_sec
* OSSL_TIME_SECOND
+ tv
.tv_usec
* OSSL_TIME_US
;
111 /* Convert OSSL_TIME to time_t */
112 static ossl_unused ossl_inline
113 time_t ossl_time_to_time_t(OSSL_TIME t
)
115 return (time_t)(t
.t
/ OSSL_TIME_SECOND
);
118 /* Convert time_t to OSSL_TIME */
119 static ossl_unused ossl_inline
120 OSSL_TIME
ossl_time_from_time_t(time_t t
)
125 ot
.t
*= OSSL_TIME_SECOND
;
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
)
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
)
144 return ossl_time_compare(t
, ossl_time_zero()) == 0;
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
)
151 return ossl_time_compare(t
, ossl_time_infinite()) == 0;
155 * Arithmetic operations on times.
156 * These operations are saturating, in that an overflow or underflow returns
157 * the largest or smallest value respectively.
159 OSSL_SAFE_MATH_UNSIGNED(time
, uint64_t)
161 static ossl_unused ossl_inline
162 OSSL_TIME
ossl_time_add(OSSL_TIME a
, OSSL_TIME b
)
167 r
.t
= safe_add_time(a
.t
, b
.t
, &err
);
168 return err
? ossl_time_infinite() : r
;
171 static ossl_unused ossl_inline
172 OSSL_TIME
ossl_time_subtract(OSSL_TIME a
, OSSL_TIME b
)
177 r
.t
= safe_sub_time(a
.t
, b
.t
, &err
);
178 return err
? ossl_time_zero() : r
;
181 /* Returns |a - b|. */
182 static ossl_unused ossl_inline
183 OSSL_TIME
ossl_time_abs_difference(OSSL_TIME a
, OSSL_TIME b
)
185 return a
.t
> b
.t
? ossl_time_subtract(a
, b
)
186 : ossl_time_subtract(b
, a
);
189 static ossl_unused ossl_inline
190 OSSL_TIME
ossl_time_multiply(OSSL_TIME a
, uint64_t b
)
195 r
.t
= safe_mul_time(a
.t
, b
, &err
);
196 return err
? ossl_time_infinite() : r
;
199 static ossl_unused ossl_inline
200 OSSL_TIME
ossl_time_divide(OSSL_TIME a
, uint64_t b
)
205 r
.t
= safe_div_time(a
.t
, b
, &err
);
206 return err
? ossl_time_zero() : r
;
209 static ossl_unused ossl_inline
210 OSSL_TIME
ossl_time_muldiv(OSSL_TIME a
, uint64_t b
, uint64_t c
)
215 r
.t
= safe_muldiv_time(a
.t
, b
, c
, &err
);
216 return err
? ossl_time_zero() : r
;
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
)
223 return a
.t
> b
.t
? a
: b
;
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
)
230 return a
.t
< b
.t
? a
: b
;