read_buffer.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 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. #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_READ_BUFFER_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_READ_BUFFER_HPP
  9. #include <boost/mysql/client_errc.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/assert.hpp>
  12. #include <boost/config.hpp>
  13. #include <boost/core/span.hpp>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include <cstring>
  17. #include <vector>
  18. namespace boost {
  19. namespace mysql {
  20. namespace detail {
  21. // Custom buffer type optimized for read operations performed in the MySQL protocol.
  22. // The buffer is a single, resizable chunk of memory with four areas:
  23. // - Reserved area: messages that have already been read but are kept alive,
  24. // either because we need them or because we haven't cleaned them yet.
  25. // - Current message area: delimits the message we are currently parsing.
  26. // - Pending bytes area: bytes we've read but haven't been parsed into a message yet.
  27. // - Free area: free space for more bytes to be read.
  28. class read_buffer
  29. {
  30. std::vector<std::uint8_t> buffer_;
  31. std::size_t current_message_offset_{0};
  32. std::size_t pending_offset_{0};
  33. std::size_t free_offset_{0};
  34. std::size_t max_size_;
  35. public:
  36. read_buffer(std::size_t size, std::size_t max_size = static_cast<std::size_t>(-1))
  37. : buffer_(size), max_size_(max_size)
  38. {
  39. BOOST_ASSERT(size <= max_size_);
  40. }
  41. void reset() noexcept
  42. {
  43. current_message_offset_ = 0;
  44. pending_offset_ = 0;
  45. free_offset_ = 0;
  46. }
  47. // Whole buffer accessors
  48. const std::uint8_t* first() const noexcept { return buffer_.data(); }
  49. std::size_t size() const noexcept { return buffer_.size(); }
  50. std::size_t max_size() const { return max_size_; }
  51. // Area accessors
  52. std::uint8_t* reserved_first() noexcept { return buffer_.data(); }
  53. const std::uint8_t* reserved_first() const noexcept { return buffer_.data(); }
  54. std::uint8_t* current_message_first() noexcept { return buffer_.data() + current_message_offset_; }
  55. const std::uint8_t* current_message_first() const noexcept
  56. {
  57. return buffer_.data() + current_message_offset_;
  58. }
  59. std::uint8_t* pending_first() noexcept { return buffer_.data() + pending_offset_; }
  60. const std::uint8_t* pending_first() const noexcept { return buffer_.data() + pending_offset_; }
  61. std::uint8_t* free_first() noexcept { return buffer_.data() + free_offset_; }
  62. const std::uint8_t* free_first() const noexcept { return buffer_.data() + free_offset_; }
  63. std::size_t reserved_size() const noexcept { return current_message_offset_; }
  64. std::size_t current_message_size() const noexcept { return pending_offset_ - current_message_offset_; }
  65. std::size_t pending_size() const noexcept { return free_offset_ - pending_offset_; }
  66. std::size_t free_size() const noexcept { return buffer_.size() - free_offset_; }
  67. span<const std::uint8_t> reserved_area() const noexcept { return {reserved_first(), reserved_size()}; }
  68. span<const std::uint8_t> current_message() const noexcept
  69. {
  70. return {current_message_first(), current_message_size()};
  71. }
  72. span<const std::uint8_t> pending_area() const noexcept { return {pending_first(), pending_size()}; }
  73. span<std::uint8_t> free_area() noexcept { return {free_first(), free_size()}; }
  74. // Moves n bytes from the free to the processing area (e.g. they've been read)
  75. void move_to_pending(std::size_t length) noexcept
  76. {
  77. BOOST_ASSERT(length <= free_size());
  78. free_offset_ += length;
  79. }
  80. // Moves n bytes from the processing to the current message area
  81. void move_to_current_message(std::size_t length) noexcept
  82. {
  83. BOOST_ASSERT(length <= pending_size());
  84. pending_offset_ += length;
  85. }
  86. // Removes the last length bytes from the current message area,
  87. // effectively memmove'ing all subsequent bytes backwards.
  88. // Used to remove intermediate headers. length must be > 0
  89. void remove_current_message_last(std::size_t length) noexcept
  90. {
  91. BOOST_ASSERT(length <= current_message_size());
  92. BOOST_ASSERT(length > 0);
  93. std::memmove(pending_first() - length, pending_first(), pending_size());
  94. pending_offset_ -= length;
  95. free_offset_ -= length;
  96. }
  97. // Moves length bytes from the current message area to the reserved area
  98. // Used to move entire parsed messages or message headers
  99. void move_to_reserved(std::size_t length) noexcept
  100. {
  101. BOOST_ASSERT(length <= current_message_size());
  102. current_message_offset_ += length;
  103. }
  104. // Removes the reserved area, effectively memmove'ing evth backwards
  105. void remove_reserved() noexcept
  106. {
  107. if (reserved_size() > 0)
  108. {
  109. // If reserved_size() > 0, these ptrs should never be NULL
  110. void* to = buffer_.data();
  111. const void* from = current_message_first();
  112. BOOST_ASSERT(to != nullptr);
  113. BOOST_ASSERT(from != nullptr);
  114. std::size_t currmsg_size = current_message_size();
  115. std::size_t pend_size = pending_size();
  116. std::memmove(to, from, currmsg_size + pend_size);
  117. current_message_offset_ = 0;
  118. pending_offset_ = currmsg_size;
  119. free_offset_ = currmsg_size + pend_size;
  120. }
  121. }
  122. // Makes sure the free size is at least n bytes long; resizes the buffer if required
  123. BOOST_ATTRIBUTE_NODISCARD
  124. error_code grow_to_fit(std::size_t n)
  125. {
  126. if (free_size() < n)
  127. {
  128. std::size_t new_size = buffer_.size() + n - free_size();
  129. if (new_size > max_size_)
  130. return client_errc::max_buffer_size_exceeded;
  131. buffer_.resize(new_size);
  132. }
  133. return error_code();
  134. }
  135. };
  136. } // namespace detail
  137. } // namespace mysql
  138. } // namespace boost
  139. #endif