Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 3 | #include <openssl/crypto.h> |
| 4 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 5 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 6 | |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 7 | #include <array> |
Ed Tanous | 74849be | 2021-02-05 09:47:47 -0800 | [diff] [blame] | 8 | #include <chrono> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 9 | #include <cstdint> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 10 | #include <ctime> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | #include <functional> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 12 | #include <limits> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 13 | #include <stdexcept> |
| 14 | #include <string> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 15 | #include <string_view> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 16 | #include <tuple> |
Ed Tanous | 9ea15c3 | 2022-01-04 14:18:22 -0800 | [diff] [blame] | 17 | #include <type_traits> |
| 18 | #include <utility> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | namespace crow |
| 21 | { |
| 22 | namespace black_magic |
| 23 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 25 | constexpr unsigned findClosingTag(std::string_view s, unsigned p) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | { |
| 27 | return s[p] == '>' ? p : findClosingTag(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 30 | constexpr bool isInt(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 32 | return s.substr(i, 5) == "<int>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 35 | constexpr bool isUint(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 37 | return s.substr(i, 6) == "<uint>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 40 | constexpr bool isFloat(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 42 | return s.substr(i, 7) == "<float>" || s.substr(i, 8) == "<double>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 45 | constexpr bool isStr(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 47 | return s.substr(i, 5) == "<str>" || s.substr(i, 8) == "<string>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 50 | constexpr bool isPath(std::string_view s, unsigned i) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | { |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 52 | return s.substr(i, 6) == "<path>"; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 53 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 54 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 55 | template <typename T> |
| 56 | constexpr int getParameterTag() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 58 | if constexpr (std::is_same_v<int, T>) |
| 59 | { |
| 60 | return 1; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | } |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 62 | if constexpr (std::is_same_v<char, T>) |
| 63 | { |
| 64 | return 1; |
| 65 | } |
| 66 | if constexpr (std::is_same_v<short, T>) |
| 67 | { |
| 68 | return 1; |
| 69 | } |
| 70 | if constexpr (std::is_same_v<long, T>) |
| 71 | { |
| 72 | return 1; |
| 73 | } |
| 74 | if constexpr (std::is_same_v<long long, T>) |
| 75 | { |
| 76 | return 1; |
| 77 | } |
| 78 | if constexpr (std::is_same_v<unsigned int, T>) |
| 79 | { |
| 80 | return 2; |
| 81 | } |
| 82 | if constexpr (std::is_same_v<unsigned char, T>) |
| 83 | { |
| 84 | return 2; |
| 85 | } |
| 86 | if constexpr (std::is_same_v<unsigned short, T>) |
| 87 | { |
| 88 | return 2; |
| 89 | } |
| 90 | if constexpr (std::is_same_v<unsigned long, T>) |
| 91 | { |
| 92 | return 2; |
| 93 | } |
| 94 | if constexpr (std::is_same_v<unsigned long long, T>) |
| 95 | { |
| 96 | return 2; |
| 97 | } |
| 98 | if constexpr (std::is_same_v<double, T>) |
| 99 | { |
| 100 | return 3; |
| 101 | } |
| 102 | if constexpr (std::is_same_v<std::string, T>) |
| 103 | { |
| 104 | return 4; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 109 | template <typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 110 | struct computeParameterTagFromArgsList; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 111 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 112 | template <> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 113 | struct computeParameterTagFromArgsList<> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 115 | static constexpr int value = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | template <typename Arg, typename... Args> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 119 | struct computeParameterTagFromArgsList<Arg, Args...> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 121 | static constexpr int subValue = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 122 | computeParameterTagFromArgsList<Args...>::value; |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 123 | static constexpr int value = |
| 124 | getParameterTag<typename std::decay<Arg>::type>() |
| 125 | ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | : subValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 129 | inline bool isParameterTagCompatible(uint64_t a, uint64_t b) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 131 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 132 | if (a == 0) |
| 133 | { |
| 134 | return b == 0; |
| 135 | } |
| 136 | if (b == 0) |
| 137 | { |
| 138 | return a == 0; |
| 139 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 140 | uint64_t sa = a % 6; |
| 141 | uint64_t sb = a % 6; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | if (sa == 5) |
| 143 | { |
| 144 | sa = 4; |
| 145 | } |
| 146 | if (sb == 5) |
| 147 | { |
| 148 | sb = 4; |
| 149 | } |
| 150 | if (sa != sb) |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | return isParameterTagCompatible(a / 6, b / 6); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 157 | constexpr uint64_t getParameterTag(std::string_view s, unsigned p = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | { |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 159 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 160 | if (p == s.size()) |
| 161 | { |
| 162 | return 0; |
| 163 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 164 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 165 | if (s[p] != '<') |
| 166 | { |
| 167 | return getParameterTag(s, p + 1); |
| 168 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 169 | |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 170 | if (isInt(s, p)) |
| 171 | { |
| 172 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 1; |
| 173 | } |
| 174 | |
| 175 | if (isUint(s, p)) |
| 176 | { |
| 177 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 2; |
| 178 | } |
| 179 | |
| 180 | if (isFloat(s, p)) |
| 181 | { |
| 182 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 3; |
| 183 | } |
| 184 | |
| 185 | if (isStr(s, p)) |
| 186 | { |
| 187 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 4; |
| 188 | } |
| 189 | |
| 190 | if (isPath(s, p)) |
| 191 | { |
| 192 | return getParameterTag(s, findClosingTag(s, p)) * 6 + 5; |
| 193 | } |
| 194 | |
| 195 | throw std::runtime_error("invalid parameter type"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 196 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 197 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 198 | template <typename... T> |
| 199 | struct S |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 200 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 201 | template <typename U> |
| 202 | using push = S<U, T...>; |
| 203 | template <typename U> |
| 204 | using push_back = S<T..., U>; |
| 205 | template <template <typename... Args> class U> |
| 206 | using rebind = U<T...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 207 | }; |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 208 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 209 | template <typename F, typename Set> |
| 210 | struct CallHelper; |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 211 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 212 | template <typename F, typename... Args> |
| 213 | struct CallHelper<F, S<Args...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 214 | { |
| 215 | template <typename F1, typename... Args1, |
| 216 | typename = decltype(std::declval<F1>()(std::declval<Args1>()...))> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 217 | static char test(int); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 218 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 219 | template <typename...> |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 220 | static int test(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 221 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 222 | static constexpr bool value = sizeof(test<F, Args...>(0)) == sizeof(char); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 225 | template <uint64_t N> |
| 226 | struct SingleTagToType |
| 227 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 228 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 229 | template <> |
| 230 | struct SingleTagToType<1> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 231 | { |
| 232 | using type = int64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 235 | template <> |
| 236 | struct SingleTagToType<2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | { |
| 238 | using type = uint64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 241 | template <> |
| 242 | struct SingleTagToType<3> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 243 | { |
| 244 | using type = double; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 247 | template <> |
| 248 | struct SingleTagToType<4> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | { |
| 250 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 253 | template <> |
| 254 | struct SingleTagToType<5> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 255 | { |
| 256 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 257 | }; |
| 258 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 259 | template <uint64_t Tag> |
| 260 | struct Arguments |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 261 | { |
| 262 | using subarguments = typename Arguments<Tag / 6>::type; |
| 263 | using type = typename subarguments::template push< |
| 264 | typename SingleTagToType<Tag % 6>::type>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 267 | template <> |
| 268 | struct Arguments<0> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | { |
| 270 | using type = S<>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 271 | }; |
| 272 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 273 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 274 | struct Promote |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | { |
| 276 | using type = T; |
| 277 | }; |
| 278 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 279 | template <typename T> |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 280 | using PromoteT = typename Promote<T>::type; |
| 281 | |
| 282 | template <> |
| 283 | struct Promote<char> |
| 284 | { |
| 285 | using type = int64_t; |
| 286 | }; |
| 287 | template <> |
| 288 | struct Promote<short> |
| 289 | { |
| 290 | using type = int64_t; |
| 291 | }; |
| 292 | template <> |
| 293 | struct Promote<int> |
| 294 | { |
| 295 | using type = int64_t; |
| 296 | }; |
| 297 | template <> |
| 298 | struct Promote<long> |
| 299 | { |
| 300 | using type = int64_t; |
| 301 | }; |
| 302 | template <> |
| 303 | struct Promote<long long> |
| 304 | { |
| 305 | using type = int64_t; |
| 306 | }; |
| 307 | template <> |
| 308 | struct Promote<unsigned char> |
| 309 | { |
| 310 | using type = uint64_t; |
| 311 | }; |
| 312 | template <> |
| 313 | struct Promote<unsigned short> |
| 314 | { |
| 315 | using type = uint64_t; |
| 316 | }; |
| 317 | template <> |
| 318 | struct Promote<unsigned int> |
| 319 | { |
| 320 | using type = uint64_t; |
| 321 | }; |
| 322 | template <> |
| 323 | struct Promote<unsigned long> |
| 324 | { |
| 325 | using type = uint64_t; |
| 326 | }; |
| 327 | template <> |
| 328 | struct Promote<unsigned long long> |
| 329 | { |
| 330 | using type = uint64_t; |
| 331 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 332 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 333 | } // namespace black_magic |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 334 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 335 | namespace detail |
| 336 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 337 | |
| 338 | template <class T, std::size_t N, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | struct GetIndexOfElementFromTupleByTypeImpl |
| 340 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 341 | static constexpr std::size_t value = N; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | template <class T, std::size_t N, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 | struct GetIndexOfElementFromTupleByTypeImpl<T, N, T, Args...> |
| 346 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 347 | static constexpr std::size_t value = N; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | template <class T, std::size_t N, class U, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 351 | struct GetIndexOfElementFromTupleByTypeImpl<T, N, U, Args...> |
| 352 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 353 | static constexpr std::size_t value = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 354 | GetIndexOfElementFromTupleByTypeImpl<T, N + 1, Args...>::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 357 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 358 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 359 | namespace utility |
| 360 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 361 | template <class T, class... Args> |
| 362 | T& getElementByType(std::tuple<Args...>& t) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 363 | { |
| 364 | return std::get< |
| 365 | detail::GetIndexOfElementFromTupleByTypeImpl<T, 0, Args...>::value>(t); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 368 | template <typename T> |
| 369 | struct function_traits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 370 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 371 | template <typename T> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 372 | struct function_traits : public function_traits<decltype(&T::operator())> |
| 373 | { |
| 374 | using parent_t = function_traits<decltype(&T::operator())>; |
| 375 | static const size_t arity = parent_t::arity; |
| 376 | using result_type = typename parent_t::result_type; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 377 | template <size_t i> |
| 378 | using arg = typename parent_t::template arg<i>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 379 | }; |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 380 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 381 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 382 | struct function_traits<r (ClassType::*)(Args...) const> |
| 383 | { |
| 384 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 385 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 386 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 387 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 388 | template <size_t i> |
| 389 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 392 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 393 | struct function_traits<r (ClassType::*)(Args...)> |
| 394 | { |
| 395 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 396 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 397 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 398 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 399 | template <size_t i> |
| 400 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 403 | template <typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 404 | struct function_traits<std::function<r(Args...)>> |
| 405 | { |
| 406 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 407 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 408 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 409 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 410 | template <size_t i> |
| 411 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 414 | inline std::string base64encode(const std::string_view data) |
| 415 | { |
| 416 | const std::array<char, 64> key = { |
| 417 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 418 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 419 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 420 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 421 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
| 422 | |
| 423 | size_t size = data.size(); |
| 424 | std::string ret; |
| 425 | ret.resize((size + 2) / 3 * 4); |
| 426 | auto it = ret.begin(); |
| 427 | |
| 428 | size_t i = 0; |
| 429 | while (i < size) |
| 430 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 431 | size_t keyIndex = 0; |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 432 | |
| 433 | keyIndex = static_cast<size_t>(data[i] & 0xFC) >> 2; |
| 434 | *it++ = key[keyIndex]; |
| 435 | |
| 436 | if (i + 1 < size) |
| 437 | { |
| 438 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 439 | keyIndex += static_cast<size_t>(data[i + 1] & 0xF0) >> 4; |
| 440 | *it++ = key[keyIndex]; |
| 441 | |
| 442 | if (i + 2 < size) |
| 443 | { |
| 444 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 445 | keyIndex += static_cast<size_t>(data[i + 2] & 0xC0) >> 6; |
| 446 | *it++ = key[keyIndex]; |
| 447 | |
| 448 | keyIndex = static_cast<size_t>(data[i + 2] & 0x3F); |
| 449 | *it++ = key[keyIndex]; |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | keyIndex = static_cast<size_t>(data[i + 1] & 0x0F) << 2; |
| 454 | *it++ = key[keyIndex]; |
| 455 | *it++ = '='; |
| 456 | } |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | keyIndex = static_cast<size_t>(data[i] & 0x03) << 4; |
| 461 | *it++ = key[keyIndex]; |
| 462 | *it++ = '='; |
| 463 | *it++ = '='; |
| 464 | } |
| 465 | |
| 466 | i += 3; |
| 467 | } |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 472 | // TODO this is temporary and should be deleted once base64 is refactored out of |
| 473 | // crow |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 474 | inline bool base64Decode(const std::string_view input, std::string& output) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 475 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 476 | static const char nop = static_cast<char>(-1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 477 | // See note on encoding_data[] in above function |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 478 | static const std::array<char, 256> decodingData = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 479 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 480 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 481 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 482 | nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, |
| 483 | 60, 61, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, |
| 484 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 485 | 19, 20, 21, 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, |
| 486 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 487 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, nop, nop, nop, |
| 488 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 489 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 490 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 491 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 492 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 493 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 494 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 495 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 496 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 497 | nop, nop, nop, nop}; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 498 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 499 | size_t inputLength = input.size(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 500 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 501 | // allocate space for output string |
| 502 | output.clear(); |
| 503 | output.reserve(((inputLength + 2) / 3) * 4); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 504 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 505 | auto getCodeValue = [](char c) { |
| 506 | auto code = static_cast<unsigned char>(c); |
| 507 | // Ensure we cannot index outside the bounds of the decoding array |
| 508 | static_assert(std::numeric_limits<decltype(code)>::max() < |
| 509 | decodingData.size()); |
| 510 | return decodingData[code]; |
| 511 | }; |
| 512 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | // for each 4-bytes sequence from the input, extract 4 6-bits sequences by |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 514 | // dropping first two bits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | // and regenerate into 3 8-bits sequences |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 516 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 517 | for (size_t i = 0; i < inputLength; i++) |
| 518 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 519 | char base64code0 = 0; |
| 520 | char base64code1 = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 521 | char base64code2 = 0; // initialized to 0 to suppress warnings |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 522 | char base64code3 = 0; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 523 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 524 | base64code0 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 525 | if (base64code0 == nop) |
| 526 | { // non base64 character |
| 527 | return false; |
| 528 | } |
| 529 | if (!(++i < inputLength)) |
| 530 | { // we need at least two input bytes for first |
| 531 | // byte output |
| 532 | return false; |
| 533 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 534 | base64code1 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 535 | if (base64code1 == nop) |
| 536 | { // non base64 character |
| 537 | return false; |
| 538 | } |
| 539 | output += |
| 540 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 541 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 542 | if (++i < inputLength) |
| 543 | { |
| 544 | char c = input[i]; |
| 545 | if (c == '=') |
| 546 | { // padding , end of input |
| 547 | return (base64code1 & 0x0f) == 0; |
| 548 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 549 | base64code2 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 550 | if (base64code2 == nop) |
| 551 | { // non base64 character |
| 552 | return false; |
| 553 | } |
| 554 | output += static_cast<char>(((base64code1 << 4) & 0xf0) | |
| 555 | ((base64code2 >> 2) & 0x0f)); |
| 556 | } |
| 557 | |
| 558 | if (++i < inputLength) |
| 559 | { |
| 560 | char c = input[i]; |
| 561 | if (c == '=') |
| 562 | { // padding , end of input |
| 563 | return (base64code2 & 0x03) == 0; |
| 564 | } |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 565 | base64code3 = getCodeValue(input[i]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 566 | if (base64code3 == nop) |
| 567 | { // non base64 character |
| 568 | return false; |
| 569 | } |
| 570 | output += |
| 571 | static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
| 572 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 575 | return true; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 578 | namespace details |
| 579 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 580 | constexpr uint64_t maxMilliSeconds = 253402300799999; |
| 581 | constexpr uint64_t maxSeconds = 253402300799; |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 582 | inline std::string getDateTime(boost::posix_time::milliseconds timeSinceEpoch) |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 583 | { |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 584 | boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 585 | boost::posix_time::ptime time = epoch + timeSinceEpoch; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 586 | // append zero offset to the end according to the Redfish spec for Date-Time |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 587 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 588 | } |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 589 | } // namespace details |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 590 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 591 | // Returns the formatted date time string. |
| 592 | // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if |
| 593 | // the given |secondsSinceEpoch| is too large, we return the maximum supported |
| 594 | // date. This behavior is to avoid exceptions throwed by Boost. |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 595 | inline std::string getDateTimeUint(uint64_t secondsSinceEpoch) |
| 596 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 597 | |
| 598 | boost::posix_time::seconds boostSeconds( |
| 599 | std::min(secondsSinceEpoch, details::maxSeconds)); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 600 | return details::getDateTime( |
| 601 | boost::posix_time::milliseconds(boostSeconds.total_milliseconds())); |
| 602 | } |
| 603 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 604 | // Returns the formatted date time string. |
| 605 | // Note that the maximum supported date is 9999-12-31T23:59:59.999+00:00, if |
| 606 | // the given |millisSecondsSinceEpoch| is too large, we return the maximum |
| 607 | // supported date. |
| 608 | inline std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch) |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 609 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 610 | return details::getDateTime(boost::posix_time::milliseconds( |
| 611 | std::min(details::maxMilliSeconds, milliSecondsSinceEpoch))); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 612 | } |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 613 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 614 | inline std::string getDateTimeStdtime(std::time_t secondsSinceEpoch) |
| 615 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 616 | boost::posix_time::ptime time = boost::posix_time::from_time_t( |
| 617 | std::min(secondsSinceEpoch, std::time_t{details::maxSeconds})); |
Krzysztof Grobelny | 45c367e | 2021-12-29 10:28:53 +0100 | [diff] [blame] | 618 | return boost::posix_time::to_iso_extended_string(time) + "+00:00"; |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 621 | /** |
| 622 | * Returns the current Date, Time & the local Time Offset |
| 623 | * infromation in a pair |
| 624 | * |
| 625 | * @param[in] None |
| 626 | * |
| 627 | * @return std::pair<std::string, std::string>, which consist |
| 628 | * of current DateTime & the TimeOffset strings respectively. |
| 629 | */ |
| 630 | inline std::pair<std::string, std::string> getDateTimeOffsetNow() |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 631 | { |
| 632 | std::time_t time = std::time(nullptr); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 633 | std::string dateTime = getDateTimeStdtime(time); |
Tejas Patil | 7c8c405 | 2021-06-04 17:43:14 +0530 | [diff] [blame] | 634 | |
| 635 | /* extract the local Time Offset value from the |
| 636 | * recevied dateTime string. |
| 637 | */ |
| 638 | std::string timeOffset("Z00:00"); |
| 639 | std::size_t lastPos = dateTime.size(); |
| 640 | std::size_t len = timeOffset.size(); |
| 641 | if (lastPos > len) |
| 642 | { |
| 643 | timeOffset = dateTime.substr(lastPos - len); |
| 644 | } |
| 645 | |
| 646 | return std::make_pair(dateTime, timeOffset); |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 649 | inline bool constantTimeStringCompare(const std::string_view a, |
| 650 | const std::string_view b) |
| 651 | { |
| 652 | // Important note, this function is ONLY constant time if the two input |
| 653 | // sizes are the same |
| 654 | if (a.size() != b.size()) |
| 655 | { |
| 656 | return false; |
| 657 | } |
| 658 | return CRYPTO_memcmp(a.data(), b.data(), a.size()) == 0; |
| 659 | } |
| 660 | |
| 661 | struct ConstantTimeCompare |
| 662 | { |
| 663 | bool operator()(const std::string_view a, const std::string_view b) const |
| 664 | { |
| 665 | return constantTimeStringCompare(a, b); |
| 666 | } |
| 667 | }; |
| 668 | |
George Liu | d139c23 | 2020-08-18 18:48:57 +0800 | [diff] [blame] | 669 | inline std::time_t getTimestamp(uint64_t millisTimeStamp) |
| 670 | { |
| 671 | // Retrieve Created property with format: |
| 672 | // yyyy-mm-ddThh:mm:ss |
| 673 | std::chrono::milliseconds chronoTimeStamp(millisTimeStamp); |
| 674 | return std::chrono::duration_cast<std::chrono::duration<int>>( |
| 675 | chronoTimeStamp) |
| 676 | .count(); |
| 677 | } |
| 678 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 679 | } // namespace utility |
| 680 | } // namespace crow |