ranges.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_RANGES_HPP
  32. #define BOOST_BEAST_ZLIB_DETAIL_RANGES_HPP
  33. #include <cstdint>
  34. #include <type_traits>
  35. namespace boost {
  36. namespace beast {
  37. namespace zlib {
  38. namespace detail {
  39. struct ranges
  40. {
  41. template<bool isConst>
  42. struct range
  43. {
  44. using iter_t =
  45. typename std::conditional<isConst,
  46. std::uint8_t const*,
  47. std::uint8_t*>::type;
  48. iter_t first;
  49. iter_t last;
  50. iter_t next;
  51. // total bytes in range
  52. std::size_t
  53. size() const
  54. {
  55. return last - first;
  56. }
  57. // bytes consumed
  58. std::size_t
  59. used() const
  60. {
  61. return next - first;
  62. }
  63. // bytes remaining
  64. std::size_t
  65. avail() const
  66. {
  67. return last - next;
  68. }
  69. };
  70. range<true> in;
  71. range<false> out;
  72. };
  73. // Clamp u to v where u and v are different types
  74. template<class U, class V>
  75. U clamp(U u, V v)
  76. {
  77. if(u > v)
  78. u = static_cast<U>(v);
  79. return u;
  80. }
  81. } // detail
  82. } // zlib
  83. } // beast
  84. } // boost
  85. #endif