mask.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
  10. #define BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/type_traits.hpp>
  13. #include <boost/asio/buffer.hpp>
  14. #include <array>
  15. #include <climits>
  16. #include <cstdint>
  17. #include <random>
  18. #include <type_traits>
  19. namespace boost {
  20. namespace beast {
  21. namespace websocket {
  22. namespace detail {
  23. using prepared_key = std::array<unsigned char, 4>;
  24. inline
  25. void
  26. prepare_key(prepared_key& prepared, std::uint32_t key)
  27. {
  28. prepared[0] = (key >> 0) & 0xff;
  29. prepared[1] = (key >> 8) & 0xff;
  30. prepared[2] = (key >> 16) & 0xff;
  31. prepared[3] = (key >> 24) & 0xff;
  32. }
  33. template<std::size_t N>
  34. void
  35. rol(std::array<unsigned char, N>& v, std::size_t n)
  36. {
  37. auto v0 = v;
  38. for(std::size_t i = 0; i < v.size(); ++i )
  39. v[i] = v0[(i + n) % v.size()];
  40. }
  41. // Apply mask in place
  42. //
  43. inline
  44. void
  45. mask_inplace(boost::asio::mutable_buffer& b, prepared_key& key)
  46. {
  47. auto n = b.size();
  48. auto mask = key; // avoid aliasing
  49. auto p = static_cast<unsigned char*>(b.data());
  50. while(n >= 4)
  51. {
  52. for(int i = 0; i < 4; ++i)
  53. p[i] ^= mask[i];
  54. p += 4;
  55. n -= 4;
  56. }
  57. if(n > 0)
  58. {
  59. for(std::size_t i = 0; i < n; ++i)
  60. p[i] ^= mask[i];
  61. rol(key, n);
  62. }
  63. }
  64. // Apply mask in place
  65. //
  66. template<class MutableBuffers, class KeyType>
  67. void
  68. mask_inplace(MutableBuffers const& bs, KeyType& key)
  69. {
  70. for(boost::asio::mutable_buffer b :
  71. beast::detail::buffers_range(bs))
  72. mask_inplace(b, key);
  73. }
  74. } // detail
  75. } // websocket
  76. } // beast
  77. } // boost
  78. #endif