summaryrefslogtreecommitdiff
path: root/http/utility.h
blob: c71187d0c84aa9abdd5f69abb70696c2b8283ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#pragma once

#include "nlohmann/json.hpp"

#include <openssl/crypto.h>

#include <cstdint>
#include <cstring>
#include <functional>
#include <regex>
#include <stdexcept>
#include <string>
#include <tuple>

namespace crow
{
namespace black_magic
{
struct OutOfRange
{
    OutOfRange(unsigned /*pos*/, unsigned /*length*/)
    {
    }
};
constexpr unsigned requiresInRange(unsigned i, unsigned len)
{
    return i >= len ? throw OutOfRange(i, len) : i;
}

class ConstStr
{
    const char* const beginPtr;
    unsigned sizeUint;

  public:
    template <unsigned N>
    constexpr ConstStr(const char (&arr)[N]) : beginPtr(arr), sizeUint(N - 1)
    {
        static_assert(N >= 1, "not a string literal");
    }
    constexpr char operator[](unsigned i) const
    {
        return requiresInRange(i, sizeUint), beginPtr[i];
    }

    constexpr operator const char*() const
    {
        return beginPtr;
    }

    constexpr const char* begin() const
    {
        return beginPtr;
    }
    constexpr const char* end() const
    {
        return beginPtr + sizeUint;
    }

