]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/websocket/detail/mask.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / websocket / detail / mask.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
11 #define BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <array>
16 #include <climits>
17 #include <cstdint>
18 #include <random>
19 #include <type_traits>
20
21 namespace boost {
22 namespace beast {
23 namespace websocket {
24 namespace detail {
25
26 // Pseudo-random source of mask keys
27 //
28 template<class Generator>
29 class maskgen_t
30 {
31 Generator g_;
32
33 public:
34 using result_type =
35 typename Generator::result_type;
36
37 maskgen_t();
38
39 result_type
40 operator()() noexcept;
41
42 void
43 rekey();
44 };
45
46 template<class Generator>
47 maskgen_t<Generator>::maskgen_t()
48 {
49 rekey();
50 }
51
52 template<class Generator>
53 auto
54 maskgen_t<Generator>::operator()() noexcept ->
55 result_type
56 {
57 for(;;)
58 if(auto key = g_())
59 return key;
60 }
61
62 template<class _>
63 void
64 maskgen_t<_>::rekey()
65 {
66 std::random_device rng;
67 #if 0
68 std::array<std::uint32_t, 32> e;
69 for(auto& i : e)
70 i = rng();
71 // VFALCO This constructor causes
72 // address sanitizer to fail, no idea why.
73 std::seed_seq ss(e.begin(), e.end());
74 g_.seed(ss);
75 #else
76 g_.seed(rng());
77 #endif
78 }
79
80 // VFALCO NOTE This generator has 5KB of state!
81 //using maskgen = maskgen_t<std::mt19937>;
82 using maskgen = maskgen_t<std::minstd_rand>;
83
84 //------------------------------------------------------------------------------
85
86 using prepared_key =
87 std::conditional<sizeof(void*) == 8,
88 std::uint64_t, std::uint32_t>::type;
89
90 inline
91 void
92 prepare_key(std::uint32_t& prepared, std::uint32_t key)
93 {
94 prepared = key;
95 }
96
97 inline
98 void
99 prepare_key(std::uint64_t& prepared, std::uint32_t key)
100 {
101 prepared =
102 (static_cast<std::uint64_t>(key) << 32) | key;
103 }
104
105 template<class T>
106 inline
107 typename std::enable_if<std::is_integral<T>::value, T>::type
108 ror(T t, unsigned n = 1)
109 {
110 auto constexpr bits =
111 static_cast<unsigned>(
112 sizeof(T) * CHAR_BIT);
113 n &= bits-1;
114 return static_cast<T>((t << (bits - n)) | (
115 static_cast<typename std::make_unsigned<T>::type>(t) >> n));
116 }
117
118 // 32-bit optimized
119 //
120 template<class = void>
121 void
122 mask_inplace_fast(
123 boost::asio::mutable_buffer const& b,
124 std::uint32_t& key)
125 {
126 auto n = b.size();
127 auto p = reinterpret_cast<std::uint8_t*>(b.data());
128 if(n >= sizeof(key))
129 {
130 // Bring p to 4-byte alignment
131 auto const i = reinterpret_cast<
132 std::uintptr_t>(p) & (sizeof(key)-1);
133 switch(i)
134 {
135 case 1: p[2] ^= static_cast<std::uint8_t>(key >> 16); BOOST_BEAST_FALLTHROUGH;
136 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8); BOOST_BEAST_FALLTHROUGH;
137 case 3: p[0] ^= static_cast<std::uint8_t>(key);
138 {
139 auto const d = static_cast<unsigned>(sizeof(key) - i);
140 key = ror(key, 8*d);
141 n -= d;
142 p += d;
143 BOOST_BEAST_FALLTHROUGH;
144 }
145 default:
146 break;
147 }
148 }
149
150 // Mask 4 bytes at a time
151 for(auto i = n / sizeof(key); i; --i)
152 {
153 *reinterpret_cast<
154 std::uint32_t*>(p) ^= key;
155 p += sizeof(key);
156 }
157
158 // Leftovers
159 n &= sizeof(key)-1;
160 switch(n)
161 {
162 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16); BOOST_BEAST_FALLTHROUGH;
163 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8); BOOST_BEAST_FALLTHROUGH;
164 case 1: p[0] ^= static_cast<std::uint8_t>(key);
165 key = ror(key, static_cast<unsigned>(8*n));
166 BOOST_BEAST_FALLTHROUGH;
167 default:
168 break;
169 }
170 }
171
172 // 64-bit optimized
173 //
174 template<class = void>
175 void
176 mask_inplace_fast(
177 boost::asio::mutable_buffer const& b,
178 std::uint64_t& key)
179 {
180 auto n = b.size();
181 auto p = reinterpret_cast<std::uint8_t*>(b.data());
182 if(n >= sizeof(key))
183 {
184 // Bring p to 8-byte alignment
185 auto const i = reinterpret_cast<
186 std::uintptr_t>(p) & (sizeof(key)-1);
187 switch(i)
188 {
189 case 1: p[6] ^= static_cast<std::uint8_t>(key >> 48);
190 case 2: p[5] ^= static_cast<std::uint8_t>(key >> 40);
191 case 3: p[4] ^= static_cast<std::uint8_t>(key >> 32);
192 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
193 case 5: p[2] ^= static_cast<std::uint8_t>(key >> 16);
194 case 6: p[1] ^= static_cast<std::uint8_t>(key >> 8);
195 case 7: p[0] ^= static_cast<std::uint8_t>(key);
196 {
197 auto const d = static_cast<
198 unsigned>(sizeof(key) - i);
199 key = ror(key, 8*d);
200 n -= d;
201 p += d;
202 }
203 default:
204 break;
205 }
206 }
207
208 // Mask 8 bytes at a time
209 for(auto i = n / sizeof(key); i; --i)
210 {
211 *reinterpret_cast<
212 std::uint64_t*>(p) ^= key;
213 p += sizeof(key);
214 }
215
216 // Leftovers
217 n &= sizeof(key)-1;
218 switch(n)
219 {
220 case 7: p[6] ^= static_cast<std::uint8_t>(key >> 48);
221 case 6: p[5] ^= static_cast<std::uint8_t>(key >> 40);
222 case 5: p[4] ^= static_cast<std::uint8_t>(key >> 32);
223 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
224 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16);
225 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8);
226 case 1: p[0] ^= static_cast<std::uint8_t>(key);
227 key = ror(key, static_cast<unsigned>(8*n));
228 default:
229 break;
230 }
231 }
232
233 inline
234 void
235 mask_inplace(
236 boost::asio::mutable_buffer const& b,
237 std::uint32_t& key)
238 {
239 mask_inplace_fast(b, key);
240 }
241
242 inline
243 void
244 mask_inplace(
245 boost::asio::mutable_buffer const& b,
246 std::uint64_t& key)
247 {
248 mask_inplace_fast(b, key);
249 }
250
251 // Apply mask in place
252 //
253 template<class MutableBuffers, class KeyType>
254 void
255 mask_inplace(
256 MutableBuffers const& bs, KeyType& key)
257 {
258 for(boost::asio::mutable_buffer b : bs)
259 mask_inplace(b, key);
260 }
261
262 } // detail
263 } // websocket
264 } // beast
265 } // boost
266
267 #endif