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