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 | { |
| 19 | struct OutOfRange |
| 20 | { |
| 21 | OutOfRange(unsigned /*pos*/, unsigned /*length*/) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 22 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 23 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | constexpr unsigned requiresInRange(unsigned i, unsigned len) |
| 25 | { |
| 26 | return i >= len ? throw OutOfRange(i, len) : i; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | class ConstStr |
| 30 | { |
| 31 | const char* const beginPtr; |
| 32 | unsigned sizeUint; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | public: |
| 35 | template <unsigned N> |
| 36 | constexpr ConstStr(const char (&arr)[N]) : beginPtr(arr), sizeUint(N - 1) |
| 37 | { |
| 38 | static_assert(N >= 1, "not a string literal"); |
| 39 | } |
| 40 | constexpr char operator[](unsigned i) const |
| 41 | { |
| 42 | return requiresInRange(i, sizeUint), beginPtr[i]; |
| 43 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 44 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | constexpr operator const char*() const |
| 46 | { |
| 47 | return beginPtr; |
| 48 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 49 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | constexpr const char* begin() const |
| 51 | { |
| 52 | return beginPtr; |
| 53 | } |
| 54 | constexpr const char* end() const |
| 55 | { |
| 56 | return beginPtr + sizeUint; |
| 57 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | constexpr unsigned size() const |
| 60 | { |
| 61 | return sizeUint; |
| 62 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | constexpr unsigned findClosingTag(ConstStr s, unsigned p) |
| 66 | { |
| 67 | return s[p] == '>' ? p : findClosingTag(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | constexpr bool isValid(ConstStr s, unsigned i = 0, int f = 0) |
| 71 | { |
| 72 | return i == s.size() |
| 73 | ? f == 0 |
| 74 | : f < 0 || f >= 2 |
| 75 | ? false |
| 76 | : s[i] == '<' ? isValid(s, i + 1, f + 1) |
| 77 | : s[i] == '>' ? isValid(s, i + 1, f - 1) |
| 78 | : isValid(s, i + 1, f); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 81 | constexpr bool isEquN(ConstStr a, unsigned ai, ConstStr b, unsigned bi, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | unsigned n) |
| 83 | { |
| 84 | return ai + n > a.size() || bi + n > b.size() |
| 85 | ? false |
| 86 | : n == 0 ? true |
| 87 | : a[ai] != b[bi] ? false |
| 88 | : isEquN(a, ai + 1, b, bi + 1, n - 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 91 | constexpr bool isInt(ConstStr s, unsigned i) |
| 92 | { |
| 93 | return isEquN(s, i, "<int>", 0, 5); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | constexpr bool isUint(ConstStr s, unsigned i) |
| 97 | { |
| 98 | return isEquN(s, i, "<uint>", 0, 6); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | constexpr bool isFloat(ConstStr s, unsigned i) |
| 102 | { |
| 103 | return isEquN(s, i, "<float>", 0, 7) || isEquN(s, i, "<double>", 0, 8); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | constexpr bool isStr(ConstStr s, unsigned i) |
| 107 | { |
| 108 | return isEquN(s, i, "<str>", 0, 5) || isEquN(s, i, "<string>", 0, 8); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | constexpr bool isPath(ConstStr s, unsigned i) |
| 112 | { |
| 113 | return isEquN(s, i, "<path>", 0, 6); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 114 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 115 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 116 | template <typename T> |
| 117 | constexpr int getParameterTag() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 119 | if constexpr (std::is_same_v<int, T>) |
| 120 | { |
| 121 | return 1; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 123 | if constexpr (std::is_same_v<char, T>) |
| 124 | { |
| 125 | return 1; |
| 126 | } |
| 127 | if constexpr (std::is_same_v<short, T>) |
| 128 | { |
| 129 | return 1; |
| 130 | } |
| 131 | if constexpr (std::is_same_v<long, T>) |
| 132 | { |
| 133 | return 1; |
| 134 | } |
| 135 | if constexpr (std::is_same_v<long long, T>) |
| 136 | { |
| 137 | return 1; |
| 138 | } |
| 139 | if constexpr (std::is_same_v<unsigned int, T>) |
| 140 | { |
| 141 | return 2; |
| 142 | } |
| 143 | if constexpr (std::is_same_v<unsigned char, T>) |
| 144 | { |
| 145 | return 2; |
| 146 | } |
| 147 | if constexpr (std::is_same_v<unsigned short, T>) |
| 148 | { |
| 149 | return 2; |
| 150 | } |
| 151 | if constexpr (std::is_same_v<unsigned long, T>) |
| 152 | { |
| 153 | return 2; |
| 154 | } |
| 155 | if constexpr (std::is_same_v<unsigned long long, T>) |
| 156 | { |
| 157 | return 2; |
| 158 | } |
| 159 | if constexpr (std::is_same_v<double, T>) |
| 160 | { |
| 161 | return 3; |
| 162 | } |
| 163 | if constexpr (std::is_same_v<std::string, T>) |
| 164 | { |
| 165 | return 4; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 170 | template <typename... Args> |
| 171 | struct compute_parameter_tag_from_args_list; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 172 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 173 | template <> |
| 174 | struct compute_parameter_tag_from_args_list<> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 176 | static constexpr int value = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | template <typename Arg, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | struct compute_parameter_tag_from_args_list<Arg, Args...> |
| 181 | { |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 182 | static constexpr int subValue = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | compute_parameter_tag_from_args_list<Args...>::value; |
Ed Tanous | 6950901 | 2019-10-24 16:53:05 -0700 | [diff] [blame] | 184 | static constexpr int value = |
| 185 | getParameterTag<typename std::decay<Arg>::type>() |
| 186 | ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 187 | : subValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | static inline bool isParameterTagCompatible(uint64_t a, uint64_t b) |
| 191 | { |
| 192 | if (a == 0) |
| 193 | { |
| 194 | return b == 0; |
| 195 | } |
| 196 | if (b == 0) |
| 197 | { |
| 198 | return a == 0; |
| 199 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 200 | uint64_t sa = a % 6; |
| 201 | uint64_t sb = a % 6; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | if (sa == 5) |
| 203 | { |
| 204 | sa = 4; |
| 205 | } |
| 206 | if (sb == 5) |
| 207 | { |
| 208 | sb = 4; |
| 209 | } |
| 210 | if (sa != sb) |
| 211 | { |
| 212 | return false; |
| 213 | } |
| 214 | return isParameterTagCompatible(a / 6, b / 6); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 217 | static inline unsigned findClosingTagRuntime(const char* s, unsigned p) |
| 218 | { |
| 219 | return s[p] == 0 ? throw std::runtime_error("unmatched tag <") |
| 220 | : s[p] == '>' ? p : findClosingTagRuntime(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 223 | static inline uint64_t getParameterTagRuntime(const char* s, unsigned p = 0) |
| 224 | { |
| 225 | return s[p] == 0 |
| 226 | ? 0 |
| 227 | : s[p] == '<' |
| 228 | ? (std::strncmp(s + p, "<int>", 5) == 0 |
| 229 | ? getParameterTagRuntime( |
| 230 | s, findClosingTagRuntime(s, p)) * |
| 231 | 6 + |
| 232 | 1 |
| 233 | : std::strncmp(s + p, "<uint>", 6) == 0 |
| 234 | ? getParameterTagRuntime( |
| 235 | s, findClosingTagRuntime(s, p)) * |
| 236 | 6 + |
| 237 | 2 |
| 238 | : (std::strncmp(s + p, "<float>", 7) == 0 || |
| 239 | std::strncmp(s + p, "<double>", 8) == 0) |
| 240 | ? getParameterTagRuntime( |
| 241 | s, findClosingTagRuntime(s, p)) * |
| 242 | 6 + |
| 243 | 3 |
| 244 | : (std::strncmp(s + p, "<str>", 5) == |
| 245 | 0 || |
| 246 | std::strncmp(s + p, "<string>", 8) == |
| 247 | 0) |
| 248 | ? getParameterTagRuntime( |
| 249 | s, findClosingTagRuntime( |
| 250 | s, p)) * |
| 251 | 6 + |
| 252 | 4 |
| 253 | : std::strncmp(s + p, "<path>", |
| 254 | 6) == 0 |
| 255 | ? getParameterTagRuntime( |
| 256 | s, |
| 257 | findClosingTagRuntime( |
| 258 | s, p)) * |
| 259 | 6 + |
| 260 | 5 |
| 261 | : throw std::runtime_error( |
| 262 | "invalid parameter " |
| 263 | "type")) |
| 264 | : getParameterTagRuntime(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 265 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 266 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | constexpr uint64_t get_parameter_tag(ConstStr s, unsigned p = 0) |
| 268 | { |
| 269 | return p == s.size() |
| 270 | ? 0 |
| 271 | : s[p] == '<' |
| 272 | ? (isInt(s, p) |
| 273 | ? get_parameter_tag(s, findClosingTag(s, p)) * 6 + 1 |
| 274 | : isUint(s, p) |
| 275 | ? get_parameter_tag(s, findClosingTag(s, p)) * |
| 276 | 6 + |
| 277 | 2 |
| 278 | : isFloat(s, p) |
| 279 | ? get_parameter_tag( |
| 280 | s, findClosingTag(s, p)) * |
| 281 | 6 + |
| 282 | 3 |
| 283 | : isStr(s, p) |
| 284 | ? get_parameter_tag( |
| 285 | s, findClosingTag(s, p)) * |
| 286 | 6 + |
| 287 | 4 |
| 288 | : isPath(s, p) |
| 289 | ? get_parameter_tag( |
| 290 | s, findClosingTag( |
| 291 | s, p)) * |
| 292 | 6 + |
| 293 | 5 |
| 294 | : throw std::runtime_error( |
| 295 | "invalid parameter " |
| 296 | "type")) |
| 297 | : get_parameter_tag(s, p + 1); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 298 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 299 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 300 | template <typename... T> |
| 301 | struct S |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 302 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 303 | template <typename U> |
| 304 | using push = S<U, T...>; |
| 305 | template <typename U> |
| 306 | using push_back = S<T..., U>; |
| 307 | template <template <typename... Args> class U> |
| 308 | using rebind = U<T...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 309 | }; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 310 | template <typename F, typename Set> |
| 311 | struct CallHelper; |
| 312 | template <typename F, typename... Args> |
| 313 | struct CallHelper<F, S<Args...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 314 | { |
| 315 | template <typename F1, typename... Args1, |
| 316 | typename = decltype(std::declval<F1>()(std::declval<Args1>()...))> |
| 317 | static char __test(int); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 318 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 319 | template <typename...> |
| 320 | static int __test(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 321 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 322 | static constexpr bool value = sizeof(__test<F, Args...>(0)) == sizeof(char); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 325 | template <uint64_t N> |
| 326 | struct SingleTagToType |
| 327 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 328 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 329 | template <> |
| 330 | struct SingleTagToType<1> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 331 | { |
| 332 | using type = int64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 333 | }; |
| 334 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 335 | template <> |
| 336 | struct SingleTagToType<2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 337 | { |
| 338 | using type = uint64_t; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 341 | template <> |
| 342 | struct SingleTagToType<3> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | { |
| 344 | using type = double; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 345 | }; |
| 346 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 347 | template <> |
| 348 | struct SingleTagToType<4> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 349 | { |
| 350 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 353 | template <> |
| 354 | struct SingleTagToType<5> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 355 | { |
| 356 | using type = std::string; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 359 | template <uint64_t Tag> |
| 360 | struct Arguments |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | { |
| 362 | using subarguments = typename Arguments<Tag / 6>::type; |
| 363 | using type = typename subarguments::template push< |
| 364 | typename SingleTagToType<Tag % 6>::type>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 367 | template <> |
| 368 | struct Arguments<0> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 369 | { |
| 370 | using type = S<>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 373 | template <typename... T> |
| 374 | struct LastElementType |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 375 | { |
| 376 | using type = |
| 377 | typename std::tuple_element<sizeof...(T) - 1, std::tuple<T...>>::type; |
| 378 | }; |
| 379 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 380 | template <> |
| 381 | struct LastElementType<> |
| 382 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 383 | |
| 384 | // from |
| 385 | // http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 386 | template <class T> |
| 387 | using Invoke = typename T::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 388 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 389 | template <unsigned...> |
| 390 | struct Seq |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 391 | { |
| 392 | using type = Seq; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 395 | template <class S1, class S2> |
| 396 | struct concat; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 397 | |
| 398 | template <unsigned... I1, unsigned... I2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 399 | struct concat<Seq<I1...>, Seq<I2...>> : Seq<I1..., (sizeof...(I1) + I2)...> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 400 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 401 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 402 | template <class S1, class S2> |
| 403 | using Concat = Invoke<concat<S1, S2>>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 404 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 405 | template <size_t N> |
| 406 | struct gen_seq; |
| 407 | template <size_t N> |
| 408 | using GenSeq = Invoke<gen_seq<N>>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 409 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 410 | template <size_t N> |
| 411 | struct gen_seq : Concat<GenSeq<N / 2>, GenSeq<N - N / 2>> |
| 412 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 413 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 414 | template <> |
| 415 | struct gen_seq<0> : Seq<> |
| 416 | {}; |
| 417 | template <> |
| 418 | struct gen_seq<1> : Seq<0> |
| 419 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 420 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 421 | template <typename Seq, typename Tuple> |
| 422 | struct PopBackHelper; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 423 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 424 | template <unsigned... N, typename Tuple> |
| 425 | struct PopBackHelper<Seq<N...>, Tuple> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 426 | { |
| 427 | template <template <typename... Args> class U> |
| 428 | using rebind = U<typename std::tuple_element<N, Tuple>::type...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | template <typename... T> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 432 | struct PopBack //: public PopBackHelper<typename |
| 433 | // gen_seq<sizeof...(T)-1>::type, std::tuple<T...>> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 434 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 435 | template <template <typename... Args> class U> |
| 436 | using rebind = |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 437 | typename PopBackHelper<typename gen_seq<sizeof...(T) - 1UL>::type, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 438 | std::tuple<T...>>::template rebind<U>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 441 | template <> |
| 442 | struct PopBack<> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 443 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 444 | template <template <typename... Args> class U> |
| 445 | using rebind = U<>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | // from |
| 449 | // http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 450 | template <typename Tp, typename... List> |
| 451 | struct Contains : std::true_type |
| 452 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 453 | |
| 454 | template <typename Tp, typename Head, typename... Rest> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 455 | struct Contains<Tp, Head, Rest...> : |
| 456 | std::conditional<std::is_same<Tp, Head>::value, std::true_type, |
| 457 | Contains<Tp, Rest...>>::type |
| 458 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 459 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 460 | template <typename Tp> |
| 461 | struct Contains<Tp> : std::false_type |
| 462 | {}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 463 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 464 | template <typename T> |
| 465 | struct EmptyContext |
| 466 | {}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 467 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 468 | template <typename T> |
| 469 | struct promote |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 470 | { |
| 471 | using type = T; |
| 472 | }; |
| 473 | |
| 474 | #define BMCWEB_INTERNAL_PROMOTE_TYPE(t1, t2) \ |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 475 | template <> \ |
| 476 | struct promote<t1> \ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 477 | { \ |
| 478 | using type = t2; \ |
| 479 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 480 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 481 | BMCWEB_INTERNAL_PROMOTE_TYPE(char, int64_t); |
| 482 | BMCWEB_INTERNAL_PROMOTE_TYPE(short, int64_t); |
| 483 | BMCWEB_INTERNAL_PROMOTE_TYPE(int, int64_t); |
| 484 | BMCWEB_INTERNAL_PROMOTE_TYPE(long, int64_t); |
| 485 | BMCWEB_INTERNAL_PROMOTE_TYPE(long long, int64_t); |
| 486 | BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned char, uint64_t); |
| 487 | BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned short, uint64_t); |
| 488 | BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned int, uint64_t); |
| 489 | BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned long, uint64_t); |
| 490 | BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned long long, uint64_t); |
| 491 | BMCWEB_INTERNAL_PROMOTE_TYPE(float, double); |
| 492 | #undef BMCWEB_INTERNAL_PROMOTE_TYPE |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 493 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 494 | template <typename T> |
| 495 | using promote_t = typename promote<T>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 496 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 497 | } // namespace black_magic |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 498 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 499 | namespace detail |
| 500 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 501 | |
| 502 | template <class T, std::size_t N, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 503 | struct GetIndexOfElementFromTupleByTypeImpl |
| 504 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 505 | static constexpr std::size_t value = N; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | template <class T, std::size_t N, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | struct GetIndexOfElementFromTupleByTypeImpl<T, N, T, Args...> |
| 510 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 511 | static constexpr std::size_t value = N; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | template <class T, std::size_t N, class U, class... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | struct GetIndexOfElementFromTupleByTypeImpl<T, N, U, Args...> |
| 516 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 517 | static constexpr std::size_t value = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 518 | GetIndexOfElementFromTupleByTypeImpl<T, N + 1, Args...>::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 521 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 522 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 523 | namespace utility |
| 524 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 525 | template <class T, class... Args> |
| 526 | T& getElementByType(std::tuple<Args...>& t) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 527 | { |
| 528 | return std::get< |
| 529 | detail::GetIndexOfElementFromTupleByTypeImpl<T, 0, Args...>::value>(t); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 532 | template <typename T> |
| 533 | struct function_traits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 534 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 535 | template <typename T> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 536 | struct function_traits : public function_traits<decltype(&T::operator())> |
| 537 | { |
| 538 | using parent_t = function_traits<decltype(&T::operator())>; |
| 539 | static const size_t arity = parent_t::arity; |
| 540 | using result_type = typename parent_t::result_type; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 541 | template <size_t i> |
| 542 | using arg = typename parent_t::template arg<i>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 543 | }; |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 544 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 545 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 546 | struct function_traits<r (ClassType::*)(Args...) const> |
| 547 | { |
| 548 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 549 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 550 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 551 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 552 | template <size_t i> |
| 553 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 556 | template <typename ClassType, typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 557 | struct function_traits<r (ClassType::*)(Args...)> |
| 558 | { |
| 559 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 560 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 561 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 562 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 563 | template <size_t i> |
| 564 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 565 | }; |
| 566 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 567 | template <typename r, typename... Args> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 568 | struct function_traits<std::function<r(Args...)>> |
| 569 | { |
| 570 | static const size_t arity = sizeof...(Args); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 571 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 572 | using result_type = r; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 573 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 574 | template <size_t i> |
| 575 | using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | inline static std::string base64encode( |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 579 | const char* data, size_t size, |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 580 | const char* key = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 581 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") |
| 582 | { |
| 583 | std::string ret; |
| 584 | ret.resize((size + 2) / 3 * 4); |
| 585 | auto it = ret.begin(); |
| 586 | while (size >= 3) |
| 587 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 588 | *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2]; |
| 589 | unsigned char h = static_cast<unsigned char>( |
| 590 | (static_cast<unsigned char>(*data++) & 0x03u) << 4u); |
| 591 | *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xF0) >> 4)]; |
| 592 | h = static_cast<unsigned char>( |
| 593 | (static_cast<unsigned char>(*data++) & 0x0F) << 2u); |
| 594 | *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xC0) >> 6)]; |
| 595 | *it++ = key[static_cast<unsigned char>(*data++) & 0x3F]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 596 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 597 | size -= 3; |
| 598 | } |
| 599 | if (size == 1) |
| 600 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 601 | *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2]; |
| 602 | unsigned char h = static_cast<unsigned char>( |
| 603 | (static_cast<unsigned char>(*data++) & 0x03) << 4u); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 604 | *it++ = key[h]; |
| 605 | *it++ = '='; |
| 606 | *it++ = '='; |
| 607 | } |
| 608 | else if (size == 2) |
| 609 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 610 | *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2]; |
| 611 | unsigned char h = static_cast<unsigned char>( |
| 612 | (static_cast<unsigned char>(*data++) & 0x03) << 4u); |
| 613 | *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xF0) >> 4)]; |
| 614 | h = static_cast<unsigned char>( |
| 615 | (static_cast<unsigned char>(*data++) & 0x0F) << 2u); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 616 | *it++ = key[h]; |
| 617 | *it++ = '='; |
| 618 | } |
| 619 | return ret; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 622 | inline static std::string base64encodeUrlsafe(const char* data, size_t size) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | { |
| 624 | return base64encode( |
| 625 | data, size, |
| 626 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 629 | // TODO this is temporary and should be deleted once base64 is refactored out of |
| 630 | // crow |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 631 | inline bool base64Decode(const std::string_view input, std::string& output) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 633 | static const char nop = static_cast<char>(-1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 634 | // See note on encoding_data[] in above function |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 635 | static const char decodingData[] = { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 637 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 638 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 639 | nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, |
| 640 | 60, 61, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, |
| 641 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 642 | 19, 20, 21, 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, |
| 643 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 644 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, nop, nop, nop, |
| 645 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 646 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 647 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 648 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 649 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 650 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 651 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 652 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 653 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 654 | nop, nop, nop, nop}; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 655 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 656 | size_t inputLength = input.size(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 657 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 658 | // allocate space for output string |
| 659 | output.clear(); |
| 660 | output.reserve(((inputLength + 2) / 3) * 4); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 661 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 662 | // 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] | 663 | // dropping first two bits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 664 | // and regenerate into 3 8-bits sequences |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 665 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 666 | for (size_t i = 0; i < inputLength; i++) |
| 667 | { |
| 668 | char base64code0; |
| 669 | char base64code1; |
| 670 | char base64code2 = 0; // initialized to 0 to suppress warnings |
| 671 | char base64code3; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 672 | |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 673 | base64code0 = decodingData[static_cast<int>(input[i])]; // NOLINT |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 674 | if (base64code0 == nop) |
| 675 | { // non base64 character |
| 676 | return false; |
| 677 | } |
| 678 | if (!(++i < inputLength)) |
| 679 | { // we need at least two input bytes for first |
| 680 | // byte output |
| 681 | return false; |
| 682 | } |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 683 | base64code1 = decodingData[static_cast<int>(input[i])]; // NOLINT |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 684 | if (base64code1 == nop) |
| 685 | { // non base64 character |
| 686 | return false; |
| 687 | } |
| 688 | output += |
| 689 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 690 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 691 | if (++i < inputLength) |
| 692 | { |
| 693 | char c = input[i]; |
| 694 | if (c == '=') |
| 695 | { // padding , end of input |
| 696 | return (base64code1 & 0x0f) == 0; |
| 697 | } |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 698 | base64code2 = decodingData[static_cast<int>(input[i])]; // NOLINT |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 699 | if (base64code2 == nop) |
| 700 | { // non base64 character |
| 701 | return false; |
| 702 | } |
| 703 | output += static_cast<char>(((base64code1 << 4) & 0xf0) | |
| 704 | ((base64code2 >> 2) & 0x0f)); |
| 705 | } |
| 706 | |
| 707 | if (++i < inputLength) |
| 708 | { |
| 709 | char c = input[i]; |
| 710 | if (c == '=') |
| 711 | { // padding , end of input |
| 712 | return (base64code2 & 0x03) == 0; |
| 713 | } |
James Feist | 5a80664 | 2020-07-31 16:40:33 +0000 | [diff] [blame] | 714 | base64code3 = decodingData[static_cast<int>(input[i])]; // NOLINT |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 715 | if (base64code3 == nop) |
| 716 | { // non base64 character |
| 717 | return false; |
| 718 | } |
| 719 | output += |
| 720 | static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
| 721 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 724 | return true; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 727 | inline void escapeHtml(std::string& data) |
| 728 | { |
| 729 | std::string buffer; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 730 | // less than 5% of characters should be larger, so reserve a buffer of the |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 731 | // right size |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 732 | buffer.reserve(data.size() * 11 / 10); |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 733 | for (size_t pos = 0; pos != data.size(); ++pos) |
| 734 | { |
| 735 | switch (data[pos]) |
| 736 | { |
| 737 | case '&': |
| 738 | buffer.append("&"); |
| 739 | break; |
| 740 | case '\"': |
| 741 | buffer.append("""); |
| 742 | break; |
| 743 | case '\'': |
| 744 | buffer.append("'"); |
| 745 | break; |
| 746 | case '<': |
| 747 | buffer.append("<"); |
| 748 | break; |
| 749 | case '>': |
| 750 | buffer.append(">"); |
| 751 | break; |
| 752 | default: |
| 753 | buffer.append(&data[pos], 1); |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | data.swap(buffer); |
| 758 | } |
| 759 | |
| 760 | inline void convertToLinks(std::string& s) |
| 761 | { |
Jason M. Bills | a6e2f1c | 2019-12-11 14:32:14 -0800 | [diff] [blame] | 762 | // Convert anything with a redfish path into a link |
| 763 | const static std::regex redfishPath{ |
| 764 | "("((.*))"[ \\n]*:[ " |
| 765 | "\\n]*)("((?!")/redfish/.*)")"}; |
| 766 | s = std::regex_replace(s, redfishPath, "$1<a href=\"$5\">$4</a>"); |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 769 | /** |
| 770 | * Method returns Date Time information according to requested format |
| 771 | * |
| 772 | * @param[in] time time in second since the Epoch |
| 773 | * |
| 774 | * @return Date Time according to requested format |
| 775 | */ |
| 776 | inline std::string getDateTime(const std::time_t& time) |
| 777 | { |
| 778 | std::array<char, 128> dateTime; |
| 779 | std::string redfishDateTime("0000-00-00T00:00:00Z00:00"); |
| 780 | |
| 781 | if (std::strftime(dateTime.begin(), dateTime.size(), "%FT%T%z", |
| 782 | std::localtime(&time))) |
| 783 | { |
| 784 | // insert the colon required by the ISO 8601 standard |
| 785 | redfishDateTime = std::string(dateTime.data()); |
| 786 | redfishDateTime.insert(redfishDateTime.end() - 2, ':'); |
| 787 | } |
| 788 | |
| 789 | return redfishDateTime; |
| 790 | } |
| 791 | |
| 792 | inline std::string dateTimeNow() |
| 793 | { |
| 794 | std::time_t time = std::time(nullptr); |
| 795 | return getDateTime(time); |
| 796 | } |
| 797 | |
Ed Tanous | 51dae67 | 2018-09-05 16:07:32 -0700 | [diff] [blame] | 798 | inline bool constantTimeStringCompare(const std::string_view a, |
| 799 | const std::string_view b) |
| 800 | { |
| 801 | // Important note, this function is ONLY constant time if the two input |
| 802 | // sizes are the same |
| 803 | if (a.size() != b.size()) |
| 804 | { |
| 805 | return false; |
| 806 | } |
| 807 | return CRYPTO_memcmp(a.data(), b.data(), a.size()) == 0; |
| 808 | } |
| 809 | |
| 810 | struct ConstantTimeCompare |
| 811 | { |
| 812 | bool operator()(const std::string_view a, const std::string_view b) const |
| 813 | { |
| 814 | return constantTimeStringCompare(a, b); |
| 815 | } |
| 816 | }; |
| 817 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 818 | } // namespace utility |
| 819 | } // namespace crow |