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