2020-08-10 12:51:09 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma GCC visibility push(internal)
|
|
|
|
namespace ircd::b64
|
|
|
|
{
|
|
|
|
constexpr char pad
|
|
|
|
{
|
|
|
|
'='
|
|
|
|
};
|
|
|
|
|
|
|
|
extern const u8
|
|
|
|
encode_permute_tab[64],
|
2020-08-10 18:00:20 +02:00
|
|
|
encode_shift_ctrl[64],
|
|
|
|
decode_permute_tab[64],
|
|
|
|
decode_permute_tab_le[64];
|
|
|
|
|
|
|
|
extern const i32
|
2020-08-12 02:13:13 +02:00
|
|
|
decode_tab[256];
|
2020-08-10 18:00:20 +02:00
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
static u8x64 decode_block(const u8x64 block, i64x8 &err) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-11 23:13:16 +02:00
|
|
|
template<const dictionary &>
|
2020-08-12 04:51:19 +02:00
|
|
|
static u8x64 encode_block(const u8x64 block) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
#pragma GCC visibility pop
|
|
|
|
|
|
|
|
decltype(ircd::b64::dict_rfc1421)
|
|
|
|
ircd::b64::dict_rfc1421
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
|
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
|
|
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::b64::dict_rfc3501)
|
|
|
|
ircd::b64::dict_rfc3501
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
|
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
|
|
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ',',
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::b64::dict_rfc4648)
|
|
|
|
ircd::b64::dict_rfc4648
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
|
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
|
|
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
|
|
|
|
};
|
|
|
|
|
2020-08-12 02:13:13 +02:00
|
|
|
decltype(ircd::b64::decode_tab)
|
|
|
|
ircd::b64::decode_tab
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 18:00:20 +02:00
|
|
|
{
|
2020-08-12 02:13:13 +02:00
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 7
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 15
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 23
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 31
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 39
|
|
|
|
0x40, 0x40, 0x40, 62, 63, 62, 0x40, 63, // 47
|
2020-08-10 18:00:20 +02:00
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, // 55
|
2020-08-12 02:13:13 +02:00
|
|
|
60, 61, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 63
|
|
|
|
0x40, 0, 1, 2, 3, 4, 5, 6, // 71
|
2020-08-10 18:00:20 +02:00
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, // 79
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, // 87
|
2020-08-12 02:13:13 +02:00
|
|
|
23, 24, 25, 0x40, 0x40, 0x40, 0x40, 63, // 95
|
|
|
|
0x40, 26, 27, 28, 29, 30, 31, 32, // 103
|
2020-08-10 18:00:20 +02:00
|
|
|
33, 34, 35, 36, 37, 38, 39, 40, // 111
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, // 119
|
2020-08-12 02:13:13 +02:00
|
|
|
49, 50, 51, 0x40, 0x40, 0x40, 0x40, 0x40, // 127
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
|
|
|
|
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, // 255
|
2020-08-10 18:00:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::b64::decode_permute_tab)
|
|
|
|
ircd::b64::decode_permute_tab
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 18:00:20 +02:00
|
|
|
{
|
|
|
|
6, 0, 1, 2, 9, 10, 4, 5, 12, 13, 14, 8, 22, 16, 17, 18,
|
|
|
|
25, 26, 20, 21, 28, 29, 30, 24, 38, 32, 33, 34, 41, 42, 36, 37,
|
|
|
|
44, 45, 46, 40, 54, 48, 49, 50, 57, 58, 52, 53, 60, 61, 62, 56,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// byte-order swapped for each 32-bit word from above
|
|
|
|
decltype(ircd::b64::decode_permute_tab_le)
|
|
|
|
ircd::b64::decode_permute_tab_le
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 18:00:20 +02:00
|
|
|
{
|
|
|
|
2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 18, 17, 16, 22,
|
|
|
|
21, 20, 26, 25, 24, 30, 29, 28, 34, 33, 32, 38, 37, 36, 42, 41,
|
|
|
|
40, 46, 45, 44, 50, 49, 48, 54, 53, 52, 58, 57, 56, 62, 61, 60,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
/// For vpermb
|
|
|
|
decltype(ircd::b64::encode_permute_tab)
|
|
|
|
ircd::b64::encode_permute_tab
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
0 + 1, 0 + 0, 0 + 2, 0 + 1, 3 + 1, 3 + 0, 3 + 2, 3 + 1,
|
|
|
|
6 + 1, 6 + 0, 6 + 2, 6 + 1, 9 + 1, 9 + 0, 9 + 2, 9 + 1,
|
|
|
|
12 + 1, 12 + 0, 12 + 2, 12 + 1, 15 + 1, 15 + 0, 15 + 2, 15 + 1,
|
|
|
|
18 + 1, 18 + 0, 18 + 2, 18 + 1, 21 + 1, 21 + 0, 21 + 2, 21 + 1,
|
|
|
|
24 + 1, 24 + 0, 24 + 2, 24 + 1, 27 + 1, 27 + 0, 27 + 2, 27 + 1,
|
|
|
|
30 + 1, 30 + 0, 30 + 2, 30 + 1, 33 + 1, 33 + 0, 33 + 2, 33 + 1,
|
|
|
|
36 + 1, 36 + 0, 36 + 2, 36 + 1, 39 + 1, 39 + 0, 39 + 2, 39 + 1,
|
|
|
|
42 + 1, 42 + 0, 42 + 2, 42 + 1, 45 + 1, 45 + 0, 45 + 2, 45 + 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// For vpmultishiftqb
|
|
|
|
decltype(ircd::b64::encode_shift_ctrl)
|
|
|
|
ircd::b64::encode_shift_ctrl
|
2020-08-12 11:55:35 +02:00
|
|
|
alignas(64)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
(10 + 0), ( 4 + 0), (22 + 0), (16 + 0), (10 + 32), ( 4 + 32), (22 + 32), (16 + 32),
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Encoding in to base64 at out. Out must be 1.33+ larger than in
|
|
|
|
/// padding is not present in the returned view.
|
2020-08-11 23:13:16 +02:00
|
|
|
template<const ircd::b64::dictionary &dict>
|
2020-08-10 12:51:09 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::b64::encode(const mutable_buffer &out,
|
|
|
|
const const_buffer &in)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const auto pads
|
|
|
|
{
|
|
|
|
(3 - size(in) % 3) % 3
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto encoded
|
|
|
|
{
|
2020-08-10 14:14:43 +02:00
|
|
|
encode_unpadded<dict>(out, in)
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
const char _pad[2]
|
|
|
|
{
|
|
|
|
pads > 0? pad: '\0',
|
|
|
|
pads > 1? pad: '\0',
|
|
|
|
};
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
auto len
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
size(encoded)
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 18:43:15 +02:00
|
|
|
len += copy(out + len, _pad[0]) & (pads > 0);
|
|
|
|
len += copy(out + len, _pad[1]) & (pads > 1);
|
2020-08-10 12:51:09 +02:00
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(out), len
|
|
|
|
};
|
|
|
|
}
|
2020-08-10 14:14:43 +02:00
|
|
|
template ircd::string_view ircd::b64::encode<ircd::b64::standard>(const mutable_buffer &, const const_buffer &) noexcept;
|
|
|
|
template ircd::string_view ircd::b64::encode<ircd::b64::urlsafe>(const mutable_buffer &, const const_buffer &) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
|
|
|
|
/// Encoding in to base64 at out. Out must be 1.33+ larger than in.
|
2020-08-11 23:13:16 +02:00
|
|
|
template<const ircd::b64::dictionary &dict>
|
2020-08-10 12:51:09 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::b64::encode_unpadded(const mutable_buffer &out,
|
|
|
|
const const_buffer &in)
|
|
|
|
noexcept
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
char *const __restrict__ dst
|
|
|
|
{
|
|
|
|
data(out)
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const __restrict__ src
|
|
|
|
{
|
|
|
|
data(in)
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
const size_t res_len
|
|
|
|
{
|
2020-08-21 08:10:42 +02:00
|
|
|
encode_unpadded_size(in)
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const size_t out_len
|
|
|
|
{
|
|
|
|
std::min(res_len, size(out))
|
|
|
|
};
|
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
u8x64 block {0};
|
2020-08-10 12:51:09 +02:00
|
|
|
size_t i(0), j(0);
|
2020-08-21 14:30:42 +02:00
|
|
|
for(; i < size(in) / 48 && i < out_len / 64; ++i)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
|
|
|
// Destination is indexed at 64 byte stride
|
|
|
|
const auto di
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
reinterpret_cast<u512x1_u *__restrict__>(dst + (i * 64))
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Source is indexed at 48 byte stride
|
|
|
|
const auto si
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
reinterpret_cast<const u512x1_u *__restrict__>(src + (i * 48))
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
block = *si;
|
|
|
|
block = encode_block<dict>(block);
|
|
|
|
*di = block;
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 01:02:56 +02:00
|
|
|
for(; i * 48 < size(in) && i * 64 < out_len; ++i)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
#if !defined(__AVX__)
|
|
|
|
#pragma clang loop unroll_count(2)
|
|
|
|
#endif
|
2020-08-12 01:02:56 +02:00
|
|
|
for(j = 0; j < 48 && i * 48 + j < size(in); ++j)
|
2020-08-12 04:51:19 +02:00
|
|
|
block[j] = src[i * 48 + j];
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-10 14:14:43 +02:00
|
|
|
block = encode_block<dict>(block);
|
2020-08-12 01:02:56 +02:00
|
|
|
for(j = 0; j < 64 && i * 64 + j < out_len; ++j)
|
2020-08-12 04:51:19 +02:00
|
|
|
dst[i * 64 + j] = block[j];
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(out), out_len
|
|
|
|
};
|
|
|
|
}
|
2020-08-31 20:38:38 +02:00
|
|
|
template ircd::string_view ircd::b64::encode_unpadded<ircd::b64::dict_rfc1421>(const mutable_buffer &, const const_buffer &) noexcept;
|
|
|
|
template ircd::string_view ircd::b64::encode_unpadded<ircd::b64::dict_rfc4648>(const mutable_buffer &, const const_buffer &) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
|
|
|
|
/// Returns 64 base64-encoded characters from 48 input characters. For any
|
|
|
|
/// inputs less than 48 characters trail with null characters; caller computes
|
|
|
|
/// result size. The following operations are performed on each triple of input
|
|
|
|
/// characters resulting in four output characters:
|
|
|
|
/// 0. in[0] / 4;
|
|
|
|
/// 1. (in[1] / 16) + ((in[0] * 16) % 64);
|
|
|
|
/// 2. ((in[1] * 4) % 64) + (in[2] / 64);
|
|
|
|
/// 3. in[2] % 64;
|
|
|
|
/// Based on https://arxiv.org/pdf/1910.05109 (and earlier work). No specific
|
|
|
|
/// intrinsics are used here; instead we recite a kotodama divination known
|
|
|
|
/// as "vector extensions" which by chance is visible to humans as C syntax.
|
2020-08-11 23:13:16 +02:00
|
|
|
template<const ircd::b64::dictionary &dict>
|
2020-08-10 12:51:09 +02:00
|
|
|
ircd::u8x64
|
|
|
|
ircd::b64::encode_block(const u8x64 in)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
size_t i, j, k;
|
|
|
|
|
|
|
|
// vpermb
|
|
|
|
u8x64 _perm;
|
|
|
|
for(k = 0; k < 64; ++k)
|
|
|
|
_perm[k] = in[encode_permute_tab[k]];
|
|
|
|
|
|
|
|
// TODO: currently does not achieve vpmultshiftqb on avx512vbmi
|
|
|
|
u64x8 sh[8], perm(_perm);
|
|
|
|
for(i = 0; i < 8; ++i)
|
|
|
|
for(j = 0; j < 8; ++j)
|
|
|
|
sh[i][j] = perm[i] >> encode_shift_ctrl[i * 8 + j];
|
|
|
|
|
|
|
|
// TODO: not needed if vpmultishiftqb is emitted.
|
|
|
|
for(i = 0; i < 8; ++i)
|
|
|
|
for(j = 0; j < 8; ++j)
|
|
|
|
sh[i][j] &= 0x3f;
|
|
|
|
|
2020-08-11 23:19:32 +02:00
|
|
|
u32x8 res[8];
|
|
|
|
for(i = 0; i < 8; ++i)
|
|
|
|
for(j = 0; j < 8; ++j)
|
|
|
|
res[i][j] = dict[sh[i][j]];
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
u8x64 ret;
|
|
|
|
for(i = 0, k = 0; i < 8; ++i)
|
|
|
|
for(j = 0; j < 8; ++j)
|
2020-08-11 23:19:32 +02:00
|
|
|
ret[k++] = res[i][j];
|
2020-08-10 12:51:09 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Base64 decode
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Decode base64 from in to the buffer at out; out can be 75% of the size
|
|
|
|
/// of in.
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::b64::decode(const mutable_buffer &out,
|
|
|
|
const string_view &in)
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
char *const __restrict__ dst
|
|
|
|
{
|
|
|
|
data(out)
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const __restrict__ src
|
|
|
|
{
|
|
|
|
data(in)
|
|
|
|
};
|
|
|
|
|
2020-08-12 01:02:56 +02:00
|
|
|
const size_t pads
|
|
|
|
{
|
|
|
|
endswith_count(in, '=')
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t in_len
|
|
|
|
{
|
|
|
|
size(in) - pads
|
|
|
|
};
|
|
|
|
|
2020-08-10 18:00:20 +02:00
|
|
|
const size_t out_len
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-08-12 01:02:56 +02:00
|
|
|
std::min(decode_size(in_len), size(out))
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-12 02:13:13 +02:00
|
|
|
i64x8 err {0};
|
|
|
|
u8x64 block {0};
|
2020-08-10 18:00:20 +02:00
|
|
|
size_t i(0), j(0);
|
2020-08-21 14:30:42 +02:00
|
|
|
for(; i < in_len / 64 && i < out_len / 48; ++i)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-08-10 18:00:20 +02:00
|
|
|
// Destination is indexed at 48 byte stride
|
|
|
|
const auto di
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
reinterpret_cast<u512x1_u *__restrict__>(dst + (i * 48))
|
2020-08-10 18:00:20 +02:00
|
|
|
};
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-10 18:00:20 +02:00
|
|
|
// Source is indexed at 64 byte stride
|
|
|
|
const auto si
|
|
|
|
{
|
2020-08-12 04:51:19 +02:00
|
|
|
reinterpret_cast<const u512x1_u *__restrict__>(src + (i * 64))
|
2020-08-10 18:00:20 +02:00
|
|
|
};
|
|
|
|
|
2020-08-12 02:13:13 +02:00
|
|
|
block = *si;
|
2020-08-12 04:51:19 +02:00
|
|
|
block = decode_block(block, err);
|
2020-08-12 02:13:13 +02:00
|
|
|
*di = block;
|
2020-08-10 18:00:20 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 01:02:56 +02:00
|
|
|
for(; i * 64 < in_len && i * 48 < out_len; ++i)
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-09-02 04:42:39 +02:00
|
|
|
u8x64 mask {0};
|
2020-08-12 01:02:56 +02:00
|
|
|
for(j = 0; j < 64 && i * 64 + j < in_len; ++j)
|
2020-09-02 04:42:39 +02:00
|
|
|
block[j] = src[i * 64 + j],
|
|
|
|
mask[j] = 0xff;
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-09-02 04:42:39 +02:00
|
|
|
i64x8 _err {0};
|
|
|
|
block = decode_block(block, _err);
|
2020-08-10 18:00:20 +02:00
|
|
|
for(j = 0; j < 48 && i * 48 + j < out_len; ++j)
|
2020-08-12 04:51:19 +02:00
|
|
|
dst[i * 48 + j] = block[j];
|
2020-08-12 02:13:13 +02:00
|
|
|
|
2020-09-02 04:42:39 +02:00
|
|
|
err |= _err & i64x8(mask);
|
2020-08-10 18:00:20 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 04:42:39 +02:00
|
|
|
if(unlikely(simd::sum_or(u64x8(err))[0]))
|
2020-08-12 02:13:13 +02:00
|
|
|
throw invalid_encoding
|
|
|
|
{
|
|
|
|
"base64 encoding contained invalid characters."
|
|
|
|
};
|
|
|
|
|
2020-08-10 18:00:20 +02:00
|
|
|
return string_view
|
2020-08-10 12:51:09 +02:00
|
|
|
{
|
2020-08-10 18:00:20 +02:00
|
|
|
data(out), out_len
|
2020-08-10 12:51:09 +02:00
|
|
|
};
|
|
|
|
}
|
2020-08-10 18:00:20 +02:00
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
/// Decode 64 base64 characters into a 48 byte result. The last 16 bytes of
|
2020-08-10 18:00:20 +02:00
|
|
|
/// the returned vector are undefined for the caller.
|
2020-08-12 04:51:19 +02:00
|
|
|
ircd::u8x64
|
|
|
|
ircd::b64::decode_block(const u8x64 block,
|
2020-09-02 04:42:39 +02:00
|
|
|
i64x8 &__restrict__ err)
|
2020-08-10 18:00:20 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
2020-08-12 02:13:13 +02:00
|
|
|
i32x16 vals[4];
|
2020-08-10 18:00:20 +02:00
|
|
|
for(i = 0; i < 4; ++i)
|
|
|
|
for(j = 0; j < 16; ++j)
|
2020-08-12 04:51:19 +02:00
|
|
|
vals[i][j] = block[i * 16 + j],
|
|
|
|
vals[i][j] = decode_tab[vals[i][j]];
|
2020-08-12 02:13:13 +02:00
|
|
|
|
2020-09-02 04:42:39 +02:00
|
|
|
u8x64 _err;
|
2020-08-12 04:51:19 +02:00
|
|
|
i32x16 errs;
|
2020-08-12 02:13:13 +02:00
|
|
|
for(i = 0; i < 4; ++i)
|
2020-08-12 04:51:19 +02:00
|
|
|
for(j = 0, errs = vals[i] >= 64; j < 16; ++j)
|
2020-09-02 04:42:39 +02:00
|
|
|
_err[i * 16 + j] = errs[j];
|
2020-08-10 18:00:20 +02:00
|
|
|
|
|
|
|
u16x32 al, ah;
|
2020-08-12 04:51:19 +02:00
|
|
|
for(i = 0; i < 4; ++i)
|
|
|
|
for(j = 0; j < 8; ++j)
|
|
|
|
ah[i * 8 + j] = vals[i][j * 2 + 0],
|
|
|
|
al[i * 8 + j] = vals[i][j * 2 + 1];
|
2020-08-10 18:00:20 +02:00
|
|
|
|
|
|
|
u16x32 a;
|
|
|
|
for(i = 0; i < 32; ++i)
|
2020-08-12 04:51:19 +02:00
|
|
|
a[i] = ah[i] * 64U + al[i];
|
2020-08-10 18:00:20 +02:00
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
i32x16 b;
|
|
|
|
for(i = 0, j = 0; i < 16; ++i, j += 2)
|
|
|
|
b[i] = a[j] * 4096U + a[j + 1];
|
2020-08-10 18:00:20 +02:00
|
|
|
|
2020-08-12 04:51:19 +02:00
|
|
|
u8x64 c(b), ret;
|
2020-08-10 18:00:20 +02:00
|
|
|
for(i = 0; i < 64; ++i)
|
2020-08-12 04:51:19 +02:00
|
|
|
ret[i] = c[decode_permute_tab_le[i]];
|
|
|
|
|
2020-09-02 04:42:39 +02:00
|
|
|
err |= i64x8(_err);
|
2020-08-12 04:51:19 +02:00
|
|
|
return ret;
|
2020-08-10 18:00:20 +02:00
|
|
|
}
|