    constexpr unsigned size() const
    {
        return sizeUint;
    }
};

constexpr unsigned findClosingTag(ConstStr s, unsigned p)
{
    return s[p] == '>' ? p : findClosingTag(s, p + 1);
}

constexpr bool isValid(ConstStr s, unsigned i = 0, int f = 0)
{
    return i == s.size()
               ? f == 0
               : f < 0 || f >= 2
                     ? false
                     : s[i] == '<' ? isValid(s, i + 1, f + 1)
                                   : s[i] == '>' ? isValid(s, i + 1, f - 1)
                                                 : isValid(s, i + 1, f);
}

constexpr bool isEquP(const char* a, const char* b, unsigned n)
{
    return *a == 0 && *b == 0 && n == 0
               ? true
               : (*a == 0 || *b == 0)
                     ? false
                     : n == 0 ? true
                              : *a != *b ? false : isEquP(a + 1, b + 1, n - 1);
}

constexpr bool isEquN(ConstStr a, unsigned ai, ConstStr b, unsigned bi,
                      unsigned n)
{
    return ai + n > a.size() || bi + n > b.size()
               ? false
               : n == 0 ? true
                        : a[ai] != b[bi] ? false
                                         : isEquN(a, ai + 1, b, bi + 1, n - 1);
}

constexpr bool isInt(ConstStr s, unsigned i)
{
    return isEquN(s, i, "<int>", 0, 5);
}

constexpr bool isUint(ConstStr s, unsigned i)
{
    return isEquN(s, i, "<uint>", 0, 6);
}

constexpr bool isFloat(ConstStr s, unsigned i)
{
    return isEquN(s, i, "<float>", 0, 7) || isEquN(s, i, "<double>", 0, 8);
}

constexpr bool isStr(ConstStr s, unsigned i)
{
    return isEquN(s, i, "<str>", 0, 5) || isEquN(s, i, "<string>", 0, 8);
}

constexpr bool isPath(ConstStr s, unsigned i)
{
    return isEquN(s, i, "<path>", 0, 6);
}

template <typename T> constexpr int getParameterTag()
{
    if constexpr (std::is_same_v<int, T>)
    {
        return 1;
    }
    if constexpr (std::is_same_v<char, T>)
    {
        return 1;
    }
    if constexpr (std::is_same_v<short, T>)
    {
        return 1;
    }
    if constexpr (std::is_same_v<long, T>)
    {
        return 1;
    }
    if constexpr (std::is_same_v<long long, T>)
    {
        return 1;
    }
    if constexpr (std::is_same_v<unsigned int, T>)
    {
        return 2;
    }
    if constexpr (std::is_same_v<unsigned char, T>)
    {
        return 2;
    }
    if constexpr (std::is_same_v<unsigned short, T>)
    {
        return 2;
    }
    if constexpr (std::is_same_v<unsigned long, T>)
    {
        return 2;
    }
    if constexpr (std::is_same_v<unsigned long long, T>)
    {
        return 2;
    }
    if constexpr (std::is_same_v<double, T>)
    {
        return 3;
    }
    if constexpr (std::is_same_v<std::string, T>)
    {
        return 4;
    }
    return 0;
}

template <typename... Args> struct compute_parameter_tag_from_args_list;

template <> struct compute_parameter_tag_from_args_list<>
{
    static constexpr int value = 0;
};

template <typename Arg, typename... Args>
struct compute_parameter_tag_from_args_list<Arg, Args...>
{
    static constexpr int subValue =
        compute_parameter_tag_from_args_list<Args...>::value;
    static constexpr int value =
        getParameterTag<typename std::decay<Arg>::type>()
            ? subValue * 6 + getParameterTag<typename std::decay<Arg>::type>()
            : subValue;
};

static inline bool isParameterTagCompatible(uint64_t a, uint64_t b)
{
    if (a == 0)
    {
        return b == 0;
    }
    if (b == 0)
    {
        return a == 0;
    }
    uint64_t sa = a % 6;
    uint64_t sb = a % 6;
    if (sa == 5)
    {
        sa = 4;
    }
    if (sb == 5)
    {
        sb = 4;
    }
    if (sa != sb)
    {
        return false;
    }
    return isParameterTagCompatible(a / 6, b / 6);
}

static inline unsigned findClosingTagRuntime(const char* s, unsigned p)
{
    return s[p] == 0 ? throw std::runtime_error("unmatched tag <")
                     : s[p] == '>' ? p : findClosingTagRuntime(s, p + 1);
}

static inline uint64_t getParameterTagRuntime(const char* s, unsigned p = 0)
{
    return s[p] == 0
               ? 0
               : s[p] == '<'
                     ? (std::strncmp(s + p, "<int>", 5) == 0
                            ? getParameterTagRuntime(
                                  s, findClosingTagRuntime(s, p)) *
                                      6 +
                                  1
                            : std::strncmp(s + p, "<uint>", 6) == 0
                                  ? getParameterTagRuntime(
                                        s, findClosingTagRuntime(s, p)) *
                                            6 +
                                        2
                                  : (std::strncmp(s + p, "<float>", 7) == 0 ||
                                     std::strncmp(s + p, "<double>", 8) == 0)
                                        ? getParameterTagRuntime(
                                              s, findClosingTagRuntime(s, p)) *
                                                  6 +
                                              3
                                        : (std::strncmp(s + p, "<str>", 5) ==
                                               0 ||
                                           std::strncmp(s + p, "<string>", 8) ==
                                               0)
                                              ? getParameterTagRuntime(
                                                    s, findClosingTagRuntime(
                                                           s, p)) *
                                                        6 +
                                                    4
                                              : std::strncmp(s + p, "<path>",
                                                             6) == 0
                                                    ? getParameterTagRuntime(
                                                          s,
                                                          findClosingTagRuntime(
                                                              s, p)) *
                                                              6 +
                                                          5
                                                    : throw std::runtime_error(
                                                          "invalid parameter "
                                                          "type"))
                     : getParameterTagRuntime(s, p + 1);
}

constexpr uint64_t get_parameter_tag(ConstStr s, unsigned p = 0)
{
    return p == s.size()
               ? 0
               : s[p] == '<'
                     ? (isInt(s, p)
                            ? get_parameter_tag(s, findClosingTag(s, p)) * 6 + 1
                            : isUint(s, p)
                                  ? get_parameter_tag(s, findClosingTag(s, p)) *
                                            6 +
                                        2
                                  : isFloat(s, p)
                                        ? get_parameter_tag(
                                              s, findClosingTag(s, p)) *
                                                  6 +
                                              3
                                        : isStr(s, p)
                                              ? get_parameter_tag(
                                                    s, findClosingTag(s, p)) *
                                                        6 +
                                                    4
                                              : isPath(s, p)
                                                    ? get_parameter_tag(
                                                          s, findClosingTag(
                                                                 s, p)) *
                                                              6 +
                                                          5
                                                    : throw std::runtime_error(
                                                          "invalid parameter "
                                                          "type"))
                     : get_parameter_tag(s, p + 1);
}

template <typename... T> struct S
{
    template <typename U> using push = S<U, T...>;
    template <typename U> using push_back = S<T..., U>;
    template <template <typename... Args> class U> using rebind = U<T...>;
};
template <typename F, typename Set> struct CallHelper;
template <typename F, typename... Args> struct CallHelper<F, S<Args...>>
{
    template <typename F1, typename... Args1,
              typename = decltype(std::declval<F1>()(std::declval<Args1>()...))>
    static char __test(int);

    template <typename...> static int __test(...);

    static constexpr bool value = sizeof(__test<F, Args...>(0)) == sizeof(char);
};

template <uint64_t N> struct SingleTagToType
{
};

template <> struct SingleTagToType<1>
{
    using type = int64_t;
};

template <> struct SingleTagToType<2>
{
    using type = uint64_t;
};

template <> struct SingleTagToType<3>
{
    using type = double;
};

template <> struct SingleTagToType<4>
{
    using type = std::string;
};

template <> struct SingleTagToType<5>
{
    using type = std::string;
};

template <uint64_t Tag> struct Arguments
{
    using subarguments = typename Arguments<Tag / 6>::type;
    using type = typename subarguments::template push<
        typename SingleTagToType<Tag % 6>::type>;
};

template <> struct Arguments<0>
{
    using type = S<>;
};

template <typename... T> struct LastElementType
{
    using type =
        typename std::tuple_element<sizeof...(T) - 1, std::tuple<T...>>::type;
};

template <> struct LastElementType<>
{
};

// from
// http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth
template <class T> using Invoke = typename T::type;

template <unsigned...> struct Seq
{
    using type = Seq;
};

template <class S1, class S2> struct concat;

template <unsigned... I1, unsigned... I2>
struct concat<Seq<I1...>, Seq<I2...>> : Seq<I1..., (sizeof...(I1) + I2)...>
{
};

template <class S1, class S2> using Concat = Invoke<concat<S1, S2>>;

template <size_t N> struct gen_seq;
template <size_t N> using GenSeq = Invoke<gen_seq<N>>;

template <size_t N> struct gen_seq : Concat<GenSeq<N / 2>, GenSeq<N - N / 2>>
{
};

template <> struct gen_seq<0> : Seq<>
{
};
template <> struct gen_seq<1> : Seq<0>
{
};

template <typename Seq, typename Tuple> struct PopBackHelper;

template <unsigned... N, typename Tuple> struct PopBackHelper<Seq<N...>, Tuple>
{
    template <template <typename... Args> class U>
    using rebind = U<typename std::tuple_element<N, Tuple>::type...>;
};

template <typename... T>
struct PopBack //: public PopBackHelper<typename
               // gen_seq<sizeof...(T)-1>::type, std::tuple<T...>>
{
    template <template <typename... Args> class U>
    using rebind =
        typename PopBackHelper<typename gen_seq<sizeof...(T) - 1UL>::type,
                               std::tuple<T...>>::template rebind<U>;
};

template <> struct PopBack<>
{
    template <template <typename... Args> class U> using rebind = U<>;
};

// from
// http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type
template <typename Tp, typename... List> struct Contains : std::true_type
{
};

template <typename Tp, typename Head, typename... Rest>
struct Contains<Tp, Head, Rest...>
    : std::conditional<std::is_same<Tp, Head>::value, std::true_type,
                       Contains<Tp, Rest...>>::type
{
};

template <typename Tp> struct Contains<Tp> : std::false_type
{
};

template <typename T> struct EmptyContext
{
};

template <typename T> struct promote
{
    using type = T;
};

#define BMCWEB_INTERNAL_PROMOTE_TYPE(t1, t2)                                   \
    template <> struct promote<t1>                                             \
    {                                                                          \
        using type = t2;                                                       \
    }

BMCWEB_INTERNAL_PROMOTE_TYPE(char, int64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(short, int64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(int, int64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(long, int64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(long long, int64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned char, uint64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned short, uint64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned int, uint64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned long, uint64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(unsigned long long, uint64_t);
BMCWEB_INTERNAL_PROMOTE_TYPE(float, double);
#undef BMCWEB_INTERNAL_PROMOTE_TYPE

template <typename T> using promote_t = typename promote<T>::type;

} // namespace black_magic

namespace detail
{

template <class T, std::size_t N, class... Args>
struct GetIndexOfElementFromTupleByTypeImpl
{
    static constexpr std::size_t value = N;
};

template <class T, std::size_t N, class... Args>
struct GetIndexOfElementFromTupleByTypeImpl<T, N, T, Args...>
{
    static constexpr std::size_t value = N;
};

template <class T, std::size_t N, class U, class... Args>
struct GetIndexOfElementFromTupleByTypeImpl<T, N, U, Args...>
{
    static constexpr std::size_t value =
        GetIndexOfElementFromTupleByTypeImpl<T, N + 1, Args...>::value;
};

} // namespace detail

namespace utility
{
template <class T, class... Args> T& getElementByType(std::tuple<Args...>& t)
{
    return std::get<
        detail::GetIndexOfElementFromTupleByTypeImpl<T, 0, Args...>::value>(t);
}

template <typename T> struct function_traits;

template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())>
{
    using parent_t = function_traits<decltype(&T::operator())>;
    static const size_t arity = parent_t::arity;
    using result_type = typename parent_t::result_type;
    template <size_t i> using arg = typename parent_t::template arg<i>;
};

template <typename ClassType, typename r, typename... Args>
struct function_traits<r (ClassType::*)(Args...) const>
{
    static const size_t arity = sizeof...(Args);

    using result_type = r;

    template <size_t i>
    using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
};

template <typename ClassType, typename r, typename... Args>
struct function_traits<r (ClassType::*)(Args...)>
{
    static const size_t arity = sizeof...(Args);

    using result_type = r;

    template <size_t i>
    using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
};

template <typename r, typename... Args>
struct function_traits<std::function<r(Args...)>>
{
    static const size_t arity = sizeof...(Args);

    using result_type = r;

    template <size_t i>
    using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
};

inline static std::string base64encode(
    const char* data, size_t size,
    const char* key =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
{
    std::string ret;
    ret.resize((size + 2) / 3 * 4);
    auto it = ret.begin();
    while (size >= 3)
    {
        *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2];
        unsigned char h = static_cast<unsigned char>(
            (static_cast<unsigned char>(*data++) & 0x03u) << 4u);
        *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xF0) >> 4)];
        h = static_cast<unsigned char>(
            (static_cast<unsigned char>(*data++) & 0x0F) << 2u);
        *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xC0) >> 6)];
        *it++ = key[static_cast<unsigned char>(*data++) & 0x3F];

