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