Fix serialization of vectors of more than 65535 elements
This commit is contained in:
@@ -41,7 +41,7 @@ namespace wivrn
|
||||
// Default port for server to listen, both TCP and UDP
|
||||
static const int default_port = 9757;
|
||||
|
||||
static constexpr int protocol_revision = 1;
|
||||
static constexpr int protocol_revision = 2;
|
||||
|
||||
enum class device_id : uint8_t
|
||||
{
|
||||
|
||||
@@ -89,15 +89,6 @@ struct hash_context
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
template <typename T, typename Enable = void>
|
||||
struct serialization_traits;
|
||||
|
||||
template <typename T>
|
||||
size_t serialized_size(const T & x)
|
||||
{
|
||||
return serialization_traits<T>::size(x);
|
||||
}
|
||||
|
||||
class deserialization_error : public std::runtime_error
|
||||
{
|
||||
static std::string hexdump(std::span<uint8_t> raw_data)
|
||||
@@ -132,6 +123,25 @@ public:
|
||||
std::runtime_error("Serialization error") {}
|
||||
};
|
||||
|
||||
template <typename T, typename Enable = void>
|
||||
struct serialization_traits;
|
||||
|
||||
template <typename T>
|
||||
size_t serialized_size(const T & x)
|
||||
{
|
||||
return serialization_traits<T>::size(x);
|
||||
}
|
||||
|
||||
constexpr size_t serialized_size_of_size(size_t size)
|
||||
{
|
||||
if (size < 0x7fff)
|
||||
return sizeof(uint16_t);
|
||||
else if (size < 0x7fff'ffff)
|
||||
return 2 * sizeof(uint16_t);
|
||||
else
|
||||
throw serialization_error{};
|
||||
}
|
||||
|
||||
class serialization_packet
|
||||
{
|
||||
std::vector<uint8_t> buffer;
|
||||
@@ -173,6 +183,23 @@ public:
|
||||
serialization_traits<T>::serialize(value, *this);
|
||||
}
|
||||
|
||||
constexpr void serialize_size(size_t size)
|
||||
{
|
||||
if (size < 0x7fff)
|
||||
{
|
||||
serialize<uint16_t>(size);
|
||||
}
|
||||
else if (size < 0x7fff'ffff)
|
||||
{
|
||||
serialize<uint16_t>((size & 0x7fff) | 0x8000);
|
||||
serialize<uint16_t>(size >> 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw serialization_error{};
|
||||
}
|
||||
}
|
||||
|
||||
operator std::vector<std::span<uint8_t>> *()
|
||||
{
|
||||
return &this->operator std::vector<std::span<uint8_t>> &();
|
||||
@@ -251,6 +278,16 @@ public:
|
||||
return serialization_traits<T>::deserialize(*this);
|
||||
}
|
||||
|
||||
size_t deserialize_size()
|
||||
{
|
||||
size_t size = deserialize<uint16_t>();
|
||||
|
||||
if (size & 0x8000)
|
||||
size = (size & 0x7fff) | (deserialize<uint16_t>() << 15);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(T & v)
|
||||
{
|
||||
@@ -402,6 +439,7 @@ struct serialize_bits<T, std::tuple<std::integer_sequence<size_t, i, mid...>, Bi
|
||||
return (sizeof(boost::pfr::tuple_element_t<mid, T>) + ... + sizeof(boost::pfr::tuple_element_t<i, T>)) + serialize_bits<T, std::tuple<Bits...>>::size(t);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename T>
|
||||
@@ -555,8 +593,6 @@ struct serialization_traits<T, std::enable_if_t<std::is_aggregate_v<T> && !is_st
|
||||
template <>
|
||||
struct serialization_traits<std::string>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("string");
|
||||
@@ -564,17 +600,14 @@ struct serialization_traits<std::string>
|
||||
|
||||
static void serialize(const std::string & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(value.size());
|
||||
packet.serialize_size(value.size());
|
||||
packet.write(value.data(), value.size());
|
||||
}
|
||||
|
||||
static std::string deserialize(deserialization_packet & packet)
|
||||
{
|
||||
std::string value;
|
||||
size_t size = packet.deserialize<size_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
|
||||
packet.check_remaining_size(size);
|
||||
|
||||
@@ -589,15 +622,13 @@ struct serialization_traits<std::string>
|
||||
}
|
||||
static size_t size(const std::string & value)
|
||||
{
|
||||
return sizeof(size_type) + value.size();
|
||||
return serialized_size_of_size(value.size()) + value.size();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct serialization_traits<std::vector<T>>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("vector<");
|
||||
@@ -607,10 +638,7 @@ struct serialization_traits<std::vector<T>>
|
||||
|
||||
static void serialize(const std::vector<T> & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(value.size());
|
||||
packet.serialize_size(value.size());
|
||||
|
||||
if constexpr (serialization_traits<T>::is_trivially_serializable())
|
||||
{
|
||||
@@ -626,7 +654,7 @@ struct serialization_traits<std::vector<T>>
|
||||
static std::vector<T> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
std::vector<T> value;
|
||||
size_t size = packet.deserialize<size_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
|
||||
if constexpr (serialization_traits<T>::is_trivially_serializable())
|
||||
{
|
||||
@@ -653,10 +681,10 @@ struct serialization_traits<std::vector<T>>
|
||||
static size_t size(const std::vector<T> & value)
|
||||
{
|
||||
if constexpr (is_trivially_serializable())
|
||||
return sizeof(size_type) + value.size() * sizeof(T);
|
||||
return serialized_size_of_size(value.size()) + value.size() * sizeof(T);
|
||||
else
|
||||
{
|
||||
size_t res = sizeof(size_type);
|
||||
size_t res = serialized_size_of_size(value.size());
|
||||
for (const auto & item: value)
|
||||
res += serialized_size(item);
|
||||
return res;
|
||||
@@ -871,8 +899,6 @@ struct serialization_traits<std::chrono::duration<Rep, Period>>
|
||||
template <>
|
||||
struct serialization_traits<std::span<uint8_t>>
|
||||
{
|
||||
using size_type = uint16_t;
|
||||
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("span<uint8_t>");
|
||||
@@ -880,16 +906,13 @@ struct serialization_traits<std::span<uint8_t>>
|
||||
|
||||
static void serialize(const std::span<uint8_t> & value, serialization_packet & packet)
|
||||
{
|
||||
if (value.size() > std::numeric_limits<size_type>::max())
|
||||
throw serialization_error{};
|
||||
|
||||
packet.serialize<size_type>(value.size());
|
||||
packet.serialize_size(value.size());
|
||||
packet.write(value);
|
||||
}
|
||||
|
||||
static std::span<uint8_t> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
size_t size = packet.deserialize<size_type>();
|
||||
size_t size = packet.deserialize_size();
|
||||
return packet.read_span(size);
|
||||
}
|
||||
|
||||
@@ -898,9 +921,9 @@ struct serialization_traits<std::span<uint8_t>>
|
||||
return false;
|
||||
}
|
||||
|
||||
static size_t size(const std::span<uint8_t> & x)
|
||||
static size_t size(const std::span<uint8_t> & value)
|
||||
{
|
||||
return sizeof(size_type) + x.size_bytes();
|
||||
return serialized_size_of_size(value.size()) + value.size_bytes();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -957,7 +980,7 @@ struct serialization_traits<crypto::bignum>
|
||||
|
||||
static size_t size(const crypto::bignum & value)
|
||||
{
|
||||
return sizeof(serialization_traits<std::string>::size_type) + value.data_size();
|
||||
return serialized_size_of_size(value.data_size()) + value.data_size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -34,21 +34,21 @@ static_assert(hash(1234) == 0x1fabbdf10314a21d);
|
||||
static_assert(hash(-1234) == 0x77fa1c9653db5a84);
|
||||
static_assert(hash(0) == 0xaf63ad4c86019caf);
|
||||
|
||||
static_assert(serialization_type_hash<bool>() == hash("uint8"));
|
||||
static_assert(serialization_type_hash<int>() == hash("int32"));
|
||||
static_assert(serialization_type_hash<float>() == hash("float32"));
|
||||
static_assert(serialization_type_hash<double>() == hash("float64"));
|
||||
static_assert(serialization_type_hash<std::chrono::nanoseconds>() == hash("duration<int64,1/1000000000>"));
|
||||
static_assert(serialization_type_hash<std::optional<int>>() == hash("optional<int32>"));
|
||||
static_assert(serialization_type_hash<std::vector<int>>() == hash("vector<int32>"));
|
||||
static_assert(serialization_type_hash<std::array<int, 42>>() == hash("array<int32,42>"));
|
||||
static_assert(serialization_type_hash<std::string>() == hash("string"));
|
||||
static_assert(serialization_type_hash<std::variant<int, float>>() == hash("variant<int32,float32>"));
|
||||
static_assert(serialization_type_hash<bool>(0) == hash("uint8"));
|
||||
static_assert(serialization_type_hash<int>(0) == hash("int32"));
|
||||
static_assert(serialization_type_hash<float>(0) == hash("float32"));
|
||||
static_assert(serialization_type_hash<double>(0) == hash("float64"));
|
||||
static_assert(serialization_type_hash<std::chrono::nanoseconds>(0) == hash("duration<int64,1/1000000000>"));
|
||||
static_assert(serialization_type_hash<std::optional<int>>(0) == hash("optional<int32>"));
|
||||
static_assert(serialization_type_hash<std::vector<int>>(0) == hash("vector<int32>"));
|
||||
static_assert(serialization_type_hash<std::array<int, 42>>(0) == hash("array<int32,42>"));
|
||||
static_assert(serialization_type_hash<std::string>(0) == hash("string"));
|
||||
static_assert(serialization_type_hash<std::variant<int, float>>(0) == hash("variant<int32,float32>"));
|
||||
|
||||
struct test
|
||||
{
|
||||
int x;
|
||||
float y;
|
||||
};
|
||||
static_assert(serialization_type_hash<test>() == hash("structure{int32,float32}"));
|
||||
static_assert(serialization_type_hash<test>(0) == hash("structure{int32,float32}"));
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user