window.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // This is a derivative work based on Zlib, copyright below:
  10. /*
  11. Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
  12. This software is provided 'as-is', without any express or implied
  13. warranty. In no event will the authors be held liable for any damages
  14. arising from the use of this software.
  15. Permission is granted to anyone to use this software for any purpose,
  16. including commercial applications, and to alter it and redistribute it
  17. freely, subject to the following restrictions:
  18. 1. The origin of this software must not be misrepresented; you must not
  19. claim that you wrote the original software. If you use this software
  20. in a product, an acknowledgment in the product documentation would be
  21. appreciated but is not required.
  22. 2. Altered source versions must be plainly marked as such, and must not be
  23. misrepresented as being the original software.
  24. 3. This notice may not be removed or altered from any source distribution.
  25. Jean-loup Gailly Mark Adler
  26. jloup@gzip.org madler@alumni.caltech.edu
  27. The data format used by the zlib library is described by RFCs (Request for
  28. Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
  29. (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
  30. */
  31. #ifndef BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP
  32. #define BOOST_BEAST_ZLIB_DETAIL_WINDOW_HPP
  33. #include <boost/assert.hpp>
  34. #include <boost/make_unique.hpp>
  35. #include <cstdint>
  36. #include <cstring>
  37. #include <memory>
  38. namespace boost {
  39. namespace beast {
  40. namespace zlib {
  41. namespace detail {
  42. class window
  43. {
  44. std::unique_ptr<std::uint8_t[]> p_;
  45. std::uint16_t i_ = 0;
  46. std::uint16_t size_ = 0;
  47. std::uint16_t capacity_ = 0;
  48. std::uint8_t bits_ = 0;
  49. public:
  50. int
  51. bits() const
  52. {
  53. return bits_;
  54. }
  55. unsigned
  56. capacity() const
  57. {
  58. return capacity_;
  59. }
  60. unsigned
  61. size() const
  62. {
  63. return size_;
  64. }
  65. void
  66. reset(int bits)
  67. {
  68. if(bits_ != bits)
  69. {
  70. p_.reset();
  71. bits_ = static_cast<std::uint8_t>(bits);
  72. capacity_ = 1U << bits_;
  73. }
  74. i_ = 0;
  75. size_ = 0;
  76. }
  77. void
  78. read(std::uint8_t* out, std::size_t pos, std::size_t n)
  79. {
  80. if(i_ >= size_)
  81. {
  82. // window is contiguous
  83. std::memcpy(out, &p_[i_ - pos], n);
  84. return;
  85. }
  86. auto i = ((i_ - pos) + capacity_) % capacity_;
  87. auto m = capacity_ - i;
  88. if(n <= m)
  89. {
  90. std::memcpy(out, &p_[i], n);
  91. return;
  92. }
  93. std::memcpy(out, &p_[i], m);
  94. out += m;
  95. std::memcpy(out, &p_[0], n - m);
  96. }
  97. void
  98. write(std::uint8_t const* in, std::size_t n)
  99. {
  100. if(! p_)
  101. p_ = boost::make_unique<
  102. std::uint8_t[]>(capacity_);
  103. if(n >= capacity_)
  104. {
  105. i_ = 0;
  106. size_ = capacity_;
  107. std::memcpy(&p_[0], in + (n - size_), size_);
  108. return;
  109. }
  110. if(i_ + n <= capacity_)
  111. {
  112. std::memcpy(&p_[i_], in, n);
  113. if(size_ >= capacity_ - n)
  114. size_ = capacity_;
  115. else
  116. size_ = static_cast<std::uint16_t>(size_ + n);
  117. i_ = static_cast<std::uint16_t>(
  118. (i_ + n) % capacity_);
  119. return;
  120. }
  121. auto m = capacity_ - i_;
  122. std::memcpy(&p_[i_], in, m);
  123. in += m;
  124. i_ = static_cast<std::uint16_t>(n - m);
  125. std::memcpy(&p_[0], in, i_);
  126. size_ = capacity_;
  127. }
  128. };
  129. } // detail
  130. } // zlib
  131. } // beast
  132. } // boost
  133. #endif