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