        size -= 3;
    }
    if (size == 1)
    {
        *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2];
        unsigned char h = static_cast<unsigned char>(
            (static_cast<unsigned char>(*data++) & 0x03) << 4u);
        *it++ = key[h];
        *it++ = '=';
        *it++ = '=';
    }
    else if (size == 2)
    {
        *it++ = key[(static_cast<unsigned char>(*data) & 0xFC) >> 2];
        unsigned char h = static_cast<unsigned char>(
            (static_cast<unsigned char>(*data++) & 0x03) << 4u);
        *it++ = key[h | ((static_cast<unsigned char>(*data) & 0xF0) >> 4)];
        h = static_cast<unsigned char>(
            (static_cast<unsigned char>(*data++) & 0x0F) << 2u);
        *it++ = key[h];
        *it++ = '=';
    }
    return ret;
}

inline static std::string base64encodeUrlsafe(const char* data, size_t size)
{
    return base64encode(
        data, size,
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
}

// TODO this is temporary and should be deleted once base64 is refactored out of
// crow
inline bool base64Decode(const std::string_view input, std::string& output)
{
    static const char nop = static_cast<char>(-1);
    // See note on encoding_data[] in above function
    static const char decodingData[] = {
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, 62,  nop, nop, nop, 63,  52,  53,  54,  55,  56,  57,  58,  59,
        60,  61,  nop, nop, nop, nop, nop, nop, nop, 0,   1,   2,   3,   4,
        5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,  18,
        19,  20,  21,  22,  23,  24,  25,  nop, nop, nop, nop, nop, nop, 26,
        27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
        41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop,
        nop, nop, nop, nop};

    size_t inputLength = input.size();

    // allocate space for output string
    output.clear();
    output.reserve(((inputLength + 2) / 3) * 4);

    // for each 4-bytes sequence from the input, extract 4 6-bits sequences by
    // droping first two bits
    // and regenerate into 3 8-bits sequences

    for (size_t i = 0; i < inputLength; i++)
    {
        char base64code0;
        char base64code1;
        char base64code2 = 0; // initialized to 0 to suppress warnings
        char base64code3;

        base64code0 = decodingData[static_cast<int>(input[i])]; // NOLINT
        if (base64code0 == nop)
        { // non base64 character
            return false;
        }
        if (!(++i < inputLength))
        { // we need at least two input bytes for first
          // byte output
            return false;
        }
        base64code1 = decodingData[static_cast<int>(input[i])]; // NOLINT
        if (base64code1 == nop)
        { // non base64 character
            return false;
        }
        output +=
            static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3));

        if (++i < inputLength)
        {
            char c = input[i];
            if (c == '=')
            { // padding , end of input
                return (base64code1 & 0x0f) == 0;
            }
            base64code2 = decodingData[static_cast<int>(input[i])]; // NOLINT
            if (base64code2 == nop)
            { // non base64 character
                return false;
            }
            output += static_cast<char>(((base64code1 << 4) & 0xf0) |
                                        ((base64code2 >> 2) & 0x0f));
        }

        if (++i < inputLength)
        {
            char c = input[i];
            if (c == '=')
            { // padding , end of input
                return (base64code2 & 0x03) == 0;
            }
            base64code3 = decodingData[static_cast<int>(input[i])]; // NOLINT
            if (base64code3 == nop)
            { // non base64 character
                return false;
            }
            output +=
                static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3));
        }
    }

    return true;
}

inline void escapeHtml(std::string& data)
{
    std::string buffer;
    // less than 5% of characters should be larger, so reserve a buffer of the
    // right size
    buffer.reserve(data.size() * 11 / 10);
    for (size_t pos = 0; pos != data.size(); ++pos)
    {
        switch (data[pos])
        {
            case '&':
                buffer.append("&amp;");
                break;
            case '\"':
                buffer.append("&quot;");
                break;
            case '\'':
                buffer.append("&apos;");
                break;
            case '<':
                buffer.append("&lt;");
                break;
            case '>':
                buffer.append("&gt;");
                break;
            default:
                buffer.append(&data[pos], 1);
                break;
        }
    }
    data.swap(buffer);
}

inline void convertToLinks(std::string& s)
{
    // Convert anything with a redfish path into a link
    const static std::regex redfishPath{
        "(&quot;((.*))&quot;[ \\n]*:[ "
        "\\n]*)(&quot;((?!&quot;)/redfish/.*)&quot;)"};
    s = std::regex_replace(s, redfishPath, "$1<a href=\"$5\">$4</a>");
}

/**
 * Method returns Date Time information according to requested format
 *
 * @param[in] time time in second since the Epoch
 *
 * @return Date Time according to requested format
 */
inline std::string getDateTime(const std::time_t& time)
{
    std::array<char, 128> dateTime;
    std::string redfishDateTime("0000-00-00T00:00:00Z00:00");

    if (std::strftime(dateTime.begin(), dateTime.size(), "%FT%T%z",
                      std::localtime(&time)))
    {
        // insert the colon required by the ISO 8601 standard
        redfishDateTime = std::string(dateTime.data());
        redfishDateTime.insert(redfishDateTime.end() - 2, ':');
    }

    return redfishDateTime;
}

inline std::string dateTimeNow()
{
    std::time_t time = std::time(nullptr);
    return getDateTime(time);
}

inline bool constantTimeStringCompare(const std::string_view a,
                                      const std::string_view b)
{
    // Important note, this function is ONLY constant time if the two input
    // sizes are the same
    if (a.size() != b.size())
    {
        return false;
    }
    return CRYPTO_memcmp(a.data(), b.data(), a.size()) == 0;
}

struct ConstantTimeCompare
{
    bool operator()(const std::string_view a, const std::string_view b) const
    {
        return constantTimeStringCompare(a, b);
    }
};

} // namespace utility
} // namespace crow