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