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