mask.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #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/buffers_range.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(net::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<
  67. class MutableBufferSequence,
  68. class KeyType>
  69. void
  70. mask_inplace(
  71. MutableBufferSequence const& buffers,
  72. KeyType& key)
  73. {
  74. for(net::mutable_buffer b :
  75. beast::buffers_range_ref(buffers))
  76. mask_inplace(b, key);
  77. }
  78. } // detail
  79. } // websocket
  80. } // beast
  81. } // boost
  82. #endif