| 1 |
/** |
| 2 |
* References and further reading: |
| 3 |
* |
| 4 |
* Wojciech Muła, Daniel Lemire, Base64 encoding and decoding at almost the |
| 5 |
* speed of a memory copy, Software: Practice and Experience 50 (2), 2020. |
| 6 |
* https://arxiv.org/abs/1910.05109 |
| 7 |
* |
| 8 |
* Wojciech Muła, Daniel Lemire, Faster Base64 Encoding and Decoding using AVX2 |
| 9 |
* Instructions, ACM Transactions on the Web 12 (3), 2018. |
| 10 |
* https://arxiv.org/abs/1704.00605 |
| 11 |
* |
| 12 |
* Simon Josefsson. 2006. The Base16, Base32, and Base64 Data Encodings. |
| 13 |
* https://tools.ietf.org/html/rfc4648. (2006). Internet Engineering Task Force, |
| 14 |
* Request for Comments: 4648. |
| 15 |
* |
| 16 |
* Alfred Klomp. 2014a. Fast Base64 encoding/decoding with SSE vectorization. |
| 17 |
* http://www.alfredklomp.com/programming/sse-base64/. (2014). |
| 18 |
* |
| 19 |
* Alfred Klomp. 2014b. Fast Base64 stream encoder/decoder in C99, with SIMD |
| 20 |
* acceleration. https://github.com/aklomp/base64. (2014). |
| 21 |
* |
| 22 |
* Hanson Char. 2014. A Fast and Correct Base 64 Codec. (2014). |
| 23 |
* https://aws.amazon.com/blogs/developer/a-fast-and-correct-base-64-codec/ |
| 24 |
* |
| 25 |
* Nick Kopp. 2013. Base64 Encoding on a GPU. |
| 26 |
* https://www.codeproject.com/Articles/276993/Base-Encoding-on-a-GPU. (2013). |
| 27 |
*/ |
| 28 |
|
| 29 |
template <bool isbase64url> |
| 30 |
size_t encode_base64(char *dst, const char *src, size_t srclen, |
| 31 |
base64_options options) { |
| 32 |
// credit: Wojciech Muła |
| 33 |
// SSE (lookup: pshufb improved unrolled) |
| 34 |
const uint8_t *input = (const uint8_t *)src; |
| 35 |
static const char *lookup_tbl = |
| 36 |
isbase64url |
| 37 |
? "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" |
| 38 |
: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 39 |
uint8_t *out = (uint8_t *)dst; |
| 40 |
|
| 41 |
v16u8 shuf; |
| 42 |
__m128i v_fc0fc00, v_3f03f0, shift_r, shift_l, base64_tbl0, base64_tbl1, |
| 43 |
base64_tbl2, base64_tbl3; |
| 44 |
if (srclen >= 16) { |
| 45 |
shuf = v16u8{1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10}; |
| 46 |
v_fc0fc00 = __lsx_vreplgr2vr_w(uint32_t(0x0fc0fc00)); |
| 47 |
v_3f03f0 = __lsx_vreplgr2vr_w(uint32_t(0x003f03f0)); |
| 48 |
shift_r = __lsx_vreplgr2vr_w(uint32_t(0x0006000a)); |
| 49 |
shift_l = __lsx_vreplgr2vr_w(uint32_t(0x00080004)); |
| 50 |
base64_tbl0 = __lsx_vld(lookup_tbl, 0); |
| 51 |
base64_tbl1 = __lsx_vld(lookup_tbl, 16); |
| 52 |
base64_tbl2 = __lsx_vld(lookup_tbl, 32); |
| 53 |
base64_tbl3 = __lsx_vld(lookup_tbl, 48); |
| 54 |
} |
| 55 |
|
| 56 |
size_t i = 0; |
| 57 |
for (; i + 52 <= srclen; i += 48) { |
| 58 |
__m128i in0 = |
| 59 |
__lsx_vld(reinterpret_cast<const __m128i *>(input + i), 4 * 3 * 0); |
| 60 |
__m128i in1 = |
| 61 |
__lsx_vld(reinterpret_cast<const __m128i *>(input + i), 4 * 3 * 1); |
| 62 |
__m128i in2 = |
| 63 |
__lsx_vld(reinterpret_cast<const __m128i *>(input + i), 4 * 3 * 2); |
| 64 |
__m128i in3 = |
| 65 |
__lsx_vld(reinterpret_cast<const __m128i *>(input + i), 4 * 3 * 3); |
| 66 |
|
| 67 |
in0 = __lsx_vshuf_b(in0, in0, (__m128i)shuf); |
| 68 |
in1 = __lsx_vshuf_b(in1, in1, (__m128i)shuf); |
| 69 |
in2 = __lsx_vshuf_b(in2, in2, (__m128i)shuf); |
| 70 |
in3 = __lsx_vshuf_b(in3, in3, (__m128i)shuf); |
| 71 |
|
| 72 |
__m128i t0_0 = __lsx_vand_v(in0, v_fc0fc00); |
| 73 |
__m128i t0_1 = __lsx_vand_v(in1, v_fc0fc00); |
| 74 |
__m128i t0_2 = __lsx_vand_v(in2, v_fc0fc00); |
| 75 |
__m128i t0_3 = __lsx_vand_v(in3, v_fc0fc00); |
| 76 |
|
| 77 |
__m128i t1_0 = __lsx_vsrl_h(t0_0, shift_r); |
| 78 |
__m128i t1_1 = __lsx_vsrl_h(t0_1, shift_r); |
| 79 |
__m128i t1_2 = __lsx_vsrl_h(t0_2, shift_r); |
| 80 |
__m128i t1_3 = __lsx_vsrl_h(t0_3, shift_r); |
| 81 |
|
| 82 |
__m128i t2_0 = __lsx_vand_v(in0, v_3f03f0); |
| 83 |
__m128i t2_1 = __lsx_vand_v(in1, v_3f03f0); |
| 84 |
__m128i t2_2 = __lsx_vand_v(in2, v_3f03f0); |
| 85 |
__m128i t2_3 = __lsx_vand_v(in3, v_3f03f0); |
| 86 |
|
| 87 |
__m128i t3_0 = __lsx_vsll_h(t2_0, shift_l); |
| 88 |
__m128i t3_1 = __lsx_vsll_h(t2_1, shift_l); |
| 89 |
__m128i t3_2 = __lsx_vsll_h(t2_2, shift_l); |
| 90 |
__m128i t3_3 = __lsx_vsll_h(t2_3, shift_l); |
| 91 |
|
| 92 |
__m128i input0 = __lsx_vor_v(t1_0, t3_0); |
| 93 |
__m128i input0_shuf0 = __lsx_vshuf_b(base64_tbl1, base64_tbl0, input0); |
| 94 |
__m128i input0_shuf1 = __lsx_vshuf_b(base64_tbl3, base64_tbl2, |
| 95 |
__lsx_vsub_b(input0, __lsx_vldi(32))); |
| 96 |
__m128i input0_mask = __lsx_vslei_bu(input0, 31); |
| 97 |
__m128i input0_result = |
| 98 |
__lsx_vbitsel_v(input0_shuf1, input0_shuf0, input0_mask); |
| 99 |
__lsx_vst(input0_result, reinterpret_cast<__m128i *>(out), 0); |
| 100 |
out += 16; |
| 101 |
|
| 102 |
__m128i input1 = __lsx_vor_v(t1_1, t3_1); |
| 103 |
__m128i input1_shuf0 = __lsx_vshuf_b(base64_tbl1, base64_tbl0, input1); |
| 104 |
__m128i input1_shuf1 = __lsx_vshuf_b(base64_tbl3, base64_tbl2, |
| 105 |
__lsx_vsub_b(input1, __lsx_vldi(32))); |
| 106 |
__m128i input1_mask = __lsx_vslei_bu(input1, 31); |
| 107 |
__m128i input1_result = |
| 108 |
__lsx_vbitsel_v(input1_shuf1, input1_shuf0, input1_mask); |
| 109 |
__lsx_vst(input1_result, reinterpret_cast<__m128i *>(out), 0); |
| 110 |
out += 16; |
| 111 |
|
| 112 |
__m128i input2 = __lsx_vor_v(t1_2, t3_2); |
| 113 |
__m128i input2_shuf0 = __lsx_vshuf_b(base64_tbl1, base64_tbl0, input2); |
| 114 |
__m128i input2_shuf1 = __lsx_vshuf_b(base64_tbl3, base64_tbl2, |
| 115 |
__lsx_vsub_b(input2, __lsx_vldi(32))); |
| 116 |
__m128i input2_mask = __lsx_vslei_bu(input2, 31); |
| 117 |
__m128i input2_result = |
| 118 |
__lsx_vbitsel_v(input2_shuf1, input2_shuf0, input2_mask); |
| 119 |
__lsx_vst(input2_result, reinterpret_cast<__m128i *>(out), 0); |
| 120 |
out += 16; |
| 121 |
|
| 122 |
__m128i input3 = __lsx_vor_v(t1_3, t3_3); |
| 123 |
__m128i input3_shuf0 = __lsx_vshuf_b(base64_tbl1, base64_tbl0, input3); |
| 124 |
__m128i input3_shuf1 = __lsx_vshuf_b(base64_tbl3, base64_tbl2, |
| 125 |
__lsx_vsub_b(input3, __lsx_vldi(32))); |
| 126 |
__m128i input3_mask = __lsx_vslei_bu(input3, 31); |
| 127 |
__m128i input3_result = |
| 128 |
__lsx_vbitsel_v(input3_shuf1, input3_shuf0, input3_mask); |
| 129 |
__lsx_vst(input3_result, reinterpret_cast<__m128i *>(out), 0); |
| 130 |
out += 16; |
| 131 |
} |
| 132 |
for (; i + 16 <= srclen; i += 12) { |
| 133 |
|
| 134 |
__m128i in = __lsx_vld(reinterpret_cast<const __m128i *>(input + i), 0); |
| 135 |
|
| 136 |
// bytes from groups A, B and C are needed in separate 32-bit lanes |
| 137 |
// in = [DDDD|CCCC|BBBB|AAAA] |
| 138 |
// |
| 139 |
// an input triplet has layout |
| 140 |
// [????????|ccdddddd|bbbbcccc|aaaaaabb] |
| 141 |
// byte 3 byte 2 byte 1 byte 0 -- byte 3 comes from the next |
| 142 |
// triplet |
| 143 |
// |
| 144 |
// shuffling changes the order of bytes: 1, 0, 2, 1 |
| 145 |
// [bbbbcccc|ccdddddd|aaaaaabb|bbbbcccc] |
| 146 |
// ^^^^ ^^^^^^^^ ^^^^^^^^ ^^^^ |
| 147 |
// processed bits |
| 148 |
in = __lsx_vshuf_b(in, in, (__m128i)shuf); |
| 149 |
|
| 150 |
// unpacking |
| 151 |
// t0 = [0000cccc|cc000000|aaaaaa00|00000000] |
| 152 |
__m128i t0 = __lsx_vand_v(in, v_fc0fc00); |
| 153 |
// t1 = [00000000|00cccccc|00000000|00aaaaaa] |
| 154 |
// ((c >> 6), (a >> 10)) |
| 155 |
__m128i t1 = __lsx_vsrl_h(t0, shift_r); |
| 156 |
|
| 157 |
// t2 = [00000000|00dddddd|000000bb|bbbb0000] |
| 158 |
__m128i t2 = __lsx_vand_v(in, v_3f03f0); |
| 159 |
// t3 = [00dddddd|00000000|00bbbbbb|00000000] |
| 160 |
// ((d << 8), (b << 4)) |
| 161 |
__m128i t3 = __lsx_vsll_h(t2, shift_l); |
| 162 |
|
| 163 |
// res = [00dddddd|00cccccc|00bbbbbb|00aaaaaa] = t1 | t3 |
| 164 |
__m128i indices = __lsx_vor_v(t1, t3); |
| 165 |
|
| 166 |
__m128i indices_shuf0 = __lsx_vshuf_b(base64_tbl1, base64_tbl0, indices); |
| 167 |
__m128i indices_shuf1 = __lsx_vshuf_b( |
| 168 |
base64_tbl3, base64_tbl2, __lsx_vsub_b(indices, __lsx_vldi(32))); |
| 169 |
__m128i indices_mask = __lsx_vslei_bu(indices, 31); |
| 170 |
__m128i indices_result = |
| 171 |
__lsx_vbitsel_v(indices_shuf1, indices_shuf0, indices_mask); |
| 172 |
|
| 173 |
__lsx_vst(indices_result, reinterpret_cast<__m128i *>(out), 0); |
| 174 |
out += 16; |
| 175 |
} |
| 176 |
|
| 177 |
return i / 3 * 4 + scalar::base64::tail_encode_base64((char *)out, src + i, |
| 178 |
srclen - i, options); |
| 179 |
} |
| 180 |
|
| 181 |
static inline void compress(__m128i data, uint16_t mask, char *output) { |
| 182 |
if (mask == 0) { |
| 183 |
__lsx_vst(data, reinterpret_cast<__m128i *>(output), 0); |
| 184 |
return; |
| 185 |
} |
| 186 |
// this particular implementation was inspired by work done by @animetosho |
| 187 |
// we do it in two steps, first 8 bytes and then second 8 bytes |
| 188 |
uint8_t mask1 = uint8_t(mask); // least significant 8 bits |
| 189 |
uint8_t mask2 = uint8_t(mask >> 8); // most significant 8 bits |
| 190 |
// next line just loads the 64-bit values thintable_epi8[mask1] and |
| 191 |
// thintable_epi8[mask2] into a 128-bit register, using only |
| 192 |
// two instructions on most compilers. |
| 193 |
|
| 194 |
v2u64 shufmask = {tables::base64::thintable_epi8[mask1], |
| 195 |
tables::base64::thintable_epi8[mask2]}; |
| 196 |
|
| 197 |
// we increment by 0x08 the second half of the mask |
| 198 |
v4u32 hi = {0, 0, 0x08080808, 0x08080808}; |
| 199 |
__m128i shufmask1 = __lsx_vadd_b((__m128i)shufmask, (__m128i)hi); |
| 200 |
|
| 201 |
// this is the version "nearly pruned" |
| 202 |
__m128i pruned = __lsx_vshuf_b(data, data, shufmask1); |
| 203 |
// we still need to put the two halves together. |
| 204 |
// we compute the popcount of the first half: |
| 205 |
int pop1 = tables::base64::BitsSetTable256mul2[mask1]; |
| 206 |
// then load the corresponding mask, what it does is to write |
| 207 |
// only the first pop1 bytes from the first 8 bytes, and then |
| 208 |
// it fills in with the bytes from the second 8 bytes + some filling |
| 209 |
// at the end. |
| 210 |
__m128i compactmask = |
| 211 |
__lsx_vld(reinterpret_cast<const __m128i *>( |
| 212 |
tables::base64::pshufb_combine_table + pop1 * 8), |
| 213 |
0); |
| 214 |
__m128i answer = __lsx_vshuf_b(pruned, pruned, compactmask); |
| 215 |
|
| 216 |
__lsx_vst(answer, reinterpret_cast<__m128i *>(output), 0); |
| 217 |
} |
| 218 |
|
| 219 |
struct block64 { |
| 220 |
__m128i chunks[4]; |
| 221 |
}; |
| 222 |
|
| 223 |
template <bool base64_url> |
| 224 |
static inline uint16_t to_base64_mask(__m128i *src, bool *error) { |
| 225 |
const v16u8 ascii_space_tbl = {0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 226 |
0x0, 0x9, 0xa, 0x0, 0xc, 0xd, 0x0, 0x0}; |
| 227 |
// credit: aqrit |
| 228 |
/* |
| 229 |
'0'(0x30)-'9'(0x39) => delta_values_index = 4 |
| 230 |
'A'(0x41)-'Z'(0x5a) => delta_values_index = 4/5/12(4+8) |
| 231 |
'a'(0x61)-'z'(0x7a) => delta_values_index = 6/7/14(6+8) |
| 232 |
'+'(0x2b) => delta_values_index = 3 |
| 233 |
'/'(0x2f) => delta_values_index = 2+8 = 10 |
| 234 |
'-'(0x2d) => delta_values_index = 2+8 = 10 |
| 235 |
'_'(0x5f) => delta_values_index = 5+8 = 13 |
| 236 |
*/ |
| 237 |
v16u8 delta_asso = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, |
| 238 |
0x0, 0x0, 0x0, 0x0, 0x0, 0xF, 0x0, 0xF}; |
| 239 |
v16i8 delta_values; |
| 240 |
if (base64_url) { |
| 241 |
delta_values = |
| 242 |
v16i8{int8_t(0x00), int8_t(0x00), int8_t(0x00), int8_t(0x13), |
| 243 |
int8_t(0x04), int8_t(0xBF), int8_t(0xBF), int8_t(0xB9), |
| 244 |
int8_t(0xB9), int8_t(0x00), int8_t(0x11), int8_t(0xC3), |
| 245 |
int8_t(0xBF), int8_t(0xE0), int8_t(0xB9), int8_t(0xB9)}; |
| 246 |
} else { |
| 247 |
delta_values = |
| 248 |
v16i8{int8_t(0x00), int8_t(0x00), int8_t(0x00), int8_t(0x13), |
| 249 |
int8_t(0x04), int8_t(0xBF), int8_t(0xBF), int8_t(0xB9), |
| 250 |
int8_t(0xB9), int8_t(0x00), int8_t(0x10), int8_t(0xC3), |
| 251 |
int8_t(0xBF), int8_t(0xBF), int8_t(0xB9), int8_t(0xB9)}; |
| 252 |
} |
| 253 |
|
| 254 |
v16u8 check_asso; |
| 255 |
if (base64_url) { |
| 256 |
check_asso = v16u8{0x0D, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 257 |
0x01, 0x01, 0x03, 0x07, 0x0B, 0x06, 0x0B, 0x12}; |
| 258 |
} else { |
| 259 |
check_asso = v16u8{0x0D, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 260 |
0x01, 0x01, 0x03, 0x07, 0x0B, 0x0B, 0x0B, 0x0F}; |
| 261 |
} |
| 262 |
|
| 263 |
v16i8 check_values; |
| 264 |
if (base64_url) { |
| 265 |
check_values = v16i8{int8_t(0x0), int8_t(0x80), int8_t(0x80), int8_t(0x80), |
| 266 |
int8_t(0xCF), int8_t(0xBF), int8_t(0xD3), int8_t(0xA6), |
| 267 |
int8_t(0xB5), int8_t(0x86), int8_t(0xD0), int8_t(0x80), |
| 268 |
int8_t(0xB0), int8_t(0x80), int8_t(0x0), int8_t(0x0)}; |
| 269 |
} else { |
| 270 |
check_values = |
| 271 |
v16i8{int8_t(0x80), int8_t(0x80), int8_t(0x80), int8_t(0x80), |
| 272 |
int8_t(0xCF), int8_t(0xBF), int8_t(0xD5), int8_t(0xA6), |
| 273 |
int8_t(0xB5), int8_t(0x86), int8_t(0xD1), int8_t(0x80), |
| 274 |
int8_t(0xB1), int8_t(0x80), int8_t(0x91), int8_t(0x80)}; |
| 275 |
} |
| 276 |
|
| 277 |
const __m128i shifted = __lsx_vsrli_b(*src, 3); |
| 278 |
__m128i asso_index = __lsx_vand_v(*src, __lsx_vldi(0xF)); |
| 279 |
const __m128i delta_hash = |
| 280 |
__lsx_vavgr_bu(__lsx_vshuf_b((__m128i)delta_asso, (__m128i)delta_asso, |
| 281 |
(__m128i)asso_index), |
| 282 |
shifted); |
| 283 |
const __m128i check_hash = |
| 284 |
__lsx_vavgr_bu(__lsx_vshuf_b((__m128i)check_asso, (__m128i)check_asso, |
| 285 |
(__m128i)asso_index), |
| 286 |
shifted); |
| 287 |
|
| 288 |
const __m128i out = |
| 289 |
__lsx_vsadd_b(__lsx_vshuf_b((__m128i)delta_values, (__m128i)delta_values, |
| 290 |
(__m128i)delta_hash), |
| 291 |
*src); |
| 292 |
const __m128i chk = |
| 293 |
__lsx_vsadd_b(__lsx_vshuf_b((__m128i)check_values, (__m128i)check_values, |
| 294 |
(__m128i)check_hash), |
| 295 |
*src); |
| 296 |
unsigned int mask = __lsx_vpickve2gr_hu(__lsx_vmskltz_b(chk), 0); |
| 297 |
if (mask) { |
| 298 |
__m128i ascii_space = __lsx_vseq_b(__lsx_vshuf_b((__m128i)ascii_space_tbl, |
| 299 |
(__m128i)ascii_space_tbl, |
| 300 |
(__m128i)asso_index), |
| 301 |
*src); |
| 302 |
*error |= |
| 303 |
(mask != __lsx_vpickve2gr_hu(__lsx_vmskltz_b((__m128i)ascii_space), 0)); |
| 304 |
} |
| 305 |
|
| 306 |
*src = out; |
| 307 |
return (uint16_t)mask; |
| 308 |
} |
| 309 |
|
| 310 |
template <bool base64_url> |
| 311 |
static inline uint64_t to_base64_mask(block64 *b, bool *error) { |
| 312 |
*error = 0; |
| 313 |
uint64_t m0 = to_base64_mask<base64_url>(&b->chunks[0], error); |
| 314 |
uint64_t m1 = to_base64_mask<base64_url>(&b->chunks[1], error); |
| 315 |
uint64_t m2 = to_base64_mask<base64_url>(&b->chunks[2], error); |
| 316 |
uint64_t m3 = to_base64_mask<base64_url>(&b->chunks[3], error); |
| 317 |
return m0 | (m1 << 16) | (m2 << 32) | (m3 << 48); |
| 318 |
} |
| 319 |
|
| 320 |
static inline void copy_block(block64 *b, char *output) { |
| 321 |
__lsx_vst(b->chunks[0], reinterpret_cast<__m128i *>(output), 0); |
| 322 |
__lsx_vst(b->chunks[1], reinterpret_cast<__m128i *>(output), 16); |
| 323 |
__lsx_vst(b->chunks[2], reinterpret_cast<__m128i *>(output), 32); |
| 324 |
__lsx_vst(b->chunks[3], reinterpret_cast<__m128i *>(output), 48); |
| 325 |
} |
| 326 |
|
| 327 |
static inline uint64_t compress_block(block64 *b, uint64_t mask, char *output) { |
| 328 |
uint64_t nmask = ~mask; |
| 329 |
uint64_t count = |
| 330 |
__lsx_vpickve2gr_d(__lsx_vpcnt_h(__lsx_vreplgr2vr_d(nmask)), 0); |
| 331 |
uint16_t *count_ptr = (uint16_t *)&count; |
| 332 |
compress(b->chunks[0], uint16_t(mask), output); |
| 333 |
compress(b->chunks[1], uint16_t(mask >> 16), output + count_ptr[0]); |
| 334 |
compress(b->chunks[2], uint16_t(mask >> 32), |
| 335 |
output + count_ptr[0] + count_ptr[1]); |
| 336 |
compress(b->chunks[3], uint16_t(mask >> 48), |
| 337 |
output + count_ptr[0] + count_ptr[1] + count_ptr[2]); |
| 338 |
return count_ones(nmask); |
| 339 |
} |
| 340 |
|
| 341 |
// The caller of this function is responsible to ensure that there are 64 bytes |
| 342 |
// available from reading at src. The data is read into a block64 structure. |
| 343 |
static inline void load_block(block64 *b, const char *src) { |
| 344 |
b->chunks[0] = __lsx_vld(reinterpret_cast<const __m128i *>(src), 0); |
| 345 |
b->chunks[1] = __lsx_vld(reinterpret_cast<const __m128i *>(src), 16); |
| 346 |
b->chunks[2] = __lsx_vld(reinterpret_cast<const __m128i *>(src), 32); |
| 347 |
b->chunks[3] = __lsx_vld(reinterpret_cast<const __m128i *>(src), 48); |
| 348 |
} |
| 349 |
|
| 350 |
// The caller of this function is responsible to ensure that there are 128 bytes |
| 351 |
// available from reading at src. The data is read into a block64 structure. |
| 352 |
static inline void load_block(block64 *b, const char16_t *src) { |
| 353 |
__m128i m1 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 0); |
| 354 |
__m128i m2 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 16); |
| 355 |
__m128i m3 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 32); |
| 356 |
__m128i m4 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 48); |
| 357 |
__m128i m5 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 64); |
| 358 |
__m128i m6 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 80); |
| 359 |
__m128i m7 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 96); |
| 360 |
__m128i m8 = __lsx_vld(reinterpret_cast<const __m128i *>(src), 112); |
| 361 |
b->chunks[0] = __lsx_vssrlni_bu_h(m2, m1, 0); |
| 362 |
b->chunks[1] = __lsx_vssrlni_bu_h(m4, m3, 0); |
| 363 |
b->chunks[2] = __lsx_vssrlni_bu_h(m6, m5, 0); |
| 364 |
b->chunks[3] = __lsx_vssrlni_bu_h(m8, m7, 0); |
| 365 |
} |
| 366 |
|
| 367 |
static inline void base64_decode(char *out, __m128i str) { |
| 368 |
__m128i t0 = __lsx_vor_v( |
| 369 |
__lsx_vslli_w(str, 26), |
| 370 |
__lsx_vslli_w(__lsx_vand_v(str, __lsx_vldi(-1758 /*0x0000FF00*/)), 12)); |
| 371 |
__m128i t1 = |
| 372 |
__lsx_vsrli_w(__lsx_vand_v(str, __lsx_vldi(-3521 /*0x003F0000*/)), 2); |
| 373 |
__m128i t2 = __lsx_vor_v(t0, t1); |
| 374 |
__m128i t3 = __lsx_vor_v(t2, __lsx_vsrli_w(str, 16)); |
| 375 |
const v16u8 pack_shuffle = {3, 2, 1, 7, 6, 5, 11, 10, |
| 376 |
9, 15, 14, 13, 0, 0, 0, 0}; |
| 377 |
t3 = __lsx_vshuf_b(t3, t3, (__m128i)pack_shuffle); |
| 378 |
|
| 379 |
// Store the output: |
| 380 |
// we only need 12. |
| 381 |
__lsx_vstelm_d(t3, out, 0, 0); |
| 382 |
__lsx_vstelm_w(t3, out + 8, 0, 2); |
| 383 |
} |
| 384 |
// decode 64 bytes and output 48 bytes |
| 385 |
static inline void base64_decode_block(char *out, const char *src) { |
| 386 |
base64_decode(out, __lsx_vld(reinterpret_cast<const __m128i *>(src), 0)); |
| 387 |
base64_decode(out + 12, |
| 388 |
__lsx_vld(reinterpret_cast<const __m128i *>(src), 16)); |
| 389 |
base64_decode(out + 24, |
| 390 |
__lsx_vld(reinterpret_cast<const __m128i *>(src), 32)); |
| 391 |
base64_decode(out + 36, |
| 392 |
__lsx_vld(reinterpret_cast<const __m128i *>(src), 48)); |
| 393 |
} |
| 394 |
static inline void base64_decode_block_safe(char *out, const char *src) { |
| 395 |
base64_decode_block(out, src); |
| 396 |
} |
| 397 |
static inline void base64_decode_block(char *out, block64 *b) { |
| 398 |
base64_decode(out, b->chunks[0]); |
| 399 |
base64_decode(out + 12, b->chunks[1]); |
| 400 |
base64_decode(out + 24, b->chunks[2]); |
| 401 |
base64_decode(out + 36, b->chunks[3]); |
| 402 |
} |
| 403 |
static inline void base64_decode_block_safe(char *out, block64 *b) { |
| 404 |
base64_decode_block(out, b); |
| 405 |
} |
| 406 |
|
| 407 |
template <bool base64_url, typename char_type> |
| 408 |
full_result |
| 409 |
compress_decode_base64(char *dst, const char_type *src, size_t srclen, |
| 410 |
base64_options options, |
| 411 |
last_chunk_handling_options last_chunk_options) { |
| 412 |
const uint8_t *to_base64 = base64_url ? tables::base64::to_base64_url_value |
| 413 |
: tables::base64::to_base64_value; |
| 414 |
size_t equallocation = |
| 415 |
srclen; // location of the first padding character if any |
| 416 |
// skip trailing spaces |
| 417 |
while (srclen > 0 && scalar::base64::is_eight_byte(src[srclen - 1]) && |
| 418 |
to_base64[uint8_t(src[srclen - 1])] == 64) { |
| 419 |
srclen--; |
| 420 |
} |
| 421 |
size_t equalsigns = 0; |
| 422 |
if (srclen > 0 && src[srclen - 1] == '=') { |
| 423 |
equallocation = srclen - 1; |
| 424 |
srclen--; |
| 425 |
equalsigns = 1; |
| 426 |
// skip trailing spaces |
| 427 |
while (srclen > 0 && scalar::base64::is_eight_byte(src[srclen - 1]) && |
| 428 |
to_base64[uint8_t(src[srclen - 1])] == 64) { |
| 429 |
srclen--; |
| 430 |
} |
| 431 |
if (srclen > 0 && src[srclen - 1] == '=') { |
| 432 |
equallocation = srclen - 1; |
| 433 |
srclen--; |
| 434 |
equalsigns = 2; |
| 435 |
} |
| 436 |
} |
| 437 |
if (srclen == 0) { |
| 438 |
if (equalsigns > 0) { |
| 439 |
return {INVALID_BASE64_CHARACTER, equallocation, 0}; |
| 440 |
} |
| 441 |
return {SUCCESS, 0, 0}; |
| 442 |
} |
| 443 |
const char_type *const srcinit = src; |
| 444 |
const char *const dstinit = dst; |
| 445 |
const char_type *const srcend = src + srclen; |
| 446 |
|
| 447 |
constexpr size_t block_size = 10; |
| 448 |
char buffer[block_size * 64]; |
| 449 |
char *bufferptr = buffer; |
| 450 |
if (srclen >= 64) { |
| 451 |
const char_type *const srcend64 = src + srclen - 64; |
| 452 |
while (src <= srcend64) { |
| 453 |
block64 b; |
| 454 |
load_block(&b, src); |
| 455 |
src += 64; |
| 456 |
bool error = false; |
| 457 |
uint64_t badcharmask = to_base64_mask<base64_url>(&b, &error); |
| 458 |
if (badcharmask) { |
| 459 |
if (error) { |
| 460 |
src -= 64; |
| 461 |
while (src < srcend && scalar::base64::is_eight_byte(*src) && |
| 462 |
to_base64[uint8_t(*src)] <= 64) { |
| 463 |
src++; |
| 464 |
} |
| 465 |
if (src < srcend) { |
| 466 |
// should never happen |
| 467 |
} |
| 468 |
return {error_code::INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
| 469 |
size_t(dst - dstinit)}; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
if (badcharmask != 0) { |
| 474 |
// optimization opportunity: check for simple masks like those made of |
| 475 |
// continuous 1s followed by continuous 0s. And masks containing a |
| 476 |
// single bad character. |
| 477 |
bufferptr += compress_block(&b, badcharmask, bufferptr); |
| 478 |
} else { |
| 479 |
// optimization opportunity: if bufferptr == buffer and mask == 0, we |
| 480 |
// can avoid the call to compress_block and decode directly. |
| 481 |
copy_block(&b, bufferptr); |
| 482 |
bufferptr += 64; |
| 483 |
} |
| 484 |
if (bufferptr >= (block_size - 1) * 64 + buffer) { |
| 485 |
for (size_t i = 0; i < (block_size - 1); i++) { |
| 486 |
base64_decode_block(dst, buffer + i * 64); |
| 487 |
dst += 48; |
| 488 |
} |
| 489 |
std::memcpy(buffer, buffer + (block_size - 1) * 64, |
| 490 |
64); // 64 might be too much |
| 491 |
bufferptr -= (block_size - 1) * 64; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
char *buffer_start = buffer; |
| 496 |
// Optimization note: if this is almost full, then it is worth our |
| 497 |
// time, otherwise, we should just decode directly. |
| 498 |
int last_block = (int)((bufferptr - buffer_start) % 64); |
| 499 |
if (last_block != 0 && srcend - src + last_block >= 64) { |
| 500 |
while ((bufferptr - buffer_start) % 64 != 0 && src < srcend) { |
| 501 |
uint8_t val = to_base64[uint8_t(*src)]; |
| 502 |
*bufferptr = char(val); |
| 503 |
if (!scalar::base64::is_eight_byte(*src) || val > 64) { |
| 504 |
return {error_code::INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
| 505 |
size_t(dst - dstinit)}; |
| 506 |
} |
| 507 |
bufferptr += (val <= 63); |
| 508 |
src++; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
for (; buffer_start + 64 <= bufferptr; buffer_start += 64) { |
| 513 |
base64_decode_block(dst, buffer_start); |
| 514 |
dst += 48; |
| 515 |
} |
| 516 |
if ((bufferptr - buffer_start) % 64 != 0) { |
| 517 |
while (buffer_start + 4 < bufferptr) { |
| 518 |
uint32_t triple = ((uint32_t(uint8_t(buffer_start[0])) << 3 * 6) + |
| 519 |
(uint32_t(uint8_t(buffer_start[1])) << 2 * 6) + |
| 520 |
(uint32_t(uint8_t(buffer_start[2])) << 1 * 6) + |
| 521 |
(uint32_t(uint8_t(buffer_start[3])) << 0 * 6)) |
| 522 |
<< 8; |
| 523 |
triple = scalar::utf32::swap_bytes(triple); |
| 524 |
std::memcpy(dst, &triple, 4); |
| 525 |
|
| 526 |
dst += 3; |
| 527 |
buffer_start += 4; |
| 528 |
} |
| 529 |
if (buffer_start + 4 <= bufferptr) { |
| 530 |
uint32_t triple = ((uint32_t(uint8_t(buffer_start[0])) << 3 * 6) + |
| 531 |
(uint32_t(uint8_t(buffer_start[1])) << 2 * 6) + |
| 532 |
(uint32_t(uint8_t(buffer_start[2])) << 1 * 6) + |
| 533 |
(uint32_t(uint8_t(buffer_start[3])) << 0 * 6)) |
| 534 |
<< 8; |
| 535 |
triple = scalar::utf32::swap_bytes(triple); |
| 536 |
std::memcpy(dst, &triple, 3); |
| 537 |
|
| 538 |
dst += 3; |
| 539 |
buffer_start += 4; |
| 540 |
} |
| 541 |
// we may have 1, 2 or 3 bytes left and we need to decode them so let us |
| 542 |
// backtrack |
| 543 |
int leftover = int(bufferptr - buffer_start); |
| 544 |
while (leftover > 0) { |
| 545 |
while (to_base64[uint8_t(*(src - 1))] == 64) { |
| 546 |
src--; |
| 547 |
} |
| 548 |
src--; |
| 549 |
leftover--; |
| 550 |
} |
| 551 |
} |
| 552 |
if (src < srcend + equalsigns) { |
| 553 |
full_result r = scalar::base64::base64_tail_decode( |
| 554 |
dst, src, srcend - src, equalsigns, options, last_chunk_options); |
| 555 |
r.input_count += size_t(src - srcinit); |
| 556 |
if (r.error == error_code::INVALID_BASE64_CHARACTER || |
| 557 |
r.error == error_code::BASE64_EXTRA_BITS) { |
| 558 |
return r; |
| 559 |
} else { |
| 560 |
r.output_count += size_t(dst - dstinit); |
| 561 |
} |
| 562 |
if (last_chunk_options != stop_before_partial && |
| 563 |
r.error == error_code::SUCCESS && equalsigns > 0) { |
| 564 |
// additional checks |
| 565 |
if ((r.output_count % 3 == 0) || |
| 566 |
((r.output_count % 3) + 1 + equalsigns != 4)) { |
| 567 |
r.error = error_code::INVALID_BASE64_CHARACTER; |
| 568 |
r.input_count = equallocation; |
| 569 |
} |
| 570 |
} |
| 571 |
return r; |
| 572 |
} |
| 573 |
if (equalsigns > 0) { |
| 574 |
if ((size_t(dst - dstinit) % 3 == 0) || |
| 575 |
((size_t(dst - dstinit) % 3) + 1 + equalsigns != 4)) { |
| 576 |
return {INVALID_BASE64_CHARACTER, equallocation, size_t(dst - dstinit)}; |
| 577 |
} |
| 578 |
} |
| 579 |
return {SUCCESS, srclen, size_t(dst - dstinit)}; |
| 580 |
} |