10 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP 11 #define BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP 13 #include <boost/asio/buffer.hpp> 18 #include <type_traits> 27 template<
class Generator>
34 typename Generator::result_type;
45 template<class Generator>
51 template<
class Generator>
65 std::random_device rng;
67 std::array<std::uint32_t, 32> e;
72 std::seed_seq ss(e.begin(), e.end());
86 std::conditional<
sizeof(
void*) == 8,
87 std::uint64_t, std::uint32_t>::type;
101 (
static_cast<std::uint64_t
>(key) << 32) | key;
106 typename std::enable_if<std::is_integral<T>::value, T>::type
109 auto constexpr bits =
110 static_cast<unsigned>(
111 sizeof(T) * CHAR_BIT);
113 return static_cast<T
>((t << (bits - n)) | (
114 static_cast<typename std::make_unsigned<T>::type
>(t) >> n));
119 template<
class =
void>
122 boost::asio::mutable_buffer
const& b,
125 using boost::asio::buffer_cast;
126 using boost::asio::buffer_size;
127 auto n = buffer_size(b);
128 auto p = buffer_cast<std::uint8_t*>(b);
132 auto const i =
reinterpret_cast< 133 std::uintptr_t
>(p) & (
sizeof(key)-1);
136 case 1: p[2] ^=
static_cast<std::uint8_t
>(key >> 16);
137 case 2: p[1] ^=
static_cast<std::uint8_t
>(key >> 8);
138 case 3: p[0] ^=
static_cast<std::uint8_t
>(key);
140 auto const d =
static_cast< 141 unsigned>(
sizeof(key) - i);
152 for(
auto i = n /
sizeof(key); i; --i)
155 std::uint32_t*
>(p) ^= key;
163 case 3: p[2] ^=
static_cast<std::uint8_t
>(key >> 16);
164 case 2: p[1] ^=
static_cast<std::uint8_t
>(key >> 8);
165 case 1: p[0] ^=
static_cast<std::uint8_t
>(key);
166 key =
ror(key, static_cast<unsigned>(8*n));
174 template<
class =
void>
177 boost::asio::mutable_buffer
const& b,
180 using boost::asio::buffer_cast;
181 using boost::asio::buffer_size;
182 auto n = buffer_size(b);
183 auto p = buffer_cast<std::uint8_t*>(b);
187 auto const i =
reinterpret_cast< 188 std::uintptr_t
>(p) & (
sizeof(key)-1);
191 case 1: p[6] ^=
static_cast<std::uint8_t
>(key >> 48);
192 case 2: p[5] ^=
static_cast<std::uint8_t
>(key >> 40);
193 case 3: p[4] ^=
static_cast<std::uint8_t
>(key >> 32);
194 case 4: p[3] ^=
static_cast<std::uint8_t
>(key >> 24);
195 case 5: p[2] ^=
static_cast<std::uint8_t
>(key >> 16);
196 case 6: p[1] ^=
static_cast<std::uint8_t
>(key >> 8);
197 case 7: p[0] ^=
static_cast<std::uint8_t
>(key);
199 auto const d =
static_cast< 200 unsigned>(
sizeof(key) - i);
211 for(
auto i = n /
sizeof(key); i; --i)
214 std::uint64_t*
>(p) ^= key;
222 case 7: p[6] ^=
static_cast<std::uint8_t
>(key >> 48);
223 case 6: p[5] ^=
static_cast<std::uint8_t
>(key >> 40);
224 case 5: p[4] ^=
static_cast<std::uint8_t
>(key >> 32);
225 case 4: p[3] ^=
static_cast<std::uint8_t
>(key >> 24);
226 case 3: p[2] ^=
static_cast<std::uint8_t
>(key >> 16);
227 case 2: p[1] ^=
static_cast<std::uint8_t
>(key >> 8);
228 case 1: p[0] ^=
static_cast<std::uint8_t
>(key);
229 key =
ror(key, static_cast<unsigned>(8*n));
238 boost::asio::mutable_buffer
const& b,
247 boost::asio::mutable_buffer
const& b,
255 template<
class MutableBuffers,
class KeyType>
258 MutableBuffers
const& bs, KeyType& key)
260 for(boost::asio::mutable_buffer b : bs)
Definition: async_result.hpp:20
void prepare_key(std::uint32_t &prepared, std::uint32_t key)
Definition: mask.hpp:91
result_type operator()() noexcept
Definition: mask.hpp:53
void mask_inplace_fast(boost::asio::mutable_buffer const &b, std::uint32_t &key)
Definition: mask.hpp:121
std::enable_if< std::is_integral< T >::value, T >::type ror(T t, unsigned n=1)
Definition: mask.hpp:107
maskgen_t()
Definition: mask.hpp:46
std::conditional< sizeof(void *)==8, std::uint64_t, std::uint32_t >::type prepared_key
Definition: mask.hpp:87
void rekey()
Definition: mask.hpp:63
void mask_inplace(boost::asio::mutable_buffer const &b, std::uint32_t &key)
Definition: mask.hpp:237
typename Generator::result_type result_type
Definition: mask.hpp:34