12#if defined(__x86_64__)
14#elif defined(__aarch64__)
33inline constexpr std::array<std::uint32_t, 256> CRC32C_TABLE = [] {
34 std::array<std::uint32_t, 256> table{};
35 for (std::uint32_t i = 0; i < 256; ++i)
38 for (
int bit = 0; bit < 8; ++bit)
40 c = (c >> 1) ^ ((c & 1U) != 0 ? 0x82F63B78U : 0U);
47inline void putLe(std::uint8_t* out, std::uint64_t value,
const std::size_t bytes)
49 for (std::size_t i = 0; i < bytes; ++i)
51 out[i] =
static_cast<std::uint8_t
>(value);
56inline std::uint64_t getLe(
const std::uint8_t* in,
const std::size_t bytes)
58 std::uint64_t value = 0;
59 for (std::size_t i = bytes; i > 0; --i)
61 value = (value << 8) | in[i - 1];
75inline std::uint32_t crc32cTable(
const std::uint8_t* data,
const std::size_t length,
const std::uint32_t crc)
77 std::uint32_t c = ~crc;
78 for (std::size_t i = 0; i < length; ++i)
80 c = CRC32C_TABLE[(c ^ data[i]) & 0xFFU] ^ (c >> 8);
87#if defined(__x86_64__)
89inline bool hasCrc32cInstruction()
91 return __builtin_cpu_supports(
"sse4.2");
102__attribute__((target(
"sse4.2")))
inline std::uint32_t crc32cHardware(
const std::uint8_t* data, std::size_t length,
103 const std::uint32_t crc)
105 std::uint64_t c = ~crc;
106 for (; length >=
sizeof(std::uint64_t); length -=
sizeof(std::uint64_t), data +=
sizeof(std::uint64_t))
109 std::memcpy(&word, data,
sizeof(word));
110 c = _mm_crc32_u64(c, word);
112 auto c32 =
static_cast<std::uint32_t
>(c);
113 for (; length > 0; --length, ++data)
115 c32 = _mm_crc32_u8(c32, *data);
120#elif defined(__aarch64__)
122inline bool hasCrc32cInstruction()
124#if defined(__ARM_FEATURE_CRC32) || defined(__APPLE__)
126#elif defined(__linux__)
127 return (getauxval(AT_HWCAP) & HWCAP_CRC32) != 0;
141__attribute__((target(
"+crc")))
inline std::uint32_t crc32cHardware(
const std::uint8_t* data, std::size_t length,
142 const std::uint32_t crc)
144 std::uint32_t c = ~crc;
145 for (; length >=
sizeof(std::uint64_t); length -=
sizeof(std::uint64_t), data +=
sizeof(std::uint64_t))
148 std::memcpy(&word, data,
sizeof(word));
149 c = __crc32cd(c, word);
151 for (; length > 0; --length, ++data)
153 c = __crc32cb(c, *data);
160inline bool hasCrc32cInstruction()
165inline std::uint32_t crc32cHardware(
const std::uint8_t* data,
const std::size_t length,
const std::uint32_t crc)
167 return crc32cTable(data, length, crc);
183inline std::uint32_t
crc32c(
const std::uint8_t* data,
const std::size_t length,
const std::uint32_t crc = 0)
185 static const bool hardware = detail::hasCrc32cInstruction();
186 return hardware ? detail::crc32cHardware(data, length, crc) : detail::crc32cTable(data, length, crc);
214 std::vector<SnapshotGatewayRow>
rows;
255 std::size_t
encode(std::uint8_t* out)
const
258 detail::putLe(out,
VERSION, 2);
259 detail::putLe(out + 2, length, 4);
261 detail::putLe(out + 14,
static_cast<std::uint32_t
>(
leaderMemberId), 4);
262 out[18] =
static_cast<std::uint8_t
>(
groups.size());
266 group[0] =
static_cast<std::uint8_t
>(each.groupId);
267 putName(group + 1, each.groupName);
272 detail::putLe(group,
static_cast<std::uint32_t
>(
gateway->gatewaySourceId), 4);
273 detail::putLe(group + 4,
static_cast<std::uint32_t
>(
gateway->activeGatewayId), 4);
274 detail::putLe(group + 8,
static_cast<std::uint32_t
>(
gateway->highestConnectionId), 4);
275 detail::putLe(group + 12,
gateway->rows.size(), 2);
279 detail::putLe(row,
static_cast<std::uint32_t
>(each.gatewayId), 4);
280 row[4] = each.preferenceRank;
281 putName(row + 5, each.gatewayName);
295 static std::int32_t
version(
const std::uint8_t* in,
const std::size_t length)
297 return length < 2 ? -1 :
static_cast<std::int32_t
>(detail::getLe(in, 2));
307 static std::optional<SnapshotHeader>
decode(
const std::uint8_t* in,
const std::size_t length)
313 const std::uint64_t headerLength = detail::getLe(in + 2, 4);
314 const std::size_t groupCount = in[18];
316 if (headerLength > length || headerLength < applicationLength)
320 SnapshotHeader header{
static_cast<std::int64_t
>(detail::getLe(in + 6, 8)),
321 static_cast<std::int32_t
>(detail::getLe(in + 14, 4)),
325 for (std::size_t i = 0; i < groupCount; ++i)
330 if (headerLength == applicationLength)
338 const std::size_t rowCount = detail::getLe(group + 12, 2);
344 static_cast<std::int32_t
>(detail::getLe(group + 4, 4)),
345 static_cast<std::int32_t
>(detail::getLe(group + 8, 4)),
348 for (std::size_t i = 0; i < rowCount; ++i)
350 state.
rows.push_back(
351 SnapshotGatewayRow{
static_cast<std::int32_t
>(detail::getLe(row, 4)), row[4], getName(row + 5) });
354 header.gateway = std::move(state);
359 static void putName(std::uint8_t* out,
const std::string& name)
361 const std::size_t nameLength = std::min(name.size(),
NAME_LENGTH);
362 std::copy_n(name.data(), nameLength, out);
363 std::fill_n(out + nameLength,
NAME_LENGTH - nameLength, std::uint8_t{ 0 });
366 static std::string getName(
const std::uint8_t* in)
368 const auto* name =
reinterpret_cast<const char*
>(in);
369 return std::string(name, std::find(name, name +
NAME_LENGTH,
'\0') - name);
Definition ProxyProtocol.hpp:8
std::uint32_t crc32c(const std::uint8_t *data, const std::size_t length, const std::uint32_t crc=0)
Computes the CRC-32C of a span of bytes, as SnapshotEnd::crc32c carries it, or continues one: with th...
Definition Snapshot.hpp:183
constexpr std::uint16_t MAX_SNAPSHOT_RECORD_LENGTH
Definition Snapshot.hpp:29
Definition Snapshot.hpp:200
std::string gatewayName
at most 32 US-ASCII characters
Definition Snapshot.hpp:203
bool operator==(const SnapshotGatewayRow &) const =default
std::int32_t gatewayId
Definition Snapshot.hpp:201
std::uint8_t preferenceRank
Definition Snapshot.hpp:202
Definition Snapshot.hpp:210
std::vector< SnapshotGatewayRow > rows
every instance of the gateway, in list order
Definition Snapshot.hpp:214
std::int32_t gatewaySourceId
Definition Snapshot.hpp:211
bool operator==(const SnapshotGatewayState &) const =default
std::int32_t activeGatewayId
the current GatewayActive's, or SnapshotHeader::NO_GATEWAY
Definition Snapshot.hpp:212
std::int32_t highestConnectionId
the highest in the source's history
Definition Snapshot.hpp:213
Definition Snapshot.hpp:191
std::int32_t groupId
1 to MAX_GROUPS
Definition Snapshot.hpp:192
bool operator==(const SnapshotGroup &) const =default
std::string groupName
at most 32 US-ASCII characters
Definition Snapshot.hpp:193