time.h 5.8 KB

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