bit_deserialization.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Copyright (c) 2019-2023 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 BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_BIT_DESERIALIZATION_HPP
  8. #define BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_BIT_DESERIALIZATION_HPP
  9. #include <asio2/bho/mysql/field_view.hpp>
  10. #include <asio2/bho/mysql/string_view.hpp>
  11. #include <asio2/bho/mysql/impl/internal/protocol/serialization.hpp>
  12. #include <asio2/bho/endian/conversion.hpp>
  13. #include <cstring>
  14. namespace bho {
  15. namespace mysql {
  16. namespace detail {
  17. // All BIT values come as binary values between 1 and 8 bytes length packed in string_lenenc's,
  18. // for both the text and the binary protocols. As the text protocol already unpacks the
  19. // string_lenenc layer, this function is in charge of just parsing the binary payload. The length of
  20. // the BIT value depends on how the type was defined in the table (e.g. BIT(14) will send a 2 byte
  21. // value; BIT(54) will send a 7 byte one). Values are sent as big-endian.
  22. inline deserialize_errc deserialize_bit(string_view from, field_view& to) noexcept
  23. {
  24. std::size_t num_bytes = from.size();
  25. if (num_bytes < 1 || num_bytes > 8)
  26. {
  27. return deserialize_errc::protocol_value_error;
  28. }
  29. unsigned char temp[8]{};
  30. unsigned char* dest = temp + sizeof(temp) - num_bytes;
  31. std::memcpy(dest, from.data(), num_bytes);
  32. to = field_view(endian::load_big_u64(temp));
  33. return deserialize_errc::ok;
  34. }
  35. } // namespace detail
  36. } // namespace mysql
  37. } // namespace bho
  38. #endif