@@ -38,6 +38,22 @@ namespace jwt {
38
38
' w' , ' x' , ' y' , ' z' , ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' +' , ' /' }};
39
39
return data;
40
40
}
41
+ static const std::array<int8_t , 256 >& rdata () {
42
+ static constexpr std::array<int8_t , 256 > rdata{{
43
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
44
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 ,
45
+ 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 ,
46
+ 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , -1 ,
47
+ -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 ,
48
+ 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
49
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
50
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
51
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
52
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
53
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
54
+ }};
55
+ return rdata;
56
+ }
41
57
static const std::string& fill () {
42
58
static const std::string fill{" =" };
43
59
return fill;
@@ -61,6 +77,22 @@ namespace jwt {
61
77
' w' , ' x' , ' y' , ' z' , ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' -' , ' _' }};
62
78
return data;
63
79
}
80
+ static const std::array<int8_t , 256 >& rdata () {
81
+ static constexpr std::array<int8_t , 256 > rdata{{
82
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
83
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 ,
84
+ 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 ,
85
+ 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , 63 ,
86
+ -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 ,
87
+ 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
88
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
89
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
90
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
91
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
92
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
93
+ }};
94
+ return rdata;
95
+ }
64
96
static const std::string& fill () {
65
97
static const std::string fill{" %3d" };
66
98
return fill;
@@ -82,18 +114,33 @@ namespace jwt {
82
114
' w' , ' x' , ' y' , ' z' , ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' -' , ' _' }};
83
115
return data;
84
116
}
117
+ static const std::array<int8_t , 256 >& rdata () {
118
+ static constexpr std::array<int8_t , 256 > rdata{{
119
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
120
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 ,
121
+ 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 ,
122
+ 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , 63 ,
123
+ -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 ,
124
+ 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
125
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
126
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
127
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
128
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
129
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
130
+ }};
131
+ return rdata;
132
+ }
85
133
static const std::vector<std::string>& fill () {
86
134
static const std::vector<std::string> fill{" %3D" , " %3d" };
87
135
return fill;
88
136
}
89
137
};
90
138
} // namespace helper
91
139
92
- inline uint32_t index (const std::array<char , 64 >& alphabet, char symbol) {
93
- auto itr = std::find_if (alphabet.cbegin (), alphabet.cend (), [symbol](char c) { return c == symbol; });
94
- if (itr == alphabet.cend ()) { throw std::runtime_error (" Invalid input: not within alphabet" ); }
95
-
96
- return static_cast <uint32_t >(std::distance (alphabet.cbegin (), itr));
140
+ inline uint32_t index (const std::array<int8_t , 256 >& rdata, char symbol) {
141
+ auto index = rdata[static_cast <unsigned char >(symbol)];
142
+ if (index <= -1 ) { throw std::runtime_error (" Invalid input: not within alphabet" ); }
143
+ return static_cast <uint32_t >(index);
97
144
}
98
145
} // namespace alphabet
99
146
@@ -178,7 +225,7 @@ namespace jwt {
178
225
return res;
179
226
}
180
227
181
- inline std::string decode (const std::string& base, const std::array<char , 64 >& alphabet ,
228
+ inline std::string decode (const std::string& base, const std::array<int8_t , 256 >& rdata ,
182
229
const std::vector<std::string>& fill) {
183
230
const auto pad = count_padding (base, fill);
184
231
if (pad.count > 2 ) throw std::runtime_error (" Invalid input: too much fill" );
@@ -190,7 +237,7 @@ namespace jwt {
190
237
std::string res;
191
238
res.reserve (out_size);
192
239
193
- auto get_sextet = [&](size_t offset) { return alphabet::index (alphabet , base[offset]); };
240
+ auto get_sextet = [&](size_t offset) { return alphabet::index (rdata , base[offset]); };
194
241
195
242
size_t fast_size = size - size % 4 ;
196
243
for (size_t i = 0 ; i < fast_size;) {
@@ -224,9 +271,9 @@ namespace jwt {
224
271
return res;
225
272
}
226
273
227
- inline std::string decode (const std::string& base, const std::array<char , 64 >& alphabet ,
274
+ inline std::string decode (const std::string& base, const std::array<int8_t , 256 >& rdata ,
228
275
const std::string& fill) {
229
- return decode (base, alphabet , std::vector<std::string>{fill});
276
+ return decode (base, rdata , std::vector<std::string>{fill});
230
277
}
231
278
232
279
inline std::string pad (const std::string& base, const std::string& fill) {
@@ -273,7 +320,7 @@ namespace jwt {
273
320
*/
274
321
template <typename T>
275
322
std::string decode (const std::string& base) {
276
- return details::decode (base, T::data (), T::fill ());
323
+ return details::decode (base, T::rdata (), T::fill ());
277
324
}
278
325
/* *
279
326
* \brief Generic base64 padding
0 commit comments