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