]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/websocket/detail/mask.ipp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / websocket / detail / mask.ipp
1 //
2 // Copyright (c) 2016-2019 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_IPP
11 #define BOOST_BEAST_WEBSOCKET_DETAIL_MASK_IPP
12
13 #include <boost/beast/websocket/detail/mask.hpp>
14
15 namespace boost {
16 namespace beast {
17 namespace websocket {
18 namespace detail {
19
20 void
21 prepare_key(prepared_key& prepared, std::uint32_t key)
22 {
23 prepared[0] = (key >> 0) & 0xff;
24 prepared[1] = (key >> 8) & 0xff;
25 prepared[2] = (key >> 16) & 0xff;
26 prepared[3] = (key >> 24) & 0xff;
27 }
28
29 inline
30 void
31 rol(prepared_key& v, std::size_t n)
32 {
33 auto v0 = v;
34 for(std::size_t i = 0; i < v.size(); ++i )
35 v[i] = v0[(i + n) % v.size()];
36 }
37
38 // Apply mask in place
39 //
40 void
41 mask_inplace(net::mutable_buffer const& b, prepared_key& key)
42 {
43 auto n = b.size();
44 auto const mask = key; // avoid aliasing
45 auto p = static_cast<unsigned char*>(b.data());
46 while(n >= 4)
47 {
48 for(int i = 0; i < 4; ++i)
49 p[i] ^= mask[i];
50 p += 4;
51 n -= 4;
52 }
53 if(n > 0)
54 {
55 for(std::size_t i = 0; i < n; ++i)
56 p[i] ^= mask[i];
57 rol(key, n);
58 }
59 }
60
61 } // detail
62 } // websocket
63 } // beast
64 } // boost
65
66 #endif