time.h 5.8 KB

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