Debian Codegraph @

src/mpt/binary/base64url.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BINARY_BASE64URL_HPP
4 #define MPT_BINARY_BASE64URL_HPP
5
6
7
8 #include "mpt/base/integer.hpp"
9 #include "mpt/base/memory.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/string/types.hpp"
12
13 #include <array>
14 #include <stdexcept>
15 #include <vector>
16
17 #include <cstddef>
18
19
20
21 namespace mpt {
22 inline namespace MPT_INLINE_NS {
23
24
25
26 class base64url_parse_error : public std::runtime_error {
27 public:
28 base64url_parse_error()
29 : std::runtime_error("invalid Base64URL encoding") {
30 }
31 };
32
33
34 inline constexpr std::array<mpt::uchar, 64> base64url = {
35 {MPT_UCHAR('A'), MPT_UCHAR('B'), MPT_UCHAR('C'), MPT_UCHAR('D'), MPT_UCHAR('E'), MPT_UCHAR('F'), MPT_UCHAR('G'), MPT_UCHAR('H'), MPT_UCHAR('I'), MPT_UCHAR('J'), MPT_UCHAR('K'), MPT_UCHAR('L'), MPT_UCHAR('M'), MPT_UCHAR('N'), MPT_UCHAR('O'), MPT_UCHAR('P'),
36 MPT_UCHAR('Q'), MPT_UCHAR('R'), MPT_UCHAR('S'), MPT_UCHAR('T'), MPT_UCHAR('U'), MPT_UCHAR('V'), MPT_UCHAR('W'), MPT_UCHAR('X'), MPT_UCHAR('Y'), MPT_UCHAR('Z'), MPT_UCHAR('a'), MPT_UCHAR('b'), MPT_UCHAR('c'), MPT_UCHAR('d'), MPT_UCHAR('e'), MPT_UCHAR('f'),
37 MPT_UCHAR('g'), MPT_UCHAR('h'), MPT_UCHAR('i'), MPT_UCHAR('j'), MPT_UCHAR('k'), MPT_UCHAR('l'), MPT_UCHAR('m'), MPT_UCHAR('n'), MPT_UCHAR('o'), MPT_UCHAR('p'), MPT_UCHAR('q'), MPT_UCHAR('r'), MPT_UCHAR('s'), MPT_UCHAR('t'), MPT_UCHAR('u'), MPT_UCHAR('v'),
38 MPT_UCHAR('w'), MPT_UCHAR('x'), MPT_UCHAR('y'), MPT_UCHAR('z'), MPT_UCHAR('0'), MPT_UCHAR('1'), MPT_UCHAR('2'), MPT_UCHAR('3'), MPT_UCHAR('4'), MPT_UCHAR('5'), MPT_UCHAR('6'), MPT_UCHAR('7'), MPT_UCHAR('8'), MPT_UCHAR('9'), MPT_UCHAR('-'), MPT_UCHAR('_')}
39 };
40
41 template <typename Tbyte>
42 inline mpt::ustring encode_base64url(mpt::span<Tbyte> src_) {
43 mpt::const_byte_span src = mpt::byte_cast<mpt::const_byte_span>(src_);
44 mpt::ustring result;
45 result.reserve(4 * ((src.size() + 2) / 3));
46 uint32 bits = 0;
47 std::size_t bytes = 0;
48 for (std::byte byte : src) {
49 bits <<= 8;
50 bits |= mpt::byte_cast<uint8>(byte);
51 bytes++;
52 if (bytes == 3) {
53 result.push_back(base64url[(bits >> 18) & 0x3f]);
54 result.push_back(base64url[(bits >> 12) & 0x3f]);
55 result.push_back(base64url[(bits >> 6) & 0x3f]);
56 result.push_back(base64url[(bits >> 0) & 0x3f]);
57 bits = 0;
58 bytes = 0;
59 }
60 }
61 std::size_t padding = 0;
62 while (bytes != 0) {
63 bits <<= 8;
64 padding++;
65 bytes++;
66 if (bytes == 3) {
67 result.push_back(base64url[(bits >> 18) & 0x3f]);
68 result.push_back(base64url[(bits >> 12) & 0x3f]);
69 if (padding <= 1) {
70 result.push_back(base64url[(bits >> 6) & 0x3f]);
71 }
72 if (padding <= 0) {
73 result.push_back(base64url[(bits >> 0) & 0x3f]);
74 }
75 bits = 0;
76 bytes = 0;
77 }
78 }
79 return result;
80 }
81
82 inline uint8 decode_base64url_bits(mpt::uchar c) {
83 for (uint8 i = 0; i < 64; ++i)
84 {
85 if (base64url[i] == c)
86 {
87 return i;
88 }
89 }
90 throw base64url_parse_error();
91 }
92
93 inline std::vector<std::byte> decode_base64url(const mpt::ustring & src) {
94 std::vector<std::byte> result;
95 result.reserve(3 * ((src.length() + 2) / 4));
96 uint32 bits = 0;
97 std::size_t chars = 0;
98 for (mpt::uchar c : src) {
99 bits <<= 6;
100 bits |= decode_base64url_bits(c);
101 chars++;
102 if (chars == 4) {
103 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 16) & 0xff)));
104 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 8) & 0xff)));
105 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 0) & 0xff)));
106 bits = 0;
107 chars = 0;
108 }
109 }
110 uint32 padding = 0;
111 if (chars != 0 && chars < 2) {
112 throw base64url_parse_error();
113 }
114 while (chars != 0) {
115 bits <<= 6;
116 padding++;
117 chars++;
118 if (chars == 4) {
119 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 16) & 0xff)));
120 if (padding < 2) {
121 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 8) & 0xff)));
122 }
123 if (padding < 1) {
124 result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 0) & 0xff)));
125 }
126 bits = 0;
127 chars = 0;
128 padding = 0;
129 }
130 }
131 return result;
132 }
133
134
135 } // namespace MPT_INLINE_NS
136 } // namespace mpt
137
138
139
140 #endif // MPT_BINARY_BASE64URL_HPP