time.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2022 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. #define ossl_seconds2time(s) ossl_ticks2time((s) * OSSL_TIME_SECOND)
  32. #define ossl_time2seconds(t) (ossl_time2ticks(t) / OSSL_TIME_SECOND)
  33. #define ossl_ms2time(ms) ossl_ticks2time((ms) * OSSL_TIME_MS)
  34. #define ossl_time2ms(t) (ossl_time2ticks(t) / OSSL_TIME_MS)
  35. #define ossl_us2time(us) ossl_ticks2time((us) * OSSL_TIME_US)
  36. #define ossl_time2us(t) (ossl_time2ticks(t) / OSSL_TIME_US)
  37. /* Convert a tick count into a time */
  38. static ossl_unused ossl_inline
  39. OSSL_TIME ossl_ticks2time(uint64_t ticks)
  40. {
  41. OSSL_TIME r;
  42. r.t = ticks;
  43. return r;
  44. }
  45. /* Convert a time to a tick count */
  46. static ossl_unused ossl_inline
  47. uint64_t ossl_time2ticks(OSSL_TIME t)
  48. {
  49. return t.t;
  50. }
  51. /* Get current time */
  52. OSSL_TIME ossl_time_now(void);
  53. /* The beginning and end of the time range */
  54. static ossl_unused ossl_inline
  55. OSSL_TIME ossl_time_zero(void)
  56. {
  57. return ossl_ticks2time(0);
  58. }
  59. static ossl_unused ossl_inline
  60. OSSL_TIME ossl_time_infinite(void)
  61. {
  62. return ossl_ticks2time(~(uint64_t)0);
  63. }
  64. /* Convert time to timeval */
  65. static ossl_unused ossl_inline
  66. struct timeval ossl_time_to_timeval(OSSL_TIME t)
  67. {
  68. struct timeval tv;
  69. #ifdef _WIN32
  70. tv.tv_sec = (long int)(t.t / OSSL_TIME_SECOND);
  71. #else
  72. tv.tv_sec = (time_t)(t.t / OSSL_TIME_SECOND);
  73. #endif
  74. tv.tv_usec = (t.t % OSSL_TIME_SECOND) / OSSL_TIME_US;
  75. return tv;
  76. }
  77. /* Convert timeval to time */
  78. static ossl_unused ossl_inline
  79. OSSL_TIME ossl_time_from_timeval(struct timeval tv)
  80. {
  81. OSSL_TIME t;
  82. #ifndef __DJGPP__ /* tv_sec is unsigned on djgpp. */
  83. if (tv.tv_sec < 0)
  84. return ossl_time_zero();
  85. #endif
  86. t.t = tv.tv_sec * OSSL_TIME_SECOND + tv.tv_usec * OSSL_TIME_US;
  87. return t;
  88. }
  89. /* Convert OSSL_TIME to time_t */
  90. static ossl_unused ossl_inline
  91. time_t ossl_time_to_time_t(OSSL_TIME t)
  92. {
  93. return (time_t)(t.t / OSSL_TIME_SECOND);
  94. }
  95. /* Convert time_t to OSSL_TIME */
  96. static ossl_unused ossl_inline
  97. OSSL_TIME ossl_time_from_time_t(time_t t)
  98. {
  99. OSSL_TIME ot;
  100. ot.t = t;
  101. ot.t *= OSSL_TIME_SECOND;
  102. return ot;
  103. }
  104. /* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
  105. static ossl_unused ossl_inline
  106. int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
  107. {
  108. if (a.t > b.t)
  109. return 1;
  110. if (a.t < b.t)
  111. return -1;
  112. return 0;
  113. }
  114. /* Returns true if an OSSL_TIME is ossl_time_zero(). */
  115. static ossl_unused ossl_inline
  116. int ossl_time_is_zero(OSSL_TIME t)
  117. {
  118. return ossl_time_compare(t, ossl_time_zero()) == 0;
  119. }
  120. /* Returns true if an OSSL_TIME is ossl_time_infinite(). */
  121. static ossl_unused ossl_inline
  122. int ossl_time_is_infinite(OSSL_TIME t)
  123. {
  124. return ossl_time_compare(t, ossl_time_infinite()) == 0;
  125. }
  126. /*
  127. * Arithmetic operations on times.
  128. * These operations are saturating, in that an overflow or underflow returns
  129. * the largest or smallest value respectively.
  130. */
  131. OSSL_SAFE_MATH_UNSIGNED(time, uint64_t)
  132. static ossl_unused ossl_inline
  133. OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
  134. {
  135. OSSL_TIME r;
  136. int err = 0;
  137. r.t = safe_add_time(a.t, b.t, &err);
  138. return err ? ossl_time_infinite() : r;
  139. }
  140. static ossl_unused ossl_inline
  141. OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
  142. {
  143. OSSL_TIME r;
  144. int err = 0;
  145. r.t = safe_sub_time(a.t, b.t, &err);
  146. return err ? ossl_time_zero() : r;
  147. }
  148. /* Returns |a - b|. */
  149. static ossl_unused ossl_inline
  150. OSSL_TIME ossl_time_abs_difference(OSSL_TIME a, OSSL_TIME b)
  151. {
  152. return a.t > b.t ? ossl_time_subtract(a, b)
  153. : ossl_time_subtract(b, a);
  154. }
  155. static ossl_unused ossl_inline
  156. OSSL_TIME ossl_time_multiply(OSSL_TIME a, uint64_t b)
  157. {
  158. OSSL_TIME r;
  159. int err = 0;
  160. r.t = safe_mul_time(a.t, b, &err);
  161. return err ? ossl_time_infinite() : r;
  162. }
  163. static ossl_unused ossl_inline
  164. OSSL_TIME ossl_time_divide(OSSL_TIME a, uint64_t b)
  165. {
  166. OSSL_TIME r;
  167. int err = 0;
  168. r.t = safe_div_time(a.t, b, &err);
  169. return err ? ossl_time_zero() : r;
  170. }
  171. static ossl_unused ossl_inline
  172. OSSL_TIME ossl_time_muldiv(OSSL_TIME a, uint64_t b, uint64_t c)
  173. {
  174. OSSL_TIME r;
  175. int err = 0;
  176. r.t = safe_muldiv_time(a.t, b, c, &err);
  177. return err ? ossl_time_zero() : r;
  178. }
  179. /* Return higher of the two given time values. */
  180. static ossl_unused ossl_inline
  181. OSSL_TIME ossl_time_max(OSSL_TIME a, OSSL_TIME b)
  182. {
  183. return a.t > b.t ? a : b;
  184. }
  185. /* Return the lower of the two given time values. */
  186. static ossl_unused ossl_inline
  187. OSSL_TIME ossl_time_min(OSSL_TIME a, OSSL_TIME b)
  188. {
  189. return a.t < b.t ? a : b;
  190. }
  191. #